2 * (C) Copyright 2015 Google, Inc
4 * (C) Copyright 2008-2014 Rockchip Electronics
5 * Peter, Software Engineering, <superpeter.cai@gmail.com>.
7 * SPDX-License-Identifier: GPL-2.0+
13 #include <linux/errno.h>
16 #include <asm/arch/clock.h>
17 #include <dm/pinctrl.h>
18 #include <dt-bindings/clock/rk3288-cru.h>
21 ROCKCHIP_GPIOS_PER_BANK = 32,
24 #define OFFSET_TO_BIT(bit) (1UL << (bit))
26 struct rockchip_gpio_priv {
27 struct rockchip_gpio_regs *regs;
28 struct udevice *pinctrl;
33 static int rockchip_gpio_direction_input(struct udevice *dev, unsigned offset)
35 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
36 struct rockchip_gpio_regs *regs = priv->regs;
38 clrbits_le32(®s->swport_ddr, OFFSET_TO_BIT(offset));
43 static int rockchip_gpio_direction_output(struct udevice *dev, unsigned offset,
46 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
47 struct rockchip_gpio_regs *regs = priv->regs;
48 int mask = OFFSET_TO_BIT(offset);
50 clrsetbits_le32(®s->swport_dr, mask, value ? mask : 0);
51 setbits_le32(®s->swport_ddr, mask);
56 static int rockchip_gpio_get_value(struct udevice *dev, unsigned offset)
58 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
59 struct rockchip_gpio_regs *regs = priv->regs;
61 return readl(®s->ext_port) & OFFSET_TO_BIT(offset) ? 1 : 0;
64 static int rockchip_gpio_set_value(struct udevice *dev, unsigned offset,
67 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
68 struct rockchip_gpio_regs *regs = priv->regs;
69 int mask = OFFSET_TO_BIT(offset);
71 clrsetbits_le32(®s->swport_dr, mask, value ? mask : 0);
76 static int rockchip_gpio_get_function(struct udevice *dev, unsigned offset)
78 #ifdef CONFIG_SPL_BUILD
81 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
82 struct rockchip_gpio_regs *regs = priv->regs;
86 ret = pinctrl_get_gpio_mux(priv->pinctrl, priv->bank, offset);
90 /* If it's not 0, then it is not a GPIO */
93 is_output = readl(®s->swport_ddr) & OFFSET_TO_BIT(offset);
95 return is_output ? GPIOF_OUTPUT : GPIOF_INPUT;
99 static int rockchip_gpio_probe(struct udevice *dev)
101 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
102 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
106 /* This only supports RK3288 at present */
107 priv->regs = (struct rockchip_gpio_regs *)dev_get_addr(dev);
108 ret = uclass_first_device_err(UCLASS_PINCTRL, &priv->pinctrl);
112 uc_priv->gpio_count = ROCKCHIP_GPIOS_PER_BANK;
113 end = strrchr(dev->name, '@');
114 priv->bank = trailing_strtoln(dev->name, end);
115 priv->name[0] = 'A' + priv->bank;
116 uc_priv->bank_name = priv->name;
121 static const struct dm_gpio_ops gpio_rockchip_ops = {
122 .direction_input = rockchip_gpio_direction_input,
123 .direction_output = rockchip_gpio_direction_output,
124 .get_value = rockchip_gpio_get_value,
125 .set_value = rockchip_gpio_set_value,
126 .get_function = rockchip_gpio_get_function,
129 static const struct udevice_id rockchip_gpio_ids[] = {
130 { .compatible = "rockchip,gpio-bank" },
134 U_BOOT_DRIVER(gpio_rockchip) = {
135 .name = "gpio_rockchip",
137 .of_match = rockchip_gpio_ids,
138 .ops = &gpio_rockchip_ops,
139 .priv_auto_alloc_size = sizeof(struct rockchip_gpio_priv),
140 .probe = rockchip_gpio_probe,