add RGA param_x when scale down
[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 /* regulator_list_mutex lock 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         mutex_lock(&rdev->mutex);
1411         kfree(regulator->supply_name);
1412         list_del(&regulator->list);
1413         kfree(regulator);
1414
1415         rdev->open_count--;
1416         rdev->exclusive = 0;
1417         mutex_unlock(&rdev->mutex);
1418
1419         module_put(rdev->owner);
1420 }
1421
1422 /**
1423  * regulator_put - "free" the regulator source
1424  * @regulator: regulator source
1425  *
1426  * Note: drivers must ensure that all regulator_enable calls made on this
1427  * regulator source are balanced by regulator_disable calls prior to calling
1428  * this function.
1429  */
1430 void regulator_put(struct regulator *regulator)
1431 {
1432         mutex_lock(&regulator_list_mutex);
1433         _regulator_put(regulator);
1434         mutex_unlock(&regulator_list_mutex);
1435 }
1436 EXPORT_SYMBOL_GPL(regulator_put);
1437
1438 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1439 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1440                                 const struct regulator_config *config)
1441 {
1442         struct regulator_enable_gpio *pin;
1443         int ret;
1444
1445         list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
1446                 if (pin->gpio == config->ena_gpio) {
1447                         rdev_dbg(rdev, "GPIO %d is already used\n",
1448                                 config->ena_gpio);
1449                         goto update_ena_gpio_to_rdev;
1450                 }
1451         }
1452
1453         ret = gpio_request_one(config->ena_gpio,
1454                                 GPIOF_DIR_OUT | config->ena_gpio_flags,
1455                                 rdev_get_name(rdev));
1456         if (ret)
1457                 return ret;
1458
1459         pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1460         if (pin == NULL) {
1461                 gpio_free(config->ena_gpio);
1462                 return -ENOMEM;
1463         }
1464
1465         pin->gpio = config->ena_gpio;
1466         pin->ena_gpio_invert = config->ena_gpio_invert;
1467         list_add(&pin->list, &regulator_ena_gpio_list);
1468
1469 update_ena_gpio_to_rdev:
1470         pin->request_count++;
1471         rdev->ena_pin = pin;
1472         return 0;
1473 }
1474
1475 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1476 {
1477         struct regulator_enable_gpio *pin, *n;
1478
1479         if (!rdev->ena_pin)
1480                 return;
1481
1482         /* Free the GPIO only in case of no use */
1483         list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
1484                 if (pin->gpio == rdev->ena_pin->gpio) {
1485                         if (pin->request_count <= 1) {
1486                                 pin->request_count = 0;
1487                                 gpio_free(pin->gpio);
1488                                 list_del(&pin->list);
1489                                 kfree(pin);
1490                         } else {
1491                                 pin->request_count--;
1492                         }
1493                 }
1494         }
1495 }
1496
1497 /**
1498  * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1499  * @rdev: regulator_dev structure
1500  * @enable: enable GPIO at initial use?
1501  *
1502  * GPIO is enabled in case of initial use. (enable_count is 0)
1503  * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1504  */
1505 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1506 {
1507         struct regulator_enable_gpio *pin = rdev->ena_pin;
1508
1509         if (!pin)
1510                 return -EINVAL;
1511
1512         if (enable) {
1513                 /* Enable GPIO at initial use */
1514                 if (pin->enable_count == 0)
1515                         gpio_set_value_cansleep(pin->gpio,
1516                                                 !pin->ena_gpio_invert);
1517
1518                 pin->enable_count++;
1519         } else {
1520                 if (pin->enable_count > 1) {
1521                         pin->enable_count--;
1522                         return 0;
1523                 }
1524
1525                 /* Disable GPIO if not used */
1526                 if (pin->enable_count <= 1) {
1527                         gpio_set_value_cansleep(pin->gpio,
1528                                                 pin->ena_gpio_invert);
1529                         pin->enable_count = 0;
1530                 }
1531         }
1532
1533         return 0;
1534 }
1535
1536 static int _regulator_do_enable(struct regulator_dev *rdev)
1537 {
1538         int ret, delay;
1539
1540         /* Query before enabling in case configuration dependent.  */
1541         ret = _regulator_get_enable_time(rdev);
1542         if (ret >= 0) {
1543                 delay = ret;
1544         } else {
1545                 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1546                 delay = 0;
1547         }
1548
1549         trace_regulator_enable(rdev_get_name(rdev));
1550
1551         if (rdev->ena_pin) {
1552                 if (!rdev->ena_gpio_state) {
1553                         ret = regulator_ena_gpio_ctrl(rdev, true);
1554                         if (ret < 0)
1555                                 return ret;
1556                         rdev->ena_gpio_state = 1;
1557                 }
1558         } else if (rdev->desc->ops->enable) {
1559                 ret = rdev->desc->ops->enable(rdev);
1560                 if (ret < 0)
1561                         return ret;
1562         } else {
1563                 return -EINVAL;
1564         }
1565
1566         /* Allow the regulator to ramp; it would be useful to extend
1567          * this for bulk operations so that the regulators can ramp
1568          * together.  */
1569         trace_regulator_enable_delay(rdev_get_name(rdev));
1570
1571         if (delay >= 1000) {
1572                 mdelay(delay / 1000);
1573                 udelay(delay % 1000);
1574         } else if (delay) {
1575                 udelay(delay);
1576         }
1577
1578         trace_regulator_enable_complete(rdev_get_name(rdev));
1579
1580         return 0;
1581 }
1582
1583 /* locks held by regulator_enable() */
1584 static int _regulator_enable(struct regulator_dev *rdev)
1585 {
1586         int ret;
1587
1588         /* check voltage and requested load before enabling */
1589         if (rdev->constraints &&
1590             (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1591                 drms_uA_update(rdev);
1592
1593         if (rdev->use_count == 0) {
1594                 /* The regulator may on if it's not switchable or left on */
1595                 ret = _regulator_is_enabled(rdev);
1596                 if (ret == -EINVAL || ret == 0) {
1597                         if (!_regulator_can_change_status(rdev))
1598                                 return -EPERM;
1599
1600                         ret = _regulator_do_enable(rdev);
1601                         if (ret < 0)
1602                                 return ret;
1603
1604                 } else if (ret < 0) {
1605                         rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1606                         return ret;
1607                 }
1608                 /* Fallthrough on positive return values - already enabled */
1609         }
1610
1611         rdev->use_count++;
1612
1613         return 0;
1614 }
1615
1616 /**
1617  * regulator_enable - enable regulator output
1618  * @regulator: regulator source
1619  *
1620  * Request that the regulator be enabled with the regulator output at
1621  * the predefined voltage or current value.  Calls to regulator_enable()
1622  * must be balanced with calls to regulator_disable().
1623  *
1624  * NOTE: the output value can be set by other drivers, boot loader or may be
1625  * hardwired in the regulator.
1626  */
1627 int regulator_enable(struct regulator *regulator)
1628 {
1629         struct regulator_dev *rdev = regulator->rdev;
1630         int ret = 0;
1631
1632         if (regulator->always_on)
1633                 return 0;
1634
1635         if (rdev->supply) {
1636                 ret = regulator_enable(rdev->supply);
1637                 if (ret != 0)
1638                         return ret;
1639         }
1640
1641         mutex_lock(&rdev->mutex);
1642         ret = _regulator_enable(rdev);
1643         mutex_unlock(&rdev->mutex);
1644
1645         if (ret != 0 && rdev->supply)
1646                 regulator_disable(rdev->supply);
1647
1648         return ret;
1649 }
1650 EXPORT_SYMBOL_GPL(regulator_enable);
1651
1652 static int _regulator_do_disable(struct regulator_dev *rdev)
1653 {
1654         int ret;
1655
1656         trace_regulator_disable(rdev_get_name(rdev));
1657
1658         if (rdev->ena_pin) {
1659                 if (rdev->ena_gpio_state) {
1660                         ret = regulator_ena_gpio_ctrl(rdev, false);
1661                         if (ret < 0)
1662                                 return ret;
1663                         rdev->ena_gpio_state = 0;
1664                 }
1665
1666         } else if (rdev->desc->ops->disable) {
1667                 ret = rdev->desc->ops->disable(rdev);
1668                 if (ret != 0)
1669                         return ret;
1670         }
1671
1672         trace_regulator_disable_complete(rdev_get_name(rdev));
1673
1674         return 0;
1675 }
1676
1677 /* locks held by regulator_disable() */
1678 static int _regulator_disable(struct regulator_dev *rdev)
1679 {
1680         int ret = 0;
1681
1682         if (WARN(rdev->use_count <= 0,
1683                  "unbalanced disables for %s\n", rdev_get_name(rdev)))
1684                 return -EIO;
1685
1686         /* are we the last user and permitted to disable ? */
1687         if (rdev->use_count == 1 &&
1688             (rdev->constraints && !rdev->constraints->always_on)) {
1689
1690                 /* we are last user */
1691                 if (_regulator_can_change_status(rdev)) {
1692                         ret = _regulator_do_disable(rdev);
1693                         if (ret < 0) {
1694                                 rdev_err(rdev, "failed to disable\n");
1695                                 return ret;
1696                         }
1697                         _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1698                                         NULL);
1699                 }
1700
1701                 rdev->use_count = 0;
1702         } else if (rdev->use_count > 1) {
1703
1704                 if (rdev->constraints &&
1705                         (rdev->constraints->valid_ops_mask &
1706                         REGULATOR_CHANGE_DRMS))
1707                         drms_uA_update(rdev);
1708
1709                 rdev->use_count--;
1710         }
1711
1712         return ret;
1713 }
1714
1715 /**
1716  * regulator_disable - disable regulator output
1717  * @regulator: regulator source
1718  *
1719  * Disable the regulator output voltage or current.  Calls to
1720  * regulator_enable() must be balanced with calls to
1721  * regulator_disable().
1722  *
1723  * NOTE: this will only disable the regulator output if no other consumer
1724  * devices have it enabled, the regulator device supports disabling and
1725  * machine constraints permit this operation.
1726  */
1727 int regulator_disable(struct regulator *regulator)
1728 {
1729         struct regulator_dev *rdev = regulator->rdev;
1730         int ret = 0;
1731
1732         if (regulator->always_on)
1733                 return 0;
1734
1735         mutex_lock(&rdev->mutex);
1736         ret = _regulator_disable(rdev);
1737         mutex_unlock(&rdev->mutex);
1738
1739         if (ret == 0 && rdev->supply)
1740                 regulator_disable(rdev->supply);
1741
1742         return ret;
1743 }
1744 EXPORT_SYMBOL_GPL(regulator_disable);
1745
1746 /* locks held by regulator_force_disable() */
1747 static int _regulator_force_disable(struct regulator_dev *rdev)
1748 {
1749         int ret = 0;
1750
1751         ret = _regulator_do_disable(rdev);
1752         if (ret < 0) {
1753                 rdev_err(rdev, "failed to force disable\n");
1754                 return ret;
1755         }
1756
1757         _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1758                         REGULATOR_EVENT_DISABLE, NULL);
1759
1760         return 0;
1761 }
1762
1763 /**
1764  * regulator_force_disable - force disable regulator output
1765  * @regulator: regulator source
1766  *
1767  * Forcibly disable the regulator output voltage or current.
1768  * NOTE: this *will* disable the regulator output even if other consumer
1769  * devices have it enabled. This should be used for situations when device
1770  * damage will likely occur if the regulator is not disabled (e.g. over temp).
1771  */
1772 int regulator_force_disable(struct regulator *regulator)
1773 {
1774         struct regulator_dev *rdev = regulator->rdev;
1775         int ret;
1776
1777         mutex_lock(&rdev->mutex);
1778         regulator->uA_load = 0;
1779         ret = _regulator_force_disable(regulator->rdev);
1780         mutex_unlock(&rdev->mutex);
1781
1782         if (rdev->supply)
1783                 while (rdev->open_count--)
1784                         regulator_disable(rdev->supply);
1785
1786         return ret;
1787 }
1788 EXPORT_SYMBOL_GPL(regulator_force_disable);
1789
1790 static void regulator_disable_work(struct work_struct *work)
1791 {
1792         struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1793                                                   disable_work.work);
1794         int count, i, ret;
1795
1796         mutex_lock(&rdev->mutex);
1797
1798         BUG_ON(!rdev->deferred_disables);
1799
1800         count = rdev->deferred_disables;
1801         rdev->deferred_disables = 0;
1802
1803         for (i = 0; i < count; i++) {
1804                 ret = _regulator_disable(rdev);
1805                 if (ret != 0)
1806                         rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1807         }
1808
1809         mutex_unlock(&rdev->mutex);
1810
1811         if (rdev->supply) {
1812                 for (i = 0; i < count; i++) {
1813                         ret = regulator_disable(rdev->supply);
1814                         if (ret != 0) {
1815                                 rdev_err(rdev,
1816                                          "Supply disable failed: %d\n", ret);
1817                         }
1818                 }
1819         }
1820 }
1821
1822 /**
1823  * regulator_disable_deferred - disable regulator output with delay
1824  * @regulator: regulator source
1825  * @ms: miliseconds until the regulator is disabled
1826  *
1827  * Execute regulator_disable() on the regulator after a delay.  This
1828  * is intended for use with devices that require some time to quiesce.
1829  *
1830  * NOTE: this will only disable the regulator output if no other consumer
1831  * devices have it enabled, the regulator device supports disabling and
1832  * machine constraints permit this operation.
1833  */
1834 int regulator_disable_deferred(struct regulator *regulator, int ms)
1835 {
1836         struct regulator_dev *rdev = regulator->rdev;
1837         int ret;
1838
1839         if (regulator->always_on)
1840                 return 0;
1841
1842         if (!ms)
1843                 return regulator_disable(regulator);
1844
1845         mutex_lock(&rdev->mutex);
1846         rdev->deferred_disables++;
1847         mutex_unlock(&rdev->mutex);
1848
1849         ret = queue_delayed_work(system_power_efficient_wq,
1850                                  &rdev->disable_work,
1851                                  msecs_to_jiffies(ms));
1852         if (ret < 0)
1853                 return ret;
1854         else
1855                 return 0;
1856 }
1857 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1858
1859 /**
1860  * regulator_is_enabled_regmap - standard is_enabled() for regmap users
1861  *
1862  * @rdev: regulator to operate on
1863  *
1864  * Regulators that use regmap for their register I/O can set the
1865  * enable_reg and enable_mask fields in their descriptor and then use
1866  * this as their is_enabled operation, saving some code.
1867  */
1868 int regulator_is_enabled_regmap(struct regulator_dev *rdev)
1869 {
1870         unsigned int val;
1871         int ret;
1872
1873         ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
1874         if (ret != 0)
1875                 return ret;
1876
1877         if (rdev->desc->enable_is_inverted)
1878                 return (val & rdev->desc->enable_mask) == 0;
1879         else
1880                 return (val & rdev->desc->enable_mask) != 0;
1881 }
1882 EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
1883
1884 /**
1885  * regulator_enable_regmap - standard enable() for regmap users
1886  *
1887  * @rdev: regulator to operate on
1888  *
1889  * Regulators that use regmap for their register I/O can set the
1890  * enable_reg and enable_mask fields in their descriptor and then use
1891  * this as their enable() operation, saving some code.
1892  */
1893 int regulator_enable_regmap(struct regulator_dev *rdev)
1894 {
1895         unsigned int val;
1896
1897         if (rdev->desc->enable_is_inverted)
1898                 val = 0;
1899         else
1900                 val = rdev->desc->enable_mask;
1901
1902         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1903                                   rdev->desc->enable_mask, val);
1904 }
1905 EXPORT_SYMBOL_GPL(regulator_enable_regmap);
1906
1907 /**
1908  * regulator_disable_regmap - standard disable() for regmap users
1909  *
1910  * @rdev: regulator to operate on
1911  *
1912  * Regulators that use regmap for their register I/O can set the
1913  * enable_reg and enable_mask fields in their descriptor and then use
1914  * this as their disable() operation, saving some code.
1915  */
1916 int regulator_disable_regmap(struct regulator_dev *rdev)
1917 {
1918         unsigned int val;
1919
1920         if (rdev->desc->enable_is_inverted)
1921                 val = rdev->desc->enable_mask;
1922         else
1923                 val = 0;
1924
1925         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1926                                   rdev->desc->enable_mask, val);
1927 }
1928 EXPORT_SYMBOL_GPL(regulator_disable_regmap);
1929
1930 static int _regulator_is_enabled(struct regulator_dev *rdev)
1931 {
1932         /* A GPIO control always takes precedence */
1933         if (rdev->ena_pin)
1934                 return rdev->ena_gpio_state;
1935
1936         /* If we don't know then assume that the regulator is always on */
1937         if (!rdev->desc->ops->is_enabled)
1938                 return 1;
1939
1940         return rdev->desc->ops->is_enabled(rdev);
1941 }
1942
1943 /**
1944  * regulator_is_enabled - is the regulator output enabled
1945  * @regulator: regulator source
1946  *
1947  * Returns positive if the regulator driver backing the source/client
1948  * has requested that the device be enabled, zero if it hasn't, else a
1949  * negative errno code.
1950  *
1951  * Note that the device backing this regulator handle can have multiple
1952  * users, so it might be enabled even if regulator_enable() was never
1953  * called for this particular source.
1954  */
1955 int regulator_is_enabled(struct regulator *regulator)
1956 {
1957         int ret;
1958
1959         if (regulator->always_on)
1960                 return 1;
1961
1962         mutex_lock(&regulator->rdev->mutex);
1963         ret = _regulator_is_enabled(regulator->rdev);
1964         mutex_unlock(&regulator->rdev->mutex);
1965
1966         return ret;
1967 }
1968 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1969
1970 /**
1971  * regulator_can_change_voltage - check if regulator can change voltage
1972  * @regulator: regulator source
1973  *
1974  * Returns positive if the regulator driver backing the source/client
1975  * can change its voltage, false otherwise. Usefull for detecting fixed
1976  * or dummy regulators and disabling voltage change logic in the client
1977  * driver.
1978  */
1979 int regulator_can_change_voltage(struct regulator *regulator)
1980 {
1981         struct regulator_dev    *rdev = regulator->rdev;
1982
1983         if (rdev->constraints &&
1984             (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
1985                 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
1986                         return 1;
1987
1988                 if (rdev->desc->continuous_voltage_range &&
1989                     rdev->constraints->min_uV && rdev->constraints->max_uV &&
1990                     rdev->constraints->min_uV != rdev->constraints->max_uV)
1991                         return 1;
1992         }
1993
1994         return 0;
1995 }
1996 EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
1997
1998 /**
1999  * regulator_count_voltages - count regulator_list_voltage() selectors
2000  * @regulator: regulator source
2001  *
2002  * Returns number of selectors, or negative errno.  Selectors are
2003  * numbered starting at zero, and typically correspond to bitfields
2004  * in hardware registers.
2005  */
2006 int regulator_count_voltages(struct regulator *regulator)
2007 {
2008         struct regulator_dev    *rdev = regulator->rdev;
2009
2010         return rdev->desc->n_voltages ? : -EINVAL;
2011 }
2012 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2013
2014 /**
2015  * regulator_list_voltage_linear - List voltages with simple calculation
2016  *
2017  * @rdev: Regulator device
2018  * @selector: Selector to convert into a voltage
2019  *
2020  * Regulators with a simple linear mapping between voltages and
2021  * selectors can set min_uV and uV_step in the regulator descriptor
2022  * and then use this function as their list_voltage() operation,
2023  */
2024 int regulator_list_voltage_linear(struct regulator_dev *rdev,
2025                                   unsigned int selector)
2026 {
2027         if (selector >= rdev->desc->n_voltages)
2028                 return -EINVAL;
2029         if (selector < rdev->desc->linear_min_sel)
2030                 return 0;
2031
2032         selector -= rdev->desc->linear_min_sel;
2033
2034         return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
2035 }
2036 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
2037
2038 /**
2039  * regulator_list_voltage_table - List voltages with table based mapping
2040  *
2041  * @rdev: Regulator device
2042  * @selector: Selector to convert into a voltage
2043  *
2044  * Regulators with table based mapping between voltages and
2045  * selectors can set volt_table in the regulator descriptor
2046  * and then use this function as their list_voltage() operation.
2047  */
2048 int regulator_list_voltage_table(struct regulator_dev *rdev,
2049                                  unsigned int selector)
2050 {
2051         if (!rdev->desc->volt_table) {
2052                 BUG_ON(!rdev->desc->volt_table);
2053                 return -EINVAL;
2054         }
2055
2056         if (selector >= rdev->desc->n_voltages)
2057                 return -EINVAL;
2058
2059         return rdev->desc->volt_table[selector];
2060 }
2061 EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
2062
2063 /**
2064  * regulator_list_voltage - enumerate supported voltages
2065  * @regulator: regulator source
2066  * @selector: identify voltage to list
2067  * Context: can sleep
2068  *
2069  * Returns a voltage that can be passed to @regulator_set_voltage(),
2070  * zero if this selector code can't be used on this system, or a
2071  * negative errno.
2072  */
2073 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2074 {
2075         struct regulator_dev    *rdev = regulator->rdev;
2076         struct regulator_ops    *ops = rdev->desc->ops;
2077         int                     ret;
2078
2079         if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
2080                 return -EINVAL;
2081
2082         mutex_lock(&rdev->mutex);
2083         ret = ops->list_voltage(rdev, selector);
2084         mutex_unlock(&rdev->mutex);
2085
2086         if (ret > 0) {
2087                 if (ret < rdev->constraints->min_uV)
2088                         ret = 0;
2089                 else if (ret > rdev->constraints->max_uV)
2090                         ret = 0;
2091         }
2092
2093         return ret;
2094 }
2095 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2096
2097 /**
2098  * regulator_is_supported_voltage - check if a voltage range can be supported
2099  *
2100  * @regulator: Regulator to check.
2101  * @min_uV: Minimum required voltage in uV.
2102  * @max_uV: Maximum required voltage in uV.
2103  *
2104  * Returns a boolean or a negative error code.
2105  */
2106 int regulator_is_supported_voltage(struct regulator *regulator,
2107                                    int min_uV, int max_uV)
2108 {
2109         struct regulator_dev *rdev = regulator->rdev;
2110         int i, voltages, ret;
2111
2112         /* If we can't change voltage check the current voltage */
2113         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2114                 ret = regulator_get_voltage(regulator);
2115                 if (ret >= 0)
2116                         return (min_uV <= ret && ret <= max_uV);
2117                 else
2118                         return ret;
2119         }
2120
2121         /* Any voltage within constrains range is fine? */
2122         if (rdev->desc->continuous_voltage_range)
2123                 return min_uV >= rdev->constraints->min_uV &&
2124                                 max_uV <= rdev->constraints->max_uV;
2125
2126         ret = regulator_count_voltages(regulator);
2127         if (ret < 0)
2128                 return ret;
2129         voltages = ret;
2130
2131         for (i = 0; i < voltages; i++) {
2132                 ret = regulator_list_voltage(regulator, i);
2133
2134                 if (ret >= min_uV && ret <= max_uV)
2135                         return 1;
2136         }
2137
2138         return 0;
2139 }
2140 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2141
2142 /**
2143  * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
2144  *
2145  * @rdev: regulator to operate on
2146  *
2147  * Regulators that use regmap for their register I/O can set the
2148  * vsel_reg and vsel_mask fields in their descriptor and then use this
2149  * as their get_voltage_vsel operation, saving some code.
2150  */
2151 int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
2152 {
2153         unsigned int val;
2154         int ret;
2155
2156         ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
2157         if (ret != 0)
2158                 return ret;
2159
2160         val &= rdev->desc->vsel_mask;
2161         val >>= ffs(rdev->desc->vsel_mask) - 1;
2162
2163         return val;
2164 }
2165 EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
2166
2167 /**
2168  * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
2169  *
2170  * @rdev: regulator to operate on
2171  * @sel: Selector to set
2172  *
2173  * Regulators that use regmap for their register I/O can set the
2174  * vsel_reg and vsel_mask fields in their descriptor and then use this
2175  * as their set_voltage_vsel operation, saving some code.
2176  */
2177 int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
2178 {
2179         int ret;
2180
2181         sel <<= ffs(rdev->desc->vsel_mask) - 1;
2182
2183         ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
2184                                   rdev->desc->vsel_mask, sel);
2185         if (ret)
2186                 return ret;
2187
2188         if (rdev->desc->apply_bit)
2189                 ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
2190                                          rdev->desc->apply_bit,
2191                                          rdev->desc->apply_bit);
2192         return ret;
2193 }
2194 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
2195
2196 /**
2197  * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
2198  *
2199  * @rdev: Regulator to operate on
2200  * @min_uV: Lower bound for voltage
2201  * @max_uV: Upper bound for voltage
2202  *
2203  * Drivers implementing set_voltage_sel() and list_voltage() can use
2204  * this as their map_voltage() operation.  It will find a suitable
2205  * voltage by calling list_voltage() until it gets something in bounds
2206  * for the requested voltages.
2207  */
2208 int regulator_map_voltage_iterate(struct regulator_dev *rdev,
2209                                   int min_uV, int max_uV)
2210 {
2211         int best_val = INT_MAX;
2212         int selector = 0;
2213         int i, ret;
2214
2215         /* Find the smallest voltage that falls within the specified
2216          * range.
2217          */
2218         for (i = 0; i < rdev->desc->n_voltages; i++) {
2219                 ret = rdev->desc->ops->list_voltage(rdev, i);
2220                 if (ret < 0)
2221                         continue;
2222
2223                 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2224                         best_val = ret;
2225                         selector = i;
2226                 }
2227         }
2228
2229         if (best_val != INT_MAX)
2230                 return selector;
2231         else
2232                 return -EINVAL;
2233 }
2234 EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
2235
2236 /**
2237  * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
2238  *
2239  * @rdev: Regulator to operate on
2240  * @min_uV: Lower bound for voltage
2241  * @max_uV: Upper bound for voltage
2242  *
2243  * Drivers that have ascendant voltage list can use this as their
2244  * map_voltage() operation.
2245  */
2246 int regulator_map_voltage_ascend(struct regulator_dev *rdev,
2247                                  int min_uV, int max_uV)
2248 {
2249         int i, ret;
2250
2251         for (i = 0; i < rdev->desc->n_voltages; i++) {
2252                 ret = rdev->desc->ops->list_voltage(rdev, i);
2253                 if (ret < 0)
2254                         continue;
2255
2256                 if (ret > max_uV)
2257                         break;
2258
2259                 if (ret >= min_uV && ret <= max_uV)
2260                         return i;
2261         }
2262
2263         return -EINVAL;
2264 }
2265 EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
2266
2267 /**
2268  * regulator_map_voltage_linear - map_voltage() for simple linear mappings
2269  *
2270  * @rdev: Regulator to operate on
2271  * @min_uV: Lower bound for voltage
2272  * @max_uV: Upper bound for voltage
2273  *
2274  * Drivers providing min_uV and uV_step in their regulator_desc can
2275  * use this as their map_voltage() operation.
2276  */
2277 int regulator_map_voltage_linear(struct regulator_dev *rdev,
2278                                  int min_uV, int max_uV)
2279 {
2280         int ret, voltage;
2281
2282         /* Allow uV_step to be 0 for fixed voltage */
2283         if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
2284                 if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
2285                         return 0;
2286                 else
2287                         return -EINVAL;
2288         }
2289
2290         if (!rdev->desc->uV_step) {
2291                 BUG_ON(!rdev->desc->uV_step);
2292                 return -EINVAL;
2293         }
2294
2295         if (min_uV < rdev->desc->min_uV)
2296                 min_uV = rdev->desc->min_uV;
2297
2298         ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
2299         if (ret < 0)
2300                 return ret;
2301
2302         ret += rdev->desc->linear_min_sel;
2303
2304         /* Map back into a voltage to verify we're still in bounds */
2305         voltage = rdev->desc->ops->list_voltage(rdev, ret);
2306         if (voltage < min_uV || voltage > max_uV)
2307                 return -EINVAL;
2308
2309         return ret;
2310 }
2311 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
2312
2313 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2314                                        int min_uV, int max_uV,
2315                                        unsigned *selector)
2316 {
2317         struct pre_voltage_change_data data;
2318         int ret;
2319
2320         data.old_uV = _regulator_get_voltage(rdev);
2321         data.min_uV = min_uV;
2322         data.max_uV = max_uV;
2323         ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2324                                    &data);
2325         if (ret & NOTIFY_STOP_MASK)
2326                 return -EINVAL;
2327
2328         ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2329         if (ret >= 0)
2330                 return ret;
2331
2332         _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2333                              (void *)data.old_uV);
2334
2335         return ret;
2336 }
2337
2338 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2339                                            int uV, unsigned selector)
2340 {
2341         struct pre_voltage_change_data data;
2342         int ret;
2343
2344         data.old_uV = _regulator_get_voltage(rdev);
2345         data.min_uV = uV;
2346         data.max_uV = uV;
2347         ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2348                                    &data);
2349         if (ret & NOTIFY_STOP_MASK)
2350                 return -EINVAL;
2351
2352         ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2353         if (ret >= 0)
2354                 return ret;
2355
2356         _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2357                              (void *)data.old_uV);
2358
2359         return ret;
2360 }
2361
2362 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2363                                      int min_uV, int max_uV)
2364 {
2365         int ret;
2366         int delay = 0;
2367         int best_val = 0;
2368         unsigned int selector;
2369         int old_selector = -1;
2370
2371         trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2372
2373         min_uV += rdev->constraints->uV_offset;
2374         max_uV += rdev->constraints->uV_offset;
2375
2376         /*
2377          * If we can't obtain the old selector there is not enough
2378          * info to call set_voltage_time_sel().
2379          */
2380         if (_regulator_is_enabled(rdev) &&
2381             rdev->desc->ops->set_voltage_time_sel &&
2382             rdev->desc->ops->get_voltage_sel) {
2383                 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2384                 if (old_selector < 0)
2385                         return old_selector;
2386         }
2387
2388         if (rdev->desc->ops->set_voltage) {
2389                 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2390                                                   &selector);
2391
2392                 if (ret >= 0) {
2393                         if (rdev->desc->ops->list_voltage)
2394                                 best_val = rdev->desc->ops->list_voltage(rdev,
2395                                                                          selector);
2396                         else
2397                                 best_val = _regulator_get_voltage(rdev);
2398                 }
2399
2400         } else if (rdev->desc->ops->set_voltage_sel) {
2401                 if (rdev->desc->ops->map_voltage) {
2402                         ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2403                                                            max_uV);
2404                 } else {
2405                         if (rdev->desc->ops->list_voltage ==
2406                             regulator_list_voltage_linear)
2407                                 ret = regulator_map_voltage_linear(rdev,
2408                                                                 min_uV, max_uV);
2409                         else
2410                                 ret = regulator_map_voltage_iterate(rdev,
2411                                                                 min_uV, max_uV);
2412                 }
2413
2414                 if (ret >= 0) {
2415                         best_val = rdev->desc->ops->list_voltage(rdev, ret);
2416                         if (min_uV <= best_val && max_uV >= best_val) {
2417                                 selector = ret;
2418                                 if (old_selector == selector)
2419                                         ret = 0;
2420                                 else
2421                                         ret = _regulator_call_set_voltage_sel(
2422                                                 rdev, best_val, selector);
2423                         } else {
2424                                 ret = -EINVAL;
2425                         }
2426                 }
2427         } else {
2428                 ret = -EINVAL;
2429         }
2430
2431         /* Call set_voltage_time_sel if successfully obtained old_selector */
2432         if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 &&
2433             old_selector != selector && rdev->desc->ops->set_voltage_time_sel) {
2434
2435                 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2436                                                 old_selector, selector);
2437                 if (delay < 0) {
2438                         rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2439                                   delay);
2440                         delay = 0;
2441                 }
2442
2443                 /* Insert any necessary delays */
2444                 if (delay >= 1000) {
2445                         mdelay(delay / 1000);
2446                         udelay(delay % 1000);
2447                 } else if (delay) {
2448                         udelay(delay);
2449                 }
2450         }
2451
2452         if (ret == 0 && best_val >= 0) {
2453                 unsigned long data = best_val;
2454
2455                 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2456                                      (void *)data);
2457         }
2458
2459         trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2460
2461         return ret;
2462 }
2463
2464 /**
2465  * regulator_set_voltage - set regulator output voltage
2466  * @regulator: regulator source
2467  * @min_uV: Minimum required voltage in uV
2468  * @max_uV: Maximum acceptable voltage in uV
2469  *
2470  * Sets a voltage regulator to the desired output voltage. This can be set
2471  * during any regulator state. IOW, regulator can be disabled or enabled.
2472  *
2473  * If the regulator is enabled then the voltage will change to the new value
2474  * immediately otherwise if the regulator is disabled the regulator will
2475  * output at the new voltage when enabled.
2476  *
2477  * NOTE: If the regulator is shared between several devices then the lowest
2478  * request voltage that meets the system constraints will be used.
2479  * Regulator system constraints must be set for this regulator before
2480  * calling this function otherwise this call will fail.
2481  */
2482 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2483 {
2484         struct regulator_dev *rdev = regulator->rdev;
2485         int ret = 0;
2486         int old_min_uV, old_max_uV;
2487
2488         mutex_lock(&rdev->mutex);
2489
2490         /* If we're setting the same range as last time the change
2491          * should be a noop (some cpufreq implementations use the same
2492          * voltage for multiple frequencies, for example).
2493          */
2494         if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2495                 goto out;
2496
2497         /* sanity check */
2498         if (!rdev->desc->ops->set_voltage &&
2499             !rdev->desc->ops->set_voltage_sel) {
2500                 ret = -EINVAL;
2501                 goto out;
2502         }
2503
2504         /* constraints check */
2505         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2506         if (ret < 0)
2507                 goto out;
2508         
2509         /* restore original values in case of error */
2510         old_min_uV = regulator->min_uV;
2511         old_max_uV = regulator->max_uV;
2512         regulator->min_uV = min_uV;
2513         regulator->max_uV = max_uV;
2514
2515         ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2516         if (ret < 0)
2517                 goto out2;
2518
2519         ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2520         if (ret < 0)
2521                 goto out2;
2522         
2523 out:
2524         mutex_unlock(&rdev->mutex);
2525         return ret;
2526 out2:
2527         regulator->min_uV = old_min_uV;
2528         regulator->max_uV = old_max_uV;
2529         mutex_unlock(&rdev->mutex);
2530         return ret;
2531 }
2532 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2533
2534 /**
2535  * regulator_set_voltage_time - get raise/fall time
2536  * @regulator: regulator source
2537  * @old_uV: starting voltage in microvolts
2538  * @new_uV: target voltage in microvolts
2539  *
2540  * Provided with the starting and ending voltage, this function attempts to
2541  * calculate the time in microseconds required to rise or fall to this new
2542  * voltage.
2543  */
2544 int regulator_set_voltage_time(struct regulator *regulator,
2545                                int old_uV, int new_uV)
2546 {
2547         struct regulator_dev    *rdev = regulator->rdev;
2548         struct regulator_ops    *ops = rdev->desc->ops;
2549         int old_sel = -1;
2550         int new_sel = -1;
2551         int voltage;
2552         int i;
2553
2554         /* Currently requires operations to do this */
2555         if (!ops->list_voltage || !ops->set_voltage_time_sel
2556             || !rdev->desc->n_voltages)
2557                 return -EINVAL;
2558
2559         for (i = 0; i < rdev->desc->n_voltages; i++) {
2560                 /* We only look for exact voltage matches here */
2561                 voltage = regulator_list_voltage(regulator, i);
2562                 if (voltage < 0)
2563                         return -EINVAL;
2564                 if (voltage == 0)
2565                         continue;
2566                 if (voltage == old_uV)
2567                         old_sel = i;
2568                 if (voltage == new_uV)
2569                         new_sel = i;
2570         }
2571
2572         if (old_sel < 0 || new_sel < 0)
2573                 return -EINVAL;
2574
2575         return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2576 }
2577 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2578
2579 /**
2580  * regulator_set_voltage_time_sel - get raise/fall time
2581  * @rdev: regulator source device
2582  * @old_selector: selector for starting voltage
2583  * @new_selector: selector for target voltage
2584  *
2585  * Provided with the starting and target voltage selectors, this function
2586  * returns time in microseconds required to rise or fall to this new voltage
2587  *
2588  * Drivers providing ramp_delay in regulation_constraints can use this as their
2589  * set_voltage_time_sel() operation.
2590  */
2591 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2592                                    unsigned int old_selector,
2593                                    unsigned int new_selector)
2594 {
2595         unsigned int ramp_delay = 0;
2596         int old_volt, new_volt;
2597
2598         if (rdev->constraints->ramp_delay)
2599                 ramp_delay = rdev->constraints->ramp_delay;
2600         else if (rdev->desc->ramp_delay)
2601                 ramp_delay = rdev->desc->ramp_delay;
2602
2603         if (ramp_delay == 0) {
2604                 rdev_warn(rdev, "ramp_delay not set\n");
2605                 return 0;
2606         }
2607
2608         /* sanity check */
2609         if (!rdev->desc->ops->list_voltage)
2610                 return -EINVAL;
2611
2612         old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2613         new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2614
2615         return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
2616 }
2617 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
2618
2619 /**
2620  * regulator_sync_voltage - re-apply last regulator output voltage
2621  * @regulator: regulator source
2622  *
2623  * Re-apply the last configured voltage.  This is intended to be used
2624  * where some external control source the consumer is cooperating with
2625  * has caused the configured voltage to change.
2626  */
2627 int regulator_sync_voltage(struct regulator *regulator)
2628 {
2629         struct regulator_dev *rdev = regulator->rdev;
2630         int ret, min_uV, max_uV;
2631
2632         mutex_lock(&rdev->mutex);
2633
2634         if (!rdev->desc->ops->set_voltage &&
2635             !rdev->desc->ops->set_voltage_sel) {
2636                 ret = -EINVAL;
2637                 goto out;
2638         }
2639
2640         /* This is only going to work if we've had a voltage configured. */
2641         if (!regulator->min_uV && !regulator->max_uV) {
2642                 ret = -EINVAL;
2643                 goto out;
2644         }
2645
2646         min_uV = regulator->min_uV;
2647         max_uV = regulator->max_uV;
2648
2649         /* This should be a paranoia check... */
2650         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2651         if (ret < 0)
2652                 goto out;
2653
2654         ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2655         if (ret < 0)
2656                 goto out;
2657
2658         ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2659
2660 out:
2661         mutex_unlock(&rdev->mutex);
2662         return ret;
2663 }
2664 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2665
2666 static int _regulator_get_voltage(struct regulator_dev *rdev)
2667 {
2668         int sel, ret;
2669
2670         if (rdev->desc->ops->get_voltage_sel) {
2671                 sel = rdev->desc->ops->get_voltage_sel(rdev);
2672                 if (sel < 0)
2673                         return sel;
2674                 ret = rdev->desc->ops->list_voltage(rdev, sel);
2675         } else if (rdev->desc->ops->get_voltage) {
2676                 ret = rdev->desc->ops->get_voltage(rdev);
2677         } else if (rdev->desc->ops->list_voltage) {
2678                 ret = rdev->desc->ops->list_voltage(rdev, 0);
2679         } else {
2680                 return -EINVAL;
2681         }
2682
2683         if (ret < 0)
2684                 return ret;
2685         return ret - rdev->constraints->uV_offset;
2686 }
2687
2688 /**
2689  * regulator_get_voltage - get regulator output voltage
2690  * @regulator: regulator source
2691  *
2692  * This returns the current regulator voltage in uV.
2693  *
2694  * NOTE: If the regulator is disabled it will return the voltage value. This
2695  * function should not be used to determine regulator state.
2696  */
2697 int regulator_get_voltage(struct regulator *regulator)
2698 {
2699         int ret;
2700
2701         mutex_lock(&regulator->rdev->mutex);
2702
2703         ret = _regulator_get_voltage(regulator->rdev);
2704
2705         mutex_unlock(&regulator->rdev->mutex);
2706
2707         return ret;
2708 }
2709 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2710
2711 /**
2712  * regulator_set_current_limit - set regulator output current limit
2713  * @regulator: regulator source
2714  * @min_uA: Minimum supported current in uA
2715  * @max_uA: Maximum supported current in uA
2716  *
2717  * Sets current sink to the desired output current. This can be set during
2718  * any regulator state. IOW, regulator can be disabled or enabled.
2719  *
2720  * If the regulator is enabled then the current will change to the new value
2721  * immediately otherwise if the regulator is disabled the regulator will
2722  * output at the new current when enabled.
2723  *
2724  * NOTE: Regulator system constraints must be set for this regulator before
2725  * calling this function otherwise this call will fail.
2726  */
2727 int regulator_set_current_limit(struct regulator *regulator,
2728                                int min_uA, int max_uA)
2729 {
2730         struct regulator_dev *rdev = regulator->rdev;
2731         int ret;
2732
2733         mutex_lock(&rdev->mutex);
2734
2735         /* sanity check */
2736         if (!rdev->desc->ops->set_current_limit) {
2737                 ret = -EINVAL;
2738                 goto out;
2739         }
2740
2741         /* constraints check */
2742         ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2743         if (ret < 0)
2744                 goto out;
2745
2746         ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2747 out:
2748         mutex_unlock(&rdev->mutex);
2749         return ret;
2750 }
2751 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2752
2753 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2754 {
2755         int ret;
2756
2757         mutex_lock(&rdev->mutex);
2758
2759         /* sanity check */
2760         if (!rdev->desc->ops->get_current_limit) {
2761                 ret = -EINVAL;
2762                 goto out;
2763         }
2764
2765         ret = rdev->desc->ops->get_current_limit(rdev);
2766 out:
2767         mutex_unlock(&rdev->mutex);
2768         return ret;
2769 }
2770
2771 /**
2772  * regulator_get_current_limit - get regulator output current
2773  * @regulator: regulator source
2774  *
2775  * This returns the current supplied by the specified current sink in uA.
2776  *
2777  * NOTE: If the regulator is disabled it will return the current value. This
2778  * function should not be used to determine regulator state.
2779  */
2780 int regulator_get_current_limit(struct regulator *regulator)
2781 {
2782         return _regulator_get_current_limit(regulator->rdev);
2783 }
2784 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2785
2786 int regulator_is_supported_mode(struct regulator *regulator, int *mode)
2787 {
2788         struct regulator_dev *rdev = regulator->rdev;
2789         int ret;
2790
2791         mutex_lock(&rdev->mutex);
2792
2793         ret = regulator_mode_constrain(rdev, mode);
2794
2795         mutex_unlock(&rdev->mutex);
2796
2797         return ret;
2798 }
2799
2800 /**
2801  * regulator_set_mode - set regulator operating mode
2802  * @regulator: regulator source
2803  * @mode: operating mode - one of the REGULATOR_MODE constants
2804  *
2805  * Set regulator operating mode to increase regulator efficiency or improve
2806  * regulation performance.
2807  *
2808  * NOTE: Regulator system constraints must be set for this regulator before
2809  * calling this function otherwise this call will fail.
2810  */
2811 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2812 {
2813         struct regulator_dev *rdev = regulator->rdev;
2814         int ret;
2815         int regulator_curr_mode;
2816
2817         mutex_lock(&rdev->mutex);
2818
2819         /* sanity check */
2820         if (!rdev->desc->ops->set_mode) {
2821                 ret = -EINVAL;
2822                 goto out;
2823         }
2824
2825         /* return if the same mode is requested */
2826         if (rdev->desc->ops->get_mode) {
2827                 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2828                 if (regulator_curr_mode == mode) {
2829                         ret = 0;
2830                         goto out;
2831                 }
2832         }
2833
2834         /* constraints check */
2835         ret = regulator_mode_constrain(rdev, &mode);
2836         if (ret < 0)
2837                 goto out;
2838
2839         ret = rdev->desc->ops->set_mode(rdev, mode);
2840 out:
2841         mutex_unlock(&rdev->mutex);
2842         return ret;
2843 }
2844 EXPORT_SYMBOL_GPL(regulator_set_mode);
2845
2846 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2847 {
2848         int ret;
2849
2850         mutex_lock(&rdev->mutex);
2851
2852         /* sanity check */
2853         if (!rdev->desc->ops->get_mode) {
2854                 ret = -EINVAL;
2855                 goto out;
2856         }
2857
2858         ret = rdev->desc->ops->get_mode(rdev);
2859 out:
2860         mutex_unlock(&rdev->mutex);
2861         return ret;
2862 }
2863
2864 /**
2865  * regulator_get_mode - get regulator operating mode
2866  * @regulator: regulator source
2867  *
2868  * Get the current regulator operating mode.
2869  */
2870 unsigned int regulator_get_mode(struct regulator *regulator)
2871 {
2872         return _regulator_get_mode(regulator->rdev);
2873 }
2874 EXPORT_SYMBOL_GPL(regulator_get_mode);
2875
2876 /**
2877  * regulator_set_optimum_mode - set regulator optimum operating mode
2878  * @regulator: regulator source
2879  * @uA_load: load current
2880  *
2881  * Notifies the regulator core of a new device load. This is then used by
2882  * DRMS (if enabled by constraints) to set the most efficient regulator
2883  * operating mode for the new regulator loading.
2884  *
2885  * Consumer devices notify their supply regulator of the maximum power
2886  * they will require (can be taken from device datasheet in the power
2887  * consumption tables) when they change operational status and hence power
2888  * state. Examples of operational state changes that can affect power
2889  * consumption are :-
2890  *
2891  *    o Device is opened / closed.
2892  *    o Device I/O is about to begin or has just finished.
2893  *    o Device is idling in between work.
2894  *
2895  * This information is also exported via sysfs to userspace.
2896  *
2897  * DRMS will sum the total requested load on the regulator and change
2898  * to the most efficient operating mode if platform constraints allow.
2899  *
2900  * Returns the new regulator mode or error.
2901  */
2902 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2903 {
2904         struct regulator_dev *rdev = regulator->rdev;
2905         struct regulator *consumer;
2906         int ret, output_uV, input_uV = 0, total_uA_load = 0;
2907         unsigned int mode;
2908
2909         if (rdev->supply)
2910                 input_uV = regulator_get_voltage(rdev->supply);
2911
2912         mutex_lock(&rdev->mutex);
2913
2914         /*
2915          * first check to see if we can set modes at all, otherwise just
2916          * tell the consumer everything is OK.
2917          */
2918         regulator->uA_load = uA_load;
2919         ret = regulator_check_drms(rdev);
2920         if (ret < 0) {
2921                 ret = 0;
2922                 goto out;
2923         }
2924
2925         if (!rdev->desc->ops->get_optimum_mode)
2926                 goto out;
2927
2928         /*
2929          * we can actually do this so any errors are indicators of
2930          * potential real failure.
2931          */
2932         ret = -EINVAL;
2933
2934         if (!rdev->desc->ops->set_mode)
2935                 goto out;
2936
2937         /* get output voltage */
2938         output_uV = _regulator_get_voltage(rdev);
2939         if (output_uV <= 0) {
2940                 rdev_err(rdev, "invalid output voltage found\n");
2941                 goto out;
2942         }
2943
2944         /* No supply? Use constraint voltage */
2945         if (input_uV <= 0)
2946                 input_uV = rdev->constraints->input_uV;
2947         if (input_uV <= 0) {
2948                 rdev_err(rdev, "invalid input voltage found\n");
2949                 goto out;
2950         }
2951
2952         /* calc total requested load for this regulator */
2953         list_for_each_entry(consumer, &rdev->consumer_list, list)
2954                 total_uA_load += consumer->uA_load;
2955
2956         mode = rdev->desc->ops->get_optimum_mode(rdev,
2957                                                  input_uV, output_uV,
2958                                                  total_uA_load);
2959         ret = regulator_mode_constrain(rdev, &mode);
2960         if (ret < 0) {
2961                 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2962                          total_uA_load, input_uV, output_uV);
2963                 goto out;
2964         }
2965
2966         ret = rdev->desc->ops->set_mode(rdev, mode);
2967         if (ret < 0) {
2968                 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2969                 goto out;
2970         }
2971         ret = mode;
2972 out:
2973         mutex_unlock(&rdev->mutex);
2974         return ret;
2975 }
2976 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2977
2978 /**
2979  * regulator_set_bypass_regmap - Default set_bypass() using regmap
2980  *
2981  * @rdev: device to operate on.
2982  * @enable: state to set.
2983  */
2984 int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
2985 {
2986         unsigned int val;
2987
2988         if (enable)
2989                 val = rdev->desc->bypass_mask;
2990         else
2991                 val = 0;
2992
2993         return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
2994                                   rdev->desc->bypass_mask, val);
2995 }
2996 EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
2997
2998 /**
2999  * regulator_get_bypass_regmap - Default get_bypass() using regmap
3000  *
3001  * @rdev: device to operate on.
3002  * @enable: current state.
3003  */
3004 int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
3005 {
3006         unsigned int val;
3007         int ret;
3008
3009         ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
3010         if (ret != 0)
3011                 return ret;
3012
3013         *enable = val & rdev->desc->bypass_mask;
3014
3015         return 0;
3016 }
3017 EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
3018
3019 /**
3020  * regulator_allow_bypass - allow the regulator to go into bypass mode
3021  *
3022  * @regulator: Regulator to configure
3023  * @enable: enable or disable bypass mode
3024  *
3025  * Allow the regulator to go into bypass mode if all other consumers
3026  * for the regulator also enable bypass mode and the machine
3027  * constraints allow this.  Bypass mode means that the regulator is
3028  * simply passing the input directly to the output with no regulation.
3029  */
3030 int regulator_allow_bypass(struct regulator *regulator, bool enable)
3031 {
3032         struct regulator_dev *rdev = regulator->rdev;
3033         int ret = 0;
3034
3035         if (!rdev->desc->ops->set_bypass)
3036                 return 0;
3037
3038         if (rdev->constraints &&
3039             !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3040                 return 0;
3041
3042         mutex_lock(&rdev->mutex);
3043
3044         if (enable && !regulator->bypass) {
3045                 rdev->bypass_count++;
3046
3047                 if (rdev->bypass_count == rdev->open_count) {
3048                         ret = rdev->desc->ops->set_bypass(rdev, enable);
3049                         if (ret != 0)
3050                                 rdev->bypass_count--;
3051                 }
3052
3053         } else if (!enable && regulator->bypass) {
3054                 rdev->bypass_count--;
3055
3056                 if (rdev->bypass_count != rdev->open_count) {
3057                         ret = rdev->desc->ops->set_bypass(rdev, enable);
3058                         if (ret != 0)
3059                                 rdev->bypass_count++;
3060                 }
3061         }
3062
3063         if (ret == 0)
3064                 regulator->bypass = enable;
3065
3066         mutex_unlock(&rdev->mutex);
3067
3068         return ret;
3069 }
3070 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3071
3072 /**
3073  * regulator_register_notifier - register regulator event notifier
3074  * @regulator: regulator source
3075  * @nb: notifier block
3076  *
3077  * Register notifier block to receive regulator events.
3078  */
3079 int regulator_register_notifier(struct regulator *regulator,
3080                               struct notifier_block *nb)
3081 {
3082         return blocking_notifier_chain_register(&regulator->rdev->notifier,
3083                                                 nb);
3084 }
3085 EXPORT_SYMBOL_GPL(regulator_register_notifier);
3086
3087 /**
3088  * regulator_unregister_notifier - unregister regulator event notifier
3089  * @regulator: regulator source
3090  * @nb: notifier block
3091  *
3092  * Unregister regulator event notifier block.
3093  */
3094 int regulator_unregister_notifier(struct regulator *regulator,
3095                                 struct notifier_block *nb)
3096 {
3097         return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3098                                                   nb);
3099 }
3100 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3101
3102 /* notify regulator consumers and downstream regulator consumers.
3103  * Note mutex must be held by caller.
3104  */
3105 static int _notifier_call_chain(struct regulator_dev *rdev,
3106                                   unsigned long event, void *data)
3107 {
3108         /* call rdev chain first */
3109         return blocking_notifier_call_chain(&rdev->notifier, event, data);
3110 }
3111
3112 /**
3113  * regulator_bulk_get - get multiple regulator consumers
3114  *
3115  * @dev:           Device to supply
3116  * @num_consumers: Number of consumers to register
3117  * @consumers:     Configuration of consumers; clients are stored here.
3118  *
3119  * @return 0 on success, an errno on failure.
3120  *
3121  * This helper function allows drivers to get several regulator
3122  * consumers in one operation.  If any of the regulators cannot be
3123  * acquired then any regulators that were allocated will be freed
3124  * before returning to the caller.
3125  */
3126 int regulator_bulk_get(struct device *dev, int num_consumers,
3127                        struct regulator_bulk_data *consumers)
3128 {
3129         int i;
3130         int ret;
3131
3132         for (i = 0; i < num_consumers; i++)
3133                 consumers[i].consumer = NULL;
3134
3135         for (i = 0; i < num_consumers; i++) {
3136                 consumers[i].consumer = regulator_get(dev,
3137                                                       consumers[i].supply);
3138                 if (IS_ERR(consumers[i].consumer)) {
3139                         ret = PTR_ERR(consumers[i].consumer);
3140                         dev_err(dev, "Failed to get supply '%s': %d\n",
3141                                 consumers[i].supply, ret);
3142                         consumers[i].consumer = NULL;
3143                         goto err;
3144                 }
3145         }
3146
3147         return 0;
3148
3149 err:
3150         while (--i >= 0)
3151                 regulator_put(consumers[i].consumer);
3152
3153         return ret;
3154 }
3155 EXPORT_SYMBOL_GPL(regulator_bulk_get);
3156
3157 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3158 {
3159         struct regulator_bulk_data *bulk = data;
3160
3161         bulk->ret = regulator_enable(bulk->consumer);
3162 }
3163
3164 /**
3165  * regulator_bulk_enable - enable multiple regulator consumers
3166  *
3167  * @num_consumers: Number of consumers
3168  * @consumers:     Consumer data; clients are stored here.
3169  * @return         0 on success, an errno on failure
3170  *
3171  * This convenience API allows consumers to enable multiple regulator
3172  * clients in a single API call.  If any consumers cannot be enabled
3173  * then any others that were enabled will be disabled again prior to
3174  * return.
3175  */
3176 int regulator_bulk_enable(int num_consumers,
3177                           struct regulator_bulk_data *consumers)
3178 {
3179         ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3180         int i;
3181         int ret = 0;
3182
3183         for (i = 0; i < num_consumers; i++) {
3184                 if (consumers[i].consumer->always_on)
3185                         consumers[i].ret = 0;
3186                 else
3187                         async_schedule_domain(regulator_bulk_enable_async,
3188                                               &consumers[i], &async_domain);
3189         }
3190
3191         async_synchronize_full_domain(&async_domain);
3192
3193         /* If any consumer failed we need to unwind any that succeeded */
3194         for (i = 0; i < num_consumers; i++) {
3195                 if (consumers[i].ret != 0) {
3196                         ret = consumers[i].ret;
3197                         goto err;
3198                 }
3199         }
3200
3201         return 0;
3202
3203 err:
3204         for (i = 0; i < num_consumers; i++) {
3205                 if (consumers[i].ret < 0)
3206                         pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3207                                consumers[i].ret);
3208                 else
3209                         regulator_disable(consumers[i].consumer);
3210         }
3211
3212         return ret;
3213 }
3214 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3215
3216 /**
3217  * regulator_bulk_disable - disable multiple regulator consumers
3218  *
3219  * @num_consumers: Number of consumers
3220  * @consumers:     Consumer data; clients are stored here.
3221  * @return         0 on success, an errno on failure
3222  *
3223  * This convenience API allows consumers to disable multiple regulator
3224  * clients in a single API call.  If any consumers cannot be disabled
3225  * then any others that were disabled will be enabled again prior to
3226  * return.
3227  */
3228 int regulator_bulk_disable(int num_consumers,
3229                            struct regulator_bulk_data *consumers)
3230 {
3231         int i;
3232         int ret, r;
3233
3234         for (i = num_consumers - 1; i >= 0; --i) {
3235                 ret = regulator_disable(consumers[i].consumer);
3236                 if (ret != 0)
3237                         goto err;
3238         }
3239
3240         return 0;
3241
3242 err:
3243         pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3244         for (++i; i < num_consumers; ++i) {
3245                 r = regulator_enable(consumers[i].consumer);
3246                 if (r != 0)
3247                         pr_err("Failed to reename %s: %d\n",
3248                                consumers[i].supply, r);
3249         }
3250
3251         return ret;
3252 }
3253 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3254
3255 /**
3256  * regulator_bulk_force_disable - force disable multiple regulator consumers
3257  *
3258  * @num_consumers: Number of consumers
3259  * @consumers:     Consumer data; clients are stored here.
3260  * @return         0 on success, an errno on failure
3261  *
3262  * This convenience API allows consumers to forcibly disable multiple regulator
3263  * clients in a single API call.
3264  * NOTE: This should be used for situations when device damage will
3265  * likely occur if the regulators are not disabled (e.g. over temp).
3266  * Although regulator_force_disable function call for some consumers can
3267  * return error numbers, the function is called for all consumers.
3268  */
3269 int regulator_bulk_force_disable(int num_consumers,
3270                            struct regulator_bulk_data *consumers)
3271 {
3272         int i;
3273         int ret;
3274
3275         for (i = 0; i < num_consumers; i++)
3276                 consumers[i].ret =
3277                             regulator_force_disable(consumers[i].consumer);
3278
3279         for (i = 0; i < num_consumers; i++) {
3280                 if (consumers[i].ret != 0) {
3281                         ret = consumers[i].ret;
3282                         goto out;
3283                 }
3284         }
3285
3286         return 0;
3287 out:
3288         return ret;
3289 }
3290 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3291
3292 /**
3293  * regulator_bulk_free - free multiple regulator consumers
3294  *
3295  * @num_consumers: Number of consumers
3296  * @consumers:     Consumer data; clients are stored here.
3297  *
3298  * This convenience API allows consumers to free multiple regulator
3299  * clients in a single API call.
3300  */
3301 void regulator_bulk_free(int num_consumers,
3302                          struct regulator_bulk_data *consumers)
3303 {
3304         int i;
3305
3306         for (i = 0; i < num_consumers; i++) {
3307                 regulator_put(consumers[i].consumer);
3308                 consumers[i].consumer = NULL;
3309         }
3310 }
3311 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3312
3313 /**
3314  * regulator_notifier_call_chain - call regulator event notifier
3315  * @rdev: regulator source
3316  * @event: notifier block
3317  * @data: callback-specific data.
3318  *
3319  * Called by regulator drivers to notify clients a regulator event has
3320  * occurred. We also notify regulator clients downstream.
3321  * Note lock must be held by caller.
3322  */
3323 int regulator_notifier_call_chain(struct regulator_dev *rdev,
3324                                   unsigned long event, void *data)
3325 {
3326         _notifier_call_chain(rdev, event, data);
3327         return NOTIFY_DONE;
3328
3329 }
3330 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3331
3332 /**
3333  * regulator_mode_to_status - convert a regulator mode into a status
3334  *
3335  * @mode: Mode to convert
3336  *
3337  * Convert a regulator mode into a status.
3338  */
3339 int regulator_mode_to_status(unsigned int mode)
3340 {
3341         switch (mode) {
3342         case REGULATOR_MODE_FAST:
3343                 return REGULATOR_STATUS_FAST;
3344         case REGULATOR_MODE_NORMAL:
3345                 return REGULATOR_STATUS_NORMAL;
3346         case REGULATOR_MODE_IDLE:
3347                 return REGULATOR_STATUS_IDLE;
3348         case REGULATOR_MODE_STANDBY:
3349                 return REGULATOR_STATUS_STANDBY;
3350         default:
3351                 return REGULATOR_STATUS_UNDEFINED;
3352         }
3353 }
3354 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3355
3356 /*
3357  * To avoid cluttering sysfs (and memory) with useless state, only
3358  * create attributes that can be meaningfully displayed.
3359  */
3360 static int add_regulator_attributes(struct regulator_dev *rdev)
3361 {
3362         struct device           *dev = &rdev->dev;
3363         struct regulator_ops    *ops = rdev->desc->ops;
3364         int                     status = 0;
3365
3366         /* some attributes need specific methods to be displayed */
3367         if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3368             (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3369             (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
3370                 status = device_create_file(dev, &dev_attr_microvolts);
3371                 if (status < 0)
3372                         return status;
3373         }
3374         if (ops->get_current_limit) {
3375                 status = device_create_file(dev, &dev_attr_microamps);
3376                 if (status < 0)
3377                         return status;
3378         }
3379         if (ops->get_mode) {
3380                 status = device_create_file(dev, &dev_attr_opmode);
3381                 if (status < 0)
3382                         return status;
3383         }
3384         if (rdev->ena_pin || ops->is_enabled) {
3385                 status = device_create_file(dev, &dev_attr_state);
3386                 if (status < 0)
3387                         return status;
3388         }
3389         if (ops->get_status) {
3390                 status = device_create_file(dev, &dev_attr_status);
3391                 if (status < 0)
3392                         return status;
3393         }
3394         if (ops->get_bypass) {
3395                 status = device_create_file(dev, &dev_attr_bypass);
3396                 if (status < 0)
3397                         return status;
3398         }
3399
3400         /* some attributes are type-specific */
3401         if (rdev->desc->type == REGULATOR_CURRENT) {
3402                 status = device_create_file(dev, &dev_attr_requested_microamps);
3403                 if (status < 0)
3404                         return status;
3405         }
3406
3407         /* all the other attributes exist to support constraints;
3408          * don't show them if there are no constraints, or if the
3409          * relevant supporting methods are missing.
3410          */
3411         if (!rdev->constraints)
3412                 return status;
3413
3414         /* constraints need specific supporting methods */
3415         if (ops->set_voltage || ops->set_voltage_sel) {
3416                 status = device_create_file(dev, &dev_attr_min_microvolts);
3417                 if (status < 0)
3418                         return status;
3419                 status = device_create_file(dev, &dev_attr_max_microvolts);
3420                 if (status < 0)
3421                         return status;
3422         }
3423         if (ops->set_current_limit) {
3424                 status = device_create_file(dev, &dev_attr_min_microamps);
3425                 if (status < 0)
3426                         return status;
3427                 status = device_create_file(dev, &dev_attr_max_microamps);
3428                 if (status < 0)
3429                         return status;
3430         }
3431
3432         status = device_create_file(dev, &dev_attr_suspend_standby_state);
3433         if (status < 0)
3434                 return status;
3435         status = device_create_file(dev, &dev_attr_suspend_mem_state);
3436         if (status < 0)
3437                 return status;
3438         status = device_create_file(dev, &dev_attr_suspend_disk_state);
3439         if (status < 0)
3440                 return status;
3441
3442         if (ops->set_suspend_voltage) {
3443                 status = device_create_file(dev,
3444                                 &dev_attr_suspend_standby_microvolts);
3445                 if (status < 0)
3446                         return status;
3447                 status = device_create_file(dev,
3448                                 &dev_attr_suspend_mem_microvolts);
3449                 if (status < 0)
3450                         return status;
3451                 status = device_create_file(dev,
3452                                 &dev_attr_suspend_disk_microvolts);
3453                 if (status < 0)
3454                         return status;
3455         }
3456
3457         if (ops->set_suspend_mode) {
3458                 status = device_create_file(dev,
3459                                 &dev_attr_suspend_standby_mode);
3460                 if (status < 0)
3461                         return status;
3462                 status = device_create_file(dev,
3463                                 &dev_attr_suspend_mem_mode);
3464                 if (status < 0)
3465                         return status;
3466                 status = device_create_file(dev,
3467                                 &dev_attr_suspend_disk_mode);
3468                 if (status < 0)
3469                         return status;
3470         }
3471
3472         return status;
3473 }
3474
3475 static void rdev_init_debugfs(struct regulator_dev *rdev)
3476 {
3477         rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
3478         if (!rdev->debugfs) {
3479                 rdev_warn(rdev, "Failed to create debugfs directory\n");
3480                 return;
3481         }
3482
3483         debugfs_create_u32("use_count", 0444, rdev->debugfs,
3484                            &rdev->use_count);
3485         debugfs_create_u32("open_count", 0444, rdev->debugfs,
3486                            &rdev->open_count);
3487         debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3488                            &rdev->bypass_count);
3489 }
3490
3491 /**
3492  * regulator_register - register regulator
3493  * @regulator_desc: regulator to register
3494  * @config: runtime configuration for regulator
3495  *
3496  * Called by regulator drivers to register a regulator.
3497  * Returns a valid pointer to struct regulator_dev on success
3498  * or an ERR_PTR() on error.
3499  */
3500 struct regulator_dev *
3501 regulator_register(const struct regulator_desc *regulator_desc,
3502                    const struct regulator_config *config)
3503 {
3504         const struct regulation_constraints *constraints = NULL;
3505         const struct regulator_init_data *init_data;
3506         static atomic_t regulator_no = ATOMIC_INIT(0);
3507         struct regulator_dev *rdev;
3508         struct device *dev;
3509         int ret, i;
3510         const char *supply = NULL;
3511
3512         if (regulator_desc == NULL || config == NULL)
3513                 return ERR_PTR(-EINVAL);
3514
3515         dev = config->dev;
3516         WARN_ON(!dev);
3517
3518         if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3519                 return ERR_PTR(-EINVAL);
3520
3521         if (regulator_desc->type != REGULATOR_VOLTAGE &&
3522             regulator_desc->type != REGULATOR_CURRENT)
3523                 return ERR_PTR(-EINVAL);
3524
3525         /* Only one of each should be implemented */
3526         WARN_ON(regulator_desc->ops->get_voltage &&
3527                 regulator_desc->ops->get_voltage_sel);
3528         WARN_ON(regulator_desc->ops->set_voltage &&
3529                 regulator_desc->ops->set_voltage_sel);
3530
3531         /* If we're using selectors we must implement list_voltage. */
3532         if (regulator_desc->ops->get_voltage_sel &&
3533             !regulator_desc->ops->list_voltage) {
3534                 return ERR_PTR(-EINVAL);
3535         }
3536         if (regulator_desc->ops->set_voltage_sel &&
3537             !regulator_desc->ops->list_voltage) {
3538                 return ERR_PTR(-EINVAL);
3539         }
3540
3541         init_data = config->init_data;
3542
3543         rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3544         if (rdev == NULL)
3545                 return ERR_PTR(-ENOMEM);
3546
3547         mutex_lock(&regulator_list_mutex);
3548
3549         mutex_init(&rdev->mutex);
3550         rdev->reg_data = config->driver_data;
3551         rdev->owner = regulator_desc->owner;
3552         rdev->desc = regulator_desc;
3553         if (config->regmap)
3554                 rdev->regmap = config->regmap;
3555         else if (dev_get_regmap(dev, NULL))
3556                 rdev->regmap = dev_get_regmap(dev, NULL);
3557         else if (dev->parent)
3558                 rdev->regmap = dev_get_regmap(dev->parent, NULL);
3559         INIT_LIST_HEAD(&rdev->consumer_list);
3560         INIT_LIST_HEAD(&rdev->list);
3561         BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3562         INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3563
3564         /* preform any regulator specific init */
3565         if (init_data && init_data->regulator_init) {
3566                 ret = init_data->regulator_init(rdev->reg_data);
3567                 if (ret < 0)
3568                         goto clean;
3569         }
3570
3571         /* register with sysfs */
3572         rdev->dev.class = &regulator_class;
3573         rdev->dev.of_node = config->of_node;
3574         rdev->dev.parent = dev;
3575         dev_set_name(&rdev->dev, "regulator.%d",
3576                      atomic_inc_return(&regulator_no) - 1);
3577         ret = device_register(&rdev->dev);
3578         if (ret != 0) {
3579                 put_device(&rdev->dev);
3580                 goto clean;
3581         }
3582
3583         dev_set_drvdata(&rdev->dev, rdev);
3584
3585         if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
3586                 ret = regulator_ena_gpio_request(rdev, config);
3587                 if (ret != 0) {
3588                         rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3589                                  config->ena_gpio, ret);
3590                         goto wash;
3591                 }
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                         if (!_regulator_is_enabled(rdev)) {
3761                                 error = _regulator_do_enable(rdev);
3762                                 if (error)
3763                                         ret = error;
3764                         }
3765                 } else {
3766                         if (!has_full_constraints)
3767                                 goto unlock;
3768                         if (!_regulator_is_enabled(rdev))
3769                                 goto unlock;
3770
3771                         error = _regulator_do_disable(rdev);
3772                         if (error)
3773                                 ret = error;
3774                 }
3775 unlock:
3776                 mutex_unlock(&rdev->mutex);
3777         }
3778         mutex_unlock(&regulator_list_mutex);
3779         return ret;
3780 }
3781 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3782
3783 /**
3784  * regulator_has_full_constraints - the system has fully specified constraints
3785  *
3786  * Calling this function will cause the regulator API to disable all
3787  * regulators which have a zero use count and don't have an always_on
3788  * constraint in a late_initcall.
3789  *
3790  * The intention is that this will become the default behaviour in a
3791  * future kernel release so users are encouraged to use this facility
3792  * now.
3793  */
3794 void regulator_has_full_constraints(void)
3795 {
3796         has_full_constraints = 1;
3797 }
3798 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3799
3800 /**
3801  * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3802  *
3803  * Calling this function will cause the regulator API to provide a
3804  * dummy regulator to consumers if no physical regulator is found,
3805  * allowing most consumers to proceed as though a regulator were
3806  * configured.  This allows systems such as those with software
3807  * controllable regulators for the CPU core only to be brought up more
3808  * readily.
3809  */
3810 void regulator_use_dummy_regulator(void)
3811 {
3812         board_wants_dummy_regulator = true;
3813 }
3814 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3815
3816 /**
3817  * rdev_get_drvdata - get rdev regulator driver data
3818  * @rdev: regulator
3819  *
3820  * Get rdev regulator driver private data. This call can be used in the
3821  * regulator driver context.
3822  */
3823 void *rdev_get_drvdata(struct regulator_dev *rdev)
3824 {
3825         return rdev->reg_data;
3826 }
3827 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3828
3829 /**
3830  * regulator_get_drvdata - get regulator driver data
3831  * @regulator: regulator
3832  *
3833  * Get regulator driver private data. This call can be used in the consumer
3834  * driver context when non API regulator specific functions need to be called.
3835  */
3836 void *regulator_get_drvdata(struct regulator *regulator)
3837 {
3838         return regulator->rdev->reg_data;
3839 }
3840 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3841
3842 /**
3843  * regulator_set_drvdata - set regulator driver data
3844  * @regulator: regulator
3845  * @data: data
3846  */
3847 void regulator_set_drvdata(struct regulator *regulator, void *data)
3848 {
3849         regulator->rdev->reg_data = data;
3850 }
3851 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3852
3853 /**
3854  * regulator_get_id - get regulator ID
3855  * @rdev: regulator
3856  */
3857 int rdev_get_id(struct regulator_dev *rdev)
3858 {
3859         return rdev->desc->id;
3860 }
3861 EXPORT_SYMBOL_GPL(rdev_get_id);
3862
3863 struct device *rdev_get_dev(struct regulator_dev *rdev)
3864 {
3865         return &rdev->dev;
3866 }
3867 EXPORT_SYMBOL_GPL(rdev_get_dev);
3868
3869 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3870 {
3871         return reg_init_data->driver_data;
3872 }
3873 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3874
3875 #ifdef CONFIG_DEBUG_FS
3876 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3877                                     size_t count, loff_t *ppos)
3878 {
3879         char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3880         ssize_t len, ret = 0;
3881         struct regulator_map *map;
3882
3883         if (!buf)
3884                 return -ENOMEM;
3885
3886         list_for_each_entry(map, &regulator_map_list, list) {
3887                 len = snprintf(buf + ret, PAGE_SIZE - ret,
3888                                "%s -> %s.%s\n",
3889                                rdev_get_name(map->regulator), map->dev_name,
3890                                map->supply);
3891                 if (len >= 0)
3892                         ret += len;
3893                 if (ret > PAGE_SIZE) {
3894                         ret = PAGE_SIZE;
3895                         break;
3896                 }
3897         }
3898
3899         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3900
3901         kfree(buf);
3902
3903         return ret;
3904 }
3905 #endif
3906
3907 static const struct file_operations supply_map_fops = {
3908 #ifdef CONFIG_DEBUG_FS
3909         .read = supply_map_read_file,
3910         .llseek = default_llseek,
3911 #endif
3912 };
3913
3914 static int __init regulator_init(void)
3915 {
3916         int ret;
3917
3918         ret = class_register(&regulator_class);
3919
3920         debugfs_root = debugfs_create_dir("regulator", NULL);
3921         if (!debugfs_root)
3922                 pr_warn("regulator: Failed to create debugfs directory\n");
3923
3924         debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3925                             &supply_map_fops);
3926
3927         regulator_dummy_init();
3928
3929         return ret;
3930 }
3931
3932 /* init early to allow our consumers to complete system booting */
3933 core_initcall(regulator_init);
3934
3935 static int __init regulator_init_complete(void)
3936 {
3937         struct regulator_dev *rdev;
3938         struct regulator_ops *ops;
3939         struct regulation_constraints *c;
3940         int enabled, ret;
3941
3942         /*
3943          * Since DT doesn't provide an idiomatic mechanism for
3944          * enabling full constraints and since it's much more natural
3945          * with DT to provide them just assume that a DT enabled
3946          * system has full constraints.
3947          */
3948         if (of_have_populated_dt())
3949                 has_full_constraints = true;
3950
3951         mutex_lock(&regulator_list_mutex);
3952
3953         /* If we have a full configuration then disable any regulators
3954          * which are not in use or always_on.  This will become the
3955          * default behaviour in the future.
3956          */
3957         list_for_each_entry(rdev, &regulator_list, list) {
3958                 ops = rdev->desc->ops;
3959                 c = rdev->constraints;
3960
3961                 if (c && c->always_on)
3962                         continue;
3963
3964                 mutex_lock(&rdev->mutex);
3965
3966                 if (rdev->use_count)
3967                         goto unlock;
3968
3969                 /* If we can't read the status assume it's on. */
3970                 if (ops->is_enabled)
3971                         enabled = ops->is_enabled(rdev);
3972                 else
3973                         enabled = 1;
3974
3975                 if (!enabled)
3976                         goto unlock;
3977
3978                 if (has_full_constraints) {
3979                         /* We log since this may kill the system if it
3980                          * goes wrong. */
3981                         rdev_info(rdev, "disabling\n");
3982                         ret = _regulator_do_disable(rdev);
3983                         if (ret != 0) {
3984                                 rdev_err(rdev, "couldn't disable: %d\n", ret);
3985                         }
3986                 } else {
3987                         /* The intention is that in future we will
3988                          * assume that full constraints are provided
3989                          * so warn even if we aren't going to do
3990                          * anything here.
3991                          */
3992                         rdev_warn(rdev, "incomplete constraints, leaving on\n");
3993                 }
3994
3995 unlock:
3996                 mutex_unlock(&rdev->mutex);
3997         }
3998
3999         mutex_unlock(&regulator_list_mutex);
4000
4001         return 0;
4002 }
4003 late_initcall(regulator_init_complete);