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