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