regulator.h 17 KB

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