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>
16 #include "pinctrl-rockchip.h"
18 #define MAX_ROCKCHIP_PINS_ENTRIES 30
19 #define MAX_ROCKCHIP_GPIO_PER_BANK 32
20 #define RK_FUNC_GPIO 0
22 static int rockchip_verify_config(struct udevice *dev, u32 bank, u32 pin)
24 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
25 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
27 if (bank >= ctrl->nr_banks) {
28 debug("pin conf bank %d >= nbanks %d\n", bank, ctrl->nr_banks);
32 if (pin >= MAX_ROCKCHIP_GPIO_PER_BANK) {
33 debug("pin conf pin %d >= %d\n", pin,
34 MAX_ROCKCHIP_GPIO_PER_BANK);
41 void rockchip_get_recalced_mux(struct rockchip_pin_bank *bank, int pin,
42 int *reg, u8 *bit, int *mask)
44 struct rockchip_pinctrl_priv *priv = bank->priv;
45 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
46 struct rockchip_mux_recalced_data *data;
49 for (i = 0; i < ctrl->niomux_recalced; i++) {
50 data = &ctrl->iomux_recalced[i];
51 if (data->num == bank->bank_num &&
56 if (i >= ctrl->niomux_recalced)
64 static enum rockchip_pin_route_type
65 rockchip_get_mux_route(struct rockchip_pin_bank *bank, int pin,
66 int mux, u32 *reg, u32 *value)
68 struct rockchip_pinctrl_priv *priv = bank->priv;
69 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
70 struct rockchip_mux_route_data *data;
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)
80 if (i >= ctrl->niomux_routes)
81 return ROUTE_TYPE_INVALID;
83 *reg = data->route_offset;
84 *value = data->route_val;
86 return data->route_type;
89 int rockchip_get_mux_data(int mux_type, int pin, u8 *bit, int *mask)
93 if (mux_type & IOMUX_WIDTH_4BIT) {
98 } else if (mux_type & IOMUX_WIDTH_3BIT) {
100 * pin0 ~ pin4 are at first register, and
101 * pin5 ~ pin7 are at second register.
105 *bit = (pin % 8 % 5) * 3;
108 *bit = (pin % 8) * 2;
115 static int rockchip_get_mux(struct rockchip_pin_bank *bank, int pin)
117 struct rockchip_pinctrl_priv *priv = bank->priv;
118 int iomux_num = (pin / 8);
119 struct regmap *regmap;
121 int reg, ret, mask, mux_type;
127 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
128 debug("pin %d is unrouted\n", pin);
132 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
135 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
136 ? priv->regmap_pmu : priv->regmap_base;
138 /* get basic quadrupel of mux registers and the correct reg inside */
139 mux_type = bank->iomux[iomux_num].type;
140 reg = bank->iomux[iomux_num].offset;
141 reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask);
143 if (bank->recalced_mask & BIT(pin))
144 rockchip_get_recalced_mux(bank, pin, ®, &bit, &mask);
146 ret = regmap_read(regmap, reg, &val);
150 return ((val >> bit) & mask);
153 static int rockchip_pinctrl_get_gpio_mux(struct udevice *dev, int banknum,
155 { struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
156 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
158 return rockchip_get_mux(&ctrl->pin_banks[banknum], index);
161 static int rockchip_verify_mux(struct rockchip_pin_bank *bank,
164 int iomux_num = (pin / 8);
169 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
170 debug("pin %d is unrouted\n", pin);
174 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) {
175 if (mux != IOMUX_GPIO_ONLY) {
176 debug("pin %d only supports a gpio mux\n", pin);
185 * Set a new mux function for a pin.
187 * The register is divided into the upper and lower 16 bit. When changing
188 * a value, the previous register value is not read and changed. Instead
189 * it seems the changed bits are marked in the upper 16 bit, while the
190 * changed value gets set in the same offset in the lower 16 bit.
191 * All pin settings seem to be 2 bit wide in both the upper and lower
193 * @bank: pin bank to change
194 * @pin: pin to change
195 * @mux: new mux function to set
197 static int rockchip_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
199 struct rockchip_pinctrl_priv *priv = bank->priv;
200 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
201 int iomux_num = (pin / 8);
204 ret = rockchip_verify_mux(bank, pin, mux);
208 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
211 debug("setting mux of GPIO%d-%d to %d\n", bank->bank_num, pin, mux);
216 ret = ctrl->set_mux(bank, pin, mux);
220 if (bank->route_mask & BIT(pin)) {
221 struct regmap *regmap;
222 u32 route_reg = 0, route_val = 0;
224 ret = rockchip_get_mux_route(bank, pin, mux,
225 &route_reg, &route_val);
227 case ROUTE_TYPE_DEFAULT:
228 if (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
229 regmap = priv->regmap_pmu;
230 else if (bank->iomux[iomux_num].type & IOMUX_L_SOURCE_PMU)
231 regmap = (pin % 8 < 4) ? priv->regmap_pmu : priv->regmap_base;
233 regmap = priv->regmap_base;
235 regmap_write(regmap, route_reg, route_val);
237 case ROUTE_TYPE_TOPGRF:
238 regmap_write(priv->regmap_base, route_reg, route_val);
240 case ROUTE_TYPE_PMUGRF:
241 regmap_write(priv->regmap_pmu, route_reg, route_val);
243 case ROUTE_TYPE_INVALID:
253 static int rockchip_perpin_drv_list[DRV_TYPE_MAX][8] = {
254 { 2, 4, 8, 12, -1, -1, -1, -1 },
255 { 3, 6, 9, 12, -1, -1, -1, -1 },
256 { 5, 10, 15, 20, -1, -1, -1, -1 },
257 { 4, 6, 8, 10, 12, 14, 16, 18 },
258 { 4, 7, 10, 13, 16, 19, 22, 26 }
261 int rockchip_translate_drive_value(int type, int strength)
266 for (i = 0; i < ARRAY_SIZE(rockchip_perpin_drv_list[type]); i++) {
267 if (rockchip_perpin_drv_list[type][i] == strength) {
270 } else if (rockchip_perpin_drv_list[type][i] < 0) {
271 ret = rockchip_perpin_drv_list[type][i];
279 static int rockchip_set_drive_perpin(struct rockchip_pin_bank *bank,
280 int pin_num, int strength)
282 struct rockchip_pinctrl_priv *priv = bank->priv;
283 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
285 debug("setting drive of GPIO%d-%d to %d\n", bank->bank_num,
288 if (!ctrl->set_drive)
291 return ctrl->set_drive(bank, pin_num, strength);
294 static int rockchip_pull_list[PULL_TYPE_MAX][4] = {
296 PIN_CONFIG_BIAS_DISABLE,
297 PIN_CONFIG_BIAS_PULL_UP,
298 PIN_CONFIG_BIAS_PULL_DOWN,
299 PIN_CONFIG_BIAS_BUS_HOLD
302 PIN_CONFIG_BIAS_DISABLE,
303 PIN_CONFIG_BIAS_PULL_DOWN,
304 PIN_CONFIG_BIAS_DISABLE,
305 PIN_CONFIG_BIAS_PULL_UP
309 int rockchip_translate_pull_value(int type, int pull)
314 for (i = 0; i < ARRAY_SIZE(rockchip_pull_list[type]);
316 if (rockchip_pull_list[type][i] == pull) {
325 static int rockchip_set_pull(struct rockchip_pin_bank *bank,
326 int pin_num, int pull)
328 struct rockchip_pinctrl_priv *priv = bank->priv;
329 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
331 debug("setting pull of GPIO%d-%d to %d\n", bank->bank_num,
337 return ctrl->set_pull(bank, pin_num, pull);
340 static int rockchip_set_schmitt(struct rockchip_pin_bank *bank,
341 int pin_num, int enable)
343 struct rockchip_pinctrl_priv *priv = bank->priv;
344 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
346 debug("setting input schmitt of GPIO%d-%d to %d\n", bank->bank_num,
349 if (!ctrl->set_schmitt)
352 return ctrl->set_schmitt(bank, pin_num, enable);
355 /* set the pin config settings for a specified pin */
356 static int rockchip_pinconf_set(struct rockchip_pin_bank *bank,
357 u32 pin, u32 param, u32 arg)
362 case PIN_CONFIG_BIAS_DISABLE:
363 case PIN_CONFIG_BIAS_PULL_UP:
364 case PIN_CONFIG_BIAS_PULL_DOWN:
365 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
366 case PIN_CONFIG_BIAS_BUS_HOLD:
367 rc = rockchip_set_pull(bank, pin, param);
372 case PIN_CONFIG_DRIVE_STRENGTH:
373 rc = rockchip_set_drive_perpin(bank, pin, arg);
378 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
379 rc = rockchip_set_schmitt(bank, pin, arg);
391 static const struct pinconf_param rockchip_conf_params[] = {
392 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
393 { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
394 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
395 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
396 { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
397 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
398 { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 },
399 { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 },
402 static int rockchip_pinconf_prop_name_to_param(const char *property,
405 const struct pinconf_param *p, *end;
407 p = rockchip_conf_params;
408 end = p + sizeof(rockchip_conf_params) / sizeof(struct pinconf_param);
410 /* See if this pctldev supports this parameter */
411 for (; p < end; p++) {
412 if (!strcmp(property, p->property)) {
413 *default_value = p->default_value;
422 static int rockchip_pinctrl_set_state(struct udevice *dev,
423 struct udevice *config)
425 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
426 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
427 u32 cells[MAX_ROCKCHIP_PINS_ENTRIES * 4];
428 u32 bank, pin, mux, conf, arg, default_val;
430 const char *prop_name;
436 data = dev_read_prop(config, "rockchip,pins", &count);
438 debug("%s: bad array size %d\n", __func__, count);
442 count /= sizeof(u32);
443 if (count > MAX_ROCKCHIP_PINS_ENTRIES * 4) {
444 debug("%s: unsupported pins array count %d\n",
449 for (i = 0; i < count; i++)
450 cells[i] = fdt32_to_cpu(data[i]);
452 for (i = 0; i < (count >> 2); i++) {
453 bank = cells[4 * i + 0];
454 pin = cells[4 * i + 1];
455 mux = cells[4 * i + 2];
456 conf = cells[4 * i + 3];
458 ret = rockchip_verify_config(dev, bank, pin);
462 ret = rockchip_set_mux(&ctrl->pin_banks[bank], pin, mux);
466 node = ofnode_get_by_phandle(conf);
467 if (!ofnode_valid(node))
469 ofnode_for_each_prop(prop, node) {
470 value = ofprop_get_property(&prop, &prop_name, &prop_len);
474 param = rockchip_pinconf_prop_name_to_param(prop_name,
479 if (prop_len >= sizeof(fdt32_t))
480 arg = fdt32_to_cpu(*(fdt32_t *)value);
484 ret = rockchip_pinconf_set(&ctrl->pin_banks[bank], pin,
487 debug("%s: rockchip_pinconf_set fail: %d\n",
497 const struct pinctrl_ops rockchip_pinctrl_ops = {
498 .set_state = rockchip_pinctrl_set_state,
499 .get_gpio_mux = rockchip_pinctrl_get_gpio_mux,
502 /* retrieve the soc specific data */
503 static struct rockchip_pin_ctrl *rockchip_pinctrl_get_soc_data(struct udevice *dev)
505 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
506 struct rockchip_pin_ctrl *ctrl =
507 (struct rockchip_pin_ctrl *)dev_get_driver_data(dev);
508 struct rockchip_pin_bank *bank;
509 int grf_offs, pmu_offs, drv_grf_offs, drv_pmu_offs, i, j;
511 grf_offs = ctrl->grf_mux_offset;
512 pmu_offs = ctrl->pmu_mux_offset;
513 drv_pmu_offs = ctrl->pmu_drv_offset;
514 drv_grf_offs = ctrl->grf_drv_offset;
515 bank = ctrl->pin_banks;
517 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
521 bank->pin_base = ctrl->nr_pins;
522 ctrl->nr_pins += bank->nr_pins;
524 /* calculate iomux and drv offsets */
525 for (j = 0; j < 4; j++) {
526 struct rockchip_iomux *iom = &bank->iomux[j];
527 struct rockchip_drv *drv = &bank->drv[j];
530 if (bank_pins >= bank->nr_pins)
533 /* preset iomux offset value, set new start value */
534 if (iom->offset >= 0) {
535 if (iom->type & IOMUX_SOURCE_PMU)
536 pmu_offs = iom->offset;
538 grf_offs = iom->offset;
539 } else { /* set current iomux offset */
540 iom->offset = (iom->type & IOMUX_SOURCE_PMU) ?
544 /* preset drv offset value, set new start value */
545 if (drv->offset >= 0) {
546 if (iom->type & IOMUX_SOURCE_PMU)
547 drv_pmu_offs = drv->offset;
549 drv_grf_offs = drv->offset;
550 } else { /* set current drv offset */
551 drv->offset = (iom->type & IOMUX_SOURCE_PMU) ?
552 drv_pmu_offs : drv_grf_offs;
555 debug("bank %d, iomux %d has iom_offset 0x%x drv_offset 0x%x\n",
556 i, j, iom->offset, drv->offset);
559 * Increase offset according to iomux width.
560 * 4bit iomux'es are spread over two registers.
562 inc = (iom->type & (IOMUX_WIDTH_4BIT |
564 IOMUX_8WIDTH_2BIT)) ? 8 : 4;
565 if ((iom->type & IOMUX_SOURCE_PMU) || (iom->type & IOMUX_L_SOURCE_PMU))
571 * Increase offset according to drv width.
572 * 3bit drive-strenth'es are spread over two registers.
574 if ((drv->drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) ||
575 (drv->drv_type == DRV_TYPE_IO_3V3_ONLY))
580 if (iom->type & IOMUX_SOURCE_PMU)
588 /* calculate the per-bank recalced_mask */
589 for (j = 0; j < ctrl->niomux_recalced; j++) {
592 if (ctrl->iomux_recalced[j].num == bank->bank_num) {
593 pin = ctrl->iomux_recalced[j].pin;
594 bank->recalced_mask |= BIT(pin);
598 /* calculate the per-bank route_mask */
599 for (j = 0; j < ctrl->niomux_routes; j++) {
602 if (ctrl->iomux_routes[j].bank_num == bank->bank_num) {
603 pin = ctrl->iomux_routes[j].pin;
604 bank->route_mask |= BIT(pin);
612 int rockchip_pinctrl_probe(struct udevice *dev)
614 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
615 struct rockchip_pin_ctrl *ctrl;
616 struct udevice *syscon;
617 struct regmap *regmap;
620 /* get rockchip grf syscon phandle */
621 ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, "rockchip,grf",
624 debug("unable to find rockchip,grf syscon device (%d)\n", ret);
628 /* get grf-reg base address */
629 regmap = syscon_get_regmap(syscon);
631 debug("unable to find rockchip grf regmap\n");
634 priv->regmap_base = regmap;
636 /* option: get pmu-reg base address */
637 ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, "rockchip,pmu",
640 /* get pmugrf-reg base address */
641 regmap = syscon_get_regmap(syscon);
643 debug("unable to find rockchip pmu regmap\n");
646 priv->regmap_pmu = regmap;
649 ctrl = rockchip_pinctrl_get_soc_data(dev);
651 debug("driver data not available\n");