blob: 0ffea32ef07fda5b987e542dab155deeff46f568 [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
Jonas Karlman2de30162024-03-12 23:36:15 +000030#define RK3399_PMUGRF_CON0 0x180
31#define RK3399_PMUGRF_CON0_VSEL BIT(8)
32#define RK3399_PMUGRF_VSEL_SUPPLY_NUM 9
33
Jonas Karlmana937c3d2023-08-21 22:30:28 +000034#define RK3568_PMU_GRF_IO_VSEL0 0x0140
35#define RK3568_PMU_GRF_IO_VSEL1 0x0144
36#define RK3568_PMU_GRF_IO_VSEL2 0x0148
37
38struct rockchip_iodomain_soc_data {
39 int grf_offset;
40 const char *supply_names[MAX_SUPPLIES];
Jonas Karlman2de30162024-03-12 23:36:15 +000041 int (*write)(struct regmap *grf, uint offset, int idx, int uV);
Jonas Karlmana937c3d2023-08-21 22:30:28 +000042};
43
Jonas Karlman2de30162024-03-12 23:36:15 +000044static int rk3568_iodomain_write(struct regmap *grf, uint offset, int idx, int uV)
Jonas Karlmana937c3d2023-08-21 22:30:28 +000045{
46 u32 is_3v3 = uV > MAX_VOLTAGE_1_8;
47 u32 val0, val1;
48 int b;
49
50 switch (idx) {
51 case 0: /* pmuio1 */
52 break;
53 case 1: /* pmuio2 */
54 b = idx;
55 val0 = BIT(16 + b) | (is_3v3 ? 0 : BIT(b));
56 b = idx + 4;
57 val1 = BIT(16 + b) | (is_3v3 ? BIT(b) : 0);
58
59 regmap_write(grf, RK3568_PMU_GRF_IO_VSEL2, val0);
60 regmap_write(grf, RK3568_PMU_GRF_IO_VSEL2, val1);
61 break;
62 case 3: /* vccio2 */
63 break;
64 case 2: /* vccio1 */
65 case 4: /* vccio3 */
66 case 5: /* vccio4 */
67 case 6: /* vccio5 */
68 case 7: /* vccio6 */
69 case 8: /* vccio7 */
70 b = idx - 1;
71 val0 = BIT(16 + b) | (is_3v3 ? 0 : BIT(b));
72 val1 = BIT(16 + b) | (is_3v3 ? BIT(b) : 0);
73
74 regmap_write(grf, RK3568_PMU_GRF_IO_VSEL0, val0);
75 regmap_write(grf, RK3568_PMU_GRF_IO_VSEL1, val1);
76 break;
77 default:
78 return -EINVAL;
79 }
80
81 return 0;
82}
83
Jonas Karlman2de30162024-03-12 23:36:15 +000084static int rockchip_iodomain_write(struct regmap *grf, uint offset, int idx, int uV)
85{
86 u32 val;
87
88 /* set value bit */
89 val = (uV > MAX_VOLTAGE_1_8) ? 0 : 1;
90 val <<= idx;
91
92 /* apply hiword-mask */
93 val |= (BIT(idx) << 16);
94
95 return regmap_write(grf, offset, val);
96}
97
98static int rk3399_pmu_iodomain_write(struct regmap *grf, uint offset, int idx, int uV)
99{
100 int ret = rockchip_iodomain_write(grf, offset, idx, uV);
101
102 if (!ret && idx == RK3399_PMUGRF_VSEL_SUPPLY_NUM) {
103 /*
104 * set pmu io iodomain to also use this framework
105 * instead of a special gpio.
106 */
107 u32 val = RK3399_PMUGRF_CON0_VSEL | (RK3399_PMUGRF_CON0_VSEL << 16);
108 ret = regmap_write(grf, RK3399_PMUGRF_CON0, val);
109 }
110
111 return ret;
112}
113
114static const struct rockchip_iodomain_soc_data soc_data_rk3399 = {
115 .grf_offset = 0xe640,
116 .supply_names = {
117 "bt656-supply", /* APIO2_VDD */
118 "audio-supply", /* APIO5_VDD */
119 "sdmmc-supply", /* SDMMC0_VDD */
120 "gpio1830-supply", /* APIO4_VDD */
121 },
122 .write = rockchip_iodomain_write,
123};
124
125static const struct rockchip_iodomain_soc_data soc_data_rk3399_pmu = {
126 .grf_offset = 0x180,
127 .supply_names = {
128 NULL,
129 NULL,
130 NULL,
131 NULL,
132 NULL,
133 NULL,
134 NULL,
135 NULL,
136 NULL,
137 "pmu1830-supply", /* PMUIO2_VDD */
138 },
139 .write = rk3399_pmu_iodomain_write,
140};
141
Jonas Karlmana937c3d2023-08-21 22:30:28 +0000142static const struct rockchip_iodomain_soc_data soc_data_rk3568_pmu = {
143 .grf_offset = 0x140,
144 .supply_names = {
145 NULL,
146 "pmuio2-supply",
147 "vccio1-supply",
148 NULL,
149 "vccio3-supply",
150 "vccio4-supply",
151 "vccio5-supply",
152 "vccio6-supply",
153 "vccio7-supply",
154 },
155 .write = rk3568_iodomain_write,
156};
157
158static const struct udevice_id rockchip_iodomain_ids[] = {
159 {
Jonas Karlman2de30162024-03-12 23:36:15 +0000160 .compatible = "rockchip,rk3399-io-voltage-domain",
161 .data = (ulong)&soc_data_rk3399,
162 },
163 {
164 .compatible = "rockchip,rk3399-pmu-io-voltage-domain",
165 .data = (ulong)&soc_data_rk3399_pmu,
166 },
167 {
Jonas Karlmana937c3d2023-08-21 22:30:28 +0000168 .compatible = "rockchip,rk3568-pmu-io-voltage-domain",
169 .data = (ulong)&soc_data_rk3568_pmu,
170 },
171 { }
172};
173
174static int rockchip_iodomain_bind(struct udevice *dev)
175{
176 /*
177 * According to the Hardware Design Guide, IO-domain configuration must
178 * be consistent with the power supply voltage (1.8V or 3.3V).
179 * Probe after bind to configure IO-domain voltage early during boot.
180 */
181 dev_or_flags(dev, DM_FLAG_PROBE_AFTER_BIND);
182
183 return 0;
184}
185
186static int rockchip_iodomain_probe(struct udevice *dev)
187{
188 struct rockchip_iodomain_soc_data *soc_data =
189 (struct rockchip_iodomain_soc_data *)dev_get_driver_data(dev);
190 struct regmap *grf;
191 int ret;
192
193 grf = syscon_get_regmap(dev_get_parent(dev));
194 if (IS_ERR(grf))
195 return PTR_ERR(grf);
196
197 for (int i = 0; i < MAX_SUPPLIES; i++) {
198 const char *supply_name = soc_data->supply_names[i];
199 struct udevice *reg;
200 int uV;
201
202 if (!supply_name)
203 continue;
204
205 ret = device_get_supply_regulator(dev, supply_name, &reg);
206 if (ret)
207 continue;
208
209 ret = regulator_autoset(reg);
210 if (ret && ret != -EALREADY && ret != -EMEDIUMTYPE &&
211 ret != -ENOSYS)
212 continue;
213
214 uV = regulator_get_value(reg);
215 if (uV <= 0)
216 continue;
217
218 if (uV > MAX_VOLTAGE_3_3) {
219 dev_crit(dev, "%s: %d uV is too high. May damage SoC!\n",
220 supply_name, uV);
221 continue;
222 }
223
Jonas Karlman2de30162024-03-12 23:36:15 +0000224 ret = soc_data->write(grf, soc_data->grf_offset, i, uV);
225 if (ret)
226 dev_err(dev, "%s: Couldn't write to GRF\n", supply_name);
Jonas Karlmana937c3d2023-08-21 22:30:28 +0000227 }
228
229 return 0;
230}
231
232U_BOOT_DRIVER(rockchip_iodomain) = {
233 .name = "rockchip_iodomain",
234 .id = UCLASS_NOP,
235 .of_match = rockchip_iodomain_ids,
236 .bind = rockchip_iodomain_bind,
237 .probe = rockchip_iodomain_probe,
238};