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