개념 설명 전체
drivers/regulator/core.c · v6.18.37
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 //
3 // core.c -- Voltage/Current Regulator framework.
4 //
5 // Copyright 2007, 2008 Wolfson Microelectronics PLC.
6 // Copyright 2008 SlimLogic Ltd.
7 //
8 // Author: Liam Girdwood <[email protected]>
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/reboot.h>
23 #include <linux/regmap.h>
24 #include <linux/regulator/of_regulator.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/regulator/coupler.h>
27 #include <linux/regulator/driver.h>
28 #include <linux/regulator/machine.h>
29 #include <linux/module.h>
30
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/regulator.h>
33
34 #include "dummy.h"
35 #include "internal.h"
36 #include "regnl.h"
37
38 static DEFINE_WW_CLASS(regulator_ww_class);
39 static DEFINE_MUTEX(regulator_nesting_mutex);
40 static DEFINE_MUTEX(regulator_list_mutex);
41 static LIST_HEAD(regulator_map_list);
42 static LIST_HEAD(regulator_ena_gpio_list);
43 static LIST_HEAD(regulator_supply_alias_list);
44 static LIST_HEAD(regulator_coupler_list);
45 static bool has_full_constraints;
46
47 static struct dentry *debugfs_root;
48
49 /*
50 * struct regulator_map
51 *
52 * Used to provide symbolic supply names to devices.
53 */
54 struct regulator_map {
55 struct list_head list;
56 const char *dev_name; /* The dev_name() for the consumer */
57 const char *supply;
58 struct regulator_dev *regulator;
59 };
60
61 /*
62 * struct regulator_enable_gpio
63 *
64 * Management for shared enable GPIO pin
65 */
66 struct regulator_enable_gpio {
67 struct list_head list;
68 struct gpio_desc *gpiod;
69 u32 enable_count; /* a number of enabled shared GPIO */
70 u32 request_count; /* a number of requested shared GPIO */
71 };
72
73 /*
74 * struct regulator_supply_alias
75 *
76 * Used to map lookups for a supply onto an alternative device.
77 */
78 struct regulator_supply_alias {
79 struct list_head list;
80 struct device *src_dev;
81 const char *src_supply;
82 struct device *alias_dev;
83 const char *alias_supply;
84 };
85
86 static int _regulator_is_enabled(struct regulator_dev *rdev);
87 static int _regulator_disable(struct regulator *regulator);
88 static int _regulator_get_error_flags(struct regulator_dev *rdev, unsigned int *flags);
89 static int _regulator_get_current_limit(struct regulator_dev *rdev);
90 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
91 static int _notifier_call_chain(struct regulator_dev *rdev,
92 unsigned long event, void *data);
93 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
94 int min_uV, int max_uV);
95 static int regulator_balance_voltage(struct regulator_dev *rdev,
96 suspend_state_t state);
97 static struct regulator *create_regulator(struct regulator_dev *rdev,
98 struct device *dev,
99 const char *supply_name);
100 static void destroy_regulator(struct regulator *regulator);
101 static void _regulator_put(struct regulator *regulator);
102
103 const char *rdev_get_name(struct regulator_dev *rdev)
104 {
105 if (rdev->constraints && rdev->constraints->name)
106 return rdev->constraints->name;
107 else if (rdev->desc->name)
108 return rdev->desc->name;
109 else
110 return "";
111 }
112 EXPORT_SYMBOL_GPL(rdev_get_name);
113
114 static bool have_full_constraints(void)
115 {
116 return has_full_constraints || of_have_populated_dt();
117 }
118
119 static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
120 {
121 if (!rdev->constraints) {
122 rdev_err(rdev, "no constraints\n");
123 return false;
124 }
125
126 if (rdev->constraints->valid_ops_mask & ops)
127 return true;
128
129 return false;
130 }
131
132 /**
133 * regulator_lock_nested - lock a single regulator
134 * @rdev: regulator source
135 * @ww_ctx: w/w mutex acquire context
136 *
137 * This function can be called many times by one task on
138 * a single regulator and its mutex will be locked only
139 * once. If a task, which is calling this function is other
140 * than the one, which initially locked the mutex, it will
141 * wait on mutex.
142 *
143 * Return: 0 on success or a negative error number on failure.
144 */
145 static inline int regulator_lock_nested(struct regulator_dev *rdev,
146 struct ww_acquire_ctx *ww_ctx)
147 {
148 bool lock = false;
149 int ret = 0;
150
151 mutex_lock(®ulator_nesting_mutex);
152
153 if (!ww_mutex_trylock(&rdev->mutex, ww_ctx)) {
154 if (rdev->mutex_owner == current)
155 rdev->ref_cnt++;
156 else
157 lock = true;
158
159 if (lock) {
160 mutex_unlock(®ulator_nesting_mutex);
161 ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
162 mutex_lock(®ulator_nesting_mutex);
163 }
164 } else {
165 lock = true;
166 }
167
168 if (lock && ret != -EDEADLK) {
169 rdev->ref_cnt++;
170 rdev->mutex_owner = current;
171 }
172
173 mutex_unlock(®ulator_nesting_mutex);
174
175 return ret;
176 }
177
178 /**
179 * regulator_lock - lock a single regulator
180 * @rdev: regulator source
181 *
182 * This function can be called many times by one task on
183 * a single regulator and its mutex will be locked only
184 * once. If a task, which is calling this function is other
185 * than the one, which initially locked the mutex, it will
186 * wait on mutex.
187 */
188 static void regulator_lock(struct regulator_dev *rdev)
189 {
190 regulator_lock_nested(rdev, NULL);
191 }
192
193 /**
194 * regulator_unlock - unlock a single regulator
195 * @rdev: regulator_source
196 *
197 * This function unlocks the mutex when the
198 * reference counter reaches 0.
199 */
200 static void regulator_unlock(struct regulator_dev *rdev)
201 {
202 mutex_lock(®ulator_nesting_mutex);
203
204 if (--rdev->ref_cnt == 0) {
205 rdev->mutex_owner = NULL;
206 ww_mutex_unlock(&rdev->mutex);
207 }
208
209 WARN_ON_ONCE(rdev->ref_cnt < 0);
210
211 mutex_unlock(®ulator_nesting_mutex);
212 }
213
214 /**
215 * regulator_lock_two - lock two regulators
216 * @rdev1: first regulator
217 * @rdev2: second regulator
218 * @ww_ctx: w/w mutex acquire context
219 *
220 * Locks both rdevs using the regulator_ww_class.
221 */
222 static void regulator_lock_two(struct regulator_dev *rdev1,
223 struct regulator_dev *rdev2,
224 struct ww_acquire_ctx *ww_ctx)
225 {
226 struct regulator_dev *held, *contended;
227 int ret;
228
229 ww_acquire_init(ww_ctx, ®ulator_ww_class);
230
231 /* Try to just grab both of them */
232 ret = regulator_lock_nested(rdev1, ww_ctx);
233 WARN_ON(ret);
234 ret = regulator_lock_nested(rdev2, ww_ctx);
235 if (ret != -EDEADLOCK) {
236 WARN_ON(ret);
237 goto exit;
238 }
239
240 held = rdev1;
241 contended = rdev2;
242 while (true) {
243 regulator_unlock(held);
244
245 ww_mutex_lock_slow(&contended->mutex, ww_ctx);
246 contended->ref_cnt++;
247 contended->mutex_owner = current;
248 swap(held, contended);
249 ret = regulator_lock_nested(contended, ww_ctx);
250
251 if (ret != -EDEADLOCK) {
252 WARN_ON(ret);
253 break;
254 }
255 }
256
257 exit:
258 ww_acquire_done(ww_ctx);
259 }
260
261 /**
262 * regulator_unlock_two - unlock two regulators
263 * @rdev1: first regulator
264 * @rdev2: second regulator
265 * @ww_ctx: w/w mutex acquire context
266 *
267 * The inverse of regulator_lock_two().
268 */
269
270 static void regulator_unlock_two(struct regulator_dev *rdev1,
271 struct regulator_dev *rdev2,
272 struct ww_acquire_ctx *ww_ctx)
273 {
274 regulator_unlock(rdev2);
275 regulator_unlock(rdev1);
276 ww_acquire_fini(ww_ctx);
277 }
278
279 static bool regulator_supply_is_couple(struct regulator_dev *rdev)
280 {
281 struct regulator_dev *c_rdev;
282 int i;
283
284 for (i = 1; i < rdev->coupling_desc.n_coupled; i++) {
285 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
286
287 if (rdev->supply->rdev == c_rdev)
288 return true;
289 }
290
291 return false;
292 }
293
294 static void regulator_unlock_recursive(struct regulator_dev *rdev,
295 unsigned int n_coupled)
296 {
297 struct regulator_dev *c_rdev, *supply_rdev;
298 int i, supply_n_coupled;
299
300 for (i = n_coupled; i > 0; i--) {
301 c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1];
302
303 if (!c_rdev)
304 continue;
305
306 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
307 supply_rdev = c_rdev->supply->rdev;
308 supply_n_coupled = supply_rdev->coupling_desc.n_coupled;
309
310 regulator_unlock_recursive(supply_rdev,
311 supply_n_coupled);
312 }
313
314 regulator_unlock(c_rdev);
315 }
316 }
317
318 static int regulator_lock_recursive(struct regulator_dev *rdev,
319 struct regulator_dev **new_contended_rdev,
320 struct regulator_dev **old_contended_rdev,
321 struct ww_acquire_ctx *ww_ctx)
322 {
323 struct regulator_dev *c_rdev;
324 int i, err;
325
326 for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
327 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
328
329 if (!c_rdev)
330 continue;
331
332 if (c_rdev != *old_contended_rdev) {
333 err = regulator_lock_nested(c_rdev, ww_ctx);
334 if (err) {
335 if (err == -EDEADLK) {
336 *new_contended_rdev = c_rdev;
337 goto err_unlock;
338 }
339
340 /* shouldn't happen */
341 WARN_ON_ONCE(err != -EALREADY);
342 }
343 } else {
344 *old_contended_rdev = NULL;
345 }
346
347 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
348 err = regulator_lock_recursive(c_rdev->supply->rdev,
349 new_contended_rdev,
350 old_contended_rdev,
351 ww_ctx);
352 if (err) {
353 regulator_unlock(c_rdev);
354 goto err_unlock;
355 }
356 }
357 }
358
359 return 0;
360
361 err_unlock:
362 regulator_unlock_recursive(rdev, i);
363
364 return err;
365 }
366
367 /**
368 * regulator_unlock_dependent - unlock regulator's suppliers and coupled
369 * regulators
370 * @rdev: regulator source
371 * @ww_ctx: w/w mutex acquire context
372 *
373 * Unlock all regulators related with rdev by coupling or supplying.
374 */
375 static void regulator_unlock_dependent(struct regulator_dev *rdev,
376 struct ww_acquire_ctx *ww_ctx)
377 {
378 regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled);
379 ww_acquire_fini(ww_ctx);
380 }
381
382 /**
383 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
384 * @rdev: regulator source
385 * @ww_ctx: w/w mutex acquire context
386 *
387 * This function as a wrapper on regulator_lock_recursive(), which locks
388 * all regulators related with rdev by coupling or supplying.
389 */
390 static void regulator_lock_dependent(struct regulator_dev *rdev,
391 struct ww_acquire_ctx *ww_ctx)
392 {
393 struct regulator_dev *new_contended_rdev = NULL;
394 struct regulator_dev *old_contended_rdev = NULL;
395 int err;
396
397 mutex_lock(®ulator_list_mutex);
398
399 ww_acquire_init(ww_ctx, ®ulator_ww_class);
400
401 do {
402 if (new_contended_rdev) {
403 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
404 old_contended_rdev = new_contended_rdev;
405 old_contended_rdev->ref_cnt++;
406 old_contended_rdev->mutex_owner = current;
407 }
408
409 err = regulator_lock_recursive(rdev,
410 &new_contended_rdev,
411 &old_contended_rdev,
412 ww_ctx);
413
414 if (old_contended_rdev)
415 regulator_unlock(old_contended_rdev);
416
417 } while (err == -EDEADLK);
418
419 ww_acquire_done(ww_ctx);
420
421 mutex_unlock(®ulator_list_mutex);
422 }
423
424 /* Platform voltage constraint check */
425 int regulator_check_voltage(struct regulator_dev *rdev,
426 int *min_uV, int *max_uV)
427 {
428 BUG_ON(*min_uV > *max_uV);
429
430 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
431 rdev_err(rdev, "voltage operation not allowed\n");
432 return -EPERM;
433 }
434
435 if (*max_uV > rdev->constraints->max_uV)
436 *max_uV = rdev->constraints->max_uV;
437 if (*min_uV < rdev->constraints->min_uV)
438 *min_uV = rdev->constraints->min_uV;
439
440 if (*min_uV > *max_uV) {
441 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
442 *min_uV, *max_uV);
443 return -EINVAL;
444 }
445
446 return 0;
447 }
448
449 /* return 0 if the state is valid */
450 static int regulator_check_states(suspend_state_t state)
451 {
452 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
453 }
454
455 /* Make sure we select a voltage that suits the needs of all
456 * regulator consumers
457 */
458 int regulator_check_consumers(struct regulator_dev *rdev,
459 int *min_uV, int *max_uV,
460 suspend_state_t state)
461 {
462 struct regulator *regulator;
463 struct regulator_voltage *voltage;
464
465 list_for_each_entry(regulator, &rdev->consumer_list, list) {
466 voltage = ®ulator->voltage[state];
467 /*
468 * Assume consumers that didn't say anything are OK
469 * with anything in the constraint range.
470 */
471 if (!voltage->min_uV && !voltage->max_uV)
472 continue;
473
474 if (*max_uV > voltage->max_uV)
475 *max_uV = voltage->max_uV;
476 if (*min_uV < voltage->min_uV)
477 *min_uV = voltage->min_uV;
478 }
479
480 if (*min_uV > *max_uV) {
481 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
482 *min_uV, *max_uV);
483 return -EINVAL;
484 }
485
486 return 0;
487 }
488
489 /* current constraint check */
490 static int regulator_check_current_limit(struct regulator_dev *rdev,
491 int *min_uA, int *max_uA)
492 {
493 BUG_ON(*min_uA > *max_uA);
494
495 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
496 rdev_err(rdev, "current operation not allowed\n");
497 return -EPERM;
498 }
499
500 if (*max_uA > rdev->constraints->max_uA &&
501 rdev->constraints->max_uA)
502 *max_uA = rdev->constraints->max_uA;
503 if (*min_uA < rdev->constraints->min_uA)
504 *min_uA = rdev->constraints->min_uA;
505
506 if (*min_uA > *max_uA) {
507 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
508 *min_uA, *max_uA);
509 return -EINVAL;
510 }
511
512 return 0;
513 }
514
515 /* operating mode constraint check */
516 static int regulator_mode_constrain(struct regulator_dev *rdev,
517 unsigned int *mode)
518 {
519 switch (*mode) {
520 case REGULATOR_MODE_FAST:
521 case REGULATOR_MODE_NORMAL:
522 case REGULATOR_MODE_IDLE:
523 case REGULATOR_MODE_STANDBY:
524 break;
525 default:
526 rdev_err(rdev, "invalid mode %x specified\n", *mode);
527 return -EINVAL;
528 }
529
530 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
531 rdev_err(rdev, "mode operation not allowed\n");
532 return -EPERM;
533 }
534
535 /* The modes are bitmasks, the most power hungry modes having
536 * the lowest values. If the requested mode isn't supported
537 * try higher modes.
538 */
539 while (*mode) {
540 if (rdev->constraints->valid_modes_mask & *mode)
541 return 0;
542 *mode /= 2;
543 }
544
545 return -EINVAL;
546 }
547
548 static inline struct regulator_state *
549 regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
550 {
551 if (rdev->constraints == NULL)
552 return NULL;
553
554 switch (state) {
555 case PM_SUSPEND_STANDBY:
556 return &rdev->constraints->state_standby;
557 case PM_SUSPEND_MEM:
558 return &rdev->constraints->state_mem;
559 case PM_SUSPEND_MAX:
560 return &rdev->constraints->state_disk;
561 default:
562 return NULL;
563 }
564 }
565
566 static const struct regulator_state *
567 regulator_get_suspend_state_check(struct regulator_dev *rdev, suspend_state_t state)
568 {
569 const struct regulator_state *rstate;
570
571 rstate = regulator_get_suspend_state(rdev, state);
572 if (rstate == NULL)
573 return NULL;
574
575 /* If we have no suspend mode configuration don't set anything;
576 * only warn if the driver implements set_suspend_voltage or
577 * set_suspend_mode callback.
578 */
579 if (rstate->enabled != ENABLE_IN_SUSPEND &&
580 rstate->enabled != DISABLE_IN_SUSPEND) {
581 if (rdev->desc->ops->set_suspend_voltage ||
582 rdev->desc->ops->set_suspend_mode)
583 rdev_warn(rdev, "No configuration\n");
584 return NULL;
585 }
586
587 return rstate;
588 }
589
590 static ssize_t microvolts_show(struct device *dev,
591 struct device_attribute *attr, char *buf)
592 {
593 struct regulator_dev *rdev = dev_get_drvdata(dev);
594 int uV;
595
596 regulator_lock(rdev);
597 uV = regulator_get_voltage_rdev(rdev);
598 regulator_unlock(rdev);
599
600 if (uV < 0)
601 return uV;
602 return sprintf(buf, "%d\n", uV);
603 }
604 static DEVICE_ATTR_RO(microvolts);
605
606 static ssize_t microamps_show(struct device *dev,
607 struct device_attribute *attr, char *buf)
608 {
609 struct regulator_dev *rdev = dev_get_drvdata(dev);
610
611 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
612 }
613 static DEVICE_ATTR_RO(microamps);
614
615 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
616 char *buf)
617 {
618 struct regulator_dev *rdev = dev_get_drvdata(dev);
619
620 return sprintf(buf, "%s\n", rdev_get_name(rdev));
621 }
622 static DEVICE_ATTR_RO(name);
623
624 static const char *regulator_opmode_to_str(int mode)
625 {
626 switch (mode) {
627 case REGULATOR_MODE_FAST:
628 return "fast";
629 case REGULATOR_MODE_NORMAL:
630 return "normal";
631 case REGULATOR_MODE_IDLE:
632 return "idle";
633 case REGULATOR_MODE_STANDBY:
634 return "standby";
635 }
636 return "unknown";
637 }
638
639 static ssize_t regulator_print_opmode(char *buf, int mode)
640 {
641 return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
642 }
643
644 static ssize_t opmode_show(struct device *dev,
645 struct device_attribute *attr, char *buf)
646 {
647 struct regulator_dev *rdev = dev_get_drvdata(dev);
648
649 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
650 }
651 static DEVICE_ATTR_RO(opmode);
652
653 static ssize_t regulator_print_state(char *buf, int state)
654 {
655 if (state > 0)
656 return sprintf(buf, "enabled\n");
657 else if (state == 0)
658 return sprintf(buf, "disabled\n");
659 else
660 return sprintf(buf, "unknown\n");
661 }
662
663 static ssize_t state_show(struct device *dev,
664 struct device_attribute *attr, char *buf)
665 {
666 struct regulator_dev *rdev = dev_get_drvdata(dev);
667 ssize_t ret;
668
669 regulator_lock(rdev);
670 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
671 regulator_unlock(rdev);
672
673 return ret;
674 }
675 static DEVICE_ATTR_RO(state);
676
677 static ssize_t status_show(struct device *dev,
678 struct device_attribute *attr, char *buf)
679 {
680 struct regulator_dev *rdev = dev_get_drvdata(dev);
681 int status;
682 char *label;
683
684 status = rdev->desc->ops->get_status(rdev);
685 if (status < 0)
686 return status;
687
688 switch (status) {
689 case REGULATOR_STATUS_OFF:
690 label = "off";
691 break;
692 case REGULATOR_STATUS_ON:
693 label = "on";
694 break;
695 case REGULATOR_STATUS_ERROR:
696 label = "error";
697 break;
698 case REGULATOR_STATUS_FAST:
699 label = "fast";
700 break;
701 case REGULATOR_STATUS_NORMAL:
702 label = "normal";
703 break;
704 case REGULATOR_STATUS_IDLE:
705 label = "idle";
706 break;
707 case REGULATOR_STATUS_STANDBY:
708 label = "standby";
709 break;
710 case REGULATOR_STATUS_BYPASS:
711 label = "bypass";
712 break;
713 case REGULATOR_STATUS_UNDEFINED:
714 label = "undefined";
715 break;
716 default:
717 return -ERANGE;
718 }
719
720 return sprintf(buf, "%s\n", label);
721 }
722 static DEVICE_ATTR_RO(status);
723
724 static ssize_t min_microamps_show(struct device *dev,
725 struct device_attribute *attr, char *buf)
726 {
727 struct regulator_dev *rdev = dev_get_drvdata(dev);
728
729 if (!rdev->constraints)
730 return sprintf(buf, "constraint not defined\n");
731
732 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
733 }
734 static DEVICE_ATTR_RO(min_microamps);
735
736 static ssize_t max_microamps_show(struct device *dev,
737 struct device_attribute *attr, char *buf)
738 {
739 struct regulator_dev *rdev = dev_get_drvdata(dev);
740
741 if (!rdev->constraints)
742 return sprintf(buf, "constraint not defined\n");
743
744 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
745 }
746 static DEVICE_ATTR_RO(max_microamps);
747
748 static ssize_t min_microvolts_show(struct device *dev,
749 struct device_attribute *attr, char *buf)
750 {
751 struct regulator_dev *rdev = dev_get_drvdata(dev);
752
753 if (!rdev->constraints)
754 return sprintf(buf, "constraint not defined\n");
755
756 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
757 }
758 static DEVICE_ATTR_RO(min_microvolts);
759
760 static ssize_t max_microvolts_show(struct device *dev,
761 struct device_attribute *attr, char *buf)
762 {
763 struct regulator_dev *rdev = dev_get_drvdata(dev);
764
765 if (!rdev->constraints)
766 return sprintf(buf, "constraint not defined\n");
767
768 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
769 }
770 static DEVICE_ATTR_RO(max_microvolts);
771
772 static ssize_t requested_microamps_show(struct device *dev,
773 struct device_attribute *attr, char *buf)
774 {
775 struct regulator_dev *rdev = dev_get_drvdata(dev);
776 struct regulator *regulator;
777 int uA = 0;
778
779 regulator_lock(rdev);
780 list_for_each_entry(regulator, &rdev->consumer_list, list) {
781 if (regulator->enable_count)
782 uA += regulator->uA_load;
783 }
784 regulator_unlock(rdev);
785 return sprintf(buf, "%d\n", uA);
786 }
787 static DEVICE_ATTR_RO(requested_microamps);
788
789 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
790 char *buf)
791 {
792 struct regulator_dev *rdev = dev_get_drvdata(dev);
793 return sprintf(buf, "%d\n", rdev->use_count);
794 }
795 static DEVICE_ATTR_RO(num_users);
796
797 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
798 char *buf)
799 {
800 struct regulator_dev *rdev = dev_get_drvdata(dev);
801
802 switch (rdev->desc->type) {
803 case REGULATOR_VOLTAGE:
804 return sprintf(buf, "voltage\n");
805 case REGULATOR_CURRENT:
806 return sprintf(buf, "current\n");
807 }
808 return sprintf(buf, "unknown\n");
809 }
810 static DEVICE_ATTR_RO(type);
811
812 static ssize_t suspend_mem_microvolts_show(struct device *dev,
813 struct device_attribute *attr, char *buf)
814 {
815 struct regulator_dev *rdev = dev_get_drvdata(dev);
816
817 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
818 }
819 static DEVICE_ATTR_RO(suspend_mem_microvolts);
820
821 static ssize_t suspend_disk_microvolts_show(struct device *dev,
822 struct device_attribute *attr, char *buf)
823 {
824 struct regulator_dev *rdev = dev_get_drvdata(dev);
825
826 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
827 }
828 static DEVICE_ATTR_RO(suspend_disk_microvolts);
829
830 static ssize_t suspend_standby_microvolts_show(struct device *dev,
831 struct device_attribute *attr, char *buf)
832 {
833 struct regulator_dev *rdev = dev_get_drvdata(dev);
834
835 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
836 }
837 static DEVICE_ATTR_RO(suspend_standby_microvolts);
838
839 static ssize_t suspend_mem_mode_show(struct device *dev,
840 struct device_attribute *attr, char *buf)
841 {
842 struct regulator_dev *rdev = dev_get_drvdata(dev);
843
844 return regulator_print_opmode(buf,
845 rdev->constraints->state_mem.mode);
846 }
847 static DEVICE_ATTR_RO(suspend_mem_mode);
848
849 static ssize_t suspend_disk_mode_show(struct device *dev,
850 struct device_attribute *attr, char *buf)
851 {
852 struct regulator_dev *rdev = dev_get_drvdata(dev);
853
854 return regulator_print_opmode(buf,
855 rdev->constraints->state_disk.mode);
856 }
857 static DEVICE_ATTR_RO(suspend_disk_mode);
858
859 static ssize_t suspend_standby_mode_show(struct device *dev,
860 struct device_attribute *attr, char *buf)
861 {
862 struct regulator_dev *rdev = dev_get_drvdata(dev);
863
864 return regulator_print_opmode(buf,
865 rdev->constraints->state_standby.mode);
866 }
867 static DEVICE_ATTR_RO(suspend_standby_mode);
868
869 static ssize_t suspend_mem_state_show(struct device *dev,
870 struct device_attribute *attr, char *buf)
871 {
872 struct regulator_dev *rdev = dev_get_drvdata(dev);
873
874 return regulator_print_state(buf,
875 rdev->constraints->state_mem.enabled);
876 }
877 static DEVICE_ATTR_RO(suspend_mem_state);
878
879 static ssize_t suspend_disk_state_show(struct device *dev,
880 struct device_attribute *attr, char *buf)
881 {
882 struct regulator_dev *rdev = dev_get_drvdata(dev);
883
884 return regulator_print_state(buf,
885 rdev->constraints->state_disk.enabled);
886 }
887 static DEVICE_ATTR_RO(suspend_disk_state);
888
889 static ssize_t suspend_standby_state_show(struct device *dev,
890 struct device_attribute *attr, char *buf)
891 {
892 struct regulator_dev *rdev = dev_get_drvdata(dev);
893
894 return regulator_print_state(buf,
895 rdev->constraints->state_standby.enabled);
896 }
897 static DEVICE_ATTR_RO(suspend_standby_state);
898
899 static ssize_t bypass_show(struct device *dev,
900 struct device_attribute *attr, char *buf)
901 {
902 struct regulator_dev *rdev = dev_get_drvdata(dev);
903 const char *report;
904 bool bypass;
905 int ret;
906
907 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
908
909 if (ret != 0)
910 report = "unknown";
911 else if (bypass)
912 report = "enabled";
913 else
914 report = "disabled";
915
916 return sprintf(buf, "%s\n", report);
917 }
918 static DEVICE_ATTR_RO(bypass);
919
920 static ssize_t power_budget_milliwatt_show(struct device *dev,
921 struct device_attribute *attr,
922 char *buf)
923 {
924 struct regulator_dev *rdev = dev_get_drvdata(dev);
925
926 return sprintf(buf, "%d\n", rdev->constraints->pw_budget_mW);
927 }
928 static DEVICE_ATTR_RO(power_budget_milliwatt);
929
930 static ssize_t power_requested_milliwatt_show(struct device *dev,
931 struct device_attribute *attr,
932 char *buf)
933 {
934 struct regulator_dev *rdev = dev_get_drvdata(dev);
935
936 return sprintf(buf, "%d\n", rdev->pw_requested_mW);
937 }
938 static DEVICE_ATTR_RO(power_requested_milliwatt);
939
940 #define REGULATOR_ERROR_ATTR(name, bit) \
941 static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \
942 char *buf) \
943 { \
944 int ret; \
945 unsigned int flags; \
946 struct regulator_dev *rdev = dev_get_drvdata(dev); \
947 ret = _regulator_get_error_flags(rdev, &flags); \
948 if (ret) \
949 return ret; \
950 return sysfs_emit(buf, "%d\n", !!(flags & (bit))); \
951 } \
952 static DEVICE_ATTR_RO(name)
953
954 REGULATOR_ERROR_ATTR(under_voltage, REGULATOR_ERROR_UNDER_VOLTAGE);
955 REGULATOR_ERROR_ATTR(over_current, REGULATOR_ERROR_OVER_CURRENT);
956 REGULATOR_ERROR_ATTR(regulation_out, REGULATOR_ERROR_REGULATION_OUT);
957 REGULATOR_ERROR_ATTR(fail, REGULATOR_ERROR_FAIL);
958 REGULATOR_ERROR_ATTR(over_temp, REGULATOR_ERROR_OVER_TEMP);
959 REGULATOR_ERROR_ATTR(under_voltage_warn, REGULATOR_ERROR_UNDER_VOLTAGE_WARN);
960 REGULATOR_ERROR_ATTR(over_current_warn, REGULATOR_ERROR_OVER_CURRENT_WARN);
961 REGULATOR_ERROR_ATTR(over_voltage_warn, REGULATOR_ERROR_OVER_VOLTAGE_WARN);
962 REGULATOR_ERROR_ATTR(over_temp_warn, REGULATOR_ERROR_OVER_TEMP_WARN);
963
964 /* Calculate the new optimum regulator operating mode based on the new total
965 * consumer load. All locks held by caller
966 */
967 static int drms_uA_update(struct regulator_dev *rdev)
968 {
969 struct regulator *sibling;
970 int current_uA = 0, output_uV, input_uV, err;
971 unsigned int mode;
972
973 /*
974 * first check to see if we can set modes at all, otherwise just
975 * tell the consumer everything is OK.
976 */
977 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) {
978 rdev_dbg(rdev, "DRMS operation not allowed\n");
979 return 0;
980 }
981
982 if (!rdev->desc->ops->get_optimum_mode &&
983 !rdev->desc->ops->set_load)
984 return 0;
985
986 if (!rdev->desc->ops->set_mode &&
987 !rdev->desc->ops->set_load)
988 return -EINVAL;
989
990 /* calc total requested load */
991 list_for_each_entry(sibling, &rdev->consumer_list, list) {
992 if (sibling->enable_count)
993 current_uA += sibling->uA_load;
994 }
995
996 current_uA += rdev->constraints->system_load;
997
998 if (rdev->desc->ops->set_load) {
999 /* set the optimum mode for our new total regulator load */
1000 err = rdev->desc->ops->set_load(rdev, current_uA);
1001 if (err < 0)
1002 rdev_err(rdev, "failed to set load %d: %pe\n",
1003 current_uA, ERR_PTR(err));
1004 } else {
1005 /*
1006 * Unfortunately in some cases the constraints->valid_ops has
1007 * REGULATOR_CHANGE_DRMS but there are no valid modes listed.
1008 * That's not really legit but we won't consider it a fatal
1009 * error here. We'll treat it as if REGULATOR_CHANGE_DRMS
1010 * wasn't set.
1011 */
1012 if (!rdev->constraints->valid_modes_mask) {
1013 rdev_dbg(rdev, "Can change modes; but no valid mode\n");
1014 return 0;
1015 }
1016
1017 /* get output voltage */
1018 output_uV = regulator_get_voltage_rdev(rdev);
1019
1020 /*
1021 * Don't return an error; if regulator driver cares about
1022 * output_uV then it's up to the driver to validate.
1023 */
1024 if (output_uV <= 0)
1025 rdev_dbg(rdev, "invalid output voltage found\n");
1026
1027 /* get input voltage */
1028 input_uV = 0;
1029 if (rdev->supply)
1030 input_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
1031 if (input_uV <= 0)
1032 input_uV = rdev->constraints->input_uV;
1033
1034 /*
1035 * Don't return an error; if regulator driver cares about
1036 * input_uV then it's up to the driver to validate.
1037 */
1038 if (input_uV <= 0)
1039 rdev_dbg(rdev, "invalid input voltage found\n");
1040
1041 /* now get the optimum mode for our new total regulator load */
1042 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
1043 output_uV, current_uA);
1044
1045 /* check the new mode is allowed */
1046 err = regulator_mode_constrain(rdev, &mode);
1047 if (err < 0) {
1048 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV: %pe\n",
1049 current_uA, input_uV, output_uV, ERR_PTR(err));
1050 return err;
1051 }
1052
1053 err = rdev->desc->ops->set_mode(rdev, mode);
1054 if (err < 0)
1055 rdev_err(rdev, "failed to set optimum mode %x: %pe\n",
1056 mode, ERR_PTR(err));
1057 }
1058
1059 return err;
1060 }
1061
1062 static int __suspend_set_state(struct regulator_dev *rdev,
1063 const struct regulator_state *rstate)
1064 {
1065 int ret = 0;
1066
1067 if (rstate->enabled == ENABLE_IN_SUSPEND &&
1068 rdev->desc->ops->set_suspend_enable)
1069 ret = rdev->desc->ops->set_suspend_enable(rdev);
1070 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
1071 rdev->desc->ops->set_suspend_disable)
1072 ret = rdev->desc->ops->set_suspend_disable(rdev);
1073 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
1074 ret = 0;
1075
1076 if (ret < 0) {
1077 rdev_err(rdev, "failed to enabled/disable: %pe\n", ERR_PTR(ret));
1078 return ret;
1079 }
1080
1081 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
1082 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
1083 if (ret < 0) {
1084 rdev_err(rdev, "failed to set voltage: %pe\n", ERR_PTR(ret));
1085 return ret;
1086 }
1087 }
1088
1089 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
1090 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
1091 if (ret < 0) {
1092 rdev_err(rdev, "failed to set mode: %pe\n", ERR_PTR(ret));
1093 return ret;
1094 }
1095 }
1096
1097 return ret;
1098 }
1099
1100 static int suspend_set_initial_state(struct regulator_dev *rdev)
1101 {
1102 const struct regulator_state *rstate;
1103
1104 rstate = regulator_get_suspend_state_check(rdev,
1105 rdev->constraints->initial_state);
1106 if (!rstate)
1107 return 0;
1108
1109 return __suspend_set_state(rdev, rstate);
1110 }
1111
1112 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
1113 static void print_constraints_debug(struct regulator_dev *rdev)
1114 {
1115 struct regulation_constraints *constraints = rdev->constraints;
1116 char buf[160] = "";
1117 size_t len = sizeof(buf) - 1;
1118 int count = 0;
1119 int ret;
1120
1121 if (constraints->min_uV && constraints->max_uV) {
1122 if (constraints->min_uV == constraints->max_uV)
1123 count += scnprintf(buf + count, len - count, "%d mV ",
1124 constraints->min_uV / 1000);
1125 else
1126 count += scnprintf(buf + count, len - count,
1127 "%d <--> %d mV ",
1128 constraints->min_uV / 1000,
1129 constraints->max_uV / 1000);
1130 }
1131
1132 if (!constraints->min_uV ||
1133 constraints->min_uV != constraints->max_uV) {
1134 ret = regulator_get_voltage_rdev(rdev);
1135 if (ret > 0)
1136 count += scnprintf(buf + count, len - count,
1137 "at %d mV ", ret / 1000);
1138 }
1139
1140 if (constraints->uV_offset)
1141 count += scnprintf(buf + count, len - count, "%dmV offset ",
1142 constraints->uV_offset / 1000);
1143
1144 if (constraints->min_uA && constraints->max_uA) {
1145 if (constraints->min_uA == constraints->max_uA)
1146 count += scnprintf(buf + count, len - count, "%d mA ",
1147 constraints->min_uA / 1000);
1148 else
1149 count += scnprintf(buf + count, len - count,
1150 "%d <--> %d mA ",
1151 constraints->min_uA / 1000,
1152 constraints->max_uA / 1000);
1153 }
1154
1155 if (!constraints->min_uA ||
1156 constraints->min_uA != constraints->max_uA) {
1157 ret = _regulator_get_current_limit(rdev);
1158 if (ret > 0)
1159 count += scnprintf(buf + count, len - count,
1160 "at %d mA ", ret / 1000);
1161 }
1162
1163 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
1164 count += scnprintf(buf + count, len - count, "fast ");
1165 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
1166 count += scnprintf(buf + count, len - count, "normal ");
1167 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
1168 count += scnprintf(buf + count, len - count, "idle ");
1169 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
1170 count += scnprintf(buf + count, len - count, "standby ");
1171
1172 if (constraints->pw_budget_mW)
1173 count += scnprintf(buf + count, len - count, "%d mW budget",
1174 constraints->pw_budget_mW);
1175
1176 if (!count)
1177 count = scnprintf(buf, len, "no parameters");
1178 else
1179 --count;
1180
1181 count += scnprintf(buf + count, len - count, ", %s",
1182 _regulator_is_enabled(rdev) ? "enabled" : "disabled");
1183
1184 rdev_dbg(rdev, "%s\n", buf);
1185 }
1186 #else /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
1187 static inline void print_constraints_debug(struct regulator_dev *rdev) {}
1188 #endif /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
1189
1190 static void print_constraints(struct regulator_dev *rdev)
1191 {
1192 struct regulation_constraints *constraints = rdev->constraints;
1193
1194 print_constraints_debug(rdev);
1195
1196 if ((constraints->min_uV != constraints->max_uV) &&
1197 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
1198 rdev_warn(rdev,
1199 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1200 }
1201
1202 static int machine_constraints_voltage(struct regulator_dev *rdev,
1203 struct regulation_constraints *constraints)
1204 {
1205 const struct regulator_ops *ops = rdev->desc->ops;
1206 int ret;
1207
1208 /* do we need to apply the constraint voltage */
1209 if (rdev->constraints->apply_uV &&
1210 rdev->constraints->min_uV && rdev->constraints->max_uV) {
1211 int target_min, target_max;
1212 int current_uV = regulator_get_voltage_rdev(rdev);
1213
1214 if (current_uV == -ENOTRECOVERABLE) {
1215 /* This regulator can't be read and must be initialized */
1216 rdev_info(rdev, "Setting %d-%duV\n",
1217 rdev->constraints->min_uV,
1218 rdev->constraints->max_uV);
1219 _regulator_do_set_voltage(rdev,
1220 rdev->constraints->min_uV,
1221 rdev->constraints->max_uV);
1222 current_uV = regulator_get_voltage_rdev(rdev);
1223 }
1224
1225 if (current_uV < 0) {
1226 if (current_uV != -EPROBE_DEFER)
1227 rdev_err(rdev,
1228 "failed to get the current voltage: %pe\n",
1229 ERR_PTR(current_uV));
1230 return current_uV;
1231 }
1232
1233 /*
1234 * If we're below the minimum voltage move up to the
1235 * minimum voltage, if we're above the maximum voltage
1236 * then move down to the maximum.
1237 */
1238 target_min = current_uV;
1239 target_max = current_uV;
1240
1241 if (current_uV < rdev->constraints->min_uV) {
1242 target_min = rdev->constraints->min_uV;
1243 target_max = rdev->constraints->min_uV;
1244 }
1245
1246 if (current_uV > rdev->constraints->max_uV) {
1247 target_min = rdev->constraints->max_uV;
1248 target_max = rdev->constraints->max_uV;
1249 }
1250
1251 if (target_min != current_uV || target_max != current_uV) {
1252 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
1253 current_uV, target_min, target_max);
1254 ret = _regulator_do_set_voltage(
1255 rdev, target_min, target_max);
1256 if (ret < 0) {
1257 rdev_err(rdev,
1258 "failed to apply %d-%duV constraint: %pe\n",
1259 target_min, target_max, ERR_PTR(ret));
1260 return ret;
1261 }
1262 }
1263 }
1264
1265 /* constrain machine-level voltage specs to fit
1266 * the actual range supported by this regulator.
1267 */
1268 if (ops->list_voltage && rdev->desc->n_voltages) {
1269 int count = rdev->desc->n_voltages;
1270 int i;
1271 int min_uV = INT_MAX;
1272 int max_uV = INT_MIN;
1273 int cmin = constraints->min_uV;
1274 int cmax = constraints->max_uV;
1275
1276 /* it's safe to autoconfigure fixed-voltage supplies
1277 * and the constraints are used by list_voltage.
1278 */
1279 if (count == 1 && !cmin) {
1280 cmin = 1;
1281 cmax = INT_MAX;
1282 constraints->min_uV = cmin;
1283 constraints->max_uV = cmax;
1284 }
1285
1286 /* voltage constraints are optional */
1287 if ((cmin == 0) && (cmax == 0))
1288 return 0;
1289
1290 /* else require explicit machine-level constraints */
1291 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1292 rdev_err(rdev, "invalid voltage constraints\n");
1293 return -EINVAL;
1294 }
1295
1296 /* no need to loop voltages if range is continuous */
1297 if (rdev->desc->continuous_voltage_range)
1298 return 0;
1299
1300 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1301 for (i = 0; i < count; i++) {
1302 int value;
1303
1304 value = ops->list_voltage(rdev, i);
1305 if (value <= 0)
1306 continue;
1307
1308 /* maybe adjust [min_uV..max_uV] */
1309 if (value >= cmin && value < min_uV)
1310 min_uV = value;
1311 if (value <= cmax && value > max_uV)
1312 max_uV = value;
1313 }
1314
1315 /* final: [min_uV..max_uV] valid iff constraints valid */
1316 if (max_uV < min_uV) {
1317 rdev_err(rdev,
1318 "unsupportable voltage constraints %u-%uuV\n",
1319 min_uV, max_uV);
1320 return -EINVAL;
1321 }
1322
1323 /* use regulator's subset of machine constraints */
1324 if (constraints->min_uV < min_uV) {
1325 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1326 constraints->min_uV, min_uV);
1327 constraints->min_uV = min_uV;
1328 }
1329 if (constraints->max_uV > max_uV) {
1330 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1331 constraints->max_uV, max_uV);
1332 constraints->max_uV = max_uV;
1333 }
1334 }
1335
1336 return 0;
1337 }
1338
1339 static int machine_constraints_current(struct regulator_dev *rdev,
1340 struct regulation_constraints *constraints)
1341 {
1342 const struct regulator_ops *ops = rdev->desc->ops;
1343 int ret;
1344
1345 if (!constraints->min_uA && !constraints->max_uA)
1346 return 0;
1347
1348 if (constraints->min_uA > constraints->max_uA) {
1349 rdev_err(rdev, "Invalid current constraints\n");
1350 return -EINVAL;
1351 }
1352
1353 if (!ops->set_current_limit || !ops->get_current_limit) {
1354 rdev_warn(rdev, "Operation of current configuration missing\n");
1355 return 0;
1356 }
1357
1358 /* Set regulator current in constraints range */
1359 ret = ops->set_current_limit(rdev, constraints->min_uA,
1360 constraints->max_uA);
1361 if (ret < 0) {
1362 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1363 return ret;
1364 }
1365
1366 return 0;
1367 }
1368
1369 static int _regulator_do_enable(struct regulator_dev *rdev);
1370
1371 static int notif_set_limit(struct regulator_dev *rdev,
1372 int (*set)(struct regulator_dev *, int, int, bool),
1373 int limit, int severity)
1374 {
1375 bool enable;
1376
1377 if (limit == REGULATOR_NOTIF_LIMIT_DISABLE) {
1378 enable = false;
1379 limit = 0;
1380 } else {
1381 enable = true;
1382 }
1383
1384 if (limit == REGULATOR_NOTIF_LIMIT_ENABLE)
1385 limit = 0;
1386
1387 return set(rdev, limit, severity, enable);
1388 }
1389
1390 static int handle_notify_limits(struct regulator_dev *rdev,
1391 int (*set)(struct regulator_dev *, int, int, bool),
1392 struct notification_limit *limits)
1393 {
1394 int ret = 0;
1395
1396 if (!set)
1397 return -EOPNOTSUPP;
1398
1399 if (limits->prot)
1400 ret = notif_set_limit(rdev, set, limits->prot,
1401 REGULATOR_SEVERITY_PROT);
1402 if (ret)
1403 return ret;
1404
1405 if (limits->err)
1406 ret = notif_set_limit(rdev, set, limits->err,
1407 REGULATOR_SEVERITY_ERR);
1408 if (ret)
1409 return ret;
1410
1411 if (limits->warn)
1412 ret = notif_set_limit(rdev, set, limits->warn,
1413 REGULATOR_SEVERITY_WARN);
1414
1415 return ret;
1416 }
1417 /**
1418 * set_machine_constraints - sets regulator constraints
1419 * @rdev: regulator source
1420 *
1421 * Allows platform initialisation code to define and constrain
1422 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1423 * Constraints *must* be set by platform code in order for some
1424 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1425 * set_mode.
1426 *
1427 * Return: 0 on success or a negative error number on failure.
1428 */
1429 static int set_machine_constraints(struct regulator_dev *rdev)
1430 {
1431 int ret = 0;
1432 const struct regulator_ops *ops = rdev->desc->ops;
1433
1434 /*
1435 * If there is no mechanism for controlling the regulator then
1436 * flag it as always_on so we don't end up duplicating checks
1437 * for this so much. Note that we could control the state of
1438 * a supply to control the output on a regulator that has no
1439 * direct control.
1440 */
1441 if (!rdev->ena_pin && !ops->enable) {
1442 if (rdev->supply_name && !rdev->supply)
1443 return -EPROBE_DEFER;
1444
1445 if (rdev->supply)
1446 rdev->constraints->always_on =
1447 rdev->supply->rdev->constraints->always_on;
1448 else
1449 rdev->constraints->always_on = true;
1450 }
1451
1452 /*
1453 * If we want to enable this regulator, make sure that we know the
1454 * supplying regulator.
1455 */
1456 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1457 if (rdev->supply_name && !rdev->supply)
1458 return -EPROBE_DEFER;
1459 }
1460
1461 ret = machine_constraints_voltage(rdev, rdev->constraints);
1462 if (ret != 0)
1463 return ret;
1464
1465 ret = machine_constraints_current(rdev, rdev->constraints);
1466 if (ret != 0)
1467 return ret;
1468
1469 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1470 ret = ops->set_input_current_limit(rdev,
1471 rdev->constraints->ilim_uA);
1472 if (ret < 0) {
1473 rdev_err(rdev, "failed to set input limit: %pe\n", ERR_PTR(ret));
1474 return ret;
1475 }
1476 }
1477
1478 /* do we need to setup our suspend state */
1479 if (rdev->constraints->initial_state) {
1480 ret = suspend_set_initial_state(rdev);
1481 if (ret < 0) {
1482 rdev_err(rdev, "failed to set suspend state: %pe\n", ERR_PTR(ret));
1483 return ret;
1484 }
1485 }
1486
1487 if (rdev->constraints->initial_mode) {
1488 if (!ops->set_mode) {
1489 rdev_err(rdev, "no set_mode operation\n");
1490 return -EINVAL;
1491 }
1492
1493 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1494 if (ret < 0) {
1495 rdev_err(rdev, "failed to set initial mode: %pe\n", ERR_PTR(ret));
1496 return ret;
1497 }
1498 } else if (rdev->constraints->system_load) {
1499 /*
1500 * We'll only apply the initial system load if an
1501 * initial mode wasn't specified.
1502 */
1503 drms_uA_update(rdev);
1504 }
1505
1506 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1507 && ops->set_ramp_delay) {
1508 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1509 if (ret < 0) {
1510 rdev_err(rdev, "failed to set ramp_delay: %pe\n", ERR_PTR(ret));
1511 return ret;
1512 }
1513 }
1514
1515 if (rdev->constraints->pull_down && ops->set_pull_down) {
1516 ret = ops->set_pull_down(rdev);
1517 if (ret < 0) {
1518 rdev_err(rdev, "failed to set pull down: %pe\n", ERR_PTR(ret));
1519 return ret;
1520 }
1521 }
1522
1523 if (rdev->constraints->soft_start && ops->set_soft_start) {
1524 ret = ops->set_soft_start(rdev);
1525 if (ret < 0) {
1526 rdev_err(rdev, "failed to set soft start: %pe\n", ERR_PTR(ret));
1527 return ret;
1528 }
1529 }
1530
1531 /*
1532 * Existing logic does not warn if over_current_protection is given as
1533 * a constraint but driver does not support that. I think we should
1534 * warn about this type of issues as it is possible someone changes
1535 * PMIC on board to another type - and the another PMIC's driver does
1536 * not support setting protection. Board composer may happily believe
1537 * the DT limits are respected - especially if the new PMIC HW also
1538 * supports protection but the driver does not. I won't change the logic
1539 * without hearing more experienced opinion on this though.
1540 *
1541 * If warning is seen as a good idea then we can merge handling the
1542 * over-curret protection and detection and get rid of this special
1543 * handling.
1544 */
1545 if (rdev->constraints->over_current_protection
1546 && ops->set_over_current_protection) {
1547 int lim = rdev->constraints->over_curr_limits.prot;
1548
1549 ret = ops->set_over_current_protection(rdev, lim,
1550 REGULATOR_SEVERITY_PROT,
1551 true);
1552 if (ret < 0) {
1553 rdev_err(rdev, "failed to set over current protection: %pe\n",
1554 ERR_PTR(ret));
1555 return ret;
1556 }
1557 }
1558
1559 if (rdev->constraints->over_current_detection)
1560 ret = handle_notify_limits(rdev,
1561 ops->set_over_current_protection,
1562 &rdev->constraints->over_curr_limits);
1563 if (ret) {
1564 if (ret != -EOPNOTSUPP) {
1565 rdev_err(rdev, "failed to set over current limits: %pe\n",
1566 ERR_PTR(ret));
1567 return ret;
1568 }
1569 rdev_warn(rdev,
1570 "IC does not support requested over-current limits\n");
1571 }
1572
1573 if (rdev->constraints->over_voltage_detection)
1574 ret = handle_notify_limits(rdev,
1575 ops->set_over_voltage_protection,
1576 &rdev->constraints->over_voltage_limits);
1577 if (ret) {
1578 if (ret != -EOPNOTSUPP) {
1579 rdev_err(rdev, "failed to set over voltage limits %pe\n",
1580 ERR_PTR(ret));
1581 return ret;
1582 }
1583 rdev_warn(rdev,
1584 "IC does not support requested over voltage limits\n");
1585 }
1586
1587 if (rdev->constraints->under_voltage_detection)
1588 ret = handle_notify_limits(rdev,
1589 ops->set_under_voltage_protection,
1590 &rdev->constraints->under_voltage_limits);
1591 if (ret) {
1592 if (ret != -EOPNOTSUPP) {
1593 rdev_err(rdev, "failed to set under voltage limits %pe\n",
1594 ERR_PTR(ret));
1595 return ret;
1596 }
1597 rdev_warn(rdev,
1598 "IC does not support requested under voltage limits\n");
1599 }
1600
1601 if (rdev->constraints->over_temp_detection)
1602 ret = handle_notify_limits(rdev,
1603 ops->set_thermal_protection,
1604 &rdev->constraints->temp_limits);
1605 if (ret) {
1606 if (ret != -EOPNOTSUPP) {
1607 rdev_err(rdev, "failed to set temperature limits %pe\n",
1608 ERR_PTR(ret));
1609 return ret;
1610 }
1611 rdev_warn(rdev,
1612 "IC does not support requested temperature limits\n");
1613 }
1614
1615 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1616 bool ad_state = rdev->constraints->active_discharge ==
1617 REGULATOR_ACTIVE_DISCHARGE_ENABLE;
1618
1619 ret = ops->set_active_discharge(rdev, ad_state);
1620 if (ret < 0) {
1621 rdev_err(rdev, "failed to set active discharge: %pe\n", ERR_PTR(ret));
1622 return ret;
1623 }
1624 }
1625
1626 /* If the constraints say the regulator should be on at this point
1627 * and we have control then make sure it is enabled.
1628 */
1629 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1630 bool supply_enabled = false;
1631
1632 /* We have ensured a potential supply has been resolved above.
1633 *
1634 * If supplying regulator has already been enabled,
1635 * it's not intended to have use_count increment
1636 * when rdev is only boot-on.
1637 */
1638 if (rdev->supply &&
1639 (rdev->constraints->always_on ||
1640 !regulator_is_enabled(rdev->supply))) {
1641 ret = regulator_enable(rdev->supply);
1642 if (ret < 0) {
1643 _regulator_put(rdev->supply);
1644 rdev->supply = NULL;
1645 return ret;
1646 }
1647 supply_enabled = true;
1648 }
1649
1650 ret = _regulator_do_enable(rdev);
1651 if (ret < 0 && ret != -EINVAL) {
1652 rdev_err(rdev, "failed to enable: %pe\n", ERR_PTR(ret));
1653 if (supply_enabled)
1654 regulator_disable(rdev->supply);
1655 return ret;
1656 }
1657
1658 if (rdev->constraints->always_on)
1659 rdev->use_count++;
1660 } else if (rdev->desc->off_on_delay) {
1661 rdev->last_off = ktime_get();
1662 }
1663
1664 if (!rdev->constraints->pw_budget_mW)
1665 rdev->constraints->pw_budget_mW = INT_MAX;
1666
1667 print_constraints(rdev);
1668 return 0;
1669 }
1670
1671 /**
1672 * set_supply - set regulator supply regulator
1673 * @rdev: regulator (locked)
1674 * @supply_rdev: supply regulator (locked))
1675 *
1676 * Called by platform initialisation code to set the supply regulator for this
1677 * regulator. This ensures that a regulators supply will also be enabled by the
1678 * core if it's child is enabled.
1679 *
1680 * Return: 0 on success or a negative error number on failure.
1681 */
1682 static int set_supply(struct regulator_dev *rdev,
1683 struct regulator_dev *supply_rdev)
1684 {
1685 int err;
1686
1687 rdev_dbg(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1688
1689 if (!try_module_get(supply_rdev->owner))
1690 return -ENODEV;
1691
1692 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1693 if (rdev->supply == NULL) {
1694 module_put(supply_rdev->owner);
1695 err = -ENOMEM;
1696 return err;
1697 }
1698 supply_rdev->open_count++;
1699
1700 return 0;
1701 }
1702
1703 /**
1704 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1705 * @rdev: regulator source
1706 * @consumer_dev_name: dev_name() string for device supply applies to
1707 * @supply: symbolic name for supply
1708 *
1709 * Allows platform initialisation code to map physical regulator
1710 * sources to symbolic names for supplies for use by devices. Devices
1711 * should use these symbolic names to request regulators, avoiding the
1712 * need to provide board-specific regulator names as platform data.
1713 *
1714 * Return: 0 on success or a negative error number on failure.
1715 */
1716 static int set_consumer_device_supply(struct regulator_dev *rdev,
1717 const char *consumer_dev_name,
1718 const char *supply)
1719 {
1720 struct regulator_map *node, *new_node;
1721 int has_dev;
1722
1723 if (supply == NULL)
1724 return -EINVAL;
1725
1726 if (consumer_dev_name != NULL)
1727 has_dev = 1;
1728 else
1729 has_dev = 0;
1730
1731 new_node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1732 if (new_node == NULL)
1733 return -ENOMEM;
1734
1735 new_node->regulator = rdev;
1736 new_node->supply = supply;
1737
1738 if (has_dev) {
1739 new_node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1740 if (new_node->dev_name == NULL) {
1741 kfree(new_node);
1742 return -ENOMEM;
1743 }
1744 }
1745
1746 mutex_lock(®ulator_list_mutex);
1747 list_for_each_entry(node, ®ulator_map_list, list) {
1748 if (node->dev_name && consumer_dev_name) {
1749 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1750 continue;
1751 } else if (node->dev_name || consumer_dev_name) {
1752 continue;
1753 }
1754
1755 if (strcmp(node->supply, supply) != 0)
1756 continue;
1757
1758 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1759 consumer_dev_name,
1760 dev_name(&node->regulator->dev),
1761 node->regulator->desc->name,
1762 supply,
1763 dev_name(&rdev->dev), rdev_get_name(rdev));
1764 goto fail;
1765 }
1766
1767 list_add(&new_node->list, ®ulator_map_list);
1768 mutex_unlock(®ulator_list_mutex);
1769
1770 return 0;
1771
1772 fail:
1773 mutex_unlock(®ulator_list_mutex);
1774 kfree(new_node->dev_name);
1775 kfree(new_node);
1776 return -EBUSY;
1777 }
1778
1779 static void unset_regulator_supplies(struct regulator_dev *rdev)
1780 {
1781 struct regulator_map *node, *n;
1782
1783 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1784 if (rdev == node->regulator) {
1785 list_del(&node->list);
1786 kfree(node->dev_name);
1787 kfree(node);
1788 }
1789 }
1790 }
1791
1792 #ifdef CONFIG_DEBUG_FS
1793 static ssize_t constraint_flags_read_file(struct file *file,
1794 char __user *user_buf,
1795 size_t count, loff_t *ppos)
1796 {
1797 const struct regulator *regulator = file->private_data;
1798 const struct regulation_constraints *c = regulator->rdev->constraints;
1799 char *buf;
1800 ssize_t ret;
1801
1802 if (!c)
1803 return 0;
1804
1805 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1806 if (!buf)
1807 return -ENOMEM;
1808
1809 ret = snprintf(buf, PAGE_SIZE,
1810 "always_on: %u\n"
1811 "boot_on: %u\n"
1812 "apply_uV: %u\n"
1813 "ramp_disable: %u\n"
1814 "soft_start: %u\n"
1815 "pull_down: %u\n"
1816 "over_current_protection: %u\n",
1817 c->always_on,
1818 c->boot_on,
1819 c->apply_uV,
1820 c->ramp_disable,
1821 c->soft_start,
1822 c->pull_down,
1823 c->over_current_protection);
1824
1825 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1826 kfree(buf);
1827
1828 return ret;
1829 }
1830
1831 #endif
1832
1833 static const struct file_operations constraint_flags_fops = {
1834 #ifdef CONFIG_DEBUG_FS
1835 .open = simple_open,
1836 .read = constraint_flags_read_file,
1837 .llseek = default_llseek,
1838 #endif
1839 };
1840
1841 static void link_and_create_debugfs(struct regulator *regulator, struct regulator_dev *rdev,
1842 struct device *dev)
1843 {
1844 int err = 0;
1845
1846 if (dev) {
1847 regulator->dev = dev;
1848
1849 /* Add a link to the device sysfs entry */
1850 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1851 regulator->supply_name);
1852 if (err) {
1853 rdev_dbg(rdev, "could not add device link %s: %pe\n",
1854 dev->kobj.name, ERR_PTR(err));
1855 /* non-fatal */
1856 }
1857 }
1858
1859 if (err != -EEXIST) {
1860 regulator->debugfs = debugfs_create_dir(regulator->supply_name, rdev->debugfs);
1861 if (IS_ERR(regulator->debugfs)) {
1862 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1863 regulator->debugfs = NULL;
1864 }
1865 }
1866
1867 if (regulator->debugfs) {
1868 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1869 ®ulator->uA_load);
1870 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1871 ®ulator->voltage[PM_SUSPEND_ON].min_uV);
1872 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1873 ®ulator->voltage[PM_SUSPEND_ON].max_uV);
1874 debugfs_create_file("constraint_flags", 0444, regulator->debugfs,
1875 regulator, &constraint_flags_fops);
1876 }
1877 }
1878
1879 static struct regulator *create_regulator(struct regulator_dev *rdev,
1880 struct device *dev,
1881 const char *supply_name)
1882 {
1883 struct regulator *regulator;
1884
1885 lockdep_assert_held_once(&rdev->mutex.base);
1886
1887 if (dev) {
1888 supply_name = kasprintf(GFP_KERNEL, "%s-%s", dev->kobj.name, supply_name);
1889 if (supply_name == NULL)
1890 return NULL;
1891 } else {
1892 supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1893 if (supply_name == NULL)
1894 return NULL;
1895 }
1896
1897 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1898 if (regulator == NULL) {
1899 kfree_const(supply_name);
1900 return NULL;
1901 }
1902
1903 regulator->rdev = rdev;
1904 regulator->supply_name = supply_name;
1905
1906 list_add(®ulator->list, &rdev->consumer_list);
1907
1908 /*
1909 * Check now if the regulator is an always on regulator - if
1910 * it is then we don't need to do nearly so much work for
1911 * enable/disable calls.
1912 */
1913 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1914 _regulator_is_enabled(rdev))
1915 regulator->always_on = true;
1916
1917 return regulator;
1918 }
1919
1920 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1921 {
1922 if (rdev->constraints && rdev->constraints->enable_time)
1923 return rdev->constraints->enable_time;
1924 if (rdev->desc->ops->enable_time)
1925 return rdev->desc->ops->enable_time(rdev);
1926 return rdev->desc->enable_time;
1927 }
1928
1929 static struct regulator_supply_alias *regulator_find_supply_alias(
1930 struct device *dev, const char *supply)
1931 {
1932 struct regulator_supply_alias *map;
1933
1934 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1935 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1936 return map;
1937
1938 return NULL;
1939 }
1940
1941 static void regulator_supply_alias(struct device **dev, const char **supply)
1942 {
1943 struct regulator_supply_alias *map;
1944
1945 mutex_lock(®ulator_list_mutex);
1946 map = regulator_find_supply_alias(*dev, *supply);
1947 if (map) {
1948 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1949 *supply, map->alias_supply,
1950 dev_name(map->alias_dev));
1951 *dev = map->alias_dev;
1952 *supply = map->alias_supply;
1953 }
1954 mutex_unlock(®ulator_list_mutex);
1955 }
1956
1957 static int regulator_match(struct device *dev, const void *data)
1958 {
1959 struct regulator_dev *r = dev_to_rdev(dev);
1960
1961 return strcmp(rdev_get_name(r), data) == 0;
1962 }
1963
1964 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1965 {
1966 struct device *dev;
1967
1968 dev = class_find_device(®ulator_class, NULL, name, regulator_match);
1969
1970 return dev ? dev_to_rdev(dev) : NULL;
1971 }
1972
1973 static struct regulator_dev *regulator_dt_lookup(struct device *dev,
1974 const char *supply)
1975 {
1976 struct regulator_dev *r = NULL;
1977
1978 if (dev_of_node(dev)) {
1979 r = of_regulator_dev_lookup(dev, dev_of_node(dev), supply);
1980 if (PTR_ERR(r) == -ENODEV)
1981 r = NULL;
1982 }
1983
1984 return r;
1985 }
1986
1987 /**
1988 * regulator_dev_lookup - lookup a regulator device.
1989 * @dev: device for regulator "consumer".
1990 * @supply: Supply name or regulator ID.
1991 *
1992 * Return: pointer to &struct regulator_dev or ERR_PTR() encoded negative error number.
1993 *
1994 * If successful, returns a struct regulator_dev that corresponds to the name
1995 * @supply and with the embedded struct device refcount incremented by one.
1996 * The refcount must be dropped by calling put_device().
1997 * On failure one of the following ERR_PTR() encoded values is returned:
1998 * -%ENODEV if lookup fails permanently, -%EPROBE_DEFER if lookup could succeed
1999 * in the future.
2000 */
2001 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
2002 const char *supply)
2003 {
2004 struct regulator_dev *r = NULL;
2005 struct regulator_map *map;
2006 const char *devname = NULL;
2007
2008 regulator_supply_alias(&dev, &supply);
2009
2010 /* first do a dt based lookup */
2011 r = regulator_dt_lookup(dev, supply);
2012 if (r)
2013 return r;
2014
2015 /* if not found, try doing it non-dt way */
2016 if (dev)
2017 devname = dev_name(dev);
2018
2019 mutex_lock(®ulator_list_mutex);
2020 list_for_each_entry(map, ®ulator_map_list, list) {
2021 /* If the mapping has a device set up it must match */
2022 if (map->dev_name &&
2023 (!devname || strcmp(map->dev_name, devname)))
2024 continue;
2025
2026 if (strcmp(map->supply, supply) == 0 &&
2027 get_device(&map->regulator->dev)) {
2028 r = map->regulator;
2029 break;
2030 }
2031 }
2032 mutex_unlock(®ulator_list_mutex);
2033
2034 if (r)
2035 return r;
2036
2037 r = regulator_lookup_by_name(supply);
2038 if (r)
2039 return r;
2040
2041 return ERR_PTR(-ENODEV);
2042 }
2043
2044 static int regulator_resolve_supply(struct regulator_dev *rdev)
2045 {
2046 struct regulator_dev *r;
2047 struct device *dev = rdev->dev.parent;
2048 struct ww_acquire_ctx ww_ctx;
2049 int ret = 0;
2050
2051 /* No supply to resolve? */
2052 if (!rdev->supply_name)
2053 return 0;
2054
2055 /* Supply already resolved? (fast-path without locking contention) */
2056 if (rdev->supply)
2057 return 0;
2058
2059 /* first do a dt based lookup on the node described in the virtual
2060 * device.
2061 */
2062 r = regulator_dt_lookup(&rdev->dev, rdev->supply_name);
2063
2064 /* If regulator not found use usual search path in the parent
2065 * device.
2066 */
2067 if (!r)
2068 r = regulator_dev_lookup(dev, rdev->supply_name);
2069
2070 if (IS_ERR(r)) {
2071 ret = PTR_ERR(r);
2072
2073 /* Did the lookup explicitly defer for us? */
2074 if (ret == -EPROBE_DEFER)
2075 goto out;
2076
2077 if (have_full_constraints()) {
2078 r = dummy_regulator_rdev;
2079 if (!r) {
2080 ret = -EPROBE_DEFER;
2081 goto out;
2082 }
2083 get_device(&r->dev);
2084 } else {
2085 dev_err(dev, "Failed to resolve %s-supply for %s\n",
2086 rdev->supply_name, rdev->desc->name);
2087 ret = -EPROBE_DEFER;
2088 goto out;
2089 }
2090 }
2091
2092 if (r == rdev) {
2093 dev_err(dev, "Supply for %s (%s) resolved to itself\n",
2094 rdev->desc->name, rdev->supply_name);
2095 if (!have_full_constraints()) {
2096 ret = -EINVAL;
2097 goto out;
2098 }
2099 r = dummy_regulator_rdev;
2100 if (!r) {
2101 ret = -EPROBE_DEFER;
2102 goto out;
2103 }
2104 get_device(&r->dev);
2105 }
2106
2107 /*
2108 * If the supply's parent device is not the same as the
2109 * regulator's parent device, then ensure the parent device
2110 * is bound before we resolve the supply, in case the parent
2111 * device get probe deferred and unregisters the supply.
2112 */
2113 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
2114 if (!device_is_bound(r->dev.parent)) {
2115 put_device(&r->dev);
2116 ret = -EPROBE_DEFER;
2117 goto out;
2118 }
2119 }
2120
2121 /* Recursively resolve the supply of the supply */
2122 ret = regulator_resolve_supply(r);
2123 if (ret < 0) {
2124 put_device(&r->dev);
2125 goto out;
2126 }
2127
2128 /*
2129 * Recheck rdev->supply with rdev->mutex lock held to avoid a race
2130 * between rdev->supply null check and setting rdev->supply in
2131 * set_supply() from concurrent tasks.
2132 */
2133 regulator_lock_two(rdev, r, &ww_ctx);
2134
2135 /* Supply just resolved by a concurrent task? */
2136 if (rdev->supply) {
2137 regulator_unlock_two(rdev, r, &ww_ctx);
2138 put_device(&r->dev);
2139 goto out;
2140 }
2141
2142 ret = set_supply(rdev, r);
2143 if (ret < 0) {
2144 regulator_unlock_two(rdev, r, &ww_ctx);
2145 put_device(&r->dev);
2146 goto out;
2147 }
2148
2149 regulator_unlock_two(rdev, r, &ww_ctx);
2150
2151 /* rdev->supply was created in set_supply() */
2152 link_and_create_debugfs(rdev->supply, r, &rdev->dev);
2153
2154 /*
2155 * In set_machine_constraints() we may have turned this regulator on
2156 * but we couldn't propagate to the supply if it hadn't been resolved
2157 * yet. Do it now.
2158 */
2159 if (rdev->use_count) {
2160 ret = regulator_enable(rdev->supply);
2161 if (ret < 0) {
2162 struct regulator *supply;
2163
2164 regulator_lock_two(rdev, rdev->supply->rdev, &ww_ctx);
2165
2166 supply = rdev->supply;
2167 rdev->supply = NULL;
2168
2169 regulator_unlock_two(rdev, supply->rdev, &ww_ctx);
2170
2171 regulator_put(supply);
2172 goto out;
2173 }
2174 }
2175
2176 out:
2177 return ret;
2178 }
2179
2180 /* common pre-checks for regulator requests */
2181 int _regulator_get_common_check(struct device *dev, const char *id,
2182 enum regulator_get_type get_type)
2183 {
2184 if (get_type >= MAX_GET_TYPE) {
2185 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
2186 return -EINVAL;
2187 }
2188
2189 if (id == NULL) {
2190 dev_err(dev, "regulator request with no identifier\n");
2191 return -EINVAL;
2192 }
2193
2194 return 0;
2195 }
2196
2197 /**
2198 * _regulator_get_common - Common code for regulator requests
2199 * @rdev: regulator device pointer as returned by *regulator_dev_lookup()
2200 * Its reference count is expected to have been incremented.
2201 * @dev: device used for dev_printk messages
2202 * @id: Supply name or regulator ID
2203 * @get_type: enum regulator_get_type value corresponding to type of request
2204 *
2205 * Returns: pointer to struct regulator corresponding to @rdev, or ERR_PTR()
2206 * encoded error.
2207 *
2208 * This function should be chained with *regulator_dev_lookup() functions.
2209 */
2210 struct regulator *_regulator_get_common(struct regulator_dev *rdev, struct device *dev,
2211 const char *id, enum regulator_get_type get_type)
2212 {
2213 struct regulator *regulator;
2214 struct device_link *link;
2215 int ret;
2216
2217 if (IS_ERR(rdev)) {
2218 ret = PTR_ERR(rdev);
2219
2220 /*
2221 * If regulator_dev_lookup() fails with error other
2222 * than -ENODEV our job here is done, we simply return it.
2223 */
2224 if (ret != -ENODEV)
2225 return ERR_PTR(ret);
2226
2227 if (!have_full_constraints()) {
2228 dev_warn(dev,
2229 "incomplete constraints, dummy supplies not allowed (id=%s)\n", id);
2230 return ERR_PTR(-ENODEV);
2231 }
2232
2233 switch (get_type) {
2234 case NORMAL_GET:
2235 /*
2236 * Assume that a regulator is physically present and
2237 * enabled, even if it isn't hooked up, and just
2238 * provide a dummy.
2239 */
2240 rdev = dummy_regulator_rdev;
2241 if (!rdev)
2242 return ERR_PTR(-EPROBE_DEFER);
2243 dev_warn(dev, "supply %s not found, using dummy regulator\n", id);
2244 get_device(&rdev->dev);
2245 break;
2246
2247 case EXCLUSIVE_GET:
2248 dev_warn(dev,
2249 "dummy supplies not allowed for exclusive requests (id=%s)\n", id);
2250 fallthrough;
2251
2252 default:
2253 return ERR_PTR(-ENODEV);
2254 }
2255 }
2256
2257 if (rdev->exclusive) {
2258 regulator = ERR_PTR(-EPERM);
2259 put_device(&rdev->dev);
2260 return regulator;
2261 }
2262
2263 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
2264 regulator = ERR_PTR(-EBUSY);
2265 put_device(&rdev->dev);
2266 return regulator;
2267 }
2268
2269 mutex_lock(®ulator_list_mutex);
2270 ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
2271 mutex_unlock(®ulator_list_mutex);
2272
2273 if (ret != 0) {
2274 regulator = ERR_PTR(-EPROBE_DEFER);
2275 put_device(&rdev->dev);
2276 return regulator;
2277 }
2278
2279 ret = regulator_resolve_supply(rdev);
2280 if (ret < 0) {
2281 regulator = ERR_PTR(ret);
2282 put_device(&rdev->dev);
2283 return regulator;
2284 }
2285
2286 if (!try_module_get(rdev->owner)) {
2287 regulator = ERR_PTR(-EPROBE_DEFER);
2288 put_device(&rdev->dev);
2289 return regulator;
2290 }
2291
2292 regulator_lock(rdev);
2293 regulator = create_regulator(rdev, dev, id);
2294 regulator_unlock(rdev);
2295 if (regulator == NULL) {
2296 regulator = ERR_PTR(-ENOMEM);
2297 module_put(rdev->owner);
2298 put_device(&rdev->dev);
2299 return regulator;
2300 }
2301
2302 link_and_create_debugfs(regulator, rdev, dev);
2303
2304 rdev->open_count++;
2305 if (get_type == EXCLUSIVE_GET) {
2306 rdev->exclusive = 1;
2307
2308 ret = _regulator_is_enabled(rdev);
2309 if (ret > 0) {
2310 rdev->use_count = 1;
2311 regulator->enable_count = 1;
2312
2313 /* Propagate the regulator state to its supply */
2314 if (rdev->supply) {
2315 ret = regulator_enable(rdev->supply);
2316 if (ret < 0) {
2317 destroy_regulator(regulator);
2318 module_put(rdev->owner);
2319 put_device(&rdev->dev);
2320 return ERR_PTR(ret);
2321 }
2322 }
2323 } else {
2324 rdev->use_count = 0;
2325 regulator->enable_count = 0;
2326 }
2327 }
2328
2329 link = device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
2330 if (!IS_ERR_OR_NULL(link))
2331 regulator->device_link = true;
2332
2333 return regulator;
2334 }
2335
2336 /* Internal regulator request function */
2337 struct regulator *_regulator_get(struct device *dev, const char *id,
2338 enum regulator_get_type get_type)
2339 {
2340 struct regulator_dev *rdev;
2341 int ret;
2342
2343 ret = _regulator_get_common_check(dev, id, get_type);
2344 if (ret)
2345 return ERR_PTR(ret);
2346
2347 rdev = regulator_dev_lookup(dev, id);
2348 return _regulator_get_common(rdev, dev, id, get_type);
2349 }
2350
2351 /**
2352 * regulator_get - lookup and obtain a reference to a regulator.
2353 * @dev: device for regulator "consumer"
2354 * @id: Supply name or regulator ID.
2355 *
2356 * Use of supply names configured via set_consumer_device_supply() is
2357 * strongly encouraged. It is recommended that the supply name used
2358 * should match the name used for the supply and/or the relevant
2359 * device pins in the datasheet.
2360 *
2361 * Return: Pointer to a &struct regulator corresponding to the regulator
2362 * producer, or an ERR_PTR() encoded negative error number.
2363 */
2364 struct regulator *regulator_get(struct device *dev, const char *id)
2365 {
2366 return _regulator_get(dev, id, NORMAL_GET);
2367 }
2368 EXPORT_SYMBOL_GPL(regulator_get);
2369
2370 /**
2371 * regulator_get_exclusive - obtain exclusive access to a regulator.
2372 * @dev: device for regulator "consumer"
2373 * @id: Supply name or regulator ID.
2374 *
2375 * Other consumers will be unable to obtain this regulator while this
2376 * reference is held and the use count for the regulator will be
2377 * initialised to reflect the current state of the regulator.
2378 *
2379 * This is intended for use by consumers which cannot tolerate shared
2380 * use of the regulator such as those which need to force the
2381 * regulator off for correct operation of the hardware they are
2382 * controlling.
2383 *
2384 * Use of supply names configured via set_consumer_device_supply() is
2385 * strongly encouraged. It is recommended that the supply name used
2386 * should match the name used for the supply and/or the relevant
2387 * device pins in the datasheet.
2388 *
2389 * Return: Pointer to a &struct regulator corresponding to the regulator
2390 * producer, or an ERR_PTR() encoded negative error number.
2391 */
2392 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
2393 {
2394 return _regulator_get(dev, id, EXCLUSIVE_GET);
2395 }
2396 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
2397
2398 /**
2399 * regulator_get_optional - obtain optional access to a regulator.
2400 * @dev: device for regulator "consumer"
2401 * @id: Supply name or regulator ID.
2402 *
2403 * This is intended for use by consumers for devices which can have
2404 * some supplies unconnected in normal use, such as some MMC devices.
2405 * It can allow the regulator core to provide stub supplies for other
2406 * supplies requested using normal regulator_get() calls without
2407 * disrupting the operation of drivers that can handle absent
2408 * supplies.
2409 *
2410 * Use of supply names configured via set_consumer_device_supply() is
2411 * strongly encouraged. It is recommended that the supply name used
2412 * should match the name used for the supply and/or the relevant
2413 * device pins in the datasheet.
2414 *
2415 * Return: Pointer to a &struct regulator corresponding to the regulator
2416 * producer, or an ERR_PTR() encoded negative error number.
2417 */
2418 struct regulator *regulator_get_optional(struct device *dev, const char *id)
2419 {
2420 return _regulator_get(dev, id, OPTIONAL_GET);
2421 }
2422 EXPORT_SYMBOL_GPL(regulator_get_optional);
2423
2424 static void destroy_regulator(struct regulator *regulator)
2425 {
2426 struct regulator_dev *rdev = regulator->rdev;
2427
2428 debugfs_remove_recursive(regulator->debugfs);
2429
2430 if (regulator->dev) {
2431 if (regulator->device_link)
2432 device_link_remove(regulator->dev, &rdev->dev);
2433
2434 /* remove any sysfs entries */
2435 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
2436 }
2437
2438 regulator_lock(rdev);
2439 list_del(®ulator->list);
2440
2441 rdev->open_count--;
2442 rdev->exclusive = 0;
2443 regulator_unlock(rdev);
2444
2445 kfree_const(regulator->supply_name);
2446 kfree(regulator);
2447 }
2448
2449 /* regulator_list_mutex lock held by regulator_put() */
2450 static void _regulator_put(struct regulator *regulator)
2451 {
2452 struct regulator_dev *rdev;
2453
2454 if (IS_ERR_OR_NULL(regulator))
2455 return;
2456
2457 lockdep_assert_held_once(®ulator_list_mutex);
2458
2459 /* Docs say you must disable before calling regulator_put() */
2460 WARN_ON(regulator->enable_count);
2461
2462 rdev = regulator->rdev;
2463
2464 destroy_regulator(regulator);
2465
2466 module_put(rdev->owner);
2467 put_device(&rdev->dev);
2468 }
2469
2470 /**
2471 * regulator_put - "free" the regulator source
2472 * @regulator: regulator source
2473 *
2474 * Note: drivers must ensure that all regulator_enable calls made on this
2475 * regulator source are balanced by regulator_disable calls prior to calling
2476 * this function.
2477 */
2478 void regulator_put(struct regulator *regulator)
2479 {
2480 mutex_lock(®ulator_list_mutex);
2481 _regulator_put(regulator);
2482 mutex_unlock(®ulator_list_mutex);
2483 }
2484 EXPORT_SYMBOL_GPL(regulator_put);
2485
2486 /**
2487 * regulator_register_supply_alias - Provide device alias for supply lookup
2488 *
2489 * @dev: device that will be given as the regulator "consumer"
2490 * @id: Supply name or regulator ID
2491 * @alias_dev: device that should be used to lookup the supply
2492 * @alias_id: Supply name or regulator ID that should be used to lookup the
2493 * supply
2494 *
2495 * All lookups for id on dev will instead be conducted for alias_id on
2496 * alias_dev.
2497 *
2498 * Return: 0 on success or a negative error number on failure.
2499 */
2500 int regulator_register_supply_alias(struct device *dev, const char *id,
2501 struct device *alias_dev,
2502 const char *alias_id)
2503 {
2504 struct regulator_supply_alias *map;
2505 struct regulator_supply_alias *new_map;
2506
2507 new_map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
2508 if (!new_map)
2509 return -ENOMEM;
2510
2511 mutex_lock(®ulator_list_mutex);
2512 map = regulator_find_supply_alias(dev, id);
2513 if (map) {
2514 mutex_unlock(®ulator_list_mutex);
2515 kfree(new_map);
2516 return -EEXIST;
2517 }
2518
2519 new_map->src_dev = dev;
2520 new_map->src_supply = id;
2521 new_map->alias_dev = alias_dev;
2522 new_map->alias_supply = alias_id;
2523 list_add(&new_map->list, ®ulator_supply_alias_list);
2524 mutex_unlock(®ulator_list_mutex);
2525 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2526 id, dev_name(dev), alias_id, dev_name(alias_dev));
2527
2528 return 0;
2529 }
2530 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
2531
2532 /**
2533 * regulator_unregister_supply_alias - Remove device alias
2534 *
2535 * @dev: device that will be given as the regulator "consumer"
2536 * @id: Supply name or regulator ID
2537 *
2538 * Remove a lookup alias if one exists for id on dev.
2539 */
2540 void regulator_unregister_supply_alias(struct device *dev, const char *id)
2541 {
2542 struct regulator_supply_alias *map;
2543
2544 mutex_lock(®ulator_list_mutex);
2545 map = regulator_find_supply_alias(dev, id);
2546 if (map) {
2547 list_del(&map->list);
2548 kfree(map);
2549 }
2550 mutex_unlock(®ulator_list_mutex);
2551 }
2552 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
2553
2554 /**
2555 * regulator_bulk_register_supply_alias - register multiple aliases
2556 *
2557 * @dev: device that will be given as the regulator "consumer"
2558 * @id: List of supply names or regulator IDs
2559 * @alias_dev: device that should be used to lookup the supply
2560 * @alias_id: List of supply names or regulator IDs that should be used to
2561 * lookup the supply
2562 * @num_id: Number of aliases to register
2563 *
2564 * This helper function allows drivers to register several supply
2565 * aliases in one operation. If any of the aliases cannot be
2566 * registered any aliases that were registered will be removed
2567 * before returning to the caller.
2568 *
2569 * Return: 0 on success or a negative error number on failure.
2570 */
2571 int regulator_bulk_register_supply_alias(struct device *dev,
2572 const char *const *id,
2573 struct device *alias_dev,
2574 const char *const *alias_id,
2575 int num_id)
2576 {
2577 int i;
2578 int ret;
2579
2580 for (i = 0; i < num_id; ++i) {
2581 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2582 alias_id[i]);
2583 if (ret < 0)
2584 goto err;
2585 }
2586
2587 return 0;
2588
2589 err:
2590 dev_err(dev,
2591 "Failed to create supply alias %s,%s -> %s,%s\n",
2592 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2593
2594 while (--i >= 0)
2595 regulator_unregister_supply_alias(dev, id[i]);
2596
2597 return ret;
2598 }
2599 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2600
2601 /**
2602 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2603 *
2604 * @dev: device that will be given as the regulator "consumer"
2605 * @id: List of supply names or regulator IDs
2606 * @num_id: Number of aliases to unregister
2607 *
2608 * This helper function allows drivers to unregister several supply
2609 * aliases in one operation.
2610 */
2611 void regulator_bulk_unregister_supply_alias(struct device *dev,
2612 const char *const *id,
2613 int num_id)
2614 {
2615 int i;
2616
2617 for (i = 0; i < num_id; ++i)
2618 regulator_unregister_supply_alias(dev, id[i]);
2619 }
2620 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2621
2622
2623 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2624 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2625 const struct regulator_config *config)
2626 {
2627 struct regulator_enable_gpio *pin, *new_pin;
2628 struct gpio_desc *gpiod;
2629
2630 gpiod = config->ena_gpiod;
2631 new_pin = kzalloc(sizeof(*new_pin), GFP_KERNEL);
2632
2633 mutex_lock(®ulator_list_mutex);
2634
2635 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
2636 if (gpiod_is_equal(pin->gpiod, gpiod)) {
2637 rdev_dbg(rdev, "GPIO is already used\n");
2638 goto update_ena_gpio_to_rdev;
2639 }
2640 }
2641
2642 if (new_pin == NULL) {
2643 mutex_unlock(®ulator_list_mutex);
2644 return -ENOMEM;
2645 }
2646
2647 pin = new_pin;
2648 new_pin = NULL;
2649
2650 pin->gpiod = gpiod;
2651 list_add(&pin->list, ®ulator_ena_gpio_list);
2652
2653 update_ena_gpio_to_rdev:
2654 pin->request_count++;
2655 rdev->ena_pin = pin;
2656
2657 mutex_unlock(®ulator_list_mutex);
2658 kfree(new_pin);
2659
2660 return 0;
2661 }
2662
2663 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2664 {
2665 struct regulator_enable_gpio *pin, *n;
2666
2667 if (!rdev->ena_pin)
2668 return;
2669
2670 /* Free the GPIO only in case of no use */
2671 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
2672 if (pin != rdev->ena_pin)
2673 continue;
2674
2675 if (--pin->request_count)
2676 break;
2677
2678 gpiod_put(pin->gpiod);
2679 list_del(&pin->list);
2680 kfree(pin);
2681 break;
2682 }
2683
2684 rdev->ena_pin = NULL;
2685 }
2686
2687 /**
2688 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2689 * @rdev: regulator_dev structure
2690 * @enable: enable GPIO at initial use?
2691 *
2692 * GPIO is enabled in case of initial use. (enable_count is 0)
2693 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2694 *
2695 * Return: 0 on success or a negative error number on failure.
2696 */
2697 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2698 {
2699 struct regulator_enable_gpio *pin = rdev->ena_pin;
2700
2701 if (!pin)
2702 return -EINVAL;
2703
2704 if (enable) {
2705 /* Enable GPIO at initial use */
2706 if (pin->enable_count == 0)
2707 gpiod_set_value_cansleep(pin->gpiod, 1);
2708
2709 pin->enable_count++;
2710 } else {
2711 if (pin->enable_count > 1) {
2712 pin->enable_count--;
2713 return 0;
2714 }
2715
2716 /* Disable GPIO if not used */
2717 if (pin->enable_count <= 1) {
2718 gpiod_set_value_cansleep(pin->gpiod, 0);
2719 pin->enable_count = 0;
2720 }
2721 }
2722
2723 return 0;
2724 }
2725
2726 /**
2727 * _regulator_check_status_enabled - check if regulator status can be
2728 * interpreted as "regulator is enabled"
2729 * @rdev: the regulator device to check
2730 *
2731 * Return:
2732 * * 1 - if status shows regulator is in enabled state
2733 * * 0 - if not enabled state
2734 * * Error Value - as received from ops->get_status()
2735 */
2736 static inline int _regulator_check_status_enabled(struct regulator_dev *rdev)
2737 {
2738 int ret = rdev->desc->ops->get_status(rdev);
2739
2740 if (ret < 0) {
2741 rdev_info(rdev, "get_status returned error: %d\n", ret);
2742 return ret;
2743 }
2744
2745 switch (ret) {
2746 case REGULATOR_STATUS_OFF:
2747 case REGULATOR_STATUS_ERROR:
2748 case REGULATOR_STATUS_UNDEFINED:
2749 return 0;
2750 default:
2751 return 1;
2752 }
2753 }
2754
2755 static int _regulator_do_enable(struct regulator_dev *rdev)
2756 {
2757 int ret, delay;
2758
2759 /* Query before enabling in case configuration dependent. */
2760 ret = _regulator_get_enable_time(rdev);
2761 if (ret >= 0) {
2762 delay = ret;
2763 } else {
2764 rdev_warn(rdev, "enable_time() failed: %pe\n", ERR_PTR(ret));
2765 delay = 0;
2766 }
2767
2768 trace_regulator_enable(rdev_get_name(rdev));
2769
2770 if (rdev->desc->off_on_delay) {
2771 /* if needed, keep a distance of off_on_delay from last time
2772 * this regulator was disabled.
2773 */
2774 ktime_t end = ktime_add_us(rdev->last_off, rdev->desc->off_on_delay);
2775 s64 remaining = ktime_us_delta(end, ktime_get_boottime());
2776
2777 if (remaining > 0)
2778 fsleep(remaining);
2779 }
2780
2781 if (rdev->ena_pin) {
2782 if (!rdev->ena_gpio_state) {
2783 ret = regulator_ena_gpio_ctrl(rdev, true);
2784 if (ret < 0)
2785 return ret;
2786 rdev->ena_gpio_state = 1;
2787 }
2788 } else if (rdev->desc->ops->enable) {
2789 ret = rdev->desc->ops->enable(rdev);
2790 if (ret < 0)
2791 return ret;
2792 } else {
2793 return -EINVAL;
2794 }
2795
2796 /* Allow the regulator to ramp; it would be useful to extend
2797 * this for bulk operations so that the regulators can ramp
2798 * together.
2799 */
2800 trace_regulator_enable_delay(rdev_get_name(rdev));
2801
2802 /* If poll_enabled_time is set, poll upto the delay calculated
2803 * above, delaying poll_enabled_time uS to check if the regulator
2804 * actually got enabled.
2805 * If the regulator isn't enabled after our delay helper has expired,
2806 * return -ETIMEDOUT.
2807 */
2808 if (rdev->desc->poll_enabled_time) {
2809 int time_remaining = delay;
2810
2811 while (time_remaining > 0) {
2812 fsleep(rdev->desc->poll_enabled_time);
2813
2814 if (rdev->desc->ops->get_status) {
2815 ret = _regulator_check_status_enabled(rdev);
2816 if (ret < 0)
2817 return ret;
2818 else if (ret)
2819 break;
2820 } else if (rdev->desc->ops->is_enabled(rdev))
2821 break;
2822
2823 time_remaining -= rdev->desc->poll_enabled_time;
2824 }
2825
2826 if (time_remaining <= 0) {
2827 rdev_err(rdev, "Enabled check timed out\n");
2828 return -ETIMEDOUT;
2829 }
2830 } else {
2831 fsleep(delay);
2832 }
2833
2834 trace_regulator_enable_complete(rdev_get_name(rdev));
2835
2836 return 0;
2837 }
2838
2839 /**
2840 * _regulator_handle_consumer_enable - handle that a consumer enabled
2841 * @regulator: regulator source
2842 *
2843 * Some things on a regulator consumer (like the contribution towards total
2844 * load on the regulator) only have an effect when the consumer wants the
2845 * regulator enabled. Explained in example with two consumers of the same
2846 * regulator:
2847 * consumer A: set_load(100); => total load = 0
2848 * consumer A: regulator_enable(); => total load = 100
2849 * consumer B: set_load(1000); => total load = 100
2850 * consumer B: regulator_enable(); => total load = 1100
2851 * consumer A: regulator_disable(); => total_load = 1000
2852 *
2853 * This function (together with _regulator_handle_consumer_disable) is
2854 * responsible for keeping track of the refcount for a given regulator consumer
2855 * and applying / unapplying these things.
2856 *
2857 * Return: 0 on success or negative error number on failure.
2858 */
2859 static int _regulator_handle_consumer_enable(struct regulator *regulator)
2860 {
2861 int ret;
2862 struct regulator_dev *rdev = regulator->rdev;
2863
2864 lockdep_assert_held_once(&rdev->mutex.base);
2865
2866 regulator->enable_count++;
2867 if (regulator->uA_load && regulator->enable_count == 1) {
2868 ret = drms_uA_update(rdev);
2869 if (ret)
2870 regulator->enable_count--;
2871 return ret;
2872 }
2873
2874 return 0;
2875 }
2876
2877 /**
2878 * _regulator_handle_consumer_disable - handle that a consumer disabled
2879 * @regulator: regulator source
2880 *
2881 * The opposite of _regulator_handle_consumer_enable().
2882 *
2883 * Return: 0 on success or a negative error number on failure.
2884 */
2885 static int _regulator_handle_consumer_disable(struct regulator *regulator)
2886 {
2887 struct regulator_dev *rdev = regulator->rdev;
2888
2889 lockdep_assert_held_once(&rdev->mutex.base);
2890
2891 if (!regulator->enable_count) {
2892 rdev_err(rdev, "Underflow of regulator enable count\n");
2893 return -EINVAL;
2894 }
2895
2896 regulator->enable_count--;
2897 if (regulator->uA_load && regulator->enable_count == 0)
2898 return drms_uA_update(rdev);
2899
2900 return 0;
2901 }
2902
2903 /* locks held by regulator_enable() */
2904 static int _regulator_enable(struct regulator *regulator)
2905 {
2906 struct regulator_dev *rdev = regulator->rdev;
2907 int ret;
2908
2909 lockdep_assert_held_once(&rdev->mutex.base);
2910
2911 if (rdev->use_count == 0 && rdev->supply) {
2912 ret = _regulator_enable(rdev->supply);
2913 if (ret < 0)
2914 return ret;
2915 }
2916
2917 /* balance only if there are regulators coupled */
2918 if (rdev->coupling_desc.n_coupled > 1) {
2919 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2920 if (ret < 0)
2921 goto err_disable_supply;
2922 }
2923
2924 ret = _regulator_handle_consumer_enable(regulator);
2925 if (ret < 0)
2926 goto err_disable_supply;
2927
2928 if (rdev->use_count == 0) {
2929 /*
2930 * The regulator may already be enabled if it's not switchable
2931 * or was left on
2932 */
2933 ret = _regulator_is_enabled(rdev);
2934 if (ret == -EINVAL || ret == 0) {
2935 if (!regulator_ops_is_valid(rdev,
2936 REGULATOR_CHANGE_STATUS)) {
2937 ret = -EPERM;
2938 goto err_consumer_disable;
2939 }
2940
2941 ret = _regulator_do_enable(rdev);
2942 if (ret < 0)
2943 goto err_consumer_disable;
2944
2945 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2946 NULL);
2947 } else if (ret < 0) {
2948 rdev_err(rdev, "is_enabled() failed: %pe\n", ERR_PTR(ret));
2949 goto err_consumer_disable;
2950 }
2951 /* Fallthrough on positive return values - already enabled */
2952 }
2953
2954 if (regulator->enable_count == 1)
2955 rdev->use_count++;
2956
2957 return 0;
2958
2959 err_consumer_disable:
2960 _regulator_handle_consumer_disable(regulator);
2961
2962 err_disable_supply:
2963 if (rdev->use_count == 0 && rdev->supply)
2964 _regulator_disable(rdev->supply);
2965
2966 return ret;
2967 }
2968
2969 /**
2970 * regulator_enable - enable regulator output
2971 * @regulator: regulator source
2972 *
2973 * Request that the regulator be enabled with the regulator output at
2974 * the predefined voltage or current value. Calls to regulator_enable()
2975 * must be balanced with calls to regulator_disable().
2976 *
2977 * NOTE: the output value can be set by other drivers, boot loader or may be
2978 * hardwired in the regulator.
2979 *
2980 * Return: 0 on success or a negative error number on failure.
2981 */
2982 int regulator_enable(struct regulator *regulator)
2983 {
2984 struct regulator_dev *rdev = regulator->rdev;
2985 struct ww_acquire_ctx ww_ctx;
2986 int ret;
2987
2988 regulator_lock_dependent(rdev, &ww_ctx);
2989 ret = _regulator_enable(regulator);
2990 regulator_unlock_dependent(rdev, &ww_ctx);
2991
2992 return ret;
2993 }
2994 EXPORT_SYMBOL_GPL(regulator_enable);
2995
2996 static int _regulator_do_disable(struct regulator_dev *rdev)
2997 {
2998 int ret;
2999
3000 trace_regulator_disable(rdev_get_name(rdev));
3001
3002 if (rdev->ena_pin) {
3003 if (rdev->ena_gpio_state) {
3004 ret = regulator_ena_gpio_ctrl(rdev, false);
3005 if (ret < 0)
3006 return ret;
3007 rdev->ena_gpio_state = 0;
3008 }
3009
3010 } else if (rdev->desc->ops->disable) {
3011 ret = rdev->desc->ops->disable(rdev);
3012 if (ret != 0)
3013 return ret;
3014 }
3015
3016 if (rdev->desc->off_on_delay)
3017 rdev->last_off = ktime_get_boottime();
3018
3019 trace_regulator_disable_complete(rdev_get_name(rdev));
3020
3021 return 0;
3022 }
3023
3024 /* locks held by regulator_disable() */
3025 static int _regulator_disable(struct regulator *regulator)
3026 {
3027 struct regulator_dev *rdev = regulator->rdev;
3028 int ret = 0;
3029
3030 lockdep_assert_held_once(&rdev->mutex.base);
3031
3032 if (WARN(regulator->enable_count == 0,
3033 "unbalanced disables for %s\n", rdev_get_name(rdev)))
3034 return -EIO;
3035
3036 if (regulator->enable_count == 1) {
3037 /* disabling last enable_count from this regulator */
3038 /* are we the last user and permitted to disable ? */
3039 if (rdev->use_count == 1 &&
3040 (rdev->constraints && !rdev->constraints->always_on)) {
3041
3042 /* we are last user */
3043 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
3044 ret = _notifier_call_chain(rdev,
3045 REGULATOR_EVENT_PRE_DISABLE,
3046 NULL);
3047 if (ret & NOTIFY_STOP_MASK)
3048 return -EINVAL;
3049
3050 ret = _regulator_do_disable(rdev);
3051 if (ret < 0) {
3052 rdev_err(rdev, "failed to disable: %pe\n", ERR_PTR(ret));
3053 _notifier_call_chain(rdev,
3054 REGULATOR_EVENT_ABORT_DISABLE,
3055 NULL);
3056 return ret;
3057 }
3058 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
3059 NULL);
3060 }
3061
3062 rdev->use_count = 0;
3063 } else if (rdev->use_count > 1) {
3064 rdev->use_count--;
3065 }
3066 }
3067
3068 if (ret == 0)
3069 ret = _regulator_handle_consumer_disable(regulator);
3070
3071 if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
3072 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
3073
3074 if (ret == 0 && rdev->use_count == 0 && rdev->supply)
3075 ret = _regulator_disable(rdev->supply);
3076
3077 return ret;
3078 }
3079
3080 /**
3081 * regulator_disable - disable regulator output
3082 * @regulator: regulator source
3083 *
3084 * Disable the regulator output voltage or current. Calls to
3085 * regulator_enable() must be balanced with calls to
3086 * regulator_disable().
3087 *
3088 * NOTE: this will only disable the regulator output if no other consumer
3089 * devices have it enabled, the regulator device supports disabling and
3090 * machine constraints permit this operation.
3091 *
3092 * Return: 0 on success or a negative error number on failure.
3093 */
3094 int regulator_disable(struct regulator *regulator)
3095 {
3096 struct regulator_dev *rdev = regulator->rdev;
3097 struct ww_acquire_ctx ww_ctx;
3098 int ret;
3099
3100 regulator_lock_dependent(rdev, &ww_ctx);
3101 ret = _regulator_disable(regulator);
3102 regulator_unlock_dependent(rdev, &ww_ctx);
3103
3104 return ret;
3105 }
3106 EXPORT_SYMBOL_GPL(regulator_disable);
3107
3108 /* locks held by regulator_force_disable() */
3109 static int _regulator_force_disable(struct regulator_dev *rdev)
3110 {
3111 int ret = 0;
3112
3113 lockdep_assert_held_once(&rdev->mutex.base);
3114
3115 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
3116 REGULATOR_EVENT_PRE_DISABLE, NULL);
3117 if (ret & NOTIFY_STOP_MASK)
3118 return -EINVAL;
3119
3120 ret = _regulator_do_disable(rdev);
3121 if (ret < 0) {
3122 rdev_err(rdev, "failed to force disable: %pe\n", ERR_PTR(ret));
3123 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
3124 REGULATOR_EVENT_ABORT_DISABLE, NULL);
3125 return ret;
3126 }
3127
3128 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
3129 REGULATOR_EVENT_DISABLE, NULL);
3130
3131 return 0;
3132 }
3133
3134 /**
3135 * regulator_force_disable - force disable regulator output
3136 * @regulator: regulator source
3137 *
3138 * Forcibly disable the regulator output voltage or current.
3139 * NOTE: this *will* disable the regulator output even if other consumer
3140 * devices have it enabled. This should be used for situations when device
3141 * damage will likely occur if the regulator is not disabled (e.g. over temp).
3142 *
3143 * Return: 0 on success or a negative error number on failure.
3144 */
3145 int regulator_force_disable(struct regulator *regulator)
3146 {
3147 struct regulator_dev *rdev = regulator->rdev;
3148 struct ww_acquire_ctx ww_ctx;
3149 int ret;
3150
3151 regulator_lock_dependent(rdev, &ww_ctx);
3152
3153 ret = _regulator_force_disable(regulator->rdev);
3154
3155 if (rdev->coupling_desc.n_coupled > 1)
3156 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
3157
3158 if (regulator->uA_load) {
3159 regulator->uA_load = 0;
3160 ret = drms_uA_update(rdev);
3161 }
3162
3163 if (rdev->use_count != 0 && rdev->supply)
3164 _regulator_disable(rdev->supply);
3165
3166 regulator_unlock_dependent(rdev, &ww_ctx);
3167
3168 return ret;
3169 }
3170 EXPORT_SYMBOL_GPL(regulator_force_disable);
3171
3172 static void regulator_disable_work(struct work_struct *work)
3173 {
3174 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
3175 disable_work.work);
3176 struct ww_acquire_ctx ww_ctx;
3177 int count, i, ret;
3178 struct regulator *regulator;
3179 int total_count = 0;
3180
3181 regulator_lock_dependent(rdev, &ww_ctx);
3182
3183 /*
3184 * Workqueue functions queue the new work instance while the previous
3185 * work instance is being processed. Cancel the queued work instance
3186 * as the work instance under processing does the job of the queued
3187 * work instance.
3188 */
3189 cancel_delayed_work(&rdev->disable_work);
3190
3191 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3192 count = regulator->deferred_disables;
3193
3194 if (!count)
3195 continue;
3196
3197 total_count += count;
3198 regulator->deferred_disables = 0;
3199
3200 for (i = 0; i < count; i++) {
3201 ret = _regulator_disable(regulator);
3202 if (ret != 0)
3203 rdev_err(rdev, "Deferred disable failed: %pe\n",
3204 ERR_PTR(ret));
3205 }
3206 }
3207 WARN_ON(!total_count);
3208
3209 if (rdev->coupling_desc.n_coupled > 1)
3210 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
3211
3212 regulator_unlock_dependent(rdev, &ww_ctx);
3213 }
3214
3215 /**
3216 * regulator_disable_deferred - disable regulator output with delay
3217 * @regulator: regulator source
3218 * @ms: milliseconds until the regulator is disabled
3219 *
3220 * Execute regulator_disable() on the regulator after a delay. This
3221 * is intended for use with devices that require some time to quiesce.
3222 *
3223 * NOTE: this will only disable the regulator output if no other consumer
3224 * devices have it enabled, the regulator device supports disabling and
3225 * machine constraints permit this operation.
3226 *
3227 * Return: 0 on success or a negative error number on failure.
3228 */
3229 int regulator_disable_deferred(struct regulator *regulator, int ms)
3230 {
3231 struct regulator_dev *rdev = regulator->rdev;
3232
3233 if (!ms)
3234 return regulator_disable(regulator);
3235
3236 regulator_lock(rdev);
3237 regulator->deferred_disables++;
3238 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
3239 msecs_to_jiffies(ms));
3240 regulator_unlock(rdev);
3241
3242 return 0;
3243 }
3244 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
3245
3246 static int _regulator_is_enabled(struct regulator_dev *rdev)
3247 {
3248 /* A GPIO control always takes precedence */
3249 if (rdev->ena_pin)
3250 return rdev->ena_gpio_state;
3251
3252 /* If we don't know then assume that the regulator is always on */
3253 if (!rdev->desc->ops->is_enabled)
3254 return 1;
3255
3256 return rdev->desc->ops->is_enabled(rdev);
3257 }
3258
3259 static int _regulator_list_voltage(struct regulator_dev *rdev,
3260 unsigned selector, int lock)
3261 {
3262 const struct regulator_ops *ops = rdev->desc->ops;
3263 int ret;
3264
3265 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
3266 return rdev->desc->fixed_uV;
3267
3268 if (ops->list_voltage) {
3269 if (selector >= rdev->desc->n_voltages)
3270 return -EINVAL;
3271 if (selector < rdev->desc->linear_min_sel)
3272 return 0;
3273 if (lock)
3274 regulator_lock(rdev);
3275 ret = ops->list_voltage(rdev, selector);
3276 if (lock)
3277 regulator_unlock(rdev);
3278 } else if (rdev->is_switch && rdev->supply) {
3279 ret = _regulator_list_voltage(rdev->supply->rdev,
3280 selector, lock);
3281 } else {
3282 return -EINVAL;
3283 }
3284
3285 if (ret > 0) {
3286 if (ret < rdev->constraints->min_uV)
3287 ret = 0;
3288 else if (ret > rdev->constraints->max_uV)
3289 ret = 0;
3290 }
3291
3292 return ret;
3293 }
3294
3295 /**
3296 * regulator_is_enabled - is the regulator output enabled
3297 * @regulator: regulator source
3298 *
3299 * Note that the device backing this regulator handle can have multiple
3300 * users, so it might be enabled even if regulator_enable() was never
3301 * called for this particular source.
3302 *
3303 * Return: Positive if the regulator driver backing the source/client
3304 * has requested that the device be enabled, zero if it hasn't,
3305 * else a negative error number.
3306 */
3307 int regulator_is_enabled(struct regulator *regulator)
3308 {
3309 int ret;
3310
3311 if (regulator->always_on)
3312 return 1;
3313
3314 regulator_lock(regulator->rdev);
3315 ret = _regulator_is_enabled(regulator->rdev);
3316 regulator_unlock(regulator->rdev);
3317
3318 return ret;
3319 }
3320 EXPORT_SYMBOL_GPL(regulator_is_enabled);
3321
3322 /**
3323 * regulator_count_voltages - count regulator_list_voltage() selectors
3324 * @regulator: regulator source
3325 *
3326 * Return: Number of selectors for @regulator, or negative error number.
3327 *
3328 * Selectors are numbered starting at zero, and typically correspond to
3329 * bitfields in hardware registers.
3330 */
3331 int regulator_count_voltages(struct regulator *regulator)
3332 {
3333 struct regulator_dev *rdev = regulator->rdev;
3334
3335 if (rdev->desc->n_voltages)
3336 return rdev->desc->n_voltages;
3337
3338 if (!rdev->is_switch || !rdev->supply)
3339 return -EINVAL;
3340
3341 return regulator_count_voltages(rdev->supply);
3342 }
3343 EXPORT_SYMBOL_GPL(regulator_count_voltages);
3344
3345 /**
3346 * regulator_list_voltage - enumerate supported voltages
3347 * @regulator: regulator source
3348 * @selector: identify voltage to list
3349 * Context: can sleep
3350 *
3351 * Return: Voltage for @selector that can be passed to regulator_set_voltage(),
3352 * 0 if @selector can't be used on this system, or a negative error
3353 * number on failure.
3354 */
3355 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
3356 {
3357 return _regulator_list_voltage(regulator->rdev, selector, 1);
3358 }
3359 EXPORT_SYMBOL_GPL(regulator_list_voltage);
3360
3361 /**
3362 * regulator_get_regmap - get the regulator's register map
3363 * @regulator: regulator source
3364 *
3365 * Return: Pointer to the &struct regmap for @regulator, or ERR_PTR()
3366 * encoded -%EOPNOTSUPP if @regulator doesn't use regmap.
3367 */
3368 struct regmap *regulator_get_regmap(struct regulator *regulator)
3369 {
3370 struct regmap *map = regulator->rdev->regmap;
3371
3372 return map ? map : ERR_PTR(-EOPNOTSUPP);
3373 }
3374 EXPORT_SYMBOL_GPL(regulator_get_regmap);
3375
3376 /**
3377 * regulator_get_hardware_vsel_register - get the HW voltage selector register
3378 * @regulator: regulator source
3379 * @vsel_reg: voltage selector register, output parameter
3380 * @vsel_mask: mask for voltage selector bitfield, output parameter
3381 *
3382 * Returns the hardware register offset and bitmask used for setting the
3383 * regulator voltage. This might be useful when configuring voltage-scaling
3384 * hardware or firmware that can make I2C requests behind the kernel's back,
3385 * for example.
3386 *
3387 * Return: 0 on success, or -%EOPNOTSUPP if the regulator does not support
3388 * voltage selectors.
3389 *
3390 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
3391 * and 0 is returned, otherwise a negative error number is returned.
3392 */
3393 int regulator_get_hardware_vsel_register(struct regulator *regulator,
3394 unsigned *vsel_reg,
3395 unsigned *vsel_mask)
3396 {
3397 struct regulator_dev *rdev = regulator->rdev;
3398 const struct regulator_ops *ops = rdev->desc->ops;
3399
3400 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3401 return -EOPNOTSUPP;
3402
3403 *vsel_reg = rdev->desc->vsel_reg;
3404 *vsel_mask = rdev->desc->vsel_mask;
3405
3406 return 0;
3407 }
3408 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
3409
3410 /**
3411 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
3412 * @regulator: regulator source
3413 * @selector: identify voltage to list
3414 *
3415 * Converts the selector to a hardware-specific voltage selector that can be
3416 * directly written to the regulator registers. The address of the voltage
3417 * register can be determined by calling @regulator_get_hardware_vsel_register.
3418 *
3419 * Return: 0 on success, -%EINVAL if the selector is outside the supported
3420 * range, or -%EOPNOTSUPP if the regulator does not support voltage
3421 * selectors.
3422 */
3423 int regulator_list_hardware_vsel(struct regulator *regulator,
3424 unsigned selector)
3425 {
3426 struct regulator_dev *rdev = regulator->rdev;
3427 const struct regulator_ops *ops = rdev->desc->ops;
3428
3429 if (selector >= rdev->desc->n_voltages)
3430 return -EINVAL;
3431 if (selector < rdev->desc->linear_min_sel)
3432 return 0;
3433 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3434 return -EOPNOTSUPP;
3435
3436 return selector;
3437 }
3438 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
3439
3440 /**
3441 * regulator_hardware_enable - access the HW for enable/disable regulator
3442 * @regulator: regulator source
3443 * @enable: true for enable, false for disable
3444 *
3445 * Request that the regulator be enabled/disabled with the regulator output at
3446 * the predefined voltage or current value.
3447 *
3448 * Return: 0 on success or a negative error number on failure.
3449 */
3450 int regulator_hardware_enable(struct regulator *regulator, bool enable)
3451 {
3452 struct regulator_dev *rdev = regulator->rdev;
3453 const struct regulator_ops *ops = rdev->desc->ops;
3454 int ret = -EOPNOTSUPP;
3455
3456 if (!rdev->exclusive || !ops || !ops->enable || !ops->disable)
3457 return ret;
3458
3459 if (enable)
3460 ret = ops->enable(rdev);
3461 else
3462 ret = ops->disable(rdev);
3463
3464 return ret;
3465 }
3466 EXPORT_SYMBOL_GPL(regulator_hardware_enable);
3467
3468 /**
3469 * regulator_get_linear_step - return the voltage step size between VSEL values
3470 * @regulator: regulator source
3471 *
3472 * Return: The voltage step size between VSEL values for linear regulators,
3473 * or 0 if the regulator isn't a linear regulator.
3474 */
3475 unsigned int regulator_get_linear_step(struct regulator *regulator)
3476 {
3477 struct regulator_dev *rdev = regulator->rdev;
3478
3479 return rdev->desc->uV_step;
3480 }
3481 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
3482
3483 /**
3484 * regulator_is_supported_voltage - check if a voltage range can be supported
3485 *
3486 * @regulator: Regulator to check.
3487 * @min_uV: Minimum required voltage in uV.
3488 * @max_uV: Maximum required voltage in uV.
3489 *
3490 * Return: 1 if the voltage range is supported, 0 if not, or a negative error
3491 * number if @regulator's voltage can't be changed and voltage readback
3492 * failed.
3493 */
3494 int regulator_is_supported_voltage(struct regulator *regulator,
3495 int min_uV, int max_uV)
3496 {
3497 struct regulator_dev *rdev = regulator->rdev;
3498 int i, voltages, ret;
3499
3500 /* If we can't change voltage check the current voltage */
3501 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3502 ret = regulator_get_voltage(regulator);
3503 if (ret >= 0)
3504 return min_uV <= ret && ret <= max_uV;
3505 else
3506 return ret;
3507 }
3508
3509 /* Any voltage within constrains range is fine? */
3510 if (rdev->desc->continuous_voltage_range)
3511 return min_uV >= rdev->constraints->min_uV &&
3512 max_uV <= rdev->constraints->max_uV;
3513
3514 ret = regulator_count_voltages(regulator);
3515 if (ret < 0)
3516 return 0;
3517 voltages = ret;
3518
3519 for (i = 0; i < voltages; i++) {
3520 ret = regulator_list_voltage(regulator, i);
3521
3522 if (ret >= min_uV && ret <= max_uV)
3523 return 1;
3524 }
3525
3526 return 0;
3527 }
3528 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
3529
3530 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
3531 int max_uV)
3532 {
3533 const struct regulator_desc *desc = rdev->desc;
3534
3535 if (desc->ops->map_voltage)
3536 return desc->ops->map_voltage(rdev, min_uV, max_uV);
3537
3538 if (desc->ops->list_voltage == regulator_list_voltage_linear)
3539 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
3540
3541 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
3542 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
3543
3544 if (desc->ops->list_voltage ==
3545 regulator_list_voltage_pickable_linear_range)
3546 return regulator_map_voltage_pickable_linear_range(rdev,
3547 min_uV, max_uV);
3548
3549 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
3550 }
3551
3552 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3553 int min_uV, int max_uV,
3554 unsigned *selector)
3555 {
3556 struct pre_voltage_change_data data;
3557 int ret;
3558
3559 data.old_uV = regulator_get_voltage_rdev(rdev);
3560 data.min_uV = min_uV;
3561 data.max_uV = max_uV;
3562 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3563 &data);
3564 if (ret & NOTIFY_STOP_MASK)
3565 return -EINVAL;
3566
3567 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
3568 if (ret >= 0)
3569 return ret;
3570
3571 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3572 (void *)data.old_uV);
3573
3574 return ret;
3575 }
3576
3577 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3578 int uV, unsigned selector)
3579 {
3580 struct pre_voltage_change_data data;
3581 int ret;
3582
3583 data.old_uV = regulator_get_voltage_rdev(rdev);
3584 data.min_uV = uV;
3585 data.max_uV = uV;
3586 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3587 &data);
3588 if (ret & NOTIFY_STOP_MASK)
3589 return -EINVAL;
3590
3591 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
3592 if (ret >= 0)
3593 return ret;
3594
3595 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3596 (void *)data.old_uV);
3597
3598 return ret;
3599 }
3600
3601 static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev,
3602 int uV, int new_selector)
3603 {
3604 const struct regulator_ops *ops = rdev->desc->ops;
3605 int diff, old_sel, curr_sel, ret;
3606
3607 /* Stepping is only needed if the regulator is enabled. */
3608 if (!_regulator_is_enabled(rdev))
3609 goto final_set;
3610
3611 if (!ops->get_voltage_sel)
3612 return -EINVAL;
3613
3614 old_sel = ops->get_voltage_sel(rdev);
3615 if (old_sel < 0)
3616 return old_sel;
3617
3618 diff = new_selector - old_sel;
3619 if (diff == 0)
3620 return 0; /* No change needed. */
3621
3622 if (diff > 0) {
3623 /* Stepping up. */
3624 for (curr_sel = old_sel + rdev->desc->vsel_step;
3625 curr_sel < new_selector;
3626 curr_sel += rdev->desc->vsel_step) {
3627 /*
3628 * Call the callback directly instead of using
3629 * _regulator_call_set_voltage_sel() as we don't
3630 * want to notify anyone yet. Same in the branch
3631 * below.
3632 */
3633 ret = ops->set_voltage_sel(rdev, curr_sel);
3634 if (ret)
3635 goto try_revert;
3636 }
3637 } else {
3638 /* Stepping down. */
3639 for (curr_sel = old_sel - rdev->desc->vsel_step;
3640 curr_sel > new_selector;
3641 curr_sel -= rdev->desc->vsel_step) {
3642 ret = ops->set_voltage_sel(rdev, curr_sel);
3643 if (ret)
3644 goto try_revert;
3645 }
3646 }
3647
3648 final_set:
3649 /* The final selector will trigger the notifiers. */
3650 return _regulator_call_set_voltage_sel(rdev, uV, new_selector);
3651
3652 try_revert:
3653 /*
3654 * At least try to return to the previous voltage if setting a new
3655 * one failed.
3656 */
3657 (void)ops->set_voltage_sel(rdev, old_sel);
3658 return ret;
3659 }
3660
3661 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3662 int old_uV, int new_uV)
3663 {
3664 unsigned int ramp_delay = 0;
3665
3666 if (rdev->constraints->ramp_delay)
3667 ramp_delay = rdev->constraints->ramp_delay;
3668 else if (rdev->desc->ramp_delay)
3669 ramp_delay = rdev->desc->ramp_delay;
3670 else if (rdev->constraints->settling_time)
3671 return rdev->constraints->settling_time;
3672 else if (rdev->constraints->settling_time_up &&
3673 (new_uV > old_uV))
3674 return rdev->constraints->settling_time_up;
3675 else if (rdev->constraints->settling_time_down &&
3676 (new_uV < old_uV))
3677 return rdev->constraints->settling_time_down;
3678
3679 if (ramp_delay == 0)
3680 return 0;
3681
3682 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
3683 }
3684
3685 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3686 int min_uV, int max_uV)
3687 {
3688 int ret;
3689 int delay = 0;
3690 int best_val = 0;
3691 unsigned int selector;
3692 int old_selector = -1;
3693 const struct regulator_ops *ops = rdev->desc->ops;
3694 int old_uV = regulator_get_voltage_rdev(rdev);
3695
3696 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3697
3698 min_uV += rdev->constraints->uV_offset;
3699 max_uV += rdev->constraints->uV_offset;
3700
3701 /*
3702 * If we can't obtain the old selector there is not enough
3703 * info to call set_voltage_time_sel().
3704 */
3705 if (_regulator_is_enabled(rdev) &&
3706 ops->set_voltage_time_sel && ops->get_voltage_sel) {
3707 old_selector = ops->get_voltage_sel(rdev);
3708 if (old_selector < 0)
3709 return old_selector;
3710 }
3711
3712 if (ops->set_voltage) {
3713 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
3714 &selector);
3715
3716 if (ret >= 0) {
3717 if (ops->list_voltage)
3718 best_val = ops->list_voltage(rdev,
3719 selector);
3720 else
3721 best_val = regulator_get_voltage_rdev(rdev);
3722 }
3723
3724 } else if (ops->set_voltage_sel) {
3725 ret = regulator_map_voltage(rdev, min_uV, max_uV);
3726 if (ret >= 0) {
3727 best_val = ops->list_voltage(rdev, ret);
3728 if (min_uV <= best_val && max_uV >= best_val) {
3729 selector = ret;
3730 if (old_selector == selector)
3731 ret = 0;
3732 else if (rdev->desc->vsel_step)
3733 ret = _regulator_set_voltage_sel_step(
3734 rdev, best_val, selector);
3735 else
3736 ret = _regulator_call_set_voltage_sel(
3737 rdev, best_val, selector);
3738 } else {
3739 ret = -EINVAL;
3740 }
3741 }
3742 } else {
3743 ret = -EINVAL;
3744 }
3745
3746 if (ret)
3747 goto out;
3748
3749 if (ops->set_voltage_time_sel) {
3750 /*
3751 * Call set_voltage_time_sel if successfully obtained
3752 * old_selector
3753 */
3754 if (old_selector >= 0 && old_selector != selector)
3755 delay = ops->set_voltage_time_sel(rdev, old_selector,
3756 selector);
3757 } else {
3758 if (old_uV != best_val) {
3759 if (ops->set_voltage_time)
3760 delay = ops->set_voltage_time(rdev, old_uV,
3761 best_val);
3762 else
3763 delay = _regulator_set_voltage_time(rdev,
3764 old_uV,
3765 best_val);
3766 }
3767 }
3768
3769 if (delay < 0) {
3770 rdev_warn(rdev, "failed to get delay: %pe\n", ERR_PTR(delay));
3771 delay = 0;
3772 }
3773
3774 /* Insert any necessary delays */
3775 fsleep(delay);
3776
3777 if (best_val >= 0) {
3778 unsigned long data = best_val;
3779
3780 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3781 (void *)data);
3782 }
3783
3784 out:
3785 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3786
3787 return ret;
3788 }
3789
3790 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3791 int min_uV, int max_uV, suspend_state_t state)
3792 {
3793 struct regulator_state *rstate;
3794 int uV, sel;
3795
3796 rstate = regulator_get_suspend_state(rdev, state);
3797 if (rstate == NULL)
3798 return -EINVAL;
3799
3800 if (min_uV < rstate->min_uV)
3801 min_uV = rstate->min_uV;
3802 if (max_uV > rstate->max_uV)
3803 max_uV = rstate->max_uV;
3804
3805 sel = regulator_map_voltage(rdev, min_uV, max_uV);
3806 if (sel < 0)
3807 return sel;
3808
3809 uV = rdev->desc->ops->list_voltage(rdev, sel);
3810 if (uV >= min_uV && uV <= max_uV)
3811 rstate->uV = uV;
3812
3813 return 0;
3814 }
3815
3816 static int regulator_get_voltage_delta(struct regulator_dev *rdev, int uV)
3817 {
3818 int current_uV = regulator_get_voltage_rdev(rdev);
3819
3820 if (current_uV < 0)
3821 return current_uV;
3822
3823 return abs(current_uV - uV);
3824 }
3825
3826 static int regulator_set_voltage_unlocked(struct regulator *regulator,
3827 int min_uV, int max_uV,
3828 suspend_state_t state)
3829 {
3830 struct regulator_dev *rdev = regulator->rdev;
3831 struct regulator_voltage *voltage = ®ulator->voltage[state];
3832 int ret = 0;
3833 int current_uV, delta, new_delta;
3834 int old_min_uV, old_max_uV;
3835
3836 /* If we're setting the same range as last time the change
3837 * should be a noop (some cpufreq implementations use the same
3838 * voltage for multiple frequencies, for example).
3839 */
3840 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3841 goto out;
3842
3843 /* If we're trying to set a range that overlaps the current voltage,
3844 * return successfully even though the regulator does not support
3845 * changing the voltage.
3846 */
3847 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3848 current_uV = regulator_get_voltage_rdev(rdev);
3849 if (min_uV <= current_uV && current_uV <= max_uV) {
3850 voltage->min_uV = min_uV;
3851 voltage->max_uV = max_uV;
3852 goto out;
3853 }
3854 }
3855
3856 /* sanity check */
3857 if (!rdev->desc->ops->set_voltage &&
3858 !rdev->desc->ops->set_voltage_sel) {
3859 ret = -EINVAL;
3860 goto out;
3861 }
3862
3863 /* constraints check */
3864 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3865 if (ret < 0)
3866 goto out;
3867
3868 /* restore original values in case of error */
3869 old_min_uV = voltage->min_uV;
3870 old_max_uV = voltage->max_uV;
3871 voltage->min_uV = min_uV;
3872 voltage->max_uV = max_uV;
3873
3874 /* for not coupled regulators this will just set the voltage */
3875 ret = regulator_balance_voltage(rdev, state);
3876 if (ret < 0) {
3877 voltage->min_uV = old_min_uV;
3878 voltage->max_uV = old_max_uV;
3879 }
3880
3881 if (rdev->constraints->max_uV_step > 0) {
3882 /* For regulators with a maximum voltage step, reaching the desired
3883 * voltage might take a few retries.
3884 */
3885 ret = regulator_get_voltage_delta(rdev, min_uV);
3886 if (ret < 0)
3887 goto out;
3888
3889 delta = ret;
3890
3891 while (delta > 0) {
3892 ret = regulator_balance_voltage(rdev, state);
3893 if (ret < 0)
3894 goto out;
3895
3896 ret = regulator_get_voltage_delta(rdev, min_uV);
3897 if (ret < 0)
3898 goto out;
3899
3900 new_delta = ret;
3901
3902 /* check that voltage is converging quickly enough */
3903 if (delta - new_delta < rdev->constraints->max_uV_step) {
3904 ret = -EWOULDBLOCK;
3905 goto out;
3906 }
3907
3908 delta = new_delta;
3909 }
3910 }
3911
3912 out:
3913 return ret;
3914 }
3915
3916 int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3917 int max_uV, suspend_state_t state)
3918 {
3919 int best_supply_uV = 0;
3920 int supply_change_uV = 0;
3921 int ret;
3922
3923 if (rdev->supply &&
3924 regulator_ops_is_valid(rdev->supply->rdev,
3925 REGULATOR_CHANGE_VOLTAGE) &&
3926 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3927 rdev->desc->ops->get_voltage_sel))) {
3928 int current_supply_uV;
3929 int selector;
3930
3931 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3932 if (selector < 0) {
3933 ret = selector;
3934 goto out;
3935 }
3936
3937 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3938 if (best_supply_uV < 0) {
3939 ret = best_supply_uV;
3940 goto out;
3941 }
3942
3943 best_supply_uV += rdev->desc->min_dropout_uV;
3944
3945 current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
3946 if (current_supply_uV < 0) {
3947 ret = current_supply_uV;
3948 goto out;
3949 }
3950
3951 supply_change_uV = best_supply_uV - current_supply_uV;
3952 }
3953
3954 if (supply_change_uV > 0) {
3955 ret = regulator_set_voltage_unlocked(rdev->supply,
3956 best_supply_uV, INT_MAX, state);
3957 if (ret) {
3958 dev_err(&rdev->dev, "Failed to increase supply voltage: %pe\n",
3959 ERR_PTR(ret));
3960 goto out;
3961 }
3962 }
3963
3964 if (state == PM_SUSPEND_ON)
3965 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3966 else
3967 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3968 max_uV, state);
3969 if (ret < 0)
3970 goto out;
3971
3972 if (supply_change_uV < 0) {
3973 ret = regulator_set_voltage_unlocked(rdev->supply,
3974 best_supply_uV, INT_MAX, state);
3975 if (ret)
3976 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %pe\n",
3977 ERR_PTR(ret));
3978 /* No need to fail here */
3979 ret = 0;
3980 }
3981
3982 out:
3983 return ret;
3984 }
3985 EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev);
3986
3987 static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3988 int *current_uV, int *min_uV)
3989 {
3990 struct regulation_constraints *constraints = rdev->constraints;
3991
3992 /* Limit voltage change only if necessary */
3993 if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3994 return 1;
3995
3996 if (*current_uV < 0) {
3997 *current_uV = regulator_get_voltage_rdev(rdev);
3998
3999 if (*current_uV < 0)
4000 return *current_uV;
4001 }
4002
4003 if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
4004 return 1;
4005
4006 /* Clamp target voltage within the given step */
4007 if (*current_uV < *min_uV)
4008 *min_uV = min(*current_uV + constraints->max_uV_step,
4009 *min_uV);
4010 else
4011 *min_uV = max(*current_uV - constraints->max_uV_step,
4012 *min_uV);
4013
4014 return 0;
4015 }
4016
4017 static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
4018 int *current_uV,
4019 int *min_uV, int *max_uV,
4020 suspend_state_t state,
4021 int n_coupled)
4022 {
4023 struct coupling_desc *c_desc = &rdev->coupling_desc;
4024 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
4025 struct regulation_constraints *constraints = rdev->constraints;
4026 int desired_min_uV = 0, desired_max_uV = INT_MAX;
4027 int max_current_uV = 0, min_current_uV = INT_MAX;
4028 int highest_min_uV = 0, target_uV, possible_uV;
4029 int i, ret, max_spread;
4030 bool done;
4031
4032 *current_uV = -1;
4033
4034 /*
4035 * If there are no coupled regulators, simply set the voltage
4036 * demanded by consumers.
4037 */
4038 if (n_coupled == 1) {
4039 /*
4040 * If consumers don't provide any demands, set voltage
4041 * to min_uV
4042 */
4043 desired_min_uV = constraints->min_uV;
4044 desired_max_uV = constraints->max_uV;
4045
4046 ret = regulator_check_consumers(rdev,
4047 &desired_min_uV,
4048 &desired_max_uV, state);
4049 if (ret < 0)
4050 return ret;
4051
4052 done = true;
4053
4054 goto finish;
4055 }
4056
4057 /* Find highest min desired voltage */
4058 for (i = 0; i < n_coupled; i++) {
4059 int tmp_min = 0;
4060 int tmp_max = INT_MAX;
4061
4062 lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
4063
4064 ret = regulator_check_consumers(c_rdevs[i],
4065 &tmp_min,
4066 &tmp_max, state);
4067 if (ret < 0)
4068 return ret;
4069
4070 ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
4071 if (ret < 0)
4072 return ret;
4073
4074 highest_min_uV = max(highest_min_uV, tmp_min);
4075
4076 if (i == 0) {
4077 desired_min_uV = tmp_min;
4078 desired_max_uV = tmp_max;
4079 }
4080 }
4081
4082 max_spread = constraints->max_spread[0];
4083
4084 /*
4085 * Let target_uV be equal to the desired one if possible.
4086 * If not, set it to minimum voltage, allowed by other coupled
4087 * regulators.
4088 */
4089 target_uV = max(desired_min_uV, highest_min_uV - max_spread);
4090
4091 /*
4092 * Find min and max voltages, which currently aren't violating
4093 * max_spread.
4094 */
4095 for (i = 1; i < n_coupled; i++) {
4096 int tmp_act;
4097
4098 if (!_regulator_is_enabled(c_rdevs[i]))
4099 continue;
4100
4101 tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
4102 if (tmp_act < 0)
4103 return tmp_act;
4104
4105 min_current_uV = min(tmp_act, min_current_uV);
4106 max_current_uV = max(tmp_act, max_current_uV);
4107 }
4108
4109 /* There aren't any other regulators enabled */
4110 if (max_current_uV == 0) {
4111 possible_uV = target_uV;
4112 } else {
4113 /*
4114 * Correct target voltage, so as it currently isn't
4115 * violating max_spread
4116 */
4117 possible_uV = max(target_uV, max_current_uV - max_spread);
4118 possible_uV = min(possible_uV, min_current_uV + max_spread);
4119 }
4120
4121 if (possible_uV > desired_max_uV)
4122 return -EINVAL;
4123
4124 done = (possible_uV == target_uV);
4125 desired_min_uV = possible_uV;
4126
4127 finish:
4128 /* Apply max_uV_step constraint if necessary */
4129 if (state == PM_SUSPEND_ON) {
4130 ret = regulator_limit_voltage_step(rdev, current_uV,
4131 &desired_min_uV);
4132 if (ret < 0)
4133 return ret;
4134
4135 if (ret == 0)
4136 done = false;
4137 }
4138
4139 /* Set current_uV if wasn't done earlier in the code and if necessary */
4140 if (n_coupled > 1 && *current_uV == -1) {
4141
4142 if (_regulator_is_enabled(rdev)) {
4143 ret = regulator_get_voltage_rdev(rdev);
4144 if (ret < 0)
4145 return ret;
4146
4147 *current_uV = ret;
4148 } else {
4149 *current_uV = desired_min_uV;
4150 }
4151 }
4152
4153 *min_uV = desired_min_uV;
4154 *max_uV = desired_max_uV;
4155
4156 return done;
4157 }
4158
4159 int regulator_do_balance_voltage(struct regulator_dev *rdev,
4160 suspend_state_t state, bool skip_coupled)
4161 {
4162 struct regulator_dev **c_rdevs;
4163 struct regulator_dev *best_rdev;
4164 struct coupling_desc *c_desc = &rdev->coupling_desc;
4165 int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
4166 unsigned int delta, best_delta;
4167 unsigned long c_rdev_done = 0;
4168 bool best_c_rdev_done;
4169
4170 c_rdevs = c_desc->coupled_rdevs;
4171 n_coupled = skip_coupled ? 1 : c_desc->n_coupled;
4172
4173 /*
4174 * Find the best possible voltage change on each loop. Leave the loop
4175 * if there isn't any possible change.
4176 */
4177 do {
4178 best_c_rdev_done = false;
4179 best_delta = 0;
4180 best_min_uV = 0;
4181 best_max_uV = 0;
4182 best_c_rdev = 0;
4183 best_rdev = NULL;
4184
4185 /*
4186 * Find highest difference between optimal voltage
4187 * and current voltage.
4188 */
4189 for (i = 0; i < n_coupled; i++) {
4190 /*
4191 * optimal_uV is the best voltage that can be set for
4192 * i-th regulator at the moment without violating
4193 * max_spread constraint in order to balance
4194 * the coupled voltages.
4195 */
4196 int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
4197
4198 if (test_bit(i, &c_rdev_done))
4199 continue;
4200
4201 ret = regulator_get_optimal_voltage(c_rdevs[i],
4202 ¤t_uV,
4203 &optimal_uV,
4204 &optimal_max_uV,
4205 state, n_coupled);
4206 if (ret < 0)
4207 goto out;
4208
4209 delta = abs(optimal_uV - current_uV);
4210
4211 if (delta && best_delta <= delta) {
4212 best_c_rdev_done = ret;
4213 best_delta = delta;
4214 best_rdev = c_rdevs[i];
4215 best_min_uV = optimal_uV;
4216 best_max_uV = optimal_max_uV;
4217 best_c_rdev = i;
4218 }
4219 }
4220
4221 /* Nothing to change, return successfully */
4222 if (!best_rdev) {
4223 ret = 0;
4224 goto out;
4225 }
4226
4227 ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
4228 best_max_uV, state);
4229
4230 if (ret < 0)
4231 goto out;
4232
4233 if (best_c_rdev_done)
4234 set_bit(best_c_rdev, &c_rdev_done);
4235
4236 } while (n_coupled > 1);
4237
4238 out:
4239 return ret;
4240 }
4241
4242 static int regulator_balance_voltage(struct regulator_dev *rdev,
4243 suspend_state_t state)
4244 {
4245 struct coupling_desc *c_desc = &rdev->coupling_desc;
4246 struct regulator_coupler *coupler = c_desc->coupler;
4247 bool skip_coupled = false;
4248
4249 /*
4250 * If system is in a state other than PM_SUSPEND_ON, don't check
4251 * other coupled regulators.
4252 */
4253 if (state != PM_SUSPEND_ON)
4254 skip_coupled = true;
4255
4256 if (c_desc->n_resolved < c_desc->n_coupled) {
4257 rdev_err(rdev, "Not all coupled regulators registered\n");
4258 return -EPERM;
4259 }
4260
4261 /* Invoke custom balancer for customized couplers */
4262 if (coupler && coupler->balance_voltage)
4263 return coupler->balance_voltage(coupler, rdev, state);
4264
4265 return regulator_do_balance_voltage(rdev, state, skip_coupled);
4266 }
4267
4268 /**
4269 * regulator_set_voltage - set regulator output voltage
4270 * @regulator: regulator source
4271 * @min_uV: Minimum required voltage in uV
4272 * @max_uV: Maximum acceptable voltage in uV
4273 *
4274 * Sets a voltage regulator to the desired output voltage. This can be set
4275 * during any regulator state. IOW, regulator can be disabled or enabled.
4276 *
4277 * If the regulator is enabled then the voltage will change to the new value
4278 * immediately otherwise if the regulator is disabled the regulator will
4279 * output at the new voltage when enabled.
4280 *
4281 * NOTE: If the regulator is shared between several devices then the lowest
4282 * request voltage that meets the system constraints will be used.
4283 * Regulator system constraints must be set for this regulator before
4284 * calling this function otherwise this call will fail.
4285 *
4286 * Return: 0 on success or a negative error number on failure.
4287 */
4288 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
4289 {
4290 struct ww_acquire_ctx ww_ctx;
4291 int ret;
4292
4293 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4294
4295 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
4296 PM_SUSPEND_ON);
4297
4298 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4299
4300 return ret;
4301 }
4302 EXPORT_SYMBOL_GPL(regulator_set_voltage);
4303
4304 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
4305 suspend_state_t state, bool en)
4306 {
4307 struct regulator_state *rstate;
4308
4309 rstate = regulator_get_suspend_state(rdev, state);
4310 if (rstate == NULL)
4311 return -EINVAL;
4312
4313 if (!rstate->changeable)
4314 return -EPERM;
4315
4316 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
4317
4318 return 0;
4319 }
4320
4321 int regulator_suspend_enable(struct regulator_dev *rdev,
4322 suspend_state_t state)
4323 {
4324 return regulator_suspend_toggle(rdev, state, true);
4325 }
4326 EXPORT_SYMBOL_GPL(regulator_suspend_enable);
4327
4328 int regulator_suspend_disable(struct regulator_dev *rdev,
4329 suspend_state_t state)
4330 {
4331 struct regulator *regulator;
4332 struct regulator_voltage *voltage;
4333
4334 /*
4335 * if any consumer wants this regulator device keeping on in
4336 * suspend states, don't set it as disabled.
4337 */
4338 list_for_each_entry(regulator, &rdev->consumer_list, list) {
4339 voltage = ®ulator->voltage[state];
4340 if (voltage->min_uV || voltage->max_uV)
4341 return 0;
4342 }
4343
4344 return regulator_suspend_toggle(rdev, state, false);
4345 }
4346 EXPORT_SYMBOL_GPL(regulator_suspend_disable);
4347
4348 static int _regulator_set_suspend_voltage(struct regulator *regulator,
4349 int min_uV, int max_uV,
4350 suspend_state_t state)
4351 {
4352 struct regulator_dev *rdev = regulator->rdev;
4353 struct regulator_state *rstate;
4354
4355 rstate = regulator_get_suspend_state(rdev, state);
4356 if (rstate == NULL)
4357 return -EINVAL;
4358
4359 if (rstate->min_uV == rstate->max_uV) {
4360 rdev_err(rdev, "The suspend voltage can't be changed!\n");
4361 return -EPERM;
4362 }
4363
4364 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
4365 }
4366
4367 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
4368 int max_uV, suspend_state_t state)
4369 {
4370 struct ww_acquire_ctx ww_ctx;
4371 int ret;
4372
4373 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
4374 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
4375 return -EINVAL;
4376
4377 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4378
4379 ret = _regulator_set_suspend_voltage(regulator, min_uV,
4380 max_uV, state);
4381
4382 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4383
4384 return ret;
4385 }
4386 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
4387
4388 /**
4389 * regulator_set_voltage_time - get raise/fall time
4390 * @regulator: regulator source
4391 * @old_uV: starting voltage in microvolts
4392 * @new_uV: target voltage in microvolts
4393 *
4394 * Provided with the starting and ending voltage, this function attempts to
4395 * calculate the time in microseconds required to rise or fall to this new
4396 * voltage.
4397 *
4398 * Return: ramp time in microseconds, or a negative error number if calculation failed.
4399 */
4400 int regulator_set_voltage_time(struct regulator *regulator,
4401 int old_uV, int new_uV)
4402 {
4403 struct regulator_dev *rdev = regulator->rdev;
4404 const struct regulator_ops *ops = rdev->desc->ops;
4405 int old_sel = -1;
4406 int new_sel = -1;
4407 int voltage;
4408 int i;
4409
4410 if (ops->set_voltage_time)
4411 return ops->set_voltage_time(rdev, old_uV, new_uV);
4412 else if (!ops->set_voltage_time_sel)
4413 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
4414
4415 /* Currently requires operations to do this */
4416 if (!ops->list_voltage || !rdev->desc->n_voltages)
4417 return -EINVAL;
4418
4419 for (i = 0; i < rdev->desc->n_voltages; i++) {
4420 /* We only look for exact voltage matches here */
4421 if (i < rdev->desc->linear_min_sel)
4422 continue;
4423
4424 if (old_sel >= 0 && new_sel >= 0)
4425 break;
4426
4427 voltage = regulator_list_voltage(regulator, i);
4428 if (voltage < 0)
4429 return -EINVAL;
4430 if (voltage == 0)
4431 continue;
4432 if (voltage == old_uV)
4433 old_sel = i;
4434 if (voltage == new_uV)
4435 new_sel = i;
4436 }
4437
4438 if (old_sel < 0 || new_sel < 0)
4439 return -EINVAL;
4440
4441 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
4442 }
4443 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
4444
4445 /**
4446 * regulator_set_voltage_time_sel - get raise/fall time
4447 * @rdev: regulator source device
4448 * @old_selector: selector for starting voltage
4449 * @new_selector: selector for target voltage
4450 *
4451 * Provided with the starting and target voltage selectors, this function
4452 * returns time in microseconds required to rise or fall to this new voltage
4453 *
4454 * Drivers providing ramp_delay in regulation_constraints can use this as their
4455 * set_voltage_time_sel() operation.
4456 *
4457 * Return: ramp time in microseconds, or a negative error number if calculation failed.
4458 */
4459 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
4460 unsigned int old_selector,
4461 unsigned int new_selector)
4462 {
4463 int old_volt, new_volt;
4464
4465 /* sanity check */
4466 if (!rdev->desc->ops->list_voltage)
4467 return -EINVAL;
4468
4469 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
4470 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
4471
4472 if (rdev->desc->ops->set_voltage_time)
4473 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
4474 new_volt);
4475 else
4476 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
4477 }
4478 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
4479
4480 int regulator_sync_voltage_rdev(struct regulator_dev *rdev)
4481 {
4482 int ret;
4483
4484 regulator_lock(rdev);
4485
4486 if (!rdev->desc->ops->set_voltage &&
4487 !rdev->desc->ops->set_voltage_sel) {
4488 ret = -EINVAL;
4489 goto out;
4490 }
4491
4492 /* balance only, if regulator is coupled */
4493 if (rdev->coupling_desc.n_coupled > 1)
4494 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
4495 else
4496 ret = -EOPNOTSUPP;
4497
4498 out:
4499 regulator_unlock(rdev);
4500 return ret;
4501 }
4502
4503 /**
4504 * regulator_sync_voltage - re-apply last regulator output voltage
4505 * @regulator: regulator source
4506 *
4507 * Re-apply the last configured voltage. This is intended to be used
4508 * where some external control source the consumer is cooperating with
4509 * has caused the configured voltage to change.
4510 *
4511 * Return: 0 on success or a negative error number on failure.
4512 */
4513 int regulator_sync_voltage(struct regulator *regulator)
4514 {
4515 struct regulator_dev *rdev = regulator->rdev;
4516 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON];
4517 int ret, min_uV, max_uV;
4518
4519 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
4520 return 0;
4521
4522 regulator_lock(rdev);
4523
4524 if (!rdev->desc->ops->set_voltage &&
4525 !rdev->desc->ops->set_voltage_sel) {
4526 ret = -EINVAL;
4527 goto out;
4528 }
4529
4530 /* This is only going to work if we've had a voltage configured. */
4531 if (!voltage->min_uV && !voltage->max_uV) {
4532 ret = -EINVAL;
4533 goto out;
4534 }
4535
4536 min_uV = voltage->min_uV;
4537 max_uV = voltage->max_uV;
4538
4539 /* This should be a paranoia check... */
4540 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
4541 if (ret < 0)
4542 goto out;
4543
4544 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
4545 if (ret < 0)
4546 goto out;
4547
4548 /* balance only, if regulator is coupled */
4549 if (rdev->coupling_desc.n_coupled > 1)
4550 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
4551 else
4552 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
4553
4554 out:
4555 regulator_unlock(rdev);
4556 return ret;
4557 }
4558 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
4559
4560 int regulator_get_voltage_rdev(struct regulator_dev *rdev)
4561 {
4562 int sel, ret;
4563 bool bypassed;
4564
4565 if (rdev->desc->ops->get_bypass) {
4566 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
4567 if (ret < 0)
4568 return ret;
4569 if (bypassed) {
4570 /* if bypassed the regulator must have a supply */
4571 if (!rdev->supply) {
4572 rdev_err(rdev,
4573 "bypassed regulator has no supply!\n");
4574 return -EPROBE_DEFER;
4575 }
4576
4577 return regulator_get_voltage_rdev(rdev->supply->rdev);
4578 }
4579 }
4580
4581 if (rdev->desc->ops->get_voltage_sel) {
4582 sel = rdev->desc->ops->get_voltage_sel(rdev);
4583 if (sel < 0)
4584 return sel;
4585 ret = rdev->desc->ops->list_voltage(rdev, sel);
4586 } else if (rdev->desc->ops->get_voltage) {
4587 ret = rdev->desc->ops->get_voltage(rdev);
4588 } else if (rdev->desc->ops->list_voltage) {
4589 ret = rdev->desc->ops->list_voltage(rdev, 0);
4590 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
4591 ret = rdev->desc->fixed_uV;
4592 } else if (rdev->supply) {
4593 ret = regulator_get_voltage_rdev(rdev->supply->rdev);
4594 } else if (rdev->supply_name) {
4595 return -EPROBE_DEFER;
4596 } else {
4597 return -EINVAL;
4598 }
4599
4600 if (ret < 0)
4601 return ret;
4602 return ret - rdev->constraints->uV_offset;
4603 }
4604 EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev);
4605
4606 /**
4607 * regulator_get_voltage - get regulator output voltage
4608 * @regulator: regulator source
4609 *
4610 * Return: Current regulator voltage in uV, or a negative error number on failure.
4611 *
4612 * NOTE: If the regulator is disabled it will return the voltage value. This
4613 * function should not be used to determine regulator state.
4614 */
4615 int regulator_get_voltage(struct regulator *regulator)
4616 {
4617 struct ww_acquire_ctx ww_ctx;
4618 int ret;
4619
4620 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4621 ret = regulator_get_voltage_rdev(regulator->rdev);
4622 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4623
4624 return ret;
4625 }
4626 EXPORT_SYMBOL_GPL(regulator_get_voltage);
4627
4628 /**
4629 * regulator_set_current_limit - set regulator output current limit
4630 * @regulator: regulator source
4631 * @min_uA: Minimum supported current in uA
4632 * @max_uA: Maximum supported current in uA
4633 *
4634 * Sets current sink to the desired output current. This can be set during
4635 * any regulator state. IOW, regulator can be disabled or enabled.
4636 *
4637 * If the regulator is enabled then the current will change to the new value
4638 * immediately otherwise if the regulator is disabled the regulator will
4639 * output at the new current when enabled.
4640 *
4641 * NOTE: Regulator system constraints must be set for this regulator before
4642 * calling this function otherwise this call will fail.
4643 *
4644 * Return: 0 on success or a negative error number on failure.
4645 */
4646 int regulator_set_current_limit(struct regulator *regulator,
4647 int min_uA, int max_uA)
4648 {
4649 struct regulator_dev *rdev = regulator->rdev;
4650 int ret;
4651
4652 regulator_lock(rdev);
4653
4654 /* sanity check */
4655 if (!rdev->desc->ops->set_current_limit) {
4656 ret = -EINVAL;
4657 goto out;
4658 }
4659
4660 /* constraints check */
4661 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
4662 if (ret < 0)
4663 goto out;
4664
4665 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
4666 out:
4667 regulator_unlock(rdev);
4668 return ret;
4669 }
4670 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
4671
4672 static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
4673 {
4674 /* sanity check */
4675 if (!rdev->desc->ops->get_current_limit)
4676 return -EINVAL;
4677
4678 return rdev->desc->ops->get_current_limit(rdev);
4679 }
4680
4681 static int _regulator_get_current_limit(struct regulator_dev *rdev)
4682 {
4683 int ret;
4684
4685 regulator_lock(rdev);
4686 ret = _regulator_get_current_limit_unlocked(rdev);
4687 regulator_unlock(rdev);
4688
4689 return ret;
4690 }
4691
4692 /**
4693 * regulator_get_current_limit - get regulator output current
4694 * @regulator: regulator source
4695 *
4696 * Return: Current supplied by the specified current sink in uA,
4697 * or a negative error number on failure.
4698 *
4699 * NOTE: If the regulator is disabled it will return the current value. This
4700 * function should not be used to determine regulator state.
4701 */
4702 int regulator_get_current_limit(struct regulator *regulator)
4703 {
4704 return _regulator_get_current_limit(regulator->rdev);
4705 }
4706 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
4707
4708 /**
4709 * regulator_get_unclaimed_power_budget - get regulator unclaimed power budget
4710 * @regulator: regulator source
4711 *
4712 * Return: Unclaimed power budget of the regulator in mW.
4713 */
4714 int regulator_get_unclaimed_power_budget(struct regulator *regulator)
4715 {
4716 return regulator->rdev->constraints->pw_budget_mW -
4717 regulator->rdev->pw_requested_mW;
4718 }
4719 EXPORT_SYMBOL_GPL(regulator_get_unclaimed_power_budget);
4720
4721 /**
4722 * regulator_request_power_budget - request power budget on a regulator
4723 * @regulator: regulator source
4724 * @pw_req: Power requested
4725 *
4726 * Return: 0 on success or a negative error number on failure.
4727 */
4728 int regulator_request_power_budget(struct regulator *regulator,
4729 unsigned int pw_req)
4730 {
4731 struct regulator_dev *rdev = regulator->rdev;
4732 int ret = 0, pw_tot_req;
4733
4734 regulator_lock(rdev);
4735 if (rdev->supply) {
4736 ret = regulator_request_power_budget(rdev->supply, pw_req);
4737 if (ret < 0)
4738 goto out;
4739 }
4740
4741 pw_tot_req = rdev->pw_requested_mW + pw_req;
4742 if (pw_tot_req > rdev->constraints->pw_budget_mW) {
4743 rdev_warn(rdev, "power requested %d mW out of budget %d mW",
4744 pw_req,
4745 rdev->constraints->pw_budget_mW - rdev->pw_requested_mW);
4746 regulator_notifier_call_chain(rdev,
4747 REGULATOR_EVENT_OVER_CURRENT_WARN,
4748 NULL);
4749 ret = -ERANGE;
4750 goto out;
4751 }
4752
4753 rdev->pw_requested_mW = pw_tot_req;
4754 out:
4755 regulator_unlock(rdev);
4756 return ret;
4757 }
4758 EXPORT_SYMBOL_GPL(regulator_request_power_budget);
4759
4760 /**
4761 * regulator_free_power_budget - free power budget on a regulator
4762 * @regulator: regulator source
4763 * @pw: Power to be released.
4764 *
4765 * Return: Power budget of the regulator in mW.
4766 */
4767 void regulator_free_power_budget(struct regulator *regulator,
4768 unsigned int pw)
4769 {
4770 struct regulator_dev *rdev = regulator->rdev;
4771 int pw_tot_req;
4772
4773 regulator_lock(rdev);
4774 if (rdev->supply)
4775 regulator_free_power_budget(rdev->supply, pw);
4776
4777 pw_tot_req = rdev->pw_requested_mW - pw;
4778 if (pw_tot_req >= 0)
4779 rdev->pw_requested_mW = pw_tot_req;
4780 else
4781 rdev_warn(rdev,
4782 "too much power freed %d mW (already requested %d mW)",
4783 pw, rdev->pw_requested_mW);
4784
4785 regulator_unlock(rdev);
4786 }
4787 EXPORT_SYMBOL_GPL(regulator_free_power_budget);
4788
4789 /**
4790 * regulator_set_mode - set regulator operating mode
4791 * @regulator: regulator source
4792 * @mode: operating mode - one of the REGULATOR_MODE constants
4793 *
4794 * Set regulator operating mode to increase regulator efficiency or improve
4795 * regulation performance.
4796 *
4797 * NOTE: Regulator system constraints must be set for this regulator before
4798 * calling this function otherwise this call will fail.
4799 *
4800 * Return: 0 on success or a negative error number on failure.
4801 */
4802 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
4803 {
4804 struct regulator_dev *rdev = regulator->rdev;
4805 int ret;
4806 int regulator_curr_mode;
4807
4808 regulator_lock(rdev);
4809
4810 /* sanity check */
4811 if (!rdev->desc->ops->set_mode) {
4812 ret = -EINVAL;
4813 goto out;
4814 }
4815
4816 /* return if the same mode is requested */
4817 if (rdev->desc->ops->get_mode) {
4818 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
4819 if (regulator_curr_mode == mode) {
4820 ret = 0;
4821 goto out;
4822 }
4823 }
4824
4825 /* constraints check */
4826 ret = regulator_mode_constrain(rdev, &mode);
4827 if (ret < 0)
4828 goto out;
4829
4830 ret = rdev->desc->ops->set_mode(rdev, mode);
4831 out:
4832 regulator_unlock(rdev);
4833 return ret;
4834 }
4835 EXPORT_SYMBOL_GPL(regulator_set_mode);
4836
4837 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
4838 {
4839 /* sanity check */
4840 if (!rdev->desc->ops->get_mode)
4841 return -EINVAL;
4842
4843 return rdev->desc->ops->get_mode(rdev);
4844 }
4845
4846 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
4847 {
4848 int ret;
4849
4850 regulator_lock(rdev);
4851 ret = _regulator_get_mode_unlocked(rdev);
4852 regulator_unlock(rdev);
4853
4854 return ret;
4855 }
4856
4857 /**
4858 * regulator_get_mode - get regulator operating mode
4859 * @regulator: regulator source
4860 *
4861 * Get the current regulator operating mode.
4862 *
4863 * Return: Current operating mode as %REGULATOR_MODE_* values,
4864 * or a negative error number on failure.
4865 */
4866 unsigned int regulator_get_mode(struct regulator *regulator)
4867 {
4868 return _regulator_get_mode(regulator->rdev);
4869 }
4870 EXPORT_SYMBOL_GPL(regulator_get_mode);
4871
4872 static int rdev_get_cached_err_flags(struct regulator_dev *rdev)
4873 {
4874 int ret = 0;
4875
4876 if (rdev->use_cached_err) {
4877 spin_lock(&rdev->err_lock);
4878 ret = rdev->cached_err;
4879 spin_unlock(&rdev->err_lock);
4880 }
4881 return ret;
4882 }
4883
4884 static int _regulator_get_error_flags(struct regulator_dev *rdev,
4885 unsigned int *flags)
4886 {
4887 int cached_flags, ret = 0;
4888
4889 regulator_lock(rdev);
4890
4891 cached_flags = rdev_get_cached_err_flags(rdev);
4892
4893 if (rdev->desc->ops->get_error_flags)
4894 ret = rdev->desc->ops->get_error_flags(rdev, flags);
4895 else if (!rdev->use_cached_err)
4896 ret = -EINVAL;
4897
4898 *flags |= cached_flags;
4899
4900 regulator_unlock(rdev);
4901
4902 return ret;
4903 }
4904
4905 /**
4906 * regulator_get_error_flags - get regulator error information
4907 * @regulator: regulator source
4908 * @flags: pointer to store error flags
4909 *
4910 * Get the current regulator error information.
4911 *
4912 * Return: 0 on success or a negative error number on failure.
4913 */
4914 int regulator_get_error_flags(struct regulator *regulator,
4915 unsigned int *flags)
4916 {
4917 return _regulator_get_error_flags(regulator->rdev, flags);
4918 }
4919 EXPORT_SYMBOL_GPL(regulator_get_error_flags);
4920
4921 /**
4922 * regulator_set_load - set regulator load
4923 * @regulator: regulator source
4924 * @uA_load: load current
4925 *
4926 * Notifies the regulator core of a new device load. This is then used by
4927 * DRMS (if enabled by constraints) to set the most efficient regulator
4928 * operating mode for the new regulator loading.
4929 *
4930 * Consumer devices notify their supply regulator of the maximum power
4931 * they will require (can be taken from device datasheet in the power
4932 * consumption tables) when they change operational status and hence power
4933 * state. Examples of operational state changes that can affect power
4934 * consumption are :-
4935 *
4936 * o Device is opened / closed.
4937 * o Device I/O is about to begin or has just finished.
4938 * o Device is idling in between work.
4939 *
4940 * This information is also exported via sysfs to userspace.
4941 *
4942 * DRMS will sum the total requested load on the regulator and change
4943 * to the most efficient operating mode if platform constraints allow.
4944 *
4945 * NOTE: when a regulator consumer requests to have a regulator
4946 * disabled then any load that consumer requested no longer counts
4947 * toward the total requested load. If the regulator is re-enabled
4948 * then the previously requested load will start counting again.
4949 *
4950 * If a regulator is an always-on regulator then an individual consumer's
4951 * load will still be removed if that consumer is fully disabled.
4952 *
4953 * Return: 0 on success or a negative error number on failure.
4954 */
4955 int regulator_set_load(struct regulator *regulator, int uA_load)
4956 {
4957 struct regulator_dev *rdev = regulator->rdev;
4958 int old_uA_load;
4959 int ret = 0;
4960
4961 regulator_lock(rdev);
4962 old_uA_load = regulator->uA_load;
4963 regulator->uA_load = uA_load;
4964 if (regulator->enable_count && old_uA_load != uA_load) {
4965 ret = drms_uA_update(rdev);
4966 if (ret < 0)
4967 regulator->uA_load = old_uA_load;
4968 }
4969 regulator_unlock(rdev);
4970
4971 return ret;
4972 }
4973 EXPORT_SYMBOL_GPL(regulator_set_load);
4974
4975 /**
4976 * regulator_allow_bypass - allow the regulator to go into bypass mode
4977 *
4978 * @regulator: Regulator to configure
4979 * @enable: enable or disable bypass mode
4980 *
4981 * Allow the regulator to go into bypass mode if all other consumers
4982 * for the regulator also enable bypass mode and the machine
4983 * constraints allow this. Bypass mode means that the regulator is
4984 * simply passing the input directly to the output with no regulation.
4985 *
4986 * Return: 0 on success or if changing bypass is not possible, or
4987 * a negative error number on failure.
4988 */
4989 int regulator_allow_bypass(struct regulator *regulator, bool enable)
4990 {
4991 struct regulator_dev *rdev = regulator->rdev;
4992 const char *name = rdev_get_name(rdev);
4993 int ret = 0;
4994
4995 if (!rdev->desc->ops->set_bypass)
4996 return 0;
4997
4998 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
4999 return 0;
5000
5001 regulator_lock(rdev);
5002
5003 if (enable && !regulator->bypass) {
5004 rdev->bypass_count++;
5005
5006 if (rdev->bypass_count == rdev->open_count) {
5007 trace_regulator_bypass_enable(name);
5008
5009 ret = rdev->desc->ops->set_bypass(rdev, enable);
5010 if (ret != 0)
5011 rdev->bypass_count--;
5012 else
5013 trace_regulator_bypass_enable_complete(name);
5014 }
5015
5016 } else if (!enable && regulator->bypass) {
5017 rdev->bypass_count--;
5018
5019 if (rdev->bypass_count != rdev->open_count) {
5020 trace_regulator_bypass_disable(name);
5021
5022 ret = rdev->desc->ops->set_bypass(rdev, enable);
5023 if (ret != 0)
5024 rdev->bypass_count++;
5025 else
5026 trace_regulator_bypass_disable_complete(name);
5027 }
5028 }
5029
5030 if (ret == 0)
5031 regulator->bypass = enable;
5032
5033 regulator_unlock(rdev);
5034
5035 return ret;
5036 }
5037 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
5038
5039 /**
5040 * regulator_register_notifier - register regulator event notifier
5041 * @regulator: regulator source
5042 * @nb: notifier block
5043 *
5044 * Register notifier block to receive regulator events.
5045 *
5046 * Return: 0 on success or a negative error number on failure.
5047 */
5048 int regulator_register_notifier(struct regulator *regulator,
5049 struct notifier_block *nb)
5050 {
5051 return blocking_notifier_chain_register(®ulator->rdev->notifier,
5052 nb);
5053 }
5054 EXPORT_SYMBOL_GPL(regulator_register_notifier);
5055
5056 /**
5057 * regulator_unregister_notifier - unregister regulator event notifier
5058 * @regulator: regulator source
5059 * @nb: notifier block
5060 *
5061 * Unregister regulator event notifier block.
5062 *
5063 * Return: 0 on success or a negative error number on failure.
5064 */
5065 int regulator_unregister_notifier(struct regulator *regulator,
5066 struct notifier_block *nb)
5067 {
5068 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
5069 nb);
5070 }
5071 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
5072
5073 /* notify regulator consumers and downstream regulator consumers.
5074 * Note mutex must be held by caller.
5075 */
5076 static int _notifier_call_chain(struct regulator_dev *rdev,
5077 unsigned long event, void *data)
5078 {
5079 /* call rdev chain first */
5080 int ret = blocking_notifier_call_chain(&rdev->notifier, event, data);
5081
5082 if (IS_REACHABLE(CONFIG_REGULATOR_NETLINK_EVENTS)) {
5083 struct device *parent = rdev->dev.parent;
5084 const char *rname = rdev_get_name(rdev);
5085 char name[32];
5086
5087 /* Avoid duplicate debugfs directory names */
5088 if (parent && rname == rdev->desc->name) {
5089 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
5090 rname);
5091 rname = name;
5092 }
5093 reg_generate_netlink_event(rname, event);
5094 }
5095
5096 return ret;
5097 }
5098
5099 int _regulator_bulk_get(struct device *dev, int num_consumers,
5100 struct regulator_bulk_data *consumers, enum regulator_get_type get_type)
5101 {
5102 int i;
5103 int ret;
5104
5105 for (i = 0; i < num_consumers; i++)
5106 consumers[i].consumer = NULL;
5107
5108 for (i = 0; i < num_consumers; i++) {
5109 consumers[i].consumer = _regulator_get(dev,
5110 consumers[i].supply, get_type);
5111 if (IS_ERR(consumers[i].consumer)) {
5112 ret = dev_err_probe(dev, PTR_ERR(consumers[i].consumer),
5113 "Failed to get supply '%s'\n",
5114 consumers[i].supply);
5115 consumers[i].consumer = NULL;
5116 goto err;
5117 }
5118
5119 if (consumers[i].init_load_uA > 0) {
5120 ret = regulator_set_load(consumers[i].consumer,
5121 consumers[i].init_load_uA);
5122 if (ret) {
5123 i++;
5124 goto err;
5125 }
5126 }
5127 }
5128
5129 return 0;
5130
5131 err:
5132 while (--i >= 0)
5133 regulator_put(consumers[i].consumer);
5134
5135 return ret;
5136 }
5137
5138 /**
5139 * regulator_bulk_get - get multiple regulator consumers
5140 *
5141 * @dev: Device to supply
5142 * @num_consumers: Number of consumers to register
5143 * @consumers: Configuration of consumers; clients are stored here.
5144 *
5145 * This helper function allows drivers to get several regulator
5146 * consumers in one operation. If any of the regulators cannot be
5147 * acquired then any regulators that were allocated will be freed
5148 * before returning to the caller.
5149 *
5150 * Return: 0 on success or a negative error number on failure.
5151 */
5152 int regulator_bulk_get(struct device *dev, int num_consumers,
5153 struct regulator_bulk_data *consumers)
5154 {
5155 return _regulator_bulk_get(dev, num_consumers, consumers, NORMAL_GET);
5156 }
5157 EXPORT_SYMBOL_GPL(regulator_bulk_get);
5158
5159 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
5160 {
5161 struct regulator_bulk_data *bulk = data;
5162
5163 bulk->ret = regulator_enable(bulk->consumer);
5164 }
5165
5166 /**
5167 * regulator_bulk_enable - enable multiple regulator consumers
5168 *
5169 * @num_consumers: Number of consumers
5170 * @consumers: Consumer data; clients are stored here.
5171 *
5172 * This convenience API allows consumers to enable multiple regulator
5173 * clients in a single API call. If any consumers cannot be enabled
5174 * then any others that were enabled will be disabled again prior to
5175 * return.
5176 *
5177 * Return: 0 on success or a negative error number on failure.
5178 */
5179 int regulator_bulk_enable(int num_consumers,
5180 struct regulator_bulk_data *consumers)
5181 {
5182 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
5183 int i;
5184 int ret = 0;
5185
5186 for (i = 0; i < num_consumers; i++) {
5187 async_schedule_domain(regulator_bulk_enable_async,
5188 &consumers[i], &async_domain);
5189 }
5190
5191 async_synchronize_full_domain(&async_domain);
5192
5193 /* If any consumer failed we need to unwind any that succeeded */
5194 for (i = 0; i < num_consumers; i++) {
5195 if (consumers[i].ret != 0) {
5196 ret = consumers[i].ret;
5197 goto err;
5198 }
5199 }
5200
5201 return 0;
5202
5203 err:
5204 for (i = 0; i < num_consumers; i++) {
5205 if (consumers[i].ret < 0)
5206 pr_err("Failed to enable %s: %pe\n", consumers[i].supply,
5207 ERR_PTR(consumers[i].ret));
5208 else
5209 regulator_disable(consumers[i].consumer);
5210 }
5211
5212 return ret;
5213 }
5214 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
5215
5216 /**
5217 * regulator_bulk_disable - disable multiple regulator consumers
5218 *
5219 * @num_consumers: Number of consumers
5220 * @consumers: Consumer data; clients are stored here.
5221 *
5222 * This convenience API allows consumers to disable multiple regulator
5223 * clients in a single API call. If any consumers cannot be disabled
5224 * then any others that were disabled will be enabled again prior to
5225 * return.
5226 *
5227 * Return: 0 on success or a negative error number on failure.
5228 */
5229 int regulator_bulk_disable(int num_consumers,
5230 struct regulator_bulk_data *consumers)
5231 {
5232 int i;
5233 int ret, r;
5234
5235 for (i = num_consumers - 1; i >= 0; --i) {
5236 ret = regulator_disable(consumers[i].consumer);
5237 if (ret != 0)
5238 goto err;
5239 }
5240
5241 return 0;
5242
5243 err:
5244 pr_err("Failed to disable %s: %pe\n", consumers[i].supply, ERR_PTR(ret));
5245 for (++i; i < num_consumers; ++i) {
5246 r = regulator_enable(consumers[i].consumer);
5247 if (r != 0)
5248 pr_err("Failed to re-enable %s: %pe\n",
5249 consumers[i].supply, ERR_PTR(r));
5250 }
5251
5252 return ret;
5253 }
5254 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
5255
5256 /**
5257 * regulator_bulk_force_disable - force disable multiple regulator consumers
5258 *
5259 * @num_consumers: Number of consumers
5260 * @consumers: Consumer data; clients are stored here.
5261 *
5262 * This convenience API allows consumers to forcibly disable multiple regulator
5263 * clients in a single API call.
5264 * NOTE: This should be used for situations when device damage will
5265 * likely occur if the regulators are not disabled (e.g. over temp).
5266 * Although regulator_force_disable function call for some consumers can
5267 * return error numbers, the function is called for all consumers.
5268 *
5269 * Return: 0 on success or a negative error number on failure.
5270 */
5271 int regulator_bulk_force_disable(int num_consumers,
5272 struct regulator_bulk_data *consumers)
5273 {
5274 int i;
5275 int ret = 0;
5276
5277 for (i = 0; i < num_consumers; i++) {
5278 consumers[i].ret =
5279 regulator_force_disable(consumers[i].consumer);
5280
5281 /* Store first error for reporting */
5282 if (consumers[i].ret && !ret)
5283 ret = consumers[i].ret;
5284 }
5285
5286 return ret;
5287 }
5288 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
5289
5290 /**
5291 * regulator_bulk_free - free multiple regulator consumers
5292 *
5293 * @num_consumers: Number of consumers
5294 * @consumers: Consumer data; clients are stored here.
5295 *
5296 * This convenience API allows consumers to free multiple regulator
5297 * clients in a single API call.
5298 */
5299 void regulator_bulk_free(int num_consumers,
5300 struct regulator_bulk_data *consumers)
5301 {
5302 int i;
5303
5304 for (i = 0; i < num_consumers; i++) {
5305 regulator_put(consumers[i].consumer);
5306 consumers[i].consumer = NULL;
5307 }
5308 }
5309 EXPORT_SYMBOL_GPL(regulator_bulk_free);
5310
5311 /**
5312 * regulator_handle_critical - Handle events for system-critical regulators.
5313 * @rdev: The regulator device.
5314 * @event: The event being handled.
5315 *
5316 * This function handles critical events such as under-voltage, over-current,
5317 * and unknown errors for regulators deemed system-critical. On detecting such
5318 * events, it triggers a hardware protection shutdown with a defined timeout.
5319 */
5320 static void regulator_handle_critical(struct regulator_dev *rdev,
5321 unsigned long event)
5322 {
5323 const char *reason = NULL;
5324
5325 if (!rdev->constraints->system_critical)
5326 return;
5327
5328 switch (event) {
5329 case REGULATOR_EVENT_UNDER_VOLTAGE:
5330 reason = "System critical regulator: voltage drop detected";
5331 break;
5332 case REGULATOR_EVENT_OVER_CURRENT:
5333 reason = "System critical regulator: over-current detected";
5334 break;
5335 case REGULATOR_EVENT_FAIL:
5336 reason = "System critical regulator: unknown error";
5337 }
5338
5339 if (!reason)
5340 return;
5341
5342 hw_protection_trigger(reason,
5343 rdev->constraints->uv_less_critical_window_ms);
5344 }
5345
5346 /**
5347 * regulator_notifier_call_chain - call regulator event notifier
5348 * @rdev: regulator source
5349 * @event: notifier block
5350 * @data: callback-specific data.
5351 *
5352 * Called by regulator drivers to notify clients a regulator event has
5353 * occurred.
5354 *
5355 * Return: %NOTIFY_DONE.
5356 */
5357 int regulator_notifier_call_chain(struct regulator_dev *rdev,
5358 unsigned long event, void *data)
5359 {
5360 regulator_handle_critical(rdev, event);
5361
5362 _notifier_call_chain(rdev, event, data);
5363 return NOTIFY_DONE;
5364
5365 }
5366 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
5367
5368 /**
5369 * regulator_mode_to_status - convert a regulator mode into a status
5370 *
5371 * @mode: Mode to convert
5372 *
5373 * Convert a regulator mode into a status.
5374 *
5375 * Return: %REGULATOR_STATUS_* value corresponding to given mode.
5376 */
5377 int regulator_mode_to_status(unsigned int mode)
5378 {
5379 switch (mode) {
5380 case REGULATOR_MODE_FAST:
5381 return REGULATOR_STATUS_FAST;
5382 case REGULATOR_MODE_NORMAL:
5383 return REGULATOR_STATUS_NORMAL;
5384 case REGULATOR_MODE_IDLE:
5385 return REGULATOR_STATUS_IDLE;
5386 case REGULATOR_MODE_STANDBY:
5387 return REGULATOR_STATUS_STANDBY;
5388 default:
5389 return REGULATOR_STATUS_UNDEFINED;
5390 }
5391 }
5392 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
5393
5394 static struct attribute *regulator_dev_attrs[] = {
5395 &dev_attr_name.attr,
5396 &dev_attr_num_users.attr,
5397 &dev_attr_type.attr,
5398 &dev_attr_microvolts.attr,
5399 &dev_attr_microamps.attr,
5400 &dev_attr_opmode.attr,
5401 &dev_attr_state.attr,
5402 &dev_attr_status.attr,
5403 &dev_attr_bypass.attr,
5404 &dev_attr_requested_microamps.attr,
5405 &dev_attr_min_microvolts.attr,
5406 &dev_attr_max_microvolts.attr,
5407 &dev_attr_min_microamps.attr,
5408 &dev_attr_max_microamps.attr,
5409 &dev_attr_under_voltage.attr,
5410 &dev_attr_over_current.attr,
5411 &dev_attr_regulation_out.attr,
5412 &dev_attr_fail.attr,
5413 &dev_attr_over_temp.attr,
5414 &dev_attr_under_voltage_warn.attr,
5415 &dev_attr_over_current_warn.attr,
5416 &dev_attr_over_voltage_warn.attr,
5417 &dev_attr_over_temp_warn.attr,
5418 &dev_attr_suspend_standby_state.attr,
5419 &dev_attr_suspend_mem_state.attr,
5420 &dev_attr_suspend_disk_state.attr,
5421 &dev_attr_suspend_standby_microvolts.attr,
5422 &dev_attr_suspend_mem_microvolts.attr,
5423 &dev_attr_suspend_disk_microvolts.attr,
5424 &dev_attr_suspend_standby_mode.attr,
5425 &dev_attr_suspend_mem_mode.attr,
5426 &dev_attr_suspend_disk_mode.attr,
5427 &dev_attr_power_budget_milliwatt.attr,
5428 &dev_attr_power_requested_milliwatt.attr,
5429 NULL
5430 };
5431
5432 /*
5433 * To avoid cluttering sysfs (and memory) with useless state, only
5434 * create attributes that can be meaningfully displayed.
5435 */
5436 static umode_t regulator_attr_is_visible(struct kobject *kobj,
5437 struct attribute *attr, int idx)
5438 {
5439 struct device *dev = kobj_to_dev(kobj);
5440 struct regulator_dev *rdev = dev_to_rdev(dev);
5441 const struct regulator_ops *ops = rdev->desc->ops;
5442 umode_t mode = attr->mode;
5443
5444 /* these three are always present */
5445 if (attr == &dev_attr_name.attr ||
5446 attr == &dev_attr_num_users.attr ||
5447 attr == &dev_attr_type.attr)
5448 return mode;
5449
5450 /* some attributes need specific methods to be displayed */
5451 if (attr == &dev_attr_microvolts.attr) {
5452 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
5453 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
5454 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
5455 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
5456 return mode;
5457 return 0;
5458 }
5459
5460 if (attr == &dev_attr_microamps.attr)
5461 return ops->get_current_limit ? mode : 0;
5462
5463 if (attr == &dev_attr_opmode.attr)
5464 return ops->get_mode ? mode : 0;
5465
5466 if (attr == &dev_attr_state.attr)
5467 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
5468
5469 if (attr == &dev_attr_status.attr)
5470 return ops->get_status ? mode : 0;
5471
5472 if (attr == &dev_attr_bypass.attr)
5473 return ops->get_bypass ? mode : 0;
5474
5475 if (attr == &dev_attr_under_voltage.attr ||
5476 attr == &dev_attr_over_current.attr ||
5477 attr == &dev_attr_regulation_out.attr ||
5478 attr == &dev_attr_fail.attr ||
5479 attr == &dev_attr_over_temp.attr ||
5480 attr == &dev_attr_under_voltage_warn.attr ||
5481 attr == &dev_attr_over_current_warn.attr ||
5482 attr == &dev_attr_over_voltage_warn.attr ||
5483 attr == &dev_attr_over_temp_warn.attr)
5484 return ops->get_error_flags ? mode : 0;
5485
5486 /* constraints need specific supporting methods */
5487 if (attr == &dev_attr_min_microvolts.attr ||
5488 attr == &dev_attr_max_microvolts.attr)
5489 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
5490
5491 if (attr == &dev_attr_min_microamps.attr ||
5492 attr == &dev_attr_max_microamps.attr)
5493 return ops->set_current_limit ? mode : 0;
5494
5495 if (attr == &dev_attr_suspend_standby_state.attr ||
5496 attr == &dev_attr_suspend_mem_state.attr ||
5497 attr == &dev_attr_suspend_disk_state.attr)
5498 return mode;
5499
5500 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
5501 attr == &dev_attr_suspend_mem_microvolts.attr ||
5502 attr == &dev_attr_suspend_disk_microvolts.attr)
5503 return ops->set_suspend_voltage ? mode : 0;
5504
5505 if (attr == &dev_attr_suspend_standby_mode.attr ||
5506 attr == &dev_attr_suspend_mem_mode.attr ||
5507 attr == &dev_attr_suspend_disk_mode.attr)
5508 return ops->set_suspend_mode ? mode : 0;
5509
5510 if (attr == &dev_attr_power_budget_milliwatt.attr ||
5511 attr == &dev_attr_power_requested_milliwatt.attr)
5512 return rdev->constraints->pw_budget_mW != INT_MAX ? mode : 0;
5513
5514 return mode;
5515 }
5516
5517 static const struct attribute_group regulator_dev_group = {
5518 .attrs = regulator_dev_attrs,
5519 .is_visible = regulator_attr_is_visible,
5520 };
5521
5522 static const struct attribute_group *regulator_dev_groups[] = {
5523 ®ulator_dev_group,
5524 NULL
5525 };
5526
5527 static void regulator_dev_release(struct device *dev)
5528 {
5529 struct regulator_dev *rdev = dev_get_drvdata(dev);
5530
5531 debugfs_remove_recursive(rdev->debugfs);
5532 kfree(rdev->constraints);
5533 of_node_put(rdev->dev.of_node);
5534 kfree(rdev);
5535 }
5536
5537 static void rdev_init_debugfs(struct regulator_dev *rdev)
5538 {
5539 struct device *parent = rdev->dev.parent;
5540 const char *rname = rdev_get_name(rdev);
5541 char name[NAME_MAX];
5542
5543 /* Avoid duplicate debugfs directory names */
5544 if (parent && rname == rdev->desc->name) {
5545 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
5546 rname);
5547 rname = name;
5548 }
5549
5550 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
5551 if (IS_ERR(rdev->debugfs))
5552 rdev_dbg(rdev, "Failed to create debugfs directory\n");
5553
5554 debugfs_create_u32("use_count", 0444, rdev->debugfs,
5555 &rdev->use_count);
5556 debugfs_create_u32("open_count", 0444, rdev->debugfs,
5557 &rdev->open_count);
5558 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
5559 &rdev->bypass_count);
5560 }
5561
5562 static int regulator_register_resolve_supply(struct device *dev, void *data)
5563 {
5564 struct regulator_dev *rdev = dev_to_rdev(dev);
5565
5566 if (regulator_resolve_supply(rdev))
5567 rdev_dbg(rdev, "unable to resolve supply\n");
5568
5569 return 0;
5570 }
5571
5572 int regulator_coupler_register(struct regulator_coupler *coupler)
5573 {
5574 mutex_lock(®ulator_list_mutex);
5575 list_add_tail(&coupler->list, ®ulator_coupler_list);
5576 mutex_unlock(®ulator_list_mutex);
5577
5578 return 0;
5579 }
5580
5581 static struct regulator_coupler *
5582 regulator_find_coupler(struct regulator_dev *rdev)
5583 {
5584 struct regulator_coupler *coupler;
5585 int err;
5586
5587 /*
5588 * Note that regulators are appended to the list and the generic
5589 * coupler is registered first, hence it will be attached at last
5590 * if nobody cared.
5591 */
5592 list_for_each_entry_reverse(coupler, ®ulator_coupler_list, list) {
5593 err = coupler->attach_regulator(coupler, rdev);
5594 if (!err) {
5595 if (!coupler->balance_voltage &&
5596 rdev->coupling_desc.n_coupled > 2)
5597 goto err_unsupported;
5598
5599 return coupler;
5600 }
5601
5602 if (err < 0)
5603 return ERR_PTR(err);
5604
5605 if (err == 1)
5606 continue;
5607
5608 break;
5609 }
5610
5611 return ERR_PTR(-EINVAL);
5612
5613 err_unsupported:
5614 if (coupler->detach_regulator)
5615 coupler->detach_regulator(coupler, rdev);
5616
5617 rdev_err(rdev,
5618 "Voltage balancing for multiple regulator couples is unimplemented\n");
5619
5620 return ERR_PTR(-EPERM);
5621 }
5622
5623 static void regulator_resolve_coupling(struct regulator_dev *rdev)
5624 {
5625 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
5626 struct coupling_desc *c_desc = &rdev->coupling_desc;
5627 int n_coupled = c_desc->n_coupled;
5628 struct regulator_dev *c_rdev;
5629 int i;
5630
5631 for (i = 1; i < n_coupled; i++) {
5632 /* already resolved */
5633 if (c_desc->coupled_rdevs[i])
5634 continue;
5635
5636 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
5637
5638 if (!c_rdev)
5639 continue;
5640
5641 if (c_rdev->coupling_desc.coupler != coupler) {
5642 rdev_err(rdev, "coupler mismatch with %s\n",
5643 rdev_get_name(c_rdev));
5644 return;
5645 }
5646
5647 c_desc->coupled_rdevs[i] = c_rdev;
5648 c_desc->n_resolved++;
5649
5650 regulator_resolve_coupling(c_rdev);
5651 }
5652 }
5653
5654 static void regulator_remove_coupling(struct regulator_dev *rdev)
5655 {
5656 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
5657 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
5658 struct regulator_dev *__c_rdev, *c_rdev;
5659 unsigned int __n_coupled, n_coupled;
5660 int i, k;
5661 int err;
5662
5663 n_coupled = c_desc->n_coupled;
5664
5665 for (i = 1; i < n_coupled; i++) {
5666 c_rdev = c_desc->coupled_rdevs[i];
5667
5668 if (!c_rdev)
5669 continue;
5670
5671 regulator_lock(c_rdev);
5672
5673 __c_desc = &c_rdev->coupling_desc;
5674 __n_coupled = __c_desc->n_coupled;
5675
5676 for (k = 1; k < __n_coupled; k++) {
5677 __c_rdev = __c_desc->coupled_rdevs[k];
5678
5679 if (__c_rdev == rdev) {
5680 __c_desc->coupled_rdevs[k] = NULL;
5681 __c_desc->n_resolved--;
5682 break;
5683 }
5684 }
5685
5686 regulator_unlock(c_rdev);
5687
5688 c_desc->coupled_rdevs[i] = NULL;
5689 c_desc->n_resolved--;
5690 }
5691
5692 if (coupler && coupler->detach_regulator) {
5693 err = coupler->detach_regulator(coupler, rdev);
5694 if (err)
5695 rdev_err(rdev, "failed to detach from coupler: %pe\n",
5696 ERR_PTR(err));
5697 }
5698
5699 rdev->coupling_desc.n_coupled = 0;
5700 kfree(rdev->coupling_desc.coupled_rdevs);
5701 rdev->coupling_desc.coupled_rdevs = NULL;
5702 }
5703
5704 static int regulator_init_coupling(struct regulator_dev *rdev)
5705 {
5706 struct regulator_dev **coupled;
5707 int err, n_phandles;
5708
5709 if (!IS_ENABLED(CONFIG_OF))
5710 n_phandles = 0;
5711 else
5712 n_phandles = of_get_n_coupled(rdev);
5713
5714 coupled = kcalloc(n_phandles + 1, sizeof(*coupled), GFP_KERNEL);
5715 if (!coupled)
5716 return -ENOMEM;
5717
5718 rdev->coupling_desc.coupled_rdevs = coupled;
5719
5720 /*
5721 * Every regulator should always have coupling descriptor filled with
5722 * at least pointer to itself.
5723 */
5724 rdev->coupling_desc.coupled_rdevs[0] = rdev;
5725 rdev->coupling_desc.n_coupled = n_phandles + 1;
5726 rdev->coupling_desc.n_resolved++;
5727
5728 /* regulator isn't coupled */
5729 if (n_phandles == 0)
5730 return 0;
5731
5732 if (!of_check_coupling_data(rdev))
5733 return -EPERM;
5734
5735 mutex_lock(®ulator_list_mutex);
5736 rdev->coupling_desc.coupler = regulator_find_coupler(rdev);
5737 mutex_unlock(®ulator_list_mutex);
5738
5739 if (IS_ERR(rdev->coupling_desc.coupler)) {
5740 err = PTR_ERR(rdev->coupling_desc.coupler);
5741 rdev_err(rdev, "failed to get coupler: %pe\n", ERR_PTR(err));
5742 return err;
5743 }
5744
5745 return 0;
5746 }
5747
5748 static int generic_coupler_attach(struct regulator_coupler *coupler,
5749 struct regulator_dev *rdev)
5750 {
5751 if (rdev->coupling_desc.n_coupled > 2) {
5752 rdev_err(rdev,
5753 "Voltage balancing for multiple regulator couples is unimplemented\n");
5754 return -EPERM;
5755 }
5756
5757 if (!rdev->constraints->always_on) {
5758 rdev_err(rdev,
5759 "Coupling of a non always-on regulator is unimplemented\n");
5760 return -ENOTSUPP;
5761 }
5762
5763 return 0;
5764 }
5765
5766 static struct regulator_coupler generic_regulator_coupler = {
5767 .attach_regulator = generic_coupler_attach,
5768 };
5769
5770 /**
5771 * regulator_register - register regulator
5772 * @dev: the device that drive the regulator
5773 * @regulator_desc: regulator to register
5774 * @cfg: runtime configuration for regulator
5775 *
5776 * Called by regulator drivers to register a regulator.
5777 *
5778 * Return: Pointer to a valid &struct regulator_dev on success or
5779 * an ERR_PTR() encoded negative error number on failure.
5780 */
5781 struct regulator_dev *
5782 regulator_register(struct device *dev,
5783 const struct regulator_desc *regulator_desc,
5784 const struct regulator_config *cfg)
5785 {
5786 const struct regulator_init_data *init_data;
5787 struct regulator_config *config = NULL;
5788 static atomic_t regulator_no = ATOMIC_INIT(-1);
5789 struct regulator_dev *rdev;
5790 bool dangling_cfg_gpiod = false;
5791 bool dangling_of_gpiod = false;
5792 int ret, i;
5793 bool resolved_early = false;
5794
5795 if (cfg == NULL)
5796 return ERR_PTR(-EINVAL);
5797 if (cfg->ena_gpiod)
5798 dangling_cfg_gpiod = true;
5799 if (regulator_desc == NULL) {
5800 ret = -EINVAL;
5801 goto rinse;
5802 }
5803
5804 WARN_ON(!dev || !cfg->dev);
5805
5806 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
5807 ret = -EINVAL;
5808 goto rinse;
5809 }
5810
5811 if (regulator_desc->type != REGULATOR_VOLTAGE &&
5812 regulator_desc->type != REGULATOR_CURRENT) {
5813 ret = -EINVAL;
5814 goto rinse;
5815 }
5816
5817 /* Only one of each should be implemented */
5818 WARN_ON(regulator_desc->ops->get_voltage &&
5819 regulator_desc->ops->get_voltage_sel);
5820 WARN_ON(regulator_desc->ops->set_voltage &&
5821 regulator_desc->ops->set_voltage_sel);
5822
5823 /* If we're using selectors we must implement list_voltage. */
5824 if (regulator_desc->ops->get_voltage_sel &&
5825 !regulator_desc->ops->list_voltage) {
5826 ret = -EINVAL;
5827 goto rinse;
5828 }
5829 if (regulator_desc->ops->set_voltage_sel &&
5830 !regulator_desc->ops->list_voltage) {
5831 ret = -EINVAL;
5832 goto rinse;
5833 }
5834
5835 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
5836 if (rdev == NULL) {
5837 ret = -ENOMEM;
5838 goto rinse;
5839 }
5840 device_initialize(&rdev->dev);
5841 dev_set_drvdata(&rdev->dev, rdev);
5842 rdev->dev.class = ®ulator_class;
5843 spin_lock_init(&rdev->err_lock);
5844
5845 /*
5846 * Duplicate the config so the driver could override it after
5847 * parsing init data.
5848 */
5849 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
5850 if (config == NULL) {
5851 ret = -ENOMEM;
5852 goto clean;
5853 }
5854
5855 /*
5856 * DT may override the config->init_data provided if the platform
5857 * needs to do so. If so, config->init_data is completely ignored.
5858 */
5859 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
5860 &rdev->dev.of_node);
5861
5862 /*
5863 * Sometimes not all resources are probed already so we need to take
5864 * that into account. This happens most the time if the ena_gpiod comes
5865 * from a gpio extender or something else.
5866 */
5867 if (PTR_ERR(init_data) == -EPROBE_DEFER) {
5868 ret = -EPROBE_DEFER;
5869 goto clean;
5870 }
5871
5872 /*
5873 * We need to keep track of any GPIO descriptor coming from the
5874 * device tree until we have handled it over to the core. If the
5875 * config that was passed in to this function DOES NOT contain
5876 * a descriptor, and the config after this call DOES contain
5877 * a descriptor, we definitely got one from parsing the device
5878 * tree.
5879 */
5880 if (!cfg->ena_gpiod && config->ena_gpiod)
5881 dangling_of_gpiod = true;
5882 if (!init_data) {
5883 init_data = config->init_data;
5884 rdev->dev.of_node = of_node_get(config->of_node);
5885 }
5886
5887 ww_mutex_init(&rdev->mutex, ®ulator_ww_class);
5888 rdev->reg_data = config->driver_data;
5889 rdev->owner = regulator_desc->owner;
5890 rdev->desc = regulator_desc;
5891 if (config->regmap)
5892 rdev->regmap = config->regmap;
5893 else if (dev_get_regmap(dev, NULL))
5894 rdev->regmap = dev_get_regmap(dev, NULL);
5895 else if (dev->parent)
5896 rdev->regmap = dev_get_regmap(dev->parent, NULL);
5897 INIT_LIST_HEAD(&rdev->consumer_list);
5898 INIT_LIST_HEAD(&rdev->list);
5899 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
5900 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
5901
5902 if (init_data && init_data->supply_regulator)
5903 rdev->supply_name = init_data->supply_regulator;
5904 else if (regulator_desc->supply_name)
5905 rdev->supply_name = regulator_desc->supply_name;
5906
5907 /* register with sysfs */
5908 rdev->dev.parent = config->dev;
5909 dev_set_name(&rdev->dev, "regulator.%lu",
5910 (unsigned long) atomic_inc_return(®ulator_no));
5911
5912 /* set regulator constraints */
5913 if (init_data)
5914 rdev->constraints = kmemdup(&init_data->constraints,
5915 sizeof(*rdev->constraints),
5916 GFP_KERNEL);
5917 else
5918 rdev->constraints = kzalloc(sizeof(*rdev->constraints),
5919 GFP_KERNEL);
5920 if (!rdev->constraints) {
5921 ret = -ENOMEM;
5922 goto wash;
5923 }
5924
5925 if (regulator_desc->init_cb) {
5926 ret = regulator_desc->init_cb(rdev, config);
5927 if (ret < 0)
5928 goto wash;
5929 }
5930
5931 if ((rdev->supply_name && !rdev->supply) &&
5932 (rdev->constraints->always_on ||
5933 rdev->constraints->boot_on)) {
5934 ret = regulator_resolve_supply(rdev);
5935 if (ret)
5936 rdev_dbg(rdev, "unable to resolve supply early: %pe\n",
5937 ERR_PTR(ret));
5938
5939 resolved_early = true;
5940 }
5941
5942 if (config->ena_gpiod) {
5943 ret = regulator_ena_gpio_request(rdev, config);
5944 if (ret != 0) {
5945 rdev_err(rdev, "Failed to request enable GPIO: %pe\n",
5946 ERR_PTR(ret));
5947 goto wash;
5948 }
5949 /* The regulator core took over the GPIO descriptor */
5950 dangling_cfg_gpiod = false;
5951 dangling_of_gpiod = false;
5952 }
5953
5954 ret = set_machine_constraints(rdev);
5955 if (ret == -EPROBE_DEFER && !resolved_early) {
5956 /* Regulator might be in bypass mode and so needs its supply
5957 * to set the constraints
5958 */
5959 /* FIXME: this currently triggers a chicken-and-egg problem
5960 * when creating -SUPPLY symlink in sysfs to a regulator
5961 * that is just being created
5962 */
5963 rdev_dbg(rdev, "will resolve supply early: %s\n",
5964 rdev->supply_name);
5965 ret = regulator_resolve_supply(rdev);
5966 if (!ret)
5967 ret = set_machine_constraints(rdev);
5968 else
5969 rdev_dbg(rdev, "unable to resolve supply early: %pe\n",
5970 ERR_PTR(ret));
5971 }
5972 if (ret < 0)
5973 goto wash;
5974
5975 ret = regulator_init_coupling(rdev);
5976 if (ret < 0)
5977 goto wash;
5978
5979 /* add consumers devices */
5980 if (init_data) {
5981 for (i = 0; i < init_data->num_consumer_supplies; i++) {
5982 ret = set_consumer_device_supply(rdev,
5983 init_data->consumer_supplies[i].dev_name,
5984 init_data->consumer_supplies[i].supply);
5985 if (ret < 0) {
5986 dev_err(dev, "Failed to set supply %s\n",
5987 init_data->consumer_supplies[i].supply);
5988 goto unset_supplies;
5989 }
5990 }
5991 }
5992
5993 if (!rdev->desc->ops->get_voltage &&
5994 !rdev->desc->ops->list_voltage &&
5995 !rdev->desc->fixed_uV)
5996 rdev->is_switch = true;
5997
5998 ret = device_add(&rdev->dev);
5999 if (ret != 0)
6000 goto unset_supplies;
6001
6002 rdev_init_debugfs(rdev);
6003
6004 /* try to resolve regulators coupling since a new one was registered */
6005 mutex_lock(®ulator_list_mutex);
6006 regulator_resolve_coupling(rdev);
6007 mutex_unlock(®ulator_list_mutex);
6008
6009 /* try to resolve regulators supply since a new one was registered */
6010 class_for_each_device(®ulator_class, NULL, NULL,
6011 regulator_register_resolve_supply);
6012 kfree(config);
6013 return rdev;
6014
6015 unset_supplies:
6016 mutex_lock(®ulator_list_mutex);
6017 unset_regulator_supplies(rdev);
6018 regulator_remove_coupling(rdev);
6019 mutex_unlock(®ulator_list_mutex);
6020 wash:
6021 regulator_put(rdev->supply);
6022 kfree(rdev->coupling_desc.coupled_rdevs);
6023 mutex_lock(®ulator_list_mutex);
6024 regulator_ena_gpio_free(rdev);
6025 mutex_unlock(®ulator_list_mutex);
6026 clean:
6027 if (dangling_of_gpiod)
6028 gpiod_put(config->ena_gpiod);
6029 kfree(config);
6030 put_device(&rdev->dev);
6031 rinse:
6032 if (dangling_cfg_gpiod)
6033 gpiod_put(cfg->ena_gpiod);
6034 return ERR_PTR(ret);
6035 }
6036 EXPORT_SYMBOL_GPL(regulator_register);
6037
6038 /**
6039 * regulator_unregister - unregister regulator
6040 * @rdev: regulator to unregister
6041 *
6042 * Called by regulator drivers to unregister a regulator.
6043 */
6044 void regulator_unregister(struct regulator_dev *rdev)
6045 {
6046 if (rdev == NULL)
6047 return;
6048
6049 if (rdev->supply) {
6050 while (rdev->use_count--)
6051 regulator_disable(rdev->supply);
6052 regulator_put(rdev->supply);
6053 }
6054
6055 flush_work(&rdev->disable_work.work);
6056
6057 mutex_lock(®ulator_list_mutex);
6058
6059 WARN_ON(rdev->open_count);
6060 regulator_remove_coupling(rdev);
6061 unset_regulator_supplies(rdev);
6062 list_del(&rdev->list);
6063 regulator_ena_gpio_free(rdev);
6064 device_unregister(&rdev->dev);
6065
6066 mutex_unlock(®ulator_list_mutex);
6067 }
6068 EXPORT_SYMBOL_GPL(regulator_unregister);
6069
6070 #ifdef CONFIG_SUSPEND
6071 /**
6072 * regulator_suspend - prepare regulators for system wide suspend
6073 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
6074 *
6075 * Configure each regulator with it's suspend operating parameters for state.
6076 *
6077 * Return: 0 on success or a negative error number on failure.
6078 */
6079 static int regulator_suspend(struct device *dev)
6080 {
6081 struct regulator_dev *rdev = dev_to_rdev(dev);
6082 suspend_state_t state = pm_suspend_target_state;
6083 int ret;
6084 const struct regulator_state *rstate;
6085
6086 rstate = regulator_get_suspend_state_check(rdev, state);
6087 if (!rstate)
6088 return 0;
6089
6090 regulator_lock(rdev);
6091 ret = __suspend_set_state(rdev, rstate);
6092 regulator_unlock(rdev);
6093
6094 return ret;
6095 }
6096
6097 static int regulator_resume(struct device *dev)
6098 {
6099 suspend_state_t state = pm_suspend_target_state;
6100 struct regulator_dev *rdev = dev_to_rdev(dev);
6101 struct regulator_state *rstate;
6102 int ret = 0;
6103
6104 rstate = regulator_get_suspend_state(rdev, state);
6105 if (rstate == NULL)
6106 return 0;
6107
6108 /* Avoid grabbing the lock if we don't need to */
6109 if (!rdev->desc->ops->resume)
6110 return 0;
6111
6112 regulator_lock(rdev);
6113
6114 if (rstate->enabled == ENABLE_IN_SUSPEND ||
6115 rstate->enabled == DISABLE_IN_SUSPEND)
6116 ret = rdev->desc->ops->resume(rdev);
6117
6118 regulator_unlock(rdev);
6119
6120 return ret;
6121 }
6122 #else /* !CONFIG_SUSPEND */
6123
6124 #define regulator_suspend NULL
6125 #define regulator_resume NULL
6126
6127 #endif /* !CONFIG_SUSPEND */
6128
6129 #ifdef CONFIG_PM
6130 static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
6131 .suspend = regulator_suspend,
6132 .resume = regulator_resume,
6133 };
6134 #endif
6135
6136 const struct class regulator_class = {
6137 .name = "regulator",
6138 .dev_release = regulator_dev_release,
6139 .dev_groups = regulator_dev_groups,
6140 #ifdef CONFIG_PM
6141 .pm = ®ulator_pm_ops,
6142 #endif
6143 };
6144 /**
6145 * regulator_has_full_constraints - the system has fully specified constraints
6146 *
6147 * Calling this function will cause the regulator API to disable all
6148 * regulators which have a zero use count and don't have an always_on
6149 * constraint in a late_initcall.
6150 *
6151 * The intention is that this will become the default behaviour in a
6152 * future kernel release so users are encouraged to use this facility
6153 * now.
6154 */
6155 void regulator_has_full_constraints(void)
6156 {
6157 has_full_constraints = 1;
6158 }
6159 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
6160
6161 /**
6162 * rdev_get_drvdata - get rdev regulator driver data
6163 * @rdev: regulator
6164 *
6165 * Get rdev regulator driver private data. This call can be used in the
6166 * regulator driver context.
6167 *
6168 * Return: Pointer to regulator driver private data.
6169 */
6170 void *rdev_get_drvdata(struct regulator_dev *rdev)
6171 {
6172 return rdev->reg_data;
6173 }
6174 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
6175
6176 /**
6177 * regulator_get_drvdata - get regulator driver data
6178 * @regulator: regulator
6179 *
6180 * Get regulator driver private data. This call can be used in the consumer
6181 * driver context when non API regulator specific functions need to be called.
6182 *
6183 * Return: Pointer to regulator driver private data.
6184 */
6185 void *regulator_get_drvdata(struct regulator *regulator)
6186 {
6187 return regulator->rdev->reg_data;
6188 }
6189 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
6190
6191 /**
6192 * regulator_set_drvdata - set regulator driver data
6193 * @regulator: regulator
6194 * @data: data
6195 */
6196 void regulator_set_drvdata(struct regulator *regulator, void *data)
6197 {
6198 regulator->rdev->reg_data = data;
6199 }
6200 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
6201
6202 /**
6203 * rdev_get_id - get regulator ID
6204 * @rdev: regulator
6205 *
6206 * Return: Regulator ID for @rdev.
6207 */
6208 int rdev_get_id(struct regulator_dev *rdev)
6209 {
6210 return rdev->desc->id;
6211 }
6212 EXPORT_SYMBOL_GPL(rdev_get_id);
6213
6214 struct device *rdev_get_dev(struct regulator_dev *rdev)
6215 {
6216 return &rdev->dev;
6217 }
6218 EXPORT_SYMBOL_GPL(rdev_get_dev);
6219
6220 struct regmap *rdev_get_regmap(struct regulator_dev *rdev)
6221 {
6222 return rdev->regmap;
6223 }
6224 EXPORT_SYMBOL_GPL(rdev_get_regmap);
6225
6226 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
6227 {
6228 return reg_init_data->driver_data;
6229 }
6230 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
6231
6232 #ifdef CONFIG_DEBUG_FS
6233 static int supply_map_show(struct seq_file *sf, void *data)
6234 {
6235 struct regulator_map *map;
6236
6237 list_for_each_entry(map, ®ulator_map_list, list) {
6238 seq_printf(sf, "%s -> %s.%s\n",
6239 rdev_get_name(map->regulator), map->dev_name,
6240 map->supply);
6241 }
6242
6243 return 0;
6244 }
6245 DEFINE_SHOW_ATTRIBUTE(supply_map);
6246
6247 struct summary_data {
6248 struct seq_file *s;
6249 struct regulator_dev *parent;
6250 int level;
6251 };
6252
6253 static void regulator_summary_show_subtree(struct seq_file *s,
6254 struct regulator_dev *rdev,
6255 int level);
6256
6257 static int regulator_summary_show_children(struct device *dev, void *data)
6258 {
6259 struct regulator_dev *rdev = dev_to_rdev(dev);
6260 struct summary_data *summary_data = data;
6261
6262 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
6263 regulator_summary_show_subtree(summary_data->s, rdev,
6264 summary_data->level + 1);
6265
6266 return 0;
6267 }
6268
6269 static void regulator_summary_show_subtree(struct seq_file *s,
6270 struct regulator_dev *rdev,
6271 int level)
6272 {
6273 struct regulation_constraints *c;
6274 struct regulator *consumer;
6275 struct summary_data summary_data;
6276 unsigned int opmode;
6277
6278 if (!rdev)
6279 return;
6280
6281 opmode = _regulator_get_mode_unlocked(rdev);
6282 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
6283 level * 3 + 1, "",
6284 30 - level * 3, rdev_get_name(rdev),
6285 rdev->use_count, rdev->open_count, rdev->bypass_count,
6286 regulator_opmode_to_str(opmode));
6287
6288 seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000);
6289 seq_printf(s, "%5dmA ",
6290 _regulator_get_current_limit_unlocked(rdev) / 1000);
6291
6292 c = rdev->constraints;
6293 if (c) {
6294 switch (rdev->desc->type) {
6295 case REGULATOR_VOLTAGE:
6296 seq_printf(s, "%5dmV %5dmV ",
6297 c->min_uV / 1000, c->max_uV / 1000);
6298 break;
6299 case REGULATOR_CURRENT:
6300 seq_printf(s, "%5dmA %5dmA ",
6301 c->min_uA / 1000, c->max_uA / 1000);
6302 break;
6303 }
6304 }
6305
6306 seq_puts(s, "\n");
6307
6308 list_for_each_entry(consumer, &rdev->consumer_list, list) {
6309 if (consumer->dev && consumer->dev->class == ®ulator_class)
6310 continue;
6311
6312 seq_printf(s, "%*s%-*s ",
6313 (level + 1) * 3 + 1, "",
6314 30 - (level + 1) * 3,
6315 consumer->supply_name ? consumer->supply_name :
6316 consumer->dev ? dev_name(consumer->dev) : "deviceless");
6317
6318 switch (rdev->desc->type) {
6319 case REGULATOR_VOLTAGE:
6320 seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
6321 consumer->enable_count,
6322 consumer->uA_load / 1000,
6323 consumer->uA_load && !consumer->enable_count ?
6324 '*' : ' ',
6325 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
6326 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
6327 break;
6328 case REGULATOR_CURRENT:
6329 break;
6330 }
6331
6332 seq_puts(s, "\n");
6333 }
6334
6335 summary_data.s = s;
6336 summary_data.level = level;
6337 summary_data.parent = rdev;
6338
6339 class_for_each_device(®ulator_class, NULL, &summary_data,
6340 regulator_summary_show_children);
6341 }
6342
6343 struct summary_lock_data {
6344 struct ww_acquire_ctx *ww_ctx;
6345 struct regulator_dev **new_contended_rdev;
6346 struct regulator_dev **old_contended_rdev;
6347 };
6348
6349 static int regulator_summary_lock_one(struct device *dev, void *data)
6350 {
6351 struct regulator_dev *rdev = dev_to_rdev(dev);
6352 struct summary_lock_data *lock_data = data;
6353 int ret = 0;
6354
6355 if (rdev != *lock_data->old_contended_rdev) {
6356 ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
6357
6358 if (ret == -EDEADLK)
6359 *lock_data->new_contended_rdev = rdev;
6360 else
6361 WARN_ON_ONCE(ret);
6362 } else {
6363 *lock_data->old_contended_rdev = NULL;
6364 }
6365
6366 return ret;
6367 }
6368
6369 static int regulator_summary_unlock_one(struct device *dev, void *data)
6370 {
6371 struct regulator_dev *rdev = dev_to_rdev(dev);
6372 struct summary_lock_data *lock_data = data;
6373
6374 if (lock_data) {
6375 if (rdev == *lock_data->new_contended_rdev)
6376 return -EDEADLK;
6377 }
6378
6379 regulator_unlock(rdev);
6380
6381 return 0;
6382 }
6383
6384 static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
6385 struct regulator_dev **new_contended_rdev,
6386 struct regulator_dev **old_contended_rdev)
6387 {
6388 struct summary_lock_data lock_data;
6389 int ret;
6390
6391 lock_data.ww_ctx = ww_ctx;
6392 lock_data.new_contended_rdev = new_contended_rdev;
6393 lock_data.old_contended_rdev = old_contended_rdev;
6394
6395 ret = class_for_each_device(®ulator_class, NULL, &lock_data,
6396 regulator_summary_lock_one);
6397 if (ret)
6398 class_for_each_device(®ulator_class, NULL, &lock_data,
6399 regulator_summary_unlock_one);
6400
6401 return ret;
6402 }
6403
6404 static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
6405 {
6406 struct regulator_dev *new_contended_rdev = NULL;
6407 struct regulator_dev *old_contended_rdev = NULL;
6408 int err;
6409
6410 mutex_lock(®ulator_list_mutex);
6411
6412 ww_acquire_init(ww_ctx, ®ulator_ww_class);
6413
6414 do {
6415 if (new_contended_rdev) {
6416 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
6417 old_contended_rdev = new_contended_rdev;
6418 old_contended_rdev->ref_cnt++;
6419 old_contended_rdev->mutex_owner = current;
6420 }
6421
6422 err = regulator_summary_lock_all(ww_ctx,
6423 &new_contended_rdev,
6424 &old_contended_rdev);
6425
6426 if (old_contended_rdev)
6427 regulator_unlock(old_contended_rdev);
6428
6429 } while (err == -EDEADLK);
6430
6431 ww_acquire_done(ww_ctx);
6432 }
6433
6434 static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
6435 {
6436 class_for_each_device(®ulator_class, NULL, NULL,
6437 regulator_summary_unlock_one);
6438 ww_acquire_fini(ww_ctx);
6439
6440 mutex_unlock(®ulator_list_mutex);
6441 }
6442
6443 static int regulator_summary_show_roots(struct device *dev, void *data)
6444 {
6445 struct regulator_dev *rdev = dev_to_rdev(dev);
6446 struct seq_file *s = data;
6447
6448 if (!rdev->supply)
6449 regulator_summary_show_subtree(s, rdev, 0);
6450
6451 return 0;
6452 }
6453
6454 static int regulator_summary_show(struct seq_file *s, void *data)
6455 {
6456 struct ww_acquire_ctx ww_ctx;
6457
6458 seq_puts(s, " regulator use open bypass opmode voltage current min max\n");
6459 seq_puts(s, "---------------------------------------------------------------------------------------\n");
6460
6461 regulator_summary_lock(&ww_ctx);
6462
6463 class_for_each_device(®ulator_class, NULL, s,
6464 regulator_summary_show_roots);
6465
6466 regulator_summary_unlock(&ww_ctx);
6467
6468 return 0;
6469 }
6470 DEFINE_SHOW_ATTRIBUTE(regulator_summary);
6471 #endif /* CONFIG_DEBUG_FS */
6472
6473 static int __init regulator_init(void)
6474 {
6475 int ret;
6476
6477 ret = class_register(®ulator_class);
6478
6479 debugfs_root = debugfs_create_dir("regulator", NULL);
6480 if (IS_ERR(debugfs_root))
6481 pr_debug("regulator: Failed to create debugfs directory\n");
6482
6483 #ifdef CONFIG_DEBUG_FS
6484 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
6485 &supply_map_fops);
6486
6487 debugfs_create_file("regulator_summary", 0444, debugfs_root,
6488 NULL, ®ulator_summary_fops);
6489 #endif
6490 regulator_dummy_init();
6491
6492 regulator_coupler_register(&generic_regulator_coupler);
6493
6494 return ret;
6495 }
6496
6497 /* init early to allow our consumers to complete system booting */
6498 core_initcall(regulator_init);
6499
6500 static int regulator_late_cleanup(struct device *dev, void *data)
6501 {
6502 struct regulator_dev *rdev = dev_to_rdev(dev);
6503 struct regulation_constraints *c = rdev->constraints;
6504 int ret;
6505
6506 if (c && c->always_on)
6507 return 0;
6508
6509 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
6510 return 0;
6511
6512 regulator_lock(rdev);
6513
6514 if (rdev->use_count)
6515 goto unlock;
6516
6517 /* If reading the status failed, assume that it's off. */
6518 if (_regulator_is_enabled(rdev) <= 0)
6519 goto unlock;
6520
6521 if (have_full_constraints()) {
6522 /* We log since this may kill the system if it goes
6523 * wrong.
6524 */
6525 rdev_info(rdev, "disabling\n");
6526 ret = _regulator_do_disable(rdev);
6527 if (ret != 0)
6528 rdev_err(rdev, "couldn't disable: %pe\n", ERR_PTR(ret));
6529 } else {
6530 /* The intention is that in future we will
6531 * assume that full constraints are provided
6532 * so warn even if we aren't going to do
6533 * anything here.
6534 */
6535 rdev_warn(rdev, "incomplete constraints, leaving on\n");
6536 }
6537
6538 unlock:
6539 regulator_unlock(rdev);
6540
6541 return 0;
6542 }
6543
6544 static bool regulator_ignore_unused;
6545 static int __init regulator_ignore_unused_setup(char *__unused)
6546 {
6547 regulator_ignore_unused = true;
6548 return 1;
6549 }
6550 __setup("regulator_ignore_unused", regulator_ignore_unused_setup);
6551
6552 static void regulator_init_complete_work_function(struct work_struct *work)
6553 {
6554 /*
6555 * Regulators may had failed to resolve their input supplies
6556 * when were registered, either because the input supply was
6557 * not registered yet or because its parent device was not
6558 * bound yet. So attempt to resolve the input supplies for
6559 * pending regulators before trying to disable unused ones.
6560 */
6561 class_for_each_device(®ulator_class, NULL, NULL,
6562 regulator_register_resolve_supply);
6563
6564 /*
6565 * For debugging purposes, it may be useful to prevent unused
6566 * regulators from being disabled.
6567 */
6568 if (regulator_ignore_unused) {
6569 pr_warn("regulator: Not disabling unused regulators\n");
6570 return;
6571 }
6572
6573 /* If we have a full configuration then disable any regulators
6574 * we have permission to change the status for and which are
6575 * not in use or always_on. This is effectively the default
6576 * for DT and ACPI as they have full constraints.
6577 */
6578 class_for_each_device(®ulator_class, NULL, NULL,
6579 regulator_late_cleanup);
6580 }
6581
6582 static DECLARE_DELAYED_WORK(regulator_init_complete_work,
6583 regulator_init_complete_work_function);
6584
6585 static int __init regulator_init_complete(void)
6586 {
6587 /*
6588 * Since DT doesn't provide an idiomatic mechanism for
6589 * enabling full constraints and since it's much more natural
6590 * with DT to provide them just assume that a DT enabled
6591 * system has full constraints.
6592 */
6593 if (of_have_populated_dt())
6594 has_full_constraints = true;
6595
6596 /*
6597 * We punt completion for an arbitrary amount of time since
6598 * systems like distros will load many drivers from userspace
6599 * so consumers might not always be ready yet, this is
6600 * particularly an issue with laptops where this might bounce
6601 * the display off then on. Ideally we'd get a notification
6602 * from userspace when this happens but we don't so just wait
6603 * a bit and hope we waited long enough. It'd be better if
6604 * we'd only do this on systems that need it, and a kernel
6605 * command line option might be useful.
6606 */
6607 schedule_delayed_work(®ulator_init_complete_work,
6608 msecs_to_jiffies(30000));
6609
6610 return 0;
6611 }
6612 late_initcall_sync(regulator_init_complete);