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