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  * @init_load_uA: After getting the regulator, regulator_set_load() will be
177  *                called with this load.  Initialised by the user before
178  *                using the bulk regulator APIs.
179  * @consumer:     The regulator consumer for the supply.  This will be managed
180  *                by the bulk API.
181  *
182  * The regulator APIs provide a series of regulator_bulk_() API calls as
183  * a convenience to consumers which require multiple supplies.  This
184  * structure is used to manage data for these calls.
185  */
186 struct regulator_bulk_data {
187 	const char *supply;
188 	int init_load_uA;
189 	struct regulator *consumer;
190 
191 	/* private: Internal use */
192 	int ret;
193 };
194 
195 #if defined(CONFIG_REGULATOR)
196 
197 /* regulator get and put */
198 struct regulator *__must_check regulator_get(struct device *dev,
199 					     const char *id);
200 struct regulator *__must_check devm_regulator_get(struct device *dev,
201 					     const char *id);
202 struct regulator *__must_check regulator_get_exclusive(struct device *dev,
203 						       const char *id);
204 struct regulator *__must_check devm_regulator_get_exclusive(struct device *dev,
205 							const char *id);
206 struct regulator *__must_check regulator_get_optional(struct device *dev,
207 						      const char *id);
208 struct regulator *__must_check devm_regulator_get_optional(struct device *dev,
209 							   const char *id);
210 void regulator_put(struct regulator *regulator);
211 void devm_regulator_put(struct regulator *regulator);
212 
213 int regulator_register_supply_alias(struct device *dev, const char *id,
214 				    struct device *alias_dev,
215 				    const char *alias_id);
216 void regulator_unregister_supply_alias(struct device *dev, const char *id);
217 
218 int regulator_bulk_register_supply_alias(struct device *dev,
219 					 const char *const *id,
220 					 struct device *alias_dev,
221 					 const char *const *alias_id,
222 					 int num_id);
223 void regulator_bulk_unregister_supply_alias(struct device *dev,
224 					    const char * const *id, int num_id);
225 
226 int devm_regulator_register_supply_alias(struct device *dev, const char *id,
227 					 struct device *alias_dev,
228 					 const char *alias_id);
229 
230 int devm_regulator_bulk_register_supply_alias(struct device *dev,
231 					      const char *const *id,
232 					      struct device *alias_dev,
233 					      const char *const *alias_id,
234 					      int num_id);
235 
236 /* regulator output control and status */
237 int __must_check regulator_enable(struct regulator *regulator);
238 int regulator_disable(struct regulator *regulator);
239 int regulator_force_disable(struct regulator *regulator);
240 int regulator_is_enabled(struct regulator *regulator);
241 int regulator_disable_deferred(struct regulator *regulator, int ms);
242 
243 int __must_check regulator_bulk_get(struct device *dev, int num_consumers,
244 				    struct regulator_bulk_data *consumers);
245 int __must_check devm_regulator_bulk_get(struct device *dev, int num_consumers,
246 					 struct regulator_bulk_data *consumers);
247 int __must_check devm_regulator_bulk_get_const(
248 	struct device *dev, int num_consumers,
249 	const struct regulator_bulk_data *in_consumers,
250 	struct regulator_bulk_data **out_consumers);
251 int __must_check regulator_bulk_enable(int num_consumers,
252 				       struct regulator_bulk_data *consumers);
253 int regulator_bulk_disable(int num_consumers,
254 			   struct regulator_bulk_data *consumers);
255 int regulator_bulk_force_disable(int num_consumers,
256 			   struct regulator_bulk_data *consumers);
257 void regulator_bulk_free(int num_consumers,
258 			 struct regulator_bulk_data *consumers);
259 
260 int regulator_count_voltages(struct regulator *regulator);
261 int regulator_list_voltage(struct regulator *regulator, unsigned selector);
262 int regulator_is_supported_voltage(struct regulator *regulator,
263 				   int min_uV, int max_uV);
264 unsigned int regulator_get_linear_step(struct regulator *regulator);
265 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV);
266 int regulator_set_voltage_time(struct regulator *regulator,
267 			       int old_uV, int new_uV);
268 int regulator_get_voltage(struct regulator *regulator);
269 int regulator_sync_voltage(struct regulator *regulator);
270 int regulator_set_current_limit(struct regulator *regulator,
271 			       int min_uA, int max_uA);
272 int regulator_get_current_limit(struct regulator *regulator);
273 
274 int regulator_set_mode(struct regulator *regulator, unsigned int mode);
275 unsigned int regulator_get_mode(struct regulator *regulator);
276 int regulator_get_error_flags(struct regulator *regulator,
277 				unsigned int *flags);
278 int regulator_set_load(struct regulator *regulator, int load_uA);
279 
280 int regulator_allow_bypass(struct regulator *regulator, bool allow);
281 
282 struct regmap *regulator_get_regmap(struct regulator *regulator);
283 int regulator_get_hardware_vsel_register(struct regulator *regulator,
284 					 unsigned *vsel_reg,
285 					 unsigned *vsel_mask);
286 int regulator_list_hardware_vsel(struct regulator *regulator,
287 				 unsigned selector);
288 
289 /* regulator notifier block */
290 int regulator_register_notifier(struct regulator *regulator,
291 			      struct notifier_block *nb);
292 int devm_regulator_register_notifier(struct regulator *regulator,
293 				     struct notifier_block *nb);
294 int regulator_unregister_notifier(struct regulator *regulator,
295 				struct notifier_block *nb);
296 void devm_regulator_unregister_notifier(struct regulator *regulator,
297 					struct notifier_block *nb);
298 
299 /* regulator suspend */
300 int regulator_suspend_enable(struct regulator_dev *rdev,
301 			     suspend_state_t state);
302 int regulator_suspend_disable(struct regulator_dev *rdev,
303 			      suspend_state_t state);
304 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
305 				  int max_uV, suspend_state_t state);
306 
307 /* driver data - core doesn't touch */
308 void *regulator_get_drvdata(struct regulator *regulator);
309 void regulator_set_drvdata(struct regulator *regulator, void *data);
310 
311 /* misc helpers */
312 
313 void regulator_bulk_set_supply_names(struct regulator_bulk_data *consumers,
314 				     const char *const *supply_names,
315 				     unsigned int num_supplies);
316 
317 bool regulator_is_equal(struct regulator *reg1, struct regulator *reg2);
318 
319 #else
320 
321 /*
322  * Make sure client drivers will still build on systems with no software
323  * controllable voltage or current regulators.
324  */
325 static inline struct regulator *__must_check regulator_get(struct device *dev,
326 	const char *id)
327 {
328 	/* Nothing except the stubbed out regulator API should be
329 	 * looking at the value except to check if it is an error
330 	 * value. Drivers are free to handle NULL specifically by
331 	 * skipping all regulator API calls, but they don't have to.
332 	 * Drivers which don't, should make sure they properly handle
333 	 * corner cases of the API, such as regulator_get_voltage()
334 	 * returning 0.
335 	 */
336 	return NULL;
337 }
338 
339 static inline struct regulator *__must_check
340 devm_regulator_get(struct device *dev, const char *id)
341 {
342 	return NULL;
343 }
344 
345 static inline struct regulator *__must_check
346 regulator_get_exclusive(struct device *dev, const char *id)
347 {
348 	return ERR_PTR(-ENODEV);
349 }
350 
351 static inline struct regulator *__must_check
352 devm_regulator_get_exclusive(struct device *dev, const char *id)
353 {
354 	return ERR_PTR(-ENODEV);
355 }
356 
357 static inline struct regulator *__must_check
358 regulator_get_optional(struct device *dev, const char *id)
359 {
360 	return ERR_PTR(-ENODEV);
361 }
362 
363 
364 static inline struct regulator *__must_check
365 devm_regulator_get_optional(struct device *dev, const char *id)
366 {
367 	return ERR_PTR(-ENODEV);
368 }
369 
370 static inline void regulator_put(struct regulator *regulator)
371 {
372 }
373 
374 static inline void devm_regulator_put(struct regulator *regulator)
375 {
376 }
377 
378 static inline int regulator_register_supply_alias(struct device *dev,
379 						  const char *id,
380 						  struct device *alias_dev,
381 						  const char *alias_id)
382 {
383 	return 0;
384 }
385 
386 static inline void regulator_unregister_supply_alias(struct device *dev,
387 						    const char *id)
388 {
389 }
390 
391 static inline int regulator_bulk_register_supply_alias(struct device *dev,
392 						const char *const *id,
393 						struct device *alias_dev,
394 						const char * const *alias_id,
395 						int num_id)
396 {
397 	return 0;
398 }
399 
400 static inline void regulator_bulk_unregister_supply_alias(struct device *dev,
401 						const char * const *id,
402 						int num_id)
403 {
404 }
405 
406 static inline int devm_regulator_register_supply_alias(struct device *dev,
407 						       const char *id,
408 						       struct device *alias_dev,
409 						       const char *alias_id)
410 {
411 	return 0;
412 }
413 
414 static inline int devm_regulator_bulk_register_supply_alias(struct device *dev,
415 						const char *const *id,
416 						struct device *alias_dev,
417 						const char *const *alias_id,
418 						int num_id)
419 {
420 	return 0;
421 }
422 
423 static inline int regulator_enable(struct regulator *regulator)
424 {
425 	return 0;
426 }
427 
428 static inline int regulator_disable(struct regulator *regulator)
429 {
430 	return 0;
431 }
432 
433 static inline int regulator_force_disable(struct regulator *regulator)
434 {
435 	return 0;
436 }
437 
438 static inline int regulator_disable_deferred(struct regulator *regulator,
439 					     int ms)
440 {
441 	return 0;
442 }
443 
444 static inline int regulator_is_enabled(struct regulator *regulator)
445 {
446 	return 1;
447 }
448 
449 static inline int regulator_bulk_get(struct device *dev,
450 				     int num_consumers,
451 				     struct regulator_bulk_data *consumers)
452 {
453 	return 0;
454 }
455 
456 static inline int devm_regulator_bulk_get(struct device *dev, int num_consumers,
457 					  struct regulator_bulk_data *consumers)
458 {
459 	return 0;
460 }
461 
462 static inline int regulator_bulk_enable(int num_consumers,
463 					struct regulator_bulk_data *consumers)
464 {
465 	return 0;
466 }
467 
468 static inline int regulator_bulk_disable(int num_consumers,
469 					 struct regulator_bulk_data *consumers)
470 {
471 	return 0;
472 }
473 
474 static inline int regulator_bulk_force_disable(int num_consumers,
475 					struct regulator_bulk_data *consumers)
476 {
477 	return 0;
478 }
479 
480 static inline void regulator_bulk_free(int num_consumers,
481 				       struct regulator_bulk_data *consumers)
482 {
483 }
484 
485 static inline int regulator_set_voltage(struct regulator *regulator,
486 					int min_uV, int max_uV)
487 {
488 	return 0;
489 }
490 
491 static inline int regulator_set_voltage_time(struct regulator *regulator,
492 					     int old_uV, int new_uV)
493 {
494 	return 0;
495 }
496 
497 static inline int regulator_get_voltage(struct regulator *regulator)
498 {
499 	return -EINVAL;
500 }
501 
502 static inline int regulator_sync_voltage(struct regulator *regulator)
503 {
504 	return -EINVAL;
505 }
506 
507 static inline int regulator_is_supported_voltage(struct regulator *regulator,
508 				   int min_uV, int max_uV)
509 {
510 	return 0;
511 }
512 
513 static inline unsigned int regulator_get_linear_step(struct regulator *regulator)
514 {
515 	return 0;
516 }
517 
518 static inline int regulator_set_current_limit(struct regulator *regulator,
519 					     int min_uA, int max_uA)
520 {
521 	return 0;
522 }
523 
524 static inline int regulator_get_current_limit(struct regulator *regulator)
525 {
526 	return 0;
527 }
528 
529 static inline int regulator_set_mode(struct regulator *regulator,
530 	unsigned int mode)
531 {
532 	return 0;
533 }
534 
535 static inline unsigned int regulator_get_mode(struct regulator *regulator)
536 {
537 	return REGULATOR_MODE_NORMAL;
538 }
539 
540 static inline int regulator_get_error_flags(struct regulator *regulator,
541 					    unsigned int *flags)
542 {
543 	return -EINVAL;
544 }
545 
546 static inline int regulator_set_load(struct regulator *regulator, int load_uA)
547 {
548 	return 0;
549 }
550 
551 static inline int regulator_allow_bypass(struct regulator *regulator,
552 					 bool allow)
553 {
554 	return 0;
555 }
556 
557 static inline struct regmap *regulator_get_regmap(struct regulator *regulator)
558 {
559 	return ERR_PTR(-EOPNOTSUPP);
560 }
561 
562 static inline int regulator_get_hardware_vsel_register(struct regulator *regulator,
563 						       unsigned *vsel_reg,
564 						       unsigned *vsel_mask)
565 {
566 	return -EOPNOTSUPP;
567 }
568 
569 static inline int regulator_list_hardware_vsel(struct regulator *regulator,
570 					       unsigned selector)
571 {
572 	return -EOPNOTSUPP;
573 }
574 
575 static inline int regulator_register_notifier(struct regulator *regulator,
576 			      struct notifier_block *nb)
577 {
578 	return 0;
579 }
580 
581 static inline int devm_regulator_register_notifier(struct regulator *regulator,
582 						   struct notifier_block *nb)
583 {
584 	return 0;
585 }
586 
587 static inline int regulator_unregister_notifier(struct regulator *regulator,
588 				struct notifier_block *nb)
589 {
590 	return 0;
591 }
592 
593 static inline int devm_regulator_unregister_notifier(struct regulator *regulator,
594 						     struct notifier_block *nb)
595 {
596 	return 0;
597 }
598 
599 static inline int regulator_suspend_enable(struct regulator_dev *rdev,
600 					   suspend_state_t state)
601 {
602 	return -EINVAL;
603 }
604 
605 static inline int regulator_suspend_disable(struct regulator_dev *rdev,
606 					    suspend_state_t state)
607 {
608 	return -EINVAL;
609 }
610 
611 static inline int regulator_set_suspend_voltage(struct regulator *regulator,
612 						int min_uV, int max_uV,
613 						suspend_state_t state)
614 {
615 	return -EINVAL;
616 }
617 
618 static inline void *regulator_get_drvdata(struct regulator *regulator)
619 {
620 	return NULL;
621 }
622 
623 static inline void regulator_set_drvdata(struct regulator *regulator,
624 	void *data)
625 {
626 }
627 
628 static inline int regulator_count_voltages(struct regulator *regulator)
629 {
630 	return 0;
631 }
632 
633 static inline int regulator_list_voltage(struct regulator *regulator, unsigned selector)
634 {
635 	return -EINVAL;
636 }
637 
638 static inline void
639 regulator_bulk_set_supply_names(struct regulator_bulk_data *consumers,
640 				const char *const *supply_names,
641 				unsigned int num_supplies)
642 {
643 }
644 
645 static inline bool
646 regulator_is_equal(struct regulator *reg1, struct regulator *reg2)
647 {
648 	return false;
649 }
650 #endif
651 
652 static inline int regulator_set_voltage_triplet(struct regulator *regulator,
653 						int min_uV, int target_uV,
654 						int max_uV)
655 {
656 	if (regulator_set_voltage(regulator, target_uV, max_uV) == 0)
657 		return 0;
658 
659 	return regulator_set_voltage(regulator, min_uV, max_uV);
660 }
661 
662 static inline int regulator_set_voltage_tol(struct regulator *regulator,
663 					    int new_uV, int tol_uV)
664 {
665 	if (regulator_set_voltage(regulator, new_uV, new_uV + tol_uV) == 0)
666 		return 0;
667 	else
668 		return regulator_set_voltage(regulator,
669 					     new_uV - tol_uV, new_uV + tol_uV);
670 }
671 
672 static inline int regulator_is_supported_voltage_tol(struct regulator *regulator,
673 						     int target_uV, int tol_uV)
674 {
675 	return regulator_is_supported_voltage(regulator,
676 					      target_uV - tol_uV,
677 					      target_uV + tol_uV);
678 }
679 
680 #endif
681