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