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