xref: /rk3399_rockchip-uboot/include/power/regulator.h (revision aee63dc84c1f5be59ea35ceb209a4ea937bdeb41)
1 /*
2  *  Copyright (C) 2014-2015 Samsung Electronics
3  *  Przemyslaw Marczak <p.marczak@samsung.com>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #ifndef _INCLUDE_REGULATOR_H_
9 #define _INCLUDE_REGULATOR_H_
10 
11 /**
12  * U-Boot Voltage/Current Regulator
13  * ================================
14  *
15  * The regulator API is based on a driver model, with the device tree support.
16  * And this header describes the functions and data types for the uclass id:
17  * 'UCLASS_REGULATOR' and the regulator driver API.
18  *
19  * The regulator uclass - is based on uclass platform data which is allocated,
20  * automatically for each regulator device on bind and 'dev->uclass_platdata'
21  * points to it. The data type is: 'struct dm_regulator_uclass_platdata'.
22  * The uclass file: 'drivers/power/regulator/regulator-uclass.c'
23  *
24  * The regulator device - is based on driver's model 'struct udevice'.
25  * The API can use regulator name in two meanings:
26  * - devname  - the regulator device's name: 'dev->name'
27  * - platname - the device's platdata's name. So in the code it looks like:
28  *              'uc_pdata = dev->uclass_platdata'; 'name = uc_pdata->name'.
29  *
30  * The regulator device driver - provide an implementation of uclass operations
31  * pointed by 'dev->driver->ops' as a struct of type 'struct dm_regulator_ops'.
32  *
33  * To proper bind the regulator device, the device tree node should provide
34  * regulator constraints, like in the example below:
35  *
36  * ldo1 {
37  *      regulator-name = "VDD_MMC_1.8V";     (must be unique for proper bind)
38  *      regulator-min-microvolt = <1000000>; (optional)
39  *      regulator-max-microvolt = <1000000>; (optional)
40  *      regulator-min-microamp = <1000>;     (optional)
41  *      regulator-max-microamp = <1000>;     (optional)
42  *      regulator-always-on;                 (optional)
43  *      regulator-boot-on;                   (optional)
44  * };
45  *
46  * Note: For the proper operation, at least name constraint is needed, since
47  * it can be used when calling regulator_get_by_platname(). And the mandatory
48  * rule for this name is, that it must be globally unique for the single dts.
49  * If regulator-name property is not provided, node name will be chosen.
50  *
51  * Regulator bind:
52  * For each regulator device, the device_bind() should be called with passed
53  * device tree offset. This is required for this uclass's '.post_bind' method,
54  * which does the scan on the device node, for the 'regulator-name' constraint.
55  * If the parent is not a PMIC device, and the child is not bind by function:
56  * 'pmic_bind_childs()', then it's recommended to bind the device by call to
57  * dm_scan_fdt_dev() - this is usually done automatically for bus devices,
58  * as a post bind method.
59  *
60  * Regulator get:
61  * Having the device's name constraint, we can call regulator_by_platname(),
62  * to find the required regulator. Before return, the regulator is probed,
63  * and the rest of its constraints are put into the device's uclass platform
64  * data, by the uclass regulator '.pre_probe' method.
65  *
66  * For more info about PMIC bind, please refer to file: 'include/power/pmic.h'
67  *
68  * Note:
69  * Please do not use the device_bind_by_name() function, since it pass '-1' as
70  * device node offset - and the bind will fail on uclass .post_bind method,
71  * because of missing 'regulator-name' constraint.
72  *
73  *
74  * Fixed Voltage/Current Regulator
75  * ===============================
76  *
77  * When fixed voltage regulator is needed, then enable the config:
78  * - CONFIG_DM_REGULATOR_FIXED
79  *
80  * The driver file: 'drivers/power/regulator/fixed.c', provides basic support
81  * for control the GPIO, and return the device tree constraint values.
82  *
83  * To bind the fixed voltage regulator device, we usually use a 'simple-bus'
84  * node as a parent. And 'regulator-fixed' for the driver compatible. This is
85  * the same as in the kernel. The example node of fixed regulator:
86  *
87  * simple-bus {
88  *     compatible = "simple-bus";
89  *     #address-cells = <1>;
90  *     #size-cells = <0>;
91  *
92  *     blue_led {
93  *         compatible = "regulator-fixed";
94  *         regulator-name = "VDD_LED_3.3V";
95  *         regulator-min-microvolt = <3300000>;
96  *         regulator-max-microvolt = <3300000>;
97  *         gpio = <&gpc1 0 GPIO_ACTIVE_LOW>;
98  *     };
99  * };
100  *
101  * The fixed regulator devices also provide regulator uclass platform data. And
102  * devices bound from such node, can use the regulator drivers API.
103 */
104 
105 /* enum regulator_type - used for regulator_*() variant calls */
106 enum regulator_type {
107 	REGULATOR_TYPE_LDO = 0,
108 	REGULATOR_TYPE_BUCK,
109 	REGULATOR_TYPE_DVS,
110 	REGULATOR_TYPE_FIXED,
111 	REGULATOR_TYPE_GPIO,
112 	REGULATOR_TYPE_OTHER,
113 };
114 
115 /**
116  * struct dm_regulator_mode - this structure holds an information about
117  * each regulator operation mode. Probably in most cases - an array.
118  * This will be probably a driver-static data, since it is device-specific.
119  *
120  * @id             - a driver-specific mode id
121  * @register_value - a driver-specific value for its mode id
122  * @name           - the name of mode - used for regulator command
123  * Note:
124  * The field 'id', should be always a positive number, since the negative values
125  * are reserved for the errno numbers when returns the mode id.
126  */
127 struct dm_regulator_mode {
128 	int id; /* Set only as >= 0 (negative value is reserved for errno) */
129 	int register_value;
130 	const char *name;
131 };
132 
133 enum regulator_flag {
134 	REGULATOR_FLAG_AUTOSET_UV	= 1 << 0,
135 	REGULATOR_FLAG_AUTOSET_UA	= 1 << 1,
136 };
137 
138 /**
139  * struct dm_regulator_uclass_platdata - pointed by dev->uclass_platdata, and
140  * allocated on each regulator bind. This structure holds an information
141  * about each regulator's constraints and supported operation modes.
142  * There is no "step" voltage value - so driver should take care of this.
143  *
144  * @type       - one of 'enum regulator_type'
145  * @mode       - pointer to the regulator mode (array if more than one)
146  * @mode_count - number of '.mode' entries
147  * @min_uV*    - minimum voltage (micro Volts)
148  * @max_uV*    - maximum voltage (micro Volts)
149  * @min_uA*    - minimum amperage (micro Amps)
150  * @max_uA*    - maximum amperage (micro Amps)
151  * @always_on* - bool type, true or false
152  * @boot_on*   - bool type, true or false
153  * TODO(sjg@chromium.org): Consider putting the above two into @flags
154  * @flags:     - flags value (see REGULATOR_FLAG_...)
155  * @name**     - fdt regulator name - should be taken from the device tree
156  * ctrl_reg:   - Control register offset used to enable/disable regulator
157  * volt_reg:   - register offset for writing voltage vsel values
158  *
159  * Note:
160  * *  - set automatically on device probe by the uclass's '.pre_probe' method.
161  * ** - set automatically on device bind by the uclass's '.post_bind' method.
162  * The constraints: type, mode, mode_count, can be set by device driver, e.g.
163  * by the driver '.probe' method.
164  */
165 struct dm_regulator_uclass_platdata {
166 	enum regulator_type type;
167 	struct dm_regulator_mode *mode;
168 	int mode_count;
169 	int min_uV;
170 	int max_uV;
171 	int min_uA;
172 	int max_uA;
173 	bool always_on;
174 	bool boot_on;
175 	const char *name;
176 	int flags;
177 	u8 ctrl_reg;
178 	u8 volt_reg;
179 	bool suspend_on;
180 	u32 suspend_uV;
181 };
182 
183 /* Regulator device operations */
184 struct dm_regulator_ops {
185 	/**
186 	 * The regulator output value function calls operates on a micro Volts.
187 	 *
188 	 * get/set_value - get/set output value of the given output number
189 	 * @dev          - regulator device
190 	 * Sets:
191 	 * @uV           - set the output value [micro Volts]
192 	 * @return output value [uV] on success or negative errno if fail.
193 	 */
194 	int (*get_value)(struct udevice *dev);
195 	int (*set_value)(struct udevice *dev, int uV);
196 	int (*set_suspend_value)(struct udevice *dev, int uV);
197 
198 	/**
199 	 * The regulator output current function calls operates on a micro Amps.
200 	 *
201 	 * get/set_current - get/set output current of the given output number
202 	 * @dev            - regulator device
203 	 * Sets:
204 	 * @uA           - set the output current [micro Amps]
205 	 * @return output value [uA] on success or negative errno if fail.
206 	 */
207 	int (*get_current)(struct udevice *dev);
208 	int (*set_current)(struct udevice *dev, int uA);
209 
210 	/**
211 	 * The most basic feature of the regulator output is its enable state.
212 	 *
213 	 * get/set_enable - get/set enable state of the given output number
214 	 * @dev           - regulator device
215 	 * Sets:
216 	 * @enable         - set true - enable or false - disable
217 	 * @return true/false for get or -errno if fail; 0 / -errno for set.
218 	 */
219 	int (*get_enable)(struct udevice *dev);
220 	int (*set_enable)(struct udevice *dev, bool enable);
221 	int (*set_suspend_enable)(struct udevice *dev, bool enable);
222 
223 	/**
224 	 * The 'get/set_mode()' function calls should operate on a driver-
225 	 * specific mode id definitions, which should be found in:
226 	 * field 'id' of struct dm_regulator_mode.
227 	 *
228 	 * get/set_mode - get/set operation mode of the given output number
229 	 * @dev         - regulator device
230 	 * Sets
231 	 * @mode_id     - set output mode id (struct dm_regulator_mode->id)
232 	 * @return id/0 for get/set on success or negative errno if fail.
233 	 * Note:
234 	 * The field 'id' of struct type 'dm_regulator_mode', should be always
235 	 * a positive number, since the negative is reserved for the error.
236 	 */
237 	int (*get_mode)(struct udevice *dev);
238 	int (*set_mode)(struct udevice *dev, int mode_id);
239 };
240 
241 /**
242  * regulator_mode: returns a pointer to the array of regulator mode info
243  *
244  * @dev        - pointer to the regulator device
245  * @modep      - pointer to the returned mode info array
246  * @return     - count of modep entries on success or negative errno if fail.
247  */
248 int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep);
249 
250 /**
251  * regulator_get_value: get microvoltage voltage value of a given regulator
252  *
253  * @dev    - pointer to the regulator device
254  * @return - positive output value [uV] on success or negative errno if fail.
255  */
256 int regulator_get_value(struct udevice *dev);
257 
258 /**
259  * regulator_set_value: set the microvoltage value of a given regulator.
260  *
261  * @dev    - pointer to the regulator device
262  * @uV     - the output value to set [micro Volts]
263  * @return - 0 on success or -errno val if fails
264  */
265 int regulator_set_value(struct udevice *dev, int uV);
266 
267 /**
268  * regulator_set_suspend_value: set the suspend microvoltage value of a given regulator.
269  *
270  * @dev    - pointer to the regulator device
271  * @uV     - the output suspend value to set [micro Volts]
272  * @return - 0 on success or -errno val if fails
273  */
274 int regulator_set_suspend_value(struct udevice *dev, int uV);
275 
276 /**
277  * regulator_set_value_force: set the microvoltage value of a given regulator
278  *			      without any min-,max condition check
279  *
280  * @dev    - pointer to the regulator device
281  * @uV     - the output value to set [micro Volts]
282  * @return - 0 on success or -errno val if fails
283  */
284 int regulator_set_value_force(struct udevice *dev, int uV);
285 
286 /**
287  * regulator_get_current: get microampere value of a given regulator
288  *
289  * @dev    - pointer to the regulator device
290  * @return - positive output current [uA] on success or negative errno if fail.
291  */
292 int regulator_get_current(struct udevice *dev);
293 
294 /**
295  * regulator_set_current: set the microampere value of a given regulator.
296  *
297  * @dev    - pointer to the regulator device
298  * @uA     - set the output current [micro Amps]
299  * @return - 0 on success or -errno val if fails
300  */
301 int regulator_set_current(struct udevice *dev, int uA);
302 
303 /**
304  * regulator_get_enable: get regulator device enable state.
305  *
306  * @dev    - pointer to the regulator device
307  * @return - true/false of enable state or -errno val if fails
308  */
309 int regulator_get_enable(struct udevice *dev);
310 
311 /**
312  * regulator_set_enable: set regulator enable state
313  *
314  * @dev    - pointer to the regulator device
315  * @enable - set true or false
316  * @return - 0 on success or -errno val if fails
317  */
318 int regulator_set_enable(struct udevice *dev, bool enable);
319 
320 /**
321  * regulator_set_suspend_enable: set regulator suspend enable state
322  *
323  * @dev    - pointer to the regulator device
324  * @enable - set true or false
325  * @return - 0 on success or -errno val if fails
326  */
327 int regulator_set_suspend_enable(struct udevice *dev, bool enable);
328 
329 /**
330  * regulator_get_mode: get active operation mode id of a given regulator
331  *
332  * @dev    - pointer to the regulator device
333  * @return - positive mode 'id' number on success or -errno val if fails
334  * Note:
335  * The device can provide an array of operating modes, which is type of struct
336  * dm_regulator_mode. Each mode has it's own 'id', which should be unique inside
337  * that array. By calling this function, the driver should return an active mode
338  * id of the given regulator device.
339  */
340 int regulator_get_mode(struct udevice *dev);
341 
342 /**
343  * regulator_set_mode: set the given regulator's, active mode id
344  *
345  * @dev     - pointer to the regulator device
346  * @mode_id - mode id to set ('id' field of struct type dm_regulator_mode)
347  * @return  - 0 on success or -errno value if fails
348  * Note:
349  * The device can provide an array of operating modes, which is type of struct
350  * dm_regulator_mode. Each mode has it's own 'id', which should be unique inside
351  * that array. By calling this function, the driver should set the active mode
352  * of a given regulator to given by "mode_id" argument.
353  */
354 int regulator_set_mode(struct udevice *dev, int mode_id);
355 
356 /**
357  * regulators_enable_boot_on() - enable regulators needed for boot
358  *
359  * This enables all regulators which are marked to be on at boot time. This
360  * only works for regulators which don't have a range for voltage/current,
361  * since in that case it is not possible to know which value to use.
362  *
363  * This effectively calls regulator_autoset() for every regulator.
364  */
365 int regulators_enable_boot_on(bool verbose);
366 
367 /**
368  * regulator_autoset: setup the voltage/current on a regulator
369  *
370  * The setup depends on constraints found in device's uclass's platform data
371  * (struct dm_regulator_uclass_platdata):
372  *
373  * - Enable - will set - if any of: 'always_on' or 'boot_on' is set to true,
374  *   or if both are unset, then the function returns
375  * - Voltage value - will set - if '.min_uV' and '.max_uV' values are equal
376  * - Current limit - will set - if '.min_uA' and '.max_uA' values are equal
377  *
378  * The function returns on the first-encountered error.
379  *
380  * @platname - expected string for dm_regulator_uclass_platdata .name field
381  * @devp     - returned pointer to the regulator device - if non-NULL passed
382  * @return: 0 on success or negative value of errno.
383  */
384 int regulator_autoset(struct udevice *dev);
385 
386 /**
387  * regulator_autoset_by_name: setup the regulator given by its uclass's
388  * platform data name field. The setup depends on constraints found in device's
389  * uclass's platform data (struct dm_regulator_uclass_platdata):
390  * - Enable - will set - if any of: 'always_on' or 'boot_on' is set to true,
391  *   or if both are unset, then the function returns
392  * - Voltage value - will set - if '.min_uV' and '.max_uV' values are equal
393  * - Current limit - will set - if '.min_uA' and '.max_uA' values are equal
394  *
395  * The function returns on first encountered error.
396  *
397  * @platname - expected string for dm_regulator_uclass_platdata .name field
398  * @devp     - returned pointer to the regulator device - if non-NULL passed
399  * @return: 0 on success or negative value of errno.
400  *
401  * The returned 'regulator' device can be used with:
402  * - regulator_get/set_*
403  */
404 int regulator_autoset_by_name(const char *platname, struct udevice **devp);
405 
406 /**
407  * regulator_list_autoset: setup the regulators given by list of their uclass's
408  * platform data name field. The setup depends on constraints found in device's
409  * uclass's platform data. The function loops with calls to:
410  * regulator_autoset_by_name() for each name from the list.
411  *
412  * @list_platname - an array of expected strings for .name field of each
413  *                  regulator's uclass platdata
414  * @list_devp     - an array of returned pointers to the successfully setup
415  *                  regulator devices if non-NULL passed
416  * @verbose       - (true/false) print each regulator setup info, or be quiet
417  * @return 0 on successfully setup of all list entries, otherwise first error.
418  *
419  * The returned 'regulator' devices can be used with:
420  * - regulator_get/set_*
421  *
422  * Note: The list must ends with NULL entry, like in the "platname" list below:
423  * char *my_regulators[] = {
424  *     "VCC_3.3V",
425  *     "VCC_1.8V",
426  *     NULL,
427  * };
428  */
429 int regulator_list_autoset(const char *list_platname[],
430 			   struct udevice *list_devp[],
431 			   bool verbose);
432 
433 /**
434  * regulator_get_by_devname: returns the pointer to the pmic regulator device.
435  * Search by name, found in regulator device's name.
436  *
437  * @devname - expected string for 'dev->name' of regulator device
438  * @devp    - returned pointer to the regulator device
439  * @return 0 on success or negative value of errno.
440  *
441  * The returned 'regulator' device is probed and can be used with:
442  * - regulator_get/set_*
443  */
444 int regulator_get_by_devname(const char *devname, struct udevice **devp);
445 
446 /**
447  * regulator_get_by_platname: returns the pointer to the pmic regulator device.
448  * Search by name, found in regulator uclass platdata.
449  *
450  * @platname - expected string for uc_pdata->name of regulator uclass platdata
451  * @devp     - returns pointer to the regulator device or NULL on error
452  * @return 0 on success or negative value of errno.
453  *
454  * The returned 'regulator' device is probed and can be used with:
455  * - regulator_get/set_*
456  */
457 int regulator_get_by_platname(const char *platname, struct udevice **devp);
458 
459 /**
460  * device_get_supply_regulator: returns the pointer to the supply regulator.
461  * Search by phandle, found in device's node.
462  *
463  * Note: Please pay attention to proper order of device bind sequence.
464  * The regulator device searched by the phandle, must be binded before
465  * this function call.
466  *
467  * @dev         - device with supply phandle
468  * @supply_name - phandle name of regulator
469  * @devp        - returned pointer to the supply device
470  * @return 0 on success or negative value of errno.
471  */
472 int device_get_supply_regulator(struct udevice *dev, const char *supply_name,
473 				struct udevice **devp);
474 
475 #endif /* _INCLUDE_REGULATOR_H_ */
476