1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2019 Rockchip Electronics Co., Ltd
8 #include <dm/pinctrl.h>
12 #include "pinctrl-rockchip.h"
14 static struct rockchip_mux_route_data rk3399_mux_route_data[] = {
20 .route_offset = 0xe21c,
21 .route_val = BIT(16 + 10) | BIT(16 + 11),
27 .route_offset = 0xe21c,
28 .route_val = BIT(16 + 10) | BIT(16 + 11) | BIT(10),
34 .route_offset = 0xe21c,
35 .route_val = BIT(16 + 10) | BIT(16 + 11) | BIT(11),
41 .route_offset = 0xe21c,
42 .route_val = BIT(16 + 14),
48 .route_offset = 0xe21c,
49 .route_val = BIT(16 + 14) | BIT(14),
53 static int rk3399_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
55 struct rockchip_pinctrl_priv *priv = bank->priv;
56 int iomux_num = (pin / 8);
57 struct regmap *regmap;
58 int reg, ret, mask, mux_type;
60 u32 data, route_reg, route_val;
62 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
63 ? priv->regmap_pmu : priv->regmap_base;
65 /* get basic quadrupel of mux registers and the correct reg inside */
66 mux_type = bank->iomux[iomux_num].type;
67 reg = bank->iomux[iomux_num].offset;
68 reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask);
70 if (bank->route_mask & BIT(pin)) {
71 if (rockchip_get_mux_route(bank, pin, mux, &route_reg,
73 ret = regmap_write(regmap, route_reg, route_val);
79 data = (mask << (bit + 16));
80 data |= (mux & mask) << bit;
81 ret = regmap_write(regmap, reg, data);
86 #define RK3399_PULL_GRF_OFFSET 0xe040
87 #define RK3399_PULL_PMU_OFFSET 0x40
89 static void rk3399_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
90 int pin_num, struct regmap **regmap,
93 struct rockchip_pinctrl_priv *priv = bank->priv;
95 /* The bank0:16 and bank1:32 pins are located in PMU */
96 if (bank->bank_num == 0 || bank->bank_num == 1) {
97 *regmap = priv->regmap_pmu;
98 *reg = RK3399_PULL_PMU_OFFSET;
100 *reg += bank->bank_num * ROCKCHIP_PULL_BANK_STRIDE;
102 *regmap = priv->regmap_base;
103 *reg = RK3399_PULL_GRF_OFFSET;
105 /* correct the offset, as we're starting with the 3rd bank */
107 *reg += bank->bank_num * ROCKCHIP_PULL_BANK_STRIDE;
110 *reg += ((pin_num / ROCKCHIP_PULL_PINS_PER_REG) * 4);
112 *bit = (pin_num % ROCKCHIP_PULL_PINS_PER_REG);
113 *bit *= ROCKCHIP_PULL_BITS_PER_PIN;
116 static int rk3399_set_pull(struct rockchip_pin_bank *bank,
117 int pin_num, int pull)
119 struct regmap *regmap;
124 if (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT)
127 rk3399_calc_pull_reg_and_bit(bank, pin_num, ®map, ®, &bit);
128 type = bank->pull_type[pin_num / 8];
129 ret = rockchip_translate_pull_value(type, pull);
131 debug("unsupported pull setting %d\n", pull);
135 /* enable the write to the equivalent lower bits */
136 data = ((1 << ROCKCHIP_PULL_BITS_PER_PIN) - 1) << (bit + 16);
137 data |= (ret << bit);
138 ret = regmap_write(regmap, reg, data);
143 static void rk3399_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
144 int pin_num, struct regmap **regmap,
147 struct rockchip_pinctrl_priv *priv = bank->priv;
148 int drv_num = (pin_num / 8);
150 /* The bank0:16 and bank1:32 pins are located in PMU */
151 if (bank->bank_num == 0 || bank->bank_num == 1)
152 *regmap = priv->regmap_pmu;
154 *regmap = priv->regmap_base;
156 *reg = bank->drv[drv_num].offset;
157 if (bank->drv[drv_num].drv_type == DRV_TYPE_IO_1V8_3V0_AUTO ||
158 bank->drv[drv_num].drv_type == DRV_TYPE_IO_3V3_ONLY)
159 *bit = (pin_num % 8) * 3;
161 *bit = (pin_num % 8) * 2;
164 static int rk3399_set_drive(struct rockchip_pin_bank *bank,
165 int pin_num, int strength)
167 struct regmap *regmap;
169 u32 data, rmask_bits, temp;
171 int drv_type = bank->drv[pin_num / 8].drv_type;
173 rk3399_calc_drv_reg_and_bit(bank, pin_num, ®map, ®, &bit);
174 ret = rockchip_translate_drive_value(drv_type, strength);
176 debug("unsupported driver strength %d\n", strength);
181 case DRV_TYPE_IO_1V8_3V0_AUTO:
182 case DRV_TYPE_IO_3V3_ONLY:
183 rmask_bits = ROCKCHIP_DRV_3BITS_PER_PIN;
186 /* regular case, nothing to do */
190 * drive-strength offset is special, as it is spread
191 * over 2 registers, the bit data[15] contains bit 0
192 * of the value while temp[1:0] contains bits 2 and 1
194 data = (ret & 0x1) << 15;
195 temp = (ret >> 0x1) & 0x3;
198 ret = regmap_write(regmap, reg, data);
204 ret = regmap_write(regmap, reg, temp);
208 /* setting fully enclosed in the second register */
213 debug("unsupported bit: %d for pinctrl drive type: %d\n",
218 case DRV_TYPE_IO_DEFAULT:
219 case DRV_TYPE_IO_1V8_OR_3V0:
220 case DRV_TYPE_IO_1V8_ONLY:
221 rmask_bits = ROCKCHIP_DRV_BITS_PER_PIN;
224 debug("unsupported pinctrl drive type: %d\n",
229 /* enable the write to the equivalent lower bits */
230 data = ((1 << rmask_bits) - 1) << (bit + 16);
231 data |= (ret << bit);
232 ret = regmap_write(regmap, reg, data);
237 static struct rockchip_pin_bank rk3399_pin_banks[] = {
238 PIN_BANK_IOMUX_FLAGS_DRV_FLAGS_OFFSET_PULL_FLAGS(0, 32, "gpio0",
243 DRV_TYPE_IO_1V8_ONLY,
244 DRV_TYPE_IO_1V8_ONLY,
251 PULL_TYPE_IO_1V8_ONLY,
252 PULL_TYPE_IO_1V8_ONLY,
253 PULL_TYPE_IO_DEFAULT,
256 PIN_BANK_IOMUX_DRV_FLAGS_OFFSET(1, 32, "gpio1", IOMUX_SOURCE_PMU,
260 DRV_TYPE_IO_1V8_OR_3V0,
261 DRV_TYPE_IO_1V8_OR_3V0,
262 DRV_TYPE_IO_1V8_OR_3V0,
263 DRV_TYPE_IO_1V8_OR_3V0,
269 PIN_BANK_DRV_FLAGS_PULL_FLAGS(2, 32, "gpio2", DRV_TYPE_IO_1V8_OR_3V0,
270 DRV_TYPE_IO_1V8_OR_3V0,
271 DRV_TYPE_IO_1V8_ONLY,
272 DRV_TYPE_IO_1V8_ONLY,
273 PULL_TYPE_IO_DEFAULT,
274 PULL_TYPE_IO_DEFAULT,
275 PULL_TYPE_IO_1V8_ONLY,
276 PULL_TYPE_IO_1V8_ONLY
278 PIN_BANK_DRV_FLAGS(3, 32, "gpio3", DRV_TYPE_IO_3V3_ONLY,
279 DRV_TYPE_IO_3V3_ONLY,
280 DRV_TYPE_IO_3V3_ONLY,
281 DRV_TYPE_IO_1V8_OR_3V0
283 PIN_BANK_DRV_FLAGS(4, 32, "gpio4", DRV_TYPE_IO_1V8_OR_3V0,
284 DRV_TYPE_IO_1V8_3V0_AUTO,
285 DRV_TYPE_IO_1V8_OR_3V0,
286 DRV_TYPE_IO_1V8_OR_3V0
290 static struct rockchip_pin_ctrl rk3399_pin_ctrl = {
291 .pin_banks = rk3399_pin_banks,
292 .nr_banks = ARRAY_SIZE(rk3399_pin_banks),
293 .label = "RK3399-GPIO",
295 .grf_mux_offset = 0xe000,
296 .pmu_mux_offset = 0x0,
297 .grf_drv_offset = 0xe100,
298 .pmu_drv_offset = 0x80,
299 .iomux_routes = rk3399_mux_route_data,
300 .niomux_routes = ARRAY_SIZE(rk3399_mux_route_data),
301 .set_mux = rk3399_set_mux,
302 .set_pull = rk3399_set_pull,
303 .set_drive = rk3399_set_drive,
306 static const struct udevice_id rk3399_pinctrl_ids[] = {
308 .compatible = "rockchip,rk3399-pinctrl",
309 .data = (ulong)&rk3399_pin_ctrl
314 U_BOOT_DRIVER(pinctrl_rk3399) = {
315 .name = "rockchip_rk3399_pinctrl",
316 .id = UCLASS_PINCTRL,
317 .of_match = rk3399_pinctrl_ids,
318 .priv_auto_alloc_size = sizeof(struct rockchip_pinctrl_priv),
319 .ops = &rockchip_pinctrl_ops,
320 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
321 .bind = dm_scan_fdt_dev,
323 .probe = rockchip_pinctrl_probe,