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