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