blob: c9e9730adf909e30fd8420145b7d66a80cd6cbaf [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Przemyslaw Marczak4d569772015-04-20 20:07:43 +02002/*
3 * Copyright (C) 2014-2015 Samsung Electronics
4 * Przemyslaw Marczak <p.marczak@samsung.com>
Przemyslaw Marczak4d569772015-04-20 20:07:43 +02005 */
6#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -06007#include <command.h>
Przemyslaw Marczak4d569772015-04-20 20:07:43 +02008#include <errno.h>
9#include <dm.h>
10#include <dm/uclass-internal.h>
11#include <power/pmic.h>
12
Przemyslaw Marczak34f922c2015-05-13 13:38:28 +020013#define LIMIT_DEV 32
14#define LIMIT_PARENT 20
Przemyslaw Marczak4d569772015-04-20 20:07:43 +020015
16static struct udevice *currdev;
17
Przemyslaw Marczak34f922c2015-05-13 13:38:28 +020018static int failure(int ret)
Przemyslaw Marczak4d569772015-04-20 20:07:43 +020019{
Przemyslaw Marczak34f922c2015-05-13 13:38:28 +020020 printf("Error: %d (%s)\n", ret, errno_str(ret));
Przemyslaw Marczak4d569772015-04-20 20:07:43 +020021
Przemyslaw Marczak34f922c2015-05-13 13:38:28 +020022 return CMD_RET_FAILURE;
Przemyslaw Marczak4d569772015-04-20 20:07:43 +020023}
24
Simon Glassed38aef2020-05-10 11:40:03 -060025static int do_dev(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Przemyslaw Marczak4d569772015-04-20 20:07:43 +020026{
Przemyslaw Marczak34f922c2015-05-13 13:38:28 +020027 char *name;
28 int ret = -ENODEV;
Przemyslaw Marczak4d569772015-04-20 20:07:43 +020029
30 switch (argc) {
31 case 2:
Przemyslaw Marczak34f922c2015-05-13 13:38:28 +020032 name = argv[1];
33 ret = pmic_get(name, &currdev);
34 if (ret) {
35 printf("Can't get PMIC: %s!\n", name);
36 return failure(ret);
37 }
Przemyslaw Marczak4d569772015-04-20 20:07:43 +020038 case 1:
Przemyslaw Marczak34f922c2015-05-13 13:38:28 +020039 if (!currdev) {
40 printf("PMIC device is not set!\n\n");
41 return CMD_RET_USAGE;
42 }
Przemyslaw Marczak4d569772015-04-20 20:07:43 +020043
Simon Glass75e534b2020-12-16 21:20:07 -070044 printf("dev: %d @ %s\n", dev_seq(currdev), currdev->name);
Przemyslaw Marczak4d569772015-04-20 20:07:43 +020045 }
46
47 return CMD_RET_SUCCESS;
Przemyslaw Marczak4d569772015-04-20 20:07:43 +020048}
49
Simon Glassed38aef2020-05-10 11:40:03 -060050static int do_list(struct cmd_tbl *cmdtp, int flag, int argc,
51 char *const argv[])
Przemyslaw Marczak4d569772015-04-20 20:07:43 +020052{
53 struct udevice *dev;
Michal Suchanek1d524ae2022-10-12 21:57:58 +020054 int ret, err = 0;
Przemyslaw Marczak4d569772015-04-20 20:07:43 +020055
Przemyslaw Marczak34f922c2015-05-13 13:38:28 +020056 printf("| %-*.*s| %-*.*s| %s @ %s\n",
57 LIMIT_DEV, LIMIT_DEV, "Name",
58 LIMIT_PARENT, LIMIT_PARENT, "Parent name",
Przemyslaw Marczak4d569772015-04-20 20:07:43 +020059 "Parent uclass", "seq");
60
Michal Suchanek1d524ae2022-10-12 21:57:58 +020061 for (ret = uclass_first_device_check(UCLASS_PMIC, &dev); dev;
62 ret = uclass_next_device_check(&dev)) {
Przemyslaw Marczak34f922c2015-05-13 13:38:28 +020063 if (ret)
Michal Suchanek1d524ae2022-10-12 21:57:58 +020064 err = ret;
Przemyslaw Marczak34f922c2015-05-13 13:38:28 +020065
Michal Suchanek1d524ae2022-10-12 21:57:58 +020066 printf("| %-*.*s| %-*.*s| %s @ %d | status: %i\n",
Przemyslaw Marczak34f922c2015-05-13 13:38:28 +020067 LIMIT_DEV, LIMIT_DEV, dev->name,
68 LIMIT_PARENT, LIMIT_PARENT, dev->parent->name,
Michal Suchanek1d524ae2022-10-12 21:57:58 +020069 dev_get_uclass_name(dev->parent), dev_seq(dev->parent),
70 ret);
Przemyslaw Marczak4d569772015-04-20 20:07:43 +020071 }
72
Michal Suchanek1d524ae2022-10-12 21:57:58 +020073 if (err)
Przemyslaw Marczak4d569772015-04-20 20:07:43 +020074 return CMD_RET_FAILURE;
75
76 return CMD_RET_SUCCESS;
77}
78
Simon Glassed38aef2020-05-10 11:40:03 -060079static int do_dump(struct cmd_tbl *cmdtp, int flag, int argc,
80 char *const argv[])
Przemyslaw Marczak4d569772015-04-20 20:07:43 +020081{
Lukasz Majewskidfd2b572018-05-15 16:26:38 +020082 struct uc_pmic_priv *priv;
Przemyslaw Marczak4d569772015-04-20 20:07:43 +020083 struct udevice *dev;
Lukasz Majewskidfd2b572018-05-15 16:26:38 +020084 char fmt[16];
Przemyslaw Marczak4d569772015-04-20 20:07:43 +020085 uint reg;
86 int ret;
87
Przemyslaw Marczak34f922c2015-05-13 13:38:28 +020088 if (!currdev) {
89 printf("First, set the PMIC device!\n");
90 return CMD_RET_USAGE;
91 }
Przemyslaw Marczak4d569772015-04-20 20:07:43 +020092
93 dev = currdev;
Lukasz Majewskidfd2b572018-05-15 16:26:38 +020094 priv = dev_get_uclass_priv(dev);
Przemyslaw Marczak4d569772015-04-20 20:07:43 +020095 printf("Dump pmic: %s registers\n", dev->name);
96
Lukasz Majewskidfd2b572018-05-15 16:26:38 +020097 sprintf(fmt, "%%%d.%dx ", priv->trans_len * 2,
98 priv->trans_len * 2);
99
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200100 for (reg = 0; reg < pmic_reg_count(dev); reg++) {
Lukasz Majewskidfd2b572018-05-15 16:26:38 +0200101 ret = pmic_reg_read(dev, reg);
Martin Fuzzey39d9a102020-01-14 15:56:18 +0000102 if (ret < 0 && ret != -ENODATA) {
Przemyslaw Marczak34f922c2015-05-13 13:38:28 +0200103 printf("Can't read register: %d\n", reg);
104 return failure(ret);
105 }
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200106
107 if (!(reg % 16))
108 printf("\n0x%02x: ", reg);
109
Martin Fuzzey39d9a102020-01-14 15:56:18 +0000110 if (ret == -ENODATA) {
111 int i;
112
113 for (i = 0; i < priv->trans_len; i++)
114 puts("--");
115 puts(" ");
116 } else {
117 printf(fmt, ret);
118 }
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200119 }
120 printf("\n");
121
122 return CMD_RET_SUCCESS;
123}
124
Simon Glassed38aef2020-05-10 11:40:03 -0600125static int do_read(struct cmd_tbl *cmdtp, int flag, int argc,
126 char *const argv[])
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200127{
Lukasz Majewskidfd2b572018-05-15 16:26:38 +0200128 struct uc_pmic_priv *priv;
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200129 struct udevice *dev;
130 int regs, ret;
Lukasz Majewskidfd2b572018-05-15 16:26:38 +0200131 char fmt[24];
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200132 uint reg;
133
Przemyslaw Marczak34f922c2015-05-13 13:38:28 +0200134 if (!currdev) {
135 printf("First, set the PMIC device!\n");
136 return CMD_RET_USAGE;
137 }
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200138
139 dev = currdev;
Lukasz Majewskidfd2b572018-05-15 16:26:38 +0200140 priv = dev_get_uclass_priv(dev);
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200141
142 if (argc != 2)
143 return CMD_RET_USAGE;
144
145 reg = simple_strtoul(argv[1], NULL, 0);
146 regs = pmic_reg_count(dev);
147 if (reg > regs) {
Przemyslaw Marczak34f922c2015-05-13 13:38:28 +0200148 printf("PMIC max reg: %d\n", regs);
149 return failure(-EFAULT);
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200150 }
151
Lukasz Majewskidfd2b572018-05-15 16:26:38 +0200152 ret = pmic_reg_read(dev, reg);
153 if (ret < 0) {
Przemyslaw Marczak34f922c2015-05-13 13:38:28 +0200154 printf("Can't read PMIC register: %d!\n", reg);
155 return failure(ret);
156 }
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200157
Lukasz Majewskidfd2b572018-05-15 16:26:38 +0200158 sprintf(fmt, "0x%%02x: 0x%%%d.%dx\n", priv->trans_len * 2,
159 priv->trans_len * 2);
160 printf(fmt, reg, ret);
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200161
162 return CMD_RET_SUCCESS;
163}
164
Simon Glassed38aef2020-05-10 11:40:03 -0600165static int do_write(struct cmd_tbl *cmdtp, int flag, int argc,
166 char *const argv[])
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200167{
168 struct udevice *dev;
Lukasz Majewskidfd2b572018-05-15 16:26:38 +0200169 uint reg, value;
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200170 int regs, ret;
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200171
Przemyslaw Marczak34f922c2015-05-13 13:38:28 +0200172 if (!currdev) {
173 printf("First, set the PMIC device!\n");
174 return CMD_RET_USAGE;
175 }
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200176
177 dev = currdev;
178
179 if (argc != 3)
180 return CMD_RET_USAGE;
181
182 reg = simple_strtoul(argv[1], NULL, 0);
183 regs = pmic_reg_count(dev);
184 if (reg > regs) {
Przemyslaw Marczak34f922c2015-05-13 13:38:28 +0200185 printf("PMIC max reg: %d\n", regs);
186 return failure(-EFAULT);
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200187 }
188
189 value = simple_strtoul(argv[2], NULL, 0);
190
Lukasz Majewskidfd2b572018-05-15 16:26:38 +0200191 ret = pmic_reg_write(dev, reg, value);
Przemyslaw Marczak34f922c2015-05-13 13:38:28 +0200192 if (ret) {
193 printf("Can't write PMIC register: %d!\n", reg);
194 return failure(ret);
195 }
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200196
197 return CMD_RET_SUCCESS;
198}
199
Simon Glassed38aef2020-05-10 11:40:03 -0600200static struct cmd_tbl subcmd[] = {
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200201 U_BOOT_CMD_MKENT(dev, 2, 1, do_dev, "", ""),
202 U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""),
203 U_BOOT_CMD_MKENT(dump, 1, 1, do_dump, "", ""),
204 U_BOOT_CMD_MKENT(read, 2, 1, do_read, "", ""),
205 U_BOOT_CMD_MKENT(write, 3, 1, do_write, "", ""),
206};
207
Simon Glassed38aef2020-05-10 11:40:03 -0600208static int do_pmic(struct cmd_tbl *cmdtp, int flag, int argc,
209 char *const argv[])
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200210{
Simon Glassed38aef2020-05-10 11:40:03 -0600211 struct cmd_tbl *cmd;
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200212
213 argc--;
214 argv++;
215
216 cmd = find_cmd_tbl(argv[0], subcmd, ARRAY_SIZE(subcmd));
217 if (cmd == NULL || argc > cmd->maxargs)
218 return CMD_RET_USAGE;
219
220 return cmd->cmd(cmdtp, flag, argc, argv);
221}
222
223U_BOOT_CMD(pmic, CONFIG_SYS_MAXARGS, 1, do_pmic,
Klaus Gogerc1b882d2018-01-03 09:10:34 +0100224 "PMIC sub-system",
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200225 "list - list pmic devices\n"
Przemyslaw Marczak34f922c2015-05-13 13:38:28 +0200226 "pmic dev [name] - show or [set] operating PMIC device\n"
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200227 "pmic dump - dump registers\n"
Quentin Schulzded03b02024-03-14 10:36:19 +0100228 "pmic read <reg> - read byte of 'reg' register\n"
229 "pmic write <reg> <byte> - write 'byte' byte to 'reg' register\n"
Przemyslaw Marczak4d569772015-04-20 20:07:43 +0200230);