1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2019 Rockchip Electronics Co., Ltd
9 #include <dm/pinctrl.h>
13 #include <linux/bitops.h>
14 #include <linux/libfdt.h>
15 #include <asm/global_data.h>
17 #include "pinctrl-rockchip.h"
19 #define MAX_ROCKCHIP_PINS_ENTRIES 30
20 #define MAX_ROCKCHIP_GPIO_PER_BANK 32
21 #define RK_FUNC_GPIO 0
23 static int rockchip_verify_config(struct udevice *dev, u32 bank, u32 pin)
25 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
26 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
28 if (bank >= ctrl->nr_banks) {
29 debug("pin conf bank %d >= nbanks %d\n", bank, ctrl->nr_banks);
33 if (pin >= MAX_ROCKCHIP_GPIO_PER_BANK) {
34 debug("pin conf pin %d >= %d\n", pin,
35 MAX_ROCKCHIP_GPIO_PER_BANK);
42 void rockchip_get_recalced_mux(struct rockchip_pin_bank *bank, int pin,
43 int *reg, u8 *bit, int *mask)
45 struct rockchip_pinctrl_priv *priv = bank->priv;
46 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
47 struct rockchip_mux_recalced_data *data;
50 for (i = 0; i < ctrl->niomux_recalced; i++) {
51 data = &ctrl->iomux_recalced[i];
52 if (data->num == bank->bank_num &&
57 if (i >= ctrl->niomux_recalced)
65 static enum rockchip_pin_route_type
66 rockchip_get_mux_route(struct rockchip_pin_bank *bank, int pin,
67 int mux, u32 *reg, u32 *value)
69 struct rockchip_pinctrl_priv *priv = bank->priv;
70 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
71 struct rockchip_mux_route_data *data;
74 for (i = 0; i < ctrl->niomux_routes; i++) {
75 data = &ctrl->iomux_routes[i];
76 if (data->bank_num == bank->bank_num &&
77 data->pin == pin && data->func == mux)
81 if (i >= ctrl->niomux_routes)
82 return ROUTE_TYPE_INVALID;
84 *reg = data->route_offset;
85 *value = data->route_val;
87 return data->route_type;
90 int rockchip_get_mux_data(int mux_type, int pin, u8 *bit, int *mask)
94 if (mux_type & IOMUX_WIDTH_4BIT) {
99 } else if (mux_type & IOMUX_WIDTH_3BIT) {
101 * pin0 ~ pin4 are at first register, and
102 * pin5 ~ pin7 are at second register.
106 *bit = (pin % 8 % 5) * 3;
109 *bit = (pin % 8) * 2;
116 static int rockchip_get_mux(struct rockchip_pin_bank *bank, int pin)
118 struct rockchip_pinctrl_priv *priv = bank->priv;
119 int iomux_num = (pin / 8);
120 struct regmap *regmap;
122 int reg, ret, mask, mux_type;
128 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
129 debug("pin %d is unrouted\n", pin);
133 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
136 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
137 ? priv->regmap_pmu : priv->regmap_base;
139 /* get basic quadrupel of mux registers and the correct reg inside */
140 mux_type = bank->iomux[iomux_num].type;
141 reg = bank->iomux[iomux_num].offset;
142 reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask);
144 if (bank->recalced_mask & BIT(pin))
145 rockchip_get_recalced_mux(bank, pin, ®, &bit, &mask);
147 ret = regmap_read(regmap, reg, &val);
151 return ((val >> bit) & mask);
154 static int rockchip_pinctrl_get_gpio_mux(struct udevice *dev, int banknum,
156 { struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
157 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
159 return rockchip_get_mux(&ctrl->pin_banks[banknum], index);
162 static int rockchip_verify_mux(struct rockchip_pin_bank *bank,
165 int iomux_num = (pin / 8);
170 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
171 debug("pin %d is unrouted\n", pin);
175 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) {
176 if (mux != IOMUX_GPIO_ONLY) {
177 debug("pin %d only supports a gpio mux\n", pin);
186 * Set a new mux function for a pin.
188 * The register is divided into the upper and lower 16 bit. When changing
189 * a value, the previous register value is not read and changed. Instead
190 * it seems the changed bits are marked in the upper 16 bit, while the
191 * changed value gets set in the same offset in the lower 16 bit.
192 * All pin settings seem to be 2 bit wide in both the upper and lower
194 * @bank: pin bank to change
195 * @pin: pin to change
196 * @mux: new mux function to set
198 static int rockchip_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
200 struct rockchip_pinctrl_priv *priv = bank->priv;
201 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
202 int iomux_num = (pin / 8);
205 ret = rockchip_verify_mux(bank, pin, mux);
209 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
212 debug("setting mux of GPIO%d-%d to %d\n", bank->bank_num, pin, mux);
217 ret = ctrl->set_mux(bank, pin, mux);
221 if (bank->route_mask & BIT(pin)) {
222 struct regmap *regmap;
223 u32 route_reg = 0, route_val = 0;
225 ret = rockchip_get_mux_route(bank, pin, mux,
226 &route_reg, &route_val);
228 case ROUTE_TYPE_DEFAULT:
229 if (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
230 regmap = priv->regmap_pmu;
231 else if (bank->iomux[iomux_num].type & IOMUX_L_SOURCE_PMU)
232 regmap = (pin % 8 < 4) ? priv->regmap_pmu : priv->regmap_base;
234 regmap = priv->regmap_base;
236 regmap_write(regmap, route_reg, route_val);
238 case ROUTE_TYPE_TOPGRF:
239 regmap_write(priv->regmap_base, route_reg, route_val);
241 case ROUTE_TYPE_PMUGRF:
242 regmap_write(priv->regmap_pmu, route_reg, route_val);
244 case ROUTE_TYPE_INVALID:
254 static int rockchip_perpin_drv_list[DRV_TYPE_MAX][8] = {
255 { 2, 4, 8, 12, -1, -1, -1, -1 },
256 { 3, 6, 9, 12, -1, -1, -1, -1 },
257 { 5, 10, 15, 20, -1, -1, -1, -1 },
258 { 4, 6, 8, 10, 12, 14, 16, 18 },
259 { 4, 7, 10, 13, 16, 19, 22, 26 }
262 int rockchip_translate_drive_value(int type, int strength)
267 for (i = 0; i < ARRAY_SIZE(rockchip_perpin_drv_list[type]); i++) {
268 if (rockchip_perpin_drv_list[type][i] == strength) {
271 } else if (rockchip_perpin_drv_list[type][i] < 0) {
272 ret = rockchip_perpin_drv_list[type][i];
280 static int rockchip_set_drive_perpin(struct rockchip_pin_bank *bank,
281 int pin_num, int strength)
283 struct rockchip_pinctrl_priv *priv = bank->priv;
284 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
286 debug("setting drive of GPIO%d-%d to %d\n", bank->bank_num,
289 if (!ctrl->set_drive)
292 return ctrl->set_drive(bank, pin_num, strength);
295 static int rockchip_pull_list[PULL_TYPE_MAX][4] = {
297 PIN_CONFIG_BIAS_DISABLE,
298 PIN_CONFIG_BIAS_PULL_UP,
299 PIN_CONFIG_BIAS_PULL_DOWN,
300 PIN_CONFIG_BIAS_BUS_HOLD
303 PIN_CONFIG_BIAS_DISABLE,
304 PIN_CONFIG_BIAS_PULL_DOWN,
305 PIN_CONFIG_BIAS_DISABLE,
306 PIN_CONFIG_BIAS_PULL_UP
310 int rockchip_translate_pull_value(int type, int pull)
315 for (i = 0; i < ARRAY_SIZE(rockchip_pull_list[type]);
317 if (rockchip_pull_list[type][i] == pull) {
326 static int rockchip_set_pull(struct rockchip_pin_bank *bank,
327 int pin_num, int pull)
329 struct rockchip_pinctrl_priv *priv = bank->priv;
330 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
332 debug("setting pull of GPIO%d-%d to %d\n", bank->bank_num,
338 return ctrl->set_pull(bank, pin_num, pull);
341 static int rockchip_set_schmitt(struct rockchip_pin_bank *bank,
342 int pin_num, int enable)
344 struct rockchip_pinctrl_priv *priv = bank->priv;
345 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
347 debug("setting input schmitt of GPIO%d-%d to %d\n", bank->bank_num,
350 if (!ctrl->set_schmitt)
353 return ctrl->set_schmitt(bank, pin_num, enable);
356 /* set the pin config settings for a specified pin */
357 static int rockchip_pinconf_set(struct rockchip_pin_bank *bank,
358 u32 pin, u32 param, u32 arg)
363 case PIN_CONFIG_BIAS_DISABLE:
364 case PIN_CONFIG_BIAS_PULL_UP:
365 case PIN_CONFIG_BIAS_PULL_DOWN:
366 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
367 case PIN_CONFIG_BIAS_BUS_HOLD:
368 rc = rockchip_set_pull(bank, pin, param);
373 case PIN_CONFIG_DRIVE_STRENGTH:
374 rc = rockchip_set_drive_perpin(bank, pin, arg);
379 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
380 rc = rockchip_set_schmitt(bank, pin, arg);
392 static const struct pinconf_param rockchip_conf_params[] = {
393 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
394 { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
395 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
396 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
397 { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
398 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
399 { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 },
400 { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 },
403 static int rockchip_pinconf_prop_name_to_param(const char *property,
406 const struct pinconf_param *p, *end;
408 p = rockchip_conf_params;
409 end = p + sizeof(rockchip_conf_params) / sizeof(struct pinconf_param);
411 /* See if this pctldev supports this parameter */
412 for (; p < end; p++) {
413 if (!strcmp(property, p->property)) {
414 *default_value = p->default_value;
423 static int rockchip_pinctrl_set_state(struct udevice *dev,
424 struct udevice *config)
426 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
427 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
428 u32 cells[MAX_ROCKCHIP_PINS_ENTRIES * 4];
429 u32 bank, pin, mux, conf, arg, default_val;
431 const char *prop_name;
436 #ifdef CONFIG_OF_LIVE
437 const struct device_node *np;
440 int property_offset, pcfg_node;
441 const void *blob = gd->fdt_blob;
443 data = dev_read_prop(config, "rockchip,pins", &count);
445 debug("%s: bad array size %d\n", __func__, count);
449 count /= sizeof(u32);
450 if (count > MAX_ROCKCHIP_PINS_ENTRIES * 4) {
451 debug("%s: unsupported pins array count %d\n",
456 for (i = 0; i < count; i++)
457 cells[i] = fdt32_to_cpu(data[i]);
459 for (i = 0; i < (count >> 2); i++) {
460 bank = cells[4 * i + 0];
461 pin = cells[4 * i + 1];
462 mux = cells[4 * i + 2];
463 conf = cells[4 * i + 3];
465 ret = rockchip_verify_config(dev, bank, pin);
469 ret = rockchip_set_mux(&ctrl->pin_banks[bank], pin, mux);
473 node = ofnode_get_by_phandle(conf);
474 if (!ofnode_valid(node))
476 #ifdef CONFIG_OF_LIVE
477 np = ofnode_to_np(node);
478 for (pp = np->properties; pp; pp = pp->next) {
479 prop_name = pp->name;
480 prop_len = pp->length;
483 pcfg_node = ofnode_to_offset(node);
484 fdt_for_each_property_offset(property_offset, blob, pcfg_node) {
485 value = fdt_getprop_by_offset(blob, property_offset,
486 &prop_name, &prop_len);
490 param = rockchip_pinconf_prop_name_to_param(prop_name,
495 if (prop_len >= sizeof(fdt32_t))
496 arg = fdt32_to_cpu(*(fdt32_t *)value);
500 ret = rockchip_pinconf_set(&ctrl->pin_banks[bank], pin,
503 debug("%s: rockchip_pinconf_set fail: %d\n",
513 const struct pinctrl_ops rockchip_pinctrl_ops = {
514 .set_state = rockchip_pinctrl_set_state,
515 .get_gpio_mux = rockchip_pinctrl_get_gpio_mux,
518 /* retrieve the soc specific data */
519 static struct rockchip_pin_ctrl *rockchip_pinctrl_get_soc_data(struct udevice *dev)
521 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
522 struct rockchip_pin_ctrl *ctrl =
523 (struct rockchip_pin_ctrl *)dev_get_driver_data(dev);
524 struct rockchip_pin_bank *bank;
525 int grf_offs, pmu_offs, drv_grf_offs, drv_pmu_offs, i, j;
527 grf_offs = ctrl->grf_mux_offset;
528 pmu_offs = ctrl->pmu_mux_offset;
529 drv_pmu_offs = ctrl->pmu_drv_offset;
530 drv_grf_offs = ctrl->grf_drv_offset;
531 bank = ctrl->pin_banks;
533 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
537 bank->pin_base = ctrl->nr_pins;
538 ctrl->nr_pins += bank->nr_pins;
540 /* calculate iomux and drv offsets */
541 for (j = 0; j < 4; j++) {
542 struct rockchip_iomux *iom = &bank->iomux[j];
543 struct rockchip_drv *drv = &bank->drv[j];
546 if (bank_pins >= bank->nr_pins)
549 /* preset iomux offset value, set new start value */
550 if (iom->offset >= 0) {
551 if (iom->type & IOMUX_SOURCE_PMU)
552 pmu_offs = iom->offset;
554 grf_offs = iom->offset;
555 } else { /* set current iomux offset */
556 iom->offset = (iom->type & IOMUX_SOURCE_PMU) ?
560 /* preset drv offset value, set new start value */
561 if (drv->offset >= 0) {
562 if (iom->type & IOMUX_SOURCE_PMU)
563 drv_pmu_offs = drv->offset;
565 drv_grf_offs = drv->offset;
566 } else { /* set current drv offset */
567 drv->offset = (iom->type & IOMUX_SOURCE_PMU) ?
568 drv_pmu_offs : drv_grf_offs;
571 debug("bank %d, iomux %d has iom_offset 0x%x drv_offset 0x%x\n",
572 i, j, iom->offset, drv->offset);
575 * Increase offset according to iomux width.
576 * 4bit iomux'es are spread over two registers.
578 inc = (iom->type & (IOMUX_WIDTH_4BIT |
580 IOMUX_8WIDTH_2BIT)) ? 8 : 4;
581 if ((iom->type & IOMUX_SOURCE_PMU) || (iom->type & IOMUX_L_SOURCE_PMU))
587 * Increase offset according to drv width.
588 * 3bit drive-strenth'es are spread over two registers.
590 if ((drv->drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) ||
591 (drv->drv_type == DRV_TYPE_IO_3V3_ONLY))
596 if (iom->type & IOMUX_SOURCE_PMU)
604 /* calculate the per-bank recalced_mask */
605 for (j = 0; j < ctrl->niomux_recalced; j++) {
608 if (ctrl->iomux_recalced[j].num == bank->bank_num) {
609 pin = ctrl->iomux_recalced[j].pin;
610 bank->recalced_mask |= BIT(pin);
614 /* calculate the per-bank route_mask */
615 for (j = 0; j < ctrl->niomux_routes; j++) {
618 if (ctrl->iomux_routes[j].bank_num == bank->bank_num) {
619 pin = ctrl->iomux_routes[j].pin;
620 bank->route_mask |= BIT(pin);
628 int rockchip_pinctrl_probe(struct udevice *dev)
630 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
631 struct rockchip_pin_ctrl *ctrl;
632 struct udevice *syscon;
633 struct regmap *regmap;
636 /* get rockchip grf syscon phandle */
637 ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, "rockchip,grf",
640 debug("unable to find rockchip,grf syscon device (%d)\n", ret);
644 /* get grf-reg base address */
645 regmap = syscon_get_regmap(syscon);
647 debug("unable to find rockchip grf regmap\n");
650 priv->regmap_base = regmap;
652 /* option: get pmu-reg base address */
653 ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, "rockchip,pmu",
656 /* get pmugrf-reg base address */
657 regmap = syscon_get_regmap(syscon);
659 debug("unable to find rockchip pmu regmap\n");
662 priv->regmap_pmu = regmap;
665 ctrl = rockchip_pinctrl_get_soc_data(dev);
667 debug("driver data not available\n");