blob: 3bda0534a36c05baedd38f5647e796c84d4e6649 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +02002/*
3 * Copyright (C) 2014-2015 Samsung Electronics
4 * Przemyslaw Marczak <p.marczak@samsung.com>
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +02005 */
6#include <common.h>
Simon Glass09140112020-05-10 11:40:03 -06007#include <command.h>
Przemyslaw Marczakd89fdcf2015-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 Marczak493cdec2015-05-13 13:38:28 +020013#define LIMIT_DEV 32
14#define LIMIT_PARENT 20
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020015
16static struct udevice *currdev;
17
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +020018static int failure(int ret)
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020019{
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +020020 printf("Error: %d (%s)\n", ret, errno_str(ret));
21
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020022 return CMD_RET_FAILURE;
23}
24
Simon Glass09140112020-05-10 11:40:03 -060025static int do_dev(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020026{
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +020027 char *name;
28 int ret = -ENODEV;
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020029
30 switch (argc) {
31 case 2:
Przemyslaw Marczak493cdec2015-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 Marczakd89fdcf2015-04-20 20:07:43 +020038 case 1:
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +020039 if (!currdev) {
40 printf("PMIC device is not set!\n\n");
41 return CMD_RET_USAGE;
42 }
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020043
44 printf("dev: %d @ %s\n", currdev->seq, currdev->name);
45 }
46
47 return CMD_RET_SUCCESS;
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020048}
49
Simon Glass09140112020-05-10 11:40:03 -060050static int do_list(struct cmd_tbl *cmdtp, int flag, int argc,
51 char *const argv[])
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020052{
53 struct udevice *dev;
54 int ret;
55
Przemyslaw Marczak493cdec2015-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 Marczakd89fdcf2015-04-20 20:07:43 +020059 "Parent uclass", "seq");
60
61 for (ret = uclass_first_device(UCLASS_PMIC, &dev); dev;
62 ret = uclass_next_device(&dev)) {
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +020063 if (ret)
64 continue;
65
66 printf("| %-*.*s| %-*.*s| %s @ %d\n",
67 LIMIT_DEV, LIMIT_DEV, dev->name,
68 LIMIT_PARENT, LIMIT_PARENT, dev->parent->name,
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020069 dev_get_uclass_name(dev->parent), dev->parent->seq);
70 }
71
72 if (ret)
73 return CMD_RET_FAILURE;
74
75 return CMD_RET_SUCCESS;
76}
77
Simon Glass09140112020-05-10 11:40:03 -060078static int do_dump(struct cmd_tbl *cmdtp, int flag, int argc,
79 char *const argv[])
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020080{
Lukasz Majewskid4505c82018-05-15 16:26:38 +020081 struct uc_pmic_priv *priv;
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020082 struct udevice *dev;
Lukasz Majewskid4505c82018-05-15 16:26:38 +020083 char fmt[16];
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020084 uint reg;
85 int ret;
86
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +020087 if (!currdev) {
88 printf("First, set the PMIC device!\n");
89 return CMD_RET_USAGE;
90 }
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020091
92 dev = currdev;
Lukasz Majewskid4505c82018-05-15 16:26:38 +020093 priv = dev_get_uclass_priv(dev);
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020094 printf("Dump pmic: %s registers\n", dev->name);
95
Lukasz Majewskid4505c82018-05-15 16:26:38 +020096 sprintf(fmt, "%%%d.%dx ", priv->trans_len * 2,
97 priv->trans_len * 2);
98
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020099 for (reg = 0; reg < pmic_reg_count(dev); reg++) {
Lukasz Majewskid4505c82018-05-15 16:26:38 +0200100 ret = pmic_reg_read(dev, reg);
Martin Fuzzeyb852cca2020-01-14 15:56:18 +0000101 if (ret < 0 && ret != -ENODATA) {
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +0200102 printf("Can't read register: %d\n", reg);
103 return failure(ret);
104 }
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200105
106 if (!(reg % 16))
107 printf("\n0x%02x: ", reg);
108
Martin Fuzzeyb852cca2020-01-14 15:56:18 +0000109 if (ret == -ENODATA) {
110 int i;
111
112 for (i = 0; i < priv->trans_len; i++)
113 puts("--");
114 puts(" ");
115 } else {
116 printf(fmt, ret);
117 }
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200118 }
119 printf("\n");
120
121 return CMD_RET_SUCCESS;
122}
123
Simon Glass09140112020-05-10 11:40:03 -0600124static int do_read(struct cmd_tbl *cmdtp, int flag, int argc,
125 char *const argv[])
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200126{
Lukasz Majewskid4505c82018-05-15 16:26:38 +0200127 struct uc_pmic_priv *priv;
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200128 struct udevice *dev;
129 int regs, ret;
Lukasz Majewskid4505c82018-05-15 16:26:38 +0200130 char fmt[24];
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200131 uint reg;
132
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +0200133 if (!currdev) {
134 printf("First, set the PMIC device!\n");
135 return CMD_RET_USAGE;
136 }
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200137
138 dev = currdev;
Lukasz Majewskid4505c82018-05-15 16:26:38 +0200139 priv = dev_get_uclass_priv(dev);
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200140
141 if (argc != 2)
142 return CMD_RET_USAGE;
143
144 reg = simple_strtoul(argv[1], NULL, 0);
145 regs = pmic_reg_count(dev);
146 if (reg > regs) {
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +0200147 printf("PMIC max reg: %d\n", regs);
148 return failure(-EFAULT);
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200149 }
150
Lukasz Majewskid4505c82018-05-15 16:26:38 +0200151 ret = pmic_reg_read(dev, reg);
152 if (ret < 0) {
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +0200153 printf("Can't read PMIC register: %d!\n", reg);
154 return failure(ret);
155 }
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200156
Lukasz Majewskid4505c82018-05-15 16:26:38 +0200157 sprintf(fmt, "0x%%02x: 0x%%%d.%dx\n", priv->trans_len * 2,
158 priv->trans_len * 2);
159 printf(fmt, reg, ret);
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200160
161 return CMD_RET_SUCCESS;
162}
163
Simon Glass09140112020-05-10 11:40:03 -0600164static int do_write(struct cmd_tbl *cmdtp, int flag, int argc,
165 char *const argv[])
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200166{
167 struct udevice *dev;
Lukasz Majewskid4505c82018-05-15 16:26:38 +0200168 uint reg, value;
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200169 int regs, ret;
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200170
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +0200171 if (!currdev) {
172 printf("First, set the PMIC device!\n");
173 return CMD_RET_USAGE;
174 }
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200175
176 dev = currdev;
177
178 if (argc != 3)
179 return CMD_RET_USAGE;
180
181 reg = simple_strtoul(argv[1], NULL, 0);
182 regs = pmic_reg_count(dev);
183 if (reg > regs) {
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +0200184 printf("PMIC max reg: %d\n", regs);
185 return failure(-EFAULT);
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200186 }
187
188 value = simple_strtoul(argv[2], NULL, 0);
189
Lukasz Majewskid4505c82018-05-15 16:26:38 +0200190 ret = pmic_reg_write(dev, reg, value);
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +0200191 if (ret) {
192 printf("Can't write PMIC register: %d!\n", reg);
193 return failure(ret);
194 }
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200195
196 return CMD_RET_SUCCESS;
197}
198
Simon Glass09140112020-05-10 11:40:03 -0600199static struct cmd_tbl subcmd[] = {
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200200 U_BOOT_CMD_MKENT(dev, 2, 1, do_dev, "", ""),
201 U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""),
202 U_BOOT_CMD_MKENT(dump, 1, 1, do_dump, "", ""),
203 U_BOOT_CMD_MKENT(read, 2, 1, do_read, "", ""),
204 U_BOOT_CMD_MKENT(write, 3, 1, do_write, "", ""),
205};
206
Simon Glass09140112020-05-10 11:40:03 -0600207static int do_pmic(struct cmd_tbl *cmdtp, int flag, int argc,
208 char *const argv[])
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200209{
Simon Glass09140112020-05-10 11:40:03 -0600210 struct cmd_tbl *cmd;
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200211
212 argc--;
213 argv++;
214
215 cmd = find_cmd_tbl(argv[0], subcmd, ARRAY_SIZE(subcmd));
216 if (cmd == NULL || argc > cmd->maxargs)
217 return CMD_RET_USAGE;
218
219 return cmd->cmd(cmdtp, flag, argc, argv);
220}
221
222U_BOOT_CMD(pmic, CONFIG_SYS_MAXARGS, 1, do_pmic,
Klaus Goger7e37a882018-01-03 09:10:34 +0100223 "PMIC sub-system",
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200224 "list - list pmic devices\n"
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +0200225 "pmic dev [name] - show or [set] operating PMIC device\n"
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200226 "pmic dump - dump registers\n"
227 "pmic read address - read byte of register at address\n"
228 "pmic write address - write byte to register at address\n"
229);