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