regulator.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Copyright (C) 2014-2015 Samsung Electronics
  3. * Przemyslaw Marczak <p.marczak@samsung.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #ifndef _INCLUDE_REGULATOR_H_
  8. #define _INCLUDE_REGULATOR_H_
  9. /**
  10. * U-Boot Voltage/Current Regulator
  11. * ================================
  12. *
  13. * The regulator API is based on a driver model, with the device tree support.
  14. * And this header describes the functions and data types for the uclass id:
  15. * 'UCLASS_REGULATOR' and the regulator driver API.
  16. *
  17. * The regulator uclass - is based on uclass platform data which is allocated,
  18. * automatically for each regulator device on bind and 'dev->uclass_platdata'
  19. * points to it. The data type is: 'struct dm_regulator_uclass_platdata'.
  20. * The uclass file: 'drivers/power/regulator/regulator-uclass.c'
  21. *
  22. * The regulator device - is based on driver's model 'struct udevice'.
  23. * The API can use regulator name in two meanings:
  24. * - devname - the regulator device's name: 'dev->name'
  25. * - platname - the device's platdata's name. So in the code it looks like:
  26. * 'uc_pdata = dev->uclass_platdata'; 'name = uc_pdata->name'.
  27. *
  28. * The regulator device driver - provide an implementation of uclass operations
  29. * pointed by 'dev->driver->ops' as a struct of type 'struct dm_regulator_ops'.
  30. *
  31. * To proper bind the regulator device, the device tree node should provide
  32. * regulator constraints, like in the example below:
  33. *
  34. * ldo1 {
  35. * regulator-name = "VDD_MMC_1.8V"; (mandatory for bind)
  36. * regulator-min-microvolt = <1000000>; (optional)
  37. * regulator-max-microvolt = <1000000>; (optional)
  38. * regulator-min-microamp = <1000>; (optional)
  39. * regulator-max-microamp = <1000>; (optional)
  40. * regulator-always-on; (optional)
  41. * regulator-boot-on; (optional)
  42. * };
  43. *
  44. * Please take a notice, that for the proper operation at least name constraint
  45. * is needed, e.g. for call the device_by_platname(...).
  46. *
  47. * Regulator bind:
  48. * For each regulator device, the device_bind() should be called with passed
  49. * device tree offset. This is required for this uclass's '.post_bind' method,
  50. * which do the scan on the device node, for the 'regulator-name' constraint.
  51. * If the parent is not a PMIC device, and the child is not bind by function:
  52. * 'pmic_bind_childs()', then it's recommended to bind the device by call to
  53. * dm_scan_fdt_node() - this is usually done automatically for bus devices,
  54. * as a post bind method.
  55. * Having the device's name constraint, we can call regulator_by_platname(),
  56. * to find interesting regulator. Before return, the regulator is probed,
  57. * and the rest of its constraints are put into the device's uclass platform
  58. * data, by the uclass regulator '.pre_probe' method.
  59. *
  60. * For more info about PMIC bind, please refer to file: 'include/power/pmic.h'
  61. *
  62. * Note:
  63. * Please do not use the device_bind_by_name() function, since it pass '-1' as
  64. * device node offset - and the bind will fail on uclass .post_bind method,
  65. * because of missing 'regulator-name' constraint.
  66. *
  67. *
  68. * Fixed Voltage/Current Regulator
  69. * ===============================
  70. *
  71. * When fixed voltage regulator is needed, then enable the config:
  72. * - CONFIG_DM_REGULATOR_FIXED
  73. *
  74. * The driver file: 'drivers/power/regulator/fixed.c', provides basic support
  75. * for control the GPIO, and return the device tree constraint values.
  76. *
  77. * To bind the fixed voltage regulator device, we usually use a 'simple-bus'
  78. * node as a parent. And 'regulator-fixed' for the driver compatible. This is
  79. * the same as in the kernel. The example node of fixed regulator:
  80. *
  81. * simple-bus {
  82. * compatible = "simple-bus";
  83. * #address-cells = <1>;
  84. * #size-cells = <0>;
  85. *
  86. * blue_led {
  87. * compatible = "regulator-fixed";
  88. * regulator-name = "VDD_LED_3.3V";
  89. * regulator-min-microvolt = <3300000>;
  90. * regulator-max-microvolt = <3300000>;
  91. * gpio = <&gpc1 0 GPIO_ACTIVE_LOW>;
  92. * };
  93. * };
  94. *
  95. * The fixed regulator devices also provide regulator uclass platform data. And
  96. * devices bound from such node, can use the regulator drivers API.
  97. */
  98. /* enum regulator_type - used for regulator_*() variant calls */
  99. enum regulator_type {
  100. REGULATOR_TYPE_LDO = 0,
  101. REGULATOR_TYPE_BUCK,
  102. REGULATOR_TYPE_DVS,
  103. REGULATOR_TYPE_FIXED,
  104. REGULATOR_TYPE_OTHER,
  105. };
  106. /**
  107. * struct dm_regulator_mode - this structure holds an information about
  108. * each regulator operation mode. Probably in most cases - an array.
  109. * This will be probably a driver-static data, since it is device-specific.
  110. *
  111. * @id - a driver-specific mode id
  112. * @register_value - a driver-specific value for its mode id
  113. * @name - the name of mode - used for regulator command
  114. * Note:
  115. * The field 'id', should be always a positive number, since the negative values
  116. * are reserved for the errno numbers when returns the mode id.
  117. */
  118. struct dm_regulator_mode {
  119. int id; /* Set only as >= 0 (negative value is reserved for errno) */
  120. int register_value;
  121. const char *name;
  122. };
  123. /**
  124. * struct dm_regulator_uclass_platdata - pointed by dev->uclass_platdata, and
  125. * allocated on each regulator bind. This structure holds an information
  126. * about each regulator's constraints and supported operation modes.
  127. * There is no "step" voltage value - so driver should take care of this.
  128. *
  129. * @type - one of 'enum regulator_type'
  130. * @mode - pointer to the regulator mode (array if more than one)
  131. * @mode_count - number of '.mode' entries
  132. * @min_uV* - minimum voltage (micro Volts)
  133. * @max_uV* - maximum voltage (micro Volts)
  134. * @min_uA* - minimum amperage (micro Amps)
  135. * @max_uA* - maximum amperage (micro Amps)
  136. * @always_on* - bool type, true or false
  137. * @boot_on* - bool type, true or false
  138. * @name** - fdt regulator name - should be taken from the device tree
  139. *
  140. * Note:
  141. * * - set automatically on device probe by the uclass's '.pre_probe' method.
  142. * ** - set automatically on device bind by the uclass's '.post_bind' method.
  143. * The constraints: type, mode, mode_count, can be set by device driver, e.g.
  144. * by the driver '.probe' method.
  145. */
  146. struct dm_regulator_uclass_platdata {
  147. enum regulator_type type;
  148. struct dm_regulator_mode *mode;
  149. int mode_count;
  150. int min_uV;
  151. int max_uV;
  152. int min_uA;
  153. int max_uA;
  154. bool always_on;
  155. bool boot_on;
  156. const char *name;
  157. };
  158. /* Regulator device operations */
  159. struct dm_regulator_ops {
  160. /**
  161. * The regulator output value function calls operates on a micro Volts.
  162. *
  163. * get/set_value - get/set output value of the given output number
  164. * @dev - regulator device
  165. * Sets:
  166. * @uV - set the output value [micro Volts]
  167. * Returns: output value [uV] on success or negative errno if fail.
  168. */
  169. int (*get_value)(struct udevice *dev);
  170. int (*set_value)(struct udevice *dev, int uV);
  171. /**
  172. * The regulator output current function calls operates on a micro Amps.
  173. *
  174. * get/set_current - get/set output current of the given output number
  175. * @dev - regulator device
  176. * Sets:
  177. * @uA - set the output current [micro Amps]
  178. * Returns: output value [uA] on success or negative errno if fail.
  179. */
  180. int (*get_current)(struct udevice *dev);
  181. int (*set_current)(struct udevice *dev, int uA);
  182. /**
  183. * The most basic feature of the regulator output is its enable state.
  184. *
  185. * get/set_enable - get/set enable state of the given output number
  186. * @dev - regulator device
  187. * Sets:
  188. * @enable - set true - enable or false - disable
  189. * Returns: true/false for get; or 0 / -errno for set.
  190. */
  191. bool (*get_enable)(struct udevice *dev);
  192. int (*set_enable)(struct udevice *dev, bool enable);
  193. /**
  194. * The 'get/set_mode()' function calls should operate on a driver
  195. * specific mode definitions, which should be found in:
  196. * field 'mode' of struct mode_desc.
  197. *
  198. * get/set_mode - get/set operation mode of the given output number
  199. * @dev - regulator device
  200. * Sets
  201. * @mode_id - set output mode id (struct dm_regulator_mode->id)
  202. * Returns: id/0 for get/set on success or negative errno if fail.
  203. * Note:
  204. * The field 'id' of struct type 'dm_regulator_mode', should be always
  205. * positive number, since the negative is reserved for the error.
  206. */
  207. int (*get_mode)(struct udevice *dev);
  208. int (*set_mode)(struct udevice *dev, int mode_id);
  209. };
  210. /**
  211. * regulator_mode: returns a pointer to the array of regulator mode info
  212. *
  213. * @dev - pointer to the regulator device
  214. * @modep - pointer to the returned mode info array
  215. * Returns - count of modep entries on success or negative errno if fail.
  216. */
  217. int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep);
  218. /**
  219. * regulator_get_value: get microvoltage voltage value of a given regulator
  220. *
  221. * @dev - pointer to the regulator device
  222. * Returns - positive output value [uV] on success or negative errno if fail.
  223. */
  224. int regulator_get_value(struct udevice *dev);
  225. /**
  226. * regulator_set_value: set the microvoltage value of a given regulator.
  227. *
  228. * @dev - pointer to the regulator device
  229. * @uV - the output value to set [micro Volts]
  230. * Returns - 0 on success or -errno val if fails
  231. */
  232. int regulator_set_value(struct udevice *dev, int uV);
  233. /**
  234. * regulator_get_current: get microampere value of a given regulator
  235. *
  236. * @dev - pointer to the regulator device
  237. * Returns - positive output current [uA] on success or negative errno if fail.
  238. */
  239. int regulator_get_current(struct udevice *dev);
  240. /**
  241. * regulator_set_current: set the microampere value of a given regulator.
  242. *
  243. * @dev - pointer to the regulator device
  244. * @uA - set the output current [micro Amps]
  245. * Returns - 0 on success or -errno val if fails
  246. */
  247. int regulator_set_current(struct udevice *dev, int uA);
  248. /**
  249. * regulator_get_enable: get regulator device enable state.
  250. *
  251. * @dev - pointer to the regulator device
  252. * Returns - true/false of enable state
  253. */
  254. bool regulator_get_enable(struct udevice *dev);
  255. /**
  256. * regulator_set_enable: set regulator enable state
  257. *
  258. * @dev - pointer to the regulator device
  259. * @enable - set true or false
  260. * Returns - 0 on success or -errno val if fails
  261. */
  262. int regulator_set_enable(struct udevice *dev, bool enable);
  263. /**
  264. * regulator_get_mode: get mode of a given device regulator
  265. *
  266. * @dev - pointer to the regulator device
  267. * Returns - positive mode number on success or -errno val if fails
  268. * Note:
  269. * The regulator driver should return one of defined, mode number rather, than
  270. * the raw register value. The struct type 'mode_desc' provides a field 'mode'
  271. * for this purpose and register_value for a raw register value.
  272. */
  273. int regulator_get_mode(struct udevice *dev);
  274. /**
  275. * regulator_set_mode: set given regulator mode
  276. *
  277. * @dev - pointer to the regulator device
  278. * @mode - mode type (field 'mode' of struct mode_desc)
  279. * Returns - 0 on success or -errno value if fails
  280. * Note:
  281. * The regulator driver should take one of defined, mode number rather
  282. * than a raw register value. The struct type 'regulator_mode_desc' has
  283. * a mode field for this purpose and register_value for a raw register value.
  284. */
  285. int regulator_set_mode(struct udevice *dev, int mode);
  286. /**
  287. * regulator_by_platname_autoset_and_enable: setup the regulator given by
  288. * its uclass's platform data '.name'. The setup depends on constraints found
  289. * in device's uclass's platform data (struct dm_regulator_uclass_platdata):
  290. * - Voltage value - will set - if '.min_uV' and '.max_uV' values are equal
  291. * - Current limit - will set - if '.min_uA' and '.max_uA' values are equal
  292. * - Enable - will set - if '.always_on' or '.boot_on' are set to true
  293. *
  294. * The function returns on first encountered error.
  295. *
  296. * @platname - expected string for dm_regulator_uclass_platdata .name field
  297. * @devp - returned pointer to the regulator device - if non-NULL passed
  298. * @verbose - (true/false) print regulator setup info, or be quiet
  299. * Returns: 0 on success or negative value of errno.
  300. *
  301. * The returned 'regulator' device can be used with:
  302. * - regulator_get/set_*
  303. * For shorter call name, the below macro regulator_autoset() can be used.
  304. */
  305. int regulator_by_platname_autoset_and_enable(const char *platname,
  306. struct udevice **devp,
  307. bool verbose);
  308. #define regulator_autoset(platname, devp, verbose) \
  309. regulator_by_platname_autoset_and_enable(platname, devp, verbose)
  310. /**
  311. * regulator_by_platname_list_autoset_and_enable: setup the regulators given by
  312. * list of its uclass's platform data '.name'. The setup depends on constraints
  313. * found in device's uclass's platform data. The function loops with calls to:
  314. * regulator_by_platname_autoset_and_enable() for each name of list.
  315. *
  316. * @list_platname - an array of expected strings for .name field of each
  317. * regulator's uclass platdata
  318. * @list_entries - number of regulator's name list entries
  319. * @list_devp - an array of returned pointers to the successfully setup
  320. * regulator devices if non-NULL passed
  321. * @verbose - (true/false) print each regulator setup info, or be quiet
  322. * Returns: 0 on successfully setup of all list entries or 1 otwerwise.
  323. *
  324. * The returned 'regulator' devices can be used with:
  325. * - regulator_get/set_*
  326. * For shorter call name, the below macro regulator_list_autoset() can be used.
  327. */
  328. int regulator_by_platname_list_autoset_and_enable(const char *list_platname[],
  329. int list_entries,
  330. struct udevice *list_devp[],
  331. bool verbose);
  332. #define regulator_list_autoset(namelist, entries, devlist, verbose) \
  333. regulator_by_platname_list_autoset_and_enable(namelist, entries, \
  334. devlist, verbose)
  335. /**
  336. * regulator_by_devname: returns the pointer to the pmic regulator device.
  337. * Search by name, found in regulator device's name.
  338. *
  339. * @devname - expected string for 'dev->name' of regulator device
  340. * @devp - returned pointer to the regulator device
  341. * Returns: 0 on success or negative value of errno.
  342. *
  343. * The returned 'regulator' device can be used with:
  344. * - regulator_get/set_*
  345. */
  346. int regulator_by_devname(const char *devname, struct udevice **devp);
  347. /**
  348. * regulator_by_platname: returns the pointer to the pmic regulator device.
  349. * Search by name, found in regulator uclass platdata.
  350. *
  351. * @platname - expected string for dm_regulator_uclass_platdata .name field
  352. * @devp - returned pointer to the regulator device
  353. * Returns: 0 on success or negative value of errno.
  354. *
  355. * The returned 'regulator' device can be used with:
  356. * - regulator_get/set_*
  357. */
  358. int regulator_by_platname(const char *platname, struct udevice **devp);
  359. #endif /* _INCLUDE_REGULATOR_H_ */