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);
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,
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);
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);
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);
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);
272 int regulator_allow_bypass(struct regulator *regulator, bool allow);
274 struct regmap *regulator_get_regmap(struct regulator *regulator);
275 int regulator_get_hardware_vsel_register(struct regulator *regulator,
277 unsigned *vsel_mask);
278 int regulator_list_hardware_vsel(struct regulator *regulator,
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);
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);
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);
305 void regulator_bulk_set_supply_names(struct regulator_bulk_data *consumers,
306 const char *const *supply_names,
307 unsigned int num_supplies);
309 bool regulator_is_equal(struct regulator *reg1, struct regulator *reg2);
314 * Make sure client drivers will still build on systems with no software
315 * controllable voltage or current regulators.
317 static inline struct regulator *__must_check regulator_get(struct device *dev,
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()
331 static inline struct regulator *__must_check
332 devm_regulator_get(struct device *dev, const char *id)
337 static inline struct regulator *__must_check
338 regulator_get_exclusive(struct device *dev, const char *id)
340 return ERR_PTR(-ENODEV);
343 static inline struct regulator *__must_check
344 devm_regulator_get_exclusive(struct device *dev, const char *id)
346 return ERR_PTR(-ENODEV);
349 static inline struct regulator *__must_check
350 regulator_get_optional(struct device *dev, const char *id)
352 return ERR_PTR(-ENODEV);
356 static inline struct regulator *__must_check
357 devm_regulator_get_optional(struct device *dev, const char *id)
359 return ERR_PTR(-ENODEV);
362 static inline void regulator_put(struct regulator *regulator)
366 static inline void devm_regulator_put(struct regulator *regulator)
370 static inline int regulator_register_supply_alias(struct device *dev,
372 struct device *alias_dev,
373 const char *alias_id)
378 static inline void regulator_unregister_supply_alias(struct device *dev,
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,
392 static inline void regulator_bulk_unregister_supply_alias(struct device *dev,
393 const char * const *id,
398 static inline int devm_regulator_register_supply_alias(struct device *dev,
400 struct device *alias_dev,
401 const char *alias_id)
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,
415 static inline int regulator_enable(struct regulator *regulator)
420 static inline int regulator_disable(struct regulator *regulator)
425 static inline int regulator_force_disable(struct regulator *regulator)
430 static inline int regulator_disable_deferred(struct regulator *regulator,
436 static inline int regulator_is_enabled(struct regulator *regulator)
441 static inline int regulator_bulk_get(struct device *dev,
443 struct regulator_bulk_data *consumers)
448 static inline int devm_regulator_bulk_get(struct device *dev, int num_consumers,
449 struct regulator_bulk_data *consumers)
454 static inline int regulator_bulk_enable(int num_consumers,
455 struct regulator_bulk_data *consumers)
460 static inline int regulator_bulk_disable(int num_consumers,
461 struct regulator_bulk_data *consumers)
466 static inline int regulator_bulk_force_disable(int num_consumers,
467 struct regulator_bulk_data *consumers)
472 static inline void regulator_bulk_free(int num_consumers,
473 struct regulator_bulk_data *consumers)
477 static inline int regulator_set_voltage(struct regulator *regulator,
478 int min_uV, int max_uV)
483 static inline int regulator_set_voltage_time(struct regulator *regulator,
484 int old_uV, int new_uV)
489 static inline int regulator_get_voltage(struct regulator *regulator)
494 static inline int regulator_sync_voltage(struct regulator *regulator)
499 static inline int regulator_is_supported_voltage(struct regulator *regulator,
500 int min_uV, int max_uV)
505 static inline unsigned int regulator_get_linear_step(struct regulator *regulator)
510 static inline int regulator_set_current_limit(struct regulator *regulator,
511 int min_uA, int max_uA)
516 static inline int regulator_get_current_limit(struct regulator *regulator)
521 static inline int regulator_set_mode(struct regulator *regulator,
527 static inline unsigned int regulator_get_mode(struct regulator *regulator)
529 return REGULATOR_MODE_NORMAL;
532 static inline int regulator_get_error_flags(struct regulator *regulator,
538 static inline int regulator_set_load(struct regulator *regulator, int load_uA)
543 static inline int regulator_allow_bypass(struct regulator *regulator,
549 static inline struct regmap *regulator_get_regmap(struct regulator *regulator)
551 return ERR_PTR(-EOPNOTSUPP);
554 static inline int regulator_get_hardware_vsel_register(struct regulator *regulator,
561 static inline int regulator_list_hardware_vsel(struct regulator *regulator,
567 static inline int regulator_register_notifier(struct regulator *regulator,
568 struct notifier_block *nb)
573 static inline int devm_regulator_register_notifier(struct regulator *regulator,
574 struct notifier_block *nb)
579 static inline int regulator_unregister_notifier(struct regulator *regulator,
580 struct notifier_block *nb)
585 static inline int devm_regulator_unregister_notifier(struct regulator *regulator,
586 struct notifier_block *nb)
591 static inline int regulator_suspend_enable(struct regulator_dev *rdev,
592 suspend_state_t state)
597 static inline int regulator_suspend_disable(struct regulator_dev *rdev,
598 suspend_state_t state)
603 static inline int regulator_set_suspend_voltage(struct regulator *regulator,
604 int min_uV, int max_uV,
605 suspend_state_t state)
610 static inline void *regulator_get_drvdata(struct regulator *regulator)
615 static inline void regulator_set_drvdata(struct regulator *regulator,
620 static inline int regulator_count_voltages(struct regulator *regulator)
625 static inline int regulator_list_voltage(struct regulator *regulator, unsigned selector)
631 regulator_bulk_set_supply_names(struct regulator_bulk_data *consumers,
632 const char *const *supply_names,
633 unsigned int num_supplies)
638 regulator_is_equal(struct regulator *reg1, struct regulator *reg2)
644 static inline int regulator_set_voltage_triplet(struct regulator *regulator,
645 int min_uV, int target_uV,
648 if (regulator_set_voltage(regulator, target_uV, max_uV) == 0)
651 return regulator_set_voltage(regulator, min_uV, max_uV);
654 static inline int regulator_set_voltage_tol(struct regulator *regulator,
655 int new_uV, int tol_uV)
657 if (regulator_set_voltage(regulator, new_uV, new_uV + tol_uV) == 0)
660 return regulator_set_voltage(regulator,
661 new_uV - tol_uV, new_uV + tol_uV);
664 static inline int regulator_is_supported_voltage_tol(struct regulator *regulator,
665 int target_uV, int tol_uV)
667 return regulator_is_supported_voltage(regulator,