blob: 015229027c874eeb860b91000eda11accf604e51 [file] [log] [blame]
Przemyslaw Marczak08edd002015-04-20 20:07:42 +02001/*
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 {
Przemyslaw Marczak75692a32015-05-13 13:38:27 +020037 * regulator-name = "VDD_MMC_1.8V"; (must be unique for proper bind)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020038 * 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 *
Przemyslaw Marczak75692a32015-05-13 13:38:27 +020046 * 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.
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020049 *
50 * Regulator bind:
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,
Przemyslaw Marczak75692a32015-05-13 13:38:27 +020053 * which does the scan on the device node, for the 'regulator-name' constraint.
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020054 * 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_node() - this is usually done automatically for bus devices,
57 * as a post bind method.
Przemyslaw Marczak75692a32015-05-13 13:38:27 +020058 *
59 * Regulator get:
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020060 * Having the device's name constraint, we can call regulator_by_platname(),
Przemyslaw Marczak75692a32015-05-13 13:38:27 +020061 * to find the required regulator. Before return, the regulator is probed,
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020062 * and the rest of its constraints are put into the device's uclass platform
63 * data, by the uclass regulator '.pre_probe' method.
64 *
65 * For more info about PMIC bind, please refer to file: 'include/power/pmic.h'
66 *
67 * Note:
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.
71 *
72 *
73 * Fixed Voltage/Current Regulator
74 * ===============================
75 *
76 * When fixed voltage regulator is needed, then enable the config:
77 * - CONFIG_DM_REGULATOR_FIXED
78 *
79 * The driver file: 'drivers/power/regulator/fixed.c', provides basic support
80 * for control the GPIO, and return the device tree constraint values.
81 *
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:
85 *
86 * simple-bus {
87 * compatible = "simple-bus";
88 * #address-cells = <1>;
89 * #size-cells = <0>;
90 *
91 * blue_led {
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>;
97 * };
98 * };
99 *
100 * The fixed regulator devices also provide regulator uclass platform data. And
101 * devices bound from such node, can use the regulator drivers API.
102*/
103
104/* enum regulator_type - used for regulator_*() variant calls */
105enum regulator_type {
106 REGULATOR_TYPE_LDO = 0,
107 REGULATOR_TYPE_BUCK,
108 REGULATOR_TYPE_DVS,
109 REGULATOR_TYPE_FIXED,
110 REGULATOR_TYPE_OTHER,
111};
112
113/**
114 * struct dm_regulator_mode - this structure holds an information about
115 * each regulator operation mode. Probably in most cases - an array.
116 * This will be probably a driver-static data, since it is device-specific.
117 *
118 * @id - a driver-specific mode id
119 * @register_value - a driver-specific value for its mode id
120 * @name - the name of mode - used for regulator command
121 * Note:
122 * The field 'id', should be always a positive number, since the negative values
123 * are reserved for the errno numbers when returns the mode id.
124 */
125struct dm_regulator_mode {
126 int id; /* Set only as >= 0 (negative value is reserved for errno) */
127 int register_value;
128 const char *name;
129};
130
Simon Glass991f9bc2015-06-23 15:38:57 -0600131enum regulator_flag {
132 REGULATOR_FLAG_AUTOSET_UV = 1 << 0,
133 REGULATOR_FLAG_AUTOSET_UA = 1 << 1,
134};
135
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200136/**
137 * struct dm_regulator_uclass_platdata - pointed by dev->uclass_platdata, and
138 * allocated on each regulator bind. This structure holds an information
139 * about each regulator's constraints and supported operation modes.
140 * There is no "step" voltage value - so driver should take care of this.
141 *
142 * @type - one of 'enum regulator_type'
143 * @mode - pointer to the regulator mode (array if more than one)
144 * @mode_count - number of '.mode' entries
145 * @min_uV* - minimum voltage (micro Volts)
146 * @max_uV* - maximum voltage (micro Volts)
147 * @min_uA* - minimum amperage (micro Amps)
148 * @max_uA* - maximum amperage (micro Amps)
149 * @always_on* - bool type, true or false
150 * @boot_on* - bool type, true or false
Simon Glass991f9bc2015-06-23 15:38:57 -0600151 * TODO(sjg@chromium.org): Consider putting the above two into @flags
152 * @flags: - flags value (see REGULATOR_FLAG_...)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200153 * @name** - fdt regulator name - should be taken from the device tree
154 *
155 * Note:
156 * * - set automatically on device probe by the uclass's '.pre_probe' method.
157 * ** - set automatically on device bind by the uclass's '.post_bind' method.
158 * The constraints: type, mode, mode_count, can be set by device driver, e.g.
159 * by the driver '.probe' method.
160 */
161struct dm_regulator_uclass_platdata {
162 enum regulator_type type;
163 struct dm_regulator_mode *mode;
164 int mode_count;
165 int min_uV;
166 int max_uV;
167 int min_uA;
168 int max_uA;
169 bool always_on;
170 bool boot_on;
171 const char *name;
Simon Glass991f9bc2015-06-23 15:38:57 -0600172 int flags;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200173};
174
175/* Regulator device operations */
176struct dm_regulator_ops {
177 /**
178 * The regulator output value function calls operates on a micro Volts.
179 *
180 * get/set_value - get/set output value of the given output number
181 * @dev - regulator device
182 * Sets:
183 * @uV - set the output value [micro Volts]
Przemyslaw Marczakafee81e2015-04-20 20:07:47 +0200184 * @return output value [uV] on success or negative errno if fail.
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200185 */
186 int (*get_value)(struct udevice *dev);
187 int (*set_value)(struct udevice *dev, int uV);
188
189 /**
190 * The regulator output current function calls operates on a micro Amps.
191 *
192 * get/set_current - get/set output current of the given output number
193 * @dev - regulator device
194 * Sets:
195 * @uA - set the output current [micro Amps]
Przemyslaw Marczakafee81e2015-04-20 20:07:47 +0200196 * @return output value [uA] on success or negative errno if fail.
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200197 */
198 int (*get_current)(struct udevice *dev);
199 int (*set_current)(struct udevice *dev, int uA);
200
201 /**
202 * The most basic feature of the regulator output is its enable state.
203 *
204 * get/set_enable - get/set enable state of the given output number
205 * @dev - regulator device
206 * Sets:
207 * @enable - set true - enable or false - disable
Przemyslaw Marczakafee81e2015-04-20 20:07:47 +0200208 * @return true/false for get; or 0 / -errno for set.
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200209 */
210 bool (*get_enable)(struct udevice *dev);
211 int (*set_enable)(struct udevice *dev, bool enable);
212
213 /**
Przemyslaw Marczakafee81e2015-04-20 20:07:47 +0200214 * The 'get/set_mode()' function calls should operate on a driver-
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200215 * specific mode id definitions, which should be found in:
216 * field 'id' of struct dm_regulator_mode.
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200217 *
218 * get/set_mode - get/set operation mode of the given output number
219 * @dev - regulator device
220 * Sets
221 * @mode_id - set output mode id (struct dm_regulator_mode->id)
Przemyslaw Marczakafee81e2015-04-20 20:07:47 +0200222 * @return id/0 for get/set on success or negative errno if fail.
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200223 * Note:
224 * The field 'id' of struct type 'dm_regulator_mode', should be always
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200225 * a positive number, since the negative is reserved for the error.
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200226 */
227 int (*get_mode)(struct udevice *dev);
228 int (*set_mode)(struct udevice *dev, int mode_id);
229};
230
231/**
232 * regulator_mode: returns a pointer to the array of regulator mode info
233 *
234 * @dev - pointer to the regulator device
235 * @modep - pointer to the returned mode info array
Przemyslaw Marczakafee81e2015-04-20 20:07:47 +0200236 * @return - count of modep entries on success or negative errno if fail.
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200237 */
238int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep);
239
240/**
241 * regulator_get_value: get microvoltage voltage value of a given regulator
242 *
243 * @dev - pointer to the regulator device
Przemyslaw Marczakafee81e2015-04-20 20:07:47 +0200244 * @return - positive output value [uV] on success or negative errno if fail.
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200245 */
246int regulator_get_value(struct udevice *dev);
247
248/**
249 * regulator_set_value: set the microvoltage value of a given regulator.
250 *
251 * @dev - pointer to the regulator device
252 * @uV - the output value to set [micro Volts]
Przemyslaw Marczakafee81e2015-04-20 20:07:47 +0200253 * @return - 0 on success or -errno val if fails
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200254 */
255int regulator_set_value(struct udevice *dev, int uV);
256
257/**
258 * regulator_get_current: get microampere value of a given regulator
259 *
260 * @dev - pointer to the regulator device
Przemyslaw Marczakafee81e2015-04-20 20:07:47 +0200261 * @return - positive output current [uA] on success or negative errno if fail.
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200262 */
263int regulator_get_current(struct udevice *dev);
264
265/**
266 * regulator_set_current: set the microampere value of a given regulator.
267 *
268 * @dev - pointer to the regulator device
269 * @uA - set the output current [micro Amps]
Przemyslaw Marczakafee81e2015-04-20 20:07:47 +0200270 * @return - 0 on success or -errno val if fails
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200271 */
272int regulator_set_current(struct udevice *dev, int uA);
273
274/**
275 * regulator_get_enable: get regulator device enable state.
276 *
277 * @dev - pointer to the regulator device
Przemyslaw Marczakafee81e2015-04-20 20:07:47 +0200278 * @return - true/false of enable state
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200279 */
280bool regulator_get_enable(struct udevice *dev);
281
282/**
283 * regulator_set_enable: set regulator enable state
284 *
285 * @dev - pointer to the regulator device
286 * @enable - set true or false
Przemyslaw Marczakafee81e2015-04-20 20:07:47 +0200287 * @return - 0 on success or -errno val if fails
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200288 */
289int regulator_set_enable(struct udevice *dev, bool enable);
290
291/**
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200292 * regulator_get_mode: get active operation mode id of a given regulator
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200293 *
294 * @dev - pointer to the regulator device
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200295 * @return - positive mode 'id' number on success or -errno val if fails
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200296 * Note:
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200297 * The device can provide an array of operating modes, which is type of struct
298 * dm_regulator_mode. Each mode has it's own 'id', which should be unique inside
299 * that array. By calling this function, the driver should return an active mode
300 * id of the given regulator device.
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200301 */
302int regulator_get_mode(struct udevice *dev);
303
304/**
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200305 * regulator_set_mode: set the given regulator's, active mode id
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200306 *
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200307 * @dev - pointer to the regulator device
308 * @mode_id - mode id to set ('id' field of struct type dm_regulator_mode)
309 * @return - 0 on success or -errno value if fails
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200310 * Note:
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200311 * The device can provide an array of operating modes, which is type of struct
312 * dm_regulator_mode. Each mode has it's own 'id', which should be unique inside
313 * that array. By calling this function, the driver should set the active mode
314 * of a given regulator to given by "mode_id" argument.
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200315 */
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200316int regulator_set_mode(struct udevice *dev, int mode_id);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200317
318/**
Simon Glassefeeb2c2015-06-23 15:38:59 -0600319 * regulators_enable_boot_on() - enable regulators needed for boot
320 *
321 * This enables all regulators which are marked to be on at boot time. This
322 * only works for regulators which don't have a range for voltage/current,
323 * since in that case it is not possible to know which value to use.
324 *
325 * This effectively calls regulator_autoset() for every regulator.
326 */
327int regulators_enable_boot_on(bool verbose);
328
329/**
Simon Glass46cb8242015-06-23 15:38:58 -0600330 * regulator_autoset: setup the voltage/current on a regulator
331 *
332 * The setup depends on constraints found in device's uclass's platform data
333 * (struct dm_regulator_uclass_platdata):
334 *
335 * - Enable - will set - if any of: 'always_on' or 'boot_on' is set to true,
336 * or if both are unset, then the function returns
337 * - Voltage value - will set - if '.min_uV' and '.max_uV' values are equal
338 * - Current limit - will set - if '.min_uA' and '.max_uA' values are equal
339 *
340 * The function returns on the first-encountered error.
341 *
342 * @platname - expected string for dm_regulator_uclass_platdata .name field
343 * @devp - returned pointer to the regulator device - if non-NULL passed
344 * @return: 0 on success or negative value of errno.
345 */
346int regulator_autoset(struct udevice *dev);
347
348/**
349 * regulator_autoset_by_name: setup the regulator given by its uclass's
350 * platform data name field. The setup depends on constraints found in device's
351 * uclass's platform data (struct dm_regulator_uclass_platdata):
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200352 * - Enable - will set - if any of: 'always_on' or 'boot_on' is set to true,
353 * or if both are unset, then the function returns
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200354 * - Voltage value - will set - if '.min_uV' and '.max_uV' values are equal
355 * - Current limit - will set - if '.min_uA' and '.max_uA' values are equal
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200356 *
357 * The function returns on first encountered error.
358 *
359 * @platname - expected string for dm_regulator_uclass_platdata .name field
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200360 * @devp - returned pointer to the regulator device - if non-NULL passed
Przemyslaw Marczakafee81e2015-04-20 20:07:47 +0200361 * @return: 0 on success or negative value of errno.
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200362 *
363 * The returned 'regulator' device can be used with:
364 * - regulator_get/set_*
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200365 */
Simon Glass46cb8242015-06-23 15:38:58 -0600366int regulator_autoset_by_name(const char *platname, struct udevice **devp);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200367
368/**
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200369 * regulator_list_autoset: setup the regulators given by list of their uclass's
370 * platform data name field. The setup depends on constraints found in device's
371 * uclass's platform data. The function loops with calls to:
Simon Glass46cb8242015-06-23 15:38:58 -0600372 * regulator_autoset_by_name() for each name from the list.
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200373 *
374 * @list_platname - an array of expected strings for .name field of each
375 * regulator's uclass platdata
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200376 * @list_devp - an array of returned pointers to the successfully setup
377 * regulator devices if non-NULL passed
378 * @verbose - (true/false) print each regulator setup info, or be quiet
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200379 * @return 0 on successfully setup of all list entries, otherwise first error.
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200380 *
381 * The returned 'regulator' devices can be used with:
382 * - regulator_get/set_*
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200383 *
384 * Note: The list must ends with NULL entry, like in the "platname" list below:
385 * char *my_regulators[] = {
386 * "VCC_3.3V",
387 * "VCC_1.8V",
388 * NULL,
389 * };
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200390 */
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200391int regulator_list_autoset(const char *list_platname[],
392 struct udevice *list_devp[],
393 bool verbose);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200394
395/**
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200396 * regulator_get_by_devname: returns the pointer to the pmic regulator device.
397 * Search by name, found in regulator device's name.
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200398 *
399 * @devname - expected string for 'dev->name' of regulator device
Przemyslaw Marczakafee81e2015-04-20 20:07:47 +0200400 * @devp - returned pointer to the regulator device
401 * @return 0 on success or negative value of errno.
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200402 *
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200403 * The returned 'regulator' device is probed and can be used with:
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200404 * - regulator_get/set_*
405 */
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200406int regulator_get_by_devname(const char *devname, struct udevice **devp);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200407
408/**
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200409 * regulator_get_by_platname: returns the pointer to the pmic regulator device.
410 * Search by name, found in regulator uclass platdata.
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200411 *
Przemyslaw Marczakafee81e2015-04-20 20:07:47 +0200412 * @platname - expected string for uc_pdata->name of regulator uclass platdata
Simon Glass46cb8242015-06-23 15:38:58 -0600413 * @devp - returns pointer to the regulator device or NULL on error
Przemyslaw Marczakafee81e2015-04-20 20:07:47 +0200414 * @return 0 on success or negative value of errno.
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200415 *
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200416 * The returned 'regulator' device is probed and can be used with:
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200417 * - regulator_get/set_*
418 */
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200419int regulator_get_by_platname(const char *platname, struct udevice **devp);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200420
421#endif /* _INCLUDE_REGULATOR_H_ */