blob: 8245b854e63c425df97dc65984584eb3364f1707 [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
Tom Riniabb9a042024-05-18 20:20:43 -06006#include <common.h>
David Wu5f596ae2019-01-02 21:00:55 +08007#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
David Wu5f596ae2019-01-02 21:00:55 +08009#include <dm/pinctrl.h>
10#include <regmap.h>
11#include <syscon.h>
12#include <fdtdec.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060013#include <linux/bitops.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060014#include <linux/libfdt.h>
David Wu5f596ae2019-01-02 21:00:55 +080015
16#include "pinctrl-rockchip.h"
Jonas Karlmanc2427012024-05-12 12:16:16 +000017#include <dt-bindings/pinctrl/rockchip.h>
David Wu5f596ae2019-01-02 21:00:55 +080018
19#define MAX_ROCKCHIP_PINS_ENTRIES 30
20#define MAX_ROCKCHIP_GPIO_PER_BANK 32
David Wu5f596ae2019-01-02 21:00:55 +080021
22static int rockchip_verify_config(struct udevice *dev, u32 bank, u32 pin)
23{
24 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
25 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
26
27 if (bank >= ctrl->nr_banks) {
28 debug("pin conf bank %d >= nbanks %d\n", bank, ctrl->nr_banks);
29 return -EINVAL;
30 }
31
32 if (pin >= MAX_ROCKCHIP_GPIO_PER_BANK) {
33 debug("pin conf pin %d >= %d\n", pin,
34 MAX_ROCKCHIP_GPIO_PER_BANK);
35 return -EINVAL;
36 }
37
38 return 0;
39}
40
David Wu3dd7d6c2019-04-16 21:50:55 +080041void rockchip_get_recalced_mux(struct rockchip_pin_bank *bank, int pin,
42 int *reg, u8 *bit, int *mask)
David Wu5f596ae2019-01-02 21:00:55 +080043{
44 struct rockchip_pinctrl_priv *priv = bank->priv;
45 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
46 struct rockchip_mux_recalced_data *data;
47 int i;
48
49 for (i = 0; i < ctrl->niomux_recalced; i++) {
50 data = &ctrl->iomux_recalced[i];
51 if (data->num == bank->bank_num &&
52 data->pin == pin)
53 break;
54 }
55
56 if (i >= ctrl->niomux_recalced)
57 return;
58
59 *reg = data->reg;
60 *mask = data->mask;
61 *bit = data->bit;
62}
63
Jagan Teki9e0e6812022-12-14 23:20:56 +053064static enum rockchip_pin_route_type
65rockchip_get_mux_route(struct rockchip_pin_bank *bank, int pin,
66 int mux, u32 *reg, u32 *value)
David Wu5f596ae2019-01-02 21:00:55 +080067{
68 struct rockchip_pinctrl_priv *priv = bank->priv;
69 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
70 struct rockchip_mux_route_data *data;
71 int i;
72
73 for (i = 0; i < ctrl->niomux_routes; i++) {
74 data = &ctrl->iomux_routes[i];
75 if (data->bank_num == bank->bank_num &&
76 data->pin == pin && data->func == mux)
77 break;
78 }
79
80 if (i >= ctrl->niomux_routes)
Jagan Teki9e0e6812022-12-14 23:20:56 +053081 return ROUTE_TYPE_INVALID;
David Wu5f596ae2019-01-02 21:00:55 +080082
83 *reg = data->route_offset;
84 *value = data->route_val;
85
Jagan Teki9e0e6812022-12-14 23:20:56 +053086 return data->route_type;
David Wu5f596ae2019-01-02 21:00:55 +080087}
88
David Wu3dd7d6c2019-04-16 21:50:55 +080089int rockchip_get_mux_data(int mux_type, int pin, u8 *bit, int *mask)
David Wu5f596ae2019-01-02 21:00:55 +080090{
91 int offset = 0;
92
93 if (mux_type & IOMUX_WIDTH_4BIT) {
94 if ((pin % 8) >= 4)
95 offset = 0x4;
96 *bit = (pin % 4) * 4;
97 *mask = 0xf;
98 } else if (mux_type & IOMUX_WIDTH_3BIT) {
99 /*
100 * pin0 ~ pin4 are at first register, and
101 * pin5 ~ pin7 are at second register.
102 */
103 if ((pin % 8) >= 5)
104 offset = 0x4;
105 *bit = (pin % 8 % 5) * 3;
106 *mask = 0x7;
107 } else {
108 *bit = (pin % 8) * 2;
109 *mask = 0x3;
110 }
111
112 return offset;
113}
114
115static int rockchip_get_mux(struct rockchip_pin_bank *bank, int pin)
116{
117 struct rockchip_pinctrl_priv *priv = bank->priv;
118 int iomux_num = (pin / 8);
119 struct regmap *regmap;
120 unsigned int val;
121 int reg, ret, mask, mux_type;
122 u8 bit;
123
124 if (iomux_num > 3)
125 return -EINVAL;
126
127 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
128 debug("pin %d is unrouted\n", pin);
129 return -EINVAL;
130 }
131
132 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
133 return RK_FUNC_GPIO;
134
Jonas Karlmandd46eac2024-05-12 12:16:15 +0000135 if (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
136 regmap = priv->regmap_pmu;
137 else if (bank->iomux[iomux_num].type & IOMUX_L_SOURCE_PMU)
138 regmap = (pin % 8 < 4) ? priv->regmap_pmu : priv->regmap_base;
139 else
140 regmap = priv->regmap_base;
David Wu5f596ae2019-01-02 21:00:55 +0800141
142 /* get basic quadrupel of mux registers and the correct reg inside */
143 mux_type = bank->iomux[iomux_num].type;
144 reg = bank->iomux[iomux_num].offset;
145 reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask);
146
147 if (bank->recalced_mask & BIT(pin))
148 rockchip_get_recalced_mux(bank, pin, &reg, &bit, &mask);
149
Jonas Karlmanc2427012024-05-12 12:16:16 +0000150 if (IS_ENABLED(CONFIG_ROCKCHIP_RK3588)) {
151 if (bank->bank_num == 0) {
152 if (pin >= RK_PB4 && pin <= RK_PD7) {
153 u32 reg0 = 0;
154
155 reg0 = reg + 0x4000 - 0xC; /* PMU2_IOC_BASE */
156 ret = regmap_read(regmap, reg0, &val);
157 if (ret)
158 return ret;
159
160 ret = ((val >> bit) & mask);
161 if (ret != 8)
162 return ret;
163
164 reg = reg + 0x8000; /* BUS_IOC_BASE */
165 regmap = priv->regmap_base;
166 }
167 } else if (bank->bank_num > 0) {
168 reg += 0x8000; /* BUS_IOC_BASE */
169 }
170 }
171
David Wu5f596ae2019-01-02 21:00:55 +0800172 ret = regmap_read(regmap, reg, &val);
173 if (ret)
174 return ret;
175
176 return ((val >> bit) & mask);
177}
178
179static int rockchip_pinctrl_get_gpio_mux(struct udevice *dev, int banknum,
180 int index)
181{ struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
182 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
183
184 return rockchip_get_mux(&ctrl->pin_banks[banknum], index);
185}
186
187static int rockchip_verify_mux(struct rockchip_pin_bank *bank,
188 int pin, int mux)
189{
190 int iomux_num = (pin / 8);
191
192 if (iomux_num > 3)
193 return -EINVAL;
194
195 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
196 debug("pin %d is unrouted\n", pin);
197 return -EINVAL;
198 }
199
200 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) {
Jonas Karlmand5da6902024-05-12 12:16:14 +0000201 if (mux != RK_FUNC_GPIO) {
David Wu5f596ae2019-01-02 21:00:55 +0800202 debug("pin %d only supports a gpio mux\n", pin);
203 return -ENOTSUPP;
204 }
205 }
206
207 return 0;
208}
209
210/*
211 * Set a new mux function for a pin.
212 *
213 * The register is divided into the upper and lower 16 bit. When changing
214 * a value, the previous register value is not read and changed. Instead
215 * it seems the changed bits are marked in the upper 16 bit, while the
216 * changed value gets set in the same offset in the lower 16 bit.
217 * All pin settings seem to be 2 bit wide in both the upper and lower
218 * parts.
219 * @bank: pin bank to change
220 * @pin: pin to change
221 * @mux: new mux function to set
222 */
223static int rockchip_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
224{
225 struct rockchip_pinctrl_priv *priv = bank->priv;
David Wu3dd7d6c2019-04-16 21:50:55 +0800226 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
David Wu5f596ae2019-01-02 21:00:55 +0800227 int iomux_num = (pin / 8);
David Wu3dd7d6c2019-04-16 21:50:55 +0800228 int ret;
David Wu5f596ae2019-01-02 21:00:55 +0800229
230 ret = rockchip_verify_mux(bank, pin, mux);
231 if (ret < 0)
232 return ret;
233
234 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
235 return 0;
236
237 debug("setting mux of GPIO%d-%d to %d\n", bank->bank_num, pin, mux);
238
David Wu3dd7d6c2019-04-16 21:50:55 +0800239 if (!ctrl->set_mux)
240 return -ENOTSUPP;
David Wu5f596ae2019-01-02 21:00:55 +0800241
David Wu3dd7d6c2019-04-16 21:50:55 +0800242 ret = ctrl->set_mux(bank, pin, mux);
Jagan Teki9e0e6812022-12-14 23:20:56 +0530243 if (ret)
244 return ret;
David Wu5f596ae2019-01-02 21:00:55 +0800245
Jagan Teki9e0e6812022-12-14 23:20:56 +0530246 if (bank->route_mask & BIT(pin)) {
247 struct regmap *regmap;
248 u32 route_reg = 0, route_val = 0;
249
250 ret = rockchip_get_mux_route(bank, pin, mux,
251 &route_reg, &route_val);
252 switch (ret) {
253 case ROUTE_TYPE_DEFAULT:
254 if (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
255 regmap = priv->regmap_pmu;
256 else if (bank->iomux[iomux_num].type & IOMUX_L_SOURCE_PMU)
257 regmap = (pin % 8 < 4) ? priv->regmap_pmu : priv->regmap_base;
258 else
259 regmap = priv->regmap_base;
260
261 regmap_write(regmap, route_reg, route_val);
262 break;
263 case ROUTE_TYPE_TOPGRF:
264 regmap_write(priv->regmap_base, route_reg, route_val);
265 break;
266 case ROUTE_TYPE_PMUGRF:
267 regmap_write(priv->regmap_pmu, route_reg, route_val);
268 break;
269 case ROUTE_TYPE_INVALID:
270 fallthrough;
271 default:
272 break;
273 }
274 }
275
276 return 0;
David Wu5f596ae2019-01-02 21:00:55 +0800277}
278
279static int rockchip_perpin_drv_list[DRV_TYPE_MAX][8] = {
280 { 2, 4, 8, 12, -1, -1, -1, -1 },
281 { 3, 6, 9, 12, -1, -1, -1, -1 },
282 { 5, 10, 15, 20, -1, -1, -1, -1 },
283 { 4, 6, 8, 10, 12, 14, 16, 18 },
284 { 4, 7, 10, 13, 16, 19, 22, 26 }
285};
286
David Wu40a55482019-04-16 21:55:26 +0800287int rockchip_translate_drive_value(int type, int strength)
David Wu5f596ae2019-01-02 21:00:55 +0800288{
David Wu40a55482019-04-16 21:55:26 +0800289 int i, ret;
David Wu5f596ae2019-01-02 21:00:55 +0800290
291 ret = -EINVAL;
David Wu40a55482019-04-16 21:55:26 +0800292 for (i = 0; i < ARRAY_SIZE(rockchip_perpin_drv_list[type]); i++) {
293 if (rockchip_perpin_drv_list[type][i] == strength) {
David Wu5f596ae2019-01-02 21:00:55 +0800294 ret = i;
295 break;
David Wu40a55482019-04-16 21:55:26 +0800296 } else if (rockchip_perpin_drv_list[type][i] < 0) {
297 ret = rockchip_perpin_drv_list[type][i];
David Wu5f596ae2019-01-02 21:00:55 +0800298 break;
299 }
300 }
301
David Wu40a55482019-04-16 21:55:26 +0800302 return ret;
303}
David Wu5f596ae2019-01-02 21:00:55 +0800304
David Wu40a55482019-04-16 21:55:26 +0800305static int rockchip_set_drive_perpin(struct rockchip_pin_bank *bank,
306 int pin_num, int strength)
307{
308 struct rockchip_pinctrl_priv *priv = bank->priv;
309 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
David Wu5f596ae2019-01-02 21:00:55 +0800310
David Wu40a55482019-04-16 21:55:26 +0800311 debug("setting drive of GPIO%d-%d to %d\n", bank->bank_num,
312 pin_num, strength);
David Wu5f596ae2019-01-02 21:00:55 +0800313
David Wu40a55482019-04-16 21:55:26 +0800314 if (!ctrl->set_drive)
315 return -ENOTSUPP;
Kever Yang56573c42019-05-07 09:36:32 +0800316
David Wu40a55482019-04-16 21:55:26 +0800317 return ctrl->set_drive(bank, pin_num, strength);
David Wu5f596ae2019-01-02 21:00:55 +0800318}
319
320static int rockchip_pull_list[PULL_TYPE_MAX][4] = {
321 {
322 PIN_CONFIG_BIAS_DISABLE,
323 PIN_CONFIG_BIAS_PULL_UP,
324 PIN_CONFIG_BIAS_PULL_DOWN,
325 PIN_CONFIG_BIAS_BUS_HOLD
326 },
327 {
328 PIN_CONFIG_BIAS_DISABLE,
329 PIN_CONFIG_BIAS_PULL_DOWN,
330 PIN_CONFIG_BIAS_DISABLE,
331 PIN_CONFIG_BIAS_PULL_UP
332 },
333};
334
David Wu2972c452019-04-16 21:57:05 +0800335int rockchip_translate_pull_value(int type, int pull)
336{
337 int i, ret;
338
339 ret = -EINVAL;
340 for (i = 0; i < ARRAY_SIZE(rockchip_pull_list[type]);
341 i++) {
342 if (rockchip_pull_list[type][i] == pull) {
343 ret = i;
344 break;
345 }
346 }
347
348 return ret;
349}
350
David Wu5f596ae2019-01-02 21:00:55 +0800351static int rockchip_set_pull(struct rockchip_pin_bank *bank,
352 int pin_num, int pull)
353{
354 struct rockchip_pinctrl_priv *priv = bank->priv;
355 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
David Wu5f596ae2019-01-02 21:00:55 +0800356
357 debug("setting pull of GPIO%d-%d to %d\n", bank->bank_num,
358 pin_num, pull);
359
David Wu2972c452019-04-16 21:57:05 +0800360 if (!ctrl->set_pull)
361 return -ENOTSUPP;
David Wu5f596ae2019-01-02 21:00:55 +0800362
David Wu2972c452019-04-16 21:57:05 +0800363 return ctrl->set_pull(bank, pin_num, pull);
David Wu5f596ae2019-01-02 21:00:55 +0800364}
365
366static int rockchip_set_schmitt(struct rockchip_pin_bank *bank,
367 int pin_num, int enable)
368{
369 struct rockchip_pinctrl_priv *priv = bank->priv;
370 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
David Wu5f596ae2019-01-02 21:00:55 +0800371
372 debug("setting input schmitt of GPIO%d-%d to %d\n", bank->bank_num,
373 pin_num, enable);
374
David Wu7ae4ec92019-04-16 21:58:13 +0800375 if (!ctrl->set_schmitt)
376 return -ENOTSUPP;
David Wu5f596ae2019-01-02 21:00:55 +0800377
David Wu7ae4ec92019-04-16 21:58:13 +0800378 return ctrl->set_schmitt(bank, pin_num, enable);
David Wu5f596ae2019-01-02 21:00:55 +0800379}
380
David Wu5f596ae2019-01-02 21:00:55 +0800381/* set the pin config settings for a specified pin */
382static int rockchip_pinconf_set(struct rockchip_pin_bank *bank,
383 u32 pin, u32 param, u32 arg)
384{
David Wu5f596ae2019-01-02 21:00:55 +0800385 int rc;
386
387 switch (param) {
388 case PIN_CONFIG_BIAS_DISABLE:
David Wu5f596ae2019-01-02 21:00:55 +0800389 case PIN_CONFIG_BIAS_PULL_UP:
390 case PIN_CONFIG_BIAS_PULL_DOWN:
391 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
392 case PIN_CONFIG_BIAS_BUS_HOLD:
David Wu5f596ae2019-01-02 21:00:55 +0800393 rc = rockchip_set_pull(bank, pin, param);
394 if (rc)
395 return rc;
396 break;
397
398 case PIN_CONFIG_DRIVE_STRENGTH:
David Wu5f596ae2019-01-02 21:00:55 +0800399 rc = rockchip_set_drive_perpin(bank, pin, arg);
400 if (rc < 0)
401 return rc;
402 break;
403
404 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
David Wu5f596ae2019-01-02 21:00:55 +0800405 rc = rockchip_set_schmitt(bank, pin, arg);
406 if (rc < 0)
407 return rc;
408 break;
409
410 default:
411 break;
412 }
413
414 return 0;
415}
416
417static const struct pinconf_param rockchip_conf_params[] = {
418 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
419 { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
420 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
421 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
David Wu18564512019-04-16 21:50:53 +0800422 { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
David Wu5f596ae2019-01-02 21:00:55 +0800423 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
David Wu5f596ae2019-01-02 21:00:55 +0800424 { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 },
425 { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 },
426};
427
428static int rockchip_pinconf_prop_name_to_param(const char *property,
429 u32 *default_value)
430{
431 const struct pinconf_param *p, *end;
432
433 p = rockchip_conf_params;
434 end = p + sizeof(rockchip_conf_params) / sizeof(struct pinconf_param);
435
436 /* See if this pctldev supports this parameter */
437 for (; p < end; p++) {
438 if (!strcmp(property, p->property)) {
439 *default_value = p->default_value;
440 return p->param;
441 }
442 }
443
444 *default_value = 0;
445 return -EPERM;
446}
447
448static int rockchip_pinctrl_set_state(struct udevice *dev,
449 struct udevice *config)
450{
451 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
452 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
453 u32 cells[MAX_ROCKCHIP_PINS_ENTRIES * 4];
454 u32 bank, pin, mux, conf, arg, default_val;
455 int ret, count, i;
456 const char *prop_name;
457 const void *value;
458 int prop_len, param;
459 const u32 *data;
460 ofnode node;
Jonas Karlman420b0102023-06-08 10:59:38 +0000461 struct ofprop prop;
David Wu5f596ae2019-01-02 21:00:55 +0800462 data = dev_read_prop(config, "rockchip,pins", &count);
463 if (count < 0) {
464 debug("%s: bad array size %d\n", __func__, count);
465 return -EINVAL;
466 }
467
468 count /= sizeof(u32);
469 if (count > MAX_ROCKCHIP_PINS_ENTRIES * 4) {
470 debug("%s: unsupported pins array count %d\n",
471 __func__, count);
472 return -EINVAL;
473 }
474
475 for (i = 0; i < count; i++)
476 cells[i] = fdt32_to_cpu(data[i]);
477
478 for (i = 0; i < (count >> 2); i++) {
479 bank = cells[4 * i + 0];
480 pin = cells[4 * i + 1];
481 mux = cells[4 * i + 2];
482 conf = cells[4 * i + 3];
483
484 ret = rockchip_verify_config(dev, bank, pin);
485 if (ret)
486 return ret;
487
488 ret = rockchip_set_mux(&ctrl->pin_banks[bank], pin, mux);
489 if (ret)
490 return ret;
491
492 node = ofnode_get_by_phandle(conf);
493 if (!ofnode_valid(node))
494 return -ENODEV;
Jonas Karlman420b0102023-06-08 10:59:38 +0000495 ofnode_for_each_prop(prop, node) {
496 value = ofprop_get_property(&prop, &prop_name, &prop_len);
David Wu5f596ae2019-01-02 21:00:55 +0800497 if (!value)
Jonas Karlman420b0102023-06-08 10:59:38 +0000498 continue;
499
David Wu5f596ae2019-01-02 21:00:55 +0800500 param = rockchip_pinconf_prop_name_to_param(prop_name,
501 &default_val);
502 if (param < 0)
Jonas Karlman420b0102023-06-08 10:59:38 +0000503 continue;
David Wu5f596ae2019-01-02 21:00:55 +0800504
505 if (prop_len >= sizeof(fdt32_t))
506 arg = fdt32_to_cpu(*(fdt32_t *)value);
507 else
508 arg = default_val;
509
510 ret = rockchip_pinconf_set(&ctrl->pin_banks[bank], pin,
511 param, arg);
512 if (ret) {
513 debug("%s: rockchip_pinconf_set fail: %d\n",
514 __func__, ret);
515 return ret;
516 }
517 }
518 }
519
520 return 0;
521}
522
523const struct pinctrl_ops rockchip_pinctrl_ops = {
524 .set_state = rockchip_pinctrl_set_state,
525 .get_gpio_mux = rockchip_pinctrl_get_gpio_mux,
526};
527
528/* retrieve the soc specific data */
529static struct rockchip_pin_ctrl *rockchip_pinctrl_get_soc_data(struct udevice *dev)
530{
531 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
532 struct rockchip_pin_ctrl *ctrl =
533 (struct rockchip_pin_ctrl *)dev_get_driver_data(dev);
534 struct rockchip_pin_bank *bank;
535 int grf_offs, pmu_offs, drv_grf_offs, drv_pmu_offs, i, j;
536
537 grf_offs = ctrl->grf_mux_offset;
538 pmu_offs = ctrl->pmu_mux_offset;
539 drv_pmu_offs = ctrl->pmu_drv_offset;
540 drv_grf_offs = ctrl->grf_drv_offset;
541 bank = ctrl->pin_banks;
542
543 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
544 int bank_pins = 0;
545
546 bank->priv = priv;
547 bank->pin_base = ctrl->nr_pins;
548 ctrl->nr_pins += bank->nr_pins;
549
550 /* calculate iomux and drv offsets */
551 for (j = 0; j < 4; j++) {
552 struct rockchip_iomux *iom = &bank->iomux[j];
553 struct rockchip_drv *drv = &bank->drv[j];
554 int inc;
555
556 if (bank_pins >= bank->nr_pins)
557 break;
558
559 /* preset iomux offset value, set new start value */
560 if (iom->offset >= 0) {
Jonas Karlmandd46eac2024-05-12 12:16:15 +0000561 if ((iom->type & IOMUX_SOURCE_PMU) ||
562 (iom->type & IOMUX_L_SOURCE_PMU))
David Wu5f596ae2019-01-02 21:00:55 +0800563 pmu_offs = iom->offset;
564 else
565 grf_offs = iom->offset;
566 } else { /* set current iomux offset */
Jonas Karlmandd46eac2024-05-12 12:16:15 +0000567 iom->offset = ((iom->type & IOMUX_SOURCE_PMU) ||
568 (iom->type & IOMUX_L_SOURCE_PMU)) ?
David Wu5f596ae2019-01-02 21:00:55 +0800569 pmu_offs : grf_offs;
570 }
571
572 /* preset drv offset value, set new start value */
573 if (drv->offset >= 0) {
574 if (iom->type & IOMUX_SOURCE_PMU)
575 drv_pmu_offs = drv->offset;
576 else
577 drv_grf_offs = drv->offset;
578 } else { /* set current drv offset */
579 drv->offset = (iom->type & IOMUX_SOURCE_PMU) ?
580 drv_pmu_offs : drv_grf_offs;
581 }
582
583 debug("bank %d, iomux %d has iom_offset 0x%x drv_offset 0x%x\n",
584 i, j, iom->offset, drv->offset);
585
586 /*
587 * Increase offset according to iomux width.
588 * 4bit iomux'es are spread over two registers.
589 */
590 inc = (iom->type & (IOMUX_WIDTH_4BIT |
David Wufd2fdf72019-12-03 19:26:50 +0800591 IOMUX_WIDTH_3BIT |
592 IOMUX_8WIDTH_2BIT)) ? 8 : 4;
Jagan Teki9e0e6812022-12-14 23:20:56 +0530593 if ((iom->type & IOMUX_SOURCE_PMU) || (iom->type & IOMUX_L_SOURCE_PMU))
David Wu5f596ae2019-01-02 21:00:55 +0800594 pmu_offs += inc;
595 else
596 grf_offs += inc;
597
598 /*
599 * Increase offset according to drv width.
600 * 3bit drive-strenth'es are spread over two registers.
601 */
602 if ((drv->drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) ||
603 (drv->drv_type == DRV_TYPE_IO_3V3_ONLY))
604 inc = 8;
605 else
606 inc = 4;
607
608 if (iom->type & IOMUX_SOURCE_PMU)
609 drv_pmu_offs += inc;
610 else
611 drv_grf_offs += inc;
612
613 bank_pins += 8;
614 }
615
616 /* calculate the per-bank recalced_mask */
617 for (j = 0; j < ctrl->niomux_recalced; j++) {
618 int pin = 0;
619
620 if (ctrl->iomux_recalced[j].num == bank->bank_num) {
621 pin = ctrl->iomux_recalced[j].pin;
622 bank->recalced_mask |= BIT(pin);
623 }
624 }
625
626 /* calculate the per-bank route_mask */
627 for (j = 0; j < ctrl->niomux_routes; j++) {
628 int pin = 0;
629
630 if (ctrl->iomux_routes[j].bank_num == bank->bank_num) {
631 pin = ctrl->iomux_routes[j].pin;
632 bank->route_mask |= BIT(pin);
633 }
634 }
635 }
636
637 return ctrl;
638}
639
640int rockchip_pinctrl_probe(struct udevice *dev)
641{
642 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
643 struct rockchip_pin_ctrl *ctrl;
644 struct udevice *syscon;
645 struct regmap *regmap;
646 int ret = 0;
647
648 /* get rockchip grf syscon phandle */
649 ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, "rockchip,grf",
650 &syscon);
651 if (ret) {
652 debug("unable to find rockchip,grf syscon device (%d)\n", ret);
653 return ret;
654 }
655
656 /* get grf-reg base address */
657 regmap = syscon_get_regmap(syscon);
658 if (!regmap) {
659 debug("unable to find rockchip grf regmap\n");
660 return -ENODEV;
661 }
662 priv->regmap_base = regmap;
663
664 /* option: get pmu-reg base address */
665 ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, "rockchip,pmu",
666 &syscon);
667 if (!ret) {
668 /* get pmugrf-reg base address */
669 regmap = syscon_get_regmap(syscon);
670 if (!regmap) {
671 debug("unable to find rockchip pmu regmap\n");
672 return -ENODEV;
673 }
674 priv->regmap_pmu = regmap;
675 }
676
677 ctrl = rockchip_pinctrl_get_soc_data(dev);
678 if (!ctrl) {
679 debug("driver data not available\n");
680 return -EINVAL;
681 }
682
683 priv->ctrl = ctrl;
684 return 0;
685}