1
2
3
4
5
6
7
8
9
10#include <linux/kernel.h>
11#include <linux/init.h>
12#include <linux/debugfs.h>
13#include <linux/device.h>
14#include <linux/slab.h>
15#include <linux/async.h>
16#include <linux/err.h>
17#include <linux/mutex.h>
18#include <linux/suspend.h>
19#include <linux/delay.h>
20#include <linux/gpio/consumer.h>
21#include <linux/of.h>
22#include <linux/regmap.h>
23#include <linux/regulator/of_regulator.h>
24#include <linux/regulator/consumer.h>
25#include <linux/regulator/coupler.h>
26#include <linux/regulator/driver.h>
27#include <linux/regulator/machine.h>
28#include <linux/module.h>
29
30#define CREATE_TRACE_POINTS
31#include <trace/events/regulator.h>
32
33#include "dummy.h"
34#include "internal.h"
35
36#define rdev_crit(rdev, fmt, ...) \
37 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
38#define rdev_err(rdev, fmt, ...) \
39 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
40#define rdev_warn(rdev, fmt, ...) \
41 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42#define rdev_info(rdev, fmt, ...) \
43 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44#define rdev_dbg(rdev, fmt, ...) \
45 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46
47static DEFINE_WW_CLASS(regulator_ww_class);
48static DEFINE_MUTEX(regulator_nesting_mutex);
49static DEFINE_MUTEX(regulator_list_mutex);
50static LIST_HEAD(regulator_map_list);
51static LIST_HEAD(regulator_ena_gpio_list);
52static LIST_HEAD(regulator_supply_alias_list);
53static LIST_HEAD(regulator_coupler_list);
54static bool has_full_constraints;
55
56static struct dentry *debugfs_root;
57
58
59
60
61
62
63struct regulator_map {
64 struct list_head list;
65 const char *dev_name;
66 const char *supply;
67 struct regulator_dev *regulator;
68};
69
70
71
72
73
74
75struct regulator_enable_gpio {
76 struct list_head list;
77 struct gpio_desc *gpiod;
78 u32 enable_count;
79 u32 request_count;
80};
81
82
83
84
85
86
87struct regulator_supply_alias {
88 struct list_head list;
89 struct device *src_dev;
90 const char *src_supply;
91 struct device *alias_dev;
92 const char *alias_supply;
93};
94
95static int _regulator_is_enabled(struct regulator_dev *rdev);
96static int _regulator_disable(struct regulator *regulator);
97static int _regulator_get_current_limit(struct regulator_dev *rdev);
98static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
99static int _notifier_call_chain(struct regulator_dev *rdev,
100 unsigned long event, void *data);
101static int _regulator_do_set_voltage(struct regulator_dev *rdev,
102 int min_uV, int max_uV);
103static int regulator_balance_voltage(struct regulator_dev *rdev,
104 suspend_state_t state);
105static struct regulator *create_regulator(struct regulator_dev *rdev,
106 struct device *dev,
107 const char *supply_name);
108static void _regulator_put(struct regulator *regulator);
109
110const char *rdev_get_name(struct regulator_dev *rdev)
111{
112 if (rdev->constraints && rdev->constraints->name)
113 return rdev->constraints->name;
114 else if (rdev->desc->name)
115 return rdev->desc->name;
116 else
117 return "";
118}
119
120static bool have_full_constraints(void)
121{
122 return has_full_constraints || of_have_populated_dt();
123}
124
125static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
126{
127 if (!rdev->constraints) {
128 rdev_err(rdev, "no constraints\n");
129 return false;
130 }
131
132 if (rdev->constraints->valid_ops_mask & ops)
133 return true;
134
135 return false;
136}
137
138
139
140
141
142
143
144
145
146
147
148
149static inline int regulator_lock_nested(struct regulator_dev *rdev,
150 struct ww_acquire_ctx *ww_ctx)
151{
152 bool lock = false;
153 int ret = 0;
154
155 mutex_lock(®ulator_nesting_mutex);
156
157 if (ww_ctx || !ww_mutex_trylock(&rdev->mutex)) {
158 if (rdev->mutex_owner == current)
159 rdev->ref_cnt++;
160 else
161 lock = true;
162
163 if (lock) {
164 mutex_unlock(®ulator_nesting_mutex);
165 ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
166 mutex_lock(®ulator_nesting_mutex);
167 }
168 } else {
169 lock = true;
170 }
171
172 if (lock && ret != -EDEADLK) {
173 rdev->ref_cnt++;
174 rdev->mutex_owner = current;
175 }
176
177 mutex_unlock(®ulator_nesting_mutex);
178
179 return ret;
180}
181
182
183
184
185
186
187
188
189
190
191
192void regulator_lock(struct regulator_dev *rdev)
193{
194 regulator_lock_nested(rdev, NULL);
195}
196EXPORT_SYMBOL_GPL(regulator_lock);
197
198
199
200
201
202
203
204
205void regulator_unlock(struct regulator_dev *rdev)
206{
207 mutex_lock(®ulator_nesting_mutex);
208
209 if (--rdev->ref_cnt == 0) {
210 rdev->mutex_owner = NULL;
211 ww_mutex_unlock(&rdev->mutex);
212 }
213
214 WARN_ON_ONCE(rdev->ref_cnt < 0);
215
216 mutex_unlock(®ulator_nesting_mutex);
217}
218EXPORT_SYMBOL_GPL(regulator_unlock);
219
220static bool regulator_supply_is_couple(struct regulator_dev *rdev)
221{
222 struct regulator_dev *c_rdev;
223 int i;
224
225 for (i = 1; i < rdev->coupling_desc.n_coupled; i++) {
226 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
227
228 if (rdev->supply->rdev == c_rdev)
229 return true;
230 }
231
232 return false;
233}
234
235static void regulator_unlock_recursive(struct regulator_dev *rdev,
236 unsigned int n_coupled)
237{
238 struct regulator_dev *c_rdev;
239 int i;
240
241 for (i = n_coupled; i > 0; i--) {
242 c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1];
243
244 if (!c_rdev)
245 continue;
246
247 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev))
248 regulator_unlock_recursive(
249 c_rdev->supply->rdev,
250 c_rdev->coupling_desc.n_coupled);
251
252 regulator_unlock(c_rdev);
253 }
254}
255
256static int regulator_lock_recursive(struct regulator_dev *rdev,
257 struct regulator_dev **new_contended_rdev,
258 struct regulator_dev **old_contended_rdev,
259 struct ww_acquire_ctx *ww_ctx)
260{
261 struct regulator_dev *c_rdev;
262 int i, err;
263
264 for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
265 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
266
267 if (!c_rdev)
268 continue;
269
270 if (c_rdev != *old_contended_rdev) {
271 err = regulator_lock_nested(c_rdev, ww_ctx);
272 if (err) {
273 if (err == -EDEADLK) {
274 *new_contended_rdev = c_rdev;
275 goto err_unlock;
276 }
277
278
279 WARN_ON_ONCE(err != -EALREADY);
280 }
281 } else {
282 *old_contended_rdev = NULL;
283 }
284
285 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
286 err = regulator_lock_recursive(c_rdev->supply->rdev,
287 new_contended_rdev,
288 old_contended_rdev,
289 ww_ctx);
290 if (err) {
291 regulator_unlock(c_rdev);
292 goto err_unlock;
293 }
294 }
295 }
296
297 return 0;
298
299err_unlock:
300 regulator_unlock_recursive(rdev, i);
301
302 return err;
303}
304
305
306
307
308
309
310
311
312
313static void regulator_unlock_dependent(struct regulator_dev *rdev,
314 struct ww_acquire_ctx *ww_ctx)
315{
316 regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled);
317 ww_acquire_fini(ww_ctx);
318}
319
320
321
322
323
324
325
326
327
328static void regulator_lock_dependent(struct regulator_dev *rdev,
329 struct ww_acquire_ctx *ww_ctx)
330{
331 struct regulator_dev *new_contended_rdev = NULL;
332 struct regulator_dev *old_contended_rdev = NULL;
333 int err;
334
335 mutex_lock(®ulator_list_mutex);
336
337 ww_acquire_init(ww_ctx, ®ulator_ww_class);
338
339 do {
340 if (new_contended_rdev) {
341 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
342 old_contended_rdev = new_contended_rdev;
343 old_contended_rdev->ref_cnt++;
344 }
345
346 err = regulator_lock_recursive(rdev,
347 &new_contended_rdev,
348 &old_contended_rdev,
349 ww_ctx);
350
351 if (old_contended_rdev)
352 regulator_unlock(old_contended_rdev);
353
354 } while (err == -EDEADLK);
355
356 ww_acquire_done(ww_ctx);
357
358 mutex_unlock(®ulator_list_mutex);
359}
360
361
362
363
364
365
366
367
368
369
370
371
372static struct device_node *of_get_child_regulator(struct device_node *parent,
373 const char *prop_name)
374{
375 struct device_node *regnode = NULL;
376 struct device_node *child = NULL;
377
378 for_each_child_of_node(parent, child) {
379 regnode = of_parse_phandle(child, prop_name, 0);
380
381 if (!regnode) {
382 regnode = of_get_child_regulator(child, prop_name);
383 if (regnode)
384 goto err_node_put;
385 } else {
386 goto err_node_put;
387 }
388 }
389 return NULL;
390
391err_node_put:
392 of_node_put(child);
393 return regnode;
394}
395
396
397
398
399
400
401
402
403
404
405static struct device_node *of_get_regulator(struct device *dev, const char *supply)
406{
407 struct device_node *regnode = NULL;
408 char prop_name[32];
409
410 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
411
412 snprintf(prop_name, 32, "%s-supply", supply);
413 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
414
415 if (!regnode) {
416 regnode = of_get_child_regulator(dev->of_node, prop_name);
417 if (regnode)
418 return regnode;
419
420 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
421 prop_name, dev->of_node);
422 return NULL;
423 }
424 return regnode;
425}
426
427
428int regulator_check_voltage(struct regulator_dev *rdev,
429 int *min_uV, int *max_uV)
430{
431 BUG_ON(*min_uV > *max_uV);
432
433 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
434 rdev_err(rdev, "voltage operation not allowed\n");
435 return -EPERM;
436 }
437
438 if (*max_uV > rdev->constraints->max_uV)
439 *max_uV = rdev->constraints->max_uV;
440 if (*min_uV < rdev->constraints->min_uV)
441 *min_uV = rdev->constraints->min_uV;
442
443 if (*min_uV > *max_uV) {
444 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
445 *min_uV, *max_uV);
446 return -EINVAL;
447 }
448
449 return 0;
450}
451
452
453static int regulator_check_states(suspend_state_t state)
454{
455 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
456}
457
458
459
460
461int regulator_check_consumers(struct regulator_dev *rdev,
462 int *min_uV, int *max_uV,
463 suspend_state_t state)
464{
465 struct regulator *regulator;
466 struct regulator_voltage *voltage;
467
468 list_for_each_entry(regulator, &rdev->consumer_list, list) {
469 voltage = ®ulator->voltage[state];
470
471
472
473
474 if (!voltage->min_uV && !voltage->max_uV)
475 continue;
476
477 if (*max_uV > voltage->max_uV)
478 *max_uV = voltage->max_uV;
479 if (*min_uV < voltage->min_uV)
480 *min_uV = voltage->min_uV;
481 }
482
483 if (*min_uV > *max_uV) {
484 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
485 *min_uV, *max_uV);
486 return -EINVAL;
487 }
488
489 return 0;
490}
491
492
493static int regulator_check_current_limit(struct regulator_dev *rdev,
494 int *min_uA, int *max_uA)
495{
496 BUG_ON(*min_uA > *max_uA);
497
498 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
499 rdev_err(rdev, "current operation not allowed\n");
500 return -EPERM;
501 }
502
503 if (*max_uA > rdev->constraints->max_uA)
504 *max_uA = rdev->constraints->max_uA;
505 if (*min_uA < rdev->constraints->min_uA)
506 *min_uA = rdev->constraints->min_uA;
507
508 if (*min_uA > *max_uA) {
509 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
510 *min_uA, *max_uA);
511 return -EINVAL;
512 }
513
514 return 0;
515}
516
517
518static int regulator_mode_constrain(struct regulator_dev *rdev,
519 unsigned int *mode)
520{
521 switch (*mode) {
522 case REGULATOR_MODE_FAST:
523 case REGULATOR_MODE_NORMAL:
524 case REGULATOR_MODE_IDLE:
525 case REGULATOR_MODE_STANDBY:
526 break;
527 default:
528 rdev_err(rdev, "invalid mode %x specified\n", *mode);
529 return -EINVAL;
530 }
531
532 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
533 rdev_err(rdev, "mode operation not allowed\n");
534 return -EPERM;
535 }
536
537
538
539
540 while (*mode) {
541 if (rdev->constraints->valid_modes_mask & *mode)
542 return 0;
543 *mode /= 2;
544 }
545
546 return -EINVAL;
547}
548
549static inline struct regulator_state *
550regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
551{
552 if (rdev->constraints == NULL)
553 return NULL;
554
555 switch (state) {
556 case PM_SUSPEND_STANDBY:
557 return &rdev->constraints->state_standby;
558 case PM_SUSPEND_MEM:
559 return &rdev->constraints->state_mem;
560 case PM_SUSPEND_MAX:
561 return &rdev->constraints->state_disk;
562 default:
563 return NULL;
564 }
565}
566
567static ssize_t regulator_uV_show(struct device *dev,
568 struct device_attribute *attr, char *buf)
569{
570 struct regulator_dev *rdev = dev_get_drvdata(dev);
571 int uV;
572
573 regulator_lock(rdev);
574 uV = regulator_get_voltage_rdev(rdev);
575 regulator_unlock(rdev);
576
577 if (uV < 0)
578 return uV;
579 return sprintf(buf, "%d\n", uV);
580}
581static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
582
583static ssize_t regulator_uA_show(struct device *dev,
584 struct device_attribute *attr, char *buf)
585{
586 struct regulator_dev *rdev = dev_get_drvdata(dev);
587
588 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
589}
590static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
591
592static ssize_t name_show(struct device *dev, struct device_attribute *attr,
593 char *buf)
594{
595 struct regulator_dev *rdev = dev_get_drvdata(dev);
596
597 return sprintf(buf, "%s\n", rdev_get_name(rdev));
598}
599static DEVICE_ATTR_RO(name);
600
601static const char *regulator_opmode_to_str(int mode)
602{
603 switch (mode) {
604 case REGULATOR_MODE_FAST:
605 return "fast";
606 case REGULATOR_MODE_NORMAL:
607 return "normal";
608 case REGULATOR_MODE_IDLE:
609 return "idle";
610 case REGULATOR_MODE_STANDBY:
611 return "standby";
612 }
613 return "unknown";
614}
615
616static ssize_t regulator_print_opmode(char *buf, int mode)
617{
618 return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
619}
620
621static ssize_t regulator_opmode_show(struct device *dev,
622 struct device_attribute *attr, char *buf)
623{
624 struct regulator_dev *rdev = dev_get_drvdata(dev);
625
626 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
627}
628static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
629
630static ssize_t regulator_print_state(char *buf, int state)
631{
632 if (state > 0)
633 return sprintf(buf, "enabled\n");
634 else if (state == 0)
635 return sprintf(buf, "disabled\n");
636 else
637 return sprintf(buf, "unknown\n");
638}
639
640static ssize_t regulator_state_show(struct device *dev,
641 struct device_attribute *attr, char *buf)
642{
643 struct regulator_dev *rdev = dev_get_drvdata(dev);
644 ssize_t ret;
645
646 regulator_lock(rdev);
647 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
648 regulator_unlock(rdev);
649
650 return ret;
651}
652static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
653
654static ssize_t regulator_status_show(struct device *dev,
655 struct device_attribute *attr, char *buf)
656{
657 struct regulator_dev *rdev = dev_get_drvdata(dev);
658 int status;
659 char *label;
660
661 status = rdev->desc->ops->get_status(rdev);
662 if (status < 0)
663 return status;
664
665 switch (status) {
666 case REGULATOR_STATUS_OFF:
667 label = "off";
668 break;
669 case REGULATOR_STATUS_ON:
670 label = "on";
671 break;
672 case REGULATOR_STATUS_ERROR:
673 label = "error";
674 break;
675 case REGULATOR_STATUS_FAST:
676 label = "fast";
677 break;
678 case REGULATOR_STATUS_NORMAL:
679 label = "normal";
680 break;
681 case REGULATOR_STATUS_IDLE:
682 label = "idle";
683 break;
684 case REGULATOR_STATUS_STANDBY:
685 label = "standby";
686 break;
687 case REGULATOR_STATUS_BYPASS:
688 label = "bypass";
689 break;
690 case REGULATOR_STATUS_UNDEFINED:
691 label = "undefined";
692 break;
693 default:
694 return -ERANGE;
695 }
696
697 return sprintf(buf, "%s\n", label);
698}
699static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
700
701static ssize_t regulator_min_uA_show(struct device *dev,
702 struct device_attribute *attr, char *buf)
703{
704 struct regulator_dev *rdev = dev_get_drvdata(dev);
705
706 if (!rdev->constraints)
707 return sprintf(buf, "constraint not defined\n");
708
709 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
710}
711static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
712
713static ssize_t regulator_max_uA_show(struct device *dev,
714 struct device_attribute *attr, char *buf)
715{
716 struct regulator_dev *rdev = dev_get_drvdata(dev);
717
718 if (!rdev->constraints)
719 return sprintf(buf, "constraint not defined\n");
720
721 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
722}
723static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
724
725static ssize_t regulator_min_uV_show(struct device *dev,
726 struct device_attribute *attr, char *buf)
727{
728 struct regulator_dev *rdev = dev_get_drvdata(dev);
729
730 if (!rdev->constraints)
731 return sprintf(buf, "constraint not defined\n");
732
733 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
734}
735static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
736
737static ssize_t regulator_max_uV_show(struct device *dev,
738 struct device_attribute *attr, char *buf)
739{
740 struct regulator_dev *rdev = dev_get_drvdata(dev);
741
742 if (!rdev->constraints)
743 return sprintf(buf, "constraint not defined\n");
744
745 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
746}
747static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
748
749static ssize_t regulator_total_uA_show(struct device *dev,
750 struct device_attribute *attr, char *buf)
751{
752 struct regulator_dev *rdev = dev_get_drvdata(dev);
753 struct regulator *regulator;
754 int uA = 0;
755
756 regulator_lock(rdev);
757 list_for_each_entry(regulator, &rdev->consumer_list, list) {
758 if (regulator->enable_count)
759 uA += regulator->uA_load;
760 }
761 regulator_unlock(rdev);
762 return sprintf(buf, "%d\n", uA);
763}
764static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
765
766static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
767 char *buf)
768{
769 struct regulator_dev *rdev = dev_get_drvdata(dev);
770 return sprintf(buf, "%d\n", rdev->use_count);
771}
772static DEVICE_ATTR_RO(num_users);
773
774static ssize_t type_show(struct device *dev, struct device_attribute *attr,
775 char *buf)
776{
777 struct regulator_dev *rdev = dev_get_drvdata(dev);
778
779 switch (rdev->desc->type) {
780 case REGULATOR_VOLTAGE:
781 return sprintf(buf, "voltage\n");
782 case REGULATOR_CURRENT:
783 return sprintf(buf, "current\n");
784 }
785 return sprintf(buf, "unknown\n");
786}
787static DEVICE_ATTR_RO(type);
788
789static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
790 struct device_attribute *attr, char *buf)
791{
792 struct regulator_dev *rdev = dev_get_drvdata(dev);
793
794 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
795}
796static DEVICE_ATTR(suspend_mem_microvolts, 0444,
797 regulator_suspend_mem_uV_show, NULL);
798
799static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
800 struct device_attribute *attr, char *buf)
801{
802 struct regulator_dev *rdev = dev_get_drvdata(dev);
803
804 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
805}
806static DEVICE_ATTR(suspend_disk_microvolts, 0444,
807 regulator_suspend_disk_uV_show, NULL);
808
809static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
810 struct device_attribute *attr, char *buf)
811{
812 struct regulator_dev *rdev = dev_get_drvdata(dev);
813
814 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
815}
816static DEVICE_ATTR(suspend_standby_microvolts, 0444,
817 regulator_suspend_standby_uV_show, NULL);
818
819static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
820 struct device_attribute *attr, char *buf)
821{
822 struct regulator_dev *rdev = dev_get_drvdata(dev);
823
824 return regulator_print_opmode(buf,
825 rdev->constraints->state_mem.mode);
826}
827static DEVICE_ATTR(suspend_mem_mode, 0444,
828 regulator_suspend_mem_mode_show, NULL);
829
830static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
831 struct device_attribute *attr, char *buf)
832{
833 struct regulator_dev *rdev = dev_get_drvdata(dev);
834
835 return regulator_print_opmode(buf,
836 rdev->constraints->state_disk.mode);
837}
838static DEVICE_ATTR(suspend_disk_mode, 0444,
839 regulator_suspend_disk_mode_show, NULL);
840
841static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
842 struct device_attribute *attr, char *buf)
843{
844 struct regulator_dev *rdev = dev_get_drvdata(dev);
845
846 return regulator_print_opmode(buf,
847 rdev->constraints->state_standby.mode);
848}
849static DEVICE_ATTR(suspend_standby_mode, 0444,
850 regulator_suspend_standby_mode_show, NULL);
851
852static ssize_t regulator_suspend_mem_state_show(struct device *dev,
853 struct device_attribute *attr, char *buf)
854{
855 struct regulator_dev *rdev = dev_get_drvdata(dev);
856
857 return regulator_print_state(buf,
858 rdev->constraints->state_mem.enabled);
859}
860static DEVICE_ATTR(suspend_mem_state, 0444,
861 regulator_suspend_mem_state_show, NULL);
862
863static ssize_t regulator_suspend_disk_state_show(struct device *dev,
864 struct device_attribute *attr, char *buf)
865{
866 struct regulator_dev *rdev = dev_get_drvdata(dev);
867
868 return regulator_print_state(buf,
869 rdev->constraints->state_disk.enabled);
870}
871static DEVICE_ATTR(suspend_disk_state, 0444,
872 regulator_suspend_disk_state_show, NULL);
873
874static ssize_t regulator_suspend_standby_state_show(struct device *dev,
875 struct device_attribute *attr, char *buf)
876{
877 struct regulator_dev *rdev = dev_get_drvdata(dev);
878
879 return regulator_print_state(buf,
880 rdev->constraints->state_standby.enabled);
881}
882static DEVICE_ATTR(suspend_standby_state, 0444,
883 regulator_suspend_standby_state_show, NULL);
884
885static ssize_t regulator_bypass_show(struct device *dev,
886 struct device_attribute *attr, char *buf)
887{
888 struct regulator_dev *rdev = dev_get_drvdata(dev);
889 const char *report;
890 bool bypass;
891 int ret;
892
893 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
894
895 if (ret != 0)
896 report = "unknown";
897 else if (bypass)
898 report = "enabled";
899 else
900 report = "disabled";
901
902 return sprintf(buf, "%s\n", report);
903}
904static DEVICE_ATTR(bypass, 0444,
905 regulator_bypass_show, NULL);
906
907
908
909static int drms_uA_update(struct regulator_dev *rdev)
910{
911 struct regulator *sibling;
912 int current_uA = 0, output_uV, input_uV, err;
913 unsigned int mode;
914
915
916
917
918
919 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) {
920 rdev_dbg(rdev, "DRMS operation not allowed\n");
921 return 0;
922 }
923
924 if (!rdev->desc->ops->get_optimum_mode &&
925 !rdev->desc->ops->set_load)
926 return 0;
927
928 if (!rdev->desc->ops->set_mode &&
929 !rdev->desc->ops->set_load)
930 return -EINVAL;
931
932
933 list_for_each_entry(sibling, &rdev->consumer_list, list) {
934 if (sibling->enable_count)
935 current_uA += sibling->uA_load;
936 }
937
938 current_uA += rdev->constraints->system_load;
939
940 if (rdev->desc->ops->set_load) {
941
942 err = rdev->desc->ops->set_load(rdev, current_uA);
943 if (err < 0)
944 rdev_err(rdev, "failed to set load %d\n", current_uA);
945 } else {
946
947 output_uV = regulator_get_voltage_rdev(rdev);
948 if (output_uV <= 0) {
949 rdev_err(rdev, "invalid output voltage found\n");
950 return -EINVAL;
951 }
952
953
954 input_uV = 0;
955 if (rdev->supply)
956 input_uV = regulator_get_voltage(rdev->supply);
957 if (input_uV <= 0)
958 input_uV = rdev->constraints->input_uV;
959 if (input_uV <= 0) {
960 rdev_err(rdev, "invalid input voltage found\n");
961 return -EINVAL;
962 }
963
964
965 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
966 output_uV, current_uA);
967
968
969 err = regulator_mode_constrain(rdev, &mode);
970 if (err < 0) {
971 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
972 current_uA, input_uV, output_uV);
973 return err;
974 }
975
976 err = rdev->desc->ops->set_mode(rdev, mode);
977 if (err < 0)
978 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
979 }
980
981 return err;
982}
983
984static int suspend_set_state(struct regulator_dev *rdev,
985 suspend_state_t state)
986{
987 int ret = 0;
988 struct regulator_state *rstate;
989
990 rstate = regulator_get_suspend_state(rdev, state);
991 if (rstate == NULL)
992 return 0;
993
994
995
996
997
998 if (rstate->enabled != ENABLE_IN_SUSPEND &&
999 rstate->enabled != DISABLE_IN_SUSPEND) {
1000 if (rdev->desc->ops->set_suspend_voltage ||
1001 rdev->desc->ops->set_suspend_mode)
1002 rdev_warn(rdev, "No configuration\n");
1003 return 0;
1004 }
1005
1006 if (rstate->enabled == ENABLE_IN_SUSPEND &&
1007 rdev->desc->ops->set_suspend_enable)
1008 ret = rdev->desc->ops->set_suspend_enable(rdev);
1009 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
1010 rdev->desc->ops->set_suspend_disable)
1011 ret = rdev->desc->ops->set_suspend_disable(rdev);
1012 else
1013 ret = 0;
1014
1015 if (ret < 0) {
1016 rdev_err(rdev, "failed to enabled/disable\n");
1017 return ret;
1018 }
1019
1020 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
1021 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
1022 if (ret < 0) {
1023 rdev_err(rdev, "failed to set voltage\n");
1024 return ret;
1025 }
1026 }
1027
1028 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
1029 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
1030 if (ret < 0) {
1031 rdev_err(rdev, "failed to set mode\n");
1032 return ret;
1033 }
1034 }
1035
1036 return ret;
1037}
1038
1039static void print_constraints(struct regulator_dev *rdev)
1040{
1041 struct regulation_constraints *constraints = rdev->constraints;
1042 char buf[160] = "";
1043 size_t len = sizeof(buf) - 1;
1044 int count = 0;
1045 int ret;
1046
1047 if (constraints->min_uV && constraints->max_uV) {
1048 if (constraints->min_uV == constraints->max_uV)
1049 count += scnprintf(buf + count, len - count, "%d mV ",
1050 constraints->min_uV / 1000);
1051 else
1052 count += scnprintf(buf + count, len - count,
1053 "%d <--> %d mV ",
1054 constraints->min_uV / 1000,
1055 constraints->max_uV / 1000);
1056 }
1057
1058 if (!constraints->min_uV ||
1059 constraints->min_uV != constraints->max_uV) {
1060 ret = regulator_get_voltage_rdev(rdev);
1061 if (ret > 0)
1062 count += scnprintf(buf + count, len - count,
1063 "at %d mV ", ret / 1000);
1064 }
1065
1066 if (constraints->uV_offset)
1067 count += scnprintf(buf + count, len - count, "%dmV offset ",
1068 constraints->uV_offset / 1000);
1069
1070 if (constraints->min_uA && constraints->max_uA) {
1071 if (constraints->min_uA == constraints->max_uA)
1072 count += scnprintf(buf + count, len - count, "%d mA ",
1073 constraints->min_uA / 1000);
1074 else
1075 count += scnprintf(buf + count, len - count,
1076 "%d <--> %d mA ",
1077 constraints->min_uA / 1000,
1078 constraints->max_uA / 1000);
1079 }
1080
1081 if (!constraints->min_uA ||
1082 constraints->min_uA != constraints->max_uA) {
1083 ret = _regulator_get_current_limit(rdev);
1084 if (ret > 0)
1085 count += scnprintf(buf + count, len - count,
1086 "at %d mA ", ret / 1000);
1087 }
1088
1089 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
1090 count += scnprintf(buf + count, len - count, "fast ");
1091 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
1092 count += scnprintf(buf + count, len - count, "normal ");
1093 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
1094 count += scnprintf(buf + count, len - count, "idle ");
1095 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
1096 count += scnprintf(buf + count, len - count, "standby");
1097
1098 if (!count)
1099 scnprintf(buf, len, "no parameters");
1100
1101 rdev_dbg(rdev, "%s\n", buf);
1102
1103 if ((constraints->min_uV != constraints->max_uV) &&
1104 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
1105 rdev_warn(rdev,
1106 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1107}
1108
1109static int machine_constraints_voltage(struct regulator_dev *rdev,
1110 struct regulation_constraints *constraints)
1111{
1112 const struct regulator_ops *ops = rdev->desc->ops;
1113 int ret;
1114
1115
1116 if (rdev->constraints->apply_uV &&
1117 rdev->constraints->min_uV && rdev->constraints->max_uV) {
1118 int target_min, target_max;
1119 int current_uV = regulator_get_voltage_rdev(rdev);
1120
1121 if (current_uV == -ENOTRECOVERABLE) {
1122
1123 rdev_info(rdev, "Setting %d-%duV\n",
1124 rdev->constraints->min_uV,
1125 rdev->constraints->max_uV);
1126 _regulator_do_set_voltage(rdev,
1127 rdev->constraints->min_uV,
1128 rdev->constraints->max_uV);
1129 current_uV = regulator_get_voltage_rdev(rdev);
1130 }
1131
1132 if (current_uV < 0) {
1133 rdev_err(rdev,
1134 "failed to get the current voltage(%d)\n",
1135 current_uV);
1136 return current_uV;
1137 }
1138
1139
1140
1141
1142
1143
1144 target_min = current_uV;
1145 target_max = current_uV;
1146
1147 if (current_uV < rdev->constraints->min_uV) {
1148 target_min = rdev->constraints->min_uV;
1149 target_max = rdev->constraints->min_uV;
1150 }
1151
1152 if (current_uV > rdev->constraints->max_uV) {
1153 target_min = rdev->constraints->max_uV;
1154 target_max = rdev->constraints->max_uV;
1155 }
1156
1157 if (target_min != current_uV || target_max != current_uV) {
1158 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
1159 current_uV, target_min, target_max);
1160 ret = _regulator_do_set_voltage(
1161 rdev, target_min, target_max);
1162 if (ret < 0) {
1163 rdev_err(rdev,
1164 "failed to apply %d-%duV constraint(%d)\n",
1165 target_min, target_max, ret);
1166 return ret;
1167 }
1168 }
1169 }
1170
1171
1172
1173
1174 if (ops->list_voltage && rdev->desc->n_voltages) {
1175 int count = rdev->desc->n_voltages;
1176 int i;
1177 int min_uV = INT_MAX;
1178 int max_uV = INT_MIN;
1179 int cmin = constraints->min_uV;
1180 int cmax = constraints->max_uV;
1181
1182
1183
1184 if (count == 1 && !cmin) {
1185 cmin = 1;
1186 cmax = INT_MAX;
1187 constraints->min_uV = cmin;
1188 constraints->max_uV = cmax;
1189 }
1190
1191
1192 if ((cmin == 0) && (cmax == 0))
1193 return 0;
1194
1195
1196 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1197 rdev_err(rdev, "invalid voltage constraints\n");
1198 return -EINVAL;
1199 }
1200
1201
1202 if (rdev->desc->continuous_voltage_range)
1203 return 0;
1204
1205
1206 for (i = 0; i < count; i++) {
1207 int value;
1208
1209 value = ops->list_voltage(rdev, i);
1210 if (value <= 0)
1211 continue;
1212
1213
1214 if (value >= cmin && value < min_uV)
1215 min_uV = value;
1216 if (value <= cmax && value > max_uV)
1217 max_uV = value;
1218 }
1219
1220
1221 if (max_uV < min_uV) {
1222 rdev_err(rdev,
1223 "unsupportable voltage constraints %u-%uuV\n",
1224 min_uV, max_uV);
1225 return -EINVAL;
1226 }
1227
1228
1229 if (constraints->min_uV < min_uV) {
1230 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1231 constraints->min_uV, min_uV);
1232 constraints->min_uV = min_uV;
1233 }
1234 if (constraints->max_uV > max_uV) {
1235 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1236 constraints->max_uV, max_uV);
1237 constraints->max_uV = max_uV;
1238 }
1239 }
1240
1241 return 0;
1242}
1243
1244static int machine_constraints_current(struct regulator_dev *rdev,
1245 struct regulation_constraints *constraints)
1246{
1247 const struct regulator_ops *ops = rdev->desc->ops;
1248 int ret;
1249
1250 if (!constraints->min_uA && !constraints->max_uA)
1251 return 0;
1252
1253 if (constraints->min_uA > constraints->max_uA) {
1254 rdev_err(rdev, "Invalid current constraints\n");
1255 return -EINVAL;
1256 }
1257
1258 if (!ops->set_current_limit || !ops->get_current_limit) {
1259 rdev_warn(rdev, "Operation of current configuration missing\n");
1260 return 0;
1261 }
1262
1263
1264 ret = ops->set_current_limit(rdev, constraints->min_uA,
1265 constraints->max_uA);
1266 if (ret < 0) {
1267 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1268 return ret;
1269 }
1270
1271 return 0;
1272}
1273
1274static int _regulator_do_enable(struct regulator_dev *rdev);
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287static int set_machine_constraints(struct regulator_dev *rdev,
1288 const struct regulation_constraints *constraints)
1289{
1290 int ret = 0;
1291 const struct regulator_ops *ops = rdev->desc->ops;
1292
1293 if (constraints)
1294 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1295 GFP_KERNEL);
1296 else
1297 rdev->constraints = kzalloc(sizeof(*constraints),
1298 GFP_KERNEL);
1299 if (!rdev->constraints)
1300 return -ENOMEM;
1301
1302 ret = machine_constraints_voltage(rdev, rdev->constraints);
1303 if (ret != 0)
1304 return ret;
1305
1306 ret = machine_constraints_current(rdev, rdev->constraints);
1307 if (ret != 0)
1308 return ret;
1309
1310 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1311 ret = ops->set_input_current_limit(rdev,
1312 rdev->constraints->ilim_uA);
1313 if (ret < 0) {
1314 rdev_err(rdev, "failed to set input limit\n");
1315 return ret;
1316 }
1317 }
1318
1319
1320 if (rdev->constraints->initial_state) {
1321 ret = suspend_set_state(rdev, rdev->constraints->initial_state);
1322 if (ret < 0) {
1323 rdev_err(rdev, "failed to set suspend state\n");
1324 return ret;
1325 }
1326 }
1327
1328 if (rdev->constraints->initial_mode) {
1329 if (!ops->set_mode) {
1330 rdev_err(rdev, "no set_mode operation\n");
1331 return -EINVAL;
1332 }
1333
1334 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1335 if (ret < 0) {
1336 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1337 return ret;
1338 }
1339 } else if (rdev->constraints->system_load) {
1340
1341
1342
1343
1344 drms_uA_update(rdev);
1345 }
1346
1347 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1348 && ops->set_ramp_delay) {
1349 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1350 if (ret < 0) {
1351 rdev_err(rdev, "failed to set ramp_delay\n");
1352 return ret;
1353 }
1354 }
1355
1356 if (rdev->constraints->pull_down && ops->set_pull_down) {
1357 ret = ops->set_pull_down(rdev);
1358 if (ret < 0) {
1359 rdev_err(rdev, "failed to set pull down\n");
1360 return ret;
1361 }
1362 }
1363
1364 if (rdev->constraints->soft_start && ops->set_soft_start) {
1365 ret = ops->set_soft_start(rdev);
1366 if (ret < 0) {
1367 rdev_err(rdev, "failed to set soft start\n");
1368 return ret;
1369 }
1370 }
1371
1372 if (rdev->constraints->over_current_protection
1373 && ops->set_over_current_protection) {
1374 ret = ops->set_over_current_protection(rdev);
1375 if (ret < 0) {
1376 rdev_err(rdev, "failed to set over current protection\n");
1377 return ret;
1378 }
1379 }
1380
1381 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1382 bool ad_state = (rdev->constraints->active_discharge ==
1383 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1384
1385 ret = ops->set_active_discharge(rdev, ad_state);
1386 if (ret < 0) {
1387 rdev_err(rdev, "failed to set active discharge\n");
1388 return ret;
1389 }
1390 }
1391
1392
1393
1394
1395 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1396 if (rdev->supply) {
1397 ret = regulator_enable(rdev->supply);
1398 if (ret < 0) {
1399 _regulator_put(rdev->supply);
1400 rdev->supply = NULL;
1401 return ret;
1402 }
1403 }
1404
1405 ret = _regulator_do_enable(rdev);
1406 if (ret < 0 && ret != -EINVAL) {
1407 rdev_err(rdev, "failed to enable\n");
1408 return ret;
1409 }
1410
1411 if (rdev->constraints->always_on)
1412 rdev->use_count++;
1413 }
1414
1415 print_constraints(rdev);
1416 return 0;
1417}
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428static int set_supply(struct regulator_dev *rdev,
1429 struct regulator_dev *supply_rdev)
1430{
1431 int err;
1432
1433 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1434
1435 if (!try_module_get(supply_rdev->owner))
1436 return -ENODEV;
1437
1438 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1439 if (rdev->supply == NULL) {
1440 err = -ENOMEM;
1441 return err;
1442 }
1443 supply_rdev->open_count++;
1444
1445 return 0;
1446}
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459static int set_consumer_device_supply(struct regulator_dev *rdev,
1460 const char *consumer_dev_name,
1461 const char *supply)
1462{
1463 struct regulator_map *node;
1464 int has_dev;
1465
1466 if (supply == NULL)
1467 return -EINVAL;
1468
1469 if (consumer_dev_name != NULL)
1470 has_dev = 1;
1471 else
1472 has_dev = 0;
1473
1474 list_for_each_entry(node, ®ulator_map_list, list) {
1475 if (node->dev_name && consumer_dev_name) {
1476 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1477 continue;
1478 } else if (node->dev_name || consumer_dev_name) {
1479 continue;
1480 }
1481
1482 if (strcmp(node->supply, supply) != 0)
1483 continue;
1484
1485 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1486 consumer_dev_name,
1487 dev_name(&node->regulator->dev),
1488 node->regulator->desc->name,
1489 supply,
1490 dev_name(&rdev->dev), rdev_get_name(rdev));
1491 return -EBUSY;
1492 }
1493
1494 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1495 if (node == NULL)
1496 return -ENOMEM;
1497
1498 node->regulator = rdev;
1499 node->supply = supply;
1500
1501 if (has_dev) {
1502 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1503 if (node->dev_name == NULL) {
1504 kfree(node);
1505 return -ENOMEM;
1506 }
1507 }
1508
1509 list_add(&node->list, ®ulator_map_list);
1510 return 0;
1511}
1512
1513static void unset_regulator_supplies(struct regulator_dev *rdev)
1514{
1515 struct regulator_map *node, *n;
1516
1517 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1518 if (rdev == node->regulator) {
1519 list_del(&node->list);
1520 kfree(node->dev_name);
1521 kfree(node);
1522 }
1523 }
1524}
1525
1526#ifdef CONFIG_DEBUG_FS
1527static ssize_t constraint_flags_read_file(struct file *file,
1528 char __user *user_buf,
1529 size_t count, loff_t *ppos)
1530{
1531 const struct regulator *regulator = file->private_data;
1532 const struct regulation_constraints *c = regulator->rdev->constraints;
1533 char *buf;
1534 ssize_t ret;
1535
1536 if (!c)
1537 return 0;
1538
1539 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1540 if (!buf)
1541 return -ENOMEM;
1542
1543 ret = snprintf(buf, PAGE_SIZE,
1544 "always_on: %u\n"
1545 "boot_on: %u\n"
1546 "apply_uV: %u\n"
1547 "ramp_disable: %u\n"
1548 "soft_start: %u\n"
1549 "pull_down: %u\n"
1550 "over_current_protection: %u\n",
1551 c->always_on,
1552 c->boot_on,
1553 c->apply_uV,
1554 c->ramp_disable,
1555 c->soft_start,
1556 c->pull_down,
1557 c->over_current_protection);
1558
1559 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1560 kfree(buf);
1561
1562 return ret;
1563}
1564
1565#endif
1566
1567static const struct file_operations constraint_flags_fops = {
1568#ifdef CONFIG_DEBUG_FS
1569 .open = simple_open,
1570 .read = constraint_flags_read_file,
1571 .llseek = default_llseek,
1572#endif
1573};
1574
1575#define REG_STR_SIZE 64
1576
1577static struct regulator *create_regulator(struct regulator_dev *rdev,
1578 struct device *dev,
1579 const char *supply_name)
1580{
1581 struct regulator *regulator;
1582 char buf[REG_STR_SIZE];
1583 int err, size;
1584
1585 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1586 if (regulator == NULL)
1587 return NULL;
1588
1589 regulator_lock(rdev);
1590 regulator->rdev = rdev;
1591 list_add(®ulator->list, &rdev->consumer_list);
1592
1593 if (dev) {
1594 regulator->dev = dev;
1595
1596
1597 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1598 dev->kobj.name, supply_name);
1599 if (size >= REG_STR_SIZE)
1600 goto overflow_err;
1601
1602 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1603 if (regulator->supply_name == NULL)
1604 goto overflow_err;
1605
1606 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1607 buf);
1608 if (err) {
1609 rdev_dbg(rdev, "could not add device link %s err %d\n",
1610 dev->kobj.name, err);
1611
1612 }
1613 } else {
1614 regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1615 if (regulator->supply_name == NULL)
1616 goto overflow_err;
1617 }
1618
1619 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1620 rdev->debugfs);
1621 if (!regulator->debugfs) {
1622 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1623 } else {
1624 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1625 ®ulator->uA_load);
1626 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1627 ®ulator->voltage[PM_SUSPEND_ON].min_uV);
1628 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1629 ®ulator->voltage[PM_SUSPEND_ON].max_uV);
1630 debugfs_create_file("constraint_flags", 0444,
1631 regulator->debugfs, regulator,
1632 &constraint_flags_fops);
1633 }
1634
1635
1636
1637
1638
1639
1640 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1641 _regulator_is_enabled(rdev))
1642 regulator->always_on = true;
1643
1644 regulator_unlock(rdev);
1645 return regulator;
1646overflow_err:
1647 list_del(®ulator->list);
1648 kfree(regulator);
1649 regulator_unlock(rdev);
1650 return NULL;
1651}
1652
1653static int _regulator_get_enable_time(struct regulator_dev *rdev)
1654{
1655 if (rdev->constraints && rdev->constraints->enable_time)
1656 return rdev->constraints->enable_time;
1657 if (rdev->desc->ops->enable_time)
1658 return rdev->desc->ops->enable_time(rdev);
1659 return rdev->desc->enable_time;
1660}
1661
1662static struct regulator_supply_alias *regulator_find_supply_alias(
1663 struct device *dev, const char *supply)
1664{
1665 struct regulator_supply_alias *map;
1666
1667 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1668 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1669 return map;
1670
1671 return NULL;
1672}
1673
1674static void regulator_supply_alias(struct device **dev, const char **supply)
1675{
1676 struct regulator_supply_alias *map;
1677
1678 map = regulator_find_supply_alias(*dev, *supply);
1679 if (map) {
1680 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1681 *supply, map->alias_supply,
1682 dev_name(map->alias_dev));
1683 *dev = map->alias_dev;
1684 *supply = map->alias_supply;
1685 }
1686}
1687
1688static int regulator_match(struct device *dev, const void *data)
1689{
1690 struct regulator_dev *r = dev_to_rdev(dev);
1691
1692 return strcmp(rdev_get_name(r), data) == 0;
1693}
1694
1695static struct regulator_dev *regulator_lookup_by_name(const char *name)
1696{
1697 struct device *dev;
1698
1699 dev = class_find_device(®ulator_class, NULL, name, regulator_match);
1700
1701 return dev ? dev_to_rdev(dev) : NULL;
1702}
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1717 const char *supply)
1718{
1719 struct regulator_dev *r = NULL;
1720 struct device_node *node;
1721 struct regulator_map *map;
1722 const char *devname = NULL;
1723
1724 regulator_supply_alias(&dev, &supply);
1725
1726
1727 if (dev && dev->of_node) {
1728 node = of_get_regulator(dev, supply);
1729 if (node) {
1730 r = of_find_regulator_by_node(node);
1731 if (r)
1732 return r;
1733
1734
1735
1736
1737
1738 return ERR_PTR(-EPROBE_DEFER);
1739 }
1740 }
1741
1742
1743 if (dev)
1744 devname = dev_name(dev);
1745
1746 mutex_lock(®ulator_list_mutex);
1747 list_for_each_entry(map, ®ulator_map_list, list) {
1748
1749 if (map->dev_name &&
1750 (!devname || strcmp(map->dev_name, devname)))
1751 continue;
1752
1753 if (strcmp(map->supply, supply) == 0 &&
1754 get_device(&map->regulator->dev)) {
1755 r = map->regulator;
1756 break;
1757 }
1758 }
1759 mutex_unlock(®ulator_list_mutex);
1760
1761 if (r)
1762 return r;
1763
1764 r = regulator_lookup_by_name(supply);
1765 if (r)
1766 return r;
1767
1768 return ERR_PTR(-ENODEV);
1769}
1770
1771static int regulator_resolve_supply(struct regulator_dev *rdev)
1772{
1773 struct regulator_dev *r;
1774 struct device *dev = rdev->dev.parent;
1775 int ret;
1776
1777
1778 if (!rdev->supply_name)
1779 return 0;
1780
1781
1782 if (rdev->supply)
1783 return 0;
1784
1785 r = regulator_dev_lookup(dev, rdev->supply_name);
1786 if (IS_ERR(r)) {
1787 ret = PTR_ERR(r);
1788
1789
1790 if (ret == -EPROBE_DEFER)
1791 return ret;
1792
1793 if (have_full_constraints()) {
1794 r = dummy_regulator_rdev;
1795 get_device(&r->dev);
1796 } else {
1797 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1798 rdev->supply_name, rdev->desc->name);
1799 return -EPROBE_DEFER;
1800 }
1801 }
1802
1803
1804
1805
1806
1807
1808
1809 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1810 if (!device_is_bound(r->dev.parent)) {
1811 put_device(&r->dev);
1812 return -EPROBE_DEFER;
1813 }
1814 }
1815
1816
1817 ret = regulator_resolve_supply(r);
1818 if (ret < 0) {
1819 put_device(&r->dev);
1820 return ret;
1821 }
1822
1823 ret = set_supply(rdev, r);
1824 if (ret < 0) {
1825 put_device(&r->dev);
1826 return ret;
1827 }
1828
1829
1830
1831
1832
1833
1834 if (rdev->use_count) {
1835 ret = regulator_enable(rdev->supply);
1836 if (ret < 0) {
1837 _regulator_put(rdev->supply);
1838 rdev->supply = NULL;
1839 return ret;
1840 }
1841 }
1842
1843 return 0;
1844}
1845
1846
1847struct regulator *_regulator_get(struct device *dev, const char *id,
1848 enum regulator_get_type get_type)
1849{
1850 struct regulator_dev *rdev;
1851 struct regulator *regulator;
1852 struct device_link *link;
1853 int ret;
1854
1855 if (get_type >= MAX_GET_TYPE) {
1856 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1857 return ERR_PTR(-EINVAL);
1858 }
1859
1860 if (id == NULL) {
1861 pr_err("get() with no identifier\n");
1862 return ERR_PTR(-EINVAL);
1863 }
1864
1865 rdev = regulator_dev_lookup(dev, id);
1866 if (IS_ERR(rdev)) {
1867 ret = PTR_ERR(rdev);
1868
1869
1870
1871
1872
1873 if (ret != -ENODEV)
1874 return ERR_PTR(ret);
1875
1876 if (!have_full_constraints()) {
1877 dev_warn(dev,
1878 "incomplete constraints, dummy supplies not allowed\n");
1879 return ERR_PTR(-ENODEV);
1880 }
1881
1882 switch (get_type) {
1883 case NORMAL_GET:
1884
1885
1886
1887
1888
1889 dev_warn(dev, "supply %s not found, using dummy regulator\n", id);
1890 rdev = dummy_regulator_rdev;
1891 get_device(&rdev->dev);
1892 break;
1893
1894 case EXCLUSIVE_GET:
1895 dev_warn(dev,
1896 "dummy supplies not allowed for exclusive requests\n");
1897
1898
1899 default:
1900 return ERR_PTR(-ENODEV);
1901 }
1902 }
1903
1904 if (rdev->exclusive) {
1905 regulator = ERR_PTR(-EPERM);
1906 put_device(&rdev->dev);
1907 return regulator;
1908 }
1909
1910 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
1911 regulator = ERR_PTR(-EBUSY);
1912 put_device(&rdev->dev);
1913 return regulator;
1914 }
1915
1916 mutex_lock(®ulator_list_mutex);
1917 ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
1918 mutex_unlock(®ulator_list_mutex);
1919
1920 if (ret != 0) {
1921 regulator = ERR_PTR(-EPROBE_DEFER);
1922 put_device(&rdev->dev);
1923 return regulator;
1924 }
1925
1926 ret = regulator_resolve_supply(rdev);
1927 if (ret < 0) {
1928 regulator = ERR_PTR(ret);
1929 put_device(&rdev->dev);
1930 return regulator;
1931 }
1932
1933 if (!try_module_get(rdev->owner)) {
1934 regulator = ERR_PTR(-EPROBE_DEFER);
1935 put_device(&rdev->dev);
1936 return regulator;
1937 }
1938
1939 regulator = create_regulator(rdev, dev, id);
1940 if (regulator == NULL) {
1941 regulator = ERR_PTR(-ENOMEM);
1942 module_put(rdev->owner);
1943 put_device(&rdev->dev);
1944 return regulator;
1945 }
1946
1947 rdev->open_count++;
1948 if (get_type == EXCLUSIVE_GET) {
1949 rdev->exclusive = 1;
1950
1951 ret = _regulator_is_enabled(rdev);
1952 if (ret > 0)
1953 rdev->use_count = 1;
1954 else
1955 rdev->use_count = 0;
1956 }
1957
1958 link = device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
1959 if (!IS_ERR_OR_NULL(link))
1960 regulator->device_link = true;
1961
1962 return regulator;
1963}
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978struct regulator *regulator_get(struct device *dev, const char *id)
1979{
1980 return _regulator_get(dev, id, NORMAL_GET);
1981}
1982EXPORT_SYMBOL_GPL(regulator_get);
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
2006{
2007 return _regulator_get(dev, id, EXCLUSIVE_GET);
2008}
2009EXPORT_SYMBOL_GPL(regulator_get_exclusive);
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031struct regulator *regulator_get_optional(struct device *dev, const char *id)
2032{
2033 return _regulator_get(dev, id, OPTIONAL_GET);
2034}
2035EXPORT_SYMBOL_GPL(regulator_get_optional);
2036
2037
2038static void _regulator_put(struct regulator *regulator)
2039{
2040 struct regulator_dev *rdev;
2041
2042 if (IS_ERR_OR_NULL(regulator))
2043 return;
2044
2045 lockdep_assert_held_once(®ulator_list_mutex);
2046
2047
2048 WARN_ON(regulator->enable_count);
2049
2050 rdev = regulator->rdev;
2051
2052 debugfs_remove_recursive(regulator->debugfs);
2053
2054 if (regulator->dev) {
2055 if (regulator->device_link)
2056 device_link_remove(regulator->dev, &rdev->dev);
2057
2058
2059 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
2060 }
2061
2062 regulator_lock(rdev);
2063 list_del(®ulator->list);
2064
2065 rdev->open_count--;
2066 rdev->exclusive = 0;
2067 regulator_unlock(rdev);
2068
2069 kfree_const(regulator->supply_name);
2070 kfree(regulator);
2071
2072 module_put(rdev->owner);
2073 put_device(&rdev->dev);
2074}
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084void regulator_put(struct regulator *regulator)
2085{
2086 mutex_lock(®ulator_list_mutex);
2087 _regulator_put(regulator);
2088 mutex_unlock(®ulator_list_mutex);
2089}
2090EXPORT_SYMBOL_GPL(regulator_put);
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104int regulator_register_supply_alias(struct device *dev, const char *id,
2105 struct device *alias_dev,
2106 const char *alias_id)
2107{
2108 struct regulator_supply_alias *map;
2109
2110 map = regulator_find_supply_alias(dev, id);
2111 if (map)
2112 return -EEXIST;
2113
2114 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
2115 if (!map)
2116 return -ENOMEM;
2117
2118 map->src_dev = dev;
2119 map->src_supply = id;
2120 map->alias_dev = alias_dev;
2121 map->alias_supply = alias_id;
2122
2123 list_add(&map->list, ®ulator_supply_alias_list);
2124
2125 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2126 id, dev_name(dev), alias_id, dev_name(alias_dev));
2127
2128 return 0;
2129}
2130EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140void regulator_unregister_supply_alias(struct device *dev, const char *id)
2141{
2142 struct regulator_supply_alias *map;
2143
2144 map = regulator_find_supply_alias(dev, id);
2145 if (map) {
2146 list_del(&map->list);
2147 kfree(map);
2148 }
2149}
2150EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169int regulator_bulk_register_supply_alias(struct device *dev,
2170 const char *const *id,
2171 struct device *alias_dev,
2172 const char *const *alias_id,
2173 int num_id)
2174{
2175 int i;
2176 int ret;
2177
2178 for (i = 0; i < num_id; ++i) {
2179 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2180 alias_id[i]);
2181 if (ret < 0)
2182 goto err;
2183 }
2184
2185 return 0;
2186
2187err:
2188 dev_err(dev,
2189 "Failed to create supply alias %s,%s -> %s,%s\n",
2190 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2191
2192 while (--i >= 0)
2193 regulator_unregister_supply_alias(dev, id[i]);
2194
2195 return ret;
2196}
2197EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209void regulator_bulk_unregister_supply_alias(struct device *dev,
2210 const char *const *id,
2211 int num_id)
2212{
2213 int i;
2214
2215 for (i = 0; i < num_id; ++i)
2216 regulator_unregister_supply_alias(dev, id[i]);
2217}
2218EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2219
2220
2221
2222static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2223 const struct regulator_config *config)
2224{
2225 struct regulator_enable_gpio *pin;
2226 struct gpio_desc *gpiod;
2227
2228 gpiod = config->ena_gpiod;
2229
2230 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
2231 if (pin->gpiod == gpiod) {
2232 rdev_dbg(rdev, "GPIO is already used\n");
2233 goto update_ena_gpio_to_rdev;
2234 }
2235 }
2236
2237 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
2238 if (pin == NULL)
2239 return -ENOMEM;
2240
2241 pin->gpiod = gpiod;
2242 list_add(&pin->list, ®ulator_ena_gpio_list);
2243
2244update_ena_gpio_to_rdev:
2245 pin->request_count++;
2246 rdev->ena_pin = pin;
2247 return 0;
2248}
2249
2250static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2251{
2252 struct regulator_enable_gpio *pin, *n;
2253
2254 if (!rdev->ena_pin)
2255 return;
2256
2257
2258 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
2259 if (pin->gpiod == rdev->ena_pin->gpiod) {
2260 if (pin->request_count <= 1) {
2261 pin->request_count = 0;
2262 gpiod_put(pin->gpiod);
2263 list_del(&pin->list);
2264 kfree(pin);
2265 rdev->ena_pin = NULL;
2266 return;
2267 } else {
2268 pin->request_count--;
2269 }
2270 }
2271 }
2272}
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2283{
2284 struct regulator_enable_gpio *pin = rdev->ena_pin;
2285
2286 if (!pin)
2287 return -EINVAL;
2288
2289 if (enable) {
2290
2291 if (pin->enable_count == 0)
2292 gpiod_set_value_cansleep(pin->gpiod, 1);
2293
2294 pin->enable_count++;
2295 } else {
2296 if (pin->enable_count > 1) {
2297 pin->enable_count--;
2298 return 0;
2299 }
2300
2301
2302 if (pin->enable_count <= 1) {
2303 gpiod_set_value_cansleep(pin->gpiod, 0);
2304 pin->enable_count = 0;
2305 }
2306 }
2307
2308 return 0;
2309}
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322static void _regulator_enable_delay(unsigned int delay)
2323{
2324 unsigned int ms = delay / 1000;
2325 unsigned int us = delay % 1000;
2326
2327 if (ms > 0) {
2328
2329
2330
2331
2332 if (ms < 20)
2333 us += ms * 1000;
2334 else
2335 msleep(ms);
2336 }
2337
2338
2339
2340
2341
2342
2343
2344 if (us >= 10)
2345 usleep_range(us, us + 100);
2346 else
2347 udelay(us);
2348}
2349
2350static int _regulator_do_enable(struct regulator_dev *rdev)
2351{
2352 int ret, delay;
2353
2354
2355 ret = _regulator_get_enable_time(rdev);
2356 if (ret >= 0) {
2357 delay = ret;
2358 } else {
2359 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2360 delay = 0;
2361 }
2362
2363 trace_regulator_enable(rdev_get_name(rdev));
2364
2365 if (rdev->desc->off_on_delay) {
2366
2367
2368
2369 unsigned long start_jiffy = jiffies;
2370 unsigned long intended, max_delay, remaining;
2371
2372 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2373 intended = rdev->last_off_jiffy + max_delay;
2374
2375 if (time_before(start_jiffy, intended)) {
2376
2377
2378
2379
2380
2381
2382
2383 remaining = intended - start_jiffy;
2384 if (remaining <= max_delay)
2385 _regulator_enable_delay(
2386 jiffies_to_usecs(remaining));
2387 }
2388 }
2389
2390 if (rdev->ena_pin) {
2391 if (!rdev->ena_gpio_state) {
2392 ret = regulator_ena_gpio_ctrl(rdev, true);
2393 if (ret < 0)
2394 return ret;
2395 rdev->ena_gpio_state = 1;
2396 }
2397 } else if (rdev->desc->ops->enable) {
2398 ret = rdev->desc->ops->enable(rdev);
2399 if (ret < 0)
2400 return ret;
2401 } else {
2402 return -EINVAL;
2403 }
2404
2405
2406
2407
2408 trace_regulator_enable_delay(rdev_get_name(rdev));
2409
2410 _regulator_enable_delay(delay);
2411
2412 trace_regulator_enable_complete(rdev_get_name(rdev));
2413
2414 return 0;
2415}
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437static int _regulator_handle_consumer_enable(struct regulator *regulator)
2438{
2439 struct regulator_dev *rdev = regulator->rdev;
2440
2441 lockdep_assert_held_once(&rdev->mutex.base);
2442
2443 regulator->enable_count++;
2444 if (regulator->uA_load && regulator->enable_count == 1)
2445 return drms_uA_update(rdev);
2446
2447 return 0;
2448}
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458static int _regulator_handle_consumer_disable(struct regulator *regulator)
2459{
2460 struct regulator_dev *rdev = regulator->rdev;
2461
2462 lockdep_assert_held_once(&rdev->mutex.base);
2463
2464 if (!regulator->enable_count) {
2465 rdev_err(rdev, "Underflow of regulator enable count\n");
2466 return -EINVAL;
2467 }
2468
2469 regulator->enable_count--;
2470 if (regulator->uA_load && regulator->enable_count == 0)
2471 return drms_uA_update(rdev);
2472
2473 return 0;
2474}
2475
2476
2477static int _regulator_enable(struct regulator *regulator)
2478{
2479 struct regulator_dev *rdev = regulator->rdev;
2480 int ret;
2481
2482 lockdep_assert_held_once(&rdev->mutex.base);
2483
2484 if (rdev->use_count == 0 && rdev->supply) {
2485 ret = _regulator_enable(rdev->supply);
2486 if (ret < 0)
2487 return ret;
2488 }
2489
2490
2491 if (rdev->coupling_desc.n_coupled > 1) {
2492 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2493 if (ret < 0)
2494 goto err_disable_supply;
2495 }
2496
2497 ret = _regulator_handle_consumer_enable(regulator);
2498 if (ret < 0)
2499 goto err_disable_supply;
2500
2501 if (rdev->use_count == 0) {
2502
2503 ret = _regulator_is_enabled(rdev);
2504 if (ret == -EINVAL || ret == 0) {
2505 if (!regulator_ops_is_valid(rdev,
2506 REGULATOR_CHANGE_STATUS)) {
2507 ret = -EPERM;
2508 goto err_consumer_disable;
2509 }
2510
2511 ret = _regulator_do_enable(rdev);
2512 if (ret < 0)
2513 goto err_consumer_disable;
2514
2515 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2516 NULL);
2517 } else if (ret < 0) {
2518 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2519 goto err_consumer_disable;
2520 }
2521
2522 }
2523
2524 rdev->use_count++;
2525
2526 return 0;
2527
2528err_consumer_disable:
2529 _regulator_handle_consumer_disable(regulator);
2530
2531err_disable_supply:
2532 if (rdev->use_count == 0 && rdev->supply)
2533 _regulator_disable(rdev->supply);
2534
2535 return ret;
2536}
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549int regulator_enable(struct regulator *regulator)
2550{
2551 struct regulator_dev *rdev = regulator->rdev;
2552 struct ww_acquire_ctx ww_ctx;
2553 int ret;
2554
2555 regulator_lock_dependent(rdev, &ww_ctx);
2556 ret = _regulator_enable(regulator);
2557 regulator_unlock_dependent(rdev, &ww_ctx);
2558
2559 return ret;
2560}
2561EXPORT_SYMBOL_GPL(regulator_enable);
2562
2563static int _regulator_do_disable(struct regulator_dev *rdev)
2564{
2565 int ret;
2566
2567 trace_regulator_disable(rdev_get_name(rdev));
2568
2569 if (rdev->ena_pin) {
2570 if (rdev->ena_gpio_state) {
2571 ret = regulator_ena_gpio_ctrl(rdev, false);
2572 if (ret < 0)
2573 return ret;
2574 rdev->ena_gpio_state = 0;
2575 }
2576
2577 } else if (rdev->desc->ops->disable) {
2578 ret = rdev->desc->ops->disable(rdev);
2579 if (ret != 0)
2580 return ret;
2581 }
2582
2583
2584
2585
2586 if (rdev->desc->off_on_delay)
2587 rdev->last_off_jiffy = jiffies;
2588
2589 trace_regulator_disable_complete(rdev_get_name(rdev));
2590
2591 return 0;
2592}
2593
2594
2595static int _regulator_disable(struct regulator *regulator)
2596{
2597 struct regulator_dev *rdev = regulator->rdev;
2598 int ret = 0;
2599
2600 lockdep_assert_held_once(&rdev->mutex.base);
2601
2602 if (WARN(rdev->use_count <= 0,
2603 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2604 return -EIO;
2605
2606
2607 if (rdev->use_count == 1 &&
2608 (rdev->constraints && !rdev->constraints->always_on)) {
2609
2610
2611 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2612 ret = _notifier_call_chain(rdev,
2613 REGULATOR_EVENT_PRE_DISABLE,
2614 NULL);
2615 if (ret & NOTIFY_STOP_MASK)
2616 return -EINVAL;
2617
2618 ret = _regulator_do_disable(rdev);
2619 if (ret < 0) {
2620 rdev_err(rdev, "failed to disable\n");
2621 _notifier_call_chain(rdev,
2622 REGULATOR_EVENT_ABORT_DISABLE,
2623 NULL);
2624 return ret;
2625 }
2626 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2627 NULL);
2628 }
2629
2630 rdev->use_count = 0;
2631 } else if (rdev->use_count > 1) {
2632 rdev->use_count--;
2633 }
2634
2635 if (ret == 0)
2636 ret = _regulator_handle_consumer_disable(regulator);
2637
2638 if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
2639 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2640
2641 if (ret == 0 && rdev->use_count == 0 && rdev->supply)
2642 ret = _regulator_disable(rdev->supply);
2643
2644 return ret;
2645}
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659int regulator_disable(struct regulator *regulator)
2660{
2661 struct regulator_dev *rdev = regulator->rdev;
2662 struct ww_acquire_ctx ww_ctx;
2663 int ret;
2664
2665 regulator_lock_dependent(rdev, &ww_ctx);
2666 ret = _regulator_disable(regulator);
2667 regulator_unlock_dependent(rdev, &ww_ctx);
2668
2669 return ret;
2670}
2671EXPORT_SYMBOL_GPL(regulator_disable);
2672
2673
2674static int _regulator_force_disable(struct regulator_dev *rdev)
2675{
2676 int ret = 0;
2677
2678 lockdep_assert_held_once(&rdev->mutex.base);
2679
2680 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2681 REGULATOR_EVENT_PRE_DISABLE, NULL);
2682 if (ret & NOTIFY_STOP_MASK)
2683 return -EINVAL;
2684
2685 ret = _regulator_do_disable(rdev);
2686 if (ret < 0) {
2687 rdev_err(rdev, "failed to force disable\n");
2688 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2689 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2690 return ret;
2691 }
2692
2693 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2694 REGULATOR_EVENT_DISABLE, NULL);
2695
2696 return 0;
2697}
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708int regulator_force_disable(struct regulator *regulator)
2709{
2710 struct regulator_dev *rdev = regulator->rdev;
2711 struct ww_acquire_ctx ww_ctx;
2712 int ret;
2713
2714 regulator_lock_dependent(rdev, &ww_ctx);
2715
2716 ret = _regulator_force_disable(regulator->rdev);
2717
2718 if (rdev->coupling_desc.n_coupled > 1)
2719 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2720
2721 if (regulator->uA_load) {
2722 regulator->uA_load = 0;
2723 ret = drms_uA_update(rdev);
2724 }
2725
2726 if (rdev->use_count != 0 && rdev->supply)
2727 _regulator_disable(rdev->supply);
2728
2729 regulator_unlock_dependent(rdev, &ww_ctx);
2730
2731 return ret;
2732}
2733EXPORT_SYMBOL_GPL(regulator_force_disable);
2734
2735static void regulator_disable_work(struct work_struct *work)
2736{
2737 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2738 disable_work.work);
2739 struct ww_acquire_ctx ww_ctx;
2740 int count, i, ret;
2741 struct regulator *regulator;
2742 int total_count = 0;
2743
2744 regulator_lock_dependent(rdev, &ww_ctx);
2745
2746
2747
2748
2749
2750
2751
2752 cancel_delayed_work(&rdev->disable_work);
2753
2754 list_for_each_entry(regulator, &rdev->consumer_list, list) {
2755 count = regulator->deferred_disables;
2756
2757 if (!count)
2758 continue;
2759
2760 total_count += count;
2761 regulator->deferred_disables = 0;
2762
2763 for (i = 0; i < count; i++) {
2764 ret = _regulator_disable(regulator);
2765 if (ret != 0)
2766 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2767 }
2768 }
2769 WARN_ON(!total_count);
2770
2771 if (rdev->coupling_desc.n_coupled > 1)
2772 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2773
2774 regulator_unlock_dependent(rdev, &ww_ctx);
2775}
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789int regulator_disable_deferred(struct regulator *regulator, int ms)
2790{
2791 struct regulator_dev *rdev = regulator->rdev;
2792
2793 if (!ms)
2794 return regulator_disable(regulator);
2795
2796 regulator_lock(rdev);
2797 regulator->deferred_disables++;
2798 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2799 msecs_to_jiffies(ms));
2800 regulator_unlock(rdev);
2801
2802 return 0;
2803}
2804EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2805
2806static int _regulator_is_enabled(struct regulator_dev *rdev)
2807{
2808
2809 if (rdev->ena_pin)
2810 return rdev->ena_gpio_state;
2811
2812
2813 if (!rdev->desc->ops->is_enabled)
2814 return 1;
2815
2816 return rdev->desc->ops->is_enabled(rdev);
2817}
2818
2819static int _regulator_list_voltage(struct regulator_dev *rdev,
2820 unsigned selector, int lock)
2821{
2822 const struct regulator_ops *ops = rdev->desc->ops;
2823 int ret;
2824
2825 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2826 return rdev->desc->fixed_uV;
2827
2828 if (ops->list_voltage) {
2829 if (selector >= rdev->desc->n_voltages)
2830 return -EINVAL;
2831 if (lock)
2832 regulator_lock(rdev);
2833 ret = ops->list_voltage(rdev, selector);
2834 if (lock)
2835 regulator_unlock(rdev);
2836 } else if (rdev->is_switch && rdev->supply) {
2837 ret = _regulator_list_voltage(rdev->supply->rdev,
2838 selector, lock);
2839 } else {
2840 return -EINVAL;
2841 }
2842
2843 if (ret > 0) {
2844 if (ret < rdev->constraints->min_uV)
2845 ret = 0;
2846 else if (ret > rdev->constraints->max_uV)
2847 ret = 0;
2848 }
2849
2850 return ret;
2851}
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865int regulator_is_enabled(struct regulator *regulator)
2866{
2867 int ret;
2868
2869 if (regulator->always_on)
2870 return 1;
2871
2872 regulator_lock(regulator->rdev);
2873 ret = _regulator_is_enabled(regulator->rdev);
2874 regulator_unlock(regulator->rdev);
2875
2876 return ret;
2877}
2878EXPORT_SYMBOL_GPL(regulator_is_enabled);
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888int regulator_count_voltages(struct regulator *regulator)
2889{
2890 struct regulator_dev *rdev = regulator->rdev;
2891
2892 if (rdev->desc->n_voltages)
2893 return rdev->desc->n_voltages;
2894
2895 if (!rdev->is_switch || !rdev->supply)
2896 return -EINVAL;
2897
2898 return regulator_count_voltages(rdev->supply);
2899}
2900EXPORT_SYMBOL_GPL(regulator_count_voltages);
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2913{
2914 return _regulator_list_voltage(regulator->rdev, selector, 1);
2915}
2916EXPORT_SYMBOL_GPL(regulator_list_voltage);
2917
2918
2919
2920
2921
2922
2923
2924
2925struct regmap *regulator_get_regmap(struct regulator *regulator)
2926{
2927 struct regmap *map = regulator->rdev->regmap;
2928
2929 return map ? map : ERR_PTR(-EOPNOTSUPP);
2930}
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946int regulator_get_hardware_vsel_register(struct regulator *regulator,
2947 unsigned *vsel_reg,
2948 unsigned *vsel_mask)
2949{
2950 struct regulator_dev *rdev = regulator->rdev;
2951 const struct regulator_ops *ops = rdev->desc->ops;
2952
2953 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2954 return -EOPNOTSUPP;
2955
2956 *vsel_reg = rdev->desc->vsel_reg;
2957 *vsel_mask = rdev->desc->vsel_mask;
2958
2959 return 0;
2960}
2961EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974int regulator_list_hardware_vsel(struct regulator *regulator,
2975 unsigned selector)
2976{
2977 struct regulator_dev *rdev = regulator->rdev;
2978 const struct regulator_ops *ops = rdev->desc->ops;
2979
2980 if (selector >= rdev->desc->n_voltages)
2981 return -EINVAL;
2982 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2983 return -EOPNOTSUPP;
2984
2985 return selector;
2986}
2987EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2988
2989
2990
2991
2992
2993
2994
2995
2996unsigned int regulator_get_linear_step(struct regulator *regulator)
2997{
2998 struct regulator_dev *rdev = regulator->rdev;
2999
3000 return rdev->desc->uV_step;
3001}
3002EXPORT_SYMBOL_GPL(regulator_get_linear_step);
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013int regulator_is_supported_voltage(struct regulator *regulator,
3014 int min_uV, int max_uV)
3015{
3016 struct regulator_dev *rdev = regulator->rdev;
3017 int i, voltages, ret;
3018
3019
3020 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3021 ret = regulator_get_voltage(regulator);
3022 if (ret >= 0)
3023 return min_uV <= ret && ret <= max_uV;
3024 else
3025 return ret;
3026 }
3027
3028
3029 if (rdev->desc->continuous_voltage_range)
3030 return min_uV >= rdev->constraints->min_uV &&
3031 max_uV <= rdev->constraints->max_uV;
3032
3033 ret = regulator_count_voltages(regulator);
3034 if (ret < 0)
3035 return 0;
3036 voltages = ret;
3037
3038 for (i = 0; i < voltages; i++) {
3039 ret = regulator_list_voltage(regulator, i);
3040
3041 if (ret >= min_uV && ret <= max_uV)
3042 return 1;
3043 }
3044
3045 return 0;
3046}
3047EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
3048
3049static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
3050 int max_uV)
3051{
3052 const struct regulator_desc *desc = rdev->desc;
3053
3054 if (desc->ops->map_voltage)
3055 return desc->ops->map_voltage(rdev, min_uV, max_uV);
3056
3057 if (desc->ops->list_voltage == regulator_list_voltage_linear)
3058 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
3059
3060 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
3061 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
3062
3063 if (desc->ops->list_voltage ==
3064 regulator_list_voltage_pickable_linear_range)
3065 return regulator_map_voltage_pickable_linear_range(rdev,
3066 min_uV, max_uV);
3067
3068 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
3069}
3070
3071static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3072 int min_uV, int max_uV,
3073 unsigned *selector)
3074{
3075 struct pre_voltage_change_data data;
3076 int ret;
3077
3078 data.old_uV = regulator_get_voltage_rdev(rdev);
3079 data.min_uV = min_uV;
3080 data.max_uV = max_uV;
3081 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3082 &data);
3083 if (ret & NOTIFY_STOP_MASK)
3084 return -EINVAL;
3085
3086 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
3087 if (ret >= 0)
3088 return ret;
3089
3090 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3091 (void *)data.old_uV);
3092
3093 return ret;
3094}
3095
3096static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3097 int uV, unsigned selector)
3098{
3099 struct pre_voltage_change_data data;
3100 int ret;
3101
3102 data.old_uV = regulator_get_voltage_rdev(rdev);
3103 data.min_uV = uV;
3104 data.max_uV = uV;
3105 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3106 &data);
3107 if (ret & NOTIFY_STOP_MASK)
3108 return -EINVAL;
3109
3110 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
3111 if (ret >= 0)
3112 return ret;
3113
3114 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3115 (void *)data.old_uV);
3116
3117 return ret;
3118}
3119
3120static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev,
3121 int uV, int new_selector)
3122{
3123 const struct regulator_ops *ops = rdev->desc->ops;
3124 int diff, old_sel, curr_sel, ret;
3125
3126
3127 if (!_regulator_is_enabled(rdev))
3128 goto final_set;
3129
3130 if (!ops->get_voltage_sel)
3131 return -EINVAL;
3132
3133 old_sel = ops->get_voltage_sel(rdev);
3134 if (old_sel < 0)
3135 return old_sel;
3136
3137 diff = new_selector - old_sel;
3138 if (diff == 0)
3139 return 0;
3140
3141 if (diff > 0) {
3142
3143 for (curr_sel = old_sel + rdev->desc->vsel_step;
3144 curr_sel < new_selector;
3145 curr_sel += rdev->desc->vsel_step) {
3146
3147
3148
3149
3150
3151
3152 ret = ops->set_voltage_sel(rdev, curr_sel);
3153 if (ret)
3154 goto try_revert;
3155 }
3156 } else {
3157
3158 for (curr_sel = old_sel - rdev->desc->vsel_step;
3159 curr_sel > new_selector;
3160 curr_sel -= rdev->desc->vsel_step) {
3161 ret = ops->set_voltage_sel(rdev, curr_sel);
3162 if (ret)
3163 goto try_revert;
3164 }
3165 }
3166
3167final_set:
3168
3169 return _regulator_call_set_voltage_sel(rdev, uV, new_selector);
3170
3171try_revert:
3172
3173
3174
3175
3176 (void)ops->set_voltage_sel(rdev, old_sel);
3177 return ret;
3178}
3179
3180static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3181 int old_uV, int new_uV)
3182{
3183 unsigned int ramp_delay = 0;
3184
3185 if (rdev->constraints->ramp_delay)
3186 ramp_delay = rdev->constraints->ramp_delay;
3187 else if (rdev->desc->ramp_delay)
3188 ramp_delay = rdev->desc->ramp_delay;
3189 else if (rdev->constraints->settling_time)
3190 return rdev->constraints->settling_time;
3191 else if (rdev->constraints->settling_time_up &&
3192 (new_uV > old_uV))
3193 return rdev->constraints->settling_time_up;
3194 else if (rdev->constraints->settling_time_down &&
3195 (new_uV < old_uV))
3196 return rdev->constraints->settling_time_down;
3197
3198 if (ramp_delay == 0) {
3199 rdev_dbg(rdev, "ramp_delay not set\n");
3200 return 0;
3201 }
3202
3203 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
3204}
3205
3206static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3207 int min_uV, int max_uV)
3208{
3209 int ret;
3210 int delay = 0;
3211 int best_val = 0;
3212 unsigned int selector;
3213 int old_selector = -1;
3214 const struct regulator_ops *ops = rdev->desc->ops;
3215 int old_uV = regulator_get_voltage_rdev(rdev);
3216
3217 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3218
3219 min_uV += rdev->constraints->uV_offset;
3220 max_uV += rdev->constraints->uV_offset;
3221
3222
3223
3224
3225
3226 if (_regulator_is_enabled(rdev) &&
3227 ops->set_voltage_time_sel && ops->get_voltage_sel) {
3228 old_selector = ops->get_voltage_sel(rdev);
3229 if (old_selector < 0)
3230 return old_selector;
3231 }
3232
3233 if (ops->set_voltage) {
3234 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
3235 &selector);
3236
3237 if (ret >= 0) {
3238 if (ops->list_voltage)
3239 best_val = ops->list_voltage(rdev,
3240 selector);
3241 else
3242 best_val = regulator_get_voltage_rdev(rdev);
3243 }
3244
3245 } else if (ops->set_voltage_sel) {
3246 ret = regulator_map_voltage(rdev, min_uV, max_uV);
3247 if (ret >= 0) {
3248 best_val = ops->list_voltage(rdev, ret);
3249 if (min_uV <= best_val && max_uV >= best_val) {
3250 selector = ret;
3251 if (old_selector == selector)
3252 ret = 0;
3253 else if (rdev->desc->vsel_step)
3254 ret = _regulator_set_voltage_sel_step(
3255 rdev, best_val, selector);
3256 else
3257 ret = _regulator_call_set_voltage_sel(
3258 rdev, best_val, selector);
3259 } else {
3260 ret = -EINVAL;
3261 }
3262 }
3263 } else {
3264 ret = -EINVAL;
3265 }
3266
3267 if (ret)
3268 goto out;
3269
3270 if (ops->set_voltage_time_sel) {
3271
3272
3273
3274
3275 if (old_selector >= 0 && old_selector != selector)
3276 delay = ops->set_voltage_time_sel(rdev, old_selector,
3277 selector);
3278 } else {
3279 if (old_uV != best_val) {
3280 if (ops->set_voltage_time)
3281 delay = ops->set_voltage_time(rdev, old_uV,
3282 best_val);
3283 else
3284 delay = _regulator_set_voltage_time(rdev,
3285 old_uV,
3286 best_val);
3287 }
3288 }
3289
3290 if (delay < 0) {
3291 rdev_warn(rdev, "failed to get delay: %d\n", delay);
3292 delay = 0;
3293 }
3294
3295
3296 if (delay >= 1000) {
3297 mdelay(delay / 1000);
3298 udelay(delay % 1000);
3299 } else if (delay) {
3300 udelay(delay);
3301 }
3302
3303 if (best_val >= 0) {
3304 unsigned long data = best_val;
3305
3306 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3307 (void *)data);
3308 }
3309
3310out:
3311 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3312
3313 return ret;
3314}
3315
3316static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3317 int min_uV, int max_uV, suspend_state_t state)
3318{
3319 struct regulator_state *rstate;
3320 int uV, sel;
3321
3322 rstate = regulator_get_suspend_state(rdev, state);
3323 if (rstate == NULL)
3324 return -EINVAL;
3325
3326 if (min_uV < rstate->min_uV)
3327 min_uV = rstate->min_uV;
3328 if (max_uV > rstate->max_uV)
3329 max_uV = rstate->max_uV;
3330
3331 sel = regulator_map_voltage(rdev, min_uV, max_uV);
3332 if (sel < 0)
3333 return sel;
3334
3335 uV = rdev->desc->ops->list_voltage(rdev, sel);
3336 if (uV >= min_uV && uV <= max_uV)
3337 rstate->uV = uV;
3338
3339 return 0;
3340}
3341
3342static int regulator_set_voltage_unlocked(struct regulator *regulator,
3343 int min_uV, int max_uV,
3344 suspend_state_t state)
3345{
3346 struct regulator_dev *rdev = regulator->rdev;
3347 struct regulator_voltage *voltage = ®ulator->voltage[state];
3348 int ret = 0;
3349 int old_min_uV, old_max_uV;
3350 int current_uV;
3351
3352
3353
3354
3355
3356 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3357 goto out;
3358
3359
3360
3361
3362
3363 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3364 current_uV = regulator_get_voltage_rdev(rdev);
3365 if (min_uV <= current_uV && current_uV <= max_uV) {
3366 voltage->min_uV = min_uV;
3367 voltage->max_uV = max_uV;
3368 goto out;
3369 }
3370 }
3371
3372
3373 if (!rdev->desc->ops->set_voltage &&
3374 !rdev->desc->ops->set_voltage_sel) {
3375 ret = -EINVAL;
3376 goto out;
3377 }
3378
3379
3380 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3381 if (ret < 0)
3382 goto out;
3383
3384
3385 old_min_uV = voltage->min_uV;
3386 old_max_uV = voltage->max_uV;
3387 voltage->min_uV = min_uV;
3388 voltage->max_uV = max_uV;
3389
3390
3391 ret = regulator_balance_voltage(rdev, state);
3392 if (ret < 0) {
3393 voltage->min_uV = old_min_uV;
3394 voltage->max_uV = old_max_uV;
3395 }
3396
3397out:
3398 return ret;
3399}
3400
3401int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3402 int max_uV, suspend_state_t state)
3403{
3404 int best_supply_uV = 0;
3405 int supply_change_uV = 0;
3406 int ret;
3407
3408 if (rdev->supply &&
3409 regulator_ops_is_valid(rdev->supply->rdev,
3410 REGULATOR_CHANGE_VOLTAGE) &&
3411 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3412 rdev->desc->ops->get_voltage_sel))) {
3413 int current_supply_uV;
3414 int selector;
3415
3416 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3417 if (selector < 0) {
3418 ret = selector;
3419 goto out;
3420 }
3421
3422 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3423 if (best_supply_uV < 0) {
3424 ret = best_supply_uV;
3425 goto out;
3426 }
3427
3428 best_supply_uV += rdev->desc->min_dropout_uV;
3429
3430 current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
3431 if (current_supply_uV < 0) {
3432 ret = current_supply_uV;
3433 goto out;
3434 }
3435
3436 supply_change_uV = best_supply_uV - current_supply_uV;
3437 }
3438
3439 if (supply_change_uV > 0) {
3440 ret = regulator_set_voltage_unlocked(rdev->supply,
3441 best_supply_uV, INT_MAX, state);
3442 if (ret) {
3443 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3444 ret);
3445 goto out;
3446 }
3447 }
3448
3449 if (state == PM_SUSPEND_ON)
3450 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3451 else
3452 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3453 max_uV, state);
3454 if (ret < 0)
3455 goto out;
3456
3457 if (supply_change_uV < 0) {
3458 ret = regulator_set_voltage_unlocked(rdev->supply,
3459 best_supply_uV, INT_MAX, state);
3460 if (ret)
3461 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3462 ret);
3463
3464 ret = 0;
3465 }
3466
3467out:
3468 return ret;
3469}
3470EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev);
3471
3472static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3473 int *current_uV, int *min_uV)
3474{
3475 struct regulation_constraints *constraints = rdev->constraints;
3476
3477
3478 if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3479 return 1;
3480
3481 if (*current_uV < 0) {
3482 *current_uV = regulator_get_voltage_rdev(rdev);
3483
3484 if (*current_uV < 0)
3485 return *current_uV;
3486 }
3487
3488 if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
3489 return 1;
3490
3491
3492 if (*current_uV < *min_uV)
3493 *min_uV = min(*current_uV + constraints->max_uV_step,
3494 *min_uV);
3495 else
3496 *min_uV = max(*current_uV - constraints->max_uV_step,
3497 *min_uV);
3498
3499 return 0;
3500}
3501
3502static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3503 int *current_uV,
3504 int *min_uV, int *max_uV,
3505 suspend_state_t state,
3506 int n_coupled)
3507{
3508 struct coupling_desc *c_desc = &rdev->coupling_desc;
3509 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3510 struct regulation_constraints *constraints = rdev->constraints;
3511 int desired_min_uV = 0, desired_max_uV = INT_MAX;
3512 int max_current_uV = 0, min_current_uV = INT_MAX;
3513 int highest_min_uV = 0, target_uV, possible_uV;
3514 int i, ret, max_spread;
3515 bool done;
3516
3517 *current_uV = -1;
3518
3519
3520
3521
3522
3523 if (n_coupled == 1) {
3524
3525
3526
3527
3528 desired_min_uV = constraints->min_uV;
3529 desired_max_uV = constraints->max_uV;
3530
3531 ret = regulator_check_consumers(rdev,
3532 &desired_min_uV,
3533 &desired_max_uV, state);
3534 if (ret < 0)
3535 return ret;
3536
3537 possible_uV = desired_min_uV;
3538 done = true;
3539
3540 goto finish;
3541 }
3542
3543
3544 for (i = 0; i < n_coupled; i++) {
3545 int tmp_min = 0;
3546 int tmp_max = INT_MAX;
3547
3548 lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
3549
3550 ret = regulator_check_consumers(c_rdevs[i],
3551 &tmp_min,
3552 &tmp_max, state);
3553 if (ret < 0)
3554 return ret;
3555
3556 ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3557 if (ret < 0)
3558 return ret;
3559
3560 highest_min_uV = max(highest_min_uV, tmp_min);
3561
3562 if (i == 0) {
3563 desired_min_uV = tmp_min;
3564 desired_max_uV = tmp_max;
3565 }
3566 }
3567
3568 max_spread = constraints->max_spread[0];
3569
3570
3571
3572
3573
3574
3575 target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3576
3577
3578
3579
3580
3581 for (i = 1; i < n_coupled; i++) {
3582 int tmp_act;
3583
3584 if (!_regulator_is_enabled(c_rdevs[i]))
3585 continue;
3586
3587 tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
3588 if (tmp_act < 0)
3589 return tmp_act;
3590
3591 min_current_uV = min(tmp_act, min_current_uV);
3592 max_current_uV = max(tmp_act, max_current_uV);
3593 }
3594
3595
3596 if (max_current_uV == 0) {
3597 possible_uV = target_uV;
3598 } else {
3599
3600
3601
3602
3603 possible_uV = max(target_uV, max_current_uV - max_spread);
3604 possible_uV = min(possible_uV, min_current_uV + max_spread);
3605 }
3606
3607 if (possible_uV > desired_max_uV)
3608 return -EINVAL;
3609
3610 done = (possible_uV == target_uV);
3611 desired_min_uV = possible_uV;
3612
3613finish:
3614
3615 if (state == PM_SUSPEND_ON) {
3616 ret = regulator_limit_voltage_step(rdev, current_uV,
3617 &desired_min_uV);
3618 if (ret < 0)
3619 return ret;
3620
3621 if (ret == 0)
3622 done = false;
3623 }
3624
3625
3626 if (n_coupled > 1 && *current_uV == -1) {
3627
3628 if (_regulator_is_enabled(rdev)) {
3629 ret = regulator_get_voltage_rdev(rdev);
3630 if (ret < 0)
3631 return ret;
3632
3633 *current_uV = ret;
3634 } else {
3635 *current_uV = desired_min_uV;
3636 }
3637 }
3638
3639 *min_uV = desired_min_uV;
3640 *max_uV = desired_max_uV;
3641
3642 return done;
3643}
3644
3645static int regulator_balance_voltage(struct regulator_dev *rdev,
3646 suspend_state_t state)
3647{
3648 struct regulator_dev **c_rdevs;
3649 struct regulator_dev *best_rdev;
3650 struct coupling_desc *c_desc = &rdev->coupling_desc;
3651 struct regulator_coupler *coupler = c_desc->coupler;
3652 int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
3653 unsigned int delta, best_delta;
3654 unsigned long c_rdev_done = 0;
3655 bool best_c_rdev_done;
3656
3657 c_rdevs = c_desc->coupled_rdevs;
3658 n_coupled = c_desc->n_coupled;
3659
3660
3661
3662
3663
3664 if (state != PM_SUSPEND_ON)
3665 n_coupled = 1;
3666
3667 if (c_desc->n_resolved < n_coupled) {
3668 rdev_err(rdev, "Not all coupled regulators registered\n");
3669 return -EPERM;
3670 }
3671
3672
3673 if (coupler && coupler->balance_voltage)
3674 return coupler->balance_voltage(coupler, rdev, state);
3675
3676
3677
3678
3679
3680 do {
3681 best_c_rdev_done = false;
3682 best_delta = 0;
3683 best_min_uV = 0;
3684 best_max_uV = 0;
3685 best_c_rdev = 0;
3686 best_rdev = NULL;
3687
3688
3689
3690
3691
3692 for (i = 0; i < n_coupled; i++) {
3693
3694
3695
3696
3697
3698
3699 int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
3700
3701 if (test_bit(i, &c_rdev_done))
3702 continue;
3703
3704 ret = regulator_get_optimal_voltage(c_rdevs[i],
3705 ¤t_uV,
3706 &optimal_uV,
3707 &optimal_max_uV,
3708 state, n_coupled);
3709 if (ret < 0)
3710 goto out;
3711
3712 delta = abs(optimal_uV - current_uV);
3713
3714 if (delta && best_delta <= delta) {
3715 best_c_rdev_done = ret;
3716 best_delta = delta;
3717 best_rdev = c_rdevs[i];
3718 best_min_uV = optimal_uV;
3719 best_max_uV = optimal_max_uV;
3720 best_c_rdev = i;
3721 }
3722 }
3723
3724
3725 if (!best_rdev) {
3726 ret = 0;
3727 goto out;
3728 }
3729
3730 ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
3731 best_max_uV, state);
3732
3733 if (ret < 0)
3734 goto out;
3735
3736 if (best_c_rdev_done)
3737 set_bit(best_c_rdev, &c_rdev_done);
3738
3739 } while (n_coupled > 1);
3740
3741out:
3742 return ret;
3743}
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3764{
3765 struct ww_acquire_ctx ww_ctx;
3766 int ret;
3767
3768 regulator_lock_dependent(regulator->rdev, &ww_ctx);
3769
3770 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3771 PM_SUSPEND_ON);
3772
3773 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3774
3775 return ret;
3776}
3777EXPORT_SYMBOL_GPL(regulator_set_voltage);
3778
3779static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3780 suspend_state_t state, bool en)
3781{
3782 struct regulator_state *rstate;
3783
3784 rstate = regulator_get_suspend_state(rdev, state);
3785 if (rstate == NULL)
3786 return -EINVAL;
3787
3788 if (!rstate->changeable)
3789 return -EPERM;
3790
3791 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
3792
3793 return 0;
3794}
3795
3796int regulator_suspend_enable(struct regulator_dev *rdev,
3797 suspend_state_t state)
3798{
3799 return regulator_suspend_toggle(rdev, state, true);
3800}
3801EXPORT_SYMBOL_GPL(regulator_suspend_enable);
3802
3803int regulator_suspend_disable(struct regulator_dev *rdev,
3804 suspend_state_t state)
3805{
3806 struct regulator *regulator;
3807 struct regulator_voltage *voltage;
3808
3809
3810
3811
3812
3813 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3814 voltage = ®ulator->voltage[state];
3815 if (voltage->min_uV || voltage->max_uV)
3816 return 0;
3817 }
3818
3819 return regulator_suspend_toggle(rdev, state, false);
3820}
3821EXPORT_SYMBOL_GPL(regulator_suspend_disable);
3822
3823static int _regulator_set_suspend_voltage(struct regulator *regulator,
3824 int min_uV, int max_uV,
3825 suspend_state_t state)
3826{
3827 struct regulator_dev *rdev = regulator->rdev;
3828 struct regulator_state *rstate;
3829
3830 rstate = regulator_get_suspend_state(rdev, state);
3831 if (rstate == NULL)
3832 return -EINVAL;
3833
3834 if (rstate->min_uV == rstate->max_uV) {
3835 rdev_err(rdev, "The suspend voltage can't be changed!\n");
3836 return -EPERM;
3837 }
3838
3839 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
3840}
3841
3842int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
3843 int max_uV, suspend_state_t state)
3844{
3845 struct ww_acquire_ctx ww_ctx;
3846 int ret;
3847
3848
3849 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
3850 return -EINVAL;
3851
3852 regulator_lock_dependent(regulator->rdev, &ww_ctx);
3853
3854 ret = _regulator_set_suspend_voltage(regulator, min_uV,
3855 max_uV, state);
3856
3857 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3858
3859 return ret;
3860}
3861EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873int regulator_set_voltage_time(struct regulator *regulator,
3874 int old_uV, int new_uV)
3875{
3876 struct regulator_dev *rdev = regulator->rdev;
3877 const struct regulator_ops *ops = rdev->desc->ops;
3878 int old_sel = -1;
3879 int new_sel = -1;
3880 int voltage;
3881 int i;
3882
3883 if (ops->set_voltage_time)
3884 return ops->set_voltage_time(rdev, old_uV, new_uV);
3885 else if (!ops->set_voltage_time_sel)
3886 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3887
3888
3889 if (!ops->list_voltage || !rdev->desc->n_voltages)
3890 return -EINVAL;
3891
3892 for (i = 0; i < rdev->desc->n_voltages; i++) {
3893
3894 voltage = regulator_list_voltage(regulator, i);
3895 if (voltage < 0)
3896 return -EINVAL;
3897 if (voltage == 0)
3898 continue;
3899 if (voltage == old_uV)
3900 old_sel = i;
3901 if (voltage == new_uV)
3902 new_sel = i;
3903 }
3904
3905 if (old_sel < 0 || new_sel < 0)
3906 return -EINVAL;
3907
3908 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3909}
3910EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3925 unsigned int old_selector,
3926 unsigned int new_selector)
3927{
3928 int old_volt, new_volt;
3929
3930
3931 if (!rdev->desc->ops->list_voltage)
3932 return -EINVAL;
3933
3934 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3935 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3936
3937 if (rdev->desc->ops->set_voltage_time)
3938 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
3939 new_volt);
3940 else
3941 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
3942}
3943EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953int regulator_sync_voltage(struct regulator *regulator)
3954{
3955 struct regulator_dev *rdev = regulator->rdev;
3956 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON];
3957 int ret, min_uV, max_uV;
3958
3959 regulator_lock(rdev);
3960
3961 if (!rdev->desc->ops->set_voltage &&
3962 !rdev->desc->ops->set_voltage_sel) {
3963 ret = -EINVAL;
3964 goto out;
3965 }
3966
3967
3968 if (!voltage->min_uV && !voltage->max_uV) {
3969 ret = -EINVAL;
3970 goto out;
3971 }
3972
3973 min_uV = voltage->min_uV;
3974 max_uV = voltage->max_uV;
3975
3976
3977 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3978 if (ret < 0)
3979 goto out;
3980
3981 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
3982 if (ret < 0)
3983 goto out;
3984
3985 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3986
3987out:
3988 regulator_unlock(rdev);
3989 return ret;
3990}
3991EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3992
3993int regulator_get_voltage_rdev(struct regulator_dev *rdev)
3994{
3995 int sel, ret;
3996 bool bypassed;
3997
3998 if (rdev->desc->ops->get_bypass) {
3999 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
4000 if (ret < 0)
4001 return ret;
4002 if (bypassed) {
4003
4004 if (!rdev->supply) {
4005 rdev_err(rdev,
4006 "bypassed regulator has no supply!\n");
4007 return -EPROBE_DEFER;
4008 }
4009
4010 return regulator_get_voltage_rdev(rdev->supply->rdev);
4011 }
4012 }
4013
4014 if (rdev->desc->ops->get_voltage_sel) {
4015 sel = rdev->desc->ops->get_voltage_sel(rdev);
4016 if (sel < 0)
4017 return sel;
4018 ret = rdev->desc->ops->list_voltage(rdev, sel);
4019 } else if (rdev->desc->ops->get_voltage) {
4020 ret = rdev->desc->ops->get_voltage(rdev);
4021 } else if (rdev->desc->ops->list_voltage) {
4022 ret = rdev->desc->ops->list_voltage(rdev, 0);
4023 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
4024 ret = rdev->desc->fixed_uV;
4025 } else if (rdev->supply) {
4026 ret = regulator_get_voltage_rdev(rdev->supply->rdev);
4027 } else {
4028 return -EINVAL;
4029 }
4030
4031 if (ret < 0)
4032 return ret;
4033 return ret - rdev->constraints->uV_offset;
4034}
4035EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev);
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046int regulator_get_voltage(struct regulator *regulator)
4047{
4048 struct ww_acquire_ctx ww_ctx;
4049 int ret;
4050
4051 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4052 ret = regulator_get_voltage_rdev(regulator->rdev);
4053 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4054
4055 return ret;
4056}
4057EXPORT_SYMBOL_GPL(regulator_get_voltage);
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075int regulator_set_current_limit(struct regulator *regulator,
4076 int min_uA, int max_uA)
4077{
4078 struct regulator_dev *rdev = regulator->rdev;
4079 int ret;
4080
4081 regulator_lock(rdev);
4082
4083
4084 if (!rdev->desc->ops->set_current_limit) {
4085 ret = -EINVAL;
4086 goto out;
4087 }
4088
4089
4090 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
4091 if (ret < 0)
4092 goto out;
4093
4094 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
4095out:
4096 regulator_unlock(rdev);
4097 return ret;
4098}
4099EXPORT_SYMBOL_GPL(regulator_set_current_limit);
4100
4101static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
4102{
4103
4104 if (!rdev->desc->ops->get_current_limit)
4105 return -EINVAL;
4106
4107 return rdev->desc->ops->get_current_limit(rdev);
4108}
4109
4110static int _regulator_get_current_limit(struct regulator_dev *rdev)
4111{
4112 int ret;
4113
4114 regulator_lock(rdev);
4115 ret = _regulator_get_current_limit_unlocked(rdev);
4116 regulator_unlock(rdev);
4117
4118 return ret;
4119}
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130int regulator_get_current_limit(struct regulator *regulator)
4131{
4132 return _regulator_get_current_limit(regulator->rdev);
4133}
4134EXPORT_SYMBOL_GPL(regulator_get_current_limit);
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147int regulator_set_mode(struct regulator *regulator, unsigned int mode)
4148{
4149 struct regulator_dev *rdev = regulator->rdev;
4150 int ret;
4151 int regulator_curr_mode;
4152
4153 regulator_lock(rdev);
4154
4155
4156 if (!rdev->desc->ops->set_mode) {
4157 ret = -EINVAL;
4158 goto out;
4159 }
4160
4161
4162 if (rdev->desc->ops->get_mode) {
4163 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
4164 if (regulator_curr_mode == mode) {
4165 ret = 0;
4166 goto out;
4167 }
4168 }
4169
4170
4171 ret = regulator_mode_constrain(rdev, &mode);
4172 if (ret < 0)
4173 goto out;
4174
4175 ret = rdev->desc->ops->set_mode(rdev, mode);
4176out:
4177 regulator_unlock(rdev);
4178 return ret;
4179}
4180EXPORT_SYMBOL_GPL(regulator_set_mode);
4181
4182static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
4183{
4184
4185 if (!rdev->desc->ops->get_mode)
4186 return -EINVAL;
4187
4188 return rdev->desc->ops->get_mode(rdev);
4189}
4190
4191static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
4192{
4193 int ret;
4194
4195 regulator_lock(rdev);
4196 ret = _regulator_get_mode_unlocked(rdev);
4197 regulator_unlock(rdev);
4198
4199 return ret;
4200}
4201
4202
4203
4204
4205
4206
4207
4208unsigned int regulator_get_mode(struct regulator *regulator)
4209{
4210 return _regulator_get_mode(regulator->rdev);
4211}
4212EXPORT_SYMBOL_GPL(regulator_get_mode);
4213
4214static int _regulator_get_error_flags(struct regulator_dev *rdev,
4215 unsigned int *flags)
4216{
4217 int ret;
4218
4219 regulator_lock(rdev);
4220
4221
4222 if (!rdev->desc->ops->get_error_flags) {
4223 ret = -EINVAL;
4224 goto out;
4225 }
4226
4227 ret = rdev->desc->ops->get_error_flags(rdev, flags);
4228out:
4229 regulator_unlock(rdev);
4230 return ret;
4231}
4232
4233
4234
4235
4236
4237
4238
4239
4240int regulator_get_error_flags(struct regulator *regulator,
4241 unsigned int *flags)
4242{
4243 return _regulator_get_error_flags(regulator->rdev, flags);
4244}
4245EXPORT_SYMBOL_GPL(regulator_get_error_flags);
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281int regulator_set_load(struct regulator *regulator, int uA_load)
4282{
4283 struct regulator_dev *rdev = regulator->rdev;
4284 int old_uA_load;
4285 int ret = 0;
4286
4287 regulator_lock(rdev);
4288 old_uA_load = regulator->uA_load;
4289 regulator->uA_load = uA_load;
4290 if (regulator->enable_count && old_uA_load != uA_load) {
4291 ret = drms_uA_update(rdev);
4292 if (ret < 0)
4293 regulator->uA_load = old_uA_load;
4294 }
4295 regulator_unlock(rdev);
4296
4297 return ret;
4298}
4299EXPORT_SYMBOL_GPL(regulator_set_load);
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312int regulator_allow_bypass(struct regulator *regulator, bool enable)
4313{
4314 struct regulator_dev *rdev = regulator->rdev;
4315 int ret = 0;
4316
4317 if (!rdev->desc->ops->set_bypass)
4318 return 0;
4319
4320 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
4321 return 0;
4322
4323 regulator_lock(rdev);
4324
4325 if (enable && !regulator->bypass) {
4326 rdev->bypass_count++;
4327
4328 if (rdev->bypass_count == rdev->open_count) {
4329 ret = rdev->desc->ops->set_bypass(rdev, enable);
4330 if (ret != 0)
4331 rdev->bypass_count--;
4332 }
4333
4334 } else if (!enable && regulator->bypass) {
4335 rdev->bypass_count--;
4336
4337 if (rdev->bypass_count != rdev->open_count) {
4338 ret = rdev->desc->ops->set_bypass(rdev, enable);
4339 if (ret != 0)
4340 rdev->bypass_count++;
4341 }
4342 }
4343
4344 if (ret == 0)
4345 regulator->bypass = enable;
4346
4347 regulator_unlock(rdev);
4348
4349 return ret;
4350}
4351EXPORT_SYMBOL_GPL(regulator_allow_bypass);
4352
4353
4354
4355
4356
4357
4358
4359
4360int regulator_register_notifier(struct regulator *regulator,
4361 struct notifier_block *nb)
4362{
4363 return blocking_notifier_chain_register(®ulator->rdev->notifier,
4364 nb);
4365}
4366EXPORT_SYMBOL_GPL(regulator_register_notifier);
4367
4368
4369
4370
4371
4372
4373
4374
4375int regulator_unregister_notifier(struct regulator *regulator,
4376 struct notifier_block *nb)
4377{
4378 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
4379 nb);
4380}
4381EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
4382
4383
4384
4385
4386static int _notifier_call_chain(struct regulator_dev *rdev,
4387 unsigned long event, void *data)
4388{
4389
4390 return blocking_notifier_call_chain(&rdev->notifier, event, data);
4391}
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407int regulator_bulk_get(struct device *dev, int num_consumers,
4408 struct regulator_bulk_data *consumers)
4409{
4410 int i;
4411 int ret;
4412
4413 for (i = 0; i < num_consumers; i++)
4414 consumers[i].consumer = NULL;
4415
4416 for (i = 0; i < num_consumers; i++) {
4417 consumers[i].consumer = regulator_get(dev,
4418 consumers[i].supply);
4419 if (IS_ERR(consumers[i].consumer)) {
4420 ret = PTR_ERR(consumers[i].consumer);
4421 consumers[i].consumer = NULL;
4422 goto err;
4423 }
4424 }
4425
4426 return 0;
4427
4428err:
4429 if (ret != -EPROBE_DEFER)
4430 dev_err(dev, "Failed to get supply '%s': %d\n",
4431 consumers[i].supply, ret);
4432 else
4433 dev_dbg(dev, "Failed to get supply '%s', deferring\n",
4434 consumers[i].supply);
4435
4436 while (--i >= 0)
4437 regulator_put(consumers[i].consumer);
4438
4439 return ret;
4440}
4441EXPORT_SYMBOL_GPL(regulator_bulk_get);
4442
4443static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
4444{
4445 struct regulator_bulk_data *bulk = data;
4446
4447 bulk->ret = regulator_enable(bulk->consumer);
4448}
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462int regulator_bulk_enable(int num_consumers,
4463 struct regulator_bulk_data *consumers)
4464{
4465 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
4466 int i;
4467 int ret = 0;
4468
4469 for (i = 0; i < num_consumers; i++) {
4470 async_schedule_domain(regulator_bulk_enable_async,
4471 &consumers[i], &async_domain);
4472 }
4473
4474 async_synchronize_full_domain(&async_domain);
4475
4476
4477 for (i = 0; i < num_consumers; i++) {
4478 if (consumers[i].ret != 0) {
4479 ret = consumers[i].ret;
4480 goto err;
4481 }
4482 }
4483
4484 return 0;
4485
4486err:
4487 for (i = 0; i < num_consumers; i++) {
4488 if (consumers[i].ret < 0)
4489 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
4490 consumers[i].ret);
4491 else
4492 regulator_disable(consumers[i].consumer);
4493 }
4494
4495 return ret;
4496}
4497EXPORT_SYMBOL_GPL(regulator_bulk_enable);
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511int regulator_bulk_disable(int num_consumers,
4512 struct regulator_bulk_data *consumers)
4513{
4514 int i;
4515 int ret, r;
4516
4517 for (i = num_consumers - 1; i >= 0; --i) {
4518 ret = regulator_disable(consumers[i].consumer);
4519 if (ret != 0)
4520 goto err;
4521 }
4522
4523 return 0;
4524
4525err:
4526 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
4527 for (++i; i < num_consumers; ++i) {
4528 r = regulator_enable(consumers[i].consumer);
4529 if (r != 0)
4530 pr_err("Failed to re-enable %s: %d\n",
4531 consumers[i].supply, r);
4532 }
4533
4534 return ret;
4535}
4536EXPORT_SYMBOL_GPL(regulator_bulk_disable);
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552int regulator_bulk_force_disable(int num_consumers,
4553 struct regulator_bulk_data *consumers)
4554{
4555 int i;
4556 int ret = 0;
4557
4558 for (i = 0; i < num_consumers; i++) {
4559 consumers[i].ret =
4560 regulator_force_disable(consumers[i].consumer);
4561
4562
4563 if (consumers[i].ret && !ret)
4564 ret = consumers[i].ret;
4565 }
4566
4567 return ret;
4568}
4569EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580void regulator_bulk_free(int num_consumers,
4581 struct regulator_bulk_data *consumers)
4582{
4583 int i;
4584
4585 for (i = 0; i < num_consumers; i++) {
4586 regulator_put(consumers[i].consumer);
4587 consumers[i].consumer = NULL;
4588 }
4589}
4590EXPORT_SYMBOL_GPL(regulator_bulk_free);
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602int regulator_notifier_call_chain(struct regulator_dev *rdev,
4603 unsigned long event, void *data)
4604{
4605 lockdep_assert_held_once(&rdev->mutex.base);
4606
4607 _notifier_call_chain(rdev, event, data);
4608 return NOTIFY_DONE;
4609
4610}
4611EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
4612
4613
4614
4615
4616
4617
4618
4619
4620int regulator_mode_to_status(unsigned int mode)
4621{
4622 switch (mode) {
4623 case REGULATOR_MODE_FAST:
4624 return REGULATOR_STATUS_FAST;
4625 case REGULATOR_MODE_NORMAL:
4626 return REGULATOR_STATUS_NORMAL;
4627 case REGULATOR_MODE_IDLE:
4628 return REGULATOR_STATUS_IDLE;
4629 case REGULATOR_MODE_STANDBY:
4630 return REGULATOR_STATUS_STANDBY;
4631 default:
4632 return REGULATOR_STATUS_UNDEFINED;
4633 }
4634}
4635EXPORT_SYMBOL_GPL(regulator_mode_to_status);
4636
4637static struct attribute *regulator_dev_attrs[] = {
4638 &dev_attr_name.attr,
4639 &dev_attr_num_users.attr,
4640 &dev_attr_type.attr,
4641 &dev_attr_microvolts.attr,
4642 &dev_attr_microamps.attr,
4643 &dev_attr_opmode.attr,
4644 &dev_attr_state.attr,
4645 &dev_attr_status.attr,
4646 &dev_attr_bypass.attr,
4647 &dev_attr_requested_microamps.attr,
4648 &dev_attr_min_microvolts.attr,
4649 &dev_attr_max_microvolts.attr,
4650 &dev_attr_min_microamps.attr,
4651 &dev_attr_max_microamps.attr,
4652 &dev_attr_suspend_standby_state.attr,
4653 &dev_attr_suspend_mem_state.attr,
4654 &dev_attr_suspend_disk_state.attr,
4655 &dev_attr_suspend_standby_microvolts.attr,
4656 &dev_attr_suspend_mem_microvolts.attr,
4657 &dev_attr_suspend_disk_microvolts.attr,
4658 &dev_attr_suspend_standby_mode.attr,
4659 &dev_attr_suspend_mem_mode.attr,
4660 &dev_attr_suspend_disk_mode.attr,
4661 NULL
4662};
4663
4664
4665
4666
4667
4668static umode_t regulator_attr_is_visible(struct kobject *kobj,
4669 struct attribute *attr, int idx)
4670{
4671 struct device *dev = kobj_to_dev(kobj);
4672 struct regulator_dev *rdev = dev_to_rdev(dev);
4673 const struct regulator_ops *ops = rdev->desc->ops;
4674 umode_t mode = attr->mode;
4675
4676
4677 if (attr == &dev_attr_name.attr ||
4678 attr == &dev_attr_num_users.attr ||
4679 attr == &dev_attr_type.attr)
4680 return mode;
4681
4682
4683 if (attr == &dev_attr_microvolts.attr) {
4684 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
4685 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
4686 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
4687 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
4688 return mode;
4689 return 0;
4690 }
4691
4692 if (attr == &dev_attr_microamps.attr)
4693 return ops->get_current_limit ? mode : 0;
4694
4695 if (attr == &dev_attr_opmode.attr)
4696 return ops->get_mode ? mode : 0;
4697
4698 if (attr == &dev_attr_state.attr)
4699 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
4700
4701 if (attr == &dev_attr_status.attr)
4702 return ops->get_status ? mode : 0;
4703
4704 if (attr == &dev_attr_bypass.attr)
4705 return ops->get_bypass ? mode : 0;
4706
4707
4708 if (attr == &dev_attr_min_microvolts.attr ||
4709 attr == &dev_attr_max_microvolts.attr)
4710 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
4711
4712 if (attr == &dev_attr_min_microamps.attr ||
4713 attr == &dev_attr_max_microamps.attr)
4714 return ops->set_current_limit ? mode : 0;
4715
4716 if (attr == &dev_attr_suspend_standby_state.attr ||
4717 attr == &dev_attr_suspend_mem_state.attr ||
4718 attr == &dev_attr_suspend_disk_state.attr)
4719 return mode;
4720
4721 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4722 attr == &dev_attr_suspend_mem_microvolts.attr ||
4723 attr == &dev_attr_suspend_disk_microvolts.attr)
4724 return ops->set_suspend_voltage ? mode : 0;
4725
4726 if (attr == &dev_attr_suspend_standby_mode.attr ||
4727 attr == &dev_attr_suspend_mem_mode.attr ||
4728 attr == &dev_attr_suspend_disk_mode.attr)
4729 return ops->set_suspend_mode ? mode : 0;
4730
4731 return mode;
4732}
4733
4734static const struct attribute_group regulator_dev_group = {
4735 .attrs = regulator_dev_attrs,
4736 .is_visible = regulator_attr_is_visible,
4737};
4738
4739static const struct attribute_group *regulator_dev_groups[] = {
4740 ®ulator_dev_group,
4741 NULL
4742};
4743
4744static void regulator_dev_release(struct device *dev)
4745{
4746 struct regulator_dev *rdev = dev_get_drvdata(dev);
4747
4748 kfree(rdev->constraints);
4749 of_node_put(rdev->dev.of_node);
4750 kfree(rdev);
4751}
4752
4753static void rdev_init_debugfs(struct regulator_dev *rdev)
4754{
4755 struct device *parent = rdev->dev.parent;
4756 const char *rname = rdev_get_name(rdev);
4757 char name[NAME_MAX];
4758
4759
4760 if (parent && rname == rdev->desc->name) {
4761 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4762 rname);
4763 rname = name;
4764 }
4765
4766 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
4767 if (!rdev->debugfs) {
4768 rdev_warn(rdev, "Failed to create debugfs directory\n");
4769 return;
4770 }
4771
4772 debugfs_create_u32("use_count", 0444, rdev->debugfs,
4773 &rdev->use_count);
4774 debugfs_create_u32("open_count", 0444, rdev->debugfs,
4775 &rdev->open_count);
4776 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4777 &rdev->bypass_count);
4778}
4779
4780static int regulator_register_resolve_supply(struct device *dev, void *data)
4781{
4782 struct regulator_dev *rdev = dev_to_rdev(dev);
4783
4784 if (regulator_resolve_supply(rdev))
4785 rdev_dbg(rdev, "unable to resolve supply\n");
4786
4787 return 0;
4788}
4789
4790int regulator_coupler_register(struct regulator_coupler *coupler)
4791{
4792 mutex_lock(®ulator_list_mutex);
4793 list_add_tail(&coupler->list, ®ulator_coupler_list);
4794 mutex_unlock(®ulator_list_mutex);
4795
4796 return 0;
4797}
4798
4799static struct regulator_coupler *
4800regulator_find_coupler(struct regulator_dev *rdev)
4801{
4802 struct regulator_coupler *coupler;
4803 int err;
4804
4805
4806
4807
4808
4809
4810 list_for_each_entry_reverse(coupler, ®ulator_coupler_list, list) {
4811 err = coupler->attach_regulator(coupler, rdev);
4812 if (!err) {
4813 if (!coupler->balance_voltage &&
4814 rdev->coupling_desc.n_coupled > 2)
4815 goto err_unsupported;
4816
4817 return coupler;
4818 }
4819
4820 if (err < 0)
4821 return ERR_PTR(err);
4822
4823 if (err == 1)
4824 continue;
4825
4826 break;
4827 }
4828
4829 return ERR_PTR(-EINVAL);
4830
4831err_unsupported:
4832 if (coupler->detach_regulator)
4833 coupler->detach_regulator(coupler, rdev);
4834
4835 rdev_err(rdev,
4836 "Voltage balancing for multiple regulator couples is unimplemented\n");
4837
4838 return ERR_PTR(-EPERM);
4839}
4840
4841static void regulator_resolve_coupling(struct regulator_dev *rdev)
4842{
4843 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
4844 struct coupling_desc *c_desc = &rdev->coupling_desc;
4845 int n_coupled = c_desc->n_coupled;
4846 struct regulator_dev *c_rdev;
4847 int i;
4848
4849 for (i = 1; i < n_coupled; i++) {
4850
4851 if (c_desc->coupled_rdevs[i])
4852 continue;
4853
4854 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
4855
4856 if (!c_rdev)
4857 continue;
4858
4859 if (c_rdev->coupling_desc.coupler != coupler) {
4860 rdev_err(rdev, "coupler mismatch with %s\n",
4861 rdev_get_name(c_rdev));
4862 return;
4863 }
4864
4865 regulator_lock(c_rdev);
4866
4867 c_desc->coupled_rdevs[i] = c_rdev;
4868 c_desc->n_resolved++;
4869
4870 regulator_unlock(c_rdev);
4871
4872 regulator_resolve_coupling(c_rdev);
4873 }
4874}
4875
4876static void regulator_remove_coupling(struct regulator_dev *rdev)
4877{
4878 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
4879 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
4880 struct regulator_dev *__c_rdev, *c_rdev;
4881 unsigned int __n_coupled, n_coupled;
4882 int i, k;
4883 int err;
4884
4885 n_coupled = c_desc->n_coupled;
4886
4887 for (i = 1; i < n_coupled; i++) {
4888 c_rdev = c_desc->coupled_rdevs[i];
4889
4890 if (!c_rdev)
4891 continue;
4892
4893 regulator_lock(c_rdev);
4894
4895 __c_desc = &c_rdev->coupling_desc;
4896 __n_coupled = __c_desc->n_coupled;
4897
4898 for (k = 1; k < __n_coupled; k++) {
4899 __c_rdev = __c_desc->coupled_rdevs[k];
4900
4901 if (__c_rdev == rdev) {
4902 __c_desc->coupled_rdevs[k] = NULL;
4903 __c_desc->n_resolved--;
4904 break;
4905 }
4906 }
4907
4908 regulator_unlock(c_rdev);
4909
4910 c_desc->coupled_rdevs[i] = NULL;
4911 c_desc->n_resolved--;
4912 }
4913
4914 if (coupler && coupler->detach_regulator) {
4915 err = coupler->detach_regulator(coupler, rdev);
4916 if (err)
4917 rdev_err(rdev, "failed to detach from coupler: %d\n",
4918 err);
4919 }
4920
4921 kfree(rdev->coupling_desc.coupled_rdevs);
4922 rdev->coupling_desc.coupled_rdevs = NULL;
4923}
4924
4925static int regulator_init_coupling(struct regulator_dev *rdev)
4926{
4927 int err, n_phandles;
4928 size_t alloc_size;
4929
4930 if (!IS_ENABLED(CONFIG_OF))
4931 n_phandles = 0;
4932 else
4933 n_phandles = of_get_n_coupled(rdev);
4934
4935 alloc_size = sizeof(*rdev) * (n_phandles + 1);
4936
4937 rdev->coupling_desc.coupled_rdevs = kzalloc(alloc_size, GFP_KERNEL);
4938 if (!rdev->coupling_desc.coupled_rdevs)
4939 return -ENOMEM;
4940
4941
4942
4943
4944
4945 rdev->coupling_desc.coupled_rdevs[0] = rdev;
4946 rdev->coupling_desc.n_coupled = n_phandles + 1;
4947 rdev->coupling_desc.n_resolved++;
4948
4949
4950 if (n_phandles == 0)
4951 return 0;
4952
4953 if (!of_check_coupling_data(rdev))
4954 return -EPERM;
4955
4956 rdev->coupling_desc.coupler = regulator_find_coupler(rdev);
4957 if (IS_ERR(rdev->coupling_desc.coupler)) {
4958 err = PTR_ERR(rdev->coupling_desc.coupler);
4959 rdev_err(rdev, "failed to get coupler: %d\n", err);
4960 return err;
4961 }
4962
4963 return 0;
4964}
4965
4966static int generic_coupler_attach(struct regulator_coupler *coupler,
4967 struct regulator_dev *rdev)
4968{
4969 if (rdev->coupling_desc.n_coupled > 2) {
4970 rdev_err(rdev,
4971 "Voltage balancing for multiple regulator couples is unimplemented\n");
4972 return -EPERM;
4973 }
4974
4975 if (!rdev->constraints->always_on) {
4976 rdev_err(rdev,
4977 "Coupling of a non always-on regulator is unimplemented\n");
4978 return -ENOTSUPP;
4979 }
4980
4981 return 0;
4982}
4983
4984static struct regulator_coupler generic_regulator_coupler = {
4985 .attach_regulator = generic_coupler_attach,
4986};
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997struct regulator_dev *
4998regulator_register(const struct regulator_desc *regulator_desc,
4999 const struct regulator_config *cfg)
5000{
5001 const struct regulation_constraints *constraints = NULL;
5002 const struct regulator_init_data *init_data;
5003 struct regulator_config *config = NULL;
5004 static atomic_t regulator_no = ATOMIC_INIT(-1);
5005 struct regulator_dev *rdev;
5006 bool dangling_cfg_gpiod = false;
5007 bool dangling_of_gpiod = false;
5008 bool reg_device_fail = false;
5009 struct device *dev;
5010 int ret, i;
5011
5012 if (cfg == NULL)
5013 return ERR_PTR(-EINVAL);
5014 if (cfg->ena_gpiod)
5015 dangling_cfg_gpiod = true;
5016 if (regulator_desc == NULL) {
5017 ret = -EINVAL;
5018 goto rinse;
5019 }
5020
5021 dev = cfg->dev;
5022 WARN_ON(!dev);
5023
5024 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
5025 ret = -EINVAL;
5026 goto rinse;
5027 }
5028
5029 if (regulator_desc->type != REGULATOR_VOLTAGE &&
5030 regulator_desc->type != REGULATOR_CURRENT) {
5031 ret = -EINVAL;
5032 goto rinse;
5033 }
5034
5035
5036 WARN_ON(regulator_desc->ops->get_voltage &&
5037 regulator_desc->ops->get_voltage_sel);
5038 WARN_ON(regulator_desc->ops->set_voltage &&
5039 regulator_desc->ops->set_voltage_sel);
5040
5041
5042 if (regulator_desc->ops->get_voltage_sel &&
5043 !regulator_desc->ops->list_voltage) {
5044 ret = -EINVAL;
5045 goto rinse;
5046 }
5047 if (regulator_desc->ops->set_voltage_sel &&
5048 !regulator_desc->ops->list_voltage) {
5049 ret = -EINVAL;
5050 goto rinse;
5051 }
5052
5053 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
5054 if (rdev == NULL) {
5055 ret = -ENOMEM;
5056 goto rinse;
5057 }
5058
5059
5060
5061
5062
5063 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
5064 if (config == NULL) {
5065 kfree(rdev);
5066 ret = -ENOMEM;
5067 goto rinse;
5068 }
5069
5070 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
5071 &rdev->dev.of_node);
5072
5073
5074
5075
5076
5077
5078 if (PTR_ERR(init_data) == -EPROBE_DEFER) {
5079 kfree(config);
5080 kfree(rdev);
5081 ret = -EPROBE_DEFER;
5082 goto rinse;
5083 }
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093 if (!cfg->ena_gpiod && config->ena_gpiod)
5094 dangling_of_gpiod = true;
5095 if (!init_data) {
5096 init_data = config->init_data;
5097 rdev->dev.of_node = of_node_get(config->of_node);
5098 }
5099
5100 ww_mutex_init(&rdev->mutex, ®ulator_ww_class);
5101 rdev->reg_data = config->driver_data;
5102 rdev->owner = regulator_desc->owner;
5103 rdev->desc = regulator_desc;
5104 if (config->regmap)
5105 rdev->regmap = config->regmap;
5106 else if (dev_get_regmap(dev, NULL))
5107 rdev->regmap = dev_get_regmap(dev, NULL);
5108 else if (dev->parent)
5109 rdev->regmap = dev_get_regmap(dev->parent, NULL);
5110 INIT_LIST_HEAD(&rdev->consumer_list);
5111 INIT_LIST_HEAD(&rdev->list);
5112 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
5113 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
5114
5115
5116 if (init_data && init_data->regulator_init) {
5117 ret = init_data->regulator_init(rdev->reg_data);
5118 if (ret < 0)
5119 goto clean;
5120 }
5121
5122 if (config->ena_gpiod) {
5123 mutex_lock(®ulator_list_mutex);
5124 ret = regulator_ena_gpio_request(rdev, config);
5125 mutex_unlock(®ulator_list_mutex);
5126 if (ret != 0) {
5127 rdev_err(rdev, "Failed to request enable GPIO: %d\n",
5128 ret);
5129 goto clean;
5130 }
5131
5132 dangling_cfg_gpiod = false;
5133 dangling_of_gpiod = false;
5134 }
5135
5136
5137 rdev->dev.class = ®ulator_class;
5138 rdev->dev.parent = dev;
5139 dev_set_name(&rdev->dev, "regulator.%lu",
5140 (unsigned long) atomic_inc_return(®ulator_no));
5141
5142
5143 if (init_data)
5144 constraints = &init_data->constraints;
5145
5146 if (init_data && init_data->supply_regulator)
5147 rdev->supply_name = init_data->supply_regulator;
5148 else if (regulator_desc->supply_name)
5149 rdev->supply_name = regulator_desc->supply_name;
5150
5151
5152
5153
5154
5155
5156 if (regulator_resolve_supply(rdev))
5157 rdev_dbg(rdev, "unable to resolve supply\n");
5158
5159 ret = set_machine_constraints(rdev, constraints);
5160 if (ret < 0)
5161 goto wash;
5162
5163 mutex_lock(®ulator_list_mutex);
5164 ret = regulator_init_coupling(rdev);
5165 mutex_unlock(®ulator_list_mutex);
5166 if (ret < 0)
5167 goto wash;
5168
5169
5170 if (init_data) {
5171 mutex_lock(®ulator_list_mutex);
5172 for (i = 0; i < init_data->num_consumer_supplies; i++) {
5173 ret = set_consumer_device_supply(rdev,
5174 init_data->consumer_supplies[i].dev_name,
5175 init_data->consumer_supplies[i].supply);
5176 if (ret < 0) {
5177 mutex_unlock(®ulator_list_mutex);
5178 dev_err(dev, "Failed to set supply %s\n",
5179 init_data->consumer_supplies[i].supply);
5180 goto unset_supplies;
5181 }
5182 }
5183 mutex_unlock(®ulator_list_mutex);
5184 }
5185
5186 if (!rdev->desc->ops->get_voltage &&
5187 !rdev->desc->ops->list_voltage &&
5188 !rdev->desc->fixed_uV)
5189 rdev->is_switch = true;
5190
5191 dev_set_drvdata(&rdev->dev, rdev);
5192 ret = device_register(&rdev->dev);
5193 if (ret != 0) {
5194 reg_device_fail = true;
5195 goto unset_supplies;
5196 }
5197
5198 rdev_init_debugfs(rdev);
5199
5200
5201 mutex_lock(®ulator_list_mutex);
5202 regulator_resolve_coupling(rdev);
5203 mutex_unlock(®ulator_list_mutex);
5204
5205
5206 class_for_each_device(®ulator_class, NULL, NULL,
5207 regulator_register_resolve_supply);
5208 kfree(config);
5209 return rdev;
5210
5211unset_supplies:
5212 mutex_lock(®ulator_list_mutex);
5213 unset_regulator_supplies(rdev);
5214 regulator_remove_coupling(rdev);
5215 mutex_unlock(®ulator_list_mutex);
5216wash:
5217 kfree(rdev->coupling_desc.coupled_rdevs);
5218 kfree(rdev->constraints);
5219 mutex_lock(®ulator_list_mutex);
5220 regulator_ena_gpio_free(rdev);
5221 mutex_unlock(®ulator_list_mutex);
5222clean:
5223 if (dangling_of_gpiod)
5224 gpiod_put(config->ena_gpiod);
5225 if (reg_device_fail)
5226 put_device(&rdev->dev);
5227 else
5228 kfree(rdev);
5229 kfree(config);
5230rinse:
5231 if (dangling_cfg_gpiod)
5232 gpiod_put(cfg->ena_gpiod);
5233 return ERR_PTR(ret);
5234}
5235EXPORT_SYMBOL_GPL(regulator_register);
5236
5237
5238
5239
5240
5241
5242
5243void regulator_unregister(struct regulator_dev *rdev)
5244{
5245 if (rdev == NULL)
5246 return;
5247
5248 if (rdev->supply) {
5249 while (rdev->use_count--)
5250 regulator_disable(rdev->supply);
5251 regulator_put(rdev->supply);
5252 }
5253
5254 flush_work(&rdev->disable_work.work);
5255
5256 mutex_lock(®ulator_list_mutex);
5257
5258 debugfs_remove_recursive(rdev->debugfs);
5259 WARN_ON(rdev->open_count);
5260 regulator_remove_coupling(rdev);
5261 unset_regulator_supplies(rdev);
5262 list_del(&rdev->list);
5263 regulator_ena_gpio_free(rdev);
5264 device_unregister(&rdev->dev);
5265
5266 mutex_unlock(®ulator_list_mutex);
5267}
5268EXPORT_SYMBOL_GPL(regulator_unregister);
5269
5270#ifdef CONFIG_SUSPEND
5271
5272
5273
5274
5275
5276
5277static int regulator_suspend(struct device *dev)
5278{
5279 struct regulator_dev *rdev = dev_to_rdev(dev);
5280 suspend_state_t state = pm_suspend_target_state;
5281 int ret;
5282
5283 regulator_lock(rdev);
5284 ret = suspend_set_state(rdev, state);
5285 regulator_unlock(rdev);
5286
5287 return ret;
5288}
5289
5290static int regulator_resume(struct device *dev)
5291{
5292 suspend_state_t state = pm_suspend_target_state;
5293 struct regulator_dev *rdev = dev_to_rdev(dev);
5294 struct regulator_state *rstate;
5295 int ret = 0;
5296
5297 rstate = regulator_get_suspend_state(rdev, state);
5298 if (rstate == NULL)
5299 return 0;
5300
5301 regulator_lock(rdev);
5302
5303 if (rdev->desc->ops->resume &&
5304 (rstate->enabled == ENABLE_IN_SUSPEND ||
5305 rstate->enabled == DISABLE_IN_SUSPEND))
5306 ret = rdev->desc->ops->resume(rdev);
5307
5308 regulator_unlock(rdev);
5309
5310 return ret;
5311}
5312#else
5313
5314#define regulator_suspend NULL
5315#define regulator_resume NULL
5316
5317#endif
5318
5319#ifdef CONFIG_PM
5320static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
5321 .suspend = regulator_suspend,
5322 .resume = regulator_resume,
5323};
5324#endif
5325
5326struct class regulator_class = {
5327 .name = "regulator",
5328 .dev_release = regulator_dev_release,
5329 .dev_groups = regulator_dev_groups,
5330#ifdef CONFIG_PM
5331 .pm = ®ulator_pm_ops,
5332#endif
5333};
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345void regulator_has_full_constraints(void)
5346{
5347 has_full_constraints = 1;
5348}
5349EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
5350
5351
5352
5353
5354
5355
5356
5357
5358void *rdev_get_drvdata(struct regulator_dev *rdev)
5359{
5360 return rdev->reg_data;
5361}
5362EXPORT_SYMBOL_GPL(rdev_get_drvdata);
5363
5364
5365
5366
5367
5368
5369
5370
5371void *regulator_get_drvdata(struct regulator *regulator)
5372{
5373 return regulator->rdev->reg_data;
5374}
5375EXPORT_SYMBOL_GPL(regulator_get_drvdata);
5376
5377
5378
5379
5380
5381
5382void regulator_set_drvdata(struct regulator *regulator, void *data)
5383{
5384 regulator->rdev->reg_data = data;
5385}
5386EXPORT_SYMBOL_GPL(regulator_set_drvdata);
5387
5388
5389
5390
5391
5392int rdev_get_id(struct regulator_dev *rdev)
5393{
5394 return rdev->desc->id;
5395}
5396EXPORT_SYMBOL_GPL(rdev_get_id);
5397
5398struct device *rdev_get_dev(struct regulator_dev *rdev)
5399{
5400 return &rdev->dev;
5401}
5402EXPORT_SYMBOL_GPL(rdev_get_dev);
5403
5404struct regmap *rdev_get_regmap(struct regulator_dev *rdev)
5405{
5406 return rdev->regmap;
5407}
5408EXPORT_SYMBOL_GPL(rdev_get_regmap);
5409
5410void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
5411{
5412 return reg_init_data->driver_data;
5413}
5414EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
5415
5416#ifdef CONFIG_DEBUG_FS
5417static int supply_map_show(struct seq_file *sf, void *data)
5418{
5419 struct regulator_map *map;
5420
5421 list_for_each_entry(map, ®ulator_map_list, list) {
5422 seq_printf(sf, "%s -> %s.%s\n",
5423 rdev_get_name(map->regulator), map->dev_name,
5424 map->supply);
5425 }
5426
5427 return 0;
5428}
5429DEFINE_SHOW_ATTRIBUTE(supply_map);
5430
5431struct summary_data {
5432 struct seq_file *s;
5433 struct regulator_dev *parent;
5434 int level;
5435};
5436
5437static void regulator_summary_show_subtree(struct seq_file *s,
5438 struct regulator_dev *rdev,
5439 int level);
5440
5441static int regulator_summary_show_children(struct device *dev, void *data)
5442{
5443 struct regulator_dev *rdev = dev_to_rdev(dev);
5444 struct summary_data *summary_data = data;
5445
5446 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
5447 regulator_summary_show_subtree(summary_data->s, rdev,
5448 summary_data->level + 1);
5449
5450 return 0;
5451}
5452
5453static void regulator_summary_show_subtree(struct seq_file *s,
5454 struct regulator_dev *rdev,
5455 int level)
5456{
5457 struct regulation_constraints *c;
5458 struct regulator *consumer;
5459 struct summary_data summary_data;
5460 unsigned int opmode;
5461
5462 if (!rdev)
5463 return;
5464
5465 opmode = _regulator_get_mode_unlocked(rdev);
5466 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
5467 level * 3 + 1, "",
5468 30 - level * 3, rdev_get_name(rdev),
5469 rdev->use_count, rdev->open_count, rdev->bypass_count,
5470 regulator_opmode_to_str(opmode));
5471
5472 seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000);
5473 seq_printf(s, "%5dmA ",
5474 _regulator_get_current_limit_unlocked(rdev) / 1000);
5475
5476 c = rdev->constraints;
5477 if (c) {
5478 switch (rdev->desc->type) {
5479 case REGULATOR_VOLTAGE:
5480 seq_printf(s, "%5dmV %5dmV ",
5481 c->min_uV / 1000, c->max_uV / 1000);
5482 break;
5483 case REGULATOR_CURRENT:
5484 seq_printf(s, "%5dmA %5dmA ",
5485 c->min_uA / 1000, c->max_uA / 1000);
5486 break;
5487 }
5488 }
5489
5490 seq_puts(s, "\n");
5491
5492 list_for_each_entry(consumer, &rdev->consumer_list, list) {
5493 if (consumer->dev && consumer->dev->class == ®ulator_class)
5494 continue;
5495
5496 seq_printf(s, "%*s%-*s ",
5497 (level + 1) * 3 + 1, "",
5498 30 - (level + 1) * 3,
5499 consumer->dev ? dev_name(consumer->dev) : "deviceless");
5500
5501 switch (rdev->desc->type) {
5502 case REGULATOR_VOLTAGE:
5503 seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
5504 consumer->enable_count,
5505 consumer->uA_load / 1000,
5506 consumer->uA_load && !consumer->enable_count ?
5507 '*' : ' ',
5508 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
5509 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
5510 break;
5511 case REGULATOR_CURRENT:
5512 break;
5513 }
5514
5515 seq_puts(s, "\n");
5516 }
5517
5518 summary_data.s = s;
5519 summary_data.level = level;
5520 summary_data.parent = rdev;
5521
5522 class_for_each_device(®ulator_class, NULL, &summary_data,
5523 regulator_summary_show_children);
5524}
5525
5526struct summary_lock_data {
5527 struct ww_acquire_ctx *ww_ctx;
5528 struct regulator_dev **new_contended_rdev;
5529 struct regulator_dev **old_contended_rdev;
5530};
5531
5532static int regulator_summary_lock_one(struct device *dev, void *data)
5533{
5534 struct regulator_dev *rdev = dev_to_rdev(dev);
5535 struct summary_lock_data *lock_data = data;
5536 int ret = 0;
5537
5538 if (rdev != *lock_data->old_contended_rdev) {
5539 ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
5540
5541 if (ret == -EDEADLK)
5542 *lock_data->new_contended_rdev = rdev;
5543 else
5544 WARN_ON_ONCE(ret);
5545 } else {
5546 *lock_data->old_contended_rdev = NULL;
5547 }
5548
5549 return ret;
5550}
5551
5552static int regulator_summary_unlock_one(struct device *dev, void *data)
5553{
5554 struct regulator_dev *rdev = dev_to_rdev(dev);
5555 struct summary_lock_data *lock_data = data;
5556
5557 if (lock_data) {
5558 if (rdev == *lock_data->new_contended_rdev)
5559 return -EDEADLK;
5560 }
5561
5562 regulator_unlock(rdev);
5563
5564 return 0;
5565}
5566
5567static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
5568 struct regulator_dev **new_contended_rdev,
5569 struct regulator_dev **old_contended_rdev)
5570{
5571 struct summary_lock_data lock_data;
5572 int ret;
5573
5574 lock_data.ww_ctx = ww_ctx;
5575 lock_data.new_contended_rdev = new_contended_rdev;
5576 lock_data.old_contended_rdev = old_contended_rdev;
5577
5578 ret = class_for_each_device(®ulator_class, NULL, &lock_data,
5579 regulator_summary_lock_one);
5580 if (ret)
5581 class_for_each_device(®ulator_class, NULL, &lock_data,
5582 regulator_summary_unlock_one);
5583
5584 return ret;
5585}
5586
5587static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
5588{
5589 struct regulator_dev *new_contended_rdev = NULL;
5590 struct regulator_dev *old_contended_rdev = NULL;
5591 int err;
5592
5593 mutex_lock(®ulator_list_mutex);
5594
5595 ww_acquire_init(ww_ctx, ®ulator_ww_class);
5596
5597 do {
5598 if (new_contended_rdev) {
5599 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
5600 old_contended_rdev = new_contended_rdev;
5601 old_contended_rdev->ref_cnt++;
5602 }
5603
5604 err = regulator_summary_lock_all(ww_ctx,
5605 &new_contended_rdev,
5606 &old_contended_rdev);
5607
5608 if (old_contended_rdev)
5609 regulator_unlock(old_contended_rdev);
5610
5611 } while (err == -EDEADLK);
5612
5613 ww_acquire_done(ww_ctx);
5614}
5615
5616static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
5617{
5618 class_for_each_device(®ulator_class, NULL, NULL,
5619 regulator_summary_unlock_one);
5620 ww_acquire_fini(ww_ctx);
5621
5622 mutex_unlock(®ulator_list_mutex);
5623}
5624
5625static int regulator_summary_show_roots(struct device *dev, void *data)
5626{
5627 struct regulator_dev *rdev = dev_to_rdev(dev);
5628 struct seq_file *s = data;
5629
5630 if (!rdev->supply)
5631 regulator_summary_show_subtree(s, rdev, 0);
5632
5633 return 0;
5634}
5635
5636static int regulator_summary_show(struct seq_file *s, void *data)
5637{
5638 struct ww_acquire_ctx ww_ctx;
5639
5640 seq_puts(s, " regulator use open bypass opmode voltage current min max\n");
5641 seq_puts(s, "---------------------------------------------------------------------------------------\n");
5642
5643 regulator_summary_lock(&ww_ctx);
5644
5645 class_for_each_device(®ulator_class, NULL, s,
5646 regulator_summary_show_roots);
5647
5648 regulator_summary_unlock(&ww_ctx);
5649
5650 return 0;
5651}
5652DEFINE_SHOW_ATTRIBUTE(regulator_summary);
5653#endif
5654
5655static int __init regulator_init(void)
5656{
5657 int ret;
5658
5659 ret = class_register(®ulator_class);
5660
5661 debugfs_root = debugfs_create_dir("regulator", NULL);
5662 if (!debugfs_root)
5663 pr_warn("regulator: Failed to create debugfs directory\n");
5664
5665#ifdef CONFIG_DEBUG_FS
5666 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
5667 &supply_map_fops);
5668
5669 debugfs_create_file("regulator_summary", 0444, debugfs_root,
5670 NULL, ®ulator_summary_fops);
5671#endif
5672 regulator_dummy_init();
5673
5674 regulator_coupler_register(&generic_regulator_coupler);
5675
5676 return ret;
5677}
5678
5679
5680core_initcall(regulator_init);
5681
5682static int regulator_late_cleanup(struct device *dev, void *data)
5683{
5684 struct regulator_dev *rdev = dev_to_rdev(dev);
5685 const struct regulator_ops *ops = rdev->desc->ops;
5686 struct regulation_constraints *c = rdev->constraints;
5687 int enabled, ret;
5688
5689 if (c && c->always_on)
5690 return 0;
5691
5692 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
5693 return 0;
5694
5695 regulator_lock(rdev);
5696
5697 if (rdev->use_count)
5698 goto unlock;
5699
5700
5701 if (ops->is_enabled)
5702 enabled = ops->is_enabled(rdev);
5703 else
5704 enabled = 1;
5705
5706 if (!enabled)
5707 goto unlock;
5708
5709 if (have_full_constraints()) {
5710
5711
5712 rdev_info(rdev, "disabling\n");
5713 ret = _regulator_do_disable(rdev);
5714 if (ret != 0)
5715 rdev_err(rdev, "couldn't disable: %d\n", ret);
5716 } else {
5717
5718
5719
5720
5721
5722 rdev_warn(rdev, "incomplete constraints, leaving on\n");
5723 }
5724
5725unlock:
5726 regulator_unlock(rdev);
5727
5728 return 0;
5729}
5730
5731static void regulator_init_complete_work_function(struct work_struct *work)
5732{
5733
5734
5735
5736
5737
5738
5739
5740 class_for_each_device(®ulator_class, NULL, NULL,
5741 regulator_register_resolve_supply);
5742
5743
5744
5745
5746
5747
5748 class_for_each_device(®ulator_class, NULL, NULL,
5749 regulator_late_cleanup);
5750}
5751
5752static DECLARE_DELAYED_WORK(regulator_init_complete_work,
5753 regulator_init_complete_work_function);
5754
5755static int __init regulator_init_complete(void)
5756{
5757
5758
5759
5760
5761
5762
5763 if (of_have_populated_dt())
5764 has_full_constraints = true;
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777 schedule_delayed_work(®ulator_init_complete_work,
5778 msecs_to_jiffies(30000));
5779
5780 return 0;
5781}
5782late_initcall_sync(regulator_init_complete);
5783