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/device.h>
39 
40 /*
41  * Regulator operating modes.
42  *
43  * Regulators can run in a variety of different operating modes depending on
44  * output load. This allows further system power savings by selecting the
45  * best (and most efficient) regulator mode for a desired load.
46  *
47  * Most drivers will only care about NORMAL. The modes below are generic and
48  * will probably not match the naming convention of your regulator data sheet
49  * but should match the use cases in the datasheet.
50  *
51  * In order of power efficiency (least efficient at top).
52  *
53  *  Mode       Description
54  *  FAST       Regulator can handle fast changes in it's load.
55  *             e.g. useful in CPU voltage & frequency scaling where
56  *             load can quickly increase with CPU frequency increases.
57  *
58  *  NORMAL     Normal regulator power supply mode. Most drivers will
59  *             use this mode.
60  *
61  *  IDLE       Regulator runs in a more efficient mode for light
62  *             loads. Can be used for devices that have a low power
63  *             requirement during periods of inactivity. This mode
64  *             may be more noisy than NORMAL and may not be able
65  *             to handle fast load switching.
66  *
67  *  STANDBY    Regulator runs in the most efficient mode for very
68  *             light loads. Can be used by devices when they are
69  *             in a sleep/standby state. This mode is likely to be
70  *             the most noisy and may not be able to handle fast load
71  *             switching.
72  *
73  * NOTE: Most regulators will only support a subset of these modes. Some
74  * will only just support NORMAL.
75  *
76  * These modes can be OR'ed together to make up a mask of valid register modes.
77  */
78 
79 #define REGULATOR_MODE_FAST			0x1
80 #define REGULATOR_MODE_NORMAL			0x2
81 #define REGULATOR_MODE_IDLE			0x4
82 #define REGULATOR_MODE_STANDBY			0x8
83 
84 /*
85  * Regulator notifier events.
86  *
87  * UNDER_VOLTAGE  Regulator output is under voltage.
88  * OVER_CURRENT   Regulator output current is too high.
89  * REGULATION_OUT Regulator output is out of regulation.
90  * FAIL           Regulator output has failed.
91  * OVER_TEMP      Regulator over temp.
92  * FORCE_DISABLE  Regulator forcibly shut down by software.
93  * VOLTAGE_CHANGE Regulator voltage changed.
94  * DISABLE        Regulator was disabled.
95  *
96  * NOTE: These events can be OR'ed together when passed into handler.
97  */
98 
99 #define REGULATOR_EVENT_UNDER_VOLTAGE		0x01
100 #define REGULATOR_EVENT_OVER_CURRENT		0x02
101 #define REGULATOR_EVENT_REGULATION_OUT		0x04
102 #define REGULATOR_EVENT_FAIL			0x08
103 #define REGULATOR_EVENT_OVER_TEMP		0x10
104 #define REGULATOR_EVENT_FORCE_DISABLE		0x20
105 #define REGULATOR_EVENT_VOLTAGE_CHANGE		0x40
106 #define REGULATOR_EVENT_DISABLE 		0x80
107 
108 struct regulator;
109 
110 /**
111  * struct regulator_bulk_data - Data used for bulk regulator operations.
112  *
113  * @supply:   The name of the supply.  Initialised by the user before
114  *            using the bulk regulator APIs.
115  * @consumer: The regulator consumer for the supply.  This will be managed
116  *            by the bulk API.
117  *
118  * The regulator APIs provide a series of regulator_bulk_() API calls as
119  * a convenience to consumers which require multiple supplies.  This
120  * structure is used to manage data for these calls.
121  */
122 struct regulator_bulk_data {
123 	const char *supply;
124 	struct regulator *consumer;
125 
126 	/* private: Internal use */
127 	int ret;
128 };
129 
130 #if defined(CONFIG_REGULATOR)
131 
132 /* regulator get and put */
133 struct regulator *__must_check regulator_get(struct device *dev,
134 					     const char *id);
135 struct regulator *__must_check devm_regulator_get(struct device *dev,
136 					     const char *id);
137 struct regulator *__must_check regulator_get_exclusive(struct device *dev,
138 						       const char *id);
139 void regulator_put(struct regulator *regulator);
140 void devm_regulator_put(struct regulator *regulator);
141 
142 /* regulator output control and status */
143 int regulator_enable(struct regulator *regulator);
144 int regulator_disable(struct regulator *regulator);
145 int regulator_force_disable(struct regulator *regulator);
146 int regulator_is_enabled(struct regulator *regulator);
147 int regulator_disable_deferred(struct regulator *regulator, int ms);
148 
149 int regulator_bulk_get(struct device *dev, int num_consumers,
150 		       struct regulator_bulk_data *consumers);
151 int devm_regulator_bulk_get(struct device *dev, int num_consumers,
152 			    struct regulator_bulk_data *consumers);
153 int regulator_bulk_enable(int num_consumers,
154 			  struct regulator_bulk_data *consumers);
155 int regulator_bulk_disable(int num_consumers,
156 			   struct regulator_bulk_data *consumers);
157 int regulator_bulk_force_disable(int num_consumers,
158 			   struct regulator_bulk_data *consumers);
159 void regulator_bulk_free(int num_consumers,
160 			 struct regulator_bulk_data *consumers);
161 
162 int regulator_count_voltages(struct regulator *regulator);
163 int regulator_list_voltage(struct regulator *regulator, unsigned selector);
164 int regulator_is_supported_voltage(struct regulator *regulator,
165 				   int min_uV, int max_uV);
166 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV);
167 int regulator_set_voltage_time(struct regulator *regulator,
168 			       int old_uV, int new_uV);
169 int regulator_get_voltage(struct regulator *regulator);
170 int regulator_sync_voltage(struct regulator *regulator);
171 int regulator_set_current_limit(struct regulator *regulator,
172 			       int min_uA, int max_uA);
173 int regulator_get_current_limit(struct regulator *regulator);
174 
175 int regulator_set_mode(struct regulator *regulator, unsigned int mode);
176 unsigned int regulator_get_mode(struct regulator *regulator);
177 int regulator_set_optimum_mode(struct regulator *regulator, int load_uA);
178 
179 /* regulator notifier block */
180 int regulator_register_notifier(struct regulator *regulator,
181 			      struct notifier_block *nb);
182 int regulator_unregister_notifier(struct regulator *regulator,
183 				struct notifier_block *nb);
184 
185 /* driver data - core doesn't touch */
186 void *regulator_get_drvdata(struct regulator *regulator);
187 void regulator_set_drvdata(struct regulator *regulator, void *data);
188 
189 #else
190 
191 /*
192  * Make sure client drivers will still build on systems with no software
193  * controllable voltage or current regulators.
194  */
195 static inline struct regulator *__must_check regulator_get(struct device *dev,
196 	const char *id)
197 {
198 	/* Nothing except the stubbed out regulator API should be
199 	 * looking at the value except to check if it is an error
200 	 * value. Drivers are free to handle NULL specifically by
201 	 * skipping all regulator API calls, but they don't have to.
202 	 * Drivers which don't, should make sure they properly handle
203 	 * corner cases of the API, such as regulator_get_voltage()
204 	 * returning 0.
205 	 */
206 	return NULL;
207 }
208 
209 static inline struct regulator *__must_check
210 devm_regulator_get(struct device *dev, const char *id)
211 {
212 	return NULL;
213 }
214 
215 static inline void regulator_put(struct regulator *regulator)
216 {
217 }
218 
219 static inline void devm_regulator_put(struct regulator *regulator)
220 {
221 }
222 
223 static inline int regulator_enable(struct regulator *regulator)
224 {
225 	return 0;
226 }
227 
228 static inline int regulator_disable(struct regulator *regulator)
229 {
230 	return 0;
231 }
232 
233 static inline int regulator_force_disable(struct regulator *regulator)
234 {
235 	return 0;
236 }
237 
238 static inline int regulator_disable_deferred(struct regulator *regulator,
239 					     int ms)
240 {
241 	return 0;
242 }
243 
244 static inline int regulator_is_enabled(struct regulator *regulator)
245 {
246 	return 1;
247 }
248 
249 static inline int regulator_bulk_get(struct device *dev,
250 				     int num_consumers,
251 				     struct regulator_bulk_data *consumers)
252 {
253 	return 0;
254 }
255 
256 static inline int devm_regulator_bulk_get(struct device *dev, int num_consumers,
257 					  struct regulator_bulk_data *consumers)
258 {
259 	return 0;
260 }
261 
262 static inline int regulator_bulk_enable(int num_consumers,
263 					struct regulator_bulk_data *consumers)
264 {
265 	return 0;
266 }
267 
268 static inline int regulator_bulk_disable(int num_consumers,
269 					 struct regulator_bulk_data *consumers)
270 {
271 	return 0;
272 }
273 
274 static inline int regulator_bulk_force_disable(int num_consumers,
275 					struct regulator_bulk_data *consumers)
276 {
277 	return 0;
278 }
279 
280 static inline void regulator_bulk_free(int num_consumers,
281 				       struct regulator_bulk_data *consumers)
282 {
283 }
284 
285 static inline int regulator_set_voltage(struct regulator *regulator,
286 					int min_uV, int max_uV)
287 {
288 	return 0;
289 }
290 
291 static inline int regulator_get_voltage(struct regulator *regulator)
292 {
293 	return 0;
294 }
295 
296 static inline int regulator_set_current_limit(struct regulator *regulator,
297 					     int min_uA, int max_uA)
298 {
299 	return 0;
300 }
301 
302 static inline int regulator_get_current_limit(struct regulator *regulator)
303 {
304 	return 0;
305 }
306 
307 static inline int regulator_set_mode(struct regulator *regulator,
308 	unsigned int mode)
309 {
310 	return 0;
311 }
312 
313 static inline unsigned int regulator_get_mode(struct regulator *regulator)
314 {
315 	return REGULATOR_MODE_NORMAL;
316 }
317 
318 static inline int regulator_set_optimum_mode(struct regulator *regulator,
319 					int load_uA)
320 {
321 	return REGULATOR_MODE_NORMAL;
322 }
323 
324 static inline int regulator_register_notifier(struct regulator *regulator,
325 			      struct notifier_block *nb)
326 {
327 	return 0;
328 }
329 
330 static inline int regulator_unregister_notifier(struct regulator *regulator,
331 				struct notifier_block *nb)
332 {
333 	return 0;
334 }
335 
336 static inline void *regulator_get_drvdata(struct regulator *regulator)
337 {
338 	return NULL;
339 }
340 
341 static inline void regulator_set_drvdata(struct regulator *regulator,
342 	void *data)
343 {
344 }
345 
346 #endif
347 
348 #endif
349