1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2019 Rockchip Electronics Co., Ltd
9 #include <dm/pinctrl.h>
12 #include <linux/bitops.h>
14 #include "pinctrl-rockchip.h"
16 static struct rockchip_mux_route_data rk3399_mux_route_data[] = {
22 .route_offset = 0xe21c,
23 .route_val = BIT(16 + 10) | BIT(16 + 11),
29 .route_offset = 0xe21c,
30 .route_val = BIT(16 + 10) | BIT(16 + 11) | BIT(10),
36 .route_offset = 0xe21c,
37 .route_val = BIT(16 + 10) | BIT(16 + 11) | BIT(11),
43 .route_offset = 0xe21c,
44 .route_val = BIT(16 + 14),
50 .route_offset = 0xe21c,
51 .route_val = BIT(16 + 14) | BIT(14),
55 static int rk3399_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
57 struct rockchip_pinctrl_priv *priv = bank->priv;
58 int iomux_num = (pin / 8);
59 struct regmap *regmap;
60 int reg, ret, mask, mux_type;
62 u32 data, route_reg, route_val;
64 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
65 ? priv->regmap_pmu : priv->regmap_base;
67 /* get basic quadrupel of mux registers and the correct reg inside */
68 mux_type = bank->iomux[iomux_num].type;
69 reg = bank->iomux[iomux_num].offset;
70 reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask);
72 if (bank->route_mask & BIT(pin)) {
73 if (rockchip_get_mux_route(bank, pin, mux, &route_reg,
75 ret = regmap_write(regmap, route_reg, route_val);
81 data = (mask << (bit + 16));
82 data |= (mux & mask) << bit;
83 ret = regmap_write(regmap, reg, data);
88 #define RK3399_PULL_GRF_OFFSET 0xe040
89 #define RK3399_PULL_PMU_OFFSET 0x40
91 static void rk3399_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
92 int pin_num, struct regmap **regmap,
95 struct rockchip_pinctrl_priv *priv = bank->priv;
97 /* The bank0:16 and bank1:32 pins are located in PMU */
98 if (bank->bank_num == 0 || bank->bank_num == 1) {
99 *regmap = priv->regmap_pmu;
100 *reg = RK3399_PULL_PMU_OFFSET;
102 *reg += bank->bank_num * ROCKCHIP_PULL_BANK_STRIDE;
104 *regmap = priv->regmap_base;
105 *reg = RK3399_PULL_GRF_OFFSET;
107 /* correct the offset, as we're starting with the 3rd bank */
109 *reg += bank->bank_num * ROCKCHIP_PULL_BANK_STRIDE;
112 *reg += ((pin_num / ROCKCHIP_PULL_PINS_PER_REG) * 4);
114 *bit = (pin_num % ROCKCHIP_PULL_PINS_PER_REG);
115 *bit *= ROCKCHIP_PULL_BITS_PER_PIN;
118 static int rk3399_set_pull(struct rockchip_pin_bank *bank,
119 int pin_num, int pull)
121 struct regmap *regmap;
126 if (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT)
129 rk3399_calc_pull_reg_and_bit(bank, pin_num, ®map, ®, &bit);
130 type = bank->pull_type[pin_num / 8];
131 ret = rockchip_translate_pull_value(type, pull);
133 debug("unsupported pull setting %d\n", pull);
137 /* enable the write to the equivalent lower bits */
138 data = ((1 << ROCKCHIP_PULL_BITS_PER_PIN) - 1) << (bit + 16);
139 data |= (ret << bit);
140 ret = regmap_write(regmap, reg, data);
145 static void rk3399_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
146 int pin_num, struct regmap **regmap,
149 struct rockchip_pinctrl_priv *priv = bank->priv;
150 int drv_num = (pin_num / 8);
152 /* The bank0:16 and bank1:32 pins are located in PMU */
153 if (bank->bank_num == 0 || bank->bank_num == 1)
154 *regmap = priv->regmap_pmu;
156 *regmap = priv->regmap_base;
158 *reg = bank->drv[drv_num].offset;
159 if (bank->drv[drv_num].drv_type == DRV_TYPE_IO_1V8_3V0_AUTO ||
160 bank->drv[drv_num].drv_type == DRV_TYPE_IO_3V3_ONLY)
161 *bit = (pin_num % 8) * 3;
163 *bit = (pin_num % 8) * 2;
166 static int rk3399_set_drive(struct rockchip_pin_bank *bank,
167 int pin_num, int strength)
169 struct regmap *regmap;
171 u32 data, rmask_bits, temp;
173 int drv_type = bank->drv[pin_num / 8].drv_type;
175 rk3399_calc_drv_reg_and_bit(bank, pin_num, ®map, ®, &bit);
176 ret = rockchip_translate_drive_value(drv_type, strength);
178 debug("unsupported driver strength %d\n", strength);
183 case DRV_TYPE_IO_1V8_3V0_AUTO:
184 case DRV_TYPE_IO_3V3_ONLY:
185 rmask_bits = ROCKCHIP_DRV_3BITS_PER_PIN;
188 /* regular case, nothing to do */
192 * drive-strength offset is special, as it is spread
193 * over 2 registers, the bit data[15] contains bit 0
194 * of the value while temp[1:0] contains bits 2 and 1
196 data = (ret & 0x1) << 15;
197 temp = (ret >> 0x1) & 0x3;
200 ret = regmap_write(regmap, reg, data);
206 ret = regmap_write(regmap, reg, temp);
210 /* setting fully enclosed in the second register */
215 debug("unsupported bit: %d for pinctrl drive type: %d\n",
220 case DRV_TYPE_IO_DEFAULT:
221 case DRV_TYPE_IO_1V8_OR_3V0:
222 case DRV_TYPE_IO_1V8_ONLY:
223 rmask_bits = ROCKCHIP_DRV_BITS_PER_PIN;
226 debug("unsupported pinctrl drive type: %d\n",
231 /* enable the write to the equivalent lower bits */
232 data = ((1 << rmask_bits) - 1) << (bit + 16);
233 data |= (ret << bit);
234 ret = regmap_write(regmap, reg, data);
239 static struct rockchip_pin_bank rk3399_pin_banks[] = {
240 PIN_BANK_IOMUX_FLAGS_DRV_FLAGS_OFFSET_PULL_FLAGS(0, 32, "gpio0",
245 DRV_TYPE_IO_1V8_ONLY,
246 DRV_TYPE_IO_1V8_ONLY,
253 PULL_TYPE_IO_1V8_ONLY,
254 PULL_TYPE_IO_1V8_ONLY,
255 PULL_TYPE_IO_DEFAULT,
258 PIN_BANK_IOMUX_DRV_FLAGS_OFFSET(1, 32, "gpio1", IOMUX_SOURCE_PMU,
262 DRV_TYPE_IO_1V8_OR_3V0,
263 DRV_TYPE_IO_1V8_OR_3V0,
264 DRV_TYPE_IO_1V8_OR_3V0,
265 DRV_TYPE_IO_1V8_OR_3V0,
271 PIN_BANK_DRV_FLAGS_PULL_FLAGS(2, 32, "gpio2", DRV_TYPE_IO_1V8_OR_3V0,
272 DRV_TYPE_IO_1V8_OR_3V0,
273 DRV_TYPE_IO_1V8_ONLY,
274 DRV_TYPE_IO_1V8_ONLY,
275 PULL_TYPE_IO_DEFAULT,
276 PULL_TYPE_IO_DEFAULT,
277 PULL_TYPE_IO_1V8_ONLY,
278 PULL_TYPE_IO_1V8_ONLY
280 PIN_BANK_DRV_FLAGS(3, 32, "gpio3", DRV_TYPE_IO_3V3_ONLY,
281 DRV_TYPE_IO_3V3_ONLY,
282 DRV_TYPE_IO_3V3_ONLY,
283 DRV_TYPE_IO_1V8_OR_3V0
285 PIN_BANK_DRV_FLAGS(4, 32, "gpio4", DRV_TYPE_IO_1V8_OR_3V0,
286 DRV_TYPE_IO_1V8_3V0_AUTO,
287 DRV_TYPE_IO_1V8_OR_3V0,
288 DRV_TYPE_IO_1V8_OR_3V0
292 static struct rockchip_pin_ctrl rk3399_pin_ctrl = {
293 .pin_banks = rk3399_pin_banks,
294 .nr_banks = ARRAY_SIZE(rk3399_pin_banks),
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 = 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,