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