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