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