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 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 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 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 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
197 int iomux_num = (pin / 8);
200 ret = rockchip_verify_mux(bank, pin, mux);
204 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
207 debug("setting mux of GPIO%d-%d to %d\n", bank->bank_num, pin, mux);
212 ret = ctrl->set_mux(bank, pin, mux);
217 static int rockchip_perpin_drv_list[DRV_TYPE_MAX][8] = {
218 { 2, 4, 8, 12, -1, -1, -1, -1 },
219 { 3, 6, 9, 12, -1, -1, -1, -1 },
220 { 5, 10, 15, 20, -1, -1, -1, -1 },
221 { 4, 6, 8, 10, 12, 14, 16, 18 },
222 { 4, 7, 10, 13, 16, 19, 22, 26 }
225 int rockchip_translate_drive_value(int type, int strength)
230 for (i = 0; i < ARRAY_SIZE(rockchip_perpin_drv_list[type]); i++) {
231 if (rockchip_perpin_drv_list[type][i] == strength) {
234 } else if (rockchip_perpin_drv_list[type][i] < 0) {
235 ret = rockchip_perpin_drv_list[type][i];
243 static int rockchip_set_drive_perpin(struct rockchip_pin_bank *bank,
244 int pin_num, int strength)
246 struct rockchip_pinctrl_priv *priv = bank->priv;
247 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
249 debug("setting drive of GPIO%d-%d to %d\n", bank->bank_num,
252 if (!ctrl->set_drive)
255 return ctrl->set_drive(bank, pin_num, strength);
258 static int rockchip_pull_list[PULL_TYPE_MAX][4] = {
260 PIN_CONFIG_BIAS_DISABLE,
261 PIN_CONFIG_BIAS_PULL_UP,
262 PIN_CONFIG_BIAS_PULL_DOWN,
263 PIN_CONFIG_BIAS_BUS_HOLD
266 PIN_CONFIG_BIAS_DISABLE,
267 PIN_CONFIG_BIAS_PULL_DOWN,
268 PIN_CONFIG_BIAS_DISABLE,
269 PIN_CONFIG_BIAS_PULL_UP
273 int rockchip_translate_pull_value(int type, int pull)
278 for (i = 0; i < ARRAY_SIZE(rockchip_pull_list[type]);
280 if (rockchip_pull_list[type][i] == pull) {
289 static int rockchip_set_pull(struct rockchip_pin_bank *bank,
290 int pin_num, int pull)
292 struct rockchip_pinctrl_priv *priv = bank->priv;
293 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
295 debug("setting pull of GPIO%d-%d to %d\n", bank->bank_num,
301 return ctrl->set_pull(bank, pin_num, pull);
304 static int rockchip_set_schmitt(struct rockchip_pin_bank *bank,
305 int pin_num, int enable)
307 struct rockchip_pinctrl_priv *priv = bank->priv;
308 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
310 debug("setting input schmitt of GPIO%d-%d to %d\n", bank->bank_num,
313 if (!ctrl->set_schmitt)
316 return ctrl->set_schmitt(bank, pin_num, enable);
319 /* set the pin config settings for a specified pin */
320 static int rockchip_pinconf_set(struct rockchip_pin_bank *bank,
321 u32 pin, u32 param, u32 arg)
326 case PIN_CONFIG_BIAS_DISABLE:
327 case PIN_CONFIG_BIAS_PULL_UP:
328 case PIN_CONFIG_BIAS_PULL_DOWN:
329 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
330 case PIN_CONFIG_BIAS_BUS_HOLD:
331 rc = rockchip_set_pull(bank, pin, param);
336 case PIN_CONFIG_DRIVE_STRENGTH:
337 rc = rockchip_set_drive_perpin(bank, pin, arg);
342 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
343 rc = rockchip_set_schmitt(bank, pin, arg);
355 static const struct pinconf_param rockchip_conf_params[] = {
356 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
357 { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
358 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
359 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
360 { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
361 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
362 { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 },
363 { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 },
366 static int rockchip_pinconf_prop_name_to_param(const char *property,
369 const struct pinconf_param *p, *end;
371 p = rockchip_conf_params;
372 end = p + sizeof(rockchip_conf_params) / sizeof(struct pinconf_param);
374 /* See if this pctldev supports this parameter */
375 for (; p < end; p++) {
376 if (!strcmp(property, p->property)) {
377 *default_value = p->default_value;
386 static int rockchip_pinctrl_set_state(struct udevice *dev,
387 struct udevice *config)
389 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
390 struct rockchip_pin_ctrl *ctrl = priv->ctrl;
391 u32 cells[MAX_ROCKCHIP_PINS_ENTRIES * 4];
392 u32 bank, pin, mux, conf, arg, default_val;
394 const char *prop_name;
399 #ifdef CONFIG_OF_LIVE
400 const struct device_node *np;
403 int property_offset, pcfg_node;
404 const void *blob = gd->fdt_blob;
406 data = dev_read_prop(config, "rockchip,pins", &count);
408 debug("%s: bad array size %d\n", __func__, count);
412 count /= sizeof(u32);
413 if (count > MAX_ROCKCHIP_PINS_ENTRIES * 4) {
414 debug("%s: unsupported pins array count %d\n",
419 for (i = 0; i < count; i++)
420 cells[i] = fdt32_to_cpu(data[i]);
422 for (i = 0; i < (count >> 2); i++) {
423 bank = cells[4 * i + 0];
424 pin = cells[4 * i + 1];
425 mux = cells[4 * i + 2];
426 conf = cells[4 * i + 3];
428 ret = rockchip_verify_config(dev, bank, pin);
432 ret = rockchip_set_mux(&ctrl->pin_banks[bank], pin, mux);
436 node = ofnode_get_by_phandle(conf);
437 if (!ofnode_valid(node))
439 #ifdef CONFIG_OF_LIVE
440 np = ofnode_to_np(node);
441 for (pp = np->properties; pp; pp = pp->next) {
442 prop_name = pp->name;
443 prop_len = pp->length;
446 pcfg_node = ofnode_to_offset(node);
447 fdt_for_each_property_offset(property_offset, blob, pcfg_node) {
448 value = fdt_getprop_by_offset(blob, property_offset,
449 &prop_name, &prop_len);
453 param = rockchip_pinconf_prop_name_to_param(prop_name,
458 if (prop_len >= sizeof(fdt32_t))
459 arg = fdt32_to_cpu(*(fdt32_t *)value);
463 ret = rockchip_pinconf_set(&ctrl->pin_banks[bank], pin,
466 debug("%s: rockchip_pinconf_set fail: %d\n",
476 const struct pinctrl_ops rockchip_pinctrl_ops = {
477 .set_state = rockchip_pinctrl_set_state,
478 .get_gpio_mux = rockchip_pinctrl_get_gpio_mux,
481 /* retrieve the soc specific data */
482 static struct rockchip_pin_ctrl *rockchip_pinctrl_get_soc_data(struct udevice *dev)
484 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
485 struct rockchip_pin_ctrl *ctrl =
486 (struct rockchip_pin_ctrl *)dev_get_driver_data(dev);
487 struct rockchip_pin_bank *bank;
488 int grf_offs, pmu_offs, drv_grf_offs, drv_pmu_offs, i, j;
490 grf_offs = ctrl->grf_mux_offset;
491 pmu_offs = ctrl->pmu_mux_offset;
492 drv_pmu_offs = ctrl->pmu_drv_offset;
493 drv_grf_offs = ctrl->grf_drv_offset;
494 bank = ctrl->pin_banks;
496 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
500 bank->pin_base = ctrl->nr_pins;
501 ctrl->nr_pins += bank->nr_pins;
503 /* calculate iomux and drv offsets */
504 for (j = 0; j < 4; j++) {
505 struct rockchip_iomux *iom = &bank->iomux[j];
506 struct rockchip_drv *drv = &bank->drv[j];
509 if (bank_pins >= bank->nr_pins)
512 /* preset iomux offset value, set new start value */
513 if (iom->offset >= 0) {
514 if (iom->type & IOMUX_SOURCE_PMU)
515 pmu_offs = iom->offset;
517 grf_offs = iom->offset;
518 } else { /* set current iomux offset */
519 iom->offset = (iom->type & IOMUX_SOURCE_PMU) ?
523 /* preset drv offset value, set new start value */
524 if (drv->offset >= 0) {
525 if (iom->type & IOMUX_SOURCE_PMU)
526 drv_pmu_offs = drv->offset;
528 drv_grf_offs = drv->offset;
529 } else { /* set current drv offset */
530 drv->offset = (iom->type & IOMUX_SOURCE_PMU) ?
531 drv_pmu_offs : drv_grf_offs;
534 debug("bank %d, iomux %d has iom_offset 0x%x drv_offset 0x%x\n",
535 i, j, iom->offset, drv->offset);
538 * Increase offset according to iomux width.
539 * 4bit iomux'es are spread over two registers.
541 inc = (iom->type & (IOMUX_WIDTH_4BIT |
542 IOMUX_WIDTH_3BIT)) ? 8 : 4;
543 if (iom->type & IOMUX_SOURCE_PMU)
549 * Increase offset according to drv width.
550 * 3bit drive-strenth'es are spread over two registers.
552 if ((drv->drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) ||
553 (drv->drv_type == DRV_TYPE_IO_3V3_ONLY))
558 if (iom->type & IOMUX_SOURCE_PMU)
566 /* calculate the per-bank recalced_mask */
567 for (j = 0; j < ctrl->niomux_recalced; j++) {
570 if (ctrl->iomux_recalced[j].num == bank->bank_num) {
571 pin = ctrl->iomux_recalced[j].pin;
572 bank->recalced_mask |= BIT(pin);
576 /* calculate the per-bank route_mask */
577 for (j = 0; j < ctrl->niomux_routes; j++) {
580 if (ctrl->iomux_routes[j].bank_num == bank->bank_num) {
581 pin = ctrl->iomux_routes[j].pin;
582 bank->route_mask |= BIT(pin);
590 int rockchip_pinctrl_probe(struct udevice *dev)
592 struct rockchip_pinctrl_priv *priv = dev_get_priv(dev);
593 struct rockchip_pin_ctrl *ctrl;
594 struct udevice *syscon;
595 struct regmap *regmap;
598 /* get rockchip grf syscon phandle */
599 ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, "rockchip,grf",
602 debug("unable to find rockchip,grf syscon device (%d)\n", ret);
606 /* get grf-reg base address */
607 regmap = syscon_get_regmap(syscon);
609 debug("unable to find rockchip grf regmap\n");
612 priv->regmap_base = regmap;
614 /* option: get pmu-reg base address */
615 ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, "rockchip,pmu",
618 /* get pmugrf-reg base address */
619 regmap = syscon_get_regmap(syscon);
621 debug("unable to find rockchip pmu regmap\n");
624 priv->regmap_pmu = regmap;
627 ctrl = rockchip_pinctrl_get_soc_data(dev);
629 debug("driver data not available\n");