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