1 // SPDX-License-Identifier: GPL-2.0+
5 * DesignWare APB GPIO driver
11 #include <asm/arch/gpio.h>
15 #include <dm/device-internal.h>
16 #include <dm/device_compat.h>
17 #include <dm/devres.h>
22 #include <linux/bitops.h>
24 #define GPIO_SWPORT_DR(p) (0x00 + (p) * 0xc)
25 #define GPIO_SWPORT_DDR(p) (0x04 + (p) * 0xc)
26 #define GPIO_INTEN 0x30
27 #define GPIO_INTMASK 0x34
28 #define GPIO_INTTYPE_LEVEL 0x38
29 #define GPIO_INT_POLARITY 0x3c
30 #define GPIO_INTSTATUS 0x40
31 #define GPIO_PORTA_DEBOUNCE 0x48
32 #define GPIO_PORTA_EOI 0x4c
33 #define GPIO_EXT_PORT(p) (0x50 + (p) * 4)
35 struct gpio_dwapb_priv {
36 struct reset_ctl_bulk resets;
39 struct gpio_dwapb_plat {
46 static int dwapb_gpio_direction_input(struct udevice *dev, unsigned pin)
48 struct gpio_dwapb_plat *plat = dev_get_plat(dev);
50 clrbits_le32(plat->base + GPIO_SWPORT_DDR(plat->bank), 1 << pin);
54 static int dwapb_gpio_direction_output(struct udevice *dev, unsigned pin,
57 struct gpio_dwapb_plat *plat = dev_get_plat(dev);
59 setbits_le32(plat->base + GPIO_SWPORT_DDR(plat->bank), 1 << pin);
62 setbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
64 clrbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
69 static int dwapb_gpio_set_value(struct udevice *dev, unsigned pin, int val)
71 struct gpio_dwapb_plat *plat = dev_get_plat(dev);
74 setbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
76 clrbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
81 static int dwapb_gpio_get_function(struct udevice *dev, unsigned offset)
83 struct gpio_dwapb_plat *plat = dev_get_plat(dev);
86 gpio = readl(plat->base + GPIO_SWPORT_DDR(plat->bank));
88 if (gpio & BIT(offset))
94 static int dwapb_gpio_get_value(struct udevice *dev, unsigned pin)
96 struct gpio_dwapb_plat *plat = dev_get_plat(dev);
99 if (dwapb_gpio_get_function(dev, pin) == GPIOF_OUTPUT)
100 value = readl(plat->base + GPIO_SWPORT_DR(plat->bank));
102 value = readl(plat->base + GPIO_EXT_PORT(plat->bank));
103 return !!(value & BIT(pin));
106 static const struct dm_gpio_ops gpio_dwapb_ops = {
107 .direction_input = dwapb_gpio_direction_input,
108 .direction_output = dwapb_gpio_direction_output,
109 .get_value = dwapb_gpio_get_value,
110 .set_value = dwapb_gpio_set_value,
111 .get_function = dwapb_gpio_get_function,
114 static int gpio_dwapb_reset(struct udevice *dev)
117 struct gpio_dwapb_priv *priv = dev_get_priv(dev);
119 ret = reset_get_bulk(dev, &priv->resets);
121 /* Return 0 if error due to !CONFIG_DM_RESET and reset
122 * DT property is not present.
124 if (ret == -ENOENT || ret == -ENOTSUPP)
127 dev_warn(dev, "Can't get reset: %d\n", ret);
131 ret = reset_deassert_bulk(&priv->resets);
133 reset_release_bulk(&priv->resets);
134 dev_err(dev, "Failed to reset: %d\n", ret);
141 static int gpio_dwapb_probe(struct udevice *dev)
143 struct gpio_dev_priv *priv = dev_get_uclass_priv(dev);
144 struct gpio_dwapb_plat *plat = dev_get_plat(dev);
147 /* Reset on parent device only */
148 return gpio_dwapb_reset(dev);
151 priv->gpio_count = plat->pins;
152 priv->bank_name = plat->name;
157 static int gpio_dwapb_bind(struct udevice *dev)
159 struct gpio_dwapb_plat *plat = dev_get_plat(dev);
160 struct udevice *subdev;
165 /* If this is a child device, there is nothing to do here */
169 base = dev_read_addr(dev);
170 if (base == FDT_ADDR_T_NONE) {
171 debug("Can't get the GPIO register base address\n");
175 for (node = dev_read_first_subnode(dev); ofnode_valid(node);
176 node = dev_read_next_subnode(node)) {
177 if (!ofnode_read_bool(node, "gpio-controller"))
180 plat = devm_kcalloc(dev, 1, sizeof(*plat), GFP_KERNEL);
184 plat->base = (void *)base;
186 plat->pins = ofnode_read_u32_default(node, "snps,nr-gpios", 0);
188 if (ofnode_read_string_index(node, "bank-name", 0,
191 * Fall back to node name. This means accessing pins
192 * via bank name won't work.
196 snprintf(name, sizeof(name), "%s_",
197 ofnode_get_name(node));
198 plat->name = strdup(name);
205 ret = device_bind(dev, dev->driver, plat->name, plat, node,
216 static int gpio_dwapb_remove(struct udevice *dev)
218 struct gpio_dwapb_plat *plat = dev_get_plat(dev);
219 struct gpio_dwapb_priv *priv = dev_get_priv(dev);
222 return reset_release_bulk(&priv->resets);
227 static const struct udevice_id gpio_dwapb_ids[] = {
228 { .compatible = "snps,dw-apb-gpio" },
232 U_BOOT_DRIVER(gpio_dwapb) = {
233 .name = "gpio-dwapb",
235 .of_match = gpio_dwapb_ids,
236 .ops = &gpio_dwapb_ops,
237 .bind = gpio_dwapb_bind,
238 .probe = gpio_dwapb_probe,
239 .remove = gpio_dwapb_remove,
240 .priv_auto = sizeof(struct gpio_dwapb_priv),