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