1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * consumer.h -- SoC Regulator consumer support.
4  *
5  * Copyright (C) 2007, 2008 Wolfson Microelectronics PLC.
6  *
7  * Author: Liam Girdwood <[email protected]>
8  *
9  * Regulator Consumer Interface.
10  *
11  * A Power Management Regulator framework for SoC based devices.
12  * Features:-
13  *   o Voltage and current level control.
14  *   o Operating mode control.
15  *   o Regulator status.
16  *   o sysfs entries for showing client devices and status
17  *
18  * EXPERIMENTAL FEATURES:
19  *   Dynamic Regulator operating Mode Switching (DRMS) - allows regulators
20  *   to use most efficient operating mode depending upon voltage and load and
21  *   is transparent to client drivers.
22  *
23  *   e.g. Devices x,y,z share regulator r. Device x and y draw 20mA each during
24  *   IO and 1mA at idle. Device z draws 100mA when under load and 5mA when
25  *   idling. Regulator r has > 90% efficiency in NORMAL mode at loads > 100mA
26  *   but this drops rapidly to 60% when below 100mA. Regulator r has > 90%
27  *   efficiency in IDLE mode at loads < 10mA. Thus regulator r will operate
28  *   in normal mode for loads > 10mA and in IDLE mode for load <= 10mA.
29  */
30 
31 #ifndef __LINUX_REGULATOR_CONSUMER_H_
32 #define __LINUX_REGULATOR_CONSUMER_H_
33 
34 #include <linux/err.h>
35 #include <linux/suspend.h>
36 
37 struct device;
38 struct notifier_block;
39 struct regmap;
40 struct regulator_dev;
41 
42 /*
43  * Regulator operating modes.
44  *
45  * Regulators can run in a variety of different operating modes depending on
46  * output load. This allows further system power savings by selecting the
47  * best (and most efficient) regulator mode for a desired load.
48  *
49  * Most drivers will only care about NORMAL. The modes below are generic and
50  * will probably not match the naming convention of your regulator data sheet
51  * but should match the use cases in the datasheet.
52  *
53  * In order of power efficiency (least efficient at top).
54  *
55  *  Mode       Description
56  *  FAST       Regulator can handle fast changes in it's load.
57  *             e.g. useful in CPU voltage & frequency scaling where
58  *             load can quickly increase with CPU frequency increases.
59  *
60  *  NORMAL     Normal regulator power supply mode. Most drivers will
61  *             use this mode.
62  *
63  *  IDLE       Regulator runs in a more efficient mode for light
64  *             loads. Can be used for devices that have a low power
65  *             requirement during periods of inactivity. This mode
66  *             may be more noisy than NORMAL and may not be able
67  *             to handle fast load switching.
68  *
69  *  STANDBY    Regulator runs in the most efficient mode for very
70  *             light loads. Can be used by devices when they are
71  *             in a sleep/standby state. This mode is likely to be
72  *             the most noisy and may not be able to handle fast load
73  *             switching.
74  *
75  * NOTE: Most regulators will only support a subset of these modes. Some
76  * will only just support NORMAL.
77  *
78  * These modes can be OR'ed together to make up a mask of valid register modes.
79  */
80 
81 #define REGULATOR_MODE_INVALID			0x0
82 #define REGULATOR_MODE_FAST			0x1
83 #define REGULATOR_MODE_NORMAL			0x2
84 #define REGULATOR_MODE_IDLE			0x4
85 #define REGULATOR_MODE_STANDBY			0x8
86 
87 /*
88  * Regulator notifier events.
89  *
90  * UNDER_VOLTAGE  Regulator output is under voltage.
91  * OVER_CURRENT   Regulator output current is too high.
92  * REGULATION_OUT Regulator output is out of regulation.
93  * FAIL           Regulator output has failed.
94  * OVER_TEMP      Regulator over temp.
95  * FORCE_DISABLE  Regulator forcibly shut down by software.
96  * VOLTAGE_CHANGE Regulator voltage changed.
97  *                Data passed is old voltage cast to (void *).
98  * DISABLE        Regulator was disabled.
99  * PRE_VOLTAGE_CHANGE   Regulator is about to have voltage changed.
100  *                      Data passed is "struct pre_voltage_change_data"
101  * ABORT_VOLTAGE_CHANGE Regulator voltage change failed for some reason.
102  *                      Data passed is old voltage cast to (void *).
103  * PRE_DISABLE    Regulator is about to be disabled
104  * ABORT_DISABLE  Regulator disable failed for some reason
105  *
106  * NOTE: These events can be OR'ed together when passed into handler.
107  */
108 
109 #define REGULATOR_EVENT_UNDER_VOLTAGE		0x01
110 #define REGULATOR_EVENT_OVER_CURRENT		0x02
111 #define REGULATOR_EVENT_REGULATION_OUT		0x04
112 #define REGULATOR_EVENT_FAIL			0x08
113 #define REGULATOR_EVENT_OVER_TEMP		0x10
114 #define REGULATOR_EVENT_FORCE_DISABLE		0x20
115 #define REGULATOR_EVENT_VOLTAGE_CHANGE		0x40
116 #define REGULATOR_EVENT_DISABLE			0x80
117 #define REGULATOR_EVENT_PRE_VOLTAGE_CHANGE	0x100
118 #define REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE	0x200
119 #define REGULATOR_EVENT_PRE_DISABLE		0x400
120 #define REGULATOR_EVENT_ABORT_DISABLE		0x800
121 #define REGULATOR_EVENT_ENABLE			0x1000
122 /*
123  * Following notifications should be emitted only if detected condition
124  * is such that the HW is likely to still be working but consumers should
125  * take a recovery action to prevent problems esacalating into errors.
126  */
127 #define REGULATOR_EVENT_UNDER_VOLTAGE_WARN	0x2000
128 #define REGULATOR_EVENT_OVER_CURRENT_WARN	0x4000
129 #define REGULATOR_EVENT_OVER_VOLTAGE_WARN	0x8000
130 #define REGULATOR_EVENT_OVER_TEMP_WARN		0x10000
131 #define REGULATOR_EVENT_WARN_MASK		0x1E000
132 
133 /*
134  * Regulator errors that can be queried using regulator_get_error_flags
135  *
136  * UNDER_VOLTAGE  Regulator output is under voltage.
137  * OVER_CURRENT   Regulator output current is too high.
138  * REGULATION_OUT Regulator output is out of regulation.
139  * FAIL           Regulator output has failed.
140  * OVER_TEMP      Regulator over temp.
141  *
142  * NOTE: These errors can be OR'ed together.
143  */
144 
145 #define REGULATOR_ERROR_UNDER_VOLTAGE		BIT(1)
146 #define REGULATOR_ERROR_OVER_CURRENT		BIT(2)
147 #define REGULATOR_ERROR_REGULATION_OUT		BIT(3)
148 #define REGULATOR_ERROR_FAIL			BIT(4)
149 #define REGULATOR_ERROR_OVER_TEMP		BIT(5)
150 
151 #define REGULATOR_ERROR_UNDER_VOLTAGE_WARN	BIT(6)
152 #define REGULATOR_ERROR_OVER_CURRENT_WARN	BIT(7)
153 #define REGULATOR_ERROR_OVER_VOLTAGE_WARN	BIT(8)
154 #define REGULATOR_ERROR_OVER_TEMP_WARN		BIT(9)
155 
156 /**
157  * struct pre_voltage_change_data - Data sent with PRE_VOLTAGE_CHANGE event
158  *
159  * @old_uV: Current voltage before change.
160  * @min_uV: Min voltage we'll change to.
161  * @max_uV: Max voltage we'll change to.
162  */
163 struct pre_voltage_change_data {
164 	unsigned long old_uV;
165 	unsigned long min_uV;
166 	unsigned long max_uV;
167 };
168 
169 struct regulator;
170 
171 /**
172  * struct regulator_bulk_data - Data used for bulk regulator operations.
173  *
174  * @supply:   The name of the supply.  Initialised by the user before
175  *            using the bulk regulator APIs.
176  * @consumer: The regulator consumer for the supply.  This will be managed
177  *            by the bulk API.
178  *
179  * The regulator APIs provide a series of regulator_bulk_() API calls as
180  * a convenience to consumers which require multiple supplies.  This
181  * structure is used to manage data for these calls.
182  */
183 struct regulator_bulk_data {
184 	const char *supply;
185 	struct regulator *consumer;
186 
187 	/* private: Internal use */
188 	int ret;
189 };
190 
191 #if defined(CONFIG_REGULATOR)
192 
193 /* regulator get and put */
194 struct regulator *__must_check regulator_get(struct device *dev,
195 					     const char *id);
196 struct regulator *__must_check devm_regulator_get(struct device *dev,
197 					     const char *id);
198 struct regulator *__must_check regulator_get_exclusive(struct device *dev,
199 						       const char *id);
200 struct regulator *__must_check devm_regulator_get_exclusive(struct device *dev,
201 							const char *id);
202 struct regulator *__must_check regulator_get_optional(struct device *dev,
203 						      const char *id);
204 struct regulator *__must_check devm_regulator_get_optional(struct device *dev,
205 							   const char *id);
206 void regulator_put(struct regulator *regulator);
207 void devm_regulator_put(struct regulator *regulator);
208 
209 int regulator_register_supply_alias(struct device *dev, const char *id,
210 				    struct device *alias_dev,
211 				    const char *alias_id);
212 void regulator_unregister_supply_alias(struct device *dev, const char *id);
213 
214 int regulator_bulk_register_supply_alias(struct device *dev,
215 					 const char *const *id,
216 					 struct device *alias_dev,
217 					 const char *const *alias_id,
218 					 int num_id);
219 void regulator_bulk_unregister_supply_alias(struct device *dev,
220 					    const char * const *id, int num_id);
221 
222 int devm_regulator_register_supply_alias(struct device *dev, const char *id,
223 					 struct device *alias_dev,
224 					 const char *alias_id);
225 void devm_regulator_unregister_supply_alias(struct device *dev,
226 					    const char *id);
227 
228 int devm_regulator_bulk_register_supply_alias(struct device *dev,
229 					      const char *const *id,
230 					      struct device *alias_dev,
231 					      const char *const *alias_id,
232 					      int num_id);
233 void devm_regulator_bulk_unregister_supply_alias(struct device *dev,
234 						 const char *const *id,
235 						 int num_id);
236 
237 /* regulator output control and status */
238 int __must_check regulator_enable(struct regulator *regulator);
239 int regulator_disable(struct regulator *regulator);
240 int regulator_force_disable(struct regulator *regulator);
241 int regulator_is_enabled(struct regulator *regulator);
242 int regulator_disable_deferred(struct regulator *regulator, int ms);
243 
244 int __must_check regulator_bulk_get(struct device *dev, int num_consumers,
245 				    struct regulator_bulk_data *consumers);
246 int __must_check devm_regulator_bulk_get(struct device *dev, int num_consumers,
247 					 struct regulator_bulk_data *consumers);
248 int __must_check regulator_bulk_enable(int num_consumers,
249 				       struct regulator_bulk_data *consumers);
250 int regulator_bulk_disable(int num_consumers,
251 			   struct regulator_bulk_data *consumers);
252 int regulator_bulk_force_disable(int num_consumers,
253 			   struct regulator_bulk_data *consumers);
254 void regulator_bulk_free(int num_consumers,
255 			 struct regulator_bulk_data *consumers);
256 
257 int regulator_count_voltages(struct regulator *regulator);
258 int regulator_list_voltage(struct regulator *regulator, unsigned selector);
259 int regulator_is_supported_voltage(struct regulator *regulator,
260 				   int min_uV, int max_uV);
261 unsigned int regulator_get_linear_step(struct regulator *regulator);
262 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV);
263 int regulator_set_voltage_time(struct regulator *regulator,
264 			       int old_uV, int new_uV);
265 int regulator_get_voltage(struct regulator *regulator);
266 int regulator_sync_voltage(struct regulator *regulator);
267 int regulator_set_current_limit(struct regulator *regulator,
268 			       int min_uA, int max_uA);
269 int regulator_get_current_limit(struct regulator *regulator);
270 
271 int regulator_set_mode(struct regulator *regulator, unsigned int mode);
272 unsigned int regulator_get_mode(struct regulator *regulator);
273 int regulator_get_error_flags(struct regulator *regulator,
274 				unsigned int *flags);
275 int regulator_set_load(struct regulator *regulator, int load_uA);
276 
277 int regulator_allow_bypass(struct regulator *regulator, bool allow);
278 
279 struct regmap *regulator_get_regmap(struct regulator *regulator);
280 int regulator_get_hardware_vsel_register(struct regulator *regulator,
281 					 unsigned *vsel_reg,
282 					 unsigned *vsel_mask);
283 int regulator_list_hardware_vsel(struct regulator *regulator,
284 				 unsigned selector);
285 
286 /* regulator notifier block */
287 int regulator_register_notifier(struct regulator *regulator,
288 			      struct notifier_block *nb);
289 int devm_regulator_register_notifier(struct regulator *regulator,
290 				     struct notifier_block *nb);
291 int regulator_unregister_notifier(struct regulator *regulator,
292 				struct notifier_block *nb);
293 void devm_regulator_unregister_notifier(struct regulator *regulator,
294 					struct notifier_block *nb);
295 
296 /* regulator suspend */
297 int regulator_suspend_enable(struct regulator_dev *rdev,
298 			     suspend_state_t state);
299 int regulator_suspend_disable(struct regulator_dev *rdev,
300 			      suspend_state_t state);
301 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
302 				  int max_uV, suspend_state_t state);
303 
304 /* driver data - core doesn't touch */
305 void *regulator_get_drvdata(struct regulator *regulator);
306 void regulator_set_drvdata(struct regulator *regulator, void *data);
307 
308 /* misc helpers */
309 
310 void regulator_bulk_set_supply_names(struct regulator_bulk_data *consumers,
311 				     const char *const *supply_names,
312 				     unsigned int num_supplies);
313 
314 bool regulator_is_equal(struct regulator *reg1, struct regulator *reg2);
315 
316 #else
317 
318 /*
319  * Make sure client drivers will still build on systems with no software
320  * controllable voltage or current regulators.
321  */
322 static inline struct regulator *__must_check regulator_get(struct device *dev,
323 	const char *id)
324 {
325 	/* Nothing except the stubbed out regulator API should be
326 	 * looking at the value except to check if it is an error
327 	 * value. Drivers are free to handle NULL specifically by
328 	 * skipping all regulator API calls, but they don't have to.
329 	 * Drivers which don't, should make sure they properly handle
330 	 * corner cases of the API, such as regulator_get_voltage()
331 	 * returning 0.
332 	 */
333 	return NULL;
334 }
335 
336 static inline struct regulator *__must_check
337 devm_regulator_get(struct device *dev, const char *id)
338 {
339 	return NULL;
340 }
341 
342 static inline struct regulator *__must_check
343 regulator_get_exclusive(struct device *dev, const char *id)
344 {
345 	return ERR_PTR(-ENODEV);
346 }
347 
348 static inline struct regulator *__must_check
349 devm_regulator_get_exclusive(struct device *dev, const char *id)
350 {
351 	return ERR_PTR(-ENODEV);
352 }
353 
354 static inline struct regulator *__must_check
355 regulator_get_optional(struct device *dev, const char *id)
356 {
357 	return ERR_PTR(-ENODEV);
358 }
359 
360 
361 static inline struct regulator *__must_check
362 devm_regulator_get_optional(struct device *dev, const char *id)
363 {
364 	return ERR_PTR(-ENODEV);
365 }
366 
367 static inline void regulator_put(struct regulator *regulator)
368 {
369 }
370 
371 static inline void devm_regulator_put(struct regulator *regulator)
372 {
373 }
374 
375 static inline int regulator_register_supply_alias(struct device *dev,
376 						  const char *id,
377 						  struct device *alias_dev,
378 						  const char *alias_id)
379 {
380 	return 0;
381 }
382 
383 static inline void regulator_unregister_supply_alias(struct device *dev,
384 						    const char *id)
385 {
386 }
387 
388 static inline int regulator_bulk_register_supply_alias(struct device *dev,
389 						const char *const *id,
390 						struct device *alias_dev,
391 						const char * const *alias_id,
392 						int num_id)
393 {
394 	return 0;
395 }
396 
397 static inline void regulator_bulk_unregister_supply_alias(struct device *dev,
398 						const char * const *id,
399 						int num_id)
400 {
401 }
402 
403 static inline int devm_regulator_register_supply_alias(struct device *dev,
404 						       const char *id,
405 						       struct device *alias_dev,
406 						       const char *alias_id)
407 {
408 	return 0;
409 }
410 
411 static inline void devm_regulator_unregister_supply_alias(struct device *dev,
412 							  const char *id)
413 {
414 }
415 
416 static inline int devm_regulator_bulk_register_supply_alias(struct device *dev,
417 						const char *const *id,
418 						struct device *alias_dev,
419 						const char *const *alias_id,
420 						int num_id)
421 {
422 	return 0;
423 }
424 
425 static inline void devm_regulator_bulk_unregister_supply_alias(
426 	struct device *dev, const char *const *id, int num_id)
427 {
428 }
429 
430 static inline int regulator_enable(struct regulator *regulator)
431 {
432 	return 0;
433 }
434 
435 static inline int regulator_disable(struct regulator *regulator)
436 {
437 	return 0;
438 }
439 
440 static inline int regulator_force_disable(struct regulator *regulator)
441 {
442 	return 0;
443 }
444 
445 static inline int regulator_disable_deferred(struct regulator *regulator,
446 					     int ms)
447 {
448 	return 0;
449 }
450 
451 static inline int regulator_is_enabled(struct regulator *regulator)
452 {
453 	return 1;
454 }
455 
456 static inline int regulator_bulk_get(struct device *dev,
457 				     int num_consumers,
458 				     struct regulator_bulk_data *consumers)
459 {
460 	return 0;
461 }
462 
463 static inline int devm_regulator_bulk_get(struct device *dev, int num_consumers,
464 					  struct regulator_bulk_data *consumers)
465 {
466 	return 0;
467 }
468 
469 static inline int regulator_bulk_enable(int num_consumers,
470 					struct regulator_bulk_data *consumers)
471 {
472 	return 0;
473 }
474 
475 static inline int regulator_bulk_disable(int num_consumers,
476 					 struct regulator_bulk_data *consumers)
477 {
478 	return 0;
479 }
480 
481 static inline int regulator_bulk_force_disable(int num_consumers,
482 					struct regulator_bulk_data *consumers)
483 {
484 	return 0;
485 }
486 
487 static inline void regulator_bulk_free(int num_consumers,
488 				       struct regulator_bulk_data *consumers)
489 {
490 }
491 
492 static inline int regulator_set_voltage(struct regulator *regulator,
493 					int min_uV, int max_uV)
494 {
495 	return 0;
496 }
497 
498 static inline int regulator_set_voltage_time(struct regulator *regulator,
499 					     int old_uV, int new_uV)
500 {
501 	return 0;
502 }
503 
504 static inline int regulator_get_voltage(struct regulator *regulator)
505 {
506 	return -EINVAL;
507 }
508 
509 static inline int regulator_sync_voltage(struct regulator *regulator)
510 {
511 	return -EINVAL;
512 }
513 
514 static inline int regulator_is_supported_voltage(struct regulator *regulator,
515 				   int min_uV, int max_uV)
516 {
517 	return 0;
518 }
519 
520 static inline unsigned int regulator_get_linear_step(struct regulator *regulator)
521 {
522 	return 0;
523 }
524 
525 static inline int regulator_set_current_limit(struct regulator *regulator,
526 					     int min_uA, int max_uA)
527 {
528 	return 0;
529 }
530 
531 static inline int regulator_get_current_limit(struct regulator *regulator)
532 {
533 	return 0;
534 }
535 
536 static inline int regulator_set_mode(struct regulator *regulator,
537 	unsigned int mode)
538 {
539 	return 0;
540 }
541 
542 static inline unsigned int regulator_get_mode(struct regulator *regulator)
543 {
544 	return REGULATOR_MODE_NORMAL;
545 }
546 
547 static inline int regulator_get_error_flags(struct regulator *regulator,
548 					    unsigned int *flags)
549 {
550 	return -EINVAL;
551 }
552 
553 static inline int regulator_set_load(struct regulator *regulator, int load_uA)
554 {
555 	return 0;
556 }
557 
558 static inline int regulator_allow_bypass(struct regulator *regulator,
559 					 bool allow)
560 {
561 	return 0;
562 }
563 
564 static inline struct regmap *regulator_get_regmap(struct regulator *regulator)
565 {
566 	return ERR_PTR(-EOPNOTSUPP);
567 }
568 
569 static inline int regulator_get_hardware_vsel_register(struct regulator *regulator,
570 						       unsigned *vsel_reg,
571 						       unsigned *vsel_mask)
572 {
573 	return -EOPNOTSUPP;
574 }
575 
576 static inline int regulator_list_hardware_vsel(struct regulator *regulator,
577 					       unsigned selector)
578 {
579 	return -EOPNOTSUPP;
580 }
581 
582 static inline int regulator_register_notifier(struct regulator *regulator,
583 			      struct notifier_block *nb)
584 {
585 	return 0;
586 }
587 
588 static inline int devm_regulator_register_notifier(struct regulator *regulator,
589 						   struct notifier_block *nb)
590 {
591 	return 0;
592 }
593 
594 static inline int regulator_unregister_notifier(struct regulator *regulator,
595 				struct notifier_block *nb)
596 {
597 	return 0;
598 }
599 
600 static inline int devm_regulator_unregister_notifier(struct regulator *regulator,
601 						     struct notifier_block *nb)
602 {
603 	return 0;
604 }
605 
606 static inline int regulator_suspend_enable(struct regulator_dev *rdev,
607 					   suspend_state_t state)
608 {
609 	return -EINVAL;
610 }
611 
612 static inline int regulator_suspend_disable(struct regulator_dev *rdev,
613 					    suspend_state_t state)
614 {
615 	return -EINVAL;
616 }
617 
618 static inline int regulator_set_suspend_voltage(struct regulator *regulator,
619 						int min_uV, int max_uV,
620 						suspend_state_t state)
621 {
622 	return -EINVAL;
623 }
624 
625 static inline void *regulator_get_drvdata(struct regulator *regulator)
626 {
627 	return NULL;
628 }
629 
630 static inline void regulator_set_drvdata(struct regulator *regulator,
631 	void *data)
632 {
633 }
634 
635 static inline int regulator_count_voltages(struct regulator *regulator)
636 {
637 	return 0;
638 }
639 
640 static inline int regulator_list_voltage(struct regulator *regulator, unsigned selector)
641 {
642 	return -EINVAL;
643 }
644 
645 static inline void
646 regulator_bulk_set_supply_names(struct regulator_bulk_data *consumers,
647 				const char *const *supply_names,
648 				unsigned int num_supplies)
649 {
650 }
651 
652 static inline bool
653 regulator_is_equal(struct regulator *reg1, struct regulator *reg2)
654 {
655 	return false;
656 }
657 #endif
658 
659 static inline int regulator_set_voltage_triplet(struct regulator *regulator,
660 						int min_uV, int target_uV,
661 						int max_uV)
662 {
663 	if (regulator_set_voltage(regulator, target_uV, max_uV) == 0)
664 		return 0;
665 
666 	return regulator_set_voltage(regulator, min_uV, max_uV);
667 }
668 
669 static inline int regulator_set_voltage_tol(struct regulator *regulator,
670 					    int new_uV, int tol_uV)
671 {
672 	if (regulator_set_voltage(regulator, new_uV, new_uV + tol_uV) == 0)
673 		return 0;
674 	else
675 		return regulator_set_voltage(regulator,
676 					     new_uV - tol_uV, new_uV + tol_uV);
677 }
678 
679 static inline int regulator_is_supported_voltage_tol(struct regulator *regulator,
680 						     int target_uV, int tol_uV)
681 {
682 	return regulator_is_supported_voltage(regulator,
683 					      target_uV - tol_uV,
684 					      target_uV + tol_uV);
685 }
686 
687 #endif
688