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