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