David Wu | fd2fdf7 | 2019-12-03 19:26:50 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2019 Rockchip Electronics Co., Ltd |
| 4 | */ |
| 5 | |
David Wu | fd2fdf7 | 2019-12-03 19:26:50 +0800 | [diff] [blame] | 6 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 7 | #include <log.h> |
David Wu | fd2fdf7 | 2019-12-03 19:26:50 +0800 | [diff] [blame] | 8 | #include <dm/pinctrl.h> |
| 9 | #include <regmap.h> |
| 10 | #include <syscon.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 11 | #include <linux/bitops.h> |
David Wu | fd2fdf7 | 2019-12-03 19:26:50 +0800 | [diff] [blame] | 12 | |
| 13 | #include "pinctrl-rockchip.h" |
| 14 | |
| 15 | static struct rockchip_mux_recalced_data rk3308_mux_recalced_data[] = { |
| 16 | { |
| 17 | .num = 1, |
| 18 | .pin = 14, |
| 19 | .reg = 0x28, |
| 20 | .bit = 12, |
| 21 | .mask = 0xf |
| 22 | }, { |
| 23 | .num = 1, |
| 24 | .pin = 15, |
| 25 | .reg = 0x2c, |
| 26 | .bit = 0, |
| 27 | .mask = 0x3 |
| 28 | }, { |
| 29 | .num = 1, |
| 30 | .pin = 18, |
| 31 | .reg = 0x30, |
| 32 | .bit = 4, |
| 33 | .mask = 0xf |
| 34 | }, { |
| 35 | .num = 1, |
| 36 | .pin = 19, |
| 37 | .reg = 0x30, |
| 38 | .bit = 8, |
| 39 | .mask = 0xf |
| 40 | }, { |
| 41 | .num = 1, |
| 42 | .pin = 20, |
| 43 | .reg = 0x30, |
| 44 | .bit = 12, |
| 45 | .mask = 0xf |
| 46 | }, { |
| 47 | .num = 1, |
| 48 | .pin = 21, |
| 49 | .reg = 0x34, |
| 50 | .bit = 0, |
| 51 | .mask = 0xf |
| 52 | }, { |
| 53 | .num = 1, |
| 54 | .pin = 22, |
| 55 | .reg = 0x34, |
| 56 | .bit = 4, |
| 57 | .mask = 0xf |
| 58 | }, { |
| 59 | .num = 1, |
| 60 | .pin = 23, |
| 61 | .reg = 0x34, |
| 62 | .bit = 8, |
| 63 | .mask = 0xf |
| 64 | }, { |
| 65 | .num = 3, |
| 66 | .pin = 12, |
| 67 | .reg = 0x68, |
| 68 | .bit = 8, |
| 69 | .mask = 0xf |
| 70 | }, { |
| 71 | .num = 3, |
| 72 | .pin = 13, |
| 73 | .reg = 0x68, |
| 74 | .bit = 12, |
| 75 | .mask = 0xf |
| 76 | }, { |
| 77 | .num = 2, |
| 78 | .pin = 2, |
| 79 | .reg = 0x608, |
| 80 | .bit = 0, |
| 81 | .mask = 0x7 |
| 82 | }, { |
| 83 | .num = 2, |
| 84 | .pin = 3, |
| 85 | .reg = 0x608, |
| 86 | .bit = 4, |
| 87 | .mask = 0x7 |
| 88 | }, { |
| 89 | .num = 2, |
| 90 | .pin = 16, |
| 91 | .reg = 0x610, |
| 92 | .bit = 8, |
| 93 | .mask = 0x7 |
| 94 | }, { |
| 95 | .num = 3, |
| 96 | .pin = 10, |
| 97 | .reg = 0x610, |
| 98 | .bit = 0, |
| 99 | .mask = 0x7 |
| 100 | }, { |
| 101 | .num = 3, |
| 102 | .pin = 11, |
| 103 | .reg = 0x610, |
| 104 | .bit = 4, |
| 105 | .mask = 0x7 |
| 106 | }, |
| 107 | }; |
| 108 | |
| 109 | static struct rockchip_mux_route_data rk3308_mux_route_data[] = { |
| 110 | { |
| 111 | /* rtc_clk */ |
| 112 | .bank_num = 0, |
| 113 | .pin = 19, |
| 114 | .func = 1, |
| 115 | .route_offset = 0x314, |
| 116 | .route_val = BIT(16 + 0) | BIT(0), |
| 117 | }, { |
| 118 | /* uart2_rxm0 */ |
| 119 | .bank_num = 1, |
| 120 | .pin = 22, |
| 121 | .func = 2, |
| 122 | .route_offset = 0x314, |
| 123 | .route_val = BIT(16 + 2) | BIT(16 + 3), |
| 124 | }, { |
| 125 | /* uart2_rxm1 */ |
| 126 | .bank_num = 4, |
| 127 | .pin = 26, |
| 128 | .func = 2, |
| 129 | .route_offset = 0x314, |
| 130 | .route_val = BIT(16 + 2) | BIT(16 + 3) | BIT(2), |
| 131 | }, { |
| 132 | /* i2c3_sdam0 */ |
| 133 | .bank_num = 0, |
| 134 | .pin = 15, |
| 135 | .func = 2, |
| 136 | .route_offset = 0x608, |
| 137 | .route_val = BIT(16 + 8) | BIT(16 + 9), |
| 138 | }, { |
| 139 | /* i2c3_sdam1 */ |
| 140 | .bank_num = 3, |
| 141 | .pin = 12, |
| 142 | .func = 2, |
| 143 | .route_offset = 0x608, |
| 144 | .route_val = BIT(16 + 8) | BIT(16 + 9) | BIT(8), |
| 145 | }, { |
| 146 | /* i2c3_sdam2 */ |
| 147 | .bank_num = 2, |
| 148 | .pin = 0, |
| 149 | .func = 3, |
| 150 | .route_offset = 0x608, |
| 151 | .route_val = BIT(16 + 8) | BIT(16 + 9) | BIT(9), |
| 152 | }, { |
| 153 | /* i2s-8ch-1-sclktxm0 */ |
| 154 | .bank_num = 1, |
| 155 | .pin = 3, |
| 156 | .func = 2, |
| 157 | .route_offset = 0x308, |
| 158 | .route_val = BIT(16 + 3), |
| 159 | }, { |
| 160 | /* i2s-8ch-1-sclkrxm0 */ |
| 161 | .bank_num = 1, |
| 162 | .pin = 4, |
| 163 | .func = 2, |
| 164 | .route_offset = 0x308, |
| 165 | .route_val = BIT(16 + 3), |
| 166 | }, { |
| 167 | /* i2s-8ch-1-sclktxm1 */ |
| 168 | .bank_num = 1, |
| 169 | .pin = 13, |
| 170 | .func = 2, |
| 171 | .route_offset = 0x308, |
| 172 | .route_val = BIT(16 + 3) | BIT(3), |
| 173 | }, { |
| 174 | /* i2s-8ch-1-sclkrxm1 */ |
| 175 | .bank_num = 1, |
| 176 | .pin = 14, |
| 177 | .func = 2, |
| 178 | .route_offset = 0x308, |
| 179 | .route_val = BIT(16 + 3) | BIT(3), |
| 180 | }, { |
| 181 | /* pdm-clkm0 */ |
| 182 | .bank_num = 1, |
| 183 | .pin = 4, |
| 184 | .func = 3, |
| 185 | .route_offset = 0x308, |
| 186 | .route_val = BIT(16 + 12) | BIT(16 + 13), |
| 187 | }, { |
| 188 | /* pdm-clkm1 */ |
| 189 | .bank_num = 1, |
| 190 | .pin = 14, |
| 191 | .func = 4, |
| 192 | .route_offset = 0x308, |
| 193 | .route_val = BIT(16 + 12) | BIT(16 + 13) | BIT(12), |
| 194 | }, { |
| 195 | /* pdm-clkm2 */ |
| 196 | .bank_num = 2, |
| 197 | .pin = 6, |
| 198 | .func = 2, |
| 199 | .route_offset = 0x308, |
| 200 | .route_val = BIT(16 + 12) | BIT(16 + 13) | BIT(13), |
| 201 | }, { |
| 202 | /* pdm-clkm-m2 */ |
| 203 | .bank_num = 2, |
| 204 | .pin = 4, |
| 205 | .func = 3, |
| 206 | .route_offset = 0x600, |
| 207 | .route_val = BIT(16 + 2) | BIT(2), |
| 208 | }, { |
| 209 | /* spi1_miso */ |
| 210 | .bank_num = 3, |
| 211 | .pin = 10, |
| 212 | .func = 3, |
| 213 | .route_offset = 0x314, |
| 214 | .route_val = BIT(16 + 9), |
| 215 | }, { |
| 216 | /* spi1_miso_m1 */ |
| 217 | .bank_num = 2, |
| 218 | .pin = 4, |
| 219 | .func = 2, |
| 220 | .route_offset = 0x314, |
| 221 | .route_val = BIT(16 + 9) | BIT(9), |
| 222 | }, { |
| 223 | /* mac_rxd0_m0 */ |
| 224 | .bank_num = 1, |
| 225 | .pin = 20, |
| 226 | .func = 3, |
| 227 | .route_offset = 0x314, |
| 228 | .route_val = BIT(16 + 14), |
| 229 | }, { |
| 230 | /* mac_rxd0_m1 */ |
| 231 | .bank_num = 4, |
| 232 | .pin = 2, |
| 233 | .func = 2, |
| 234 | .route_offset = 0x314, |
| 235 | .route_val = BIT(16 + 14) | BIT(14), |
| 236 | }, { |
| 237 | /* uart3_rx */ |
| 238 | .bank_num = 3, |
| 239 | .pin = 12, |
| 240 | .func = 4, |
| 241 | .route_offset = 0x314, |
| 242 | .route_val = BIT(16 + 15), |
| 243 | }, { |
| 244 | /* uart3_rx_m1 */ |
| 245 | .bank_num = 0, |
| 246 | .pin = 17, |
| 247 | .func = 3, |
| 248 | .route_offset = 0x314, |
| 249 | .route_val = BIT(16 + 15) | BIT(15), |
| 250 | }, |
| 251 | }; |
| 252 | |
| 253 | static int rk3308_set_mux(struct rockchip_pin_bank *bank, int pin, int mux) |
| 254 | { |
| 255 | struct rockchip_pinctrl_priv *priv = bank->priv; |
| 256 | int iomux_num = (pin / 8); |
| 257 | struct regmap *regmap; |
| 258 | int reg, ret, mask, mux_type; |
| 259 | u8 bit; |
Jagan Teki | 9e0e681 | 2022-12-14 23:20:56 +0530 | [diff] [blame] | 260 | u32 data; |
David Wu | fd2fdf7 | 2019-12-03 19:26:50 +0800 | [diff] [blame] | 261 | |
| 262 | regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU) |
| 263 | ? priv->regmap_pmu : priv->regmap_base; |
| 264 | |
| 265 | /* get basic quadrupel of mux registers and the correct reg inside */ |
| 266 | mux_type = bank->iomux[iomux_num].type; |
| 267 | reg = bank->iomux[iomux_num].offset; |
| 268 | reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask); |
| 269 | |
| 270 | if (bank->recalced_mask & BIT(pin)) |
| 271 | rockchip_get_recalced_mux(bank, pin, ®, &bit, &mask); |
| 272 | |
David Wu | fd2fdf7 | 2019-12-03 19:26:50 +0800 | [diff] [blame] | 273 | data = (mask << (bit + 16)); |
| 274 | data |= (mux & mask) << bit; |
| 275 | ret = regmap_write(regmap, reg, data); |
| 276 | |
| 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | #define RK3308_PULL_OFFSET 0xa0 |
| 281 | |
| 282 | static void rk3308_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, |
| 283 | int pin_num, struct regmap **regmap, |
| 284 | int *reg, u8 *bit) |
| 285 | { |
| 286 | struct rockchip_pinctrl_priv *priv = bank->priv; |
| 287 | |
| 288 | *regmap = priv->regmap_base; |
| 289 | *reg = RK3308_PULL_OFFSET; |
| 290 | *reg += bank->bank_num * ROCKCHIP_PULL_BANK_STRIDE; |
| 291 | *reg += ((pin_num / ROCKCHIP_PULL_PINS_PER_REG) * 4); |
| 292 | |
| 293 | *bit = (pin_num % ROCKCHIP_PULL_PINS_PER_REG); |
| 294 | *bit *= ROCKCHIP_PULL_BITS_PER_PIN; |
| 295 | } |
| 296 | |
| 297 | static int rk3308_set_pull(struct rockchip_pin_bank *bank, |
| 298 | int pin_num, int pull) |
| 299 | { |
| 300 | struct regmap *regmap; |
| 301 | int reg, ret; |
| 302 | u8 bit, type; |
| 303 | u32 data; |
| 304 | |
| 305 | if (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT) |
| 306 | return -ENOTSUPP; |
| 307 | |
| 308 | rk3308_calc_pull_reg_and_bit(bank, pin_num, ®map, ®, &bit); |
| 309 | type = bank->pull_type[pin_num / 8]; |
| 310 | ret = rockchip_translate_pull_value(type, pull); |
| 311 | if (ret < 0) { |
| 312 | debug("unsupported pull setting %d\n", pull); |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | /* enable the write to the equivalent lower bits */ |
| 317 | data = ((1 << ROCKCHIP_PULL_BITS_PER_PIN) - 1) << (bit + 16); |
| 318 | data |= (ret << bit); |
| 319 | |
| 320 | ret = regmap_write(regmap, reg, data); |
| 321 | |
| 322 | return ret; |
| 323 | } |
| 324 | |
| 325 | #define RK3308_DRV_GRF_OFFSET 0x100 |
| 326 | |
| 327 | static void rk3308_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank, |
| 328 | int pin_num, struct regmap **regmap, |
| 329 | int *reg, u8 *bit) |
| 330 | { |
| 331 | struct rockchip_pinctrl_priv *priv = bank->priv; |
| 332 | |
| 333 | *regmap = priv->regmap_base; |
| 334 | *reg = RK3308_DRV_GRF_OFFSET; |
| 335 | *reg += bank->bank_num * ROCKCHIP_DRV_BANK_STRIDE; |
| 336 | *reg += ((pin_num / ROCKCHIP_DRV_PINS_PER_REG) * 4); |
| 337 | |
| 338 | *bit = (pin_num % ROCKCHIP_DRV_PINS_PER_REG); |
| 339 | *bit *= ROCKCHIP_DRV_BITS_PER_PIN; |
| 340 | } |
| 341 | |
| 342 | static int rk3308_set_drive(struct rockchip_pin_bank *bank, |
| 343 | int pin_num, int strength) |
| 344 | { |
| 345 | struct regmap *regmap; |
| 346 | int reg, ret; |
| 347 | u32 data; |
| 348 | u8 bit; |
| 349 | int type = bank->drv[pin_num / 8].drv_type; |
| 350 | |
| 351 | rk3308_calc_drv_reg_and_bit(bank, pin_num, ®map, ®, &bit); |
| 352 | ret = rockchip_translate_drive_value(type, strength); |
| 353 | if (ret < 0) { |
| 354 | debug("unsupported driver strength %d\n", strength); |
| 355 | return ret; |
| 356 | } |
| 357 | |
| 358 | /* enable the write to the equivalent lower bits */ |
| 359 | data = ((1 << ROCKCHIP_DRV_BITS_PER_PIN) - 1) << (bit + 16); |
| 360 | data |= (ret << bit); |
| 361 | ret = regmap_write(regmap, reg, data); |
| 362 | return ret; |
| 363 | } |
| 364 | |
| 365 | #define RK3308_SCHMITT_PINS_PER_REG 8 |
| 366 | #define RK3308_SCHMITT_BANK_STRIDE 16 |
| 367 | #define RK3308_SCHMITT_GRF_OFFSET 0x1a0 |
| 368 | |
| 369 | static int rk3308_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank, |
| 370 | int pin_num, |
| 371 | struct regmap **regmap, |
| 372 | int *reg, u8 *bit) |
| 373 | { |
| 374 | struct rockchip_pinctrl_priv *priv = bank->priv; |
| 375 | |
| 376 | *regmap = priv->regmap_base; |
| 377 | *reg = RK3308_SCHMITT_GRF_OFFSET; |
| 378 | |
| 379 | *reg += bank->bank_num * RK3308_SCHMITT_BANK_STRIDE; |
| 380 | *reg += ((pin_num / RK3308_SCHMITT_PINS_PER_REG) * 4); |
| 381 | *bit = pin_num % RK3308_SCHMITT_PINS_PER_REG; |
| 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | static int rk3308_set_schmitt(struct rockchip_pin_bank *bank, |
| 387 | int pin_num, int enable) |
| 388 | { |
| 389 | struct regmap *regmap; |
| 390 | int reg; |
| 391 | u8 bit; |
| 392 | u32 data; |
| 393 | |
| 394 | rk3308_calc_schmitt_reg_and_bit(bank, pin_num, ®map, ®, &bit); |
| 395 | /* enable the write to the equivalent lower bits */ |
| 396 | data = BIT(bit + 16) | (enable << bit); |
| 397 | |
| 398 | return regmap_write(regmap, reg, data); |
| 399 | } |
| 400 | |
| 401 | static struct rockchip_pin_bank rk3308_pin_banks[] = { |
| 402 | PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_8WIDTH_2BIT, |
| 403 | IOMUX_8WIDTH_2BIT, |
| 404 | IOMUX_8WIDTH_2BIT, |
| 405 | IOMUX_8WIDTH_2BIT), |
| 406 | PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_8WIDTH_2BIT, |
| 407 | IOMUX_8WIDTH_2BIT, |
| 408 | IOMUX_8WIDTH_2BIT, |
| 409 | IOMUX_8WIDTH_2BIT), |
| 410 | PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", IOMUX_8WIDTH_2BIT, |
| 411 | IOMUX_8WIDTH_2BIT, |
| 412 | IOMUX_8WIDTH_2BIT, |
| 413 | IOMUX_8WIDTH_2BIT), |
| 414 | PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", IOMUX_8WIDTH_2BIT, |
| 415 | IOMUX_8WIDTH_2BIT, |
| 416 | IOMUX_8WIDTH_2BIT, |
| 417 | IOMUX_8WIDTH_2BIT), |
| 418 | PIN_BANK_IOMUX_FLAGS(4, 32, "gpio4", IOMUX_8WIDTH_2BIT, |
| 419 | IOMUX_8WIDTH_2BIT, |
| 420 | IOMUX_8WIDTH_2BIT, |
| 421 | IOMUX_8WIDTH_2BIT), |
| 422 | }; |
| 423 | |
| 424 | static struct rockchip_pin_ctrl rk3308_pin_ctrl = { |
| 425 | .pin_banks = rk3308_pin_banks, |
| 426 | .nr_banks = ARRAY_SIZE(rk3308_pin_banks), |
| 427 | .grf_mux_offset = 0x0, |
| 428 | .iomux_recalced = rk3308_mux_recalced_data, |
| 429 | .niomux_recalced = ARRAY_SIZE(rk3308_mux_recalced_data), |
| 430 | .iomux_routes = rk3308_mux_route_data, |
| 431 | .niomux_routes = ARRAY_SIZE(rk3308_mux_route_data), |
| 432 | .set_mux = rk3308_set_mux, |
| 433 | .set_drive = rk3308_set_drive, |
| 434 | .set_pull = rk3308_set_pull, |
| 435 | .set_schmitt = rk3308_set_schmitt, |
| 436 | }; |
| 437 | |
| 438 | static const struct udevice_id rk3308_pinctrl_ids[] = { |
| 439 | { |
| 440 | .compatible = "rockchip,rk3308-pinctrl", |
| 441 | .data = (ulong)&rk3308_pin_ctrl |
| 442 | }, |
| 443 | { } |
| 444 | }; |
| 445 | |
| 446 | U_BOOT_DRIVER(pinctrl_rk3308) = { |
| 447 | .name = "rockchip_rk3308_pinctrl", |
| 448 | .id = UCLASS_PINCTRL, |
| 449 | .of_match = rk3308_pinctrl_ids, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 450 | .priv_auto = sizeof(struct rockchip_pinctrl_priv), |
David Wu | fd2fdf7 | 2019-12-03 19:26:50 +0800 | [diff] [blame] | 451 | .ops = &rockchip_pinctrl_ops, |
Simon Glass | 9288265 | 2021-08-07 07:24:04 -0600 | [diff] [blame] | 452 | #if CONFIG_IS_ENABLED(OF_REAL) |
David Wu | fd2fdf7 | 2019-12-03 19:26:50 +0800 | [diff] [blame] | 453 | .bind = dm_scan_fdt_dev, |
| 454 | #endif |
| 455 | .probe = rockchip_pinctrl_probe, |
| 456 | }; |