1 /*
2  * consumer.h -- SoC Regulator consumer support.
3  *
4  * Copyright (C) 2007, 2008 Wolfson Microelectronics PLC.
5  *
6  * Author: Liam Girdwood <[email protected]>
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  * PRE_DISABLE    Regulator is about to be disabled
105  * ABORT_DISABLE  Regulator disable failed for some reason
106  *
107  * NOTE: These events can be OR'ed together when passed into handler.
108  */
109 
110 #define REGULATOR_EVENT_UNDER_VOLTAGE		0x01
111 #define REGULATOR_EVENT_OVER_CURRENT		0x02
112 #define REGULATOR_EVENT_REGULATION_OUT		0x04
113 #define REGULATOR_EVENT_FAIL			0x08
114 #define REGULATOR_EVENT_OVER_TEMP		0x10
115 #define REGULATOR_EVENT_FORCE_DISABLE		0x20
116 #define REGULATOR_EVENT_VOLTAGE_CHANGE		0x40
117 #define REGULATOR_EVENT_DISABLE			0x80
118 #define REGULATOR_EVENT_PRE_VOLTAGE_CHANGE	0x100
119 #define REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE	0x200
120 #define REGULATOR_EVENT_PRE_DISABLE		0x400
121 #define REGULATOR_EVENT_ABORT_DISABLE		0x800
122 
123 /*
124  * Regulator errors that can be queried using regulator_get_error_flags
125  *
126  * UNDER_VOLTAGE  Regulator output is under voltage.
127  * OVER_CURRENT   Regulator output current is too high.
128  * REGULATION_OUT Regulator output is out of regulation.
129  * FAIL           Regulator output has failed.
130  * OVER_TEMP      Regulator over temp.
131  *
132  * NOTE: These errors can be OR'ed together.
133  */
134 
135 #define REGULATOR_ERROR_UNDER_VOLTAGE		BIT(1)
136 #define REGULATOR_ERROR_OVER_CURRENT		BIT(2)
137 #define REGULATOR_ERROR_REGULATION_OUT		BIT(3)
138 #define REGULATOR_ERROR_FAIL			BIT(4)
139 #define REGULATOR_ERROR_OVER_TEMP		BIT(5)
140 
141 
142 /**
143  * struct pre_voltage_change_data - Data sent with PRE_VOLTAGE_CHANGE event
144  *
145  * @old_uV: Current voltage before change.
146  * @min_uV: Min voltage we'll change to.
147  * @max_uV: Max voltage we'll change to.
148  */
149 struct pre_voltage_change_data {
150 	unsigned long old_uV;
151 	unsigned long min_uV;
152 	unsigned long max_uV;
153 };
154 
155 struct regulator;
156 
157 /**
158  * struct regulator_bulk_data - Data used for bulk regulator operations.
159  *
160  * @supply:   The name of the supply.  Initialised by the user before
161  *            using the bulk regulator APIs.
162  * @consumer: The regulator consumer for the supply.  This will be managed
163  *            by the bulk API.
164  *
165  * The regulator APIs provide a series of regulator_bulk_() API calls as
166  * a convenience to consumers which require multiple supplies.  This
167  * structure is used to manage data for these calls.
168  */
169 struct regulator_bulk_data {
170 	const char *supply;
171 	struct regulator *consumer;
172 
173 	/* private: Internal use */
174 	int ret;
175 };
176 
177 #if defined(CONFIG_REGULATOR)
178 
179 /* regulator get and put */
180 struct regulator *__must_check regulator_get(struct device *dev,
181 					     const char *id);
182 struct regulator *__must_check devm_regulator_get(struct device *dev,
183 					     const char *id);
184 struct regulator *__must_check regulator_get_exclusive(struct device *dev,
185 						       const char *id);
186 struct regulator *__must_check devm_regulator_get_exclusive(struct device *dev,
187 							const char *id);
188 struct regulator *__must_check regulator_get_optional(struct device *dev,
189 						      const char *id);
190 struct regulator *__must_check devm_regulator_get_optional(struct device *dev,
191 							   const char *id);
192 void regulator_put(struct regulator *regulator);
193 void devm_regulator_put(struct regulator *regulator);
194 
195 int regulator_register_supply_alias(struct device *dev, const char *id,
196 				    struct device *alias_dev,
197 				    const char *alias_id);
198 void regulator_unregister_supply_alias(struct device *dev, const char *id);
199 
200 int regulator_bulk_register_supply_alias(struct device *dev,
201 					 const char *const *id,
202 					 struct device *alias_dev,
203 					 const char *const *alias_id,
204 					 int num_id);
205 void regulator_bulk_unregister_supply_alias(struct device *dev,
206 					    const char * const *id, int num_id);
207 
208 int devm_regulator_register_supply_alias(struct device *dev, const char *id,
209 					 struct device *alias_dev,
210 					 const char *alias_id);
211 void devm_regulator_unregister_supply_alias(struct device *dev,
212 					    const char *id);
213 
214 int devm_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 devm_regulator_bulk_unregister_supply_alias(struct device *dev,
220 						 const char *const *id,
221 						 int num_id);
222 
223 /* regulator output control and status */
224 int __must_check regulator_enable(struct regulator *regulator);
225 int regulator_disable(struct regulator *regulator);
226 int regulator_force_disable(struct regulator *regulator);
227 int regulator_is_enabled(struct regulator *regulator);
228 int regulator_disable_deferred(struct regulator *regulator, int ms);
229 
230 int __must_check regulator_bulk_get(struct device *dev, int num_consumers,
231 				    struct regulator_bulk_data *consumers);
232 int __must_check devm_regulator_bulk_get(struct device *dev, int num_consumers,
233 					 struct regulator_bulk_data *consumers);
234 int __must_check regulator_bulk_enable(int num_consumers,
235 				       struct regulator_bulk_data *consumers);
236 int regulator_bulk_disable(int num_consumers,
237 			   struct regulator_bulk_data *consumers);
238 int regulator_bulk_force_disable(int num_consumers,
239 			   struct regulator_bulk_data *consumers);
240 void regulator_bulk_free(int num_consumers,
241 			 struct regulator_bulk_data *consumers);
242 
243 int regulator_count_voltages(struct regulator *regulator);
244 int regulator_list_voltage(struct regulator *regulator, unsigned selector);
245 int regulator_is_supported_voltage(struct regulator *regulator,
246 				   int min_uV, int max_uV);
247 unsigned int regulator_get_linear_step(struct regulator *regulator);
248 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV);
249 int regulator_set_voltage_time(struct regulator *regulator,
250 			       int old_uV, int new_uV);
251 int regulator_get_voltage(struct regulator *regulator);
252 int regulator_sync_voltage(struct regulator *regulator);
253 int regulator_set_current_limit(struct regulator *regulator,
254 			       int min_uA, int max_uA);
255 int regulator_get_current_limit(struct regulator *regulator);
256 
257 int regulator_set_mode(struct regulator *regulator, unsigned int mode);
258 unsigned int regulator_get_mode(struct regulator *regulator);
259 int regulator_get_error_flags(struct regulator *regulator,
260 				unsigned int *flags);
261 int regulator_set_load(struct regulator *regulator, int load_uA);
262 
263 int regulator_allow_bypass(struct regulator *regulator, bool allow);
264 
265 struct regmap *regulator_get_regmap(struct regulator *regulator);
266 int regulator_get_hardware_vsel_register(struct regulator *regulator,
267 					 unsigned *vsel_reg,
268 					 unsigned *vsel_mask);
269 int regulator_list_hardware_vsel(struct regulator *regulator,
270 				 unsigned selector);
271 
272 /* regulator notifier block */
273 int regulator_register_notifier(struct regulator *regulator,
274 			      struct notifier_block *nb);
275 int devm_regulator_register_notifier(struct regulator *regulator,
276 				     struct notifier_block *nb);
277 int regulator_unregister_notifier(struct regulator *regulator,
278 				struct notifier_block *nb);
279 void devm_regulator_unregister_notifier(struct regulator *regulator,
280 					struct notifier_block *nb);
281 
282 /* driver data - core doesn't touch */
283 void *regulator_get_drvdata(struct regulator *regulator);
284 void regulator_set_drvdata(struct regulator *regulator, void *data);
285 
286 #else
287 
288 /*
289  * Make sure client drivers will still build on systems with no software
290  * controllable voltage or current regulators.
291  */
292 static inline struct regulator *__must_check regulator_get(struct device *dev,
293 	const char *id)
294 {
295 	/* Nothing except the stubbed out regulator API should be
296 	 * looking at the value except to check if it is an error
297 	 * value. Drivers are free to handle NULL specifically by
298 	 * skipping all regulator API calls, but they don't have to.
299 	 * Drivers which don't, should make sure they properly handle
300 	 * corner cases of the API, such as regulator_get_voltage()
301 	 * returning 0.
302 	 */
303 	return NULL;
304 }
305 
306 static inline struct regulator *__must_check
307 devm_regulator_get(struct device *dev, const char *id)
308 {
309 	return NULL;
310 }
311 
312 static inline struct regulator *__must_check
313 regulator_get_exclusive(struct device *dev, const char *id)
314 {
315 	return ERR_PTR(-ENODEV);
316 }
317 
318 static inline struct regulator *__must_check
319 regulator_get_optional(struct device *dev, const char *id)
320 {
321 	return ERR_PTR(-ENODEV);
322 }
323 
324 
325 static inline struct regulator *__must_check
326 devm_regulator_get_optional(struct device *dev, const char *id)
327 {
328 	return ERR_PTR(-ENODEV);
329 }
330 
331 static inline void regulator_put(struct regulator *regulator)
332 {
333 }
334 
335 static inline void devm_regulator_put(struct regulator *regulator)
336 {
337 }
338 
339 static inline int 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 
347 static inline void regulator_unregister_supply_alias(struct device *dev,
348 						    const char *id)
349 {
350 }
351 
352 static inline int 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 
361 static inline void regulator_bulk_unregister_supply_alias(struct device *dev,
362 						const char * const *id,
363 						int num_id)
364 {
365 }
366 
367 static inline int devm_regulator_register_supply_alias(struct device *dev,
368 						       const char *id,
369 						       struct device *alias_dev,
370 						       const char *alias_id)
371 {
372 	return 0;
373 }
374 
375 static inline void devm_regulator_unregister_supply_alias(struct device *dev,
376 							  const char *id)
377 {
378 }
379 
380 static inline int devm_regulator_bulk_register_supply_alias(struct device *dev,
381 						const char *const *id,
382 						struct device *alias_dev,
383 						const char *const *alias_id,
384 						int num_id)
385 {
386 	return 0;
387 }
388 
389 static inline void devm_regulator_bulk_unregister_supply_alias(
390 	struct device *dev, const char *const *id, int num_id)
391 {
392 }
393 
394 static inline int regulator_enable(struct regulator *regulator)
395 {
396 	return 0;
397 }
398 
399 static inline int regulator_disable(struct regulator *regulator)
400 {
401 	return 0;
402 }
403 
404 static inline int regulator_force_disable(struct regulator *regulator)
405 {
406 	return 0;
407 }
408 
409 static inline int regulator_disable_deferred(struct regulator *regulator,
410 					     int ms)
411 {
412 	return 0;
413 }
414 
415 static inline int regulator_is_enabled(struct regulator *regulator)
416 {
417 	return 1;
418 }
419 
420 static inline int regulator_bulk_get(struct device *dev,
421 				     int num_consumers,
422 				     struct regulator_bulk_data *consumers)
423 {
424 	return 0;
425 }
426 
427 static inline int devm_regulator_bulk_get(struct device *dev, int num_consumers,
428 					  struct regulator_bulk_data *consumers)
429 {
430 	return 0;
431 }
432 
433 static inline int regulator_bulk_enable(int num_consumers,
434 					struct regulator_bulk_data *consumers)
435 {
436 	return 0;
437 }
438 
439 static inline int regulator_bulk_disable(int num_consumers,
440 					 struct regulator_bulk_data *consumers)
441 {
442 	return 0;
443 }
444 
445 static inline int regulator_bulk_force_disable(int num_consumers,
446 					struct regulator_bulk_data *consumers)
447 {
448 	return 0;
449 }
450 
451 static inline void regulator_bulk_free(int num_consumers,
452 				       struct regulator_bulk_data *consumers)
453 {
454 }
455 
456 static inline int regulator_set_voltage(struct regulator *regulator,
457 					int min_uV, int max_uV)
458 {
459 	return 0;
460 }
461 
462 static inline int regulator_set_voltage_time(struct regulator *regulator,
463 					     int old_uV, int new_uV)
464 {
465 	return 0;
466 }
467 
468 static inline int regulator_get_voltage(struct regulator *regulator)
469 {
470 	return -EINVAL;
471 }
472 
473 static inline int regulator_is_supported_voltage(struct regulator *regulator,
474 				   int min_uV, int max_uV)
475 {
476 	return 0;
477 }
478 
479 static inline int regulator_set_current_limit(struct regulator *regulator,
480 					     int min_uA, int max_uA)
481 {
482 	return 0;
483 }
484 
485 static inline int regulator_get_current_limit(struct regulator *regulator)
486 {
487 	return 0;
488 }
489 
490 static inline int regulator_set_mode(struct regulator *regulator,
491 	unsigned int mode)
492 {
493 	return 0;
494 }
495 
496 static inline unsigned int regulator_get_mode(struct regulator *regulator)
497 {
498 	return REGULATOR_MODE_NORMAL;
499 }
500 
501 static inline int regulator_get_error_flags(struct regulator *regulator,
502 					    unsigned int *flags)
503 {
504 	return -EINVAL;
505 }
506 
507 static inline int regulator_set_load(struct regulator *regulator, int load_uA)
508 {
509 	return REGULATOR_MODE_NORMAL;
510 }
511 
512 static inline int regulator_allow_bypass(struct regulator *regulator,
513 					 bool allow)
514 {
515 	return 0;
516 }
517 
518 static inline struct regmap *regulator_get_regmap(struct regulator *regulator)
519 {
520 	return ERR_PTR(-EOPNOTSUPP);
521 }
522 
523 static inline int regulator_get_hardware_vsel_register(struct regulator *regulator,
524 						       unsigned *vsel_reg,
525 						       unsigned *vsel_mask)
526 {
527 	return -EOPNOTSUPP;
528 }
529 
530 static inline int regulator_list_hardware_vsel(struct regulator *regulator,
531 					       unsigned selector)
532 {
533 	return -EOPNOTSUPP;
534 }
535 
536 static inline int regulator_register_notifier(struct regulator *regulator,
537 			      struct notifier_block *nb)
538 {
539 	return 0;
540 }
541 
542 static inline int devm_regulator_register_notifier(struct regulator *regulator,
543 						   struct notifier_block *nb)
544 {
545 	return 0;
546 }
547 
548 static inline int regulator_unregister_notifier(struct regulator *regulator,
549 				struct notifier_block *nb)
550 {
551 	return 0;
552 }
553 
554 static inline int devm_regulator_unregister_notifier(struct regulator *regulator,
555 						     struct notifier_block *nb)
556 {
557 	return 0;
558 }
559 
560 static inline void *regulator_get_drvdata(struct regulator *regulator)
561 {
562 	return NULL;
563 }
564 
565 static inline void regulator_set_drvdata(struct regulator *regulator,
566 	void *data)
567 {
568 }
569 
570 static inline int regulator_count_voltages(struct regulator *regulator)
571 {
572 	return 0;
573 }
574 
575 static inline int regulator_list_voltage(struct regulator *regulator, unsigned selector)
576 {
577 	return -EINVAL;
578 }
579 
580 #endif
581 
582 static inline int regulator_set_voltage_triplet(struct regulator *regulator,
583 						int min_uV, int target_uV,
584 						int max_uV)
585 {
586 	if (regulator_set_voltage(regulator, target_uV, max_uV) == 0)
587 		return 0;
588 
589 	return regulator_set_voltage(regulator, min_uV, max_uV);
590 }
591 
592 static inline int regulator_set_voltage_tol(struct regulator *regulator,
593 					    int new_uV, int tol_uV)
594 {
595 	if (regulator_set_voltage(regulator, new_uV, new_uV + tol_uV) == 0)
596 		return 0;
597 	else
598 		return regulator_set_voltage(regulator,
599 					     new_uV - tol_uV, new_uV + tol_uV);
600 }
601 
602 static inline int regulator_is_supported_voltage_tol(struct regulator *regulator,
603 						     int target_uV, int tol_uV)
604 {
605 	return regulator_is_supported_voltage(regulator,
606 					      target_uV - tol_uV,
607 					      target_uV + tol_uV);
608 }
609 
610 #endif
611