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> |
| 10 | #include <syscon.h> |
| 11 | |
| 12 | #include "pinctrl-rockchip.h" |
| 13 | |
| 14 | static struct rockchip_mux_recalced_data rv1108_mux_recalced_data[] = { |
| 15 | { |
| 16 | .num = 1, |
| 17 | .pin = 0, |
| 18 | .reg = 0x418, |
| 19 | .bit = 0, |
| 20 | .mask = 0x3 |
| 21 | }, { |
| 22 | .num = 1, |
| 23 | .pin = 1, |
| 24 | .reg = 0x418, |
| 25 | .bit = 2, |
| 26 | .mask = 0x3 |
| 27 | }, { |
| 28 | .num = 1, |
| 29 | .pin = 2, |
| 30 | .reg = 0x418, |
| 31 | .bit = 4, |
| 32 | .mask = 0x3 |
| 33 | }, { |
| 34 | .num = 1, |
| 35 | .pin = 3, |
| 36 | .reg = 0x418, |
| 37 | .bit = 6, |
| 38 | .mask = 0x3 |
| 39 | }, { |
| 40 | .num = 1, |
| 41 | .pin = 4, |
| 42 | .reg = 0x418, |
| 43 | .bit = 8, |
| 44 | .mask = 0x3 |
| 45 | }, { |
| 46 | .num = 1, |
| 47 | .pin = 5, |
| 48 | .reg = 0x418, |
| 49 | .bit = 10, |
| 50 | .mask = 0x3 |
| 51 | }, { |
| 52 | .num = 1, |
| 53 | .pin = 6, |
| 54 | .reg = 0x418, |
| 55 | .bit = 12, |
| 56 | .mask = 0x3 |
| 57 | }, { |
| 58 | .num = 1, |
| 59 | .pin = 7, |
| 60 | .reg = 0x418, |
| 61 | .bit = 14, |
| 62 | .mask = 0x3 |
| 63 | }, { |
| 64 | .num = 1, |
| 65 | .pin = 8, |
| 66 | .reg = 0x41c, |
| 67 | .bit = 0, |
| 68 | .mask = 0x3 |
| 69 | }, { |
| 70 | .num = 1, |
| 71 | .pin = 9, |
| 72 | .reg = 0x41c, |
| 73 | .bit = 2, |
| 74 | .mask = 0x3 |
| 75 | }, |
| 76 | }; |
| 77 | |
David Wu | 3dd7d6c | 2019-04-16 21:50:55 +0800 | [diff] [blame^] | 78 | static int rv1108_set_mux(struct rockchip_pin_bank *bank, int pin, int mux) |
| 79 | { |
| 80 | struct rockchip_pinctrl_priv *priv = bank->priv; |
| 81 | int iomux_num = (pin / 8); |
| 82 | struct regmap *regmap; |
| 83 | int reg, ret, mask, mux_type; |
| 84 | u8 bit; |
| 85 | u32 data; |
| 86 | |
| 87 | regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU) |
| 88 | ? priv->regmap_pmu : priv->regmap_base; |
| 89 | |
| 90 | /* get basic quadrupel of mux registers and the correct reg inside */ |
| 91 | mux_type = bank->iomux[iomux_num].type; |
| 92 | reg = bank->iomux[iomux_num].offset; |
| 93 | reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask); |
| 94 | |
| 95 | if (bank->recalced_mask & BIT(pin)) |
| 96 | rockchip_get_recalced_mux(bank, pin, ®, &bit, &mask); |
| 97 | |
| 98 | data = (mask << (bit + 16)); |
| 99 | data |= (mux & mask) << bit; |
| 100 | ret = regmap_write(regmap, reg, data); |
| 101 | |
| 102 | return ret; |
| 103 | } |
| 104 | |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 105 | #define RV1108_PULL_PMU_OFFSET 0x10 |
| 106 | #define RV1108_PULL_OFFSET 0x110 |
| 107 | |
| 108 | static void rv1108_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, |
| 109 | int pin_num, struct regmap **regmap, |
| 110 | int *reg, u8 *bit) |
| 111 | { |
| 112 | struct rockchip_pinctrl_priv *priv = bank->priv; |
| 113 | |
| 114 | /* The first 24 pins of the first bank are located in PMU */ |
| 115 | if (bank->bank_num == 0) { |
| 116 | *regmap = priv->regmap_pmu; |
| 117 | *reg = RV1108_PULL_PMU_OFFSET; |
| 118 | } else { |
| 119 | *reg = RV1108_PULL_OFFSET; |
| 120 | *regmap = priv->regmap_base; |
| 121 | /* correct the offset, as we're starting with the 2nd bank */ |
| 122 | *reg -= 0x10; |
| 123 | *reg += bank->bank_num * ROCKCHIP_PULL_BANK_STRIDE; |
| 124 | } |
| 125 | |
| 126 | *reg += ((pin_num / ROCKCHIP_PULL_PINS_PER_REG) * 4); |
| 127 | *bit = (pin_num % ROCKCHIP_PULL_PINS_PER_REG); |
| 128 | *bit *= ROCKCHIP_PULL_BITS_PER_PIN; |
| 129 | } |
| 130 | |
| 131 | #define RV1108_DRV_PMU_OFFSET 0x20 |
| 132 | #define RV1108_DRV_GRF_OFFSET 0x210 |
| 133 | |
| 134 | static void rv1108_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank, |
| 135 | int pin_num, struct regmap **regmap, |
| 136 | int *reg, u8 *bit) |
| 137 | { |
| 138 | struct rockchip_pinctrl_priv *priv = bank->priv; |
| 139 | |
| 140 | /* The first 24 pins of the first bank are located in PMU */ |
| 141 | if (bank->bank_num == 0) { |
| 142 | *regmap = priv->regmap_pmu; |
| 143 | *reg = RV1108_DRV_PMU_OFFSET; |
| 144 | } else { |
| 145 | *regmap = priv->regmap_base; |
| 146 | *reg = RV1108_DRV_GRF_OFFSET; |
| 147 | |
| 148 | /* correct the offset, as we're starting with the 2nd bank */ |
| 149 | *reg -= 0x10; |
| 150 | *reg += bank->bank_num * ROCKCHIP_DRV_BANK_STRIDE; |
| 151 | } |
| 152 | |
| 153 | *reg += ((pin_num / ROCKCHIP_DRV_PINS_PER_REG) * 4); |
| 154 | *bit = pin_num % ROCKCHIP_DRV_PINS_PER_REG; |
| 155 | *bit *= ROCKCHIP_DRV_BITS_PER_PIN; |
| 156 | } |
| 157 | |
| 158 | #define RV1108_SCHMITT_PMU_OFFSET 0x30 |
| 159 | #define RV1108_SCHMITT_GRF_OFFSET 0x388 |
| 160 | #define RV1108_SCHMITT_BANK_STRIDE 8 |
| 161 | #define RV1108_SCHMITT_PINS_PER_GRF_REG 16 |
| 162 | #define RV1108_SCHMITT_PINS_PER_PMU_REG 8 |
| 163 | |
| 164 | static int rv1108_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank, |
| 165 | int pin_num, |
| 166 | struct regmap **regmap, |
| 167 | int *reg, u8 *bit) |
| 168 | { |
| 169 | struct rockchip_pinctrl_priv *priv = bank->priv; |
| 170 | int pins_per_reg; |
| 171 | |
| 172 | if (bank->bank_num == 0) { |
| 173 | *regmap = priv->regmap_pmu; |
| 174 | *reg = RV1108_SCHMITT_PMU_OFFSET; |
| 175 | pins_per_reg = RV1108_SCHMITT_PINS_PER_PMU_REG; |
| 176 | } else { |
| 177 | *regmap = priv->regmap_base; |
| 178 | *reg = RV1108_SCHMITT_GRF_OFFSET; |
| 179 | pins_per_reg = RV1108_SCHMITT_PINS_PER_GRF_REG; |
| 180 | *reg += (bank->bank_num - 1) * RV1108_SCHMITT_BANK_STRIDE; |
| 181 | } |
| 182 | *reg += ((pin_num / pins_per_reg) * 4); |
| 183 | *bit = pin_num % pins_per_reg; |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static struct rockchip_pin_bank rv1108_pin_banks[] = { |
| 189 | PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU, |
| 190 | IOMUX_SOURCE_PMU, |
| 191 | IOMUX_SOURCE_PMU, |
| 192 | IOMUX_SOURCE_PMU), |
| 193 | PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", 0, 0, 0, 0), |
| 194 | PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, 0), |
| 195 | PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, 0), |
| 196 | }; |
| 197 | |
| 198 | static struct rockchip_pin_ctrl rv1108_pin_ctrl = { |
| 199 | .pin_banks = rv1108_pin_banks, |
| 200 | .nr_banks = ARRAY_SIZE(rv1108_pin_banks), |
| 201 | .label = "RV1108-GPIO", |
| 202 | .type = RV1108, |
| 203 | .grf_mux_offset = 0x10, |
| 204 | .pmu_mux_offset = 0x0, |
| 205 | .iomux_recalced = rv1108_mux_recalced_data, |
| 206 | .niomux_recalced = ARRAY_SIZE(rv1108_mux_recalced_data), |
David Wu | 3dd7d6c | 2019-04-16 21:50:55 +0800 | [diff] [blame^] | 207 | .set_mux = rv1108_set_mux, |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 208 | .pull_calc_reg = rv1108_calc_pull_reg_and_bit, |
| 209 | .drv_calc_reg = rv1108_calc_drv_reg_and_bit, |
| 210 | .schmitt_calc_reg = rv1108_calc_schmitt_reg_and_bit, |
| 211 | }; |
| 212 | |
| 213 | static const struct udevice_id rv1108_pinctrl_ids[] = { |
| 214 | { |
| 215 | .compatible = "rockchip,rv1108-pinctrl", |
| 216 | .data = (ulong)&rv1108_pin_ctrl |
| 217 | }, |
| 218 | { } |
| 219 | }; |
| 220 | |
| 221 | U_BOOT_DRIVER(pinctrl_rv1108) = { |
| 222 | .name = "pinctrl_rv1108", |
| 223 | .id = UCLASS_PINCTRL, |
| 224 | .of_match = rv1108_pinctrl_ids, |
| 225 | .priv_auto_alloc_size = sizeof(struct rockchip_pinctrl_priv), |
| 226 | .ops = &rockchip_pinctrl_ops, |
| 227 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 228 | .bind = dm_scan_fdt_dev, |
| 229 | #endif |
| 230 | .probe = rockchip_pinctrl_probe, |
| 231 | }; |