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