regulator.h 14 KB

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