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