1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (C) 2014-2015 Samsung Electronics
4 * Przemyslaw Marczak <p.marczak@samsung.com>
7 #ifndef _INCLUDE_REGULATOR_H_
8 #define _INCLUDE_REGULATOR_H_
11 * U-Boot Voltage/Current Regulator
12 * ================================
14 * The regulator API is based on a driver model, with the device tree support.
15 * And this header describes the functions and data types for the uclass id:
16 * 'UCLASS_REGULATOR' and the regulator driver API.
18 * The regulator uclass - is based on uclass platform data which is allocated,
19 * automatically for each regulator device on bind and 'dev->uclass_platdata'
20 * points to it. The data type is: 'struct dm_regulator_uclass_platdata'.
21 * The uclass file: 'drivers/power/regulator/regulator-uclass.c'
23 * The regulator device - is based on driver's model 'struct udevice'.
24 * The API can use regulator name in two meanings:
25 * - devname - the regulator device's name: 'dev->name'
26 * - platname - the device's platdata's name. So in the code it looks like:
27 * 'uc_pdata = dev->uclass_platdata'; 'name = uc_pdata->name'.
29 * The regulator device driver - provide an implementation of uclass operations
30 * pointed by 'dev->driver->ops' as a struct of type 'struct dm_regulator_ops'.
32 * To proper bind the regulator device, the device tree node should provide
33 * regulator constraints, like in the example below:
36 * regulator-name = "VDD_MMC_1.8V"; (must be unique for proper bind)
37 * regulator-min-microvolt = <1000000>; (optional)
38 * regulator-max-microvolt = <1000000>; (optional)
39 * regulator-min-microamp = <1000>; (optional)
40 * regulator-max-microamp = <1000>; (optional)
41 * regulator-always-on; (optional)
42 * regulator-boot-on; (optional)
45 * Note: For the proper operation, at least name constraint is needed, since
46 * it can be used when calling regulator_get_by_platname(). And the mandatory
47 * rule for this name is, that it must be globally unique for the single dts.
48 * If regulator-name property is not provided, node name will be chosen.
51 * For each regulator device, the device_bind() should be called with passed
52 * device tree offset. This is required for this uclass's '.post_bind' method,
53 * which does the scan on the device node, for the 'regulator-name' constraint.
54 * If the parent is not a PMIC device, and the child is not bind by function:
55 * 'pmic_bind_childs()', then it's recommended to bind the device by call to
56 * dm_scan_fdt_dev() - this is usually done automatically for bus devices,
57 * as a post bind method.
60 * Having the device's name constraint, we can call regulator_by_platname(),
61 * to find the required regulator. Before return, the regulator is probed,
62 * and the rest of its constraints are put into the device's uclass platform
63 * data, by the uclass regulator '.pre_probe' method.
65 * For more info about PMIC bind, please refer to file: 'include/power/pmic.h'
68 * Please do not use the device_bind_by_name() function, since it pass '-1' as
69 * device node offset - and the bind will fail on uclass .post_bind method,
70 * because of missing 'regulator-name' constraint.
73 * Fixed Voltage/Current Regulator
74 * ===============================
76 * When fixed voltage regulator is needed, then enable the config:
77 * - CONFIG_DM_REGULATOR_FIXED
79 * The driver file: 'drivers/power/regulator/fixed.c', provides basic support
80 * for control the GPIO, and return the device tree constraint values.
82 * To bind the fixed voltage regulator device, we usually use a 'simple-bus'
83 * node as a parent. And 'regulator-fixed' for the driver compatible. This is
84 * the same as in the kernel. The example node of fixed regulator:
87 * compatible = "simple-bus";
88 * #address-cells = <1>;
92 * compatible = "regulator-fixed";
93 * regulator-name = "VDD_LED_3.3V";
94 * regulator-min-microvolt = <3300000>;
95 * regulator-max-microvolt = <3300000>;
96 * gpio = <&gpc1 0 GPIO_ACTIVE_LOW>;
100 * The fixed regulator devices also provide regulator uclass platform data. And
101 * devices bound from such node, can use the regulator drivers API.
104 /* enum regulator_type - used for regulator_*() variant calls */
105 enum regulator_type {
106 REGULATOR_TYPE_LDO = 0,
109 REGULATOR_TYPE_FIXED,
111 REGULATOR_TYPE_OTHER,
115 * struct dm_regulator_mode - this structure holds an information about
116 * each regulator operation mode. Probably in most cases - an array.
117 * This will be probably a driver-static data, since it is device-specific.
119 * @id - a driver-specific mode id
120 * @register_value - a driver-specific value for its mode id
121 * @name - the name of mode - used for regulator command
123 * The field 'id', should be always a positive number, since the negative values
124 * are reserved for the errno numbers when returns the mode id.
126 struct dm_regulator_mode {
127 int id; /* Set only as >= 0 (negative value is reserved for errno) */
132 enum regulator_flag {
133 REGULATOR_FLAG_AUTOSET_UV = 1 << 0,
134 REGULATOR_FLAG_AUTOSET_UA = 1 << 1,
138 * struct dm_regulator_uclass_platdata - pointed by dev->uclass_platdata, and
139 * allocated on each regulator bind. This structure holds an information
140 * about each regulator's constraints and supported operation modes.
141 * There is no "step" voltage value - so driver should take care of this.
143 * @type - one of 'enum regulator_type'
144 * @mode - pointer to the regulator mode (array if more than one)
145 * @mode_count - number of '.mode' entries
146 * @min_uV* - minimum voltage (micro Volts)
147 * @max_uV* - maximum voltage (micro Volts)
148 * @min_uA* - minimum amperage (micro Amps)
149 * @max_uA* - maximum amperage (micro Amps)
150 * @always_on* - bool type, true or false
151 * @boot_on* - bool type, true or false
152 * TODO(sjg@chromium.org): Consider putting the above two into @flags
153 * @ramp_delay - Time to settle down after voltage change (unit: uV/us)
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
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.
165 struct dm_regulator_uclass_platdata {
166 enum regulator_type type;
167 struct dm_regulator_mode *mode;
174 unsigned int ramp_delay;
185 /* Regulator device operations */
186 struct dm_regulator_ops {
188 * The regulator output value function calls operates on a micro Volts.
190 * get/set_value - get/set output value of the given output number
191 * @dev - regulator device
193 * @uV - set the output value [micro Volts]
194 * @return output value [uV] on success or negative errno if fail.
196 int (*get_value)(struct udevice *dev);
197 int (*set_value)(struct udevice *dev, int uV);
200 * The regulator suspend output value function calls operates
203 * get/set_suspen_value - get/set suspend mode output value
204 * @dev - regulator device
206 * @uV - set the suspend output value [micro Volts]
207 * @return output value [uV] on success or negative errno if fail.
209 int (*set_suspend_value)(struct udevice *dev, int uV);
210 int (*get_suspend_value)(struct udevice *dev);
213 * The regulator output current function calls operates on a micro Amps.
215 * get/set_current - get/set output current of the given output number
216 * @dev - regulator device
218 * @uA - set the output current [micro Amps]
219 * @return output value [uA] on success or negative errno if fail.
221 int (*get_current)(struct udevice *dev);
222 int (*set_current)(struct udevice *dev, int uA);
225 * The most basic feature of the regulator output is its enable state.
227 * get/set_enable - get/set enable state of the given output number
228 * @dev - regulator device
230 * @enable - set true - enable or false - disable
231 * @return true/false for get or -errno if fail; 0 / -errno for set.
233 int (*get_enable)(struct udevice *dev);
234 int (*set_enable)(struct udevice *dev, bool enable);
237 * The most basic feature of the regulator output is its enable state
240 * get/set_suspend_enable - get/set enable state of the suspend output
241 * @dev - regulator device
243 * @enable - set true - enable or false - disable
244 * @return true/false for get or -errno if fail; 0 / -errno for set.
246 int (*set_suspend_enable)(struct udevice *dev, bool enable);
247 int (*get_suspend_enable)(struct udevice *dev);
250 * The 'get/set_mode()' function calls should operate on a driver-
251 * specific mode id definitions, which should be found in:
252 * field 'id' of struct dm_regulator_mode.
254 * get/set_mode - get/set operation mode of the given output number
255 * @dev - regulator device
257 * @mode_id - set output mode id (struct dm_regulator_mode->id)
258 * @return id/0 for get/set on success or negative errno if fail.
260 * The field 'id' of struct type 'dm_regulator_mode', should be always
261 * a positive number, since the negative is reserved for the error.
263 int (*get_mode)(struct udevice *dev);
264 int (*set_mode)(struct udevice *dev, int mode_id);
268 * regulator_mode: returns a pointer to the array of regulator mode info
270 * @dev - pointer to the regulator device
271 * @modep - pointer to the returned mode info array
272 * @return - count of modep entries on success or negative errno if fail.
274 int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep);
277 * regulator_get_value: get microvoltage voltage value of a given regulator
279 * @dev - pointer to the regulator device
280 * @return - positive output value [uV] on success or negative errno if fail.
282 int regulator_get_value(struct udevice *dev);
285 * regulator_set_value: set the microvoltage value of a given regulator.
287 * @dev - pointer to the regulator device
288 * @uV - the output value to set [micro Volts]
289 * @return - 0 on success or -errno val if fails
291 int regulator_set_value(struct udevice *dev, int uV);
294 * regulator_set_suspend_value: set the suspend microvoltage value of a given regulator.
296 * @dev - pointer to the regulator device
297 * @uV - the output suspend value to set [micro Volts]
298 * @return - 0 on success or -errno val if fails
300 int regulator_set_suspend_value(struct udevice *dev, int uV);
303 * regulator_get_suspend_value: get the suspend microvoltage value of a given regulator.
305 * @dev - pointer to the regulator device
306 * @return - positive output value [uV] on success or negative errno if fail.
308 int regulator_get_suspend_value(struct udevice *dev);
311 * regulator_set_value_force: set the microvoltage value of a given regulator
312 * without any min-,max condition check
314 * @dev - pointer to the regulator device
315 * @uV - the output value to set [micro Volts]
316 * @return - 0 on success or -errno val if fails
318 int regulator_set_value_force(struct udevice *dev, int uV);
321 * regulator_get_current: get microampere value of a given regulator
323 * @dev - pointer to the regulator device
324 * @return - positive output current [uA] on success or negative errno if fail.
326 int regulator_get_current(struct udevice *dev);
329 * regulator_set_current: set the microampere value of a given regulator.
331 * @dev - pointer to the regulator device
332 * @uA - set the output current [micro Amps]
333 * @return - 0 on success or -errno val if fails
335 int regulator_set_current(struct udevice *dev, int uA);
338 * regulator_get_enable: get regulator device enable state.
340 * @dev - pointer to the regulator device
341 * @return - true/false of enable state or -errno val if fails
343 int regulator_get_enable(struct udevice *dev);
346 * regulator_set_enable: set regulator enable state
348 * @dev - pointer to the regulator device
349 * @enable - set true or false
350 * @return - 0 on success or -errno val if fails
352 int regulator_set_enable(struct udevice *dev, bool enable);
355 * regulator_set_enable_if_allowed: set regulator enable state if allowed by
358 * @dev - pointer to the regulator device
359 * @enable - set true or false
360 * @return - 0 on success or if enabling is not supported
361 * -errno val if fails.
363 int regulator_set_enable_if_allowed(struct udevice *dev, bool enable);
366 * regulator_set_suspend_enable: set regulator suspend enable state
368 * @dev - pointer to the regulator device
369 * @enable - set true or false
370 * @return - 0 on success or -errno val if fails
372 int regulator_set_suspend_enable(struct udevice *dev, bool enable);
375 * regulator_get_suspend_enable: get regulator suspend enable state
377 * @dev - pointer to the regulator device
378 * @return - true/false of enable state or -errno val if fails
380 int regulator_get_suspend_enable(struct udevice *dev);
383 * regulator_get_mode: get active operation mode id of a given regulator
385 * @dev - pointer to the regulator device
386 * @return - positive mode 'id' number on success or -errno val if fails
388 * The device can provide an array of operating modes, which is type of struct
389 * dm_regulator_mode. Each mode has it's own 'id', which should be unique inside
390 * that array. By calling this function, the driver should return an active mode
391 * id of the given regulator device.
393 int regulator_get_mode(struct udevice *dev);
396 * regulator_set_mode: set the given regulator's, active mode id
398 * @dev - pointer to the regulator device
399 * @mode_id - mode id to set ('id' field of struct type dm_regulator_mode)
400 * @return - 0 on success or -errno value if fails
402 * The device can provide an array of operating modes, which is type of struct
403 * dm_regulator_mode. Each mode has it's own 'id', which should be unique inside
404 * that array. By calling this function, the driver should set the active mode
405 * of a given regulator to given by "mode_id" argument.
407 int regulator_set_mode(struct udevice *dev, int mode_id);
410 * regulators_enable_boot_on() - enable regulators needed for boot
412 * This enables all regulators which are marked to be on at boot time. This
413 * only works for regulators which don't have a range for voltage/current,
414 * since in that case it is not possible to know which value to use.
416 * This effectively calls regulator_autoset() for every regulator.
418 int regulators_enable_boot_on(bool verbose);
421 * regulator_autoset: setup the voltage/current on a regulator
423 * The setup depends on constraints found in device's uclass's platform data
424 * (struct dm_regulator_uclass_platdata):
426 * - Enable - will set - if any of: 'always_on' or 'boot_on' is set to true,
427 * or if both are unset, then the function returns
428 * - Voltage value - will set - if '.min_uV' and '.max_uV' values are equal
429 * - Current limit - will set - if '.min_uA' and '.max_uA' values are equal
431 * The function returns on the first-encountered error.
433 * @platname - expected string for dm_regulator_uclass_platdata .name field
434 * @devp - returned pointer to the regulator device - if non-NULL passed
435 * @return: 0 on success or negative value of errno.
437 int regulator_autoset(struct udevice *dev);
440 * regulator_autoset_by_name: setup the regulator given by its uclass's
441 * platform data name field. The setup depends on constraints found in device's
442 * uclass's platform data (struct dm_regulator_uclass_platdata):
443 * - Enable - will set - if any of: 'always_on' or 'boot_on' is set to true,
444 * or if both are unset, then the function returns
445 * - Voltage value - will set - if '.min_uV' and '.max_uV' values are equal
446 * - Current limit - will set - if '.min_uA' and '.max_uA' values are equal
448 * The function returns on first encountered error.
450 * @platname - expected string for dm_regulator_uclass_platdata .name field
451 * @devp - returned pointer to the regulator device - if non-NULL passed
452 * @return: 0 on success or negative value of errno.
454 * The returned 'regulator' device can be used with:
455 * - regulator_get/set_*
457 int regulator_autoset_by_name(const char *platname, struct udevice **devp);
460 * regulator_list_autoset: setup the regulators given by list of their uclass's
461 * platform data name field. The setup depends on constraints found in device's
462 * uclass's platform data. The function loops with calls to:
463 * regulator_autoset_by_name() for each name from the list.
465 * @list_platname - an array of expected strings for .name field of each
466 * regulator's uclass platdata
467 * @list_devp - an array of returned pointers to the successfully setup
468 * regulator devices if non-NULL passed
469 * @verbose - (true/false) print each regulator setup info, or be quiet
470 * @return 0 on successfully setup of all list entries, otherwise first error.
472 * The returned 'regulator' devices can be used with:
473 * - regulator_get/set_*
475 * Note: The list must ends with NULL entry, like in the "platname" list below:
476 * char *my_regulators[] = {
482 int regulator_list_autoset(const char *list_platname[],
483 struct udevice *list_devp[],
487 * regulator_get_by_devname: returns the pointer to the pmic regulator device.
488 * Search by name, found in regulator device's name.
490 * @devname - expected string for 'dev->name' of regulator device
491 * @devp - returned pointer to the regulator device
492 * @return 0 on success or negative value of errno.
494 * The returned 'regulator' device is probed and can be used with:
495 * - regulator_get/set_*
497 int regulator_get_by_devname(const char *devname, struct udevice **devp);
500 * regulator_get_by_platname: returns the pointer to the pmic regulator device.
501 * Search by name, found in regulator uclass platdata.
503 * @platname - expected string for uc_pdata->name of regulator uclass platdata
504 * @devp - returns pointer to the regulator device or NULL on error
505 * @return 0 on success or negative value of errno.
507 * The returned 'regulator' device is probed and can be used with:
508 * - regulator_get/set_*
510 int regulator_get_by_platname(const char *platname, struct udevice **devp);
513 * device_get_supply_regulator: returns the pointer to the supply regulator.
514 * Search by phandle, found in device's node.
516 * Note: Please pay attention to proper order of device bind sequence.
517 * The regulator device searched by the phandle, must be binded before
518 * this function call.
520 * @dev - device with supply phandle
521 * @supply_name - phandle name of regulator
522 * @devp - returned pointer to the supply device
523 * @return 0 on success or negative value of errno.
525 int device_get_supply_regulator(struct udevice *dev, const char *supply_name,
526 struct udevice **devp);
528 #endif /* _INCLUDE_REGULATOR_H_ */