blob: 9bcd728120aa9a53b2f4d1a4ebe157539a7d1675 [file] [log] [blame]
Przemyslaw Marczakaf41e8d2015-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 Marczak3b880752015-05-13 13:38:27 +020037 * regulator-name = "VDD_MMC_1.8V"; (must be unique for proper bind)
Przemyslaw Marczakaf41e8d2015-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 Marczak3b880752015-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.
Peng Fan40ade2c2015-08-07 16:43:43 +080049 * If regulator-name property is not provided, node name will be chosen.
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +020050 *
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,
Przemyslaw Marczak3b880752015-05-13 13:38:27 +020054 * which does the scan on the device node, for the 'regulator-name' constraint.
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +020055 * 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
Simon Glass2e3f1ff2016-07-05 17:10:09 -060057 * dm_scan_fdt_dev() - this is usually done automatically for bus devices,
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +020058 * as a post bind method.
Przemyslaw Marczak3b880752015-05-13 13:38:27 +020059 *
60 * Regulator get:
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +020061 * Having the device's name constraint, we can call regulator_by_platname(),
Przemyslaw Marczak3b880752015-05-13 13:38:27 +020062 * to find the required regulator. Before return, the regulator is probed,
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +020063 * 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 */
106enum regulator_type {
107 REGULATOR_TYPE_LDO = 0,
108 REGULATOR_TYPE_BUCK,
109 REGULATOR_TYPE_DVS,
110 REGULATOR_TYPE_FIXED,
111 REGULATOR_TYPE_OTHER,
112};
113
114/**
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.
118 *
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
122 * Note:
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.
125 */
126struct dm_regulator_mode {
127 int id; /* Set only as >= 0 (negative value is reserved for errno) */
128 int register_value;
129 const char *name;
130};
131
Simon Glass7837cea2015-06-23 15:38:57 -0600132enum regulator_flag {
133 REGULATOR_FLAG_AUTOSET_UV = 1 << 0,
134 REGULATOR_FLAG_AUTOSET_UA = 1 << 1,
135};
136
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200137/**
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.
142 *
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
Simon Glass7837cea2015-06-23 15:38:57 -0600152 * TODO(sjg@chromium.org): Consider putting the above two into @flags
153 * @flags: - flags value (see REGULATOR_FLAG_...)
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200154 * @name** - fdt regulator name - should be taken from the device tree
155 *
156 * Note:
157 * * - set automatically on device probe by the uclass's '.pre_probe' method.
158 * ** - set automatically on device bind by the uclass's '.post_bind' method.
159 * The constraints: type, mode, mode_count, can be set by device driver, e.g.
160 * by the driver '.probe' method.
161 */
162struct dm_regulator_uclass_platdata {
163 enum regulator_type type;
164 struct dm_regulator_mode *mode;
165 int mode_count;
166 int min_uV;
167 int max_uV;
168 int min_uA;
169 int max_uA;
170 bool always_on;
171 bool boot_on;
172 const char *name;
Simon Glass7837cea2015-06-23 15:38:57 -0600173 int flags;
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200174};
175
176/* Regulator device operations */
177struct dm_regulator_ops {
178 /**
179 * The regulator output value function calls operates on a micro Volts.
180 *
181 * get/set_value - get/set output value of the given output number
182 * @dev - regulator device
183 * Sets:
184 * @uV - set the output value [micro Volts]
Przemyslaw Marczak1757df42015-04-20 20:07:47 +0200185 * @return output value [uV] on success or negative errno if fail.
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200186 */
187 int (*get_value)(struct udevice *dev);
188 int (*set_value)(struct udevice *dev, int uV);
189
190 /**
191 * The regulator output current function calls operates on a micro Amps.
192 *
193 * get/set_current - get/set output current of the given output number
194 * @dev - regulator device
195 * Sets:
196 * @uA - set the output current [micro Amps]
Przemyslaw Marczak1757df42015-04-20 20:07:47 +0200197 * @return output value [uA] on success or negative errno if fail.
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200198 */
199 int (*get_current)(struct udevice *dev);
200 int (*set_current)(struct udevice *dev, int uA);
201
202 /**
203 * The most basic feature of the regulator output is its enable state.
204 *
205 * get/set_enable - get/set enable state of the given output number
206 * @dev - regulator device
207 * Sets:
208 * @enable - set true - enable or false - disable
Przemyslaw Marczak1757df42015-04-20 20:07:47 +0200209 * @return true/false for get; or 0 / -errno for set.
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200210 */
211 bool (*get_enable)(struct udevice *dev);
212 int (*set_enable)(struct udevice *dev, bool enable);
213
214 /**
Przemyslaw Marczak1757df42015-04-20 20:07:47 +0200215 * The 'get/set_mode()' function calls should operate on a driver-
Przemyslaw Marczak3b880752015-05-13 13:38:27 +0200216 * specific mode id definitions, which should be found in:
217 * field 'id' of struct dm_regulator_mode.
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200218 *
219 * get/set_mode - get/set operation mode of the given output number
220 * @dev - regulator device
221 * Sets
222 * @mode_id - set output mode id (struct dm_regulator_mode->id)
Przemyslaw Marczak1757df42015-04-20 20:07:47 +0200223 * @return id/0 for get/set on success or negative errno if fail.
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200224 * Note:
225 * The field 'id' of struct type 'dm_regulator_mode', should be always
Przemyslaw Marczak3b880752015-05-13 13:38:27 +0200226 * a positive number, since the negative is reserved for the error.
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200227 */
228 int (*get_mode)(struct udevice *dev);
229 int (*set_mode)(struct udevice *dev, int mode_id);
230};
231
232/**
233 * regulator_mode: returns a pointer to the array of regulator mode info
234 *
235 * @dev - pointer to the regulator device
236 * @modep - pointer to the returned mode info array
Przemyslaw Marczak1757df42015-04-20 20:07:47 +0200237 * @return - count of modep entries on success or negative errno if fail.
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200238 */
239int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep);
240
241/**
242 * regulator_get_value: get microvoltage voltage value of a given regulator
243 *
244 * @dev - pointer to the regulator device
Przemyslaw Marczak1757df42015-04-20 20:07:47 +0200245 * @return - positive output value [uV] on success or negative errno if fail.
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200246 */
247int regulator_get_value(struct udevice *dev);
248
249/**
250 * regulator_set_value: set the microvoltage value of a given regulator.
251 *
252 * @dev - pointer to the regulator device
253 * @uV - the output value to set [micro Volts]
Przemyslaw Marczak1757df42015-04-20 20:07:47 +0200254 * @return - 0 on success or -errno val if fails
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200255 */
256int regulator_set_value(struct udevice *dev, int uV);
257
258/**
259 * regulator_get_current: get microampere value of a given regulator
260 *
261 * @dev - pointer to the regulator device
Przemyslaw Marczak1757df42015-04-20 20:07:47 +0200262 * @return - positive output current [uA] on success or negative errno if fail.
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200263 */
264int regulator_get_current(struct udevice *dev);
265
266/**
267 * regulator_set_current: set the microampere value of a given regulator.
268 *
269 * @dev - pointer to the regulator device
270 * @uA - set the output current [micro Amps]
Przemyslaw Marczak1757df42015-04-20 20:07:47 +0200271 * @return - 0 on success or -errno val if fails
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200272 */
273int regulator_set_current(struct udevice *dev, int uA);
274
275/**
276 * regulator_get_enable: get regulator device enable state.
277 *
278 * @dev - pointer to the regulator device
Przemyslaw Marczak1757df42015-04-20 20:07:47 +0200279 * @return - true/false of enable state
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200280 */
281bool regulator_get_enable(struct udevice *dev);
282
283/**
284 * regulator_set_enable: set regulator enable state
285 *
286 * @dev - pointer to the regulator device
287 * @enable - set true or false
Przemyslaw Marczak1757df42015-04-20 20:07:47 +0200288 * @return - 0 on success or -errno val if fails
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200289 */
290int regulator_set_enable(struct udevice *dev, bool enable);
291
292/**
Przemyslaw Marczak3b880752015-05-13 13:38:27 +0200293 * regulator_get_mode: get active operation mode id of a given regulator
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200294 *
295 * @dev - pointer to the regulator device
Przemyslaw Marczak3b880752015-05-13 13:38:27 +0200296 * @return - positive mode 'id' number on success or -errno val if fails
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200297 * Note:
Przemyslaw Marczak3b880752015-05-13 13:38:27 +0200298 * The device can provide an array of operating modes, which is type of struct
299 * dm_regulator_mode. Each mode has it's own 'id', which should be unique inside
300 * that array. By calling this function, the driver should return an active mode
301 * id of the given regulator device.
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200302 */
303int regulator_get_mode(struct udevice *dev);
304
305/**
Przemyslaw Marczak3b880752015-05-13 13:38:27 +0200306 * regulator_set_mode: set the given regulator's, active mode id
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200307 *
Przemyslaw Marczak3b880752015-05-13 13:38:27 +0200308 * @dev - pointer to the regulator device
309 * @mode_id - mode id to set ('id' field of struct type dm_regulator_mode)
310 * @return - 0 on success or -errno value if fails
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200311 * Note:
Przemyslaw Marczak3b880752015-05-13 13:38:27 +0200312 * The device can provide an array of operating modes, which is type of struct
313 * dm_regulator_mode. Each mode has it's own 'id', which should be unique inside
314 * that array. By calling this function, the driver should set the active mode
315 * of a given regulator to given by "mode_id" argument.
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200316 */
Przemyslaw Marczak3b880752015-05-13 13:38:27 +0200317int regulator_set_mode(struct udevice *dev, int mode_id);
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200318
319/**
Simon Glass083fc832015-06-23 15:38:59 -0600320 * regulators_enable_boot_on() - enable regulators needed for boot
321 *
322 * This enables all regulators which are marked to be on at boot time. This
323 * only works for regulators which don't have a range for voltage/current,
324 * since in that case it is not possible to know which value to use.
325 *
326 * This effectively calls regulator_autoset() for every regulator.
327 */
328int regulators_enable_boot_on(bool verbose);
329
330/**
Simon Glass3b55d302015-06-23 15:38:58 -0600331 * regulator_autoset: setup the voltage/current on a regulator
332 *
333 * The setup depends on constraints found in device's uclass's platform data
334 * (struct dm_regulator_uclass_platdata):
335 *
336 * - Enable - will set - if any of: 'always_on' or 'boot_on' is set to true,
337 * or if both are unset, then the function returns
338 * - Voltage value - will set - if '.min_uV' and '.max_uV' values are equal
339 * - Current limit - will set - if '.min_uA' and '.max_uA' values are equal
340 *
341 * The function returns on the first-encountered error.
342 *
343 * @platname - expected string for dm_regulator_uclass_platdata .name field
344 * @devp - returned pointer to the regulator device - if non-NULL passed
345 * @return: 0 on success or negative value of errno.
346 */
347int regulator_autoset(struct udevice *dev);
348
349/**
350 * regulator_autoset_by_name: setup the regulator given by its uclass's
351 * platform data name field. The setup depends on constraints found in device's
352 * uclass's platform data (struct dm_regulator_uclass_platdata):
Przemyslaw Marczak3b880752015-05-13 13:38:27 +0200353 * - Enable - will set - if any of: 'always_on' or 'boot_on' is set to true,
354 * or if both are unset, then the function returns
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200355 * - Voltage value - will set - if '.min_uV' and '.max_uV' values are equal
356 * - Current limit - will set - if '.min_uA' and '.max_uA' values are equal
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200357 *
358 * The function returns on first encountered error.
359 *
360 * @platname - expected string for dm_regulator_uclass_platdata .name field
Przemyslaw Marczak3b880752015-05-13 13:38:27 +0200361 * @devp - returned pointer to the regulator device - if non-NULL passed
Przemyslaw Marczak1757df42015-04-20 20:07:47 +0200362 * @return: 0 on success or negative value of errno.
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200363 *
364 * The returned 'regulator' device can be used with:
365 * - regulator_get/set_*
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200366 */
Simon Glass3b55d302015-06-23 15:38:58 -0600367int regulator_autoset_by_name(const char *platname, struct udevice **devp);
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200368
369/**
Przemyslaw Marczak3b880752015-05-13 13:38:27 +0200370 * regulator_list_autoset: setup the regulators given by list of their uclass's
371 * platform data name field. The setup depends on constraints found in device's
372 * uclass's platform data. The function loops with calls to:
Simon Glass3b55d302015-06-23 15:38:58 -0600373 * regulator_autoset_by_name() for each name from the list.
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200374 *
375 * @list_platname - an array of expected strings for .name field of each
376 * regulator's uclass platdata
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200377 * @list_devp - an array of returned pointers to the successfully setup
378 * regulator devices if non-NULL passed
379 * @verbose - (true/false) print each regulator setup info, or be quiet
Przemyslaw Marczak3b880752015-05-13 13:38:27 +0200380 * @return 0 on successfully setup of all list entries, otherwise first error.
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200381 *
382 * The returned 'regulator' devices can be used with:
383 * - regulator_get/set_*
Przemyslaw Marczak3b880752015-05-13 13:38:27 +0200384 *
385 * Note: The list must ends with NULL entry, like in the "platname" list below:
386 * char *my_regulators[] = {
387 * "VCC_3.3V",
388 * "VCC_1.8V",
389 * NULL,
390 * };
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200391 */
Przemyslaw Marczak3b880752015-05-13 13:38:27 +0200392int regulator_list_autoset(const char *list_platname[],
393 struct udevice *list_devp[],
394 bool verbose);
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200395
396/**
Przemyslaw Marczak3b880752015-05-13 13:38:27 +0200397 * regulator_get_by_devname: returns the pointer to the pmic regulator device.
398 * Search by name, found in regulator device's name.
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200399 *
400 * @devname - expected string for 'dev->name' of regulator device
Przemyslaw Marczak1757df42015-04-20 20:07:47 +0200401 * @devp - returned pointer to the regulator device
402 * @return 0 on success or negative value of errno.
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200403 *
Przemyslaw Marczak3b880752015-05-13 13:38:27 +0200404 * The returned 'regulator' device is probed and can be used with:
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200405 * - regulator_get/set_*
406 */
Przemyslaw Marczak3b880752015-05-13 13:38:27 +0200407int regulator_get_by_devname(const char *devname, struct udevice **devp);
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200408
409/**
Przemyslaw Marczak3b880752015-05-13 13:38:27 +0200410 * regulator_get_by_platname: returns the pointer to the pmic regulator device.
411 * Search by name, found in regulator uclass platdata.
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200412 *
Przemyslaw Marczak1757df42015-04-20 20:07:47 +0200413 * @platname - expected string for uc_pdata->name of regulator uclass platdata
Simon Glass3b55d302015-06-23 15:38:58 -0600414 * @devp - returns pointer to the regulator device or NULL on error
Przemyslaw Marczak1757df42015-04-20 20:07:47 +0200415 * @return 0 on success or negative value of errno.
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200416 *
Przemyslaw Marczak3b880752015-05-13 13:38:27 +0200417 * The returned 'regulator' device is probed and can be used with:
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200418 * - regulator_get/set_*
419 */
Przemyslaw Marczak3b880752015-05-13 13:38:27 +0200420int regulator_get_by_platname(const char *platname, struct udevice **devp);
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200421
Przemyslaw Marczak7c816e22015-10-27 13:07:59 +0100422/**
423 * device_get_supply_regulator: returns the pointer to the supply regulator.
424 * Search by phandle, found in device's node.
425 *
426 * Note: Please pay attention to proper order of device bind sequence.
427 * The regulator device searched by the phandle, must be binded before
428 * this function call.
429 *
430 * @dev - device with supply phandle
431 * @supply_name - phandle name of regulator
432 * @devp - returned pointer to the supply device
433 * @return 0 on success or negative value of errno.
434 */
435int device_get_supply_regulator(struct udevice *dev, const char *supply_name,
436 struct udevice **devp);
437
Przemyslaw Marczakaf41e8d2015-04-20 20:07:42 +0200438#endif /* _INCLUDE_REGULATOR_H_ */