12e192b24SSimon Glass /* 22e192b24SSimon Glass * Copyright (C) 2014-2015 Samsung Electronics 32e192b24SSimon Glass * Przemyslaw Marczak <p.marczak@samsung.com> 42e192b24SSimon Glass * 52e192b24SSimon Glass * SPDX-License-Identifier: GPL-2.0+ 62e192b24SSimon Glass */ 72e192b24SSimon Glass #include <common.h> 82e192b24SSimon Glass #include <errno.h> 92e192b24SSimon Glass #include <dm.h> 102e192b24SSimon Glass #include <dm/uclass-internal.h> 112e192b24SSimon Glass #include <power/pmic.h> 122e192b24SSimon Glass 132e192b24SSimon Glass #define LIMIT_DEV 32 142e192b24SSimon Glass #define LIMIT_PARENT 20 152e192b24SSimon Glass 162e192b24SSimon Glass static struct udevice *currdev; 172e192b24SSimon Glass 182e192b24SSimon Glass static int failure(int ret) 192e192b24SSimon Glass { 202e192b24SSimon Glass printf("Error: %d (%s)\n", ret, errno_str(ret)); 212e192b24SSimon Glass 222e192b24SSimon Glass return CMD_RET_FAILURE; 232e192b24SSimon Glass } 242e192b24SSimon Glass 252e192b24SSimon Glass static int do_dev(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 262e192b24SSimon Glass { 272e192b24SSimon Glass char *name; 282e192b24SSimon Glass int ret = -ENODEV; 292e192b24SSimon Glass 302e192b24SSimon Glass switch (argc) { 312e192b24SSimon Glass case 2: 322e192b24SSimon Glass name = argv[1]; 332e192b24SSimon Glass ret = pmic_get(name, &currdev); 342e192b24SSimon Glass if (ret) { 352e192b24SSimon Glass printf("Can't get PMIC: %s!\n", name); 362e192b24SSimon Glass return failure(ret); 372e192b24SSimon Glass } 382e192b24SSimon Glass case 1: 392e192b24SSimon Glass if (!currdev) { 402e192b24SSimon Glass printf("PMIC device is not set!\n\n"); 412e192b24SSimon Glass return CMD_RET_USAGE; 422e192b24SSimon Glass } 432e192b24SSimon Glass 442e192b24SSimon Glass printf("dev: %d @ %s\n", currdev->seq, currdev->name); 452e192b24SSimon Glass } 462e192b24SSimon Glass 472e192b24SSimon Glass return CMD_RET_SUCCESS; 482e192b24SSimon Glass } 492e192b24SSimon Glass 502e192b24SSimon Glass static int do_list(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 512e192b24SSimon Glass { 522e192b24SSimon Glass struct udevice *dev; 532e192b24SSimon Glass int ret; 542e192b24SSimon Glass 552e192b24SSimon Glass printf("| %-*.*s| %-*.*s| %s @ %s\n", 562e192b24SSimon Glass LIMIT_DEV, LIMIT_DEV, "Name", 572e192b24SSimon Glass LIMIT_PARENT, LIMIT_PARENT, "Parent name", 582e192b24SSimon Glass "Parent uclass", "seq"); 592e192b24SSimon Glass 602e192b24SSimon Glass for (ret = uclass_first_device(UCLASS_PMIC, &dev); dev; 612e192b24SSimon Glass ret = uclass_next_device(&dev)) { 622e192b24SSimon Glass if (ret) 632e192b24SSimon Glass continue; 642e192b24SSimon Glass 652e192b24SSimon Glass printf("| %-*.*s| %-*.*s| %s @ %d\n", 662e192b24SSimon Glass LIMIT_DEV, LIMIT_DEV, dev->name, 672e192b24SSimon Glass LIMIT_PARENT, LIMIT_PARENT, dev->parent->name, 682e192b24SSimon Glass dev_get_uclass_name(dev->parent), dev->parent->seq); 692e192b24SSimon Glass } 702e192b24SSimon Glass 712e192b24SSimon Glass if (ret) 722e192b24SSimon Glass return CMD_RET_FAILURE; 732e192b24SSimon Glass 742e192b24SSimon Glass return CMD_RET_SUCCESS; 752e192b24SSimon Glass } 762e192b24SSimon Glass 772e192b24SSimon Glass static int do_dump(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 782e192b24SSimon Glass { 792e192b24SSimon Glass struct udevice *dev; 802e192b24SSimon Glass uint8_t value; 812e192b24SSimon Glass uint reg; 822e192b24SSimon Glass int ret; 832e192b24SSimon Glass 842e192b24SSimon Glass if (!currdev) { 852e192b24SSimon Glass printf("First, set the PMIC device!\n"); 862e192b24SSimon Glass return CMD_RET_USAGE; 872e192b24SSimon Glass } 882e192b24SSimon Glass 892e192b24SSimon Glass dev = currdev; 902e192b24SSimon Glass 912e192b24SSimon Glass printf("Dump pmic: %s registers\n", dev->name); 922e192b24SSimon Glass 932e192b24SSimon Glass for (reg = 0; reg < pmic_reg_count(dev); reg++) { 942e192b24SSimon Glass ret = pmic_read(dev, reg, &value, 1); 952e192b24SSimon Glass if (ret) { 962e192b24SSimon Glass printf("Can't read register: %d\n", reg); 972e192b24SSimon Glass return failure(ret); 982e192b24SSimon Glass } 992e192b24SSimon Glass 1002e192b24SSimon Glass if (!(reg % 16)) 1012e192b24SSimon Glass printf("\n0x%02x: ", reg); 1022e192b24SSimon Glass 1032e192b24SSimon Glass printf("%2.2x ", value); 1042e192b24SSimon Glass } 1052e192b24SSimon Glass printf("\n"); 1062e192b24SSimon Glass 1072e192b24SSimon Glass return CMD_RET_SUCCESS; 1082e192b24SSimon Glass } 1092e192b24SSimon Glass 1102e192b24SSimon Glass static int do_read(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 1112e192b24SSimon Glass { 1122e192b24SSimon Glass struct udevice *dev; 1132e192b24SSimon Glass int regs, ret; 1142e192b24SSimon Glass uint8_t value; 1152e192b24SSimon Glass uint reg; 1162e192b24SSimon Glass 1172e192b24SSimon Glass if (!currdev) { 1182e192b24SSimon Glass printf("First, set the PMIC device!\n"); 1192e192b24SSimon Glass return CMD_RET_USAGE; 1202e192b24SSimon Glass } 1212e192b24SSimon Glass 1222e192b24SSimon Glass dev = currdev; 1232e192b24SSimon Glass 1242e192b24SSimon Glass if (argc != 2) 1252e192b24SSimon Glass return CMD_RET_USAGE; 1262e192b24SSimon Glass 1272e192b24SSimon Glass reg = simple_strtoul(argv[1], NULL, 0); 1282e192b24SSimon Glass regs = pmic_reg_count(dev); 1292e192b24SSimon Glass if (reg > regs) { 1302e192b24SSimon Glass printf("PMIC max reg: %d\n", regs); 1312e192b24SSimon Glass return failure(-EFAULT); 1322e192b24SSimon Glass } 1332e192b24SSimon Glass 1342e192b24SSimon Glass ret = pmic_read(dev, reg, &value, 1); 1352e192b24SSimon Glass if (ret) { 1362e192b24SSimon Glass printf("Can't read PMIC register: %d!\n", reg); 1372e192b24SSimon Glass return failure(ret); 1382e192b24SSimon Glass } 1392e192b24SSimon Glass 1402e192b24SSimon Glass printf("0x%02x: 0x%2.2x\n", reg, value); 1412e192b24SSimon Glass 1422e192b24SSimon Glass return CMD_RET_SUCCESS; 1432e192b24SSimon Glass } 1442e192b24SSimon Glass 1452e192b24SSimon Glass static int do_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 1462e192b24SSimon Glass { 1472e192b24SSimon Glass struct udevice *dev; 1482e192b24SSimon Glass int regs, ret; 1492e192b24SSimon Glass uint8_t value; 1502e192b24SSimon Glass uint reg; 1512e192b24SSimon Glass 1522e192b24SSimon Glass if (!currdev) { 1532e192b24SSimon Glass printf("First, set the PMIC device!\n"); 1542e192b24SSimon Glass return CMD_RET_USAGE; 1552e192b24SSimon Glass } 1562e192b24SSimon Glass 1572e192b24SSimon Glass dev = currdev; 1582e192b24SSimon Glass 1592e192b24SSimon Glass if (argc != 3) 1602e192b24SSimon Glass return CMD_RET_USAGE; 1612e192b24SSimon Glass 1622e192b24SSimon Glass reg = simple_strtoul(argv[1], NULL, 0); 1632e192b24SSimon Glass regs = pmic_reg_count(dev); 1642e192b24SSimon Glass if (reg > regs) { 1652e192b24SSimon Glass printf("PMIC max reg: %d\n", regs); 1662e192b24SSimon Glass return failure(-EFAULT); 1672e192b24SSimon Glass } 1682e192b24SSimon Glass 1692e192b24SSimon Glass value = simple_strtoul(argv[2], NULL, 0); 1702e192b24SSimon Glass 1712e192b24SSimon Glass ret = pmic_write(dev, reg, &value, 1); 1722e192b24SSimon Glass if (ret) { 1732e192b24SSimon Glass printf("Can't write PMIC register: %d!\n", reg); 1742e192b24SSimon Glass return failure(ret); 1752e192b24SSimon Glass } 1762e192b24SSimon Glass 1772e192b24SSimon Glass return CMD_RET_SUCCESS; 1782e192b24SSimon Glass } 1792e192b24SSimon Glass 1802e192b24SSimon Glass static cmd_tbl_t subcmd[] = { 1812e192b24SSimon Glass U_BOOT_CMD_MKENT(dev, 2, 1, do_dev, "", ""), 1822e192b24SSimon Glass U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""), 1832e192b24SSimon Glass U_BOOT_CMD_MKENT(dump, 1, 1, do_dump, "", ""), 1842e192b24SSimon Glass U_BOOT_CMD_MKENT(read, 2, 1, do_read, "", ""), 1852e192b24SSimon Glass U_BOOT_CMD_MKENT(write, 3, 1, do_write, "", ""), 1862e192b24SSimon Glass }; 1872e192b24SSimon Glass 1882e192b24SSimon Glass static int do_pmic(cmd_tbl_t *cmdtp, int flag, int argc, 1892e192b24SSimon Glass char * const argv[]) 1902e192b24SSimon Glass { 1912e192b24SSimon Glass cmd_tbl_t *cmd; 1922e192b24SSimon Glass 1932e192b24SSimon Glass argc--; 1942e192b24SSimon Glass argv++; 1952e192b24SSimon Glass 1962e192b24SSimon Glass cmd = find_cmd_tbl(argv[0], subcmd, ARRAY_SIZE(subcmd)); 1972e192b24SSimon Glass if (cmd == NULL || argc > cmd->maxargs) 1982e192b24SSimon Glass return CMD_RET_USAGE; 1992e192b24SSimon Glass 2002e192b24SSimon Glass return cmd->cmd(cmdtp, flag, argc, argv); 2012e192b24SSimon Glass } 2022e192b24SSimon Glass 2032e192b24SSimon Glass U_BOOT_CMD(pmic, CONFIG_SYS_MAXARGS, 1, do_pmic, 204*7e37a884SKlaus Goger "PMIC sub-system", 2052e192b24SSimon Glass "list - list pmic devices\n" 2062e192b24SSimon Glass "pmic dev [name] - show or [set] operating PMIC device\n" 2072e192b24SSimon Glass "pmic dump - dump registers\n" 2082e192b24SSimon Glass "pmic read address - read byte of register at address\n" 2092e192b24SSimon Glass "pmic write address - write byte to register at address\n" 2102e192b24SSimon Glass ); 211