48bd8383fc2d0a30fa18fdf78d06110b623339bc
[linux-2.6.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/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/async.h>
22 #include <linux/err.h>
23 #include <linux/mutex.h>
24 #include <linux/suspend.h>
25 #include <linux/delay.h>
26 #include <linux/of.h>
27 #include <linux/regulator/of_regulator.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/regulator/driver.h>
30 #include <linux/regulator/machine.h>
31 #include <linux/module.h>
32
33 #define CREATE_TRACE_POINTS
34 #include <trace/events/regulator.h>
35 #include <linux/debugfs.h>
36 #include <linux/seq_file.h>
37 #include <linux/uaccess.h>
38
39 #include "dummy.h"
40
41 #define rdev_crit(rdev, fmt, ...)                                       \
42         pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
43 #define rdev_err(rdev, fmt, ...)                                        \
44         pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
45 #define rdev_warn(rdev, fmt, ...)                                       \
46         pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47 #define rdev_info(rdev, fmt, ...)                                       \
48         pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
49 #define rdev_dbg(rdev, fmt, ...)                                        \
50         pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
51
52 static DEFINE_MUTEX(regulator_list_mutex);
53 static LIST_HEAD(regulator_list);
54 static LIST_HEAD(regulator_map_list);
55 static bool has_full_constraints;
56 static bool board_wants_dummy_regulator;
57
58 static struct dentry *debugfs_root;
59
60 /*
61  * struct regulator_map
62  *
63  * Used to provide symbolic supply names to devices.
64  */
65 struct regulator_map {
66         struct list_head list;
67         const char *dev_name;   /* The dev_name() for the consumer */
68         const char *supply;
69         struct regulator_dev *regulator;
70 };
71
72 /*
73  * struct regulator
74  *
75  * One for each consumer device.
76  */
77 struct regulator {
78         struct device *dev;
79         struct list_head list;
80         int uA_load;
81         int min_uV;
82         int max_uV;
83         char *supply_name;
84         struct device_attribute dev_attr;
85         struct regulator_dev *rdev;
86         struct dentry *debugfs;
87 };
88
89 static int _regulator_is_enabled(struct regulator_dev *rdev);
90 static int _regulator_disable(struct regulator_dev *rdev);
91 static int _regulator_enable(struct regulator_dev *rdev);
92 static int _regulator_get_enable_time(struct regulator_dev *rdev);
93 static int _regulator_get_voltage(struct regulator_dev *rdev);
94 static int _regulator_get_current_limit(struct regulator_dev *rdev);
95 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
96 static void _notifier_call_chain(struct regulator_dev *rdev,
97                                   unsigned long event, void *data);
98 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
99                                      int min_uV, int max_uV);
100 static struct regulator *create_regulator(struct regulator_dev *rdev,
101                                           struct device *dev,
102                                           const char *supply_name);
103
104 static const char *rdev_get_name(struct regulator_dev *rdev)
105 {
106         if (rdev->constraints && rdev->constraints->name)
107                 return rdev->constraints->name;
108         else if (rdev->desc->name)
109                 return rdev->desc->name;
110         else
111                 return "";
112 }
113
114 /* gets the regulator for a given consumer device */
115 static struct regulator *get_device_regulator(struct device *dev)
116 {
117         struct regulator *regulator = NULL;
118         struct regulator_dev *rdev;
119
120         mutex_lock(&regulator_list_mutex);
121         list_for_each_entry(rdev, &regulator_list, list) {
122                 mutex_lock(&rdev->mutex);
123                 list_for_each_entry(regulator, &rdev->consumer_list, list) {
124                         if (regulator->dev == dev) {
125                                 mutex_unlock(&rdev->mutex);
126                                 mutex_unlock(&regulator_list_mutex);
127                                 return regulator;
128                         }
129                 }
130                 mutex_unlock(&rdev->mutex);
131         }
132         mutex_unlock(&regulator_list_mutex);
133         return NULL;
134 }
135
136 /**
137  * of_get_regulator - get a regulator device node based on supply name
138  * @dev: Device pointer for the consumer (of regulator) device
139  * @supply: regulator supply name
140  *
141  * Extract the regulator device node corresponding to the supply name.
142  * retruns the device node corresponding to the regulator if found, else
143  * returns NULL.
144  */
145 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
146 {
147         struct device_node *regnode = NULL;
148         char prop_name[32]; /* 32 is max size of property name */
149
150         dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
151
152         snprintf(prop_name, 32, "%s-supply", supply);
153         regnode = of_parse_phandle(dev->of_node, prop_name, 0);
154
155         if (!regnode) {
156                 dev_dbg(dev, "Looking up %s property in node %s failed",
157                                 prop_name, dev->of_node->full_name);
158                 return NULL;
159         }
160         return regnode;
161 }
162
163 /* Platform voltage constraint check */
164 static int regulator_check_voltage(struct regulator_dev *rdev,
165                                    int *min_uV, int *max_uV)
166 {
167         BUG_ON(*min_uV > *max_uV);
168
169         if (!rdev->constraints) {
170                 rdev_err(rdev, "no constraints\n");
171                 return -ENODEV;
172         }
173         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
174                 rdev_err(rdev, "operation not allowed\n");
175                 return -EPERM;
176         }
177
178         if (*max_uV > rdev->constraints->max_uV)
179                 *max_uV = rdev->constraints->max_uV;
180         if (*min_uV < rdev->constraints->min_uV)
181                 *min_uV = rdev->constraints->min_uV;
182
183         if (*min_uV > *max_uV) {
184                 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
185                          *min_uV, *max_uV);
186                 return -EINVAL;
187         }
188
189         return 0;
190 }
191
192 /* Make sure we select a voltage that suits the needs of all
193  * regulator consumers
194  */
195 static int regulator_check_consumers(struct regulator_dev *rdev,
196                                      int *min_uV, int *max_uV)
197 {
198         struct regulator *regulator;
199
200         list_for_each_entry(regulator, &rdev->consumer_list, list) {
201                 /*
202                  * Assume consumers that didn't say anything are OK
203                  * with anything in the constraint range.
204                  */
205                 if (!regulator->min_uV && !regulator->max_uV)
206                         continue;
207
208                 if (*max_uV > regulator->max_uV)
209                         *max_uV = regulator->max_uV;
210                 if (*min_uV < regulator->min_uV)
211                         *min_uV = regulator->min_uV;
212         }
213
214         if (*min_uV > *max_uV)
215                 return -EINVAL;
216
217         return 0;
218 }
219
220 /* current constraint check */
221 static int regulator_check_current_limit(struct regulator_dev *rdev,
222                                         int *min_uA, int *max_uA)
223 {
224         BUG_ON(*min_uA > *max_uA);
225
226         if (!rdev->constraints) {
227                 rdev_err(rdev, "no constraints\n");
228                 return -ENODEV;
229         }
230         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
231                 rdev_err(rdev, "operation not allowed\n");
232                 return -EPERM;
233         }
234
235         if (*max_uA > rdev->constraints->max_uA)
236                 *max_uA = rdev->constraints->max_uA;
237         if (*min_uA < rdev->constraints->min_uA)
238                 *min_uA = rdev->constraints->min_uA;
239
240         if (*min_uA > *max_uA) {
241                 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
242                          *min_uA, *max_uA);
243                 return -EINVAL;
244         }
245
246         return 0;
247 }
248
249 /* operating mode constraint check */
250 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
251 {
252         switch (*mode) {
253         case REGULATOR_MODE_FAST:
254         case REGULATOR_MODE_NORMAL:
255         case REGULATOR_MODE_IDLE:
256         case REGULATOR_MODE_STANDBY:
257                 break;
258         default:
259                 rdev_err(rdev, "invalid mode %x specified\n", *mode);
260                 return -EINVAL;
261         }
262
263         if (!rdev->constraints) {
264                 rdev_err(rdev, "no constraints\n");
265                 return -ENODEV;
266         }
267         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
268                 rdev_err(rdev, "operation not allowed\n");
269                 return -EPERM;
270         }
271
272         /* The modes are bitmasks, the most power hungry modes having
273          * the lowest values. If the requested mode isn't supported
274          * try higher modes. */
275         while (*mode) {
276                 if (rdev->constraints->valid_modes_mask & *mode)
277                         return 0;
278                 *mode /= 2;
279         }
280
281         return -EINVAL;
282 }
283
284 /* dynamic regulator mode switching constraint check */
285 static int regulator_check_drms(struct regulator_dev *rdev)
286 {
287         if (!rdev->constraints) {
288                 rdev_err(rdev, "no constraints\n");
289                 return -ENODEV;
290         }
291         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
292                 rdev_err(rdev, "operation not allowed\n");
293                 return -EPERM;
294         }
295         return 0;
296 }
297
298 static ssize_t device_requested_uA_show(struct device *dev,
299                              struct device_attribute *attr, char *buf)
300 {
301         struct regulator *regulator;
302
303         regulator = get_device_regulator(dev);
304         if (regulator == NULL)
305                 return 0;
306
307         return sprintf(buf, "%d\n", regulator->uA_load);
308 }
309
310 static ssize_t regulator_uV_set(struct device *dev,
311         struct device_attribute *attr, const char *buf, size_t count)
312 {
313         struct regulator_dev *rdev = dev_get_drvdata(dev);
314         int ret;
315         int min_uV;
316         int max_uV = rdev->constraints->max_uV;
317         char *p = (char *)buf;
318
319         min_uV = memparse(p, &p);
320         mutex_lock(&rdev->mutex);
321
322         /* sanity check */
323         if (!rdev->desc->ops->set_voltage &&
324                 !rdev->desc->ops->set_voltage_sel) {
325                 rdev_err(rdev, "The operation is not supported\n");
326                 goto out;
327         }
328
329         /* constraints check */
330         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
331         if (ret < 0) {
332                 rdev_err(rdev, "Voltage is out of range min:max= %d:%d\n",
333                         rdev->constraints->min_uV, rdev->constraints->max_uV);
334                 goto out;
335         }
336
337         /* Consumer check */
338         ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
339         if (ret < 0) {
340                 rdev_warn(rdev, "new voltage is out-range for some consumer\n");
341                 rdev_warn(rdev, "min: max = %d:%d\n", min_uV, max_uV);
342         }
343
344         rdev_info(rdev, "Setting voltage min:max = %d:%d\n", min_uV, max_uV);
345         ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
346         if (ret < 0)
347                 rdev_warn(rdev, "Can not set voltage %d:%d\n",  min_uV, max_uV);
348
349 out:
350         mutex_unlock(&rdev->mutex);
351         return count;
352 }
353
354 static ssize_t regulator_uV_show(struct device *dev,
355                                 struct device_attribute *attr, char *buf)
356 {
357         struct regulator_dev *rdev = dev_get_drvdata(dev);
358         ssize_t ret;
359
360         mutex_lock(&rdev->mutex);
361         ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
362         mutex_unlock(&rdev->mutex);
363
364         return ret;
365 }
366 static DEVICE_ATTR(microvolts, 0666, regulator_uV_show, regulator_uV_set);
367
368 static ssize_t regulator_uA_show(struct device *dev,
369                                 struct device_attribute *attr, char *buf)
370 {
371         struct regulator_dev *rdev = dev_get_drvdata(dev);
372
373         return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
374 }
375 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
376
377 static ssize_t regulator_name_show(struct device *dev,
378                              struct device_attribute *attr, char *buf)
379 {
380         struct regulator_dev *rdev = dev_get_drvdata(dev);
381
382         return sprintf(buf, "%s\n", rdev_get_name(rdev));
383 }
384
385 static ssize_t regulator_print_opmode(char *buf, int mode)
386 {
387         switch (mode) {
388         case REGULATOR_MODE_FAST:
389                 return sprintf(buf, "fast\n");
390         case REGULATOR_MODE_NORMAL:
391                 return sprintf(buf, "normal\n");
392         case REGULATOR_MODE_IDLE:
393                 return sprintf(buf, "idle\n");
394         case REGULATOR_MODE_STANDBY:
395                 return sprintf(buf, "standby\n");
396         }
397         return sprintf(buf, "unknown\n");
398 }
399
400 static ssize_t regulator_opmode_show(struct device *dev,
401                                     struct device_attribute *attr, char *buf)
402 {
403         struct regulator_dev *rdev = dev_get_drvdata(dev);
404
405         return regulator_print_opmode(buf, _regulator_get_mode(rdev));
406 }
407 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
408
409 static ssize_t regulator_print_state(char *buf, int state)
410 {
411         if (state > 0)
412                 return sprintf(buf, "enabled\n");
413         else if (state == 0)
414                 return sprintf(buf, "disabled\n");
415         else
416                 return sprintf(buf, "unknown\n");
417 }
418
419 static ssize_t regulator_state_show(struct device *dev,
420                                    struct device_attribute *attr, char *buf)
421 {
422         struct regulator_dev *rdev = dev_get_drvdata(dev);
423         ssize_t ret;
424
425         mutex_lock(&rdev->mutex);
426         ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
427         mutex_unlock(&rdev->mutex);
428
429         return ret;
430 }
431
432 static ssize_t regulator_state_set(struct device *dev,
433                    struct device_attribute *attr, const char *buf, size_t count)
434 {
435         struct regulator_dev *rdev = dev_get_drvdata(dev);
436         int ret;
437         bool enabled;
438
439         if ((*buf == 'E') || (*buf == 'e'))
440                 enabled = true;
441         else if ((*buf == 'D') || (*buf == 'd'))
442                 enabled = false;
443         else
444                 return -EINVAL;
445
446         if ((_regulator_is_enabled(rdev) && enabled) ||
447                 (!_regulator_is_enabled(rdev) && !enabled))
448                 return count;
449
450         mutex_lock(&rdev->mutex);
451         if (enabled) {
452                 int delay = 0;
453                 if (!rdev->desc->ops->enable) {
454                         ret = -EINVAL;
455                         goto end;
456                 }
457                 ret = _regulator_get_enable_time(rdev);
458                 if (ret >= 0)
459                         delay = ret;
460                 ret = rdev->desc->ops->enable(rdev);
461                 if (ret < 0) {
462                         rdev_warn(rdev, "enable() failed: %d\n", ret);
463                         goto end;
464                 }
465                 if (delay >= 1000) {
466                         mdelay(delay / 1000);
467                         udelay(delay % 1000);
468                 } else if (delay) {
469                         udelay(delay);
470                 }
471         } else {
472                 if (!rdev->desc->ops->disable) {
473                         ret = -EINVAL;
474                         goto end;
475                 }
476                 ret = rdev->desc->ops->disable(rdev);
477                 if (ret < 0) {
478                         rdev_warn(rdev, "disable() failed: %d\n", ret);
479                         goto end;
480                 }
481         }
482
483 end:
484         mutex_unlock(&rdev->mutex);
485         if (ret < 0)
486                 return ret;
487         return count;
488 }
489 static DEVICE_ATTR(state, 0644, regulator_state_show, regulator_state_set);
490
491 static ssize_t regulator_status_show(struct device *dev,
492                                    struct device_attribute *attr, char *buf)
493 {
494         struct regulator_dev *rdev = dev_get_drvdata(dev);
495         int status;
496         char *label;
497
498         status = rdev->desc->ops->get_status(rdev);
499         if (status < 0)
500                 return status;
501
502         switch (status) {
503         case REGULATOR_STATUS_OFF:
504                 label = "off";
505                 break;
506         case REGULATOR_STATUS_ON:
507                 label = "on";
508                 break;
509         case REGULATOR_STATUS_ERROR:
510                 label = "error";
511                 break;
512         case REGULATOR_STATUS_FAST:
513                 label = "fast";
514                 break;
515         case REGULATOR_STATUS_NORMAL:
516                 label = "normal";
517                 break;
518         case REGULATOR_STATUS_IDLE:
519                 label = "idle";
520                 break;
521         case REGULATOR_STATUS_STANDBY:
522                 label = "standby";
523                 break;
524         default:
525                 return -ERANGE;
526         }
527
528         return sprintf(buf, "%s\n", label);
529 }
530 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
531
532 static ssize_t regulator_min_uA_show(struct device *dev,
533                                     struct device_attribute *attr, char *buf)
534 {
535         struct regulator_dev *rdev = dev_get_drvdata(dev);
536
537         if (!rdev->constraints)
538                 return sprintf(buf, "constraint not defined\n");
539
540         return sprintf(buf, "%d\n", rdev->constraints->min_uA);
541 }
542 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
543
544 static ssize_t regulator_max_uA_show(struct device *dev,
545                                     struct device_attribute *attr, char *buf)
546 {
547         struct regulator_dev *rdev = dev_get_drvdata(dev);
548
549         if (!rdev->constraints)
550                 return sprintf(buf, "constraint not defined\n");
551
552         return sprintf(buf, "%d\n", rdev->constraints->max_uA);
553 }
554 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
555
556 static ssize_t regulator_min_uV_show(struct device *dev,
557                                     struct device_attribute *attr, char *buf)
558 {
559         struct regulator_dev *rdev = dev_get_drvdata(dev);
560
561         if (!rdev->constraints)
562                 return sprintf(buf, "constraint not defined\n");
563
564         return sprintf(buf, "%d\n", rdev->constraints->min_uV);
565 }
566 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
567
568 static ssize_t regulator_max_uV_show(struct device *dev,
569                                     struct device_attribute *attr, char *buf)
570 {
571         struct regulator_dev *rdev = dev_get_drvdata(dev);
572
573         if (!rdev->constraints)
574                 return sprintf(buf, "constraint not defined\n");
575
576         return sprintf(buf, "%d\n", rdev->constraints->max_uV);
577 }
578 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
579
580 static ssize_t regulator_total_uA_show(struct device *dev,
581                                       struct device_attribute *attr, char *buf)
582 {
583         struct regulator_dev *rdev = dev_get_drvdata(dev);
584         struct regulator *regulator;
585         int uA = 0;
586
587         mutex_lock(&rdev->mutex);
588         list_for_each_entry(regulator, &rdev->consumer_list, list)
589                 uA += regulator->uA_load;
590         mutex_unlock(&rdev->mutex);
591         return sprintf(buf, "%d\n", uA);
592 }
593 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
594
595 static ssize_t regulator_num_users_show(struct device *dev,
596                                       struct device_attribute *attr, char *buf)
597 {
598         struct regulator_dev *rdev = dev_get_drvdata(dev);
599         return sprintf(buf, "%d\n", rdev->use_count);
600 }
601
602 static ssize_t regulator_type_show(struct device *dev,
603                                   struct device_attribute *attr, char *buf)
604 {
605         struct regulator_dev *rdev = dev_get_drvdata(dev);
606
607         switch (rdev->desc->type) {
608         case REGULATOR_VOLTAGE:
609                 return sprintf(buf, "voltage\n");
610         case REGULATOR_CURRENT:
611                 return sprintf(buf, "current\n");
612         }
613         return sprintf(buf, "unknown\n");
614 }
615
616 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
617                                 struct device_attribute *attr, char *buf)
618 {
619         struct regulator_dev *rdev = dev_get_drvdata(dev);
620
621         return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
622 }
623 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
624                 regulator_suspend_mem_uV_show, NULL);
625
626 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
627                                 struct device_attribute *attr, char *buf)
628 {
629         struct regulator_dev *rdev = dev_get_drvdata(dev);
630
631         return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
632 }
633 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
634                 regulator_suspend_disk_uV_show, NULL);
635
636 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
637                                 struct device_attribute *attr, char *buf)
638 {
639         struct regulator_dev *rdev = dev_get_drvdata(dev);
640
641         return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
642 }
643 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
644                 regulator_suspend_standby_uV_show, NULL);
645
646 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
647                                 struct device_attribute *attr, char *buf)
648 {
649         struct regulator_dev *rdev = dev_get_drvdata(dev);
650
651         return regulator_print_opmode(buf,
652                 rdev->constraints->state_mem.mode);
653 }
654 static DEVICE_ATTR(suspend_mem_mode, 0444,
655                 regulator_suspend_mem_mode_show, NULL);
656
657 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
658                                 struct device_attribute *attr, char *buf)
659 {
660         struct regulator_dev *rdev = dev_get_drvdata(dev);
661
662         return regulator_print_opmode(buf,
663                 rdev->constraints->state_disk.mode);
664 }
665 static DEVICE_ATTR(suspend_disk_mode, 0444,
666                 regulator_suspend_disk_mode_show, NULL);
667
668 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
669                                 struct device_attribute *attr, char *buf)
670 {
671         struct regulator_dev *rdev = dev_get_drvdata(dev);
672
673         return regulator_print_opmode(buf,
674                 rdev->constraints->state_standby.mode);
675 }
676 static DEVICE_ATTR(suspend_standby_mode, 0444,
677                 regulator_suspend_standby_mode_show, NULL);
678
679 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
680                                    struct device_attribute *attr, char *buf)
681 {
682         struct regulator_dev *rdev = dev_get_drvdata(dev);
683
684         return regulator_print_state(buf,
685                         rdev->constraints->state_mem.enabled);
686 }
687 static DEVICE_ATTR(suspend_mem_state, 0444,
688                 regulator_suspend_mem_state_show, NULL);
689
690 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
691                                    struct device_attribute *attr, char *buf)
692 {
693         struct regulator_dev *rdev = dev_get_drvdata(dev);
694
695         return regulator_print_state(buf,
696                         rdev->constraints->state_disk.enabled);
697 }
698 static DEVICE_ATTR(suspend_disk_state, 0444,
699                 regulator_suspend_disk_state_show, NULL);
700
701 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
702                                    struct device_attribute *attr, char *buf)
703 {
704         struct regulator_dev *rdev = dev_get_drvdata(dev);
705
706         return regulator_print_state(buf,
707                         rdev->constraints->state_standby.enabled);
708 }
709 static DEVICE_ATTR(suspend_standby_state, 0444,
710                 regulator_suspend_standby_state_show, NULL);
711
712
713 /*
714  * These are the only attributes are present for all regulators.
715  * Other attributes are a function of regulator functionality.
716  */
717 static struct device_attribute regulator_dev_attrs[] = {
718         __ATTR(name, 0444, regulator_name_show, NULL),
719         __ATTR(num_users, 0444, regulator_num_users_show, NULL),
720         __ATTR(type, 0444, regulator_type_show, NULL),
721         __ATTR_NULL,
722 };
723
724 static void regulator_dev_release(struct device *dev)
725 {
726         struct regulator_dev *rdev = dev_get_drvdata(dev);
727         kfree(rdev);
728 }
729
730 static struct class regulator_class = {
731         .name = "regulator",
732         .dev_release = regulator_dev_release,
733         .dev_attrs = regulator_dev_attrs,
734 };
735
736 /* Calculate the new optimum regulator operating mode based on the new total
737  * consumer load. All locks held by caller */
738 static void drms_uA_update(struct regulator_dev *rdev)
739 {
740         struct regulator *sibling;
741         int current_uA = 0, output_uV, input_uV, err;
742         unsigned int mode;
743
744         err = regulator_check_drms(rdev);
745         if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
746             (!rdev->desc->ops->get_voltage &&
747              !rdev->desc->ops->get_voltage_sel) ||
748             !rdev->desc->ops->set_mode)
749                 return;
750
751         /* get output voltage */
752         output_uV = _regulator_get_voltage(rdev);
753         if (output_uV <= 0)
754                 return;
755
756         /* get input voltage */
757         input_uV = 0;
758         if (rdev->supply)
759                 input_uV = _regulator_get_voltage(rdev);
760         if (input_uV <= 0)
761                 input_uV = rdev->constraints->input_uV;
762         if (input_uV <= 0)
763                 return;
764
765         /* calc total requested load */
766         list_for_each_entry(sibling, &rdev->consumer_list, list)
767                 current_uA += sibling->uA_load;
768
769         /* now get the optimum mode for our new total regulator load */
770         mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
771                                                   output_uV, current_uA);
772
773         /* check the new mode is allowed */
774         err = regulator_mode_constrain(rdev, &mode);
775         if (err == 0)
776                 rdev->desc->ops->set_mode(rdev, mode);
777 }
778
779 static int suspend_set_state(struct regulator_dev *rdev,
780         struct regulator_state *rstate)
781 {
782         int ret = 0;
783         bool can_set_state;
784
785         can_set_state = rdev->desc->ops->set_suspend_enable &&
786                 rdev->desc->ops->set_suspend_disable;
787
788         /* If we have no suspend mode configration don't set anything;
789          * only warn if the driver actually makes the suspend mode
790          * configurable.
791          */
792         if (!rstate->enabled && !rstate->disabled) {
793                 if (can_set_state)
794                         rdev_warn(rdev, "No configuration\n");
795                 return 0;
796         }
797
798         if (rstate->enabled && rstate->disabled) {
799                 rdev_err(rdev, "invalid configuration\n");
800                 return -EINVAL;
801         }
802
803         if (!can_set_state) {
804                 rdev_err(rdev, "no way to set suspend state\n");
805                 return -EINVAL;
806         }
807
808         if (rstate->enabled)
809                 ret = rdev->desc->ops->set_suspend_enable(rdev);
810         else
811                 ret = rdev->desc->ops->set_suspend_disable(rdev);
812         if (ret < 0) {
813                 rdev_err(rdev, "failed to enabled/disable\n");
814                 return ret;
815         }
816
817         if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
818                 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
819                 if (ret < 0) {
820                         rdev_err(rdev, "failed to set voltage\n");
821                         return ret;
822                 }
823         }
824
825         if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
826                 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
827                 if (ret < 0) {
828                         rdev_err(rdev, "failed to set mode\n");
829                         return ret;
830                 }
831         }
832         return ret;
833 }
834
835 /* locks held by caller */
836 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
837 {
838         if (!rdev->constraints)
839                 return -EINVAL;
840
841         switch (state) {
842         case PM_SUSPEND_STANDBY:
843                 return suspend_set_state(rdev,
844                         &rdev->constraints->state_standby);
845         case PM_SUSPEND_MEM:
846                 return suspend_set_state(rdev,
847                         &rdev->constraints->state_mem);
848         case PM_SUSPEND_MAX:
849                 return suspend_set_state(rdev,
850                         &rdev->constraints->state_disk);
851         default:
852                 return -EINVAL;
853         }
854 }
855
856 static void print_constraints(struct regulator_dev *rdev)
857 {
858         struct regulation_constraints *constraints = rdev->constraints;
859         char buf[80] = "";
860         int count = 0;
861         int ret;
862
863         if (constraints->min_uV && constraints->max_uV) {
864                 if (constraints->min_uV == constraints->max_uV)
865                         count += sprintf(buf + count, "%d mV ",
866                                          constraints->min_uV / 1000);
867                 else
868                         count += sprintf(buf + count, "%d <--> %d mV ",
869                                          constraints->min_uV / 1000,
870                                          constraints->max_uV / 1000);
871         }
872
873         if (!constraints->min_uV ||
874             constraints->min_uV != constraints->max_uV) {
875                 ret = _regulator_get_voltage(rdev);
876                 if (ret > 0)
877                         count += sprintf(buf + count, "at %d mV ", ret / 1000);
878         }
879
880         if (constraints->uV_offset)
881                 count += sprintf(buf, "%dmV offset ",
882                                  constraints->uV_offset / 1000);
883
884         if (constraints->min_uA && constraints->max_uA) {
885                 if (constraints->min_uA == constraints->max_uA)
886                         count += sprintf(buf + count, "%d mA ",
887                                          constraints->min_uA / 1000);
888                 else
889                         count += sprintf(buf + count, "%d <--> %d mA ",
890                                          constraints->min_uA / 1000,
891                                          constraints->max_uA / 1000);
892         }
893
894         if (!constraints->min_uA ||
895             constraints->min_uA != constraints->max_uA) {
896                 ret = _regulator_get_current_limit(rdev);
897                 if (ret > 0)
898                         count += sprintf(buf + count, "at %d mA ", ret / 1000);
899         }
900
901         if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
902                 count += sprintf(buf + count, "fast ");
903         if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
904                 count += sprintf(buf + count, "normal ");
905         if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
906                 count += sprintf(buf + count, "idle ");
907         if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
908                 count += sprintf(buf + count, "standby");
909
910         rdev_info(rdev, "%s\n", buf);
911
912         if ((constraints->min_uV != constraints->max_uV) &&
913             !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
914                 rdev_warn(rdev,
915                           "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
916 }
917
918 static int machine_constraints_voltage(struct regulator_dev *rdev,
919         struct regulation_constraints *constraints)
920 {
921         struct regulator_ops *ops = rdev->desc->ops;
922         int ret;
923
924         /* do we need to apply the constraint voltage */
925         if (rdev->constraints->apply_uV &&
926             rdev->constraints->min_uV == rdev->constraints->max_uV) {
927                 ret = _regulator_do_set_voltage(rdev,
928                                                 rdev->constraints->min_uV,
929                                                 rdev->constraints->max_uV);
930                 if (ret < 0) {
931                         rdev_err(rdev, "failed to apply %duV constraint\n",
932                                  rdev->constraints->min_uV);
933                         return ret;
934                 }
935         }
936
937         /* constrain machine-level voltage specs to fit
938          * the actual range supported by this regulator.
939          */
940         if (ops->list_voltage && rdev->desc->n_voltages) {
941                 int     count = rdev->desc->n_voltages;
942                 int     i;
943                 int     min_uV = INT_MAX;
944                 int     max_uV = INT_MIN;
945                 int     cmin = constraints->min_uV;
946                 int     cmax = constraints->max_uV;
947
948                 /* it's safe to autoconfigure fixed-voltage supplies
949                    and the constraints are used by list_voltage. */
950                 if (count == 1 && !cmin) {
951                         cmin = 1;
952                         cmax = INT_MAX;
953                         constraints->min_uV = cmin;
954                         constraints->max_uV = cmax;
955                 }
956
957                 /* voltage constraints are optional */
958                 if ((cmin == 0) && (cmax == 0))
959                         return 0;
960
961                 /* else require explicit machine-level constraints */
962                 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
963                         rdev_err(rdev, "invalid voltage constraints\n");
964                         return -EINVAL;
965                 }
966
967                 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
968                 for (i = 0; i < count; i++) {
969                         int     value;
970
971                         value = ops->list_voltage(rdev, i);
972                         if (value <= 0)
973                                 continue;
974
975                         /* maybe adjust [min_uV..max_uV] */
976                         if (value >= cmin && value < min_uV)
977                                 min_uV = value;
978                         if (value <= cmax && value > max_uV)
979                                 max_uV = value;
980                 }
981
982                 /* final: [min_uV..max_uV] valid iff constraints valid */
983                 if (max_uV < min_uV) {
984                         rdev_err(rdev, "unsupportable voltage constraints\n");
985                         return -EINVAL;
986                 }
987
988                 /* use regulator's subset of machine constraints */
989                 if (constraints->min_uV < min_uV) {
990                         rdev_dbg(rdev, "override min_uV, %d -> %d\n",
991                                  constraints->min_uV, min_uV);
992                         constraints->min_uV = min_uV;
993                 }
994                 if (constraints->max_uV > max_uV) {
995                         rdev_dbg(rdev, "override max_uV, %d -> %d\n",
996                                  constraints->max_uV, max_uV);
997                         constraints->max_uV = max_uV;
998                 }
999         }
1000
1001         return 0;
1002 }
1003
1004 /**
1005  * set_machine_constraints - sets regulator constraints
1006  * @rdev: regulator source
1007  * @constraints: constraints to apply
1008  *
1009  * Allows platform initialisation code to define and constrain
1010  * regulator circuits e.g. valid voltage/current ranges, etc.  NOTE:
1011  * Constraints *must* be set by platform code in order for some
1012  * regulator operations to proceed i.e. set_voltage, set_current_limit,
1013  * set_mode.
1014  */
1015 static int set_machine_constraints(struct regulator_dev *rdev,
1016         const struct regulation_constraints *constraints)
1017 {
1018         int ret = 0;
1019         struct regulator_ops *ops = rdev->desc->ops;
1020
1021         if (constraints)
1022                 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1023                                             GFP_KERNEL);
1024         else
1025                 rdev->constraints = kzalloc(sizeof(*constraints),
1026                                             GFP_KERNEL);
1027         if (!rdev->constraints)
1028                 return -ENOMEM;
1029
1030         ret = machine_constraints_voltage(rdev, rdev->constraints);
1031         if (ret != 0)
1032                 goto out;
1033
1034         /* do we need to setup our suspend state */
1035         if (rdev->constraints->initial_state) {
1036                 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
1037                 if (ret < 0) {
1038                         rdev_err(rdev, "failed to set suspend state\n");
1039                         goto out;
1040                 }
1041         }
1042
1043         if (rdev->constraints->initial_mode) {
1044                 if (!ops->set_mode) {
1045                         rdev_err(rdev, "no set_mode operation\n");
1046                         ret = -EINVAL;
1047                         goto out;
1048                 }
1049
1050                 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1051                 if (ret < 0) {
1052                         rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1053                         goto out;
1054                 }
1055         }
1056
1057         /* If the constraints say the regulator should be on at this point
1058          * and we have control then make sure it is enabled.
1059          */
1060         if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
1061             ops->enable) {
1062                 ret = ops->enable(rdev);
1063                 if (ret < 0) {
1064                         rdev_err(rdev, "failed to enable\n");
1065                         goto out;
1066                 }
1067         }
1068
1069         print_constraints(rdev);
1070         return 0;
1071 out:
1072         kfree(rdev->constraints);
1073         rdev->constraints = NULL;
1074         return ret;
1075 }
1076
1077 /**
1078  * set_supply - set regulator supply regulator
1079  * @rdev: regulator name
1080  * @supply_rdev: supply regulator name
1081  *
1082  * Called by platform initialisation code to set the supply regulator for this
1083  * regulator. This ensures that a regulators supply will also be enabled by the
1084  * core if it's child is enabled.
1085  */
1086 static int set_supply(struct regulator_dev *rdev,
1087                       struct regulator_dev *supply_rdev)
1088 {
1089         int err;
1090
1091         rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1092
1093         rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1094         if (rdev->supply == NULL) {
1095                 err = -ENOMEM;
1096                 return err;
1097         }
1098
1099         return 0;
1100 }
1101
1102 /**
1103  * set_consumer_device_supply - Bind a regulator to a symbolic supply
1104  * @rdev:         regulator source
1105  * @consumer_dev_name: dev_name() string for device supply applies to
1106  * @supply:       symbolic name for supply
1107  *
1108  * Allows platform initialisation code to map physical regulator
1109  * sources to symbolic names for supplies for use by devices.  Devices
1110  * should use these symbolic names to request regulators, avoiding the
1111  * need to provide board-specific regulator names as platform data.
1112  */
1113 static int set_consumer_device_supply(struct regulator_dev *rdev,
1114                                       const char *consumer_dev_name,
1115                                       const char *supply)
1116 {
1117         struct regulator_map *node;
1118         int has_dev;
1119
1120         if (supply == NULL)
1121                 return -EINVAL;
1122
1123         if (consumer_dev_name != NULL)
1124                 has_dev = 1;
1125         else
1126                 has_dev = 0;
1127
1128         list_for_each_entry(node, &regulator_map_list, list) {
1129                 if (node->dev_name && consumer_dev_name) {
1130                         if (strcmp(node->dev_name, consumer_dev_name) != 0)
1131                                 continue;
1132                 } else if (node->dev_name || consumer_dev_name) {
1133                         continue;
1134                 }
1135
1136                 if (strcmp(node->supply, supply) != 0)
1137                         continue;
1138
1139                 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1140                          consumer_dev_name,
1141                          dev_name(&node->regulator->dev),
1142                          node->regulator->desc->name,
1143                          supply,
1144                          dev_name(&rdev->dev), rdev_get_name(rdev));
1145                 return -EBUSY;
1146         }
1147
1148         node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1149         if (node == NULL)
1150                 return -ENOMEM;
1151
1152         node->regulator = rdev;
1153         node->supply = supply;
1154
1155         if (has_dev) {
1156                 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1157                 if (node->dev_name == NULL) {
1158                         kfree(node);
1159                         return -ENOMEM;
1160                 }
1161         }
1162
1163         list_add(&node->list, &regulator_map_list);
1164         return 0;
1165 }
1166
1167 static void unset_regulator_supplies(struct regulator_dev *rdev)
1168 {
1169         struct regulator_map *node, *n;
1170
1171         list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1172                 if (rdev == node->regulator) {
1173                         list_del(&node->list);
1174                         kfree(node->dev_name);
1175                         kfree(node);
1176                 }
1177         }
1178 }
1179
1180 #define REG_STR_SIZE    64
1181
1182 static struct regulator *create_regulator(struct regulator_dev *rdev,
1183                                           struct device *dev,
1184                                           const char *supply_name)
1185 {
1186         struct regulator *regulator;
1187         char buf[REG_STR_SIZE];
1188         int err, size;
1189
1190         regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1191         if (regulator == NULL)
1192                 return NULL;
1193
1194         mutex_lock(&rdev->mutex);
1195         regulator->rdev = rdev;
1196         list_add(&regulator->list, &rdev->consumer_list);
1197
1198         if (dev) {
1199                 /* create a 'requested_microamps_name' sysfs entry */
1200                 size = scnprintf(buf, REG_STR_SIZE,
1201                                  "microamps_requested_%s-%s",
1202                                  dev_name(dev), supply_name);
1203                 if (size >= REG_STR_SIZE)
1204                         goto overflow_err;
1205
1206                 regulator->dev = dev;
1207                 sysfs_attr_init(&regulator->dev_attr.attr);
1208                 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1209                 if (regulator->dev_attr.attr.name == NULL)
1210                         goto attr_name_err;
1211
1212                 regulator->dev_attr.attr.mode = 0444;
1213                 regulator->dev_attr.show = device_requested_uA_show;
1214                 err = device_create_file(dev, &regulator->dev_attr);
1215                 if (err < 0) {
1216                         rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
1217                         goto attr_name_err;
1218                 }
1219
1220                 /* also add a link to the device sysfs entry */
1221                 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1222                                  dev->kobj.name, supply_name);
1223                 if (size >= REG_STR_SIZE)
1224                         goto attr_err;
1225
1226                 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1227                 if (regulator->supply_name == NULL)
1228                         goto attr_err;
1229
1230                 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1231                                         buf);
1232                 if (err) {
1233                         rdev_warn(rdev, "could not add device link %s err %d\n",
1234                                   dev->kobj.name, err);
1235                         goto link_name_err;
1236                 }
1237         } else {
1238                 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1239                 if (regulator->supply_name == NULL)
1240                         goto attr_err;
1241         }
1242
1243         regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1244                                                 rdev->debugfs);
1245         if (!regulator->debugfs) {
1246                 rdev_warn(rdev, "Failed to create debugfs directory\n");
1247         } else {
1248                 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1249                                    &regulator->uA_load);
1250                 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1251                                    &regulator->min_uV);
1252                 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1253                                    &regulator->max_uV);
1254         }
1255
1256         mutex_unlock(&rdev->mutex);
1257         return regulator;
1258 link_name_err:
1259         kfree(regulator->supply_name);
1260 attr_err:
1261         device_remove_file(regulator->dev, &regulator->dev_attr);
1262 attr_name_err:
1263         kfree(regulator->dev_attr.attr.name);
1264 overflow_err:
1265         list_del(&regulator->list);
1266         kfree(regulator);
1267         mutex_unlock(&rdev->mutex);
1268         return NULL;
1269 }
1270
1271 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1272 {
1273         if (!rdev->desc->ops->enable_time)
1274                 return 0;
1275         return rdev->desc->ops->enable_time(rdev);
1276 }
1277
1278 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1279                                                          const char *supply)
1280 {
1281         struct regulator_dev *r;
1282         struct device_node *node;
1283
1284         /* first do a dt based lookup */
1285         if (dev && dev->of_node) {
1286                 node = of_get_regulator(dev, supply);
1287                 if (node)
1288                         list_for_each_entry(r, &regulator_list, list)
1289                                 if (r->dev.parent &&
1290                                         node == r->dev.of_node)
1291                                         return r;
1292         }
1293
1294         /* if not found, try doing it non-dt way */
1295         list_for_each_entry(r, &regulator_list, list)
1296                 if (strcmp(rdev_get_name(r), supply) == 0)
1297                         return r;
1298
1299         return NULL;
1300 }
1301
1302 /* Internal regulator request function */
1303 static struct regulator *_regulator_get(struct device *dev, const char *id,
1304                                         int exclusive)
1305 {
1306         struct regulator_dev *rdev;
1307         struct regulator_map *map;
1308         struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
1309         const char *devname = NULL;
1310         int ret;
1311
1312         if (id == NULL) {
1313                 pr_err("get() with no identifier\n");
1314                 return regulator;
1315         }
1316
1317         if (dev)
1318                 devname = dev_name(dev);
1319
1320         mutex_lock(&regulator_list_mutex);
1321
1322         rdev = regulator_dev_lookup(dev, id);
1323         if (rdev)
1324                 goto found;
1325
1326         list_for_each_entry(map, &regulator_map_list, list) {
1327                 /* If the mapping has a device set up it must match */
1328                 if (map->dev_name &&
1329                     (!devname || strcmp(map->dev_name, devname)))
1330                         continue;
1331
1332                 if (strcmp(map->supply, id) == 0) {
1333                         rdev = map->regulator;
1334                         goto found;
1335                 }
1336         }
1337
1338         if (board_wants_dummy_regulator) {
1339                 rdev = dummy_regulator_rdev;
1340                 goto found;
1341         }
1342
1343 #ifdef CONFIG_REGULATOR_DUMMY
1344         if (!devname)
1345                 devname = "deviceless";
1346
1347         /* If the board didn't flag that it was fully constrained then
1348          * substitute in a dummy regulator so consumers can continue.
1349          */
1350         if (!has_full_constraints) {
1351                 pr_warn("%s supply %s not found, using dummy regulator\n",
1352                         devname, id);
1353                 rdev = dummy_regulator_rdev;
1354                 goto found;
1355         }
1356 #endif
1357
1358         mutex_unlock(&regulator_list_mutex);
1359         return regulator;
1360
1361 found:
1362         if (rdev->exclusive) {
1363                 regulator = ERR_PTR(-EPERM);
1364                 goto out;
1365         }
1366
1367         if (exclusive && rdev->open_count) {
1368                 regulator = ERR_PTR(-EBUSY);
1369                 goto out;
1370         }
1371
1372         if (!try_module_get(rdev->owner))
1373                 goto out;
1374
1375         regulator = create_regulator(rdev, dev, id);
1376         if (regulator == NULL) {
1377                 regulator = ERR_PTR(-ENOMEM);
1378                 module_put(rdev->owner);
1379                 goto out;
1380         }
1381
1382         rdev->open_count++;
1383         if (exclusive) {
1384                 rdev->exclusive = 1;
1385
1386                 ret = _regulator_is_enabled(rdev);
1387                 if (ret > 0)
1388                         rdev->use_count = 1;
1389                 else
1390                         rdev->use_count = 0;
1391         }
1392
1393 out:
1394         mutex_unlock(&regulator_list_mutex);
1395
1396         return regulator;
1397 }
1398
1399 /**
1400  * regulator_get - lookup and obtain a reference to a regulator.
1401  * @dev: device for regulator "consumer"
1402  * @id: Supply name or regulator ID.
1403  *
1404  * Returns a struct regulator corresponding to the regulator producer,
1405  * or IS_ERR() condition containing errno.
1406  *
1407  * Use of supply names configured via regulator_set_device_supply() is
1408  * strongly encouraged.  It is recommended that the supply name used
1409  * should match the name used for the supply and/or the relevant
1410  * device pins in the datasheet.
1411  */
1412 struct regulator *regulator_get(struct device *dev, const char *id)
1413 {
1414         return _regulator_get(dev, id, 0);
1415 }
1416 EXPORT_SYMBOL_GPL(regulator_get);
1417
1418 static void devm_regulator_release(struct device *dev, void *res)
1419 {
1420         regulator_put(*(struct regulator **)res);
1421 }
1422
1423 /**
1424  * devm_regulator_get - Resource managed regulator_get()
1425  * @dev: device for regulator "consumer"
1426  * @id: Supply name or regulator ID.
1427  *
1428  * Managed regulator_get(). Regulators returned from this function are
1429  * automatically regulator_put() on driver detach. See regulator_get() for more
1430  * information.
1431  */
1432 struct regulator *devm_regulator_get(struct device *dev, const char *id)
1433 {
1434         struct regulator **ptr, *regulator;
1435
1436         ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1437         if (!ptr)
1438                 return ERR_PTR(-ENOMEM);
1439
1440         regulator = regulator_get(dev, id);
1441         if (!IS_ERR(regulator)) {
1442                 *ptr = regulator;
1443                 devres_add(dev, ptr);
1444         } else {
1445                 devres_free(ptr);
1446         }
1447
1448         return regulator;
1449 }
1450 EXPORT_SYMBOL_GPL(devm_regulator_get);
1451
1452 /**
1453  * regulator_get_exclusive - obtain exclusive access to a regulator.
1454  * @dev: device for regulator "consumer"
1455  * @id: Supply name or regulator ID.
1456  *
1457  * Returns a struct regulator corresponding to the regulator producer,
1458  * or IS_ERR() condition containing errno.  Other consumers will be
1459  * unable to obtain this reference is held and the use count for the
1460  * regulator will be initialised to reflect the current state of the
1461  * regulator.
1462  *
1463  * This is intended for use by consumers which cannot tolerate shared
1464  * use of the regulator such as those which need to force the
1465  * regulator off for correct operation of the hardware they are
1466  * controlling.
1467  *
1468  * Use of supply names configured via regulator_set_device_supply() is
1469  * strongly encouraged.  It is recommended that the supply name used
1470  * should match the name used for the supply and/or the relevant
1471  * device pins in the datasheet.
1472  */
1473 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1474 {
1475         return _regulator_get(dev, id, 1);
1476 }
1477 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1478
1479 /**
1480  * regulator_put - "free" the regulator source
1481  * @regulator: regulator source
1482  *
1483  * Note: drivers must ensure that all regulator_enable calls made on this
1484  * regulator source are balanced by regulator_disable calls prior to calling
1485  * this function.
1486  */
1487 void regulator_put(struct regulator *regulator)
1488 {
1489         struct regulator_dev *rdev;
1490
1491         if (regulator == NULL || IS_ERR(regulator))
1492                 return;
1493
1494         mutex_lock(&regulator_list_mutex);
1495         rdev = regulator->rdev;
1496
1497         debugfs_remove_recursive(regulator->debugfs);
1498
1499         /* remove any sysfs entries */
1500         if (regulator->dev) {
1501                 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1502                 device_remove_file(regulator->dev, &regulator->dev_attr);
1503                 kfree(regulator->dev_attr.attr.name);
1504         }
1505         kfree(regulator->supply_name);
1506         list_del(&regulator->list);
1507         kfree(regulator);
1508
1509         rdev->open_count--;
1510         rdev->exclusive = 0;
1511
1512         module_put(rdev->owner);
1513         mutex_unlock(&regulator_list_mutex);
1514 }
1515 EXPORT_SYMBOL_GPL(regulator_put);
1516
1517 static int devm_regulator_match(struct device *dev, void *res, void *data)
1518 {
1519         struct regulator **r = res;
1520         if (!r || !*r) {
1521                 WARN_ON(!r || !*r);
1522                 return 0;
1523         }
1524         return *r == data;
1525 }
1526
1527 /**
1528  * devm_regulator_put - Resource managed regulator_put()
1529  * @regulator: regulator to free
1530  *
1531  * Deallocate a regulator allocated with devm_regulator_get(). Normally
1532  * this function will not need to be called and the resource management
1533  * code will ensure that the resource is freed.
1534  */
1535 void devm_regulator_put(struct regulator *regulator)
1536 {
1537         int rc;
1538
1539         rc = devres_destroy(regulator->dev, devm_regulator_release,
1540                             devm_regulator_match, regulator);
1541         if (rc == 0)
1542                 regulator_put(regulator);
1543         else
1544                 WARN_ON(rc);
1545 }
1546 EXPORT_SYMBOL_GPL(devm_regulator_put);
1547
1548 static int _regulator_can_change_status(struct regulator_dev *rdev)
1549 {
1550         if (!rdev->constraints)
1551                 return 0;
1552
1553         if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1554                 return 1;
1555         else
1556                 return 0;
1557 }
1558
1559 /* locks held by regulator_enable() */
1560 static int _regulator_enable(struct regulator_dev *rdev)
1561 {
1562         int ret, delay;
1563
1564         /* check voltage and requested load before enabling */
1565         if (rdev->constraints &&
1566             (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1567                 drms_uA_update(rdev);
1568
1569         if (rdev->use_count == 0) {
1570                 /* The regulator may on if it's not switchable or left on */
1571                 ret = _regulator_is_enabled(rdev);
1572                 if (ret == -EINVAL || ret == 0) {
1573                         if (!_regulator_can_change_status(rdev))
1574                                 return -EPERM;
1575
1576                         if (!rdev->desc->ops->enable)
1577                                 return -EINVAL;
1578
1579                         /* Query before enabling in case configuration
1580                          * dependent.  */
1581                         ret = _regulator_get_enable_time(rdev);
1582                         if (ret >= 0) {
1583                                 delay = ret;
1584                         } else {
1585                                 rdev_warn(rdev, "enable_time() failed: %d\n",
1586                                            ret);
1587                                 delay = 0;
1588                         }
1589
1590                         trace_regulator_enable(rdev_get_name(rdev));
1591                         _notifier_call_chain(
1592                                 rdev, REGULATOR_EVENT_PRE_ENABLE, NULL);
1593
1594                         /* Allow the regulator to ramp; it would be useful
1595                          * to extend this for bulk operations so that the
1596                          * regulators can ramp together.  */
1597                         ret = rdev->desc->ops->enable(rdev);
1598                         if (ret < 0)
1599                                 return ret;
1600
1601                         trace_regulator_enable_delay(rdev_get_name(rdev));
1602
1603                         if (delay >= 1000) {
1604                                 mdelay(delay / 1000);
1605                                 udelay(delay % 1000);
1606                         } else if (delay) {
1607                                 udelay(delay);
1608                         }
1609
1610                         _notifier_call_chain(
1611                                 rdev, REGULATOR_EVENT_POST_ENABLE, NULL);
1612                         trace_regulator_enable_complete(rdev_get_name(rdev));
1613
1614                 } else if (ret < 0) {
1615                         rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1616                         return ret;
1617                 }
1618                 /* Fallthrough on positive return values - already enabled */
1619         }
1620
1621         rdev->use_count++;
1622
1623         return 0;
1624 }
1625
1626 /**
1627  * regulator_enable - enable regulator output
1628  * @regulator: regulator source
1629  *
1630  * Request that the regulator be enabled with the regulator output at
1631  * the predefined voltage or current value.  Calls to regulator_enable()
1632  * must be balanced with calls to regulator_disable().
1633  *
1634  * NOTE: the output value can be set by other drivers, boot loader or may be
1635  * hardwired in the regulator.
1636  */
1637 int regulator_enable(struct regulator *regulator)
1638 {
1639         struct regulator_dev *rdev = regulator->rdev;
1640         int ret = 0;
1641
1642         if (rdev->supply) {
1643                 ret = regulator_enable(rdev->supply);
1644                 if (ret != 0)
1645                         return ret;
1646         }
1647
1648         mutex_lock(&rdev->mutex);
1649         ret = _regulator_enable(rdev);
1650         mutex_unlock(&rdev->mutex);
1651
1652         if (ret != 0 && rdev->supply)
1653                 regulator_disable(rdev->supply);
1654
1655         return ret;
1656 }
1657 EXPORT_SYMBOL_GPL(regulator_enable);
1658
1659 /* locks held by regulator_disable() */
1660 static int _regulator_disable(struct regulator_dev *rdev)
1661 {
1662         int ret = 0;
1663
1664         if (WARN(rdev->use_count <= 0,
1665                  "unbalanced disables for %s\n", rdev_get_name(rdev)))
1666                 return -EIO;
1667
1668         /* are we the last user and permitted to disable ? */
1669         if (rdev->use_count == 1 &&
1670             (rdev->constraints && !rdev->constraints->always_on)) {
1671
1672                 /* we are last user */
1673                 if (_regulator_can_change_status(rdev) &&
1674                     rdev->desc->ops->disable) {
1675                         trace_regulator_disable(rdev_get_name(rdev));
1676
1677                         ret = rdev->desc->ops->disable(rdev);
1678                         if (ret < 0) {
1679                                 rdev_err(rdev, "failed to disable\n");
1680                                 return ret;
1681                         }
1682
1683                         trace_regulator_disable_complete(rdev_get_name(rdev));
1684
1685                         _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1686                                              NULL);
1687                 }
1688
1689                 rdev->use_count = 0;
1690         } else if (rdev->use_count > 1) {
1691
1692                 if (rdev->constraints &&
1693                         (rdev->constraints->valid_ops_mask &
1694                         REGULATOR_CHANGE_DRMS))
1695                         drms_uA_update(rdev);
1696
1697                 rdev->use_count--;
1698         }
1699
1700         return ret;
1701 }
1702
1703 /**
1704  * regulator_disable - disable regulator output
1705  * @regulator: regulator source
1706  *
1707  * Disable the regulator output voltage or current.  Calls to
1708  * regulator_enable() must be balanced with calls to
1709  * regulator_disable().
1710  *
1711  * NOTE: this will only disable the regulator output if no other consumer
1712  * devices have it enabled, the regulator device supports disabling and
1713  * machine constraints permit this operation.
1714  */
1715 int regulator_disable(struct regulator *regulator)
1716 {
1717         struct regulator_dev *rdev = regulator->rdev;
1718         int ret = 0;
1719
1720         mutex_lock(&rdev->mutex);
1721         ret = _regulator_disable(rdev);
1722         mutex_unlock(&rdev->mutex);
1723
1724         if (ret == 0 && rdev->supply)
1725                 regulator_disable(rdev->supply);
1726
1727         return ret;
1728 }
1729 EXPORT_SYMBOL_GPL(regulator_disable);
1730
1731 /* locks held by regulator_force_disable() */
1732 static int _regulator_force_disable(struct regulator_dev *rdev)
1733 {
1734         int ret = 0;
1735
1736         /* force disable */
1737         if (rdev->desc->ops->disable) {
1738                 /* ah well, who wants to live forever... */
1739                 ret = rdev->desc->ops->disable(rdev);
1740                 if (ret < 0) {
1741                         rdev_err(rdev, "failed to force disable\n");
1742                         return ret;
1743                 }
1744                 /* notify other consumers that power has been forced off */
1745                 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1746                         REGULATOR_EVENT_DISABLE, NULL);
1747         }
1748
1749         return ret;
1750 }
1751
1752 /**
1753  * regulator_force_disable - force disable regulator output
1754  * @regulator: regulator source
1755  *
1756  * Forcibly disable the regulator output voltage or current.
1757  * NOTE: this *will* disable the regulator output even if other consumer
1758  * devices have it enabled. This should be used for situations when device
1759  * damage will likely occur if the regulator is not disabled (e.g. over temp).
1760  */
1761 int regulator_force_disable(struct regulator *regulator)
1762 {
1763         struct regulator_dev *rdev = regulator->rdev;
1764         int ret;
1765
1766         mutex_lock(&rdev->mutex);
1767         regulator->uA_load = 0;
1768         ret = _regulator_force_disable(regulator->rdev);
1769         mutex_unlock(&rdev->mutex);
1770
1771         if (rdev->supply)
1772                 while (rdev->open_count--)
1773                         regulator_disable(rdev->supply);
1774
1775         return ret;
1776 }
1777 EXPORT_SYMBOL_GPL(regulator_force_disable);
1778
1779 static void regulator_disable_work(struct work_struct *work)
1780 {
1781         struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1782                                                   disable_work.work);
1783         int count, i, ret;
1784
1785         mutex_lock(&rdev->mutex);
1786
1787         BUG_ON(!rdev->deferred_disables);
1788
1789         count = rdev->deferred_disables;
1790         rdev->deferred_disables = 0;
1791
1792         for (i = 0; i < count; i++) {
1793                 ret = _regulator_disable(rdev);
1794                 if (ret != 0)
1795                         rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1796         }
1797
1798         mutex_unlock(&rdev->mutex);
1799
1800         if (rdev->supply) {
1801                 for (i = 0; i < count; i++) {
1802                         ret = regulator_disable(rdev->supply);
1803                         if (ret != 0) {
1804                                 rdev_err(rdev,
1805                                          "Supply disable failed: %d\n", ret);
1806                         }
1807                 }
1808         }
1809 }
1810
1811 /**
1812  * regulator_disable_deferred - disable regulator output with delay
1813  * @regulator: regulator source
1814  * @ms: miliseconds until the regulator is disabled
1815  *
1816  * Execute regulator_disable() on the regulator after a delay.  This
1817  * is intended for use with devices that require some time to quiesce.
1818  *
1819  * NOTE: this will only disable the regulator output if no other consumer
1820  * devices have it enabled, the regulator device supports disabling and
1821  * machine constraints permit this operation.
1822  */
1823 int regulator_disable_deferred(struct regulator *regulator, int ms)
1824 {
1825         struct regulator_dev *rdev = regulator->rdev;
1826         int ret;
1827
1828         mutex_lock(&rdev->mutex);
1829         rdev->deferred_disables++;
1830         mutex_unlock(&rdev->mutex);
1831
1832         ret = schedule_delayed_work(&rdev->disable_work,
1833                                     msecs_to_jiffies(ms));
1834         if (ret < 0)
1835                 return ret;
1836         else
1837                 return 0;
1838 }
1839 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1840
1841 static int _regulator_is_enabled(struct regulator_dev *rdev)
1842 {
1843         /* If we don't know then assume that the regulator is always on */
1844         if (!rdev->desc->ops->is_enabled)
1845                 return 1;
1846
1847         return rdev->desc->ops->is_enabled(rdev);
1848 }
1849
1850 /**
1851  * regulator_is_enabled - is the regulator output enabled
1852  * @regulator: regulator source
1853  *
1854  * Returns positive if the regulator driver backing the source/client
1855  * has requested that the device be enabled, zero if it hasn't, else a
1856  * negative errno code.
1857  *
1858  * Note that the device backing this regulator handle can have multiple
1859  * users, so it might be enabled even if regulator_enable() was never
1860  * called for this particular source.
1861  */
1862 int regulator_is_enabled(struct regulator *regulator)
1863 {
1864         int ret;
1865
1866         mutex_lock(&regulator->rdev->mutex);
1867         ret = _regulator_is_enabled(regulator->rdev);
1868         mutex_unlock(&regulator->rdev->mutex);
1869
1870         return ret;
1871 }
1872 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1873
1874 /**
1875  * regulator_count_voltages - count regulator_list_voltage() selectors
1876  * @regulator: regulator source
1877  *
1878  * Returns number of selectors, or negative errno.  Selectors are
1879  * numbered starting at zero, and typically correspond to bitfields
1880  * in hardware registers.
1881  */
1882 int regulator_count_voltages(struct regulator *regulator)
1883 {
1884         struct regulator_dev    *rdev = regulator->rdev;
1885
1886         return rdev->desc->n_voltages ? : -EINVAL;
1887 }
1888 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1889
1890 /**
1891  * regulator_list_voltage - enumerate supported voltages
1892  * @regulator: regulator source
1893  * @selector: identify voltage to list
1894  * Context: can sleep
1895  *
1896  * Returns a voltage that can be passed to @regulator_set_voltage(),
1897  * zero if this selector code can't be used on this system, or a
1898  * negative errno.
1899  */
1900 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1901 {
1902         struct regulator_dev    *rdev = regulator->rdev;
1903         struct regulator_ops    *ops = rdev->desc->ops;
1904         int                     ret;
1905
1906         if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1907                 return -EINVAL;
1908
1909         mutex_lock(&rdev->mutex);
1910         ret = ops->list_voltage(rdev, selector);
1911         mutex_unlock(&rdev->mutex);
1912
1913         if (ret > 0) {
1914                 if (ret < rdev->constraints->min_uV)
1915                         ret = 0;
1916                 else if (ret > rdev->constraints->max_uV)
1917                         ret = 0;
1918         }
1919
1920         return ret;
1921 }
1922 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1923
1924 /**
1925  * regulator_is_supported_voltage - check if a voltage range can be supported
1926  *
1927  * @regulator: Regulator to check.
1928  * @min_uV: Minimum required voltage in uV.
1929  * @max_uV: Maximum required voltage in uV.
1930  *
1931  * Returns a boolean or a negative error code.
1932  */
1933 int regulator_is_supported_voltage(struct regulator *regulator,
1934                                    int min_uV, int max_uV)
1935 {
1936         int i, voltages, ret;
1937
1938         ret = regulator_count_voltages(regulator);
1939         if (ret < 0)
1940                 return ret;
1941         voltages = ret;
1942
1943         for (i = 0; i < voltages; i++) {
1944                 ret = regulator_list_voltage(regulator, i);
1945
1946                 if (ret >= min_uV && ret <= max_uV)
1947                         return 1;
1948         }
1949
1950         return 0;
1951 }
1952 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
1953
1954 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1955                                      int min_uV, int max_uV)
1956 {
1957         int ret;
1958         int delay = 0;
1959         unsigned int selector;
1960
1961         trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1962
1963         min_uV += rdev->constraints->uV_offset;
1964         max_uV += rdev->constraints->uV_offset;
1965
1966         if (_regulator_is_enabled(rdev))
1967                 _notifier_call_chain(rdev, REGULATOR_EVENT_OUT_PRECHANGE,
1968                                      NULL);
1969
1970         if (rdev->desc->ops->set_voltage) {
1971                 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1972                                                    &selector);
1973
1974                 if (rdev->desc->ops->list_voltage)
1975                         selector = rdev->desc->ops->list_voltage(rdev,
1976                                                                  selector);
1977                 else
1978                         selector = -1;
1979         } else if (rdev->desc->ops->set_voltage_sel) {
1980                 int best_val = INT_MAX;
1981                 int i;
1982
1983                 selector = 0;
1984
1985                 /* Find the smallest voltage that falls within the specified
1986                  * range.
1987                  */
1988                 for (i = 0; i < rdev->desc->n_voltages; i++) {
1989                         ret = rdev->desc->ops->list_voltage(rdev, i);
1990                         if (ret < 0)
1991                                 continue;
1992
1993                         if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1994                                 best_val = ret;
1995                                 selector = i;
1996                         }
1997                 }
1998
1999                 /*
2000                  * If we can't obtain the old selector there is not enough
2001                  * info to call set_voltage_time_sel().
2002                  */
2003                 if (rdev->desc->ops->set_voltage_time_sel &&
2004                     rdev->desc->ops->get_voltage_sel) {
2005                         unsigned int old_selector = 0;
2006
2007                         ret = rdev->desc->ops->get_voltage_sel(rdev);
2008                         if (ret < 0)
2009                                 return ret;
2010                         old_selector = ret;
2011                         ret = rdev->desc->ops->set_voltage_time_sel(rdev,
2012                                                 old_selector, selector);
2013                         if (ret < 0)
2014                                 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n", ret);
2015                         else
2016                                 delay = ret;
2017                 }
2018
2019                 if (best_val != INT_MAX) {
2020                         ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2021                         selector = best_val;
2022                 } else {
2023                         ret = -EINVAL;
2024                 }
2025         } else {
2026                 ret = -EINVAL;
2027         }
2028
2029         /* Insert any necessary delays */
2030         if (delay >= 1000) {
2031                 mdelay(delay / 1000);
2032                 udelay(delay % 1000);
2033         } else if (delay) {
2034                 udelay(delay);
2035         }
2036
2037         if (ret == 0)
2038                 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2039                                      NULL);
2040
2041         if (_regulator_is_enabled(rdev))
2042                 _notifier_call_chain(rdev, REGULATOR_EVENT_OUT_POSTCHANGE,
2043                                      NULL);
2044
2045         trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
2046
2047         return ret;
2048 }
2049
2050 /**
2051  * regulator_set_voltage - set regulator output voltage
2052  * @regulator: regulator source
2053  * @min_uV: Minimum required voltage in uV
2054  * @max_uV: Maximum acceptable voltage in uV
2055  *
2056  * Sets a voltage regulator to the desired output voltage. This can be set
2057  * during any regulator state. IOW, regulator can be disabled or enabled.
2058  *
2059  * If the regulator is enabled then the voltage will change to the new value
2060  * immediately otherwise if the regulator is disabled the regulator will
2061  * output at the new voltage when enabled.
2062  *
2063  * NOTE: If the regulator is shared between several devices then the lowest
2064  * request voltage that meets the system constraints will be used.
2065  * Regulator system constraints must be set for this regulator before
2066  * calling this function otherwise this call will fail.
2067  */
2068 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2069 {
2070         struct regulator_dev *rdev = regulator->rdev;
2071         int ret = 0;
2072
2073         mutex_lock(&rdev->mutex);
2074
2075         /* If we're setting the same range as last time the change
2076          * should be a noop (some cpufreq implementations use the same
2077          * voltage for multiple frequencies, for example).
2078          */
2079         if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2080                 goto out;
2081
2082         /* sanity check */
2083         if (!rdev->desc->ops->set_voltage &&
2084             !rdev->desc->ops->set_voltage_sel) {
2085                 ret = -EINVAL;
2086                 goto out;
2087         }
2088
2089         /* constraints check */
2090         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2091         if (ret < 0)
2092                 goto out;
2093         regulator->min_uV = min_uV;
2094         regulator->max_uV = max_uV;
2095
2096         ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2097         if (ret < 0)
2098                 goto out;
2099
2100         ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2101
2102 out:
2103         mutex_unlock(&rdev->mutex);
2104         return ret;
2105 }
2106 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2107
2108 /**
2109  * regulator_set_voltage_time - get raise/fall time
2110  * @regulator: regulator source
2111  * @old_uV: starting voltage in microvolts
2112  * @new_uV: target voltage in microvolts
2113  *
2114  * Provided with the starting and ending voltage, this function attempts to
2115  * calculate the time in microseconds required to rise or fall to this new
2116  * voltage.
2117  */
2118 int regulator_set_voltage_time(struct regulator *regulator,
2119                                int old_uV, int new_uV)
2120 {
2121         struct regulator_dev    *rdev = regulator->rdev;
2122         struct regulator_ops    *ops = rdev->desc->ops;
2123         int old_sel = -1;
2124         int new_sel = -1;
2125         int voltage;
2126         int i;
2127
2128         /* Currently requires operations to do this */
2129         if (!ops->list_voltage || !ops->set_voltage_time_sel
2130             || !rdev->desc->n_voltages)
2131                 return -EINVAL;
2132
2133         for (i = 0; i < rdev->desc->n_voltages; i++) {
2134                 /* We only look for exact voltage matches here */
2135                 voltage = regulator_list_voltage(regulator, i);
2136                 if (voltage < 0)
2137                         return -EINVAL;
2138                 if (voltage == 0)
2139                         continue;
2140                 if (voltage == old_uV)
2141                         old_sel = i;
2142                 if (voltage == new_uV)
2143                         new_sel = i;
2144         }
2145
2146         if (old_sel < 0 || new_sel < 0)
2147                 return -EINVAL;
2148
2149         return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2150 }
2151 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2152
2153 /**
2154  * regulator_sync_voltage - re-apply last regulator output voltage
2155  * @regulator: regulator source
2156  *
2157  * Re-apply the last configured voltage.  This is intended to be used
2158  * where some external control source the consumer is cooperating with
2159  * has caused the configured voltage to change.
2160  */
2161 int regulator_sync_voltage(struct regulator *regulator)
2162 {
2163         struct regulator_dev *rdev = regulator->rdev;
2164         int ret, min_uV, max_uV;
2165
2166         mutex_lock(&rdev->mutex);
2167
2168         if (!rdev->desc->ops->set_voltage &&
2169             !rdev->desc->ops->set_voltage_sel) {
2170                 ret = -EINVAL;
2171                 goto out;
2172         }
2173
2174         /* This is only going to work if we've had a voltage configured. */
2175         if (!regulator->min_uV && !regulator->max_uV) {
2176                 ret = -EINVAL;
2177                 goto out;
2178         }
2179
2180         min_uV = regulator->min_uV;
2181         max_uV = regulator->max_uV;
2182
2183         /* This should be a paranoia check... */
2184         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2185         if (ret < 0)
2186                 goto out;
2187
2188         ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2189         if (ret < 0)
2190                 goto out;
2191
2192         ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2193
2194 out:
2195         mutex_unlock(&rdev->mutex);
2196         return ret;
2197 }
2198 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2199
2200 static int _regulator_get_voltage(struct regulator_dev *rdev)
2201 {
2202         int sel, ret;
2203
2204         if (rdev->desc->ops->get_voltage_sel) {
2205                 sel = rdev->desc->ops->get_voltage_sel(rdev);
2206                 if (sel < 0)
2207                         return sel;
2208                 ret = rdev->desc->ops->list_voltage(rdev, sel);
2209         } else if (rdev->desc->ops->get_voltage) {
2210                 ret = rdev->desc->ops->get_voltage(rdev);
2211         } else {
2212                 return -EINVAL;
2213         }
2214
2215         if (ret < 0)
2216                 return ret;
2217         return ret - rdev->constraints->uV_offset;
2218 }
2219
2220 /**
2221  * regulator_get_voltage - get regulator output voltage
2222  * @regulator: regulator source
2223  *
2224  * This returns the current regulator voltage in uV.
2225  *
2226  * NOTE: If the regulator is disabled it will return the voltage value. This
2227  * function should not be used to determine regulator state.
2228  */
2229 int regulator_get_voltage(struct regulator *regulator)
2230 {
2231         int ret;
2232
2233         mutex_lock(&regulator->rdev->mutex);
2234
2235         ret = _regulator_get_voltage(regulator->rdev);
2236
2237         mutex_unlock(&regulator->rdev->mutex);
2238
2239         return ret;
2240 }
2241 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2242
2243 /**
2244  * regulator_set_current_limit - set regulator output current limit
2245  * @regulator: regulator source
2246  * @min_uA: Minimuum supported current in uA
2247  * @max_uA: Maximum supported current in uA
2248  *
2249  * Sets current sink to the desired output current. This can be set during
2250  * any regulator state. IOW, regulator can be disabled or enabled.
2251  *
2252  * If the regulator is enabled then the current will change to the new value
2253  * immediately otherwise if the regulator is disabled the regulator will
2254  * output at the new current when enabled.
2255  *
2256  * NOTE: Regulator system constraints must be set for this regulator before
2257  * calling this function otherwise this call will fail.
2258  */
2259 int regulator_set_current_limit(struct regulator *regulator,
2260                                int min_uA, int max_uA)
2261 {
2262         struct regulator_dev *rdev = regulator->rdev;
2263         int ret;
2264
2265         mutex_lock(&rdev->mutex);
2266
2267         /* sanity check */
2268         if (!rdev->desc->ops->set_current_limit) {
2269                 ret = -EINVAL;
2270                 goto out;
2271         }
2272
2273         /* constraints check */
2274         ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2275         if (ret < 0)
2276                 goto out;
2277
2278         ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2279 out:
2280         mutex_unlock(&rdev->mutex);
2281         return ret;
2282 }
2283 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2284
2285 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2286 {
2287         int ret;
2288
2289         mutex_lock(&rdev->mutex);
2290
2291         /* sanity check */
2292         if (!rdev->desc->ops->get_current_limit) {
2293                 ret = -EINVAL;
2294                 goto out;
2295         }
2296
2297         ret = rdev->desc->ops->get_current_limit(rdev);
2298 out:
2299         mutex_unlock(&rdev->mutex);
2300         return ret;
2301 }
2302
2303 /**
2304  * regulator_get_current_limit - get regulator output current
2305  * @regulator: regulator source
2306  *
2307  * This returns the current supplied by the specified current sink in uA.
2308  *
2309  * NOTE: If the regulator is disabled it will return the current value. This
2310  * function should not be used to determine regulator state.
2311  */
2312 int regulator_get_current_limit(struct regulator *regulator)
2313 {
2314         return _regulator_get_current_limit(regulator->rdev);
2315 }
2316 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2317
2318 /**
2319  * regulator_set_mode - set regulator operating mode
2320  * @regulator: regulator source
2321  * @mode: operating mode - one of the REGULATOR_MODE constants
2322  *
2323  * Set regulator operating mode to increase regulator efficiency or improve
2324  * regulation performance.
2325  *
2326  * NOTE: Regulator system constraints must be set for this regulator before
2327  * calling this function otherwise this call will fail.
2328  */
2329 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2330 {
2331         struct regulator_dev *rdev = regulator->rdev;
2332         int ret;
2333         int regulator_curr_mode;
2334
2335         mutex_lock(&rdev->mutex);
2336
2337         /* sanity check */
2338         if (!rdev->desc->ops->set_mode) {
2339                 ret = -EINVAL;
2340                 goto out;
2341         }
2342
2343         /* return if the same mode is requested */
2344         if (rdev->desc->ops->get_mode) {
2345                 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2346                 if (regulator_curr_mode == mode) {
2347                         ret = 0;
2348                         goto out;
2349                 }
2350         }
2351
2352         /* constraints check */
2353         ret = regulator_mode_constrain(rdev, &mode);
2354         if (ret < 0)
2355                 goto out;
2356
2357         ret = rdev->desc->ops->set_mode(rdev, mode);
2358 out:
2359         mutex_unlock(&rdev->mutex);
2360         return ret;
2361 }
2362 EXPORT_SYMBOL_GPL(regulator_set_mode);
2363
2364 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2365 {
2366         int ret;
2367
2368         mutex_lock(&rdev->mutex);
2369
2370         /* sanity check */
2371         if (!rdev->desc->ops->get_mode) {
2372                 ret = -EINVAL;
2373                 goto out;
2374         }
2375
2376         ret = rdev->desc->ops->get_mode(rdev);
2377 out:
2378         mutex_unlock(&rdev->mutex);
2379         return ret;
2380 }
2381
2382 /**
2383  * regulator_get_mode - get regulator operating mode
2384  * @regulator: regulator source
2385  *
2386  * Get the current regulator operating mode.
2387  */
2388 unsigned int regulator_get_mode(struct regulator *regulator)
2389 {
2390         return _regulator_get_mode(regulator->rdev);
2391 }
2392 EXPORT_SYMBOL_GPL(regulator_get_mode);
2393
2394 /**
2395  * regulator_set_optimum_mode - set regulator optimum operating mode
2396  * @regulator: regulator source
2397  * @uA_load: load current
2398  *
2399  * Notifies the regulator core of a new device load. This is then used by
2400  * DRMS (if enabled by constraints) to set the most efficient regulator
2401  * operating mode for the new regulator loading.
2402  *
2403  * Consumer devices notify their supply regulator of the maximum power
2404  * they will require (can be taken from device datasheet in the power
2405  * consumption tables) when they change operational status and hence power
2406  * state. Examples of operational state changes that can affect power
2407  * consumption are :-
2408  *
2409  *    o Device is opened / closed.
2410  *    o Device I/O is about to begin or has just finished.
2411  *    o Device is idling in between work.
2412  *
2413  * This information is also exported via sysfs to userspace.
2414  *
2415  * DRMS will sum the total requested load on the regulator and change
2416  * to the most efficient operating mode if platform constraints allow.
2417  *
2418  * Returns the new regulator mode or error.
2419  */
2420 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2421 {
2422         struct regulator_dev *rdev = regulator->rdev;
2423         struct regulator *consumer;
2424         int ret, output_uV, input_uV, total_uA_load = 0;
2425         unsigned int mode;
2426
2427         mutex_lock(&rdev->mutex);
2428
2429         /*
2430          * first check to see if we can set modes at all, otherwise just
2431          * tell the consumer everything is OK.
2432          */
2433         regulator->uA_load = uA_load;
2434         ret = regulator_check_drms(rdev);
2435         if (ret < 0) {
2436                 ret = 0;
2437                 goto out;
2438         }
2439
2440         if (!rdev->desc->ops->get_optimum_mode)
2441                 goto out;
2442
2443         /*
2444          * we can actually do this so any errors are indicators of
2445          * potential real failure.
2446          */
2447         ret = -EINVAL;
2448
2449         /* get output voltage */
2450         output_uV = _regulator_get_voltage(rdev);
2451         if (output_uV <= 0) {
2452                 rdev_err(rdev, "invalid output voltage found\n");
2453                 goto out;
2454         }
2455
2456         /* get input voltage */
2457         input_uV = 0;
2458         if (rdev->supply)
2459                 input_uV = regulator_get_voltage(rdev->supply);
2460         if (input_uV <= 0)
2461                 input_uV = rdev->constraints->input_uV;
2462         if (input_uV <= 0) {
2463                 rdev_err(rdev, "invalid input voltage found\n");
2464                 goto out;
2465         }
2466
2467         /* calc total requested load for this regulator */
2468         list_for_each_entry(consumer, &rdev->consumer_list, list)
2469                 total_uA_load += consumer->uA_load;
2470
2471         mode = rdev->desc->ops->get_optimum_mode(rdev,
2472                                                  input_uV, output_uV,
2473                                                  total_uA_load);
2474         ret = regulator_mode_constrain(rdev, &mode);
2475         if (ret < 0) {
2476                 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2477                          total_uA_load, input_uV, output_uV);
2478                 goto out;
2479         }
2480
2481         ret = rdev->desc->ops->set_mode(rdev, mode);
2482         if (ret < 0) {
2483                 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2484                 goto out;
2485         }
2486         ret = mode;
2487 out:
2488         mutex_unlock(&rdev->mutex);
2489         return ret;
2490 }
2491 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2492
2493 /**
2494  * regulator_register_notifier - register regulator event notifier
2495  * @regulator: regulator source
2496  * @nb: notifier block
2497  *
2498  * Register notifier block to receive regulator events.
2499  */
2500 int regulator_register_notifier(struct regulator *regulator,
2501                               struct notifier_block *nb)
2502 {
2503         return blocking_notifier_chain_register(&regulator->rdev->notifier,
2504                                                 nb);
2505 }
2506 EXPORT_SYMBOL_GPL(regulator_register_notifier);
2507
2508 /**
2509  * regulator_unregister_notifier - unregister regulator event notifier
2510  * @regulator: regulator source
2511  * @nb: notifier block
2512  *
2513  * Unregister regulator event notifier block.
2514  */
2515 int regulator_unregister_notifier(struct regulator *regulator,
2516                                 struct notifier_block *nb)
2517 {
2518         return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2519                                                   nb);
2520 }
2521 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2522
2523 /* notify regulator consumers and downstream regulator consumers.
2524  * Note mutex must be held by caller.
2525  */
2526 static void _notifier_call_chain(struct regulator_dev *rdev,
2527                                   unsigned long event, void *data)
2528 {
2529         /* call rdev chain first */
2530         blocking_notifier_call_chain(&rdev->notifier, event, NULL);
2531 }
2532
2533 /**
2534  * regulator_bulk_get - get multiple regulator consumers
2535  *
2536  * @dev:           Device to supply
2537  * @num_consumers: Number of consumers to register
2538  * @consumers:     Configuration of consumers; clients are stored here.
2539  *
2540  * @return 0 on success, an errno on failure.
2541  *
2542  * This helper function allows drivers to get several regulator
2543  * consumers in one operation.  If any of the regulators cannot be
2544  * acquired then any regulators that were allocated will be freed
2545  * before returning to the caller.
2546  */
2547 int regulator_bulk_get(struct device *dev, int num_consumers,
2548                        struct regulator_bulk_data *consumers)
2549 {
2550         int i;
2551         int ret;
2552
2553         for (i = 0; i < num_consumers; i++)
2554                 consumers[i].consumer = NULL;
2555
2556         for (i = 0; i < num_consumers; i++) {
2557                 consumers[i].consumer = regulator_get(dev,
2558                                                       consumers[i].supply);
2559                 if (IS_ERR(consumers[i].consumer)) {
2560                         ret = PTR_ERR(consumers[i].consumer);
2561                         dev_err(dev, "Failed to get supply '%s': %d\n",
2562                                 consumers[i].supply, ret);
2563                         consumers[i].consumer = NULL;
2564                         goto err;
2565                 }
2566         }
2567
2568         return 0;
2569
2570 err:
2571         while (--i >= 0)
2572                 regulator_put(consumers[i].consumer);
2573
2574         return ret;
2575 }
2576 EXPORT_SYMBOL_GPL(regulator_bulk_get);
2577
2578 /**
2579  * devm_regulator_bulk_get - managed get multiple regulator consumers
2580  *
2581  * @dev:           Device to supply
2582  * @num_consumers: Number of consumers to register
2583  * @consumers:     Configuration of consumers; clients are stored here.
2584  *
2585  * @return 0 on success, an errno on failure.
2586  *
2587  * This helper function allows drivers to get several regulator
2588  * consumers in one operation with management, the regulators will
2589  * automatically be freed when the device is unbound.  If any of the
2590  * regulators cannot be acquired then any regulators that were
2591  * allocated will be freed before returning to the caller.
2592  */
2593 int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2594                             struct regulator_bulk_data *consumers)
2595 {
2596         int i;
2597         int ret;
2598
2599         for (i = 0; i < num_consumers; i++)
2600                 consumers[i].consumer = NULL;
2601
2602         for (i = 0; i < num_consumers; i++) {
2603                 consumers[i].consumer = devm_regulator_get(dev,
2604                                                            consumers[i].supply);
2605                 if (IS_ERR(consumers[i].consumer)) {
2606                         ret = PTR_ERR(consumers[i].consumer);
2607                         dev_err(dev, "Failed to get supply '%s': %d\n",
2608                                 consumers[i].supply, ret);
2609                         consumers[i].consumer = NULL;
2610                         goto err;
2611                 }
2612         }
2613
2614         return 0;
2615
2616 err:
2617         for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2618                 devm_regulator_put(consumers[i].consumer);
2619
2620         return ret;
2621 }
2622 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2623
2624 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2625 {
2626         struct regulator_bulk_data *bulk = data;
2627
2628         bulk->ret = regulator_enable(bulk->consumer);
2629 }
2630
2631 /**
2632  * regulator_bulk_enable - enable multiple regulator consumers
2633  *
2634  * @num_consumers: Number of consumers
2635  * @consumers:     Consumer data; clients are stored here.
2636  * @return         0 on success, an errno on failure
2637  *
2638  * This convenience API allows consumers to enable multiple regulator
2639  * clients in a single API call.  If any consumers cannot be enabled
2640  * then any others that were enabled will be disabled again prior to
2641  * return.
2642  */
2643 int regulator_bulk_enable(int num_consumers,
2644                           struct regulator_bulk_data *consumers)
2645 {
2646         LIST_HEAD(async_domain);
2647         int i;
2648         int ret = 0;
2649
2650         for (i = 0; i < num_consumers; i++)
2651                 async_schedule_domain(regulator_bulk_enable_async,
2652                                       &consumers[i], &async_domain);
2653
2654         async_synchronize_full_domain(&async_domain);
2655
2656         /* If any consumer failed we need to unwind any that succeeded */
2657         for (i = 0; i < num_consumers; i++) {
2658                 if (consumers[i].ret != 0) {
2659                         ret = consumers[i].ret;
2660                         goto err;
2661                 }
2662         }
2663
2664         return 0;
2665
2666 err:
2667         pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2668         while (--i >= 0)
2669                 regulator_disable(consumers[i].consumer);
2670
2671         return ret;
2672 }
2673 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2674
2675 /**
2676  * regulator_bulk_disable - disable multiple regulator consumers
2677  *
2678  * @num_consumers: Number of consumers
2679  * @consumers:     Consumer data; clients are stored here.
2680  * @return         0 on success, an errno on failure
2681  *
2682  * This convenience API allows consumers to disable multiple regulator
2683  * clients in a single API call.  If any consumers cannot be disabled
2684  * then any others that were disabled will be enabled again prior to
2685  * return.
2686  */
2687 int regulator_bulk_disable(int num_consumers,
2688                            struct regulator_bulk_data *consumers)
2689 {
2690         int i;
2691         int ret;
2692
2693         for (i = num_consumers - 1; i >= 0; --i) {
2694                 ret = regulator_disable(consumers[i].consumer);
2695                 if (ret != 0)
2696                         goto err;
2697         }
2698
2699         return 0;
2700
2701 err:
2702         pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
2703         for (++i; i < num_consumers; ++i)
2704                 regulator_enable(consumers[i].consumer);
2705
2706         return ret;
2707 }
2708 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2709
2710 /**
2711  * regulator_bulk_force_disable - force disable multiple regulator consumers
2712  *
2713  * @num_consumers: Number of consumers
2714  * @consumers:     Consumer data; clients are stored here.
2715  * @return         0 on success, an errno on failure
2716  *
2717  * This convenience API allows consumers to forcibly disable multiple regulator
2718  * clients in a single API call.
2719  * NOTE: This should be used for situations when device damage will
2720  * likely occur if the regulators are not disabled (e.g. over temp).
2721  * Although regulator_force_disable function call for some consumers can
2722  * return error numbers, the function is called for all consumers.
2723  */
2724 int regulator_bulk_force_disable(int num_consumers,
2725                            struct regulator_bulk_data *consumers)
2726 {
2727         int i;
2728         int ret;
2729
2730         for (i = 0; i < num_consumers; i++)
2731                 consumers[i].ret =
2732                             regulator_force_disable(consumers[i].consumer);
2733
2734         for (i = 0; i < num_consumers; i++) {
2735                 if (consumers[i].ret != 0) {
2736                         ret = consumers[i].ret;
2737                         goto out;
2738                 }
2739         }
2740
2741         return 0;
2742 out:
2743         return ret;
2744 }
2745 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
2746
2747 /**
2748  * regulator_bulk_free - free multiple regulator consumers
2749  *
2750  * @num_consumers: Number of consumers
2751  * @consumers:     Consumer data; clients are stored here.
2752  *
2753  * This convenience API allows consumers to free multiple regulator
2754  * clients in a single API call.
2755  */
2756 void regulator_bulk_free(int num_consumers,
2757                          struct regulator_bulk_data *consumers)
2758 {
2759         int i;
2760
2761         for (i = 0; i < num_consumers; i++) {
2762                 regulator_put(consumers[i].consumer);
2763                 consumers[i].consumer = NULL;
2764         }
2765 }
2766 EXPORT_SYMBOL_GPL(regulator_bulk_free);
2767
2768 /**
2769  * regulator_notifier_call_chain - call regulator event notifier
2770  * @rdev: regulator source
2771  * @event: notifier block
2772  * @data: callback-specific data.
2773  *
2774  * Called by regulator drivers to notify clients a regulator event has
2775  * occurred. We also notify regulator clients downstream.
2776  * Note lock must be held by caller.
2777  */
2778 int regulator_notifier_call_chain(struct regulator_dev *rdev,
2779                                   unsigned long event, void *data)
2780 {
2781         _notifier_call_chain(rdev, event, data);
2782         return NOTIFY_DONE;
2783
2784 }
2785 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2786
2787 /**
2788  * regulator_mode_to_status - convert a regulator mode into a status
2789  *
2790  * @mode: Mode to convert
2791  *
2792  * Convert a regulator mode into a status.
2793  */
2794 int regulator_mode_to_status(unsigned int mode)
2795 {
2796         switch (mode) {
2797         case REGULATOR_MODE_FAST:
2798                 return REGULATOR_STATUS_FAST;
2799         case REGULATOR_MODE_NORMAL:
2800                 return REGULATOR_STATUS_NORMAL;
2801         case REGULATOR_MODE_IDLE:
2802                 return REGULATOR_STATUS_IDLE;
2803         case REGULATOR_STATUS_STANDBY:
2804                 return REGULATOR_STATUS_STANDBY;
2805         default:
2806                 return 0;
2807         }
2808 }
2809 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2810
2811 /*
2812  * To avoid cluttering sysfs (and memory) with useless state, only
2813  * create attributes that can be meaningfully displayed.
2814  */
2815 static int add_regulator_attributes(struct regulator_dev *rdev)
2816 {
2817         struct device           *dev = &rdev->dev;
2818         struct regulator_ops    *ops = rdev->desc->ops;
2819         int                     status = 0;
2820
2821         /* some attributes need specific methods to be displayed */
2822         if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
2823             (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
2824                 status = device_create_file(dev, &dev_attr_microvolts);
2825                 if (status < 0)
2826                         return status;
2827         }
2828         if (ops->get_current_limit) {
2829                 status = device_create_file(dev, &dev_attr_microamps);
2830                 if (status < 0)
2831                         return status;
2832         }
2833         if (ops->get_mode) {
2834                 status = device_create_file(dev, &dev_attr_opmode);
2835                 if (status < 0)
2836                         return status;
2837         }
2838         if (ops->is_enabled) {
2839                 status = device_create_file(dev, &dev_attr_state);
2840                 if (status < 0)
2841                         return status;
2842         }
2843         if (ops->get_status) {
2844                 status = device_create_file(dev, &dev_attr_status);
2845                 if (status < 0)
2846                         return status;
2847         }
2848
2849         /* some attributes are type-specific */
2850         if (rdev->desc->type == REGULATOR_CURRENT) {
2851                 status = device_create_file(dev, &dev_attr_requested_microamps);
2852                 if (status < 0)
2853                         return status;
2854         }
2855
2856         /* all the other attributes exist to support constraints;
2857          * don't show them if there are no constraints, or if the
2858          * relevant supporting methods are missing.
2859          */
2860         if (!rdev->constraints)
2861                 return status;
2862
2863         /* constraints need specific supporting methods */
2864         if (ops->set_voltage || ops->set_voltage_sel) {
2865                 status = device_create_file(dev, &dev_attr_min_microvolts);
2866                 if (status < 0)
2867                         return status;
2868                 status = device_create_file(dev, &dev_attr_max_microvolts);
2869                 if (status < 0)
2870                         return status;
2871         }
2872         if (ops->set_current_limit) {
2873                 status = device_create_file(dev, &dev_attr_min_microamps);
2874                 if (status < 0)
2875                         return status;
2876                 status = device_create_file(dev, &dev_attr_max_microamps);
2877                 if (status < 0)
2878                         return status;
2879         }
2880
2881         /* suspend mode constraints need multiple supporting methods */
2882         if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2883                 return status;
2884
2885         status = device_create_file(dev, &dev_attr_suspend_standby_state);
2886         if (status < 0)
2887                 return status;
2888         status = device_create_file(dev, &dev_attr_suspend_mem_state);
2889         if (status < 0)
2890                 return status;
2891         status = device_create_file(dev, &dev_attr_suspend_disk_state);
2892         if (status < 0)
2893                 return status;
2894
2895         if (ops->set_suspend_voltage) {
2896                 status = device_create_file(dev,
2897                                 &dev_attr_suspend_standby_microvolts);
2898                 if (status < 0)
2899                         return status;
2900                 status = device_create_file(dev,
2901                                 &dev_attr_suspend_mem_microvolts);
2902                 if (status < 0)
2903                         return status;
2904                 status = device_create_file(dev,
2905                                 &dev_attr_suspend_disk_microvolts);
2906                 if (status < 0)
2907                         return status;
2908         }
2909
2910         if (ops->set_suspend_mode) {
2911                 status = device_create_file(dev,
2912                                 &dev_attr_suspend_standby_mode);
2913                 if (status < 0)
2914                         return status;
2915                 status = device_create_file(dev,
2916                                 &dev_attr_suspend_mem_mode);
2917                 if (status < 0)
2918                         return status;
2919                 status = device_create_file(dev,
2920                                 &dev_attr_suspend_disk_mode);
2921                 if (status < 0)
2922                         return status;
2923         }
2924
2925         return status;
2926 }
2927
2928 static void rdev_init_debugfs(struct regulator_dev *rdev)
2929 {
2930         rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
2931         if (!rdev->debugfs) {
2932                 rdev_warn(rdev, "Failed to create debugfs directory\n");
2933                 return;
2934         }
2935
2936         debugfs_create_u32("use_count", 0444, rdev->debugfs,
2937                            &rdev->use_count);
2938         debugfs_create_u32("open_count", 0444, rdev->debugfs,
2939                            &rdev->open_count);
2940 }
2941
2942 /**
2943  * regulator_register - register regulator
2944  * @regulator_desc: regulator to register
2945  * @dev: struct device for the regulator
2946  * @init_data: platform provided init data, passed through by driver
2947  * @driver_data: private regulator data
2948  * @of_node: OpenFirmware node to parse for device tree bindings (may be
2949  *           NULL).
2950  *
2951  * Called by regulator drivers to register a regulator.
2952  * Returns 0 on success.
2953  */
2954 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2955         struct device *dev, const struct regulator_init_data *init_data,
2956         void *driver_data, struct device_node *of_node)
2957 {
2958         const struct regulation_constraints *constraints = NULL;
2959         static atomic_t regulator_no = ATOMIC_INIT(0);
2960         struct regulator_dev *rdev;
2961         int ret, i;
2962         const char *supply = NULL;
2963
2964         if (regulator_desc == NULL)
2965                 return ERR_PTR(-EINVAL);
2966
2967         if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2968                 return ERR_PTR(-EINVAL);
2969
2970         if (regulator_desc->type != REGULATOR_VOLTAGE &&
2971             regulator_desc->type != REGULATOR_CURRENT)
2972                 return ERR_PTR(-EINVAL);
2973
2974         /* Only one of each should be implemented */
2975         WARN_ON(regulator_desc->ops->get_voltage &&
2976                 regulator_desc->ops->get_voltage_sel);
2977         WARN_ON(regulator_desc->ops->set_voltage &&
2978                 regulator_desc->ops->set_voltage_sel);
2979
2980         /* If we're using selectors we must implement list_voltage. */
2981         if (regulator_desc->ops->get_voltage_sel &&
2982             !regulator_desc->ops->list_voltage) {
2983                 return ERR_PTR(-EINVAL);
2984         }
2985         if (regulator_desc->ops->set_voltage_sel &&
2986             !regulator_desc->ops->list_voltage) {
2987                 return ERR_PTR(-EINVAL);
2988         }
2989
2990         rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2991         if (rdev == NULL)
2992                 return ERR_PTR(-ENOMEM);
2993
2994         mutex_lock(&regulator_list_mutex);
2995
2996         mutex_init(&rdev->mutex);
2997         rdev->reg_data = driver_data;
2998         rdev->owner = regulator_desc->owner;
2999         rdev->desc = regulator_desc;
3000         INIT_LIST_HEAD(&rdev->consumer_list);
3001         INIT_LIST_HEAD(&rdev->list);
3002         BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3003         INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3004
3005         /* preform any regulator specific init */
3006         if (init_data && init_data->regulator_init) {
3007                 ret = init_data->regulator_init(rdev->reg_data);
3008                 if (ret < 0)
3009                         goto clean;
3010         }
3011
3012         /* register with sysfs */
3013         rdev->dev.class = &regulator_class;
3014         rdev->dev.of_node = of_node;
3015         rdev->dev.parent = dev;
3016         dev_set_name(&rdev->dev, "regulator.%d",
3017                      atomic_inc_return(&regulator_no) - 1);
3018         ret = device_register(&rdev->dev);
3019         if (ret != 0) {
3020                 put_device(&rdev->dev);
3021                 goto clean;
3022         }
3023
3024         dev_set_drvdata(&rdev->dev, rdev);
3025
3026         /* set regulator constraints */
3027         if (init_data)
3028                 constraints = &init_data->constraints;
3029
3030         ret = set_machine_constraints(rdev, constraints);
3031         if (ret < 0)
3032                 goto scrub;
3033
3034         /* add attributes supported by this regulator */
3035         ret = add_regulator_attributes(rdev);
3036         if (ret < 0)
3037                 goto scrub;
3038
3039         if (init_data && init_data->supply_regulator)
3040                 supply = init_data->supply_regulator;
3041         else if (regulator_desc->supply_name)
3042                 supply = regulator_desc->supply_name;
3043
3044         if (supply) {
3045                 struct regulator_dev *r;
3046
3047                 r = regulator_dev_lookup(dev, supply);
3048                 if (!r) {
3049                         dev_err(dev, "Failed to find supply %s\n", supply);
3050                         ret = -EPROBE_DEFER;
3051                         goto scrub;
3052                 }
3053
3054                 ret = set_supply(rdev, r);
3055                 if (ret < 0)
3056                         goto scrub;
3057
3058                 /* Enable supply if rail is enabled */
3059                 if (rdev->desc->ops->is_enabled &&
3060                                 rdev->desc->ops->is_enabled(rdev)) {
3061                         ret = regulator_enable(rdev->supply);
3062                         if (ret < 0)
3063                                 goto scrub;
3064                 }
3065         }
3066
3067         /* add consumers devices */
3068         if (init_data) {
3069                 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3070                         ret = set_consumer_device_supply(rdev,
3071                                 init_data->consumer_supplies[i].dev_name,
3072                                 init_data->consumer_supplies[i].supply);
3073                         if (ret < 0) {
3074                                 dev_err(dev, "Failed to set supply %s\n",
3075                                         init_data->consumer_supplies[i].supply);
3076                                 goto unset_supplies;
3077                         }
3078                 }
3079         }
3080
3081         list_add(&rdev->list, &regulator_list);
3082
3083         rdev_init_debugfs(rdev);
3084 out:
3085         mutex_unlock(&regulator_list_mutex);
3086         return rdev;
3087
3088 unset_supplies:
3089         unset_regulator_supplies(rdev);
3090
3091 scrub:
3092         if (rdev->supply)
3093                 regulator_put(rdev->supply);
3094         kfree(rdev->constraints);
3095         device_unregister(&rdev->dev);
3096         /* device core frees rdev */
3097         rdev = ERR_PTR(ret);
3098         goto out;
3099
3100 clean:
3101         kfree(rdev);
3102         rdev = ERR_PTR(ret);
3103         goto out;
3104 }
3105 EXPORT_SYMBOL_GPL(regulator_register);
3106
3107 /**
3108  * regulator_unregister - unregister regulator
3109  * @rdev: regulator to unregister
3110  *
3111  * Called by regulator drivers to unregister a regulator.
3112  */
3113 void regulator_unregister(struct regulator_dev *rdev)
3114 {
3115         if (rdev == NULL)
3116                 return;
3117
3118         if (rdev->supply)
3119                 regulator_put(rdev->supply);
3120         mutex_lock(&regulator_list_mutex);
3121         debugfs_remove_recursive(rdev->debugfs);
3122         flush_work_sync(&rdev->disable_work.work);
3123         WARN_ON(rdev->open_count);
3124         unset_regulator_supplies(rdev);
3125         list_del(&rdev->list);
3126         kfree(rdev->constraints);
3127         device_unregister(&rdev->dev);
3128         mutex_unlock(&regulator_list_mutex);
3129 }
3130 EXPORT_SYMBOL_GPL(regulator_unregister);
3131
3132 /**
3133  * regulator_suspend_prepare - prepare regulators for system wide suspend
3134  * @state: system suspend state
3135  *
3136  * Configure each regulator with it's suspend operating parameters for state.
3137  * This will usually be called by machine suspend code prior to supending.
3138  */
3139 int regulator_suspend_prepare(suspend_state_t state)
3140 {
3141         struct regulator_dev *rdev;
3142         int ret = 0;
3143
3144         /* ON is handled by regulator active state */
3145         if (state == PM_SUSPEND_ON)
3146                 return -EINVAL;
3147
3148         mutex_lock(&regulator_list_mutex);
3149         list_for_each_entry(rdev, &regulator_list, list) {
3150
3151                 mutex_lock(&rdev->mutex);
3152                 ret = suspend_prepare(rdev, state);
3153                 mutex_unlock(&rdev->mutex);
3154
3155                 if (ret < 0) {
3156                         rdev_err(rdev, "failed to prepare\n");
3157                         goto out;
3158                 }
3159         }
3160 out:
3161         mutex_unlock(&regulator_list_mutex);
3162         return ret;
3163 }
3164 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3165
3166 /**
3167  * regulator_suspend_finish - resume regulators from system wide suspend
3168  *
3169  * Turn on regulators that might be turned off by regulator_suspend_prepare
3170  * and that should be turned on according to the regulators properties.
3171  */
3172 int regulator_suspend_finish(void)
3173 {
3174         struct regulator_dev *rdev;
3175         int ret = 0, error;
3176
3177         mutex_lock(&regulator_list_mutex);
3178         list_for_each_entry(rdev, &regulator_list, list) {
3179                 struct regulator_ops *ops = rdev->desc->ops;
3180
3181                 mutex_lock(&rdev->mutex);
3182                 if ((rdev->use_count > 0  || rdev->constraints->always_on) &&
3183                                 ops->enable) {
3184                         error = ops->enable(rdev);
3185                         if (error)
3186                                 ret = error;
3187                 } else {
3188                         if (!has_full_constraints)
3189                                 goto unlock;
3190                         if (!ops->disable)
3191                                 goto unlock;
3192                         if (ops->is_enabled && !ops->is_enabled(rdev))
3193                                 goto unlock;
3194
3195                         error = ops->disable(rdev);
3196                         if (error)
3197                                 ret = error;
3198                 }
3199 unlock:
3200                 mutex_unlock(&rdev->mutex);
3201         }
3202         mutex_unlock(&regulator_list_mutex);
3203         return ret;
3204 }
3205 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3206
3207 /**
3208  * regulator_has_full_constraints - the system has fully specified constraints
3209  *
3210  * Calling this function will cause the regulator API to disable all
3211  * regulators which have a zero use count and don't have an always_on
3212  * constraint in a late_initcall.
3213  *
3214  * The intention is that this will become the default behaviour in a
3215  * future kernel release so users are encouraged to use this facility
3216  * now.
3217  */
3218 void regulator_has_full_constraints(void)
3219 {
3220         has_full_constraints = 1;
3221 }
3222 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3223
3224 /**
3225  * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3226  *
3227  * Calling this function will cause the regulator API to provide a
3228  * dummy regulator to consumers if no physical regulator is found,
3229  * allowing most consumers to proceed as though a regulator were
3230  * configured.  This allows systems such as those with software
3231  * controllable regulators for the CPU core only to be brought up more
3232  * readily.
3233  */
3234 void regulator_use_dummy_regulator(void)
3235 {
3236         board_wants_dummy_regulator = true;
3237 }
3238 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3239
3240 /**
3241  * rdev_get_drvdata - get rdev regulator driver data
3242  * @rdev: regulator
3243  *
3244  * Get rdev regulator driver private data. This call can be used in the
3245  * regulator driver context.
3246  */
3247 void *rdev_get_drvdata(struct regulator_dev *rdev)
3248 {
3249         return rdev->reg_data;
3250 }
3251 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3252
3253 /**
3254  * regulator_get_drvdata - get regulator driver data
3255  * @regulator: regulator
3256  *
3257  * Get regulator driver private data. This call can be used in the consumer
3258  * driver context when non API regulator specific functions need to be called.
3259  */
3260 void *regulator_get_drvdata(struct regulator *regulator)
3261 {
3262         return regulator->rdev->reg_data;
3263 }
3264 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3265
3266 /**
3267  * regulator_set_drvdata - set regulator driver data
3268  * @regulator: regulator
3269  * @data: data
3270  */
3271 void regulator_set_drvdata(struct regulator *regulator, void *data)
3272 {
3273         regulator->rdev->reg_data = data;
3274 }
3275 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3276
3277 /**
3278  * regulator_get_id - get regulator ID
3279  * @rdev: regulator
3280  */
3281 int rdev_get_id(struct regulator_dev *rdev)
3282 {
3283         return rdev->desc->id;
3284 }
3285 EXPORT_SYMBOL_GPL(rdev_get_id);
3286
3287 struct device *rdev_get_dev(struct regulator_dev *rdev)
3288 {
3289         return &rdev->dev;
3290 }
3291 EXPORT_SYMBOL_GPL(rdev_get_dev);
3292
3293 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3294 {
3295         return reg_init_data->driver_data;
3296 }
3297 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3298
3299 #ifdef CONFIG_DEBUG_FS
3300 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3301                                     size_t count, loff_t *ppos)
3302 {
3303         char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3304         ssize_t len, ret = 0;
3305         struct regulator_map *map;
3306
3307         if (!buf)
3308                 return -ENOMEM;
3309
3310         list_for_each_entry(map, &regulator_map_list, list) {
3311                 len = snprintf(buf + ret, PAGE_SIZE - ret,
3312                                "%s -> %s.%s\n",
3313                                rdev_get_name(map->regulator), map->dev_name,
3314                                map->supply);
3315                 if (len >= 0)
3316                         ret += len;
3317                 if (ret > PAGE_SIZE) {
3318                         ret = PAGE_SIZE;
3319                         break;
3320                 }
3321         }
3322
3323         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3324
3325         kfree(buf);
3326
3327         return ret;
3328 }
3329 #endif
3330
3331 static const struct file_operations supply_map_fops = {
3332 #ifdef CONFIG_DEBUG_FS
3333         .read = supply_map_read_file,
3334         .llseek = default_llseek,
3335 #endif
3336 };
3337
3338 static int __init regulator_init(void)
3339 {
3340         int ret;
3341
3342         ret = class_register(&regulator_class);
3343
3344         debugfs_root = debugfs_create_dir("regulator", NULL);
3345         if (!debugfs_root)
3346                 pr_warn("regulator: Failed to create debugfs directory\n");
3347
3348         debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3349                             &supply_map_fops);
3350
3351         regulator_dummy_init();
3352
3353         return ret;
3354 }
3355
3356 /* init early to allow our consumers to complete system booting */
3357 core_initcall(regulator_init);
3358
3359 static int __init regulator_init_complete(void)
3360 {
3361         struct regulator_dev *rdev;
3362         struct regulator_ops *ops;
3363         struct regulation_constraints *c;
3364         int enabled, ret;
3365
3366         mutex_lock(&regulator_list_mutex);
3367
3368         /* If we have a full configuration then disable any regulators
3369          * which are not in use or always_on.  This will become the
3370          * default behaviour in the future.
3371          */
3372         list_for_each_entry(rdev, &regulator_list, list) {
3373                 ops = rdev->desc->ops;
3374                 c = rdev->constraints;
3375
3376                 if (!ops->disable || (c && c->always_on))
3377                         continue;
3378
3379                 mutex_lock(&rdev->mutex);
3380
3381                 if (rdev->use_count)
3382                         goto unlock;
3383
3384                 /* If we can't read the status assume it's on. */
3385                 if (ops->is_enabled)
3386                         enabled = ops->is_enabled(rdev);
3387                 else
3388                         enabled = 1;
3389
3390                 if (!enabled)
3391                         goto unlock;
3392
3393                 if (has_full_constraints) {
3394                         /* We log since this may kill the system if it
3395                          * goes wrong. */
3396                         rdev_info(rdev, "disabling\n");
3397                         ret = ops->disable(rdev);
3398                         if (ret != 0) {
3399                                 rdev_err(rdev, "couldn't disable: %d\n", ret);
3400                         }
3401                 } else {
3402                         /* The intention is that in future we will
3403                          * assume that full constraints are provided
3404                          * so warn even if we aren't going to do
3405                          * anything here.
3406                          */
3407                         rdev_warn(rdev, "incomplete constraints, leaving on\n");
3408                 }
3409
3410 unlock:
3411                 mutex_unlock(&rdev->mutex);
3412         }
3413
3414         mutex_unlock(&regulator_list_mutex);
3415
3416         return 0;
3417 }
3418
3419 #ifdef CONFIG_DEBUG_FS
3420 static int regulator_syncevent(struct file *file, const char __user *user_buf,
3421                                 size_t count, loff_t *ppos)
3422 {
3423         struct regulator_dev *rdev;
3424         char buffer[40];
3425         int buf_size;
3426
3427         memset(buffer, 0, sizeof(buffer));
3428         buf_size = min(count, (sizeof(buffer)-1));
3429
3430         if (copy_from_user(buffer, user_buf, buf_size))
3431                 return -EFAULT;
3432
3433         if (!strnicmp("all", buffer, 3)) {
3434
3435                 mutex_lock(&regulator_list_mutex);
3436
3437                 list_for_each_entry(rdev, &regulator_list, list) {
3438                         mutex_lock(&rdev->mutex);
3439
3440                         if (_regulator_is_enabled(rdev))
3441                                 trace_regulator_enable(rdev_get_name(rdev));
3442                         else
3443                                 trace_regulator_disable(rdev_get_name(rdev));
3444
3445                         trace_regulator_set_voltage(rdev_get_name(rdev),
3446                                 _regulator_get_voltage(rdev),
3447                                 _regulator_get_voltage(rdev));
3448
3449                         mutex_unlock(&rdev->mutex);
3450                 }
3451         }
3452
3453         mutex_unlock(&regulator_list_mutex);
3454
3455         return count;
3456 }
3457
3458 static const struct file_operations regulator_syncevent_fops = {
3459         .write          = regulator_syncevent,
3460 };
3461
3462 static int __init regulator_init_debugfs(void)
3463 {
3464         debugfs_create_file("syncevent_regulators", S_IWUSR, NULL, NULL,
3465                         &regulator_syncevent_fops);
3466
3467         return 0;
3468 }
3469
3470 late_initcall(regulator_init_debugfs);
3471 #endif
3472
3473 late_initcall(regulator_init_complete);