regulator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. #include <common.h>
  8. #include <errno.h>
  9. #include <dm.h>
  10. #include <dm/uclass-internal.h>
  11. #include <power/regulator.h>
  12. #define LIMIT_DEVNAME 20
  13. #define LIMIT_OFNAME 32
  14. #define LIMIT_INFO 18
  15. static struct udevice *currdev;
  16. static int failure(int ret)
  17. {
  18. printf("Error: %d (%s)\n", ret, errno_str(ret));
  19. return CMD_RET_FAILURE;
  20. }
  21. static int do_dev(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  22. {
  23. struct dm_regulator_uclass_platdata *uc_pdata;
  24. const char *name;
  25. int ret = -ENXIO;
  26. switch (argc) {
  27. case 2:
  28. name = argv[1];
  29. ret = regulator_get_by_platname(name, &currdev);
  30. if (ret) {
  31. printf("Can't get the regulator: %s!\n", name);
  32. return failure(ret);
  33. }
  34. case 1:
  35. if (!currdev) {
  36. printf("Regulator device is not set!\n\n");
  37. return CMD_RET_USAGE;
  38. }
  39. uc_pdata = dev_get_uclass_platdata(currdev);
  40. if (!uc_pdata) {
  41. printf("%s: no regulator platform data!\n", currdev->name);
  42. return failure(ret);
  43. }
  44. printf("dev: %s @ %s\n", uc_pdata->name, currdev->name);
  45. }
  46. return CMD_RET_SUCCESS;
  47. }
  48. static int curr_dev_and_platdata(struct udevice **devp,
  49. struct dm_regulator_uclass_platdata **uc_pdata,
  50. bool allow_type_fixed)
  51. {
  52. *devp = NULL;
  53. *uc_pdata = NULL;
  54. if (!currdev) {
  55. printf("First, set the regulator device!\n");
  56. return CMD_RET_FAILURE;
  57. }
  58. *devp = currdev;
  59. *uc_pdata = dev_get_uclass_platdata(*devp);
  60. if (!*uc_pdata) {
  61. error("Regulator: %s - missing platform data!", currdev->name);
  62. return CMD_RET_FAILURE;
  63. }
  64. if (!allow_type_fixed && (*uc_pdata)->type == REGULATOR_TYPE_FIXED) {
  65. printf("Operation not allowed for fixed regulator!\n");
  66. return CMD_RET_FAILURE;
  67. }
  68. return CMD_RET_SUCCESS;
  69. }
  70. static int do_list(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  71. {
  72. struct dm_regulator_uclass_platdata *uc_pdata;
  73. struct udevice *dev;
  74. int ret;
  75. printf("| %-*.*s| %-*.*s| %s\n",
  76. LIMIT_DEVNAME, LIMIT_DEVNAME, "Device",
  77. LIMIT_OFNAME, LIMIT_OFNAME, "regulator-name",
  78. "Parent");
  79. for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
  80. ret = uclass_find_next_device(&dev)) {
  81. if (ret)
  82. continue;
  83. uc_pdata = dev_get_uclass_platdata(dev);
  84. printf("| %-*.*s| %-*.*s| %s\n",
  85. LIMIT_DEVNAME, LIMIT_DEVNAME, dev->name,
  86. LIMIT_OFNAME, LIMIT_OFNAME, uc_pdata->name,
  87. dev->parent->name);
  88. }
  89. return ret;
  90. }
  91. static int constraint(const char *name, int val, const char *val_name)
  92. {
  93. printf("%-*s", LIMIT_INFO, name);
  94. if (val < 0) {
  95. printf(" %s (err: %d)\n", errno_str(val), val);
  96. return val;
  97. }
  98. if (val_name)
  99. printf(" %d (%s)\n", val, val_name);
  100. else
  101. printf(" %d\n", val);
  102. return 0;
  103. }
  104. static const char *get_mode_name(struct dm_regulator_mode *mode,
  105. int mode_count,
  106. int mode_id)
  107. {
  108. while (mode_count--) {
  109. if (mode->id == mode_id)
  110. return mode->name;
  111. mode++;
  112. }
  113. return NULL;
  114. }
  115. static int do_info(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  116. {
  117. struct udevice *dev;
  118. struct dm_regulator_uclass_platdata *uc_pdata;
  119. struct dm_regulator_mode *modes;
  120. const char *parent_uc;
  121. int mode_count;
  122. int ret;
  123. int i;
  124. ret = curr_dev_and_platdata(&dev, &uc_pdata, true);
  125. if (ret)
  126. return ret;
  127. parent_uc = dev_get_uclass_name(dev->parent);
  128. printf("%s\n%-*s %s\n%-*s %s\n%-*s %s\n%-*s %s\n%-*s\n",
  129. "Regulator info:",
  130. LIMIT_INFO, "* regulator-name:", uc_pdata->name,
  131. LIMIT_INFO, "* device name:", dev->name,
  132. LIMIT_INFO, "* parent name:", dev->parent->name,
  133. LIMIT_INFO, "* parent uclass:", parent_uc,
  134. LIMIT_INFO, "* constraints:");
  135. constraint(" - min uV:", uc_pdata->min_uV, NULL);
  136. constraint(" - max uV:", uc_pdata->max_uV, NULL);
  137. constraint(" - min uA:", uc_pdata->min_uA, NULL);
  138. constraint(" - max uA:", uc_pdata->max_uA, NULL);
  139. constraint(" - always on:", uc_pdata->always_on,
  140. uc_pdata->always_on ? "true" : "false");
  141. constraint(" - boot on:", uc_pdata->boot_on,
  142. uc_pdata->boot_on ? "true" : "false");
  143. mode_count = regulator_mode(dev, &modes);
  144. constraint("* op modes:", mode_count, NULL);
  145. for (i = 0; i < mode_count; i++, modes++)
  146. constraint(" - mode id:", modes->id, modes->name);
  147. return CMD_RET_SUCCESS;
  148. }
  149. static void do_status_detail(struct udevice *dev,
  150. struct dm_regulator_uclass_platdata *uc_pdata)
  151. {
  152. int current, value, mode;
  153. const char *mode_name;
  154. bool enabled;
  155. printf("Regulator %s status:\n", uc_pdata->name);
  156. enabled = regulator_get_enable(dev);
  157. constraint(" * enable:", enabled, enabled ? "true" : "false");
  158. value = regulator_get_value(dev);
  159. constraint(" * value uV:", value, NULL);
  160. current = regulator_get_current(dev);
  161. constraint(" * current uA:", current, NULL);
  162. mode = regulator_get_mode(dev);
  163. mode_name = get_mode_name(uc_pdata->mode, uc_pdata->mode_count, mode);
  164. constraint(" * mode id:", mode, mode_name);
  165. }
  166. static void do_status_line(struct udevice *dev)
  167. {
  168. struct dm_regulator_uclass_platdata *pdata;
  169. int current, value, mode;
  170. const char *mode_name;
  171. bool enabled;
  172. pdata = dev_get_uclass_platdata(dev);
  173. enabled = regulator_get_enable(dev);
  174. value = regulator_get_value(dev);
  175. current = regulator_get_current(dev);
  176. mode = regulator_get_mode(dev);
  177. mode_name = get_mode_name(pdata->mode, pdata->mode_count, mode);
  178. printf("%-20s %-10s ", pdata->name, enabled ? "enabled" : "disabled");
  179. if (value >= 0)
  180. printf("%10d ", value);
  181. else
  182. printf("%10s ", "-");
  183. if (current >= 0)
  184. printf("%10d ", current);
  185. else
  186. printf("%10s ", "-");
  187. if (mode >= 0)
  188. printf("%-10s", mode_name);
  189. else
  190. printf("%-10s", "-");
  191. printf("\n");
  192. }
  193. static int do_status(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  194. {
  195. struct dm_regulator_uclass_platdata *uc_pdata;
  196. struct udevice *dev;
  197. int ret;
  198. if (currdev && (argc < 2 || strcmp(argv[1], "-a"))) {
  199. ret = curr_dev_and_platdata(&dev, &uc_pdata, true);
  200. if (ret)
  201. return CMD_RET_FAILURE;
  202. do_status_detail(dev, uc_pdata);
  203. return 0;
  204. }
  205. /* Show all of them in a list, probing them as needed */
  206. printf("%-20s %-10s %10s %10s %-10s\n", "Name", "Enabled", "uV", "mA",
  207. "Mode");
  208. for (ret = uclass_first_device(UCLASS_REGULATOR, &dev); dev;
  209. ret = uclass_next_device(&dev))
  210. do_status_line(dev);
  211. return CMD_RET_SUCCESS;
  212. }
  213. static int do_value(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  214. {
  215. struct udevice *dev;
  216. struct dm_regulator_uclass_platdata *uc_pdata;
  217. int value;
  218. int force;
  219. int ret;
  220. ret = curr_dev_and_platdata(&dev, &uc_pdata, argc == 1);
  221. if (ret)
  222. return ret;
  223. if (argc == 1) {
  224. ret = regulator_get_value(dev);
  225. if (ret < 0) {
  226. printf("Regulator: %s - can't get the Voltage!\n",
  227. uc_pdata->name);
  228. return failure(ret);
  229. }
  230. printf("%d uV\n", ret);
  231. return CMD_RET_SUCCESS;
  232. }
  233. if (argc == 3)
  234. force = !strcmp("-f", argv[2]);
  235. else
  236. force = 0;
  237. value = simple_strtoul(argv[1], NULL, 0);
  238. if ((value < uc_pdata->min_uV || value > uc_pdata->max_uV) && !force) {
  239. printf("Value exceeds regulator constraint limits %d..%d uV\n",
  240. uc_pdata->min_uV, uc_pdata->max_uV);
  241. return CMD_RET_FAILURE;
  242. }
  243. ret = regulator_set_value(dev, value);
  244. if (ret) {
  245. printf("Regulator: %s - can't set the Voltage!\n",
  246. uc_pdata->name);
  247. return failure(ret);
  248. }
  249. return CMD_RET_SUCCESS;
  250. }
  251. static int do_current(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  252. {
  253. struct udevice *dev;
  254. struct dm_regulator_uclass_platdata *uc_pdata;
  255. int current;
  256. int ret;
  257. ret = curr_dev_and_platdata(&dev, &uc_pdata, argc == 1);
  258. if (ret)
  259. return ret;
  260. if (argc == 1) {
  261. ret = regulator_get_current(dev);
  262. if (ret < 0) {
  263. printf("Regulator: %s - can't get the Current!\n",
  264. uc_pdata->name);
  265. return failure(ret);
  266. }
  267. printf("%d uA\n", ret);
  268. return CMD_RET_SUCCESS;
  269. }
  270. current = simple_strtoul(argv[1], NULL, 0);
  271. if (current < uc_pdata->min_uA || current > uc_pdata->max_uA) {
  272. printf("Current exceeds regulator constraint limits\n");
  273. return CMD_RET_FAILURE;
  274. }
  275. ret = regulator_set_current(dev, current);
  276. if (ret) {
  277. printf("Regulator: %s - can't set the Current!\n",
  278. uc_pdata->name);
  279. return failure(ret);
  280. }
  281. return CMD_RET_SUCCESS;
  282. }
  283. static int do_mode(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  284. {
  285. struct udevice *dev;
  286. struct dm_regulator_uclass_platdata *uc_pdata;
  287. int mode;
  288. int ret;
  289. ret = curr_dev_and_platdata(&dev, &uc_pdata, false);
  290. if (ret)
  291. return ret;
  292. if (argc == 1) {
  293. ret = regulator_get_mode(dev);
  294. if (ret < 0) {
  295. printf("Regulator: %s - can't get the operation mode!\n",
  296. uc_pdata->name);
  297. return failure(ret);
  298. }
  299. printf("mode id: %d\n", ret);
  300. return CMD_RET_SUCCESS;
  301. }
  302. mode = simple_strtoul(argv[1], NULL, 0);
  303. ret = regulator_set_mode(dev, mode);
  304. if (ret) {
  305. printf("Regulator: %s - can't set the operation mode!\n",
  306. uc_pdata->name);
  307. return failure(ret);
  308. }
  309. return CMD_RET_SUCCESS;
  310. }
  311. static int do_enable(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  312. {
  313. struct udevice *dev;
  314. struct dm_regulator_uclass_platdata *uc_pdata;
  315. int ret;
  316. ret = curr_dev_and_platdata(&dev, &uc_pdata, true);
  317. if (ret)
  318. return ret;
  319. ret = regulator_set_enable(dev, true);
  320. if (ret) {
  321. printf("Regulator: %s - can't enable!\n", uc_pdata->name);
  322. return failure(ret);
  323. }
  324. return CMD_RET_SUCCESS;
  325. }
  326. static int do_disable(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  327. {
  328. struct udevice *dev;
  329. struct dm_regulator_uclass_platdata *uc_pdata;
  330. int ret;
  331. ret = curr_dev_and_platdata(&dev, &uc_pdata, true);
  332. if (ret)
  333. return ret;
  334. ret = regulator_set_enable(dev, false);
  335. if (ret) {
  336. printf("Regulator: %s - can't disable!\n", uc_pdata->name);
  337. return failure(ret);
  338. }
  339. return CMD_RET_SUCCESS;
  340. }
  341. static cmd_tbl_t subcmd[] = {
  342. U_BOOT_CMD_MKENT(dev, 2, 1, do_dev, "", ""),
  343. U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""),
  344. U_BOOT_CMD_MKENT(info, 2, 1, do_info, "", ""),
  345. U_BOOT_CMD_MKENT(status, 2, 1, do_status, "", ""),
  346. U_BOOT_CMD_MKENT(value, 3, 1, do_value, "", ""),
  347. U_BOOT_CMD_MKENT(current, 3, 1, do_current, "", ""),
  348. U_BOOT_CMD_MKENT(mode, 2, 1, do_mode, "", ""),
  349. U_BOOT_CMD_MKENT(enable, 1, 1, do_enable, "", ""),
  350. U_BOOT_CMD_MKENT(disable, 1, 1, do_disable, "", ""),
  351. };
  352. static int do_regulator(cmd_tbl_t *cmdtp, int flag, int argc,
  353. char * const argv[])
  354. {
  355. cmd_tbl_t *cmd;
  356. argc--;
  357. argv++;
  358. cmd = find_cmd_tbl(argv[0], subcmd, ARRAY_SIZE(subcmd));
  359. if (cmd == NULL || argc > cmd->maxargs)
  360. return CMD_RET_USAGE;
  361. return cmd->cmd(cmdtp, flag, argc, argv);
  362. }
  363. U_BOOT_CMD(regulator, CONFIG_SYS_MAXARGS, 1, do_regulator,
  364. "uclass operations",
  365. "list - list UCLASS regulator devices\n"
  366. "regulator dev [regulator-name] - show/[set] operating regulator device\n"
  367. "regulator info - print constraints info\n"
  368. "regulator status [-a] - print operating status [for all]\n"
  369. "regulator value [val] [-f] - print/[set] voltage value [uV] (force)\n"
  370. "regulator current [val] - print/[set] current value [uA]\n"
  371. "regulator mode [id] - print/[set] operating mode id\n"
  372. "regulator enable - enable the regulator output\n"
  373. "regulator disable - disable the regulator output\n"
  374. );