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> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 11 | #include <linux/bitops.h> |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 12 | |
| 13 | #include "pinctrl-rockchip.h" |
| 14 | |
| 15 | static struct rockchip_mux_route_data rk3288_mux_route_data[] = { |
| 16 | { |
| 17 | /* edphdmi_cecinoutt1 */ |
| 18 | .bank_num = 7, |
| 19 | .pin = 16, |
| 20 | .func = 2, |
| 21 | .route_offset = 0x264, |
| 22 | .route_val = BIT(16 + 12) | BIT(12), |
| 23 | }, { |
| 24 | /* edphdmi_cecinout */ |
| 25 | .bank_num = 7, |
| 26 | .pin = 23, |
| 27 | .func = 4, |
| 28 | .route_offset = 0x264, |
| 29 | .route_val = BIT(16 + 12), |
| 30 | }, |
| 31 | }; |
| 32 | |
David Wu | 3dd7d6c | 2019-04-16 21:50:55 +0800 | [diff] [blame] | 33 | static int rk3288_set_mux(struct rockchip_pin_bank *bank, int pin, int mux) |
| 34 | { |
| 35 | struct rockchip_pinctrl_priv *priv = bank->priv; |
| 36 | int iomux_num = (pin / 8); |
| 37 | struct regmap *regmap; |
| 38 | int reg, ret, mask, mux_type; |
| 39 | u8 bit; |
| 40 | u32 data, route_reg, route_val; |
| 41 | |
| 42 | regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU) |
| 43 | ? priv->regmap_pmu : priv->regmap_base; |
| 44 | |
| 45 | /* get basic quadrupel of mux registers and the correct reg inside */ |
| 46 | mux_type = bank->iomux[iomux_num].type; |
| 47 | reg = bank->iomux[iomux_num].offset; |
| 48 | reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask); |
| 49 | |
| 50 | if (bank->route_mask & BIT(pin)) { |
| 51 | if (rockchip_get_mux_route(bank, pin, mux, &route_reg, |
| 52 | &route_val)) { |
| 53 | ret = regmap_write(regmap, route_reg, route_val); |
| 54 | if (ret) |
| 55 | return ret; |
| 56 | } |
| 57 | } |
| 58 | |
David Wu | 84248ec | 2019-04-16 21:50:56 +0800 | [diff] [blame] | 59 | /* bank0 is special, there are no higher 16 bit writing bits. */ |
| 60 | if (bank->bank_num == 0) { |
| 61 | regmap_read(regmap, reg, &data); |
| 62 | data &= ~(mask << bit); |
| 63 | } else { |
| 64 | /* enable the write to the equivalent lower bits */ |
| 65 | data = (mask << (bit + 16)); |
| 66 | } |
| 67 | |
David Wu | 3dd7d6c | 2019-04-16 21:50:55 +0800 | [diff] [blame] | 68 | data |= (mux & mask) << bit; |
| 69 | ret = regmap_write(regmap, reg, data); |
| 70 | |
| 71 | return ret; |
| 72 | } |
| 73 | |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 74 | #define RK3288_PULL_OFFSET 0x140 |
| 75 | #define RK3288_PULL_PMU_OFFSET 0x64 |
| 76 | |
| 77 | static void rk3288_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, |
| 78 | int pin_num, struct regmap **regmap, |
| 79 | int *reg, u8 *bit) |
| 80 | { |
| 81 | struct rockchip_pinctrl_priv *priv = bank->priv; |
| 82 | |
| 83 | /* The first 24 pins of the first bank are located in PMU */ |
| 84 | if (bank->bank_num == 0) { |
| 85 | *regmap = priv->regmap_pmu; |
| 86 | *reg = RK3288_PULL_PMU_OFFSET; |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 87 | } else { |
| 88 | *regmap = priv->regmap_base; |
| 89 | *reg = RK3288_PULL_OFFSET; |
| 90 | |
| 91 | /* correct the offset, as we're starting with the 2nd bank */ |
| 92 | *reg -= 0x10; |
| 93 | *reg += bank->bank_num * ROCKCHIP_PULL_BANK_STRIDE; |
David Wu | 2972c45 | 2019-04-16 21:57:05 +0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | *reg += ((pin_num / ROCKCHIP_PULL_PINS_PER_REG) * 4); |
| 97 | |
| 98 | *bit = (pin_num % ROCKCHIP_PULL_PINS_PER_REG); |
| 99 | *bit *= ROCKCHIP_PULL_BITS_PER_PIN; |
| 100 | } |
| 101 | |
| 102 | static int rk3288_set_pull(struct rockchip_pin_bank *bank, |
| 103 | int pin_num, int pull) |
| 104 | { |
| 105 | struct regmap *regmap; |
| 106 | int reg, ret; |
| 107 | u8 bit, type; |
| 108 | u32 data; |
| 109 | |
| 110 | if (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT) |
| 111 | return -ENOTSUPP; |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 112 | |
David Wu | 2972c45 | 2019-04-16 21:57:05 +0800 | [diff] [blame] | 113 | rk3288_calc_pull_reg_and_bit(bank, pin_num, ®map, ®, &bit); |
| 114 | type = bank->pull_type[pin_num / 8]; |
| 115 | ret = rockchip_translate_pull_value(type, pull); |
| 116 | if (ret < 0) { |
| 117 | debug("unsupported pull setting %d\n", pull); |
| 118 | return ret; |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 119 | } |
David Wu | 2972c45 | 2019-04-16 21:57:05 +0800 | [diff] [blame] | 120 | |
David Wu | 2e6959e | 2019-04-16 21:57:28 +0800 | [diff] [blame] | 121 | /* bank0 is special, there are no higher 16 bit writing bits */ |
| 122 | if (bank->bank_num == 0) { |
| 123 | regmap_read(regmap, reg, &data); |
| 124 | data &= ~(((1 << ROCKCHIP_PULL_BITS_PER_PIN) - 1) << bit); |
| 125 | } else { |
| 126 | /* enable the write to the equivalent lower bits */ |
| 127 | data = ((1 << ROCKCHIP_PULL_BITS_PER_PIN) - 1) << (bit + 16); |
| 128 | } |
| 129 | |
David Wu | 2972c45 | 2019-04-16 21:57:05 +0800 | [diff] [blame] | 130 | data |= (ret << bit); |
| 131 | ret = regmap_write(regmap, reg, data); |
| 132 | |
| 133 | return ret; |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | #define RK3288_DRV_PMU_OFFSET 0x70 |
| 137 | #define RK3288_DRV_GRF_OFFSET 0x1c0 |
| 138 | |
| 139 | static void rk3288_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank, |
| 140 | int pin_num, struct regmap **regmap, |
| 141 | int *reg, u8 *bit) |
| 142 | { |
| 143 | struct rockchip_pinctrl_priv *priv = bank->priv; |
| 144 | |
| 145 | /* The first 24 pins of the first bank are located in PMU */ |
| 146 | if (bank->bank_num == 0) { |
| 147 | *regmap = priv->regmap_pmu; |
| 148 | *reg = RK3288_DRV_PMU_OFFSET; |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 149 | } else { |
| 150 | *regmap = priv->regmap_base; |
| 151 | *reg = RK3288_DRV_GRF_OFFSET; |
| 152 | |
| 153 | /* correct the offset, as we're starting with the 2nd bank */ |
| 154 | *reg -= 0x10; |
| 155 | *reg += bank->bank_num * ROCKCHIP_DRV_BANK_STRIDE; |
David Wu | 40a5548 | 2019-04-16 21:55:26 +0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | *reg += ((pin_num / ROCKCHIP_DRV_PINS_PER_REG) * 4); |
| 159 | *bit = (pin_num % ROCKCHIP_DRV_PINS_PER_REG); |
| 160 | *bit *= ROCKCHIP_DRV_BITS_PER_PIN; |
| 161 | } |
| 162 | |
| 163 | static int rk3288_set_drive(struct rockchip_pin_bank *bank, |
| 164 | int pin_num, int strength) |
| 165 | { |
| 166 | struct regmap *regmap; |
| 167 | int reg, ret; |
| 168 | u32 data; |
| 169 | u8 bit; |
| 170 | int type = bank->drv[pin_num / 8].drv_type; |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 171 | |
David Wu | 40a5548 | 2019-04-16 21:55:26 +0800 | [diff] [blame] | 172 | rk3288_calc_drv_reg_and_bit(bank, pin_num, ®map, ®, &bit); |
| 173 | ret = rockchip_translate_drive_value(type, strength); |
| 174 | if (ret < 0) { |
| 175 | debug("unsupported driver strength %d\n", strength); |
| 176 | return ret; |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 177 | } |
David Wu | 40a5548 | 2019-04-16 21:55:26 +0800 | [diff] [blame] | 178 | |
David Wu | 1530047 | 2019-04-16 21:56:34 +0800 | [diff] [blame] | 179 | /* bank0 is special, there are no higher 16 bit writing bits. */ |
| 180 | if (bank->bank_num == 0) { |
| 181 | regmap_read(regmap, reg, &data); |
| 182 | data &= ~(((1 << ROCKCHIP_DRV_BITS_PER_PIN) - 1) << bit); |
| 183 | } else { |
| 184 | /* enable the write to the equivalent lower bits */ |
| 185 | data = ((1 << ROCKCHIP_DRV_BITS_PER_PIN) - 1) << (bit + 16); |
| 186 | } |
| 187 | |
David Wu | 40a5548 | 2019-04-16 21:55:26 +0800 | [diff] [blame] | 188 | data |= (ret << bit); |
| 189 | ret = regmap_write(regmap, reg, data); |
| 190 | return ret; |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static struct rockchip_pin_bank rk3288_pin_banks[] = { |
Kever Yang | 56573c4 | 2019-05-07 09:36:32 +0800 | [diff] [blame] | 194 | PIN_BANK_IOMUX_FLAGS(0, 24, "gpio0", IOMUX_SOURCE_PMU, |
| 195 | IOMUX_SOURCE_PMU, |
| 196 | IOMUX_SOURCE_PMU, |
| 197 | IOMUX_UNROUTED |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 198 | ), |
| 199 | PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_UNROUTED, |
| 200 | IOMUX_UNROUTED, |
| 201 | IOMUX_UNROUTED, |
| 202 | 0 |
| 203 | ), |
| 204 | PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, IOMUX_UNROUTED), |
| 205 | PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, IOMUX_WIDTH_4BIT), |
| 206 | PIN_BANK_IOMUX_FLAGS(4, 32, "gpio4", IOMUX_WIDTH_4BIT, |
| 207 | IOMUX_WIDTH_4BIT, |
| 208 | 0, |
| 209 | 0 |
| 210 | ), |
| 211 | PIN_BANK_IOMUX_FLAGS(5, 32, "gpio5", IOMUX_UNROUTED, |
| 212 | 0, |
| 213 | 0, |
| 214 | IOMUX_UNROUTED |
| 215 | ), |
| 216 | PIN_BANK_IOMUX_FLAGS(6, 32, "gpio6", 0, 0, 0, IOMUX_UNROUTED), |
| 217 | PIN_BANK_IOMUX_FLAGS(7, 32, "gpio7", 0, |
| 218 | 0, |
| 219 | IOMUX_WIDTH_4BIT, |
| 220 | IOMUX_UNROUTED |
| 221 | ), |
| 222 | PIN_BANK(8, 16, "gpio8"), |
| 223 | }; |
| 224 | |
| 225 | static struct rockchip_pin_ctrl rk3288_pin_ctrl = { |
David Wu | 71aede0 | 2019-04-16 21:50:54 +0800 | [diff] [blame] | 226 | .pin_banks = rk3288_pin_banks, |
| 227 | .nr_banks = ARRAY_SIZE(rk3288_pin_banks), |
David Wu | 71aede0 | 2019-04-16 21:50:54 +0800 | [diff] [blame] | 228 | .grf_mux_offset = 0x0, |
| 229 | .pmu_mux_offset = 0x84, |
| 230 | .iomux_routes = rk3288_mux_route_data, |
| 231 | .niomux_routes = ARRAY_SIZE(rk3288_mux_route_data), |
David Wu | 3dd7d6c | 2019-04-16 21:50:55 +0800 | [diff] [blame] | 232 | .set_mux = rk3288_set_mux, |
David Wu | 2972c45 | 2019-04-16 21:57:05 +0800 | [diff] [blame] | 233 | .set_pull = rk3288_set_pull, |
David Wu | 40a5548 | 2019-04-16 21:55:26 +0800 | [diff] [blame] | 234 | .set_drive = rk3288_set_drive, |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | static const struct udevice_id rk3288_pinctrl_ids[] = { |
| 238 | { |
| 239 | .compatible = "rockchip,rk3288-pinctrl", |
| 240 | .data = (ulong)&rk3288_pin_ctrl |
| 241 | }, |
| 242 | { } |
| 243 | }; |
| 244 | |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 245 | U_BOOT_DRIVER(rockchip_rk3288_pinctrl) = { |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 246 | .name = "rockchip_rk3288_pinctrl", |
| 247 | .id = UCLASS_PINCTRL, |
| 248 | .of_match = rk3288_pinctrl_ids, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 249 | .priv_auto = sizeof(struct rockchip_pinctrl_priv), |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 250 | .ops = &rockchip_pinctrl_ops, |
Simon Glass | 9288265 | 2021-08-07 07:24:04 -0600 | [diff] [blame] | 251 | #if CONFIG_IS_ENABLED(OF_REAL) |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 252 | .bind = dm_scan_fdt_dev, |
| 253 | #endif |
| 254 | .probe = rockchip_pinctrl_probe, |
| 255 | }; |