regulator.c 11 KB

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