blob: 98ababc7c90700c4b27359fc3625519f20437306 [file] [log] [blame]
Jianqun Xu8170c4d2023-03-15 17:32:15 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2021 Rockchip Electronics Co., Ltd
4 */
5
Jianqun Xu8170c4d2023-03-15 17:32:15 +02006#include <dm.h>
7#include <dm/pinctrl.h>
8#include <regmap.h>
9#include <syscon.h>
10
11#include "pinctrl-rockchip.h"
12#include <dt-bindings/pinctrl/rockchip.h>
13
14static int rk3588_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
15{
16 struct rockchip_pinctrl_priv *priv = bank->priv;
17 struct regmap *regmap;
18 int iomux_num = (pin / 8);
19 int reg, ret, mask;
20 u8 bit;
21 u32 data;
22
23 debug("setting mux of GPIO%d-%d to %d\n", bank->bank_num, pin, mux);
24
25 regmap = priv->regmap_base;
26 reg = bank->iomux[iomux_num].offset;
27 if ((pin % 8) >= 4)
28 reg += 0x4;
29 bit = (pin % 4) * 4;
30 mask = 0xf;
31
32 if (bank->bank_num == 0) {
33 if (pin >= RK_PB4 && pin <= RK_PD7) {
34 if (mux < 8) {
35 reg += 0x4000 - 0xC; /* PMU2_IOC_BASE */
36 data = (mask << (bit + 16));
37 data |= (mux & mask) << bit;
38 ret = regmap_write(regmap, reg, data);
39 } else {
40 u32 reg0 = 0;
41
42 reg0 = reg + 0x4000 - 0xC; /* PMU2_IOC_BASE */
43 data = (mask << (bit + 16));
44 data |= 8 << bit;
45 ret = regmap_write(regmap, reg0, data);
46
47 reg0 = reg + 0x8000; /* BUS_IOC_BASE */
48 data = (mask << (bit + 16));
49 data |= mux << bit;
50 regmap = priv->regmap_base;
51 regmap_write(regmap, reg0, data);
52 }
53 } else {
54 data = (mask << (bit + 16));
55 data |= (mux & mask) << bit;
56 ret = regmap_write(regmap, reg, data);
57 }
58 return ret;
59 } else if (bank->bank_num > 0) {
60 reg += 0x8000; /* BUS_IOC_BASE */
61 }
62
63 data = (mask << (bit + 16));
64 data |= (mux & mask) << bit;
65
66 return regmap_write(regmap, reg, data);
67}
68
69#define RK3588_PMU1_IOC_REG (0x0000)
70#define RK3588_PMU2_IOC_REG (0x4000)
71#define RK3588_BUS_IOC_REG (0x8000)
72#define RK3588_VCCIO1_4_IOC_REG (0x9000)
73#define RK3588_VCCIO3_5_IOC_REG (0xA000)
74#define RK3588_VCCIO2_IOC_REG (0xB000)
75#define RK3588_VCCIO6_IOC_REG (0xC000)
76#define RK3588_EMMC_IOC_REG (0xD000)
77
78static const u32 rk3588_ds_regs[][2] = {
79 {RK_GPIO0_A0, RK3588_PMU1_IOC_REG + 0x0010},
80 {RK_GPIO0_A4, RK3588_PMU1_IOC_REG + 0x0014},
81 {RK_GPIO0_B0, RK3588_PMU1_IOC_REG + 0x0018},
82 {RK_GPIO0_B4, RK3588_PMU2_IOC_REG + 0x0014},
83 {RK_GPIO0_C0, RK3588_PMU2_IOC_REG + 0x0018},
84 {RK_GPIO0_C4, RK3588_PMU2_IOC_REG + 0x001C},
85 {RK_GPIO0_D0, RK3588_PMU2_IOC_REG + 0x0020},
86 {RK_GPIO0_D4, RK3588_PMU2_IOC_REG + 0x0024},
87 {RK_GPIO1_A0, RK3588_VCCIO1_4_IOC_REG + 0x0020},
88 {RK_GPIO1_A4, RK3588_VCCIO1_4_IOC_REG + 0x0024},
89 {RK_GPIO1_B0, RK3588_VCCIO1_4_IOC_REG + 0x0028},
90 {RK_GPIO1_B4, RK3588_VCCIO1_4_IOC_REG + 0x002C},
91 {RK_GPIO1_C0, RK3588_VCCIO1_4_IOC_REG + 0x0030},
92 {RK_GPIO1_C4, RK3588_VCCIO1_4_IOC_REG + 0x0034},
93 {RK_GPIO1_D0, RK3588_VCCIO1_4_IOC_REG + 0x0038},
94 {RK_GPIO1_D4, RK3588_VCCIO1_4_IOC_REG + 0x003C},
95 {RK_GPIO2_A0, RK3588_EMMC_IOC_REG + 0x0040},
96 {RK_GPIO2_A4, RK3588_VCCIO3_5_IOC_REG + 0x0044},
97 {RK_GPIO2_B0, RK3588_VCCIO3_5_IOC_REG + 0x0048},
98 {RK_GPIO2_B4, RK3588_VCCIO3_5_IOC_REG + 0x004C},
99 {RK_GPIO2_C0, RK3588_VCCIO3_5_IOC_REG + 0x0050},
100 {RK_GPIO2_C4, RK3588_VCCIO3_5_IOC_REG + 0x0054},
101 {RK_GPIO2_D0, RK3588_EMMC_IOC_REG + 0x0058},
102 {RK_GPIO2_D4, RK3588_EMMC_IOC_REG + 0x005C},
103 {RK_GPIO3_A0, RK3588_VCCIO3_5_IOC_REG + 0x0060},
104 {RK_GPIO3_A4, RK3588_VCCIO3_5_IOC_REG + 0x0064},
105 {RK_GPIO3_B0, RK3588_VCCIO3_5_IOC_REG + 0x0068},
106 {RK_GPIO3_B4, RK3588_VCCIO3_5_IOC_REG + 0x006C},
107 {RK_GPIO3_C0, RK3588_VCCIO3_5_IOC_REG + 0x0070},
108 {RK_GPIO3_C4, RK3588_VCCIO3_5_IOC_REG + 0x0074},
109 {RK_GPIO3_D0, RK3588_VCCIO3_5_IOC_REG + 0x0078},
110 {RK_GPIO3_D4, RK3588_VCCIO3_5_IOC_REG + 0x007C},
111 {RK_GPIO4_A0, RK3588_VCCIO6_IOC_REG + 0x0080},
112 {RK_GPIO4_A4, RK3588_VCCIO6_IOC_REG + 0x0084},
113 {RK_GPIO4_B0, RK3588_VCCIO6_IOC_REG + 0x0088},
114 {RK_GPIO4_B4, RK3588_VCCIO6_IOC_REG + 0x008C},
115 {RK_GPIO4_C0, RK3588_VCCIO6_IOC_REG + 0x0090},
116 {RK_GPIO4_C2, RK3588_VCCIO3_5_IOC_REG + 0x0090},
117 {RK_GPIO4_C4, RK3588_VCCIO3_5_IOC_REG + 0x0094},
118 {RK_GPIO4_D0, RK3588_VCCIO2_IOC_REG + 0x0098},
119 {RK_GPIO4_D4, RK3588_VCCIO2_IOC_REG + 0x009C},
120};
121
122static const u32 rk3588_p_regs[][2] = {
123 {RK_GPIO0_A0, RK3588_PMU1_IOC_REG + 0x0020},
124 {RK_GPIO0_B0, RK3588_PMU1_IOC_REG + 0x0024},
125 {RK_GPIO0_B5, RK3588_PMU2_IOC_REG + 0x0028},
126 {RK_GPIO0_C0, RK3588_PMU2_IOC_REG + 0x002C},
127 {RK_GPIO0_D0, RK3588_PMU2_IOC_REG + 0x0030},
128 {RK_GPIO1_A0, RK3588_VCCIO1_4_IOC_REG + 0x0110},
129 {RK_GPIO1_B0, RK3588_VCCIO1_4_IOC_REG + 0x0114},
130 {RK_GPIO1_C0, RK3588_VCCIO1_4_IOC_REG + 0x0118},
131 {RK_GPIO1_D0, RK3588_VCCIO1_4_IOC_REG + 0x011C},
132 {RK_GPIO2_A0, RK3588_EMMC_IOC_REG + 0x0120},
133 {RK_GPIO2_A6, RK3588_VCCIO3_5_IOC_REG + 0x0120},
134 {RK_GPIO2_B0, RK3588_VCCIO3_5_IOC_REG + 0x0124},
135 {RK_GPIO2_C0, RK3588_VCCIO3_5_IOC_REG + 0x0128},
136 {RK_GPIO2_D0, RK3588_EMMC_IOC_REG + 0x012C},
137 {RK_GPIO3_A0, RK3588_VCCIO3_5_IOC_REG + 0x0130},
138 {RK_GPIO3_B0, RK3588_VCCIO3_5_IOC_REG + 0x0134},
139 {RK_GPIO3_C0, RK3588_VCCIO3_5_IOC_REG + 0x0138},
140 {RK_GPIO3_D0, RK3588_VCCIO3_5_IOC_REG + 0x013C},
141 {RK_GPIO4_A0, RK3588_VCCIO6_IOC_REG + 0x0140},
142 {RK_GPIO4_B0, RK3588_VCCIO6_IOC_REG + 0x0144},
143 {RK_GPIO4_C0, RK3588_VCCIO6_IOC_REG + 0x0148},
144 {RK_GPIO4_C2, RK3588_VCCIO3_5_IOC_REG + 0x0148},
145 {RK_GPIO4_D0, RK3588_VCCIO2_IOC_REG + 0x014C},
146};
147
148static const u32 rk3588_smt_regs[][2] = {
149 {RK_GPIO0_A0, RK3588_PMU1_IOC_REG + 0x0030},
150 {RK_GPIO0_B0, RK3588_PMU1_IOC_REG + 0x0034},
151 {RK_GPIO0_B5, RK3588_PMU2_IOC_REG + 0x0040},
152 {RK_GPIO0_C0, RK3588_PMU2_IOC_REG + 0x0044},
153 {RK_GPIO0_D0, RK3588_PMU2_IOC_REG + 0x0048},
154 {RK_GPIO1_A0, RK3588_VCCIO1_4_IOC_REG + 0x0210},
155 {RK_GPIO1_B0, RK3588_VCCIO1_4_IOC_REG + 0x0214},
156 {RK_GPIO1_C0, RK3588_VCCIO1_4_IOC_REG + 0x0218},
157 {RK_GPIO1_D0, RK3588_VCCIO1_4_IOC_REG + 0x021C},
158 {RK_GPIO2_A0, RK3588_EMMC_IOC_REG + 0x0220},
159 {RK_GPIO2_A6, RK3588_VCCIO3_5_IOC_REG + 0x0220},
160 {RK_GPIO2_B0, RK3588_VCCIO3_5_IOC_REG + 0x0224},
161 {RK_GPIO2_C0, RK3588_VCCIO3_5_IOC_REG + 0x0228},
162 {RK_GPIO2_D0, RK3588_EMMC_IOC_REG + 0x022C},
163 {RK_GPIO3_A0, RK3588_VCCIO3_5_IOC_REG + 0x0230},
164 {RK_GPIO3_B0, RK3588_VCCIO3_5_IOC_REG + 0x0234},
165 {RK_GPIO3_C0, RK3588_VCCIO3_5_IOC_REG + 0x0238},
166 {RK_GPIO3_D0, RK3588_VCCIO3_5_IOC_REG + 0x023C},
167 {RK_GPIO4_A0, RK3588_VCCIO6_IOC_REG + 0x0240},
168 {RK_GPIO4_B0, RK3588_VCCIO6_IOC_REG + 0x0244},
169 {RK_GPIO4_C0, RK3588_VCCIO6_IOC_REG + 0x0248},
170 {RK_GPIO4_C2, RK3588_VCCIO3_5_IOC_REG + 0x0248},
171 {RK_GPIO4_D0, RK3588_VCCIO2_IOC_REG + 0x024C},
172};
173
174#define RK3588_PULL_BITS_PER_PIN 2
175#define RK3588_PULL_PINS_PER_REG 8
176
177static void rk3588_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
178 int pin_num, struct regmap **regmap,
179 int *reg, u8 *bit)
180{
181 struct rockchip_pinctrl_priv *info = bank->priv;
182 u8 bank_num = bank->bank_num;
183 u32 pin = bank_num * 32 + pin_num;
184 int i;
185
186 for (i = ARRAY_SIZE(rk3588_p_regs) - 1; i >= 0; i--) {
187 if (pin >= rk3588_p_regs[i][0]) {
188 *reg = rk3588_p_regs[i][1];
189 break;
190 }
191 }
192
193 assert(i >= 0);
194
195 *regmap = info->regmap_base;
196 *bit = pin_num % RK3588_PULL_PINS_PER_REG;
197 *bit *= RK3588_PULL_BITS_PER_PIN;
198}
199
200#define RK3588_DRV_BITS_PER_PIN 4
201#define RK3588_DRV_PINS_PER_REG 4
202
203static void rk3588_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
204 int pin_num, struct regmap **regmap,
205 int *reg, u8 *bit)
206{
207 struct rockchip_pinctrl_priv *info = bank->priv;
208 u8 bank_num = bank->bank_num;
209 u32 pin = bank_num * 32 + pin_num;
210 int i;
211
212 for (i = ARRAY_SIZE(rk3588_ds_regs) - 1; i >= 0; i--) {
213 if (pin >= rk3588_ds_regs[i][0]) {
214 *reg = rk3588_ds_regs[i][1];
215 break;
216 }
217 }
218
219 assert(i >= 0);
220
221 *regmap = info->regmap_base;
222 *bit = pin_num % RK3588_DRV_PINS_PER_REG;
223 *bit *= RK3588_DRV_BITS_PER_PIN;
224}
225
226#define RK3588_SMT_BITS_PER_PIN 1
227#define RK3588_SMT_PINS_PER_REG 8
228
229static int rk3588_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank,
230 int pin_num, struct regmap **regmap,
231 int *reg, u8 *bit)
232{
233 struct rockchip_pinctrl_priv *info = bank->priv;
234 u8 bank_num = bank->bank_num;
235 u32 pin = bank_num * 32 + pin_num;
236 int i;
237
238 for (i = ARRAY_SIZE(rk3588_smt_regs) - 1; i >= 0; i--) {
239 if (pin >= rk3588_smt_regs[i][0]) {
240 *reg = rk3588_smt_regs[i][1];
241 break;
242 }
243 }
244
245 assert(i >= 0);
246
247 *regmap = info->regmap_base;
248 *bit = pin_num % RK3588_SMT_PINS_PER_REG;
249 *bit *= RK3588_SMT_BITS_PER_PIN;
250
251 return 0;
252}
253
254static int rk3588_set_pull(struct rockchip_pin_bank *bank,
255 int pin_num, int pull)
256{
257 struct regmap *regmap;
258 int reg, translated_pull;
259 u8 bit, type;
260 u32 data;
261
262 rk3588_calc_pull_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
263 type = bank->pull_type[pin_num / 8];
264 translated_pull = rockchip_translate_pull_value(type, pull);
265 if (translated_pull < 0) {
266 debug("unsupported pull setting %d\n", pull);
267 return -EINVAL;
268 }
269
270 /* enable the write to the equivalent lower bits */
271 data = ((1 << ROCKCHIP_PULL_BITS_PER_PIN) - 1) << (bit + 16);
272 data |= (translated_pull << bit);
273
274 return regmap_write(regmap, reg, data);
275}
276
277static int rk3588_set_drive(struct rockchip_pin_bank *bank,
278 int pin_num, int strength)
279{
280 struct regmap *regmap;
281 int reg;
282 u32 data;
283 u8 bit;
284
285 rk3588_calc_drv_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
286
287 /* enable the write to the equivalent lower bits */
288 data = ((1 << RK3588_DRV_BITS_PER_PIN) - 1) << (bit + 16);
289 data |= (strength << bit);
290
291 return regmap_write(regmap, reg, data);
292}
293
294static int rk3588_set_schmitt(struct rockchip_pin_bank *bank,
295 int pin_num, int enable)
296{
297 struct regmap *regmap;
298 int reg;
299 u32 data;
300 u8 bit;
301
302 rk3588_calc_schmitt_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
303
304 /* enable the write to the equivalent lower bits */
305 data = ((1 << RK3588_SMT_BITS_PER_PIN) - 1) << (bit + 16);
306 data |= (enable << bit);
307
308 return regmap_write(regmap, reg, data);
309}
310
311static struct rockchip_pin_bank rk3588_pin_banks[] = {
312 RK3588_PIN_BANK_FLAGS(0, 32, "gpio0",
313 IOMUX_WIDTH_4BIT, PULL_TYPE_IO_1V8_ONLY),
314 RK3588_PIN_BANK_FLAGS(1, 32, "gpio1",
315 IOMUX_WIDTH_4BIT, PULL_TYPE_IO_1V8_ONLY),
316 RK3588_PIN_BANK_FLAGS(2, 32, "gpio2",
317 IOMUX_WIDTH_4BIT, PULL_TYPE_IO_1V8_ONLY),
318 RK3588_PIN_BANK_FLAGS(3, 32, "gpio3",
319 IOMUX_WIDTH_4BIT, PULL_TYPE_IO_1V8_ONLY),
320 RK3588_PIN_BANK_FLAGS(4, 32, "gpio4",
321 IOMUX_WIDTH_4BIT, PULL_TYPE_IO_1V8_ONLY),
322};
323
324static const struct rockchip_pin_ctrl rk3588_pin_ctrl = {
325 .pin_banks = rk3588_pin_banks,
326 .nr_banks = ARRAY_SIZE(rk3588_pin_banks),
327 .nr_pins = 160,
328 .set_mux = rk3588_set_mux,
329 .set_pull = rk3588_set_pull,
330 .set_drive = rk3588_set_drive,
331 .set_schmitt = rk3588_set_schmitt,
332};
333
334static const struct udevice_id rk3588_pinctrl_ids[] = {
335 {
336 .compatible = "rockchip,rk3588-pinctrl",
337 .data = (ulong)&rk3588_pin_ctrl
338 },
339 { }
340};
341
342U_BOOT_DRIVER(pinctrl_rk3588) = {
343 .name = "rockchip_rk3588_pinctrl",
344 .id = UCLASS_PINCTRL,
345 .of_match = rk3588_pinctrl_ids,
346 .priv_auto = sizeof(struct rockchip_pinctrl_priv),
347 .ops = &rockchip_pinctrl_ops,
348#if CONFIG_IS_ENABLED(OF_REAL)
349 .bind = dm_scan_fdt_dev,
350#endif
351 .probe = rockchip_pinctrl_probe,
352};