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