blob: cc7885bae40802eccfc2cad377727da9ee899139 [file] [log] [blame]
David Wue2b28012019-07-11 10:37:14 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2019 Rockchip Electronics Co., Ltd
4 */
5
David Wue2b28012019-07-11 10:37:14 +02006#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
David Wue2b28012019-07-11 10:37:14 +02008#include <dm/pinctrl.h>
9#include <regmap.h>
10#include <syscon.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060011#include <linux/bitops.h>
David Wue2b28012019-07-11 10:37:14 +020012
13#include "pinctrl-rockchip.h"
14
15static struct rockchip_mux_route_data px30_mux_route_data[] = {
16 {
17 /* cif-d2m0 */
18 .bank_num = 2,
19 .pin = 0,
20 .func = 1,
21 .route_offset = 0x184,
22 .route_val = BIT(16 + 7),
23 }, {
24 /* cif-d2m1 */
25 .bank_num = 3,
26 .pin = 3,
27 .func = 3,
28 .route_offset = 0x184,
29 .route_val = BIT(16 + 7) | BIT(7),
30 }, {
31 /* pdm-m0 */
32 .bank_num = 3,
33 .pin = 22,
34 .func = 2,
35 .route_offset = 0x184,
36 .route_val = BIT(16 + 8),
37 }, {
38 /* pdm-m1 */
39 .bank_num = 2,
40 .pin = 22,
41 .func = 1,
42 .route_offset = 0x184,
43 .route_val = BIT(16 + 8) | BIT(8),
44 }, {
45 /* uart2-rxm0 */
46 .bank_num = 1,
47 .pin = 27,
48 .func = 2,
49 .route_offset = 0x184,
50 .route_val = BIT(16 + 10),
51 }, {
52 /* uart2-rxm1 */
53 .bank_num = 2,
54 .pin = 14,
55 .func = 2,
56 .route_offset = 0x184,
57 .route_val = BIT(16 + 10) | BIT(10),
58 }, {
59 /* uart3-rxm0 */
60 .bank_num = 0,
61 .pin = 17,
62 .func = 2,
63 .route_offset = 0x184,
64 .route_val = BIT(16 + 9),
65 }, {
66 /* uart3-rxm1 */
67 .bank_num = 1,
68 .pin = 15,
69 .func = 2,
70 .route_offset = 0x184,
71 .route_val = BIT(16 + 9) | BIT(9),
72 },
73};
74
75static int px30_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
76{
77 struct rockchip_pinctrl_priv *priv = bank->priv;
78 int iomux_num = (pin / 8);
79 struct regmap *regmap;
80 int reg, ret, mask, mux_type;
81 u8 bit;
Jagan Teki9e0e6812022-12-14 23:20:56 +053082 u32 data;
David Wue2b28012019-07-11 10:37:14 +020083
84 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
85 ? priv->regmap_pmu : priv->regmap_base;
86
87 /* get basic quadrupel of mux registers and the correct reg inside */
88 mux_type = bank->iomux[iomux_num].type;
89 reg = bank->iomux[iomux_num].offset;
90 reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask);
91
David Wue2b28012019-07-11 10:37:14 +020092 data = (mask << (bit + 16));
93 data |= (mux & mask) << bit;
94 ret = regmap_write(regmap, reg, data);
95
96 return ret;
97}
98
99#define PX30_PULL_PMU_OFFSET 0x10
100#define PX30_PULL_GRF_OFFSET 0x60
101#define PX30_PULL_BITS_PER_PIN 2
102#define PX30_PULL_PINS_PER_REG 8
103#define PX30_PULL_BANK_STRIDE 16
104
105static void px30_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
106 int pin_num, struct regmap **regmap,
107 int *reg, u8 *bit)
108{
109 struct rockchip_pinctrl_priv *priv = bank->priv;
110
111 /* The first 32 pins of the first bank are located in PMU */
112 if (bank->bank_num == 0) {
113 *regmap = priv->regmap_pmu;
114 *reg = PX30_PULL_PMU_OFFSET;
115 } else {
116 *regmap = priv->regmap_base;
117 *reg = PX30_PULL_GRF_OFFSET;
118
119 /* correct the offset, as we're starting with the 2nd bank */
120 *reg -= 0x10;
121 *reg += bank->bank_num * PX30_PULL_BANK_STRIDE;
122 }
123
124 *reg += ((pin_num / PX30_PULL_PINS_PER_REG) * 4);
125 *bit = (pin_num % PX30_PULL_PINS_PER_REG);
126 *bit *= PX30_PULL_BITS_PER_PIN;
127}
128
129static int px30_set_pull(struct rockchip_pin_bank *bank,
130 int pin_num, int pull)
131{
132 struct regmap *regmap;
133 int reg, ret;
134 u8 bit, type;
135 u32 data;
136
137 if (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT)
138 return -ENOTSUPP;
139
140 px30_calc_pull_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
141 type = bank->pull_type[pin_num / 8];
142 ret = rockchip_translate_pull_value(type, pull);
143 if (ret < 0) {
144 debug("unsupported pull setting %d\n", pull);
145 return ret;
146 }
147
148 /* enable the write to the equivalent lower bits */
149 data = ((1 << ROCKCHIP_PULL_BITS_PER_PIN) - 1) << (bit + 16);
150 data |= (ret << bit);
151 ret = regmap_write(regmap, reg, data);
152
153 return ret;
154}
155
156#define PX30_DRV_PMU_OFFSET 0x20
157#define PX30_DRV_GRF_OFFSET 0xf0
158#define PX30_DRV_BITS_PER_PIN 2
159#define PX30_DRV_PINS_PER_REG 8
160#define PX30_DRV_BANK_STRIDE 16
161
162static void px30_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
163 int pin_num, struct regmap **regmap,
164 int *reg, u8 *bit)
165{
166 struct rockchip_pinctrl_priv *priv = bank->priv;
167
168 /* The first 32 pins of the first bank are located in PMU */
169 if (bank->bank_num == 0) {
170 *regmap = priv->regmap_pmu;
171 *reg = PX30_DRV_PMU_OFFSET;
172 } else {
173 *regmap = priv->regmap_base;
174 *reg = PX30_DRV_GRF_OFFSET;
175
176 /* correct the offset, as we're starting with the 2nd bank */
177 *reg -= 0x10;
178 *reg += bank->bank_num * PX30_DRV_BANK_STRIDE;
179 }
180
181 *reg += ((pin_num / PX30_DRV_PINS_PER_REG) * 4);
182 *bit = (pin_num % PX30_DRV_PINS_PER_REG);
183 *bit *= PX30_DRV_BITS_PER_PIN;
184}
185
186static int px30_set_drive(struct rockchip_pin_bank *bank,
187 int pin_num, int strength)
188{
189 struct regmap *regmap;
190 int reg, ret;
191 u32 data, rmask_bits, temp;
192 u8 bit;
193 int drv_type = bank->drv[pin_num / 8].drv_type;
194
195 px30_calc_drv_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
196 ret = rockchip_translate_drive_value(drv_type, strength);
197 if (ret < 0) {
198 debug("unsupported driver strength %d\n", strength);
199 return ret;
200 }
201
202 switch (drv_type) {
203 case DRV_TYPE_IO_1V8_3V0_AUTO:
204 case DRV_TYPE_IO_3V3_ONLY:
205 rmask_bits = ROCKCHIP_DRV_3BITS_PER_PIN;
206 switch (bit) {
207 case 0 ... 12:
208 /* regular case, nothing to do */
209 break;
210 case 15:
211 /*
212 * drive-strength offset is special, as it is spread
213 * over 2 registers, the bit data[15] contains bit 0
214 * of the value while temp[1:0] contains bits 2 and 1
215 */
216 data = (ret & 0x1) << 15;
217 temp = (ret >> 0x1) & 0x3;
218
219 data |= BIT(31);
220 ret = regmap_write(regmap, reg, data);
221 if (ret)
222 return ret;
223
224 temp |= (0x3 << 16);
225 reg += 0x4;
226 ret = regmap_write(regmap, reg, temp);
227
228 return ret;
229 case 18 ... 21:
230 /* setting fully enclosed in the second register */
231 reg += 4;
232 bit -= 16;
233 break;
234 default:
235 debug("unsupported bit: %d for pinctrl drive type: %d\n",
236 bit, drv_type);
237 return -EINVAL;
238 }
239 break;
240 case DRV_TYPE_IO_DEFAULT:
241 case DRV_TYPE_IO_1V8_OR_3V0:
242 case DRV_TYPE_IO_1V8_ONLY:
243 rmask_bits = ROCKCHIP_DRV_BITS_PER_PIN;
244 break;
245 default:
246 debug("unsupported pinctrl drive type: %d\n",
247 drv_type);
248 return -EINVAL;
249 }
250
251 /* enable the write to the equivalent lower bits */
252 data = ((1 << rmask_bits) - 1) << (bit + 16);
253 data |= (ret << bit);
254 ret = regmap_write(regmap, reg, data);
255
256 return ret;
257}
258
259#define PX30_SCHMITT_PMU_OFFSET 0x38
260#define PX30_SCHMITT_GRF_OFFSET 0xc0
261#define PX30_SCHMITT_PINS_PER_PMU_REG 16
262#define PX30_SCHMITT_BANK_STRIDE 16
263#define PX30_SCHMITT_PINS_PER_GRF_REG 8
264
265static int px30_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank,
266 int pin_num,
267 struct regmap **regmap,
268 int *reg, u8 *bit)
269{
270 struct rockchip_pinctrl_priv *priv = bank->priv;
271 int pins_per_reg;
272
273 if (bank->bank_num == 0) {
274 *regmap = priv->regmap_pmu;
275 *reg = PX30_SCHMITT_PMU_OFFSET;
276 pins_per_reg = PX30_SCHMITT_PINS_PER_PMU_REG;
277 } else {
278 *regmap = priv->regmap_base;
279 *reg = PX30_SCHMITT_GRF_OFFSET;
280 pins_per_reg = PX30_SCHMITT_PINS_PER_GRF_REG;
281 *reg += (bank->bank_num - 1) * PX30_SCHMITT_BANK_STRIDE;
282 }
283 *reg += ((pin_num / pins_per_reg) * 4);
284 *bit = pin_num % pins_per_reg;
285
286 return 0;
287}
288
289static int px30_set_schmitt(struct rockchip_pin_bank *bank,
290 int pin_num, int enable)
291{
292 struct regmap *regmap;
293 int reg;
294 u8 bit;
295 u32 data;
296
297 px30_calc_schmitt_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
298 /* enable the write to the equivalent lower bits */
299 data = BIT(bit + 16) | (enable << bit);
300
301 return regmap_write(regmap, reg, data);
302}
303
304static struct rockchip_pin_bank px30_pin_banks[] = {
305 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU,
306 IOMUX_SOURCE_PMU,
307 IOMUX_SOURCE_PMU,
308 IOMUX_SOURCE_PMU
309 ),
310 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_WIDTH_4BIT,
311 IOMUX_WIDTH_4BIT,
312 IOMUX_WIDTH_4BIT,
313 IOMUX_WIDTH_4BIT
314 ),
315 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", IOMUX_WIDTH_4BIT,
316 IOMUX_WIDTH_4BIT,
317 IOMUX_WIDTH_4BIT,
318 IOMUX_WIDTH_4BIT
319 ),
320 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", IOMUX_WIDTH_4BIT,
321 IOMUX_WIDTH_4BIT,
322 IOMUX_WIDTH_4BIT,
323 IOMUX_WIDTH_4BIT
324 ),
325};
326
327static struct rockchip_pin_ctrl px30_pin_ctrl = {
328 .pin_banks = px30_pin_banks,
329 .nr_banks = ARRAY_SIZE(px30_pin_banks),
330 .grf_mux_offset = 0x0,
331 .pmu_mux_offset = 0x0,
332 .grf_drv_offset = 0xf0,
333 .pmu_drv_offset = 0x20,
334 .iomux_routes = px30_mux_route_data,
335 .niomux_routes = ARRAY_SIZE(px30_mux_route_data),
336 .set_mux = px30_set_mux,
337 .set_pull = px30_set_pull,
338 .set_drive = px30_set_drive,
339 .set_schmitt = px30_set_schmitt,
340};
341
342static const struct udevice_id px30_pinctrl_ids[] = {
343 {
344 .compatible = "rockchip,px30-pinctrl",
345 .data = (ulong)&px30_pin_ctrl
346 },
347 { }
348};
349
350U_BOOT_DRIVER(pinctrl_px30) = {
351 .name = "rockchip_px30_pinctrl",
352 .id = UCLASS_PINCTRL,
353 .of_match = px30_pinctrl_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700354 .priv_auto = sizeof(struct rockchip_pinctrl_priv),
David Wue2b28012019-07-11 10:37:14 +0200355 .ops = &rockchip_pinctrl_ops,
Simon Glass92882652021-08-07 07:24:04 -0600356#if CONFIG_IS_ENABLED(OF_REAL)
David Wue2b28012019-07-11 10:37:14 +0200357 .bind = dm_scan_fdt_dev,
358#endif
359 .probe = rockchip_pinctrl_probe,
360};