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