1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * consumer.h -- SoC Regulator consumer support.
5 * Copyright (C) 2007, 2008 Wolfson Microelectronics PLC.
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 * Regulator Consumer Interface.
11 * A Power Management Regulator framework for SoC based devices.
13 * o Voltage and current level control.
14 * o Operating mode control.
16 * o sysfs entries for showing client devices and status
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.
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.
31 #ifndef __LINUX_REGULATOR_CONSUMER_H_
32 #define __LINUX_REGULATOR_CONSUMER_H_
34 #include <linux/err.h>
35 #include <linux/suspend.h>
38 struct notifier_block;
43 * Regulator operating modes.
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.
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.
53 * In order of power efficiency (least efficient at top).
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.
60 * NORMAL Normal regulator power supply mode. Most drivers will
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.
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
75 * NOTE: Most regulators will only support a subset of these modes. Some
76 * will only just support NORMAL.
78 * These modes can be OR'ed together to make up a mask of valid register modes.
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
88 * Regulator notifier events.
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
106 * NOTE: These events can be OR'ed together when passed into handler.
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
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.
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
134 * Regulator errors that can be queried using regulator_get_error_flags
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.
142 * NOTE: These errors can be OR'ed together.
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)
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)
157 * struct pre_voltage_change_data - Data sent with PRE_VOLTAGE_CHANGE event
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.
163 struct pre_voltage_change_data {
164 unsigned long old_uV;
165 unsigned long min_uV;
166 unsigned long max_uV;
172 * struct regulator_bulk_data - Data used for bulk regulator operations.
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
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.
183 struct regulator_bulk_data {
185 struct regulator *consumer;
187 /* private: Internal use */
191 #if defined(CONFIG_REGULATOR)
193 /* regulator get and put */
194 struct regulator *__must_check regulator_get(struct device *dev,
196 struct regulator *__must_check devm_regulator_get(struct device *dev,
198 struct regulator *__must_check regulator_get_exclusive(struct device *dev,
200 struct regulator *__must_check devm_regulator_get_exclusive(struct device *dev,
202 struct regulator *__must_check regulator_get_optional(struct device *dev,
204 struct regulator *__must_check devm_regulator_get_optional(struct device *dev,
206 void regulator_put(struct regulator *regulator);
207 void devm_regulator_put(struct regulator *regulator);
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);
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,
219 void regulator_bulk_unregister_supply_alias(struct device *dev,
220 const char * const *id, int num_id);
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,
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,
233 void devm_regulator_bulk_unregister_supply_alias(struct device *dev,
234 const char *const *id,
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);
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);
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);
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);
277 int regulator_allow_bypass(struct regulator *regulator, bool allow);
279 struct regmap *regulator_get_regmap(struct regulator *regulator);
280 int regulator_get_hardware_vsel_register(struct regulator *regulator,
282 unsigned *vsel_mask);
283 int regulator_list_hardware_vsel(struct regulator *regulator,
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);
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);
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);
310 void regulator_bulk_set_supply_names(struct regulator_bulk_data *consumers,
311 const char *const *supply_names,
312 unsigned int num_supplies);
314 bool regulator_is_equal(struct regulator *reg1, struct regulator *reg2);
319 * Make sure client drivers will still build on systems with no software
320 * controllable voltage or current regulators.
322 static inline struct regulator *__must_check regulator_get(struct device *dev,
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()
336 static inline struct regulator *__must_check
337 devm_regulator_get(struct device *dev, const char *id)
342 static inline struct regulator *__must_check
343 regulator_get_exclusive(struct device *dev, const char *id)
345 return ERR_PTR(-ENODEV);
348 static inline struct regulator *__must_check
349 devm_regulator_get_exclusive(struct device *dev, const char *id)
351 return ERR_PTR(-ENODEV);
354 static inline struct regulator *__must_check
355 regulator_get_optional(struct device *dev, const char *id)
357 return ERR_PTR(-ENODEV);
361 static inline struct regulator *__must_check
362 devm_regulator_get_optional(struct device *dev, const char *id)
364 return ERR_PTR(-ENODEV);
367 static inline void regulator_put(struct regulator *regulator)
371 static inline void devm_regulator_put(struct regulator *regulator)
375 static inline int regulator_register_supply_alias(struct device *dev,
377 struct device *alias_dev,
378 const char *alias_id)
383 static inline void regulator_unregister_supply_alias(struct device *dev,
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,
397 static inline void regulator_bulk_unregister_supply_alias(struct device *dev,
398 const char * const *id,
403 static inline int devm_regulator_register_supply_alias(struct device *dev,
405 struct device *alias_dev,
406 const char *alias_id)
411 static inline void devm_regulator_unregister_supply_alias(struct device *dev,
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,
425 static inline void devm_regulator_bulk_unregister_supply_alias(
426 struct device *dev, const char *const *id, int num_id)
430 static inline int regulator_enable(struct regulator *regulator)
435 static inline int regulator_disable(struct regulator *regulator)
440 static inline int regulator_force_disable(struct regulator *regulator)
445 static inline int regulator_disable_deferred(struct regulator *regulator,
451 static inline int regulator_is_enabled(struct regulator *regulator)
456 static inline int regulator_bulk_get(struct device *dev,
458 struct regulator_bulk_data *consumers)
463 static inline int devm_regulator_bulk_get(struct device *dev, int num_consumers,
464 struct regulator_bulk_data *consumers)
469 static inline int regulator_bulk_enable(int num_consumers,
470 struct regulator_bulk_data *consumers)
475 static inline int regulator_bulk_disable(int num_consumers,
476 struct regulator_bulk_data *consumers)
481 static inline int regulator_bulk_force_disable(int num_consumers,
482 struct regulator_bulk_data *consumers)
487 static inline void regulator_bulk_free(int num_consumers,
488 struct regulator_bulk_data *consumers)
492 static inline int regulator_set_voltage(struct regulator *regulator,
493 int min_uV, int max_uV)
498 static inline int regulator_set_voltage_time(struct regulator *regulator,
499 int old_uV, int new_uV)
504 static inline int regulator_get_voltage(struct regulator *regulator)
509 static inline int regulator_sync_voltage(struct regulator *regulator)
514 static inline int regulator_is_supported_voltage(struct regulator *regulator,
515 int min_uV, int max_uV)
520 static inline unsigned int regulator_get_linear_step(struct regulator *regulator)
525 static inline int regulator_set_current_limit(struct regulator *regulator,
526 int min_uA, int max_uA)
531 static inline int regulator_get_current_limit(struct regulator *regulator)
536 static inline int regulator_set_mode(struct regulator *regulator,
542 static inline unsigned int regulator_get_mode(struct regulator *regulator)
544 return REGULATOR_MODE_NORMAL;
547 static inline int regulator_get_error_flags(struct regulator *regulator,
553 static inline int regulator_set_load(struct regulator *regulator, int load_uA)
558 static inline int regulator_allow_bypass(struct regulator *regulator,
564 static inline struct regmap *regulator_get_regmap(struct regulator *regulator)
566 return ERR_PTR(-EOPNOTSUPP);
569 static inline int regulator_get_hardware_vsel_register(struct regulator *regulator,
576 static inline int regulator_list_hardware_vsel(struct regulator *regulator,
582 static inline int regulator_register_notifier(struct regulator *regulator,
583 struct notifier_block *nb)
588 static inline int devm_regulator_register_notifier(struct regulator *regulator,
589 struct notifier_block *nb)
594 static inline int regulator_unregister_notifier(struct regulator *regulator,
595 struct notifier_block *nb)
600 static inline int devm_regulator_unregister_notifier(struct regulator *regulator,
601 struct notifier_block *nb)
606 static inline int regulator_suspend_enable(struct regulator_dev *rdev,
607 suspend_state_t state)
612 static inline int regulator_suspend_disable(struct regulator_dev *rdev,
613 suspend_state_t state)
618 static inline int regulator_set_suspend_voltage(struct regulator *regulator,
619 int min_uV, int max_uV,
620 suspend_state_t state)
625 static inline void *regulator_get_drvdata(struct regulator *regulator)
630 static inline void regulator_set_drvdata(struct regulator *regulator,
635 static inline int regulator_count_voltages(struct regulator *regulator)
640 static inline int regulator_list_voltage(struct regulator *regulator, unsigned selector)
646 regulator_bulk_set_supply_names(struct regulator_bulk_data *consumers,
647 const char *const *supply_names,
648 unsigned int num_supplies)
653 regulator_is_equal(struct regulator *reg1, struct regulator *reg2)
659 static inline int regulator_set_voltage_triplet(struct regulator *regulator,
660 int min_uV, int target_uV,
663 if (regulator_set_voltage(regulator, target_uV, max_uV) == 0)
666 return regulator_set_voltage(regulator, min_uV, max_uV);
669 static inline int regulator_set_voltage_tol(struct regulator *regulator,
670 int new_uV, int tol_uV)
672 if (regulator_set_voltage(regulator, new_uV, new_uV + tol_uV) == 0)
675 return regulator_set_voltage(regulator,
676 new_uV - tol_uV, new_uV + tol_uV);
679 static inline int regulator_is_supported_voltage_tol(struct regulator *regulator,
680 int target_uV, int tol_uV)
682 return regulator_is_supported_voltage(regulator,