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