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