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