1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * Copyright (C) 2014-2015 Samsung Electronics
4 * Przemyslaw Marczak <p.marczak@samsung.com>
5 */
6
7 #ifndef _INCLUDE_REGULATOR_H_
8 #define _INCLUDE_REGULATOR_H_
9
10 struct udevice;
11
12 /**
13 * U-Boot Voltage/Current Regulator
14 * ================================
15 *
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.
19 *
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'
24 *
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'.
30 *
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'.
33 *
34 * To proper bind the regulator device, the device tree node should provide
35 * regulator constraints, like in the example below:
36 *
37 * ldo1 {
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)
45 * };
46 *
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.
51 *
52 * Regulator bind:
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.
60 *
61 * Regulator get:
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.
66 *
67 * For more info about PMIC bind, please refer to file: 'include/power/pmic.h'
68 *
69 * Note:
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.
73 *
74 *
75 * Fixed Voltage/Current Regulator
76 * ===============================
77 *
78 * When fixed voltage regulator is needed, then enable the config:
79 * - CONFIG_DM_REGULATOR_FIXED
80 *
81 * The driver file: 'drivers/power/regulator/fixed.c', provides basic support
82 * for control the GPIO, and return the device tree constraint values.
83 *
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:
87 *
88 * simple-bus {
89 * compatible = "simple-bus";
90 * #address-cells = <1>;
91 * #size-cells = <0>;
92 *
93 * blue_led {
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>;
99 * };
100 * };
101 *
102 * The fixed regulator devices also provide regulator uclass platform data. And
103 * devices bound from such node, can use the regulator drivers API.
104 */
105
106 /* enum regulator_type - used for regulator_*() variant calls */
107 enum regulator_type {
108 REGULATOR_TYPE_LDO = 0,
109 REGULATOR_TYPE_BUCK,
110 REGULATOR_TYPE_DVS,
111 REGULATOR_TYPE_FIXED,
112 REGULATOR_TYPE_GPIO,
113 REGULATOR_TYPE_OTHER,
114 };
115
116 /**
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.
120 *
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
124 * Note:
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.
127 */
128 struct dm_regulator_mode {
129 int id; /* Set only as >= 0 (negative value is reserved for errno) */
130 int register_value;
131 const char *name;
132 };
133
134 enum regulator_flag {
135 REGULATOR_FLAG_AUTOSET_UV = 1 << 0,
136 REGULATOR_FLAG_AUTOSET_UA = 1 << 1,
137 };
138
139 /**
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.
144 *
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
154 * TODO(sjg@chromium.org): Consider putting the above two into @flags
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
160 *
161 * Note:
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.
166 */
167 struct dm_regulator_uclass_plat {
168 enum regulator_type type;
169 struct dm_regulator_mode *mode;
170 int mode_count;
171 int min_uV;
172 int max_uV;
173 int init_uV;
174 int min_uA;
175 int max_uA;
176 unsigned int ramp_delay;
177 bool always_on;
178 bool boot_on;
179 const char *name;
180 int flags;
181 u8 ctrl_reg;
182 u8 volt_reg;
183 bool suspend_on;
184 u32 suspend_uV;
185 };
186
187 /* Regulator device operations */
188 struct dm_regulator_ops {
189 /**
190 * The regulator output value function calls operates on a micro Volts.
191 *
192 * get/set_value - get/set output value of the given output number
193 * @dev - regulator device
194 * Sets:
195 * @uV - set the output value [micro Volts]
196 * @return output value [uV] on success or negative errno if fail.
197 */
198 int (*get_value)(struct udevice *dev);
199 int (*set_value)(struct udevice *dev, int uV);
200
201 /**
202 * The regulator suspend output value function calls operates
203 * on a micro Volts.
204 *
205 * get/set_suspen_value - get/set suspend mode output value
206 * @dev - regulator device
207 * Sets:
208 * @uV - set the suspend output value [micro Volts]
209 * @return output value [uV] on success or negative errno if fail.
210 */
211 int (*set_suspend_value)(struct udevice *dev, int uV);
212 int (*get_suspend_value)(struct udevice *dev);
213
214 /**
215 * The regulator output current function calls operates on a micro Amps.
216 *
217 * get/set_current - get/set output current of the given output number
218 * @dev - regulator device
219 * Sets:
220 * @uA - set the output current [micro Amps]
221 * @return output value [uA] on success or negative errno if fail.
222 */
223 int (*get_current)(struct udevice *dev);
224 int (*set_current)(struct udevice *dev, int uA);
225
226 /**
227 * The most basic feature of the regulator output is its enable state.
228 *
229 * get/set_enable - get/set enable state of the given output number
230 * @dev - regulator device
231 * Sets:
232 * @enable - set true - enable or false - disable
233 * @return true/false for get or -errno if fail; 0 / -errno for set.
234 */
235 int (*get_enable)(struct udevice *dev);
236 int (*set_enable)(struct udevice *dev, bool enable);
237
238 /**
239 * The most basic feature of the regulator output is its enable state
240 * in suspend mode.
241 *
242 * get/set_suspend_enable - get/set enable state of the suspend output
243 * @dev - regulator device
244 * Sets:
245 * @enable - set true - enable or false - disable
246 * @return true/false for get or -errno if fail; 0 / -errno for set.
247 */
248 int (*set_suspend_enable)(struct udevice *dev, bool enable);
249 int (*get_suspend_enable)(struct udevice *dev);
250
251 /**
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.
255 *
256 * get/set_mode - get/set operation mode of the given output number
257 * @dev - regulator device
258 * Sets
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.
261 * Note:
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.
264 */
265 int (*get_mode)(struct udevice *dev);
266 int (*set_mode)(struct udevice *dev, int mode_id);
267 };
268
269 #if CONFIG_IS_ENABLED(DM_REGULATOR)
270 /**
271 * regulator_mode: returns a pointer to the array of regulator mode info
272 *
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.
276 */
277 int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep);
278
279 /**
280 * regulator_get_value: get microvoltage voltage value of a given regulator
281 *
282 * @dev - pointer to the regulator device
283 * @return - positive output value [uV] on success or negative errno if fail.
284 */
285 int regulator_get_value(struct udevice *dev);
286
287 /**
288 * regulator_set_value: set the microvoltage value of a given regulator.
289 *
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
293 */
294 int regulator_set_value(struct udevice *dev, int uV);
295
296 /**
297 * regulator_set_suspend_value: set the suspend microvoltage value of a given regulator.
298 *
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
302 */
303 int regulator_set_suspend_value(struct udevice *dev, int uV);
304
305 /**
306 * regulator_get_suspend_value: get the suspend microvoltage value of a given regulator.
307 *
308 * @dev - pointer to the regulator device
309 * @return - positive output value [uV] on success or negative errno if fail.
310 */
311 int regulator_get_suspend_value(struct udevice *dev);
312
313 /**
314 * regulator_set_value_force: set the microvoltage value of a given regulator
315 * without any min-,max condition check
316 *
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
320 */
321 int regulator_set_value_force(struct udevice *dev, int uV);
322
323 /**
324 * regulator_get_current: get microampere value of a given regulator
325 *
326 * @dev - pointer to the regulator device
327 * @return - positive output current [uA] on success or negative errno if fail.
328 */
329 int regulator_get_current(struct udevice *dev);
330
331 /**
332 * regulator_set_current: set the microampere value of a given regulator.
333 *
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
337 */
338 int regulator_set_current(struct udevice *dev, int uA);
339
340 /**
341 * regulator_get_enable: get regulator device enable state.
342 *
343 * @dev - pointer to the regulator device
344 * @return - true/false of enable state or -errno val if fails
345 */
346 int regulator_get_enable(struct udevice *dev);
347
348 /**
349 * regulator_set_enable: set regulator enable state
350 *
351 * @dev - pointer to the regulator device
352 * @enable - set true or false
353 * @return - 0 on success or -errno val if fails
354 */
355 int regulator_set_enable(struct udevice *dev, bool enable);
356
357 /**
358 * regulator_set_enable_if_allowed: set regulator enable state if allowed by
359 * regulator
360 *
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.
365 */
366 int regulator_set_enable_if_allowed(struct udevice *dev, bool enable);
367
368 /**
369 * regulator_set_suspend_enable: set regulator suspend enable state
370 *
371 * @dev - pointer to the regulator device
372 * @enable - set true or false
373 * @return - 0 on success or -errno val if fails
374 */
375 int regulator_set_suspend_enable(struct udevice *dev, bool enable);
376
377 /**
378 * regulator_get_suspend_enable: get regulator suspend enable state
379 *
380 * @dev - pointer to the regulator device
381 * @return - true/false of enable state or -errno val if fails
382 */
383 int regulator_get_suspend_enable(struct udevice *dev);
384
385 /**
386 * regulator_get_mode: get active operation mode id of a given regulator
387 *
388 * @dev - pointer to the regulator device
389 * @return - positive mode 'id' number on success or -errno val if fails
390 * Note:
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.
395 */
396 int regulator_get_mode(struct udevice *dev);
397
398 /**
399 * regulator_set_mode: set the given regulator's, active mode id
400 *
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
404 * Note:
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.
409 */
410 int regulator_set_mode(struct udevice *dev, int mode_id);
411
412 /**
413 * regulators_enable_boot_on() - enable regulators needed for boot
414 *
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.
418 *
419 * This effectively calls regulator_autoset() for every regulator.
420 */
421 int regulators_enable_boot_on(bool verbose);
422
423 /**
424 * regulator_autoset: setup the voltage/current on a regulator
425 *
426 * The setup depends on constraints found in device's uclass's platform data
427 * (struct dm_regulator_uclass_plat):
428 *
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
433 *
434 * The function returns on the first-encountered error.
435 *
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.
439 */
440 int regulator_autoset(struct udevice *dev);
441
442 /**
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
450 *
451 * The function returns on first encountered error.
452 *
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.
456 *
457 * The returned 'regulator' device can be used with:
458 * - regulator_get/set_*
459 */
460 int regulator_autoset_by_name(const char *platname, struct udevice **devp);
461
462 /**
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.
467 *
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.
474 *
475 * The returned 'regulator' devices can be used with:
476 * - regulator_get/set_*
477 *
478 * Note: The list must ends with NULL entry, like in the "platname" list below:
479 * char *my_regulators[] = {
480 * "VCC_3.3V",
481 * "VCC_1.8V",
482 * NULL,
483 * };
484 */
485 int regulator_list_autoset(const char *list_platname[],
486 struct udevice *list_devp[],
487 bool verbose);
488
489 /**
490 * regulator_get_by_devname: returns the pointer to the pmic regulator device.
491 * Search by name, found in regulator device's name.
492 *
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.
496 *
497 * The returned 'regulator' device is probed and can be used with:
498 * - regulator_get/set_*
499 */
500 int regulator_get_by_devname(const char *devname, struct udevice **devp);
501
502 /**
503 * regulator_get_by_platname: returns the pointer to the pmic regulator device.
504 * Search by name, found in regulator uclass plat.
505 *
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.
509 *
510 * The returned 'regulator' device is probed and can be used with:
511 * - regulator_get/set_*
512 */
513 int regulator_get_by_platname(const char *platname, struct udevice **devp);
514
515 /**
516 * device_get_supply_regulator: returns the pointer to the supply regulator.
517 * Search by phandle, found in device's node.
518 *
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.
522 *
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.
527 */
528 int device_get_supply_regulator(struct udevice *dev, const char *supply_name,
529 struct udevice **devp);
530 #else
regulator_mode(struct udevice * dev,struct dm_regulator_mode ** modep)531 static inline int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep)
532 {
533 return -ENOSYS;
534 }
535
regulator_get_value(struct udevice * dev)536 static inline int regulator_get_value(struct udevice *dev)
537 {
538 return -ENOSYS;
539 }
540
regulator_set_value(struct udevice * dev,int uV)541 static inline int regulator_set_value(struct udevice *dev, int uV)
542 {
543 return -ENOSYS;
544 }
545
regulator_set_suspend_value(struct udevice * dev,int uV)546 static inline int regulator_set_suspend_value(struct udevice *dev, int uV)
547 {
548 return -ENOSYS;
549 }
550
regulator_get_suspend_value(struct udevice * dev)551 static inline int regulator_get_suspend_value(struct udevice *dev)
552 {
553 return -ENOSYS;
554 }
555
regulator_set_value_force(struct udevice * dev,int uV)556 static inline int regulator_set_value_force(struct udevice *dev, int uV)
557 {
558 return -ENOSYS;
559 }
560
regulator_get_current(struct udevice * dev)561 static inline int regulator_get_current(struct udevice *dev)
562 {
563 return -ENOSYS;
564 }
565
regulator_set_current(struct udevice * dev,int uA)566 static inline int regulator_set_current(struct udevice *dev, int uA)
567 {
568 return -ENOSYS;
569 }
570
regulator_get_enable(struct udevice * dev)571 static inline int regulator_get_enable(struct udevice *dev)
572 {
573 return -ENOSYS;
574 }
575
regulator_set_enable(struct udevice * dev,bool enable)576 static inline int regulator_set_enable(struct udevice *dev, bool enable)
577 {
578 return -ENOSYS;
579 }
580
regulator_set_enable_if_allowed(struct udevice * dev,bool enable)581 static inline int regulator_set_enable_if_allowed(struct udevice *dev, bool enable)
582 {
583 return -ENOSYS;
584 }
585
regulator_set_suspend_enable(struct udevice * dev,bool enable)586 static inline int regulator_set_suspend_enable(struct udevice *dev, bool enable)
587 {
588 return -ENOSYS;
589 }
590
regulator_get_suspend_enable(struct udevice * dev)591 static inline int regulator_get_suspend_enable(struct udevice *dev)
592 {
593 return -ENOSYS;
594 }
595
regulator_get_mode(struct udevice * dev)596 static inline int regulator_get_mode(struct udevice *dev)
597 {
598 return -ENOSYS;
599 }
600
regulator_set_mode(struct udevice * dev,int mode_id)601 static inline int regulator_set_mode(struct udevice *dev, int mode_id)
602 {
603 return -ENOSYS;
604 }
605
regulators_enable_boot_on(bool verbose)606 static inline int regulators_enable_boot_on(bool verbose)
607 {
608 return -ENOSYS;
609 }
610
regulator_autoset(struct udevice * dev)611 static inline int regulator_autoset(struct udevice *dev)
612 {
613 return -ENOSYS;
614 }
615
regulator_autoset_by_name(const char * platname,struct udevice ** devp)616 static inline int regulator_autoset_by_name(const char *platname, struct udevice **devp)
617 {
618 return -ENOSYS;
619 }
620
regulator_list_autoset(const char * list_platname[],struct udevice * list_devp[],bool verbose)621 static inline int regulator_list_autoset(const char *list_platname[], struct udevice *list_devp[],
622 bool verbose)
623 {
624 return -ENOSYS;
625 }
626
regulator_get_by_devname(const char * devname,struct udevice ** devp)627 static inline int regulator_get_by_devname(const char *devname, struct udevice **devp)
628 {
629 return -ENOSYS;
630 }
631
regulator_get_by_platname(const char * platname,struct udevice ** devp)632 static inline int regulator_get_by_platname(const char *platname, struct udevice **devp)
633 {
634 return -ENOSYS;
635 }
636
device_get_supply_regulator(struct udevice * dev,const char * supply_name,struct udevice ** devp)637 static inline int device_get_supply_regulator(struct udevice *dev, const char *supply_name,
638 struct udevice **devp)
639 {
640 return -ENOSYS;
641 }
642 #endif
643
644 #endif /* _INCLUDE_REGULATOR_H_ */
645