blob: cf4f7c3984c77787f4e65fa4d6d4c717433c7dce [file] [log] [blame]
Jonas Karlmana937c3d2023-08-21 22:30:28 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Rockchip IO Voltage Domain driver
4 *
5 * Ported from linux drivers/soc/rockchip/io-domain.c
6 */
7
Jonas Karlmana937c3d2023-08-21 22:30:28 +00008#include <dm.h>
9#include <dm/device_compat.h>
10#include <regmap.h>
11#include <syscon.h>
12#include <power/regulator.h>
13
14#define MAX_SUPPLIES 16
15
16/*
17 * The max voltage for 1.8V and 3.3V come from the Rockchip datasheet under
18 * "Recommended Operating Conditions" for "Digital GPIO". When the typical
19 * is 3.3V the max is 3.6V. When the typical is 1.8V the max is 1.98V.
20 *
21 * They are used like this:
22 * - If the voltage on a rail is above the "1.8" voltage (1.98V) we'll tell the
23 * SoC we're at 3.3.
24 * - If the voltage on a rail is above the "3.3" voltage (3.6V) we'll consider
25 * that to be an error.
26 */
27#define MAX_VOLTAGE_1_8 1980000
28#define MAX_VOLTAGE_3_3 3600000
29
Quentin Schulzc36413c2024-06-14 18:54:13 +020030#define PX30_IO_VSEL 0x180
31#define PX30_IO_VSEL_VCCIO6_SRC BIT(0)
32#define PX30_IO_VSEL_VCCIO6_SUPPLY_NUM 1
33
Jonas Karlman84677e22024-04-21 20:09:03 +000034#define RK3328_SOC_CON4 0x410
35#define RK3328_SOC_CON4_VCCIO2 BIT(7)
36#define RK3328_SOC_VCCIO2_SUPPLY_NUM 1
37
Jonas Karlman2de30162024-03-12 23:36:15 +000038#define RK3399_PMUGRF_CON0 0x180
39#define RK3399_PMUGRF_CON0_VSEL BIT(8)
40#define RK3399_PMUGRF_VSEL_SUPPLY_NUM 9
41
Jonas Karlmana937c3d2023-08-21 22:30:28 +000042#define RK3568_PMU_GRF_IO_VSEL0 0x0140
43#define RK3568_PMU_GRF_IO_VSEL1 0x0144
44#define RK3568_PMU_GRF_IO_VSEL2 0x0148
45
46struct rockchip_iodomain_soc_data {
47 int grf_offset;
48 const char *supply_names[MAX_SUPPLIES];
Jonas Karlman2de30162024-03-12 23:36:15 +000049 int (*write)(struct regmap *grf, uint offset, int idx, int uV);
Jonas Karlmana937c3d2023-08-21 22:30:28 +000050};
51
Jonas Karlman2de30162024-03-12 23:36:15 +000052static int rk3568_iodomain_write(struct regmap *grf, uint offset, int idx, int uV)
Jonas Karlmana937c3d2023-08-21 22:30:28 +000053{
54 u32 is_3v3 = uV > MAX_VOLTAGE_1_8;
55 u32 val0, val1;
56 int b;
57
58 switch (idx) {
59 case 0: /* pmuio1 */
60 break;
61 case 1: /* pmuio2 */
62 b = idx;
63 val0 = BIT(16 + b) | (is_3v3 ? 0 : BIT(b));
64 b = idx + 4;
65 val1 = BIT(16 + b) | (is_3v3 ? BIT(b) : 0);
66
67 regmap_write(grf, RK3568_PMU_GRF_IO_VSEL2, val0);
68 regmap_write(grf, RK3568_PMU_GRF_IO_VSEL2, val1);
69 break;
70 case 3: /* vccio2 */
71 break;
72 case 2: /* vccio1 */
73 case 4: /* vccio3 */
74 case 5: /* vccio4 */
75 case 6: /* vccio5 */
76 case 7: /* vccio6 */
77 case 8: /* vccio7 */
78 b = idx - 1;
79 val0 = BIT(16 + b) | (is_3v3 ? 0 : BIT(b));
80 val1 = BIT(16 + b) | (is_3v3 ? BIT(b) : 0);
81
82 regmap_write(grf, RK3568_PMU_GRF_IO_VSEL0, val0);
83 regmap_write(grf, RK3568_PMU_GRF_IO_VSEL1, val1);
84 break;
85 default:
86 return -EINVAL;
87 }
88
89 return 0;
90}
91
Jonas Karlman2de30162024-03-12 23:36:15 +000092static int rockchip_iodomain_write(struct regmap *grf, uint offset, int idx, int uV)
93{
94 u32 val;
95
96 /* set value bit */
97 val = (uV > MAX_VOLTAGE_1_8) ? 0 : 1;
98 val <<= idx;
99
100 /* apply hiword-mask */
101 val |= (BIT(idx) << 16);
102
103 return regmap_write(grf, offset, val);
104}
105
Quentin Schulzc36413c2024-06-14 18:54:13 +0200106static int px30_iodomain_write(struct regmap *grf, uint offset, int idx, int uV)
107{
108 int ret = rockchip_iodomain_write(grf, offset, idx, uV);
109
110 if (!ret && idx == PX30_IO_VSEL_VCCIO6_SUPPLY_NUM) {
111 /*
112 * set vccio6 iodomain to also use this framework
113 * instead of a special gpio.
114 */
115 u32 val = PX30_IO_VSEL_VCCIO6_SRC | (PX30_IO_VSEL_VCCIO6_SRC << 16);
116 ret = regmap_write(grf, PX30_IO_VSEL, val);
117 }
118
119 return ret;
120}
121
Jonas Karlman84677e22024-04-21 20:09:03 +0000122static int rk3328_iodomain_write(struct regmap *grf, uint offset, int idx, int uV)
123{
124 int ret = rockchip_iodomain_write(grf, offset, idx, uV);
125
126 if (!ret && idx == RK3328_SOC_VCCIO2_SUPPLY_NUM) {
127 /*
128 * set vccio2 iodomain to also use this framework
129 * instead of a special gpio.
130 */
131 u32 val = RK3328_SOC_CON4_VCCIO2 | (RK3328_SOC_CON4_VCCIO2 << 16);
132 ret = regmap_write(grf, RK3328_SOC_CON4, val);
133 }
134
135 return ret;
136}
137
Jonas Karlman2de30162024-03-12 23:36:15 +0000138static int rk3399_pmu_iodomain_write(struct regmap *grf, uint offset, int idx, int uV)
139{
140 int ret = rockchip_iodomain_write(grf, offset, idx, uV);
141
142 if (!ret && idx == RK3399_PMUGRF_VSEL_SUPPLY_NUM) {
143 /*
144 * set pmu io iodomain to also use this framework
145 * instead of a special gpio.
146 */
147 u32 val = RK3399_PMUGRF_CON0_VSEL | (RK3399_PMUGRF_CON0_VSEL << 16);
148 ret = regmap_write(grf, RK3399_PMUGRF_CON0, val);
149 }
150
151 return ret;
152}
153
Quentin Schulzc36413c2024-06-14 18:54:13 +0200154static const struct rockchip_iodomain_soc_data soc_data_px30 = {
155 .grf_offset = 0x180,
156 .supply_names = {
157 NULL,
158 "vccio6-supply",
159 "vccio1-supply",
160 "vccio2-supply",
161 "vccio3-supply",
162 "vccio4-supply",
163 "vccio5-supply",
164 "vccio-oscgpi-supply",
165 },
166 .write = px30_iodomain_write,
167};
168
169static const struct rockchip_iodomain_soc_data soc_data_px30_pmu = {
170 .grf_offset = 0x100,
171 .supply_names = {
172 NULL,
173 NULL,
174 NULL,
175 NULL,
176 NULL,
177 NULL,
178 NULL,
179 NULL,
180 NULL,
181 NULL,
182 NULL,
183 NULL,
184 NULL,
185 NULL,
186 "pmuio1-supply",
187 "pmuio2-supply",
188 },
189 .write = rockchip_iodomain_write,
190};
191
Jonas Karlman84677e22024-04-21 20:09:03 +0000192static const struct rockchip_iodomain_soc_data soc_data_rk3328 = {
193 .grf_offset = 0x410,
194 .supply_names = {
195 "vccio1-supply",
196 "vccio2-supply",
197 "vccio3-supply",
198 "vccio4-supply",
199 "vccio5-supply",
200 "vccio6-supply",
201 "pmuio-supply",
202 },
203 .write = rk3328_iodomain_write,
204};
205
Jonas Karlman2de30162024-03-12 23:36:15 +0000206static const struct rockchip_iodomain_soc_data soc_data_rk3399 = {
207 .grf_offset = 0xe640,
208 .supply_names = {
209 "bt656-supply", /* APIO2_VDD */
210 "audio-supply", /* APIO5_VDD */
211 "sdmmc-supply", /* SDMMC0_VDD */
212 "gpio1830-supply", /* APIO4_VDD */
213 },
214 .write = rockchip_iodomain_write,
215};
216
217static const struct rockchip_iodomain_soc_data soc_data_rk3399_pmu = {
218 .grf_offset = 0x180,
219 .supply_names = {
220 NULL,
221 NULL,
222 NULL,
223 NULL,
224 NULL,
225 NULL,
226 NULL,
227 NULL,
228 NULL,
229 "pmu1830-supply", /* PMUIO2_VDD */
230 },
231 .write = rk3399_pmu_iodomain_write,
232};
233
Jonas Karlmana937c3d2023-08-21 22:30:28 +0000234static const struct rockchip_iodomain_soc_data soc_data_rk3568_pmu = {
235 .grf_offset = 0x140,
236 .supply_names = {
237 NULL,
238 "pmuio2-supply",
239 "vccio1-supply",
240 NULL,
241 "vccio3-supply",
242 "vccio4-supply",
243 "vccio5-supply",
244 "vccio6-supply",
245 "vccio7-supply",
246 },
247 .write = rk3568_iodomain_write,
248};
249
250static const struct udevice_id rockchip_iodomain_ids[] = {
251 {
Quentin Schulzc36413c2024-06-14 18:54:13 +0200252 .compatible = "rockchip,px30-io-voltage-domain",
253 .data = (ulong)&soc_data_px30,
254 },
255 {
256 .compatible = "rockchip,px30-pmu-io-voltage-domain",
257 .data = (ulong)&soc_data_px30_pmu,
258 },
259 {
Jonas Karlman84677e22024-04-21 20:09:03 +0000260 .compatible = "rockchip,rk3328-io-voltage-domain",
261 .data = (ulong)&soc_data_rk3328,
262 },
263 {
Jonas Karlman2de30162024-03-12 23:36:15 +0000264 .compatible = "rockchip,rk3399-io-voltage-domain",
265 .data = (ulong)&soc_data_rk3399,
266 },
267 {
268 .compatible = "rockchip,rk3399-pmu-io-voltage-domain",
269 .data = (ulong)&soc_data_rk3399_pmu,
270 },
271 {
Jonas Karlmana937c3d2023-08-21 22:30:28 +0000272 .compatible = "rockchip,rk3568-pmu-io-voltage-domain",
273 .data = (ulong)&soc_data_rk3568_pmu,
274 },
275 { }
276};
277
278static int rockchip_iodomain_bind(struct udevice *dev)
279{
280 /*
281 * According to the Hardware Design Guide, IO-domain configuration must
282 * be consistent with the power supply voltage (1.8V or 3.3V).
283 * Probe after bind to configure IO-domain voltage early during boot.
284 */
285 dev_or_flags(dev, DM_FLAG_PROBE_AFTER_BIND);
286
287 return 0;
288}
289
290static int rockchip_iodomain_probe(struct udevice *dev)
291{
292 struct rockchip_iodomain_soc_data *soc_data =
293 (struct rockchip_iodomain_soc_data *)dev_get_driver_data(dev);
294 struct regmap *grf;
295 int ret;
296
297 grf = syscon_get_regmap(dev_get_parent(dev));
298 if (IS_ERR(grf))
299 return PTR_ERR(grf);
300
301 for (int i = 0; i < MAX_SUPPLIES; i++) {
302 const char *supply_name = soc_data->supply_names[i];
303 struct udevice *reg;
304 int uV;
305
306 if (!supply_name)
307 continue;
308
309 ret = device_get_supply_regulator(dev, supply_name, &reg);
310 if (ret)
311 continue;
312
313 ret = regulator_autoset(reg);
314 if (ret && ret != -EALREADY && ret != -EMEDIUMTYPE &&
315 ret != -ENOSYS)
316 continue;
317
318 uV = regulator_get_value(reg);
319 if (uV <= 0)
320 continue;
321
322 if (uV > MAX_VOLTAGE_3_3) {
323 dev_crit(dev, "%s: %d uV is too high. May damage SoC!\n",
324 supply_name, uV);
325 continue;
326 }
327
Jonas Karlman2de30162024-03-12 23:36:15 +0000328 ret = soc_data->write(grf, soc_data->grf_offset, i, uV);
329 if (ret)
330 dev_err(dev, "%s: Couldn't write to GRF\n", supply_name);
Jonas Karlmana937c3d2023-08-21 22:30:28 +0000331 }
332
333 return 0;
334}
335
336U_BOOT_DRIVER(rockchip_iodomain) = {
337 .name = "rockchip_iodomain",
338 .id = UCLASS_NOP,
339 .of_match = rockchip_iodomain_ids,
340 .bind = rockchip_iodomain_bind,
341 .probe = rockchip_iodomain_probe,
342};