1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (C) 2014-2015 Samsung Electronics
7 #ifndef _INCLUDE_REGULATOR_H_
8 #define _INCLUDE_REGULATOR_H_
13 * U-Boot Voltage/Current Regulator
14 * ================================
16 * The regulator API is based on a driver model, with the device tree support.
17 * And this header describes the functions and data types for the uclass id:
18 * 'UCLASS_REGULATOR' and the regulator driver API.
20 * The regulator uclass - is based on uclass platform data which is allocated,
21 * automatically for each regulator device on bind and 'dev->uclass_plat'
22 * points to it. The data type is: 'struct dm_regulator_uclass_plat'.
23 * The uclass file: 'drivers/power/regulator/regulator-uclass.c'
25 * The regulator device - is based on driver's model 'struct udevice'.
26 * The API can use regulator name in two meanings:
27 * - devname - the regulator device's name: 'dev->name'
28 * - platname - the device's plat's name. So in the code it looks like:
29 * 'uc_pdata = dev->uclass_plat'; 'name = uc_pdata->name'.
31 * The regulator device driver - provide an implementation of uclass operations
32 * pointed by 'dev->driver->ops' as a struct of type 'struct dm_regulator_ops'.
34 * To proper bind the regulator device, the device tree node should provide
35 * regulator constraints, like in the example below:
38 * regulator-name = "VDD_MMC_1.8V"; (must be unique for proper bind)
39 * regulator-min-microvolt = <1000000>; (optional)
40 * regulator-max-microvolt = <1000000>; (optional)
41 * regulator-min-microamp = <1000>; (optional)
42 * regulator-max-microamp = <1000>; (optional)
43 * regulator-always-on; (optional)
44 * regulator-boot-on; (optional)
47 * Note: For the proper operation, at least name constraint is needed, since
48 * it can be used when calling regulator_get_by_platname(). And the mandatory
49 * rule for this name is, that it must be globally unique for the single dts.
50 * If regulator-name property is not provided, node name will be chosen.
53 * For each regulator device, the device_bind() should be called with passed
54 * device tree offset. This is required for this uclass's '.post_bind' method,
55 * which does the scan on the device node, for the 'regulator-name' constraint.
56 * If the parent is not a PMIC device, and the child is not bind by function:
57 * 'pmic_bind_childs()', then it's recommended to bind the device by call to
58 * dm_scan_fdt_dev() - this is usually done automatically for bus devices,
59 * as a post bind method.
62 * Having the device's name constraint, we can call regulator_by_platname(),
63 * to find the required regulator. Before return, the regulator is probed,
64 * and the rest of its constraints are put into the device's uclass platform
65 * data, by the uclass regulator '.pre_probe' method.
67 * For more info about PMIC bind, please refer to file: 'include/power/pmic.h'
70 * Please do not use the device_bind_by_name() function, since it pass '-1' as
71 * device node offset - and the bind will fail on uclass .post_bind method,
72 * because of missing 'regulator-name' constraint.
75 * Fixed Voltage/Current Regulator
76 * ===============================
78 * When fixed voltage regulator is needed, then enable the config:
79 * - CONFIG_DM_REGULATOR_FIXED
81 * The driver file: 'drivers/power/regulator/fixed.c', provides basic support
82 * for control the GPIO, and return the device tree constraint values.
84 * To bind the fixed voltage regulator device, we usually use a 'simple-bus'
85 * node as a parent. And 'regulator-fixed' for the driver compatible. This is
86 * the same as in the kernel. The example node of fixed regulator:
89 * compatible = "simple-bus";
90 * #address-cells = <1>;
94 * compatible = "regulator-fixed";
95 * regulator-name = "VDD_LED_3.3V";
96 * regulator-min-microvolt = <3300000>;
97 * regulator-max-microvolt = <3300000>;
98 * gpio = <&gpc1 0 GPIO_ACTIVE_LOW>;
102 * The fixed regulator devices also provide regulator uclass platform data. And
103 * devices bound from such node, can use the regulator drivers API.
106 /* enum regulator_type - used for regulator_*() variant calls */
107 enum regulator_type {
108 REGULATOR_TYPE_LDO = 0,
111 REGULATOR_TYPE_FIXED,
113 REGULATOR_TYPE_OTHER,
117 * struct dm_regulator_mode - this structure holds an information about
118 * each regulator operation mode. Probably in most cases - an array.
119 * This will be probably a driver-static data, since it is device-specific.
121 * @id - a driver-specific mode id
122 * @register_value - a driver-specific value for its mode id
123 * @name - the name of mode - used for regulator command
125 * The field 'id', should be always a positive number, since the negative values
126 * are reserved for the errno numbers when returns the mode id.
128 struct dm_regulator_mode {
129 int id; /* Set only as >= 0 (negative value is reserved for errno) */
134 enum regulator_flag {
135 REGULATOR_FLAG_AUTOSET_UV = 1 << 0,
136 REGULATOR_FLAG_AUTOSET_UA = 1 << 1,
140 * struct dm_regulator_uclass_plat - pointed by dev->uclass_plat, and
141 * allocated on each regulator bind. This structure holds an information
142 * about each regulator's constraints and supported operation modes.
143 * There is no "step" voltage value - so driver should take care of this.
145 * @type - one of 'enum regulator_type'
146 * @mode - pointer to the regulator mode (array if more than one)
147 * @mode_count - number of '.mode' entries
148 * @min_uV* - minimum voltage (micro Volts)
149 * @max_uV* - maximum voltage (micro Volts)
150 * @min_uA* - minimum amperage (micro Amps)
151 * @max_uA* - maximum amperage (micro Amps)
152 * @always_on* - bool type, true or false
153 * @boot_on* - bool type, true or false
155 * @ramp_delay - Time to settle down after voltage change (unit: uV/us)
156 * @flags: - flags value (see REGULATOR_FLAG_...)
157 * @name** - fdt regulator name - should be taken from the device tree
158 * ctrl_reg: - Control register offset used to enable/disable regulator
159 * volt_reg: - register offset for writing voltage vsel values
162 * * - set automatically on device probe by the uclass's '.pre_probe' method.
163 * ** - set automatically on device bind by the uclass's '.post_bind' method.
164 * The constraints: type, mode, mode_count, can be set by device driver, e.g.
165 * by the driver '.probe' method.
167 struct dm_regulator_uclass_plat {
168 enum regulator_type type;
169 struct dm_regulator_mode *mode;
176 unsigned int ramp_delay;
187 /* Regulator device operations */
188 struct dm_regulator_ops {
190 * The regulator output value function calls operates on a micro Volts.
192 * get/set_value - get/set output value of the given output number
193 * @dev - regulator device
195 * @uV - set the output value [micro Volts]
196 * @return output value [uV] on success or negative errno if fail.
198 int (*get_value)(struct udevice *dev);
199 int (*set_value)(struct udevice *dev, int uV);
202 * The regulator suspend output value function calls operates
205 * get/set_suspen_value - get/set suspend mode output value
206 * @dev - regulator device
208 * @uV - set the suspend output value [micro Volts]
209 * @return output value [uV] on success or negative errno if fail.
211 int (*set_suspend_value)(struct udevice *dev, int uV);
212 int (*get_suspend_value)(struct udevice *dev);
215 * The regulator output current function calls operates on a micro Amps.
217 * get/set_current - get/set output current of the given output number
218 * @dev - regulator device
220 * @uA - set the output current [micro Amps]
221 * @return output value [uA] on success or negative errno if fail.
223 int (*get_current)(struct udevice *dev);
224 int (*set_current)(struct udevice *dev, int uA);
227 * The most basic feature of the regulator output is its enable state.
229 * get/set_enable - get/set enable state of the given output number
230 * @dev - regulator device
232 * @enable - set true - enable or false - disable
233 * @return true/false for get or -errno if fail; 0 / -errno for set.
235 int (*get_enable)(struct udevice *dev);
236 int (*set_enable)(struct udevice *dev, bool enable);
239 * The most basic feature of the regulator output is its enable state
242 * get/set_suspend_enable - get/set enable state of the suspend output
243 * @dev - regulator device
245 * @enable - set true - enable or false - disable
246 * @return true/false for get or -errno if fail; 0 / -errno for set.
248 int (*set_suspend_enable)(struct udevice *dev, bool enable);
249 int (*get_suspend_enable)(struct udevice *dev);
252 * The 'get/set_mode()' function calls should operate on a driver-
253 * specific mode id definitions, which should be found in:
254 * field 'id' of struct dm_regulator_mode.
256 * get/set_mode - get/set operation mode of the given output number
257 * @dev - regulator device
259 * @mode_id - set output mode id (struct dm_regulator_mode->id)
260 * @return id/0 for get/set on success or negative errno if fail.
262 * The field 'id' of struct type 'dm_regulator_mode', should be always
263 * a positive number, since the negative is reserved for the error.
265 int (*get_mode)(struct udevice *dev);
266 int (*set_mode)(struct udevice *dev, int mode_id);
269 #if CONFIG_IS_ENABLED(DM_REGULATOR)
271 * regulator_mode: returns a pointer to the array of regulator mode info
273 * @dev - pointer to the regulator device
274 * @modep - pointer to the returned mode info array
275 * @return - count of modep entries on success or negative errno if fail.
277 int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep);
280 * regulator_get_value: get microvoltage voltage value of a given regulator
282 * @dev - pointer to the regulator device
283 * @return - positive output value [uV] on success or negative errno if fail.
285 int regulator_get_value(struct udevice *dev);
288 * regulator_set_value: set the microvoltage value of a given regulator.
290 * @dev - pointer to the regulator device
291 * @uV - the output value to set [micro Volts]
292 * @return - 0 on success or -errno val if fails
294 int regulator_set_value(struct udevice *dev, int uV);
297 * regulator_set_suspend_value: set the suspend microvoltage value of a given regulator.
299 * @dev - pointer to the regulator device
300 * @uV - the output suspend value to set [micro Volts]
301 * @return - 0 on success or -errno val if fails
303 int regulator_set_suspend_value(struct udevice *dev, int uV);
306 * regulator_get_suspend_value: get the suspend microvoltage value of a given regulator.
308 * @dev - pointer to the regulator device
309 * @return - positive output value [uV] on success or negative errno if fail.
311 int regulator_get_suspend_value(struct udevice *dev);
314 * regulator_set_value_force: set the microvoltage value of a given regulator
315 * without any min-,max condition check
317 * @dev - pointer to the regulator device
318 * @uV - the output value to set [micro Volts]
319 * @return - 0 on success or -errno val if fails
321 int regulator_set_value_force(struct udevice *dev, int uV);
324 * regulator_get_current: get microampere value of a given regulator
326 * @dev - pointer to the regulator device
327 * @return - positive output current [uA] on success or negative errno if fail.
329 int regulator_get_current(struct udevice *dev);
332 * regulator_set_current: set the microampere value of a given regulator.
334 * @dev - pointer to the regulator device
335 * @uA - set the output current [micro Amps]
336 * @return - 0 on success or -errno val if fails
338 int regulator_set_current(struct udevice *dev, int uA);
341 * regulator_get_enable: get regulator device enable state.
343 * @dev - pointer to the regulator device
344 * @return - true/false of enable state or -errno val if fails
346 int regulator_get_enable(struct udevice *dev);
349 * regulator_set_enable: set regulator enable state
351 * @dev - pointer to the regulator device
352 * @enable - set true or false
353 * @return - 0 on success or -errno val if fails
355 int regulator_set_enable(struct udevice *dev, bool enable);
358 * regulator_set_enable_if_allowed: set regulator enable state if allowed by
361 * @dev - pointer to the regulator device
362 * @enable - set true or false
363 * @return - 0 on success or if enabling is not supported
364 * -errno val if fails.
366 int regulator_set_enable_if_allowed(struct udevice *dev, bool enable);
369 * regulator_set_suspend_enable: set regulator suspend enable state
371 * @dev - pointer to the regulator device
372 * @enable - set true or false
373 * @return - 0 on success or -errno val if fails
375 int regulator_set_suspend_enable(struct udevice *dev, bool enable);
378 * regulator_get_suspend_enable: get regulator suspend enable state
380 * @dev - pointer to the regulator device
381 * @return - true/false of enable state or -errno val if fails
383 int regulator_get_suspend_enable(struct udevice *dev);
386 * regulator_get_mode: get active operation mode id of a given regulator
388 * @dev - pointer to the regulator device
389 * @return - positive mode 'id' number on success or -errno val if fails
391 * The device can provide an array of operating modes, which is type of struct
392 * dm_regulator_mode. Each mode has it's own 'id', which should be unique inside
393 * that array. By calling this function, the driver should return an active mode
394 * id of the given regulator device.
396 int regulator_get_mode(struct udevice *dev);
399 * regulator_set_mode: set the given regulator's, active mode id
401 * @dev - pointer to the regulator device
402 * @mode_id - mode id to set ('id' field of struct type dm_regulator_mode)
403 * @return - 0 on success or -errno value if fails
405 * The device can provide an array of operating modes, which is type of struct
406 * dm_regulator_mode. Each mode has it's own 'id', which should be unique inside
407 * that array. By calling this function, the driver should set the active mode
408 * of a given regulator to given by "mode_id" argument.
410 int regulator_set_mode(struct udevice *dev, int mode_id);
413 * regulators_enable_boot_on() - enable regulators needed for boot
415 * This enables all regulators which are marked to be on at boot time. This
416 * only works for regulators which don't have a range for voltage/current,
417 * since in that case it is not possible to know which value to use.
419 * This effectively calls regulator_autoset() for every regulator.
421 int regulators_enable_boot_on(bool verbose);
424 * regulator_autoset: setup the voltage/current on a regulator
426 * The setup depends on constraints found in device's uclass's platform data
427 * (struct dm_regulator_uclass_plat):
429 * - Enable - will set - if any of: 'always_on' or 'boot_on' is set to true,
430 * or if both are unset, then the function returns
431 * - Voltage value - will set - if '.min_uV' and '.max_uV' values are equal
432 * - Current limit - will set - if '.min_uA' and '.max_uA' values are equal
434 * The function returns on the first-encountered error.
436 * @platname - expected string for dm_regulator_uclass_plat .name field
437 * @devp - returned pointer to the regulator device - if non-NULL passed
438 * @return: 0 on success or negative value of errno.
440 int regulator_autoset(struct udevice *dev);
443 * regulator_autoset_by_name: setup the regulator given by its uclass's
444 * platform data name field. The setup depends on constraints found in device's
445 * uclass's platform data (struct dm_regulator_uclass_plat):
446 * - Enable - will set - if any of: 'always_on' or 'boot_on' is set to true,
447 * or if both are unset, then the function returns
448 * - Voltage value - will set - if '.min_uV' and '.max_uV' values are equal
449 * - Current limit - will set - if '.min_uA' and '.max_uA' values are equal
451 * The function returns on first encountered error.
453 * @platname - expected string for dm_regulator_uclass_plat .name field
454 * @devp - returned pointer to the regulator device - if non-NULL passed
455 * @return: 0 on success or negative value of errno.
457 * The returned 'regulator' device can be used with:
458 * - regulator_get/set_*
460 int regulator_autoset_by_name(const char *platname, struct udevice **devp);
463 * regulator_list_autoset: setup the regulators given by list of their uclass's
464 * platform data name field. The setup depends on constraints found in device's
465 * uclass's platform data. The function loops with calls to:
466 * regulator_autoset_by_name() for each name from the list.
468 * @list_platname - an array of expected strings for .name field of each
469 * regulator's uclass plat
470 * @list_devp - an array of returned pointers to the successfully setup
471 * regulator devices if non-NULL passed
472 * @verbose - (true/false) print each regulator setup info, or be quiet
473 * @return 0 on successfully setup of all list entries, otherwise first error.
475 * The returned 'regulator' devices can be used with:
476 * - regulator_get/set_*
478 * Note: The list must ends with NULL entry, like in the "platname" list below:
479 * char *my_regulators[] = {
485 int regulator_list_autoset(const char *list_platname[],
486 struct udevice *list_devp[],
490 * regulator_get_by_devname: returns the pointer to the pmic regulator device.
491 * Search by name, found in regulator device's name.
493 * @devname - expected string for 'dev->name' of regulator device
494 * @devp - returned pointer to the regulator device
495 * @return 0 on success or negative value of errno.
497 * The returned 'regulator' device is probed and can be used with:
498 * - regulator_get/set_*
500 int regulator_get_by_devname(const char *devname, struct udevice **devp);
503 * regulator_get_by_platname: returns the pointer to the pmic regulator device.
504 * Search by name, found in regulator uclass plat.
506 * @platname - expected string for uc_pdata->name of regulator uclass plat
507 * @devp - returns pointer to the regulator device or NULL on error
508 * @return 0 on success or negative value of errno.
510 * The returned 'regulator' device is probed and can be used with:
511 * - regulator_get/set_*
513 int regulator_get_by_platname(const char *platname, struct udevice **devp);
516 * device_get_supply_regulator: returns the pointer to the supply regulator.
517 * Search by phandle, found in device's node.
519 * Note: Please pay attention to proper order of device bind sequence.
520 * The regulator device searched by the phandle, must be binded before
521 * this function call.
523 * @dev - device with supply phandle
524 * @supply_name - phandle name of regulator
525 * @devp - returned pointer to the supply device
526 * @return 0 on success or negative value of errno.
528 int device_get_supply_regulator(struct udevice *dev, const char *supply_name,
529 struct udevice **devp);
531 static inline int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep)
536 static inline int regulator_get_value(struct udevice *dev)
541 static inline int regulator_set_value(struct udevice *dev, int uV)
546 static inline int regulator_set_suspend_value(struct udevice *dev, int uV)
551 static inline int regulator_get_suspend_value(struct udevice *dev)
556 static inline int regulator_set_value_force(struct udevice *dev, int uV)
561 static inline int regulator_get_current(struct udevice *dev)
566 static inline int regulator_set_current(struct udevice *dev, int uA)
571 static inline int regulator_get_enable(struct udevice *dev)
576 static inline int regulator_set_enable(struct udevice *dev, bool enable)
581 static inline int regulator_set_enable_if_allowed(struct udevice *dev, bool enable)
586 static inline int regulator_set_suspend_enable(struct udevice *dev, bool enable)
591 static inline int regulator_get_suspend_enable(struct udevice *dev)
596 static inline int regulator_get_mode(struct udevice *dev)
601 static inline int regulator_set_mode(struct udevice *dev, int mode_id)
606 static inline int regulators_enable_boot_on(bool verbose)
611 static inline int regulator_autoset(struct udevice *dev)
616 static inline int regulator_autoset_by_name(const char *platname, struct udevice **devp)
621 static inline int regulator_list_autoset(const char *list_platname[], struct udevice *list_devp[],
627 static inline int regulator_get_by_devname(const char *devname, struct udevice **devp)
632 static inline int regulator_get_by_platname(const char *platname, struct udevice **devp)
637 static inline int device_get_supply_regulator(struct udevice *dev, const char *supply_name,
638 struct udevice **devp)
644 #endif /* _INCLUDE_REGULATOR_H_ */