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