1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2019 Rockchip Electronics Co., Ltd
8 #include <dm/pinctrl.h>
13 #include "pinctrl-rockchip.h"
15 #define MAX_ROCKCHIP_PINS_ENTRIES 30
16 #define MAX_ROCKCHIP_GPIO_PER_BANK 32
17 #define RK_FUNC_GPIO 0
19 static int rockchip_verify_config(struct udevice *dev, u32 bank, u32 pin)
21 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
22 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
24 if (bank >= ctrl->nr_banks) {
25 debug("pin conf bank %d >= nbanks %d\n", bank, ctrl->nr_banks);
29 if (pin >= MAX_ROCKCHIP_GPIO_PER_BANK) {
30 debug("pin conf pin %d >= %d\n", pin,
31 MAX_ROCKCHIP_GPIO_PER_BANK);
38 static void rockchip_get_recalced_mux(struct rockchip_pin_bank *bank, int pin,
39 int *reg, u8 *bit, int *mask)
41 struct rockchip_pinctrl_priv *priv = bank->priv;
42 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
43 struct rockchip_mux_recalced_data *data;
46 for (i = 0; i < ctrl->niomux_recalced; i++) {
47 data = &ctrl->iomux_recalced[i];
48 if (data->num == bank->bank_num &&
53 if (i >= ctrl->niomux_recalced)
61 static bool rockchip_get_mux_route(struct rockchip_pin_bank *bank, int pin,
62 int mux, u32 *reg, u32 *value)
64 struct rockchip_pinctrl_priv *priv = bank->priv;
65 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
66 struct rockchip_mux_route_data *data;
69 for (i = 0; i < ctrl->niomux_routes; i++) {
70 data = &ctrl->iomux_routes[i];
71 if (data->bank_num == bank->bank_num &&
72 data->pin == pin && data->func == mux)
76 if (i >= ctrl->niomux_routes)
79 *reg = data->route_offset;
80 *value = data->route_val;
85 static int rockchip_get_mux_data(int mux_type, int pin, u8 *bit, int *mask)
89 if (mux_type & IOMUX_WIDTH_4BIT) {
94 } else if (mux_type & IOMUX_WIDTH_3BIT) {
96 * pin0 ~ pin4 are at first register, and
97 * pin5 ~ pin7 are at second register.
101 *bit = (pin % 8 % 5) * 3;
104 *bit = (pin % 8) * 2;
111 static int rockchip_get_mux(struct rockchip_pin_bank *bank, int pin)
113 struct rockchip_pinctrl_priv *priv = bank->priv;
114 int iomux_num = (pin / 8);
115 struct regmap *regmap;
117 int reg, ret, mask, mux_type;
123 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
124 debug("pin %d is unrouted\n", pin);
128 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
131 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
132 ? priv->regmap_pmu : priv->regmap_base;
134 /* get basic quadrupel of mux registers and the correct reg inside */
135 mux_type = bank->iomux[iomux_num].type;
136 reg = bank->iomux[iomux_num].offset;
137 reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask);
139 if (bank->recalced_mask & BIT(pin))
140 rockchip_get_recalced_mux(bank, pin, ®, &bit, &mask);
142 ret = regmap_read(regmap, reg, &val);
146 return ((val >> bit) & mask);
149 static int rockchip_pinctrl_get_gpio_mux(struct udevice *dev, int banknum,
151 { struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
152 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
154 return rockchip_get_mux(&ctrl->pin_banks[banknum], index);
157 static int rockchip_verify_mux(struct rockchip_pin_bank *bank,
160 int iomux_num = (pin / 8);
165 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
166 debug("pin %d is unrouted\n", pin);
170 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) {
171 if (mux != IOMUX_GPIO_ONLY) {
172 debug("pin %d only supports a gpio mux\n", pin);
181 * Set a new mux function for a pin.
183 * The register is divided into the upper and lower 16 bit. When changing
184 * a value, the previous register value is not read and changed. Instead
185 * it seems the changed bits are marked in the upper 16 bit, while the
186 * changed value gets set in the same offset in the lower 16 bit.
187 * All pin settings seem to be 2 bit wide in both the upper and lower
189 * @bank: pin bank to change
190 * @pin: pin to change
191 * @mux: new mux function to set
193 static int rockchip_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
195 struct rockchip_pinctrl_priv *priv = bank->priv;
196 int iomux_num = (pin / 8);
197 struct regmap *regmap;
198 int reg, ret, mask, mux_type;
200 u32 data, route_reg, route_val;
202 ret = rockchip_verify_mux(bank, pin, mux);
206 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
209 debug("setting mux of GPIO%d-%d to %d\n", bank->bank_num, pin, mux);
211 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
212 ? priv->regmap_pmu : priv->regmap_base;
214 /* get basic quadrupel of mux registers and the correct reg inside */
215 mux_type = bank->iomux[iomux_num].type;
216 reg = bank->iomux[iomux_num].offset;
217 reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask);
219 if (bank->recalced_mask & BIT(pin))
220 rockchip_get_recalced_mux(bank, pin, ®, &bit, &mask);
222 if (bank->route_mask & BIT(pin)) {
223 if (rockchip_get_mux_route(bank, pin, mux, &route_reg,
225 ret = regmap_write(regmap, route_reg, route_val);
231 if (mux_type & IOMUX_WRITABLE_32BIT) {
232 regmap_read(regmap, reg, &data);
233 data &= ~(mask << bit);
235 data = (mask << (bit + 16));
238 data |= (mux & mask) << bit;
239 ret = regmap_write(regmap, reg, data);
244 static int rockchip_perpin_drv_list[DRV_TYPE_MAX][8] = {
245 { 2, 4, 8, 12, -1, -1, -1, -1 },
246 { 3, 6, 9, 12, -1, -1, -1, -1 },
247 { 5, 10, 15, 20, -1, -1, -1, -1 },
248 { 4, 6, 8, 10, 12, 14, 16, 18 },
249 { 4, 7, 10, 13, 16, 19, 22, 26 }
252 static int rockchip_set_drive_perpin(struct rockchip_pin_bank *bank,
253 int pin_num, int strength)
255 struct rockchip_pinctrl_priv *priv = bank->priv;
256 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
257 struct regmap *regmap;
259 u32 data, rmask_bits, temp;
261 /* Where need to clean the special mask for rockchip_perpin_drv_list */
262 int drv_type = bank->drv[pin_num / 8].drv_type & (~DRV_TYPE_IO_MASK);
264 debug("setting drive of GPIO%d-%d to %d\n", bank->bank_num,
267 ctrl->drv_calc_reg(bank, pin_num, ®map, ®, &bit);
270 for (i = 0; i < ARRAY_SIZE(rockchip_perpin_drv_list[drv_type]); i++) {
271 if (rockchip_perpin_drv_list[drv_type][i] == strength) {
274 } else if (rockchip_perpin_drv_list[drv_type][i] < 0) {
275 ret = rockchip_perpin_drv_list[drv_type][i];
281 debug("unsupported driver strength %d\n", strength);
286 case DRV_TYPE_IO_1V8_3V0_AUTO:
287 case DRV_TYPE_IO_3V3_ONLY:
288 rmask_bits = ROCKCHIP_DRV_3BITS_PER_PIN;
291 /* regular case, nothing to do */
295 * drive-strength offset is special, as it is spread
296 * over 2 registers, the bit data[15] contains bit 0
297 * of the value while temp[1:0] contains bits 2 and 1
299 data = (ret & 0x1) << 15;
300 temp = (ret >> 0x1) & 0x3;
303 ret = regmap_write(regmap, reg, data);
309 ret = regmap_write(regmap, reg, temp);
313 /* setting fully enclosed in the second register */
318 debug("unsupported bit: %d for pinctrl drive type: %d\n",
323 case DRV_TYPE_IO_DEFAULT:
324 case DRV_TYPE_IO_1V8_OR_3V0:
325 case DRV_TYPE_IO_1V8_ONLY:
326 rmask_bits = ROCKCHIP_DRV_BITS_PER_PIN;
329 debug("unsupported pinctrl drive type: %d\n",
334 if (bank->drv[pin_num / 8].drv_type & DRV_TYPE_WRITABLE_32BIT) {
335 regmap_read(regmap, reg, &data);
336 data &= ~(((1 << rmask_bits) - 1) << bit);
338 /* enable the write to the equivalent lower bits */
339 data = ((1 << rmask_bits) - 1) << (bit + 16);
342 data |= (ret << bit);
343 ret = regmap_write(regmap, reg, data);
347 static int rockchip_pull_list[PULL_TYPE_MAX][4] = {
349 PIN_CONFIG_BIAS_DISABLE,
350 PIN_CONFIG_BIAS_PULL_UP,
351 PIN_CONFIG_BIAS_PULL_DOWN,
352 PIN_CONFIG_BIAS_BUS_HOLD
355 PIN_CONFIG_BIAS_DISABLE,
356 PIN_CONFIG_BIAS_PULL_DOWN,
357 PIN_CONFIG_BIAS_DISABLE,
358 PIN_CONFIG_BIAS_PULL_UP
362 static int rockchip_set_pull(struct rockchip_pin_bank *bank,
363 int pin_num, int pull)
365 struct rockchip_pinctrl_priv *priv = bank->priv;
366 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
367 struct regmap *regmap;
368 int reg, ret, i, pull_type;
372 debug("setting pull of GPIO%d-%d to %d\n", bank->bank_num,
375 ctrl->pull_calc_reg(bank, pin_num, ®map, ®, &bit);
377 switch (ctrl->type) {
380 data = BIT(bit + 16);
381 if (pull == PIN_CONFIG_BIAS_DISABLE)
383 ret = regmap_write(regmap, reg, data);
391 * Where need to clean the special mask for
392 * rockchip_pull_list.
394 pull_type = bank->pull_type[pin_num / 8] & (~PULL_TYPE_IO_MASK);
396 for (i = 0; i < ARRAY_SIZE(rockchip_pull_list[pull_type]);
398 if (rockchip_pull_list[pull_type][i] == pull) {
405 debug("unsupported pull setting %d\n", pull);
409 if (bank->pull_type[pin_num / 8] & PULL_TYPE_WRITABLE_32BIT) {
410 regmap_read(regmap, reg, &data);
411 data &= ~(((1 << ROCKCHIP_PULL_BITS_PER_PIN) - 1) << bit);
413 /* enable the write to the equivalent lower bits */
414 data = ((1 << ROCKCHIP_PULL_BITS_PER_PIN) - 1) << (bit + 16);
417 data |= (ret << bit);
418 ret = regmap_write(regmap, reg, data);
421 debug("unsupported pinctrl type\n");
428 static int rockchip_set_schmitt(struct rockchip_pin_bank *bank,
429 int pin_num, int enable)
431 struct rockchip_pinctrl_priv *priv = bank->priv;
432 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
433 struct regmap *regmap;
438 debug("setting input schmitt of GPIO%d-%d to %d\n", bank->bank_num,
441 ret = ctrl->schmitt_calc_reg(bank, pin_num, ®map, ®, &bit);
445 /* enable the write to the equivalent lower bits */
446 data = BIT(bit + 16) | (enable << bit);
448 return regmap_write(regmap, reg, data);
452 * Pinconf_ops handling
454 static bool rockchip_pinconf_pull_valid(struct rockchip_pin_ctrl *ctrl,
457 switch (ctrl->type) {
460 return (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT ||
461 pull == PIN_CONFIG_BIAS_DISABLE);
467 return (pull != PIN_CONFIG_BIAS_PULL_PIN_DEFAULT);
473 /* set the pin config settings for a specified pin */
474 static int rockchip_pinconf_set(struct rockchip_pin_bank *bank,
475 u32 pin, u32 param, u32 arg)
477 struct rockchip_pinctrl_priv *priv = bank->priv;
478 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
482 case PIN_CONFIG_BIAS_DISABLE:
483 rc = rockchip_set_pull(bank, pin, param);
488 case PIN_CONFIG_BIAS_PULL_UP:
489 case PIN_CONFIG_BIAS_PULL_DOWN:
490 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
491 case PIN_CONFIG_BIAS_BUS_HOLD:
492 if (!rockchip_pinconf_pull_valid(ctrl, param))
498 rc = rockchip_set_pull(bank, pin, param);
503 case PIN_CONFIG_DRIVE_STRENGTH:
504 if (!ctrl->drv_calc_reg)
507 rc = rockchip_set_drive_perpin(bank, pin, arg);
512 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
513 if (!ctrl->schmitt_calc_reg)
516 rc = rockchip_set_schmitt(bank, pin, arg);
528 static const struct pinconf_param rockchip_conf_params[] = {
529 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
530 { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
531 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
532 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
533 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
534 { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
535 { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
536 { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 },
537 { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 },
540 static int rockchip_pinconf_prop_name_to_param(const char *property,
543 const struct pinconf_param *p, *end;
545 p = rockchip_conf_params;
546 end = p + sizeof(rockchip_conf_params) / sizeof(struct pinconf_param);
548 /* See if this pctldev supports this parameter */
549 for (; p < end; p++) {
550 if (!strcmp(property, p->property)) {
551 *default_value = p->default_value;
560 static int rockchip_pinctrl_set_state(struct udevice *dev,
561 struct udevice *config)
563 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
564 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
565 u32 cells[MAX_ROCKCHIP_PINS_ENTRIES * 4];
566 u32 bank, pin, mux, conf, arg, default_val;
568 const char *prop_name;
573 #ifdef CONFIG_OF_LIVE
574 const struct device_node *np;
577 int property_offset, pcfg_node;
578 const void *blob = gd->fdt_blob;
580 data = dev_read_prop(config, "rockchip,pins", &count);
582 debug("%s: bad array size %d\n", __func__, count);
586 count /= sizeof(u32);
587 if (count > MAX_ROCKCHIP_PINS_ENTRIES * 4) {
588 debug("%s: unsupported pins array count %d\n",
593 for (i = 0; i < count; i++)
594 cells[i] = fdt32_to_cpu(data[i]);
596 for (i = 0; i < (count >> 2); i++) {
597 bank = cells[4 * i + 0];
598 pin = cells[4 * i + 1];
599 mux = cells[4 * i + 2];
600 conf = cells[4 * i + 3];
602 ret = rockchip_verify_config(dev, bank, pin);
606 ret = rockchip_set_mux(&ctrl->pin_banks[bank], pin, mux);
610 node = ofnode_get_by_phandle(conf);
611 if (!ofnode_valid(node))
613 #ifdef CONFIG_OF_LIVE
614 np = ofnode_to_np(node);
615 for (pp = np->properties; pp; pp = pp->next) {
616 prop_name = pp->name;
617 prop_len = pp->length;
620 pcfg_node = ofnode_to_offset(node);
621 fdt_for_each_property_offset(property_offset, blob, pcfg_node) {
622 value = fdt_getprop_by_offset(blob, property_offset,
623 &prop_name, &prop_len);
627 param = rockchip_pinconf_prop_name_to_param(prop_name,
632 if (prop_len >= sizeof(fdt32_t))
633 arg = fdt32_to_cpu(*(fdt32_t *)value);
637 ret = rockchip_pinconf_set(&ctrl->pin_banks[bank], pin,
640 debug("%s: rockchip_pinconf_set fail: %d\n",
650 const struct pinctrl_ops rockchip_pinctrl_ops = {
651 .set_state = rockchip_pinctrl_set_state,
652 .get_gpio_mux = rockchip_pinctrl_get_gpio_mux,
655 /* retrieve the soc specific data */
656 static struct rockchip_pin_ctrl *rockchip_pinctrl_get_soc_data(struct udevice *dev)
658 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
659 struct rockchip_pin_ctrl *ctrl =
660 (struct rockchip_pin_ctrl *)dev_get_driver_data(dev);
661 struct rockchip_pin_bank *bank;
662 int grf_offs, pmu_offs, drv_grf_offs, drv_pmu_offs, i, j;
664 grf_offs = ctrl->grf_mux_offset;
665 pmu_offs = ctrl->pmu_mux_offset;
666 drv_pmu_offs = ctrl->pmu_drv_offset;
667 drv_grf_offs = ctrl->grf_drv_offset;
668 bank = ctrl->pin_banks;
670 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
674 bank->pin_base = ctrl->nr_pins;
675 ctrl->nr_pins += bank->nr_pins;
677 /* calculate iomux and drv offsets */
678 for (j = 0; j < 4; j++) {
679 struct rockchip_iomux *iom = &bank->iomux[j];
680 struct rockchip_drv *drv = &bank->drv[j];
683 if (bank_pins >= bank->nr_pins)
686 /* preset iomux offset value, set new start value */
687 if (iom->offset >= 0) {
688 if (iom->type & IOMUX_SOURCE_PMU)
689 pmu_offs = iom->offset;
691 grf_offs = iom->offset;
692 } else { /* set current iomux offset */
693 iom->offset = (iom->type & IOMUX_SOURCE_PMU) ?
697 /* preset drv offset value, set new start value */
698 if (drv->offset >= 0) {
699 if (iom->type & IOMUX_SOURCE_PMU)
700 drv_pmu_offs = drv->offset;
702 drv_grf_offs = drv->offset;
703 } else { /* set current drv offset */
704 drv->offset = (iom->type & IOMUX_SOURCE_PMU) ?
705 drv_pmu_offs : drv_grf_offs;
708 debug("bank %d, iomux %d has iom_offset 0x%x drv_offset 0x%x\n",
709 i, j, iom->offset, drv->offset);
712 * Increase offset according to iomux width.
713 * 4bit iomux'es are spread over two registers.
715 inc = (iom->type & (IOMUX_WIDTH_4BIT |
716 IOMUX_WIDTH_3BIT)) ? 8 : 4;
717 if (iom->type & IOMUX_SOURCE_PMU)
723 * Increase offset according to drv width.
724 * 3bit drive-strenth'es are spread over two registers.
726 if ((drv->drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) ||
727 (drv->drv_type == DRV_TYPE_IO_3V3_ONLY))
732 if (iom->type & IOMUX_SOURCE_PMU)
740 /* calculate the per-bank recalced_mask */
741 for (j = 0; j < ctrl->niomux_recalced; j++) {
744 if (ctrl->iomux_recalced[j].num == bank->bank_num) {
745 pin = ctrl->iomux_recalced[j].pin;
746 bank->recalced_mask |= BIT(pin);
750 /* calculate the per-bank route_mask */
751 for (j = 0; j < ctrl->niomux_routes; j++) {
754 if (ctrl->iomux_routes[j].bank_num == bank->bank_num) {
755 pin = ctrl->iomux_routes[j].pin;
756 bank->route_mask |= BIT(pin);
764 int rockchip_pinctrl_probe(struct udevice *dev)
766 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
767 struct rockchip_pin_ctrl *ctrl;
768 struct udevice *syscon;
769 struct regmap *regmap;
772 /* get rockchip grf syscon phandle */
773 ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, "rockchip,grf",
776 debug("unable to find rockchip,grf syscon device (%d)\n", ret);
780 /* get grf-reg base address */
781 regmap = syscon_get_regmap(syscon);
783 debug("unable to find rockchip grf regmap\n");
786 priv->regmap_base = regmap;
788 /* option: get pmu-reg base address */
789 ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, "rockchip,pmu",
792 /* get pmugrf-reg base address */
793 regmap = syscon_get_regmap(syscon);
795 debug("unable to find rockchip pmu regmap\n");
798 priv->regmap_pmu = regmap;
801 ctrl = rockchip_pinctrl_get_soc_data(dev);
803 debug("driver data not available\n");