blob: d449d07d32e74d1dec1978b1405bec94887908a2 [file] [log] [blame]
David Wu5f596ae2019-01-02 21:00:55 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2019 Rockchip Electronics Co., Ltd
4 */
5
David Wu5f596ae2019-01-02 21:00:55 +08006#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
David Wu5f596ae2019-01-02 21:00:55 +08008#include <dm/pinctrl.h>
9#include <regmap.h>
10#include <syscon.h>
11#include <fdtdec.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060012#include <linux/bitops.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060013#include <linux/libfdt.h>
David Wu5f596ae2019-01-02 21:00:55 +080014
15#include "pinctrl-rockchip.h"
Jonas Karlmanc2427012024-05-12 12:16:16 +000016#include <dt-bindings/pinctrl/rockchip.h>
David Wu5f596ae2019-01-02 21:00:55 +080017
18#define MAX_ROCKCHIP_PINS_ENTRIES 30
19#define MAX_ROCKCHIP_GPIO_PER_BANK 32
David Wu5f596ae2019-01-02 21:00:55 +080020
21static int rockchip_verify_config(struct udevice *dev, u32 bank, u32 pin)
22{
23 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
24 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
25
26 if (bank >= ctrl->nr_banks) {
27 debug("pin conf bank %d >= nbanks %d\n", bank, ctrl->nr_banks);
28 return -EINVAL;
29 }
30
31 if (pin >= MAX_ROCKCHIP_GPIO_PER_BANK) {
32 debug("pin conf pin %d >= %d\n", pin,
33 MAX_ROCKCHIP_GPIO_PER_BANK);
34 return -EINVAL;
35 }
36
37 return 0;
38}
39
David Wu3dd7d6c2019-04-16 21:50:55 +080040void rockchip_get_recalced_mux(struct rockchip_pin_bank *bank, int pin,
41 int *reg, u8 *bit, int *mask)
David Wu5f596ae2019-01-02 21:00:55 +080042{
43 struct rockchip_pinctrl_priv *priv = bank->priv;
44 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
45 struct rockchip_mux_recalced_data *data;
46 int i;
47
48 for (i = 0; i < ctrl->niomux_recalced; i++) {
49 data = &ctrl->iomux_recalced[i];
50 if (data->num == bank->bank_num &&
51 data->pin == pin)
52 break;
53 }
54
55 if (i >= ctrl->niomux_recalced)
56 return;
57
58 *reg = data->reg;
59 *mask = data->mask;
60 *bit = data->bit;
61}
62
Jagan Teki9e0e6812022-12-14 23:20:56 +053063static enum rockchip_pin_route_type
64rockchip_get_mux_route(struct rockchip_pin_bank *bank, int pin,
65 int mux, u32 *reg, u32 *value)
David Wu5f596ae2019-01-02 21:00:55 +080066{
67 struct rockchip_pinctrl_priv *priv = bank->priv;
68 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
69 struct rockchip_mux_route_data *data;
70 int i;
71
72 for (i = 0; i < ctrl->niomux_routes; i++) {
73 data = &ctrl->iomux_routes[i];
74 if (data->bank_num == bank->bank_num &&
75 data->pin == pin && data->func == mux)
76 break;
77 }
78
79 if (i >= ctrl->niomux_routes)
Jagan Teki9e0e6812022-12-14 23:20:56 +053080 return ROUTE_TYPE_INVALID;
David Wu5f596ae2019-01-02 21:00:55 +080081
82 *reg = data->route_offset;
83 *value = data->route_val;
84
Jagan Teki9e0e6812022-12-14 23:20:56 +053085 return data->route_type;
David Wu5f596ae2019-01-02 21:00:55 +080086}
87
David Wu3dd7d6c2019-04-16 21:50:55 +080088int rockchip_get_mux_data(int mux_type, int pin, u8 *bit, int *mask)
David Wu5f596ae2019-01-02 21:00:55 +080089{
90 int offset = 0;
91
92 if (mux_type & IOMUX_WIDTH_4BIT) {
93 if ((pin % 8) >= 4)
94 offset = 0x4;
95 *bit = (pin % 4) * 4;
96 *mask = 0xf;
97 } else if (mux_type & IOMUX_WIDTH_3BIT) {
98 /*
99 * pin0 ~ pin4 are at first register, and
100 * pin5 ~ pin7 are at second register.
101 */
102 if ((pin % 8) >= 5)
103 offset = 0x4;
104 *bit = (pin % 8 % 5) * 3;
105 *mask = 0x7;
106 } else {
107 *bit = (pin % 8) * 2;
108 *mask = 0x3;
109 }
110
111 return offset;
112}
113
114static int rockchip_get_mux(struct rockchip_pin_bank *bank, int pin)
115{
116 struct rockchip_pinctrl_priv *priv = bank->priv;
117 int iomux_num = (pin / 8);
118 struct regmap *regmap;
119 unsigned int val;
120 int reg, ret, mask, mux_type;
121 u8 bit;
122
123 if (iomux_num > 3)
124 return -EINVAL;
125
126 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
127 debug("pin %d is unrouted\n", pin);
128 return -EINVAL;
129 }
130
131 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
132 return RK_FUNC_GPIO;
133
Jonas Karlmandd46eac2024-05-12 12:16:15 +0000134 if (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
135 regmap = priv->regmap_pmu;
136 else if (bank->iomux[iomux_num].type & IOMUX_L_SOURCE_PMU)
137 regmap = (pin % 8 < 4) ? priv->regmap_pmu : priv->regmap_base;
138 else
139 regmap = priv->regmap_base;
David Wu5f596ae2019-01-02 21:00:55 +0800140
141 /* get basic quadrupel of mux registers and the correct reg inside */
142 mux_type = bank->iomux[iomux_num].type;
143 reg = bank->iomux[iomux_num].offset;
144 reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask);
145
146 if (bank->recalced_mask & BIT(pin))
147 rockchip_get_recalced_mux(bank, pin, &reg, &bit, &mask);
148
Jonas Karlmanc2427012024-05-12 12:16:16 +0000149 if (IS_ENABLED(CONFIG_ROCKCHIP_RK3588)) {
150 if (bank->bank_num == 0) {
151 if (pin >= RK_PB4 && pin <= RK_PD7) {
152 u32 reg0 = 0;
153
154 reg0 = reg + 0x4000 - 0xC; /* PMU2_IOC_BASE */
155 ret = regmap_read(regmap, reg0, &val);
156 if (ret)
157 return ret;
158
159 ret = ((val >> bit) & mask);
160 if (ret != 8)
161 return ret;
162
163 reg = reg + 0x8000; /* BUS_IOC_BASE */
164 regmap = priv->regmap_base;
165 }
166 } else if (bank->bank_num > 0) {
167 reg += 0x8000; /* BUS_IOC_BASE */
168 }
169 }
170
David Wu5f596ae2019-01-02 21:00:55 +0800171 ret = regmap_read(regmap, reg, &val);
172 if (ret)
173 return ret;
174
175 return ((val >> bit) & mask);
176}
177
178static int rockchip_pinctrl_get_gpio_mux(struct udevice *dev, int banknum,
179 int index)
180{ struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
181 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
182
183 return rockchip_get_mux(&ctrl->pin_banks[banknum], index);
184}
185
186static int rockchip_verify_mux(struct rockchip_pin_bank *bank,
187 int pin, int mux)
188{
189 int iomux_num = (pin / 8);
190
191 if (iomux_num > 3)
192 return -EINVAL;
193
194 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
195 debug("pin %d is unrouted\n", pin);
196 return -EINVAL;
197 }
198
199 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) {
Jonas Karlmand5da6902024-05-12 12:16:14 +0000200 if (mux != RK_FUNC_GPIO) {
David Wu5f596ae2019-01-02 21:00:55 +0800201 debug("pin %d only supports a gpio mux\n", pin);
202 return -ENOTSUPP;
203 }
204 }
205
206 return 0;
207}
208
209/*
210 * Set a new mux function for a pin.
211 *
212 * The register is divided into the upper and lower 16 bit. When changing
213 * a value, the previous register value is not read and changed. Instead
214 * it seems the changed bits are marked in the upper 16 bit, while the
215 * changed value gets set in the same offset in the lower 16 bit.
216 * All pin settings seem to be 2 bit wide in both the upper and lower
217 * parts.
218 * @bank: pin bank to change
219 * @pin: pin to change
220 * @mux: new mux function to set
221 */
222static int rockchip_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
223{
224 struct rockchip_pinctrl_priv *priv = bank->priv;
David Wu3dd7d6c2019-04-16 21:50:55 +0800225 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
David Wu5f596ae2019-01-02 21:00:55 +0800226 int iomux_num = (pin / 8);
David Wu3dd7d6c2019-04-16 21:50:55 +0800227 int ret;
David Wu5f596ae2019-01-02 21:00:55 +0800228
229 ret = rockchip_verify_mux(bank, pin, mux);
230 if (ret < 0)
231 return ret;
232
233 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
234 return 0;
235
236 debug("setting mux of GPIO%d-%d to %d\n", bank->bank_num, pin, mux);
237
David Wu3dd7d6c2019-04-16 21:50:55 +0800238 if (!ctrl->set_mux)
239 return -ENOTSUPP;
David Wu5f596ae2019-01-02 21:00:55 +0800240
David Wu3dd7d6c2019-04-16 21:50:55 +0800241 ret = ctrl->set_mux(bank, pin, mux);
Jagan Teki9e0e6812022-12-14 23:20:56 +0530242 if (ret)
243 return ret;
David Wu5f596ae2019-01-02 21:00:55 +0800244
Jagan Teki9e0e6812022-12-14 23:20:56 +0530245 if (bank->route_mask & BIT(pin)) {
246 struct regmap *regmap;
247 u32 route_reg = 0, route_val = 0;
248
249 ret = rockchip_get_mux_route(bank, pin, mux,
250 &route_reg, &route_val);
251 switch (ret) {
252 case ROUTE_TYPE_DEFAULT:
253 if (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
254 regmap = priv->regmap_pmu;
255 else if (bank->iomux[iomux_num].type & IOMUX_L_SOURCE_PMU)
256 regmap = (pin % 8 < 4) ? priv->regmap_pmu : priv->regmap_base;
257 else
258 regmap = priv->regmap_base;
259
260 regmap_write(regmap, route_reg, route_val);
261 break;
262 case ROUTE_TYPE_TOPGRF:
263 regmap_write(priv->regmap_base, route_reg, route_val);
264 break;
265 case ROUTE_TYPE_PMUGRF:
266 regmap_write(priv->regmap_pmu, route_reg, route_val);
267 break;
268 case ROUTE_TYPE_INVALID:
269 fallthrough;
270 default:
271 break;
272 }
273 }
274
275 return 0;
David Wu5f596ae2019-01-02 21:00:55 +0800276}
277
278static int rockchip_perpin_drv_list[DRV_TYPE_MAX][8] = {
279 { 2, 4, 8, 12, -1, -1, -1, -1 },
280 { 3, 6, 9, 12, -1, -1, -1, -1 },
281 { 5, 10, 15, 20, -1, -1, -1, -1 },
282 { 4, 6, 8, 10, 12, 14, 16, 18 },
283 { 4, 7, 10, 13, 16, 19, 22, 26 }
284};
285
David Wu40a55482019-04-16 21:55:26 +0800286int rockchip_translate_drive_value(int type, int strength)
David Wu5f596ae2019-01-02 21:00:55 +0800287{
David Wu40a55482019-04-16 21:55:26 +0800288 int i, ret;
David Wu5f596ae2019-01-02 21:00:55 +0800289
290 ret = -EINVAL;
David Wu40a55482019-04-16 21:55:26 +0800291 for (i = 0; i < ARRAY_SIZE(rockchip_perpin_drv_list[type]); i++) {
292 if (rockchip_perpin_drv_list[type][i] == strength) {
David Wu5f596ae2019-01-02 21:00:55 +0800293 ret = i;
294 break;
David Wu40a55482019-04-16 21:55:26 +0800295 } else if (rockchip_perpin_drv_list[type][i] < 0) {
296 ret = rockchip_perpin_drv_list[type][i];
David Wu5f596ae2019-01-02 21:00:55 +0800297 break;
298 }
299 }
300
David Wu40a55482019-04-16 21:55:26 +0800301 return ret;
302}
David Wu5f596ae2019-01-02 21:00:55 +0800303
David Wu40a55482019-04-16 21:55:26 +0800304static int rockchip_set_drive_perpin(struct rockchip_pin_bank *bank,
305 int pin_num, int strength)
306{
307 struct rockchip_pinctrl_priv *priv = bank->priv;
308 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
David Wu5f596ae2019-01-02 21:00:55 +0800309
David Wu40a55482019-04-16 21:55:26 +0800310 debug("setting drive of GPIO%d-%d to %d\n", bank->bank_num,
311 pin_num, strength);
David Wu5f596ae2019-01-02 21:00:55 +0800312
David Wu40a55482019-04-16 21:55:26 +0800313 if (!ctrl->set_drive)
314 return -ENOTSUPP;
Kever Yang56573c42019-05-07 09:36:32 +0800315
David Wu40a55482019-04-16 21:55:26 +0800316 return ctrl->set_drive(bank, pin_num, strength);
David Wu5f596ae2019-01-02 21:00:55 +0800317}
318
319static int rockchip_pull_list[PULL_TYPE_MAX][4] = {
320 {
321 PIN_CONFIG_BIAS_DISABLE,
322 PIN_CONFIG_BIAS_PULL_UP,
323 PIN_CONFIG_BIAS_PULL_DOWN,
324 PIN_CONFIG_BIAS_BUS_HOLD
325 },
326 {
327 PIN_CONFIG_BIAS_DISABLE,
328 PIN_CONFIG_BIAS_PULL_DOWN,
329 PIN_CONFIG_BIAS_DISABLE,
330 PIN_CONFIG_BIAS_PULL_UP
331 },
332};
333
David Wu2972c452019-04-16 21:57:05 +0800334int rockchip_translate_pull_value(int type, int pull)
335{
336 int i, ret;
337
338 ret = -EINVAL;
339 for (i = 0; i < ARRAY_SIZE(rockchip_pull_list[type]);
340 i++) {
341 if (rockchip_pull_list[type][i] == pull) {
342 ret = i;
343 break;
344 }
345 }
346
347 return ret;
348}
349
David Wu5f596ae2019-01-02 21:00:55 +0800350static int rockchip_set_pull(struct rockchip_pin_bank *bank,
351 int pin_num, int pull)
352{
353 struct rockchip_pinctrl_priv *priv = bank->priv;
354 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
David Wu5f596ae2019-01-02 21:00:55 +0800355
356 debug("setting pull of GPIO%d-%d to %d\n", bank->bank_num,
357 pin_num, pull);
358
David Wu2972c452019-04-16 21:57:05 +0800359 if (!ctrl->set_pull)
360 return -ENOTSUPP;
David Wu5f596ae2019-01-02 21:00:55 +0800361
David Wu2972c452019-04-16 21:57:05 +0800362 return ctrl->set_pull(bank, pin_num, pull);
David Wu5f596ae2019-01-02 21:00:55 +0800363}
364
365static int rockchip_set_schmitt(struct rockchip_pin_bank *bank,
366 int pin_num, int enable)
367{
368 struct rockchip_pinctrl_priv *priv = bank->priv;
369 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
David Wu5f596ae2019-01-02 21:00:55 +0800370
371 debug("setting input schmitt of GPIO%d-%d to %d\n", bank->bank_num,
372 pin_num, enable);
373
David Wu7ae4ec92019-04-16 21:58:13 +0800374 if (!ctrl->set_schmitt)
375 return -ENOTSUPP;
David Wu5f596ae2019-01-02 21:00:55 +0800376
David Wu7ae4ec92019-04-16 21:58:13 +0800377 return ctrl->set_schmitt(bank, pin_num, enable);
David Wu5f596ae2019-01-02 21:00:55 +0800378}
379
David Wu5f596ae2019-01-02 21:00:55 +0800380/* set the pin config settings for a specified pin */
381static int rockchip_pinconf_set(struct rockchip_pin_bank *bank,
382 u32 pin, u32 param, u32 arg)
383{
David Wu5f596ae2019-01-02 21:00:55 +0800384 int rc;
385
386 switch (param) {
387 case PIN_CONFIG_BIAS_DISABLE:
David Wu5f596ae2019-01-02 21:00:55 +0800388 case PIN_CONFIG_BIAS_PULL_UP:
389 case PIN_CONFIG_BIAS_PULL_DOWN:
390 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
391 case PIN_CONFIG_BIAS_BUS_HOLD:
David Wu5f596ae2019-01-02 21:00:55 +0800392 rc = rockchip_set_pull(bank, pin, param);
393 if (rc)
394 return rc;
395 break;
396
397 case PIN_CONFIG_DRIVE_STRENGTH:
David Wu5f596ae2019-01-02 21:00:55 +0800398 rc = rockchip_set_drive_perpin(bank, pin, arg);
399 if (rc < 0)
400 return rc;
401 break;
402
403 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
David Wu5f596ae2019-01-02 21:00:55 +0800404 rc = rockchip_set_schmitt(bank, pin, arg);
405 if (rc < 0)
406 return rc;
407 break;
408
409 default:
410 break;
411 }
412
413 return 0;
414}
415
416static const struct pinconf_param rockchip_conf_params[] = {
417 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
418 { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
419 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
420 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
David Wu18564512019-04-16 21:50:53 +0800421 { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
David Wu5f596ae2019-01-02 21:00:55 +0800422 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
David Wu5f596ae2019-01-02 21:00:55 +0800423 { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 },
424 { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 },
425};
426
427static int rockchip_pinconf_prop_name_to_param(const char *property,
428 u32 *default_value)
429{
430 const struct pinconf_param *p, *end;
431
432 p = rockchip_conf_params;
433 end = p + sizeof(rockchip_conf_params) / sizeof(struct pinconf_param);
434
435 /* See if this pctldev supports this parameter */
436 for (; p < end; p++) {
437 if (!strcmp(property, p->property)) {
438 *default_value = p->default_value;
439 return p->param;
440 }
441 }
442
443 *default_value = 0;
444 return -EPERM;
445}
446
447static int rockchip_pinctrl_set_state(struct udevice *dev,
448 struct udevice *config)
449{
450 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
451 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
452 u32 cells[MAX_ROCKCHIP_PINS_ENTRIES * 4];
453 u32 bank, pin, mux, conf, arg, default_val;
454 int ret, count, i;
455 const char *prop_name;
456 const void *value;
457 int prop_len, param;
458 const u32 *data;
459 ofnode node;
Jonas Karlman420b0102023-06-08 10:59:38 +0000460 struct ofprop prop;
David Wu5f596ae2019-01-02 21:00:55 +0800461 data = dev_read_prop(config, "rockchip,pins", &count);
462 if (count < 0) {
463 debug("%s: bad array size %d\n", __func__, count);
464 return -EINVAL;
465 }
466
467 count /= sizeof(u32);
468 if (count > MAX_ROCKCHIP_PINS_ENTRIES * 4) {
469 debug("%s: unsupported pins array count %d\n",
470 __func__, count);
471 return -EINVAL;
472 }
473
474 for (i = 0; i < count; i++)
475 cells[i] = fdt32_to_cpu(data[i]);
476
477 for (i = 0; i < (count >> 2); i++) {
478 bank = cells[4 * i + 0];
479 pin = cells[4 * i + 1];
480 mux = cells[4 * i + 2];
481 conf = cells[4 * i + 3];
482
483 ret = rockchip_verify_config(dev, bank, pin);
484 if (ret)
485 return ret;
486
487 ret = rockchip_set_mux(&ctrl->pin_banks[bank], pin, mux);
488 if (ret)
489 return ret;
490
491 node = ofnode_get_by_phandle(conf);
492 if (!ofnode_valid(node))
493 return -ENODEV;
Jonas Karlman420b0102023-06-08 10:59:38 +0000494 ofnode_for_each_prop(prop, node) {
495 value = ofprop_get_property(&prop, &prop_name, &prop_len);
David Wu5f596ae2019-01-02 21:00:55 +0800496 if (!value)
Jonas Karlman420b0102023-06-08 10:59:38 +0000497 continue;
498
David Wu5f596ae2019-01-02 21:00:55 +0800499 param = rockchip_pinconf_prop_name_to_param(prop_name,
500 &default_val);
501 if (param < 0)
Jonas Karlman420b0102023-06-08 10:59:38 +0000502 continue;
David Wu5f596ae2019-01-02 21:00:55 +0800503
504 if (prop_len >= sizeof(fdt32_t))
505 arg = fdt32_to_cpu(*(fdt32_t *)value);
506 else
507 arg = default_val;
508
509 ret = rockchip_pinconf_set(&ctrl->pin_banks[bank], pin,
510 param, arg);
511 if (ret) {
512 debug("%s: rockchip_pinconf_set fail: %d\n",
513 __func__, ret);
514 return ret;
515 }
516 }
517 }
518
519 return 0;
520}
521
522const struct pinctrl_ops rockchip_pinctrl_ops = {
523 .set_state = rockchip_pinctrl_set_state,
524 .get_gpio_mux = rockchip_pinctrl_get_gpio_mux,
525};
526
527/* retrieve the soc specific data */
528static struct rockchip_pin_ctrl *rockchip_pinctrl_get_soc_data(struct udevice *dev)
529{
530 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
531 struct rockchip_pin_ctrl *ctrl =
532 (struct rockchip_pin_ctrl *)dev_get_driver_data(dev);
533 struct rockchip_pin_bank *bank;
534 int grf_offs, pmu_offs, drv_grf_offs, drv_pmu_offs, i, j;
535
536 grf_offs = ctrl->grf_mux_offset;
537 pmu_offs = ctrl->pmu_mux_offset;
538 drv_pmu_offs = ctrl->pmu_drv_offset;
539 drv_grf_offs = ctrl->grf_drv_offset;
540 bank = ctrl->pin_banks;
541
542 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
543 int bank_pins = 0;
544
545 bank->priv = priv;
546 bank->pin_base = ctrl->nr_pins;
547 ctrl->nr_pins += bank->nr_pins;
548
549 /* calculate iomux and drv offsets */
550 for (j = 0; j < 4; j++) {
551 struct rockchip_iomux *iom = &bank->iomux[j];
552 struct rockchip_drv *drv = &bank->drv[j];
553 int inc;
554
555 if (bank_pins >= bank->nr_pins)
556 break;
557
558 /* preset iomux offset value, set new start value */
559 if (iom->offset >= 0) {
Jonas Karlmandd46eac2024-05-12 12:16:15 +0000560 if ((iom->type & IOMUX_SOURCE_PMU) ||
561 (iom->type & IOMUX_L_SOURCE_PMU))
David Wu5f596ae2019-01-02 21:00:55 +0800562 pmu_offs = iom->offset;
563 else
564 grf_offs = iom->offset;
565 } else { /* set current iomux offset */
Jonas Karlmandd46eac2024-05-12 12:16:15 +0000566 iom->offset = ((iom->type & IOMUX_SOURCE_PMU) ||
567 (iom->type & IOMUX_L_SOURCE_PMU)) ?
David Wu5f596ae2019-01-02 21:00:55 +0800568 pmu_offs : grf_offs;
569 }
570
571 /* preset drv offset value, set new start value */
572 if (drv->offset >= 0) {
573 if (iom->type & IOMUX_SOURCE_PMU)
574 drv_pmu_offs = drv->offset;
575 else
576 drv_grf_offs = drv->offset;
577 } else { /* set current drv offset */
578 drv->offset = (iom->type & IOMUX_SOURCE_PMU) ?
579 drv_pmu_offs : drv_grf_offs;
580 }
581
582 debug("bank %d, iomux %d has iom_offset 0x%x drv_offset 0x%x\n",
583 i, j, iom->offset, drv->offset);
584
585 /*
586 * Increase offset according to iomux width.
587 * 4bit iomux'es are spread over two registers.
588 */
589 inc = (iom->type & (IOMUX_WIDTH_4BIT |
David Wufd2fdf72019-12-03 19:26:50 +0800590 IOMUX_WIDTH_3BIT |
591 IOMUX_8WIDTH_2BIT)) ? 8 : 4;
Jagan Teki9e0e6812022-12-14 23:20:56 +0530592 if ((iom->type & IOMUX_SOURCE_PMU) || (iom->type & IOMUX_L_SOURCE_PMU))
David Wu5f596ae2019-01-02 21:00:55 +0800593 pmu_offs += inc;
594 else
595 grf_offs += inc;
596
597 /*
598 * Increase offset according to drv width.
599 * 3bit drive-strenth'es are spread over two registers.
600 */
601 if ((drv->drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) ||
602 (drv->drv_type == DRV_TYPE_IO_3V3_ONLY))
603 inc = 8;
604 else
605 inc = 4;
606
607 if (iom->type & IOMUX_SOURCE_PMU)
608 drv_pmu_offs += inc;
609 else
610 drv_grf_offs += inc;
611
612 bank_pins += 8;
613 }
614
615 /* calculate the per-bank recalced_mask */
616 for (j = 0; j < ctrl->niomux_recalced; j++) {
617 int pin = 0;
618
619 if (ctrl->iomux_recalced[j].num == bank->bank_num) {
620 pin = ctrl->iomux_recalced[j].pin;
621 bank->recalced_mask |= BIT(pin);
622 }
623 }
624
625 /* calculate the per-bank route_mask */
626 for (j = 0; j < ctrl->niomux_routes; j++) {
627 int pin = 0;
628
629 if (ctrl->iomux_routes[j].bank_num == bank->bank_num) {
630 pin = ctrl->iomux_routes[j].pin;
631 bank->route_mask |= BIT(pin);
632 }
633 }
634 }
635
636 return ctrl;
637}
638
639int rockchip_pinctrl_probe(struct udevice *dev)
640{
641 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
642 struct rockchip_pin_ctrl *ctrl;
643 struct udevice *syscon;
644 struct regmap *regmap;
645 int ret = 0;
646
647 /* get rockchip grf syscon phandle */
648 ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, "rockchip,grf",
649 &syscon);
650 if (ret) {
651 debug("unable to find rockchip,grf syscon device (%d)\n", ret);
652 return ret;
653 }
654
655 /* get grf-reg base address */
656 regmap = syscon_get_regmap(syscon);
657 if (!regmap) {
658 debug("unable to find rockchip grf regmap\n");
659 return -ENODEV;
660 }
661 priv->regmap_base = regmap;
662
663 /* option: get pmu-reg base address */
664 ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, "rockchip,pmu",
665 &syscon);
666 if (!ret) {
667 /* get pmugrf-reg base address */
668 regmap = syscon_get_regmap(syscon);
669 if (!regmap) {
670 debug("unable to find rockchip pmu regmap\n");
671 return -ENODEV;
672 }
673 priv->regmap_pmu = regmap;
674 }
675
676 ctrl = rockchip_pinctrl_get_soc_data(dev);
677 if (!ctrl) {
678 debug("driver data not available\n");
679 return -EINVAL;
680 }
681
682 priv->ctrl = ctrl;
683 return 0;
684}