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