xref: /openbmc/u-boot/cmd/pmic.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom 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 
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 
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 
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 
762e192b24SSimon Glass static int do_dump(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
772e192b24SSimon Glass {
782e192b24SSimon Glass 	struct udevice *dev;
792e192b24SSimon Glass 	uint8_t value;
802e192b24SSimon Glass 	uint reg;
812e192b24SSimon Glass 	int ret;
822e192b24SSimon Glass 
832e192b24SSimon Glass 	if (!currdev) {
842e192b24SSimon Glass 		printf("First, set the PMIC device!\n");
852e192b24SSimon Glass 		return CMD_RET_USAGE;
862e192b24SSimon Glass 	}
872e192b24SSimon Glass 
882e192b24SSimon Glass 	dev = currdev;
892e192b24SSimon Glass 
902e192b24SSimon Glass 	printf("Dump pmic: %s registers\n", dev->name);
912e192b24SSimon Glass 
922e192b24SSimon Glass 	for (reg = 0; reg < pmic_reg_count(dev); reg++) {
932e192b24SSimon Glass 		ret = pmic_read(dev, reg, &value, 1);
942e192b24SSimon Glass 		if (ret) {
952e192b24SSimon Glass 			printf("Can't read register: %d\n", reg);
962e192b24SSimon Glass 			return failure(ret);
972e192b24SSimon Glass 		}
982e192b24SSimon Glass 
992e192b24SSimon Glass 		if (!(reg % 16))
1002e192b24SSimon Glass 			printf("\n0x%02x: ", reg);
1012e192b24SSimon Glass 
1022e192b24SSimon Glass 		printf("%2.2x ", value);
1032e192b24SSimon Glass 	}
1042e192b24SSimon Glass 	printf("\n");
1052e192b24SSimon Glass 
1062e192b24SSimon Glass 	return CMD_RET_SUCCESS;
1072e192b24SSimon Glass }
1082e192b24SSimon Glass 
1092e192b24SSimon Glass static int do_read(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1102e192b24SSimon Glass {
1112e192b24SSimon Glass 	struct udevice *dev;
1122e192b24SSimon Glass 	int regs, ret;
1132e192b24SSimon Glass 	uint8_t value;
1142e192b24SSimon Glass 	uint reg;
1152e192b24SSimon Glass 
1162e192b24SSimon Glass 	if (!currdev) {
1172e192b24SSimon Glass 		printf("First, set the PMIC device!\n");
1182e192b24SSimon Glass 		return CMD_RET_USAGE;
1192e192b24SSimon Glass 	}
1202e192b24SSimon Glass 
1212e192b24SSimon Glass 	dev = currdev;
1222e192b24SSimon Glass 
1232e192b24SSimon Glass 	if (argc != 2)
1242e192b24SSimon Glass 		return CMD_RET_USAGE;
1252e192b24SSimon Glass 
1262e192b24SSimon Glass 	reg = simple_strtoul(argv[1], NULL, 0);
1272e192b24SSimon Glass 	regs = pmic_reg_count(dev);
1282e192b24SSimon Glass 	if (reg > regs) {
1292e192b24SSimon Glass 		printf("PMIC max reg: %d\n", regs);
1302e192b24SSimon Glass 		return failure(-EFAULT);
1312e192b24SSimon Glass 	}
1322e192b24SSimon Glass 
1332e192b24SSimon Glass 	ret = pmic_read(dev, reg, &value, 1);
1342e192b24SSimon Glass 	if (ret) {
1352e192b24SSimon Glass 		printf("Can't read PMIC register: %d!\n", reg);
1362e192b24SSimon Glass 		return failure(ret);
1372e192b24SSimon Glass 	}
1382e192b24SSimon Glass 
1392e192b24SSimon Glass 	printf("0x%02x: 0x%2.2x\n", reg, value);
1402e192b24SSimon Glass 
1412e192b24SSimon Glass 	return CMD_RET_SUCCESS;
1422e192b24SSimon Glass }
1432e192b24SSimon Glass 
1442e192b24SSimon Glass static int do_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1452e192b24SSimon Glass {
1462e192b24SSimon Glass 	struct udevice *dev;
1472e192b24SSimon Glass 	int regs, ret;
1482e192b24SSimon Glass 	uint8_t value;
1492e192b24SSimon Glass 	uint reg;
1502e192b24SSimon Glass 
1512e192b24SSimon Glass 	if (!currdev) {
1522e192b24SSimon Glass 		printf("First, set the PMIC device!\n");
1532e192b24SSimon Glass 		return CMD_RET_USAGE;
1542e192b24SSimon Glass 	}
1552e192b24SSimon Glass 
1562e192b24SSimon Glass 	dev = currdev;
1572e192b24SSimon Glass 
1582e192b24SSimon Glass 	if (argc != 3)
1592e192b24SSimon Glass 		return CMD_RET_USAGE;
1602e192b24SSimon Glass 
1612e192b24SSimon Glass 	reg = simple_strtoul(argv[1], NULL, 0);
1622e192b24SSimon Glass 	regs = pmic_reg_count(dev);
1632e192b24SSimon Glass 	if (reg > regs) {
1642e192b24SSimon Glass 		printf("PMIC max reg: %d\n", regs);
1652e192b24SSimon Glass 		return failure(-EFAULT);
1662e192b24SSimon Glass 	}
1672e192b24SSimon Glass 
1682e192b24SSimon Glass 	value = simple_strtoul(argv[2], NULL, 0);
1692e192b24SSimon Glass 
1702e192b24SSimon Glass 	ret = pmic_write(dev, reg, &value, 1);
1712e192b24SSimon Glass 	if (ret) {
1722e192b24SSimon Glass 		printf("Can't write PMIC register: %d!\n", reg);
1732e192b24SSimon Glass 		return failure(ret);
1742e192b24SSimon Glass 	}
1752e192b24SSimon Glass 
1762e192b24SSimon Glass 	return CMD_RET_SUCCESS;
1772e192b24SSimon Glass }
1782e192b24SSimon Glass 
1792e192b24SSimon Glass static cmd_tbl_t subcmd[] = {
1802e192b24SSimon Glass 	U_BOOT_CMD_MKENT(dev, 2, 1, do_dev, "", ""),
1812e192b24SSimon Glass 	U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""),
1822e192b24SSimon Glass 	U_BOOT_CMD_MKENT(dump, 1, 1, do_dump, "", ""),
1832e192b24SSimon Glass 	U_BOOT_CMD_MKENT(read, 2, 1, do_read, "", ""),
1842e192b24SSimon Glass 	U_BOOT_CMD_MKENT(write, 3, 1, do_write, "", ""),
1852e192b24SSimon Glass };
1862e192b24SSimon Glass 
1872e192b24SSimon Glass static int do_pmic(cmd_tbl_t *cmdtp, int flag, int argc,
1882e192b24SSimon Glass 			char * const argv[])
1892e192b24SSimon Glass {
1902e192b24SSimon Glass 	cmd_tbl_t *cmd;
1912e192b24SSimon Glass 
1922e192b24SSimon Glass 	argc--;
1932e192b24SSimon Glass 	argv++;
1942e192b24SSimon Glass 
1952e192b24SSimon Glass 	cmd = find_cmd_tbl(argv[0], subcmd, ARRAY_SIZE(subcmd));
1962e192b24SSimon Glass 	if (cmd == NULL || argc > cmd->maxargs)
1972e192b24SSimon Glass 		return CMD_RET_USAGE;
1982e192b24SSimon Glass 
1992e192b24SSimon Glass 	return cmd->cmd(cmdtp, flag, argc, argv);
2002e192b24SSimon Glass }
2012e192b24SSimon Glass 
2022e192b24SSimon Glass U_BOOT_CMD(pmic, CONFIG_SYS_MAXARGS, 1, do_pmic,
2037e37a884SKlaus Goger 	"PMIC sub-system",
2042e192b24SSimon Glass 	"list          - list pmic devices\n"
2052e192b24SSimon Glass 	"pmic dev [name]    - show or [set] operating PMIC device\n"
2062e192b24SSimon Glass 	"pmic dump          - dump registers\n"
2072e192b24SSimon Glass 	"pmic read address  - read byte of register at address\n"
2082e192b24SSimon Glass 	"pmic write address - write byte to register at address\n"
2092e192b24SSimon Glass );
210