David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2019 Rockchip Electronics Co., Ltd |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 9 | #include <dm/pinctrl.h> |
| 10 | #include <regmap.h> |
| 11 | #include <syscon.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 12 | #include <linux/bitops.h> |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 13 | |
| 14 | #include "pinctrl-rockchip.h" |
| 15 | |
| 16 | static struct rockchip_mux_route_data rk3228_mux_route_data[] = { |
| 17 | { |
| 18 | /* pwm0-0 */ |
| 19 | .bank_num = 0, |
| 20 | .pin = 26, |
| 21 | .func = 1, |
| 22 | .route_offset = 0x50, |
| 23 | .route_val = BIT(16), |
| 24 | }, { |
| 25 | /* pwm0-1 */ |
| 26 | .bank_num = 3, |
| 27 | .pin = 21, |
| 28 | .func = 1, |
| 29 | .route_offset = 0x50, |
| 30 | .route_val = BIT(16) | BIT(0), |
| 31 | }, { |
| 32 | /* pwm1-0 */ |
| 33 | .bank_num = 0, |
| 34 | .pin = 27, |
| 35 | .func = 1, |
| 36 | .route_offset = 0x50, |
| 37 | .route_val = BIT(16 + 1), |
| 38 | }, { |
| 39 | /* pwm1-1 */ |
| 40 | .bank_num = 0, |
| 41 | .pin = 30, |
| 42 | .func = 2, |
| 43 | .route_offset = 0x50, |
| 44 | .route_val = BIT(16 + 1) | BIT(1), |
| 45 | }, { |
| 46 | /* pwm2-0 */ |
| 47 | .bank_num = 0, |
| 48 | .pin = 28, |
| 49 | .func = 1, |
| 50 | .route_offset = 0x50, |
| 51 | .route_val = BIT(16 + 2), |
| 52 | }, { |
| 53 | /* pwm2-1 */ |
| 54 | .bank_num = 1, |
| 55 | .pin = 12, |
| 56 | .func = 2, |
| 57 | .route_offset = 0x50, |
| 58 | .route_val = BIT(16 + 2) | BIT(2), |
| 59 | }, { |
| 60 | /* pwm3-0 */ |
| 61 | .bank_num = 3, |
| 62 | .pin = 26, |
| 63 | .func = 1, |
| 64 | .route_offset = 0x50, |
| 65 | .route_val = BIT(16 + 3), |
| 66 | }, { |
| 67 | /* pwm3-1 */ |
| 68 | .bank_num = 1, |
| 69 | .pin = 11, |
| 70 | .func = 2, |
| 71 | .route_offset = 0x50, |
| 72 | .route_val = BIT(16 + 3) | BIT(3), |
| 73 | }, { |
| 74 | /* sdio-0_d0 */ |
| 75 | .bank_num = 1, |
| 76 | .pin = 1, |
| 77 | .func = 1, |
| 78 | .route_offset = 0x50, |
| 79 | .route_val = BIT(16 + 4), |
| 80 | }, { |
| 81 | /* sdio-1_d0 */ |
| 82 | .bank_num = 3, |
| 83 | .pin = 2, |
| 84 | .func = 1, |
| 85 | .route_offset = 0x50, |
| 86 | .route_val = BIT(16 + 4) | BIT(4), |
| 87 | }, { |
| 88 | /* spi-0_rx */ |
| 89 | .bank_num = 0, |
| 90 | .pin = 13, |
| 91 | .func = 2, |
| 92 | .route_offset = 0x50, |
| 93 | .route_val = BIT(16 + 5), |
| 94 | }, { |
| 95 | /* spi-1_rx */ |
| 96 | .bank_num = 2, |
| 97 | .pin = 0, |
| 98 | .func = 2, |
| 99 | .route_offset = 0x50, |
| 100 | .route_val = BIT(16 + 5) | BIT(5), |
| 101 | }, { |
| 102 | /* emmc-0_cmd */ |
| 103 | .bank_num = 1, |
| 104 | .pin = 22, |
| 105 | .func = 2, |
| 106 | .route_offset = 0x50, |
| 107 | .route_val = BIT(16 + 7), |
| 108 | }, { |
| 109 | /* emmc-1_cmd */ |
| 110 | .bank_num = 2, |
| 111 | .pin = 4, |
| 112 | .func = 2, |
| 113 | .route_offset = 0x50, |
| 114 | .route_val = BIT(16 + 7) | BIT(7), |
| 115 | }, { |
| 116 | /* uart2-0_rx */ |
| 117 | .bank_num = 1, |
| 118 | .pin = 19, |
| 119 | .func = 2, |
| 120 | .route_offset = 0x50, |
| 121 | .route_val = BIT(16 + 8), |
| 122 | }, { |
| 123 | /* uart2-1_rx */ |
| 124 | .bank_num = 1, |
| 125 | .pin = 10, |
| 126 | .func = 2, |
| 127 | .route_offset = 0x50, |
| 128 | .route_val = BIT(16 + 8) | BIT(8), |
| 129 | }, { |
| 130 | /* uart1-0_rx */ |
| 131 | .bank_num = 1, |
| 132 | .pin = 10, |
| 133 | .func = 1, |
| 134 | .route_offset = 0x50, |
| 135 | .route_val = BIT(16 + 11), |
| 136 | }, { |
| 137 | /* uart1-1_rx */ |
| 138 | .bank_num = 3, |
| 139 | .pin = 13, |
| 140 | .func = 1, |
| 141 | .route_offset = 0x50, |
| 142 | .route_val = BIT(16 + 11) | BIT(11), |
| 143 | }, |
| 144 | }; |
| 145 | |
David Wu | 3dd7d6c | 2019-04-16 21:50:55 +0800 | [diff] [blame] | 146 | static int rk3228_set_mux(struct rockchip_pin_bank *bank, int pin, int mux) |
| 147 | { |
| 148 | struct rockchip_pinctrl_priv *priv = bank->priv; |
| 149 | int iomux_num = (pin / 8); |
| 150 | struct regmap *regmap; |
| 151 | int reg, ret, mask, mux_type; |
| 152 | u8 bit; |
Jagan Teki | 9e0e681 | 2022-12-14 23:20:56 +0530 | [diff] [blame] | 153 | u32 data; |
David Wu | 3dd7d6c | 2019-04-16 21:50:55 +0800 | [diff] [blame] | 154 | |
| 155 | regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU) |
| 156 | ? priv->regmap_pmu : priv->regmap_base; |
| 157 | |
| 158 | /* get basic quadrupel of mux registers and the correct reg inside */ |
| 159 | mux_type = bank->iomux[iomux_num].type; |
| 160 | reg = bank->iomux[iomux_num].offset; |
| 161 | reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask); |
| 162 | |
David Wu | 3dd7d6c | 2019-04-16 21:50:55 +0800 | [diff] [blame] | 163 | data = (mask << (bit + 16)); |
| 164 | data |= (mux & mask) << bit; |
| 165 | ret = regmap_write(regmap, reg, data); |
| 166 | |
| 167 | return ret; |
| 168 | } |
| 169 | |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 170 | #define RK3228_PULL_OFFSET 0x100 |
| 171 | |
| 172 | static void rk3228_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, |
| 173 | int pin_num, struct regmap **regmap, |
| 174 | int *reg, u8 *bit) |
| 175 | { |
| 176 | struct rockchip_pinctrl_priv *priv = bank->priv; |
| 177 | |
| 178 | *regmap = priv->regmap_base; |
| 179 | *reg = RK3228_PULL_OFFSET; |
| 180 | *reg += bank->bank_num * ROCKCHIP_PULL_BANK_STRIDE; |
| 181 | *reg += ((pin_num / ROCKCHIP_PULL_PINS_PER_REG) * 4); |
| 182 | |
| 183 | *bit = (pin_num % ROCKCHIP_PULL_PINS_PER_REG); |
| 184 | *bit *= ROCKCHIP_PULL_BITS_PER_PIN; |
| 185 | } |
| 186 | |
David Wu | 2972c45 | 2019-04-16 21:57:05 +0800 | [diff] [blame] | 187 | static int rk3228_set_pull(struct rockchip_pin_bank *bank, |
| 188 | int pin_num, int pull) |
| 189 | { |
| 190 | struct regmap *regmap; |
| 191 | int reg, ret; |
| 192 | u8 bit, type; |
| 193 | u32 data; |
| 194 | |
| 195 | if (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT) |
| 196 | return -ENOTSUPP; |
| 197 | |
| 198 | rk3228_calc_pull_reg_and_bit(bank, pin_num, ®map, ®, &bit); |
| 199 | type = bank->pull_type[pin_num / 8]; |
| 200 | ret = rockchip_translate_pull_value(type, pull); |
| 201 | if (ret < 0) { |
| 202 | debug("unsupported pull setting %d\n", pull); |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | /* enable the write to the equivalent lower bits */ |
| 207 | data = ((1 << ROCKCHIP_PULL_BITS_PER_PIN) - 1) << (bit + 16); |
| 208 | data |= (ret << bit); |
| 209 | ret = regmap_write(regmap, reg, data); |
| 210 | |
| 211 | return ret; |
| 212 | } |
| 213 | |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 214 | #define RK3228_DRV_GRF_OFFSET 0x200 |
| 215 | |
| 216 | static void rk3228_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank, |
| 217 | int pin_num, struct regmap **regmap, |
| 218 | int *reg, u8 *bit) |
| 219 | { |
| 220 | struct rockchip_pinctrl_priv *priv = bank->priv; |
| 221 | |
| 222 | *regmap = priv->regmap_base; |
| 223 | *reg = RK3228_DRV_GRF_OFFSET; |
| 224 | *reg += bank->bank_num * ROCKCHIP_DRV_BANK_STRIDE; |
| 225 | *reg += ((pin_num / ROCKCHIP_DRV_PINS_PER_REG) * 4); |
| 226 | |
| 227 | *bit = (pin_num % ROCKCHIP_DRV_PINS_PER_REG); |
| 228 | *bit *= ROCKCHIP_DRV_BITS_PER_PIN; |
| 229 | } |
| 230 | |
David Wu | 40a5548 | 2019-04-16 21:55:26 +0800 | [diff] [blame] | 231 | static int rk3228_set_drive(struct rockchip_pin_bank *bank, |
| 232 | int pin_num, int strength) |
| 233 | { |
| 234 | struct regmap *regmap; |
| 235 | int reg, ret; |
| 236 | u32 data; |
| 237 | u8 bit; |
| 238 | int type = bank->drv[pin_num / 8].drv_type; |
| 239 | |
| 240 | rk3228_calc_drv_reg_and_bit(bank, pin_num, ®map, ®, &bit); |
| 241 | ret = rockchip_translate_drive_value(type, strength); |
| 242 | if (ret < 0) { |
| 243 | debug("unsupported driver strength %d\n", strength); |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | /* enable the write to the equivalent lower bits */ |
| 248 | data = ((1 << ROCKCHIP_DRV_BITS_PER_PIN) - 1) << (bit + 16); |
| 249 | data |= (ret << bit); |
| 250 | ret = regmap_write(regmap, reg, data); |
| 251 | return ret; |
| 252 | } |
| 253 | |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 254 | static struct rockchip_pin_bank rk3228_pin_banks[] = { |
| 255 | PIN_BANK(0, 32, "gpio0"), |
| 256 | PIN_BANK(1, 32, "gpio1"), |
| 257 | PIN_BANK(2, 32, "gpio2"), |
| 258 | PIN_BANK(3, 32, "gpio3"), |
| 259 | }; |
| 260 | |
| 261 | static struct rockchip_pin_ctrl rk3228_pin_ctrl = { |
David Wu | 71aede0 | 2019-04-16 21:50:54 +0800 | [diff] [blame] | 262 | .pin_banks = rk3228_pin_banks, |
| 263 | .nr_banks = ARRAY_SIZE(rk3228_pin_banks), |
David Wu | 71aede0 | 2019-04-16 21:50:54 +0800 | [diff] [blame] | 264 | .grf_mux_offset = 0x0, |
| 265 | .iomux_routes = rk3228_mux_route_data, |
| 266 | .niomux_routes = ARRAY_SIZE(rk3228_mux_route_data), |
David Wu | 3dd7d6c | 2019-04-16 21:50:55 +0800 | [diff] [blame] | 267 | .set_mux = rk3228_set_mux, |
David Wu | 2972c45 | 2019-04-16 21:57:05 +0800 | [diff] [blame] | 268 | .set_pull = rk3228_set_pull, |
David Wu | 40a5548 | 2019-04-16 21:55:26 +0800 | [diff] [blame] | 269 | .set_drive = rk3228_set_drive, |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 270 | }; |
| 271 | |
| 272 | static const struct udevice_id rk3228_pinctrl_ids[] = { |
| 273 | { |
| 274 | .compatible = "rockchip,rk3228-pinctrl", |
| 275 | .data = (ulong)&rk3228_pin_ctrl |
| 276 | }, |
| 277 | { } |
| 278 | }; |
| 279 | |
| 280 | U_BOOT_DRIVER(pinctrl_rk3228) = { |
| 281 | .name = "rockchip_rk3228_pinctrl", |
| 282 | .id = UCLASS_PINCTRL, |
| 283 | .of_match = rk3228_pinctrl_ids, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 284 | .priv_auto = sizeof(struct rockchip_pinctrl_priv), |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 285 | .ops = &rockchip_pinctrl_ops, |
Simon Glass | 9288265 | 2021-08-07 07:24:04 -0600 | [diff] [blame] | 286 | #if CONFIG_IS_ENABLED(OF_REAL) |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 287 | .bind = dm_scan_fdt_dev, |
| 288 | #endif |
| 289 | .probe = rockchip_pinctrl_probe, |
| 290 | }; |