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