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