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