blob: e46d813a70b2b75431bec04a7a64d8cc8b1b5564 [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>
7#include <errno.h>
8#include <dm.h>
9#include <dm/uclass-internal.h>
10#include <power/pmic.h>
11
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +020012#define LIMIT_DEV 32
13#define LIMIT_PARENT 20
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020014
15static struct udevice *currdev;
16
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +020017static int failure(int ret)
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020018{
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +020019 printf("Error: %d (%s)\n", ret, errno_str(ret));
20
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020021 return CMD_RET_FAILURE;
22}
23
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020024static int do_dev(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
25{
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +020026 char *name;
27 int ret = -ENODEV;
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020028
29 switch (argc) {
30 case 2:
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +020031 name = argv[1];
32 ret = pmic_get(name, &currdev);
33 if (ret) {
34 printf("Can't get PMIC: %s!\n", name);
35 return failure(ret);
36 }
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020037 case 1:
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +020038 if (!currdev) {
39 printf("PMIC device is not set!\n\n");
40 return CMD_RET_USAGE;
41 }
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020042
43 printf("dev: %d @ %s\n", currdev->seq, currdev->name);
44 }
45
46 return CMD_RET_SUCCESS;
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020047}
48
49static int do_list(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
50{
51 struct udevice *dev;
52 int ret;
53
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +020054 printf("| %-*.*s| %-*.*s| %s @ %s\n",
55 LIMIT_DEV, LIMIT_DEV, "Name",
56 LIMIT_PARENT, LIMIT_PARENT, "Parent name",
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020057 "Parent uclass", "seq");
58
59 for (ret = uclass_first_device(UCLASS_PMIC, &dev); dev;
60 ret = uclass_next_device(&dev)) {
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +020061 if (ret)
62 continue;
63
64 printf("| %-*.*s| %-*.*s| %s @ %d\n",
65 LIMIT_DEV, LIMIT_DEV, dev->name,
66 LIMIT_PARENT, LIMIT_PARENT, dev->parent->name,
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020067 dev_get_uclass_name(dev->parent), dev->parent->seq);
68 }
69
70 if (ret)
71 return CMD_RET_FAILURE;
72
73 return CMD_RET_SUCCESS;
74}
75
76static int do_dump(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
77{
Lukasz Majewskid4505c82018-05-15 16:26:38 +020078 struct uc_pmic_priv *priv;
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020079 struct udevice *dev;
Lukasz Majewskid4505c82018-05-15 16:26:38 +020080 char fmt[16];
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020081 uint reg;
82 int ret;
83
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +020084 if (!currdev) {
85 printf("First, set the PMIC device!\n");
86 return CMD_RET_USAGE;
87 }
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020088
89 dev = currdev;
Lukasz Majewskid4505c82018-05-15 16:26:38 +020090 priv = dev_get_uclass_priv(dev);
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020091 printf("Dump pmic: %s registers\n", dev->name);
92
Lukasz Majewskid4505c82018-05-15 16:26:38 +020093 sprintf(fmt, "%%%d.%dx ", priv->trans_len * 2,
94 priv->trans_len * 2);
95
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +020096 for (reg = 0; reg < pmic_reg_count(dev); reg++) {
Lukasz Majewskid4505c82018-05-15 16:26:38 +020097 ret = pmic_reg_read(dev, reg);
98 if (ret < 0) {
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +020099 printf("Can't read register: %d\n", reg);
100 return failure(ret);
101 }
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200102
103 if (!(reg % 16))
104 printf("\n0x%02x: ", reg);
105
Lukasz Majewskid4505c82018-05-15 16:26:38 +0200106 printf(fmt, ret);
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200107 }
108 printf("\n");
109
110 return CMD_RET_SUCCESS;
111}
112
113static int do_read(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
114{
Lukasz Majewskid4505c82018-05-15 16:26:38 +0200115 struct uc_pmic_priv *priv;
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200116 struct udevice *dev;
117 int regs, ret;
Lukasz Majewskid4505c82018-05-15 16:26:38 +0200118 char fmt[24];
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200119 uint reg;
120
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +0200121 if (!currdev) {
122 printf("First, set the PMIC device!\n");
123 return CMD_RET_USAGE;
124 }
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200125
126 dev = currdev;
Lukasz Majewskid4505c82018-05-15 16:26:38 +0200127 priv = dev_get_uclass_priv(dev);
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200128
129 if (argc != 2)
130 return CMD_RET_USAGE;
131
132 reg = simple_strtoul(argv[1], NULL, 0);
133 regs = pmic_reg_count(dev);
134 if (reg > regs) {
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +0200135 printf("PMIC max reg: %d\n", regs);
136 return failure(-EFAULT);
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200137 }
138
Lukasz Majewskid4505c82018-05-15 16:26:38 +0200139 ret = pmic_reg_read(dev, reg);
140 if (ret < 0) {
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +0200141 printf("Can't read PMIC register: %d!\n", reg);
142 return failure(ret);
143 }
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200144
Lukasz Majewskid4505c82018-05-15 16:26:38 +0200145 sprintf(fmt, "0x%%02x: 0x%%%d.%dx\n", priv->trans_len * 2,
146 priv->trans_len * 2);
147 printf(fmt, reg, ret);
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200148
149 return CMD_RET_SUCCESS;
150}
151
152static int do_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
153{
154 struct udevice *dev;
Lukasz Majewskid4505c82018-05-15 16:26:38 +0200155 uint reg, value;
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200156 int regs, ret;
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200157
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +0200158 if (!currdev) {
159 printf("First, set the PMIC device!\n");
160 return CMD_RET_USAGE;
161 }
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200162
163 dev = currdev;
164
165 if (argc != 3)
166 return CMD_RET_USAGE;
167
168 reg = simple_strtoul(argv[1], NULL, 0);
169 regs = pmic_reg_count(dev);
170 if (reg > regs) {
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +0200171 printf("PMIC max reg: %d\n", regs);
172 return failure(-EFAULT);
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200173 }
174
175 value = simple_strtoul(argv[2], NULL, 0);
176
Lukasz Majewskid4505c82018-05-15 16:26:38 +0200177 ret = pmic_reg_write(dev, reg, value);
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +0200178 if (ret) {
179 printf("Can't write PMIC register: %d!\n", reg);
180 return failure(ret);
181 }
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200182
183 return CMD_RET_SUCCESS;
184}
185
186static cmd_tbl_t subcmd[] = {
187 U_BOOT_CMD_MKENT(dev, 2, 1, do_dev, "", ""),
188 U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""),
189 U_BOOT_CMD_MKENT(dump, 1, 1, do_dump, "", ""),
190 U_BOOT_CMD_MKENT(read, 2, 1, do_read, "", ""),
191 U_BOOT_CMD_MKENT(write, 3, 1, do_write, "", ""),
192};
193
194static int do_pmic(cmd_tbl_t *cmdtp, int flag, int argc,
195 char * const argv[])
196{
197 cmd_tbl_t *cmd;
198
199 argc--;
200 argv++;
201
202 cmd = find_cmd_tbl(argv[0], subcmd, ARRAY_SIZE(subcmd));
203 if (cmd == NULL || argc > cmd->maxargs)
204 return CMD_RET_USAGE;
205
206 return cmd->cmd(cmdtp, flag, argc, argv);
207}
208
209U_BOOT_CMD(pmic, CONFIG_SYS_MAXARGS, 1, do_pmic,
Klaus Goger7e37a882018-01-03 09:10:34 +0100210 "PMIC sub-system",
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200211 "list - list pmic devices\n"
Przemyslaw Marczak493cdec2015-05-13 13:38:28 +0200212 "pmic dev [name] - show or [set] operating PMIC device\n"
Przemyslaw Marczakd89fdcf2015-04-20 20:07:43 +0200213 "pmic dump - dump registers\n"
214 "pmic read address - read byte of register at address\n"
215 "pmic write address - write byte to register at address\n"
216);