xref: /rk3399_rockchip-uboot/test/rockchip/test-power.c (revision 90a8d4436c3a92cdad55cacda5a86e5b739628d5)
1 /*
2  * (C) Copyright 2019 Rockchip Electronics Co., Ltd
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <boot_rkimg.h>
9 #include <console.h>
10 #include <dm.h>
11 #include <thermal.h>
12 #include <wdt.h>
13 #include <asm/io.h>
14 #include <power/pmic.h>
15 #include <power/regulator.h>
16 #include "test-rockchip.h"
17 
18 #ifdef CONFIG_DM_REGULATOR
19 static int regulator_change_voltage(struct udevice *dev)
20 {
21 	struct dm_regulator_uclass_platdata *uc_pdata;
22 	int uV, save_uV, step_uV = 12500;
23 	int i, ret;
24 
25 	uc_pdata = dev_get_uclass_platdata(dev);
26 
27 	/* Only voltage changeable regulator will be tested! */
28 	if ((uc_pdata->max_uV == uc_pdata->min_uV) ||
29 	    !regulator_get_enable(dev) || strncmp("DCDC", dev->name, 4))
30 		return 0;
31 
32 	/* Test get/set() */
33 	save_uV = regulator_get_value(dev);
34 	if (save_uV < 0)
35 		return save_uV;
36 
37 	ret = regulator_set_value(dev, save_uV);
38 	if (ret < 0)
39 		return ret;
40 
41 	for (i = 1; i < 4; i++) {
42 		uV = regulator_get_value(dev);
43 		regulator_set_value(dev, uV + step_uV * i);
44 		printf("%s@%s: set %d uV -> %d uV; read back %d uV\n",
45 		       dev->name, uc_pdata->name, uV, uV + step_uV * i,
46 		       regulator_get_value(dev));
47 
48 		if ((uV + step_uV * i) != regulator_get_value(dev)) {
49 			printf("%s@%s: set voltage failed\n",
50 			       dev->name, uc_pdata->name);
51 			ret = -EINVAL;
52 			break;
53 		}
54 	}
55 
56 	regulator_set_value(dev, save_uV);
57 
58 	return ret;
59 }
60 
61 static int do_test_regulator(cmd_tbl_t *cmdtp, int flag,
62 			     int argc, char *const argv[])
63 {
64 	struct udevice *dev;
65 	struct uclass *uc;
66 	int ret;
67 
68 	ret = uclass_get(UCLASS_REGULATOR, &uc);
69 	if (ret)
70 		return ret;
71 
72 	for (uclass_first_device(UCLASS_REGULATOR, &dev);
73 	     dev;
74 	     uclass_next_device(&dev)) {
75 		ret = regulator_change_voltage(dev);
76 		if (ret)
77 			return ret;
78 	}
79 
80 	return 0;
81 }
82 #endif
83 
84 #ifdef CONFIG_DM_RESET
85 static int do_test_reset(cmd_tbl_t *cmdtp, int flag,
86 			 int argc, char *const argv[])
87 {
88 	return run_command("reset", 0);
89 }
90 #endif
91 
92 #ifdef CONFIG_CMD_DVFS
93 static int do_test_dvfs(cmd_tbl_t *cmdtp, int flag,
94 			int argc, char *const argv[])
95 {
96 	return run_command("dvfs", 0);
97 }
98 #endif
99 
100 #if defined(CONFIG_WDT)
101 static int do_test_wdt(cmd_tbl_t *cmdtp, int flag,
102 		       int argc, char *const argv[])
103 {
104 	struct udevice *dev;
105 	int ret;
106 
107 	ret = uclass_get_device(UCLASS_WDT, 0, &dev);
108 	if (ret) {
109 		if (ret != -ENODEV)
110 			printf("Get watchdog device failed, ret=%d\n", ret);
111 		return ret;
112 	}
113 
114 	printf("Watchdog would reset system 10s later\n");
115 	wdt_start(dev, 5000, 0);
116 	wdt_stop(dev);
117 
118 	return 0;
119 }
120 #endif
121 
122 #ifdef CONFIG_DM_THERMAL
123 static int do_test_thermal(cmd_tbl_t *cmdtp, int flag,
124 			   int argc, char *const argv[])
125 {
126 	struct udevice *dev;
127 	int ret, temp;
128 
129 	ret = uclass_get_device(UCLASS_THERMAL, 0, &dev);
130 	if (ret) {
131 		printf("Get thermal device failed, ret=%d\n", ret);
132 		return ret;
133 	}
134 
135 	ret = thermal_get_temp(dev, &temp);
136 	if (ret) {
137 		printf("Get temperature failed, ret=%d\n", ret);
138 		return ret;
139 	}
140 
141 	printf("Get temperature: %d'c\n", temp);
142 
143 	return 0;
144 }
145 #endif
146 
147 #ifdef CONFIG_CMD_PMIC
148 static int do_test_pmic(cmd_tbl_t *cmdtp, int flag,
149 			int argc, char *const argv[])
150 {
151 	struct udevice *dev;
152 	struct uclass *uc;
153 	int ret;
154 
155 	ret = uclass_get(UCLASS_PMIC, &uc);
156 	if (ret)
157 		return ret;
158 
159 	for (uclass_first_device(UCLASS_PMIC, &dev);
160 	     dev;
161 	     uclass_next_device(&dev)) {
162 		env_set("this_pmic", dev->name);
163 		run_command("pmic dev $this_pmic", 0);
164 		ret = run_command("pmic dump", 0);
165 		if (ret)
166 			goto out;
167 	}
168 
169 out:
170 	env_set("this_pmic", NULL);
171 
172 	return ret;
173 }
174 #endif
175 
176 #ifdef CONFIG_DM_CHARGE_DISPLAY
177 static int do_test_charge(cmd_tbl_t *cmdtp, int flag,
178 			  int argc, char *const argv[])
179 {
180 	/* TODO */
181 	return 0;
182 }
183 #endif
184 
185 static cmd_tbl_t sub_cmd[] = {
186 #ifdef CONFIG_CMD_CHARGE_DISPLAY
187 	UNIT_CMD_ATTR_DEFINE(charge, 0, CMD_FLG_NORETURN),
188 #endif
189 #ifdef CONFIG_CMD_DVFS
190 	UNIT_CMD_DEFINE(dvfs, 0),
191 #endif
192 #ifdef CONFIG_CMD_PMIC
193 	UNIT_CMD_DEFINE(pmic, 0),
194 #endif
195 	UNIT_CMD_ATTR_DEFINE(reset, 0, CMD_FLG_NORETURN),
196 #ifdef CONFIG_DM_REGULATOR
197 	UNIT_CMD_ATTR_DEFINE(regulator, 0, CMD_FLG_INTERACTIVE),
198 #endif
199 #ifdef CONFIG_DM_THERMAL
200 	UNIT_CMD_DEFINE(thermal, 0),
201 #endif
202 #if defined(CONFIG_WDT)
203 	UNIT_CMD_DEFINE(wdt, 0),
204 #endif
205 };
206 
207 static char sub_cmd_help[] =
208 #ifdef CONFIG_CMD_CHARGE_DISPLAY
209 "    [i] rktest charge                      - test charge animation\n"
210 #endif
211 #ifdef CONFIG_CMD_DVFS
212 "    [.] rktest dvfs                        - test rockchip wide temperature dvfs\n"
213 #endif
214 #ifdef CONFIG_CMD_PMIC
215 "    [.] rktest pmic                        - test pmic, dump registers\n"
216 #endif
217 "    [n] rktest reset                       - test sysreset\n"
218 #ifdef CONFIG_DM_REGULATOR
219 "    [i] rktest regulator                   - test regulator set and show\n"
220 #endif
221 #ifdef CONFIG_DM_THERMAL
222 "    [.] rktest thermal                     - test thermal, getting temperature\n"
223 #endif
224 ;
225 
226 const struct cmd_group cmd_grp_power = {
227 	.id	= TEST_ID_POWER,
228 	.help	= sub_cmd_help,
229 	.cmd	= sub_cmd,
230 	.cmd_n	= ARRAY_SIZE(sub_cmd),
231 };
232