regulator.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Tests for the driver model regulator API
  3. *
  4. * Copyright (c) 2015 Samsung Electronics
  5. * Przemyslaw Marczak <p.marczak@samsung.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <errno.h>
  11. #include <dm.h>
  12. #include <fdtdec.h>
  13. #include <malloc.h>
  14. #include <dm/device-internal.h>
  15. #include <dm/root.h>
  16. #include <dm/util.h>
  17. #include <dm/test.h>
  18. #include <dm/uclass-internal.h>
  19. #include <power/pmic.h>
  20. #include <power/regulator.h>
  21. #include <power/sandbox_pmic.h>
  22. #include <test/ut.h>
  23. DECLARE_GLOBAL_DATA_PTR;
  24. enum {
  25. BUCK1,
  26. BUCK2,
  27. LDO1,
  28. LDO2,
  29. OUTPUT_COUNT,
  30. };
  31. enum {
  32. DEVNAME = 0,
  33. PLATNAME,
  34. OUTPUT_NAME_COUNT,
  35. };
  36. static const char *regulator_names[OUTPUT_COUNT][OUTPUT_NAME_COUNT] = {
  37. /* devname, platname */
  38. { SANDBOX_BUCK1_DEVNAME, SANDBOX_BUCK1_PLATNAME },
  39. { SANDBOX_BUCK2_DEVNAME, SANDBOX_BUCK2_PLATNAME },
  40. { SANDBOX_LDO1_DEVNAME, SANDBOX_LDO1_PLATNAME},
  41. { SANDBOX_LDO2_DEVNAME, SANDBOX_LDO2_PLATNAME},
  42. };
  43. /* Test regulator get method */
  44. static int dm_test_power_regulator_get(struct unit_test_state *uts)
  45. {
  46. struct dm_regulator_uclass_platdata *uc_pdata;
  47. struct udevice *dev_by_devname;
  48. struct udevice *dev_by_platname;
  49. const char *devname;
  50. const char *platname;
  51. int i;
  52. for (i = 0; i < OUTPUT_COUNT; i++) {
  53. /*
  54. * Do the test for each regulator's devname and platname,
  55. * which are related to a single device.
  56. */
  57. devname = regulator_names[i][DEVNAME];
  58. platname = regulator_names[i][PLATNAME];
  59. /*
  60. * Check, that regulator_get_by_devname() function, returns
  61. * a device with the name equal to the requested one.
  62. */
  63. ut_assertok(regulator_get_by_devname(devname, &dev_by_devname));
  64. ut_asserteq_str(devname, dev_by_devname->name);
  65. /*
  66. * Check, that regulator_get_by_platname() function, returns
  67. * a device with the name equal to the requested one.
  68. */
  69. ut_assertok(regulator_get_by_platname(platname, &dev_by_platname));
  70. uc_pdata = dev_get_uclass_platdata(dev_by_platname);
  71. ut_assert(uc_pdata);
  72. ut_asserteq_str(platname, uc_pdata->name);
  73. /*
  74. * Check, that the pointers returned by both get functions,
  75. * points to the same regulator device.
  76. */
  77. ut_asserteq_ptr(dev_by_devname, dev_by_platname);
  78. }
  79. return 0;
  80. }
  81. DM_TEST(dm_test_power_regulator_get, DM_TESTF_SCAN_FDT);
  82. /* Test regulator set and get Voltage method */
  83. static int dm_test_power_regulator_set_get_voltage(struct unit_test_state *uts)
  84. {
  85. struct dm_regulator_uclass_platdata *uc_pdata;
  86. struct udevice *dev;
  87. const char *platname;
  88. int val_set, val_get;
  89. /* Set and get Voltage of BUCK1 - set to 'min' constraint */
  90. platname = regulator_names[BUCK1][PLATNAME];
  91. ut_assertok(regulator_get_by_platname(platname, &dev));
  92. uc_pdata = dev_get_uclass_platdata(dev);
  93. ut_assert(uc_pdata);
  94. val_set = uc_pdata->min_uV;
  95. ut_assertok(regulator_set_value(dev, val_set));
  96. val_get = regulator_get_value(dev);
  97. ut_assert(val_get >= 0);
  98. ut_asserteq(val_set, val_get);
  99. return 0;
  100. }
  101. DM_TEST(dm_test_power_regulator_set_get_voltage, DM_TESTF_SCAN_FDT);
  102. /* Test regulator set and get Current method */
  103. static int dm_test_power_regulator_set_get_current(struct unit_test_state *uts)
  104. {
  105. struct dm_regulator_uclass_platdata *uc_pdata;
  106. struct udevice *dev;
  107. const char *platname;
  108. int val_set, val_get;
  109. /* Set and get the Current of LDO1 - set to 'min' constraint */
  110. platname = regulator_names[LDO1][PLATNAME];
  111. ut_assertok(regulator_get_by_platname(platname, &dev));
  112. uc_pdata = dev_get_uclass_platdata(dev);
  113. ut_assert(uc_pdata);
  114. val_set = uc_pdata->min_uA;
  115. ut_assertok(regulator_set_current(dev, val_set));
  116. val_get = regulator_get_current(dev);
  117. ut_assert(val_get >= 0);
  118. ut_asserteq(val_set, val_get);
  119. /* Check LDO2 current limit constraints - should be -ENODATA */
  120. platname = regulator_names[LDO2][PLATNAME];
  121. ut_assertok(regulator_get_by_platname(platname, &dev));
  122. uc_pdata = dev_get_uclass_platdata(dev);
  123. ut_assert(uc_pdata);
  124. ut_asserteq(-ENODATA, uc_pdata->min_uA);
  125. ut_asserteq(-ENODATA, uc_pdata->max_uA);
  126. /* Try set the Current of LDO2 - should return -ENOSYS */
  127. ut_asserteq(-ENOSYS, regulator_set_current(dev, 0));
  128. return 0;
  129. }
  130. DM_TEST(dm_test_power_regulator_set_get_current, DM_TESTF_SCAN_FDT);
  131. /* Test regulator set and get Enable method */
  132. static int dm_test_power_regulator_set_get_enable(struct unit_test_state *uts)
  133. {
  134. const char *platname;
  135. struct udevice *dev;
  136. bool val_set = true;
  137. /* Set the Enable of LDO1 - default is disabled */
  138. platname = regulator_names[LDO1][PLATNAME];
  139. ut_assertok(regulator_get_by_platname(platname, &dev));
  140. ut_assertok(regulator_set_enable(dev, val_set));
  141. /* Get the Enable state of LDO1 and compare it with the requested one */
  142. ut_asserteq(regulator_get_enable(dev), val_set);
  143. return 0;
  144. }
  145. DM_TEST(dm_test_power_regulator_set_get_enable, DM_TESTF_SCAN_FDT);
  146. /* Test regulator set and get mode method */
  147. static int dm_test_power_regulator_set_get_mode(struct unit_test_state *uts)
  148. {
  149. const char *platname;
  150. struct udevice *dev;
  151. int val_set = LDO_OM_SLEEP;
  152. /* Set the mode id to LDO_OM_SLEEP of LDO1 - default is LDO_OM_OFF */
  153. platname = regulator_names[LDO1][PLATNAME];
  154. ut_assertok(regulator_get_by_platname(platname, &dev));
  155. ut_assertok(regulator_set_mode(dev, val_set));
  156. /* Get the mode id of LDO1 and compare it with the requested one */
  157. ut_asserteq(regulator_get_mode(dev), val_set);
  158. return 0;
  159. }
  160. DM_TEST(dm_test_power_regulator_set_get_mode, DM_TESTF_SCAN_FDT);
  161. /* Test regulator autoset method */
  162. static int dm_test_power_regulator_autoset(struct unit_test_state *uts)
  163. {
  164. const char *platname;
  165. struct udevice *dev, *dev_autoset;
  166. /*
  167. * Test the BUCK1 with fdt properties
  168. * - min-microvolt = max-microvolt = 1200000
  169. * - min-microamp = max-microamp = 200000
  170. * - always-on = set
  171. * - boot-on = not set
  172. * Expected output state: uV=1200000; uA=200000; output enabled
  173. */
  174. platname = regulator_names[BUCK1][PLATNAME];
  175. ut_assertok(regulator_autoset_by_name(platname, &dev_autoset));
  176. /* Check, that the returned device is proper */
  177. ut_assertok(regulator_get_by_platname(platname, &dev));
  178. ut_asserteq_ptr(dev, dev_autoset);
  179. /* Check the setup after autoset */
  180. ut_asserteq(regulator_get_value(dev),
  181. SANDBOX_BUCK1_AUTOSET_EXPECTED_UV);
  182. ut_asserteq(regulator_get_current(dev),
  183. SANDBOX_BUCK1_AUTOSET_EXPECTED_UA);
  184. ut_asserteq(regulator_get_enable(dev),
  185. SANDBOX_BUCK1_AUTOSET_EXPECTED_ENABLE);
  186. return 0;
  187. }
  188. DM_TEST(dm_test_power_regulator_autoset, DM_TESTF_SCAN_FDT);
  189. /*
  190. * Struct setting: to keep the expected output settings.
  191. * @voltage: Voltage value [uV]
  192. * @current: Current value [uA]
  193. * @enable: output enable state: true/false
  194. */
  195. struct setting {
  196. int voltage;
  197. int current;
  198. bool enable;
  199. };
  200. /*
  201. * platname_list: an array of regulator platform names.
  202. * For testing regulator_list_autoset() for outputs:
  203. * - LDO1
  204. * - LDO2
  205. */
  206. static const char *platname_list[] = {
  207. SANDBOX_LDO1_PLATNAME,
  208. SANDBOX_LDO2_PLATNAME,
  209. NULL,
  210. };
  211. /*
  212. * expected_setting_list: an array of regulator output setting, expected after
  213. * call of the regulator_list_autoset() for the "platname_list" array.
  214. * For testing results of regulator_list_autoset() for outputs:
  215. * - LDO1
  216. * - LDO2
  217. * The settings are defined in: include/power/sandbox_pmic.h
  218. */
  219. static const struct setting expected_setting_list[] = {
  220. [0] = { /* LDO1 */
  221. .voltage = SANDBOX_LDO1_AUTOSET_EXPECTED_UV,
  222. .current = SANDBOX_LDO1_AUTOSET_EXPECTED_UA,
  223. .enable = SANDBOX_LDO1_AUTOSET_EXPECTED_ENABLE,
  224. },
  225. [1] = { /* LDO2 */
  226. .voltage = SANDBOX_LDO2_AUTOSET_EXPECTED_UV,
  227. .current = SANDBOX_LDO2_AUTOSET_EXPECTED_UA,
  228. .enable = SANDBOX_LDO2_AUTOSET_EXPECTED_ENABLE,
  229. },
  230. };
  231. static int list_count = ARRAY_SIZE(expected_setting_list);
  232. /* Test regulator list autoset method */
  233. static int dm_test_power_regulator_autoset_list(struct unit_test_state *uts)
  234. {
  235. struct udevice *dev_list[2], *dev;
  236. int i;
  237. /*
  238. * Test the settings of the regulator list:
  239. * LDO1 with fdt properties:
  240. * - min-microvolt = max-microvolt = 1800000
  241. * - min-microamp = max-microamp = 100000
  242. * - always-on = not set
  243. * - boot-on = set
  244. * Expected output state: uV=1800000; uA=100000; output enabled
  245. *
  246. * LDO2 with fdt properties:
  247. * - min-microvolt = max-microvolt = 3300000
  248. * - always-on = not set
  249. * - boot-on = not set
  250. * Expected output state: uV=300000(default); output disabled(default)
  251. * The expected settings are defined in: include/power/sandbox_pmic.h.
  252. */
  253. ut_assertok(regulator_list_autoset(platname_list, dev_list, false));
  254. for (i = 0; i < list_count; i++) {
  255. /* Check, that the returned device is non-NULL */
  256. ut_assert(dev_list[i]);
  257. /* Check, that the returned device is proper */
  258. ut_assertok(regulator_get_by_platname(platname_list[i], &dev));
  259. ut_asserteq_ptr(dev_list[i], dev);
  260. /* Check, that regulator output Voltage value is as expected */
  261. ut_asserteq(regulator_get_value(dev_list[i]),
  262. expected_setting_list[i].voltage);
  263. /* Check, that regulator output Current value is as expected */
  264. ut_asserteq(regulator_get_current(dev_list[i]),
  265. expected_setting_list[i].current);
  266. /* Check, that regulator output Enable state is as expected */
  267. ut_asserteq(regulator_get_enable(dev_list[i]),
  268. expected_setting_list[i].enable);
  269. }
  270. return 0;
  271. }
  272. DM_TEST(dm_test_power_regulator_autoset_list, DM_TESTF_SCAN_FDT);