1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2015 Marek Vasut <marex@denx.de>
5 * DesignWare APB GPIO driver
10 #include <asm/arch/gpio.h>
14 #include <dm/device-internal.h>
20 #define GPIO_SWPORT_DR(p) (0x00 + (p) * 0xc)
21 #define GPIO_SWPORT_DDR(p) (0x04 + (p) * 0xc)
22 #define GPIO_INTEN 0x30
23 #define GPIO_INTMASK 0x34
24 #define GPIO_INTTYPE_LEVEL 0x38
25 #define GPIO_INT_POLARITY 0x3c
26 #define GPIO_INTSTATUS 0x40
27 #define GPIO_PORTA_DEBOUNCE 0x48
28 #define GPIO_PORTA_EOI 0x4c
29 #define GPIO_EXT_PORT(p) (0x50 + (p) * 4)
31 struct gpio_dwapb_priv {
32 struct reset_ctl_bulk resets;
35 struct gpio_dwapb_platdata {
42 static int dwapb_gpio_direction_input(struct udevice *dev, unsigned pin)
44 struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
46 clrbits_le32(plat->base + GPIO_SWPORT_DDR(plat->bank), 1 << pin);
50 static int dwapb_gpio_direction_output(struct udevice *dev, unsigned pin,
53 struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
55 setbits_le32(plat->base + GPIO_SWPORT_DDR(plat->bank), 1 << pin);
58 setbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
60 clrbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
65 static int dwapb_gpio_get_value(struct udevice *dev, unsigned pin)
67 struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
68 return !!(readl(plat->base + GPIO_EXT_PORT(plat->bank)) & (1 << pin));
72 static int dwapb_gpio_set_value(struct udevice *dev, unsigned pin, int val)
74 struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
77 setbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
79 clrbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
84 static int dwapb_gpio_get_function(struct udevice *dev, unsigned offset)
86 struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
89 gpio = readl(plat->base + GPIO_SWPORT_DDR(plat->bank));
91 if (gpio & BIT(offset))
97 static const struct dm_gpio_ops gpio_dwapb_ops = {
98 .direction_input = dwapb_gpio_direction_input,
99 .direction_output = dwapb_gpio_direction_output,
100 .get_value = dwapb_gpio_get_value,
101 .set_value = dwapb_gpio_set_value,
102 .get_function = dwapb_gpio_get_function,
105 static int gpio_dwapb_reset(struct udevice *dev)
108 struct gpio_dwapb_priv *priv = dev_get_priv(dev);
110 ret = reset_get_bulk(dev, &priv->resets);
112 /* Return 0 if error due to !CONFIG_DM_RESET and reset
113 * DT property is not present.
115 if (ret == -ENOENT || ret == -ENOTSUPP)
118 dev_warn(dev, "Can't get reset: %d\n", ret);
122 ret = reset_deassert_bulk(&priv->resets);
124 reset_release_bulk(&priv->resets);
125 dev_err(dev, "Failed to reset: %d\n", ret);
132 static int gpio_dwapb_probe(struct udevice *dev)
134 struct gpio_dev_priv *priv = dev_get_uclass_priv(dev);
135 struct gpio_dwapb_platdata *plat = dev->platdata;
138 /* Reset on parent device only */
139 return gpio_dwapb_reset(dev);
142 priv->gpio_count = plat->pins;
143 priv->bank_name = plat->name;
148 static int gpio_dwapb_bind(struct udevice *dev)
150 struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
151 struct udevice *subdev;
156 /* If this is a child device, there is nothing to do here */
160 base = dev_read_addr(dev);
161 if (base == FDT_ADDR_T_NONE) {
162 debug("Can't get the GPIO register base address\n");
166 for (node = dev_read_first_subnode(dev); ofnode_valid(node);
167 node = dev_read_next_subnode(node)) {
168 if (!ofnode_read_bool(node, "gpio-controller"))
171 plat = devm_kcalloc(dev, 1, sizeof(*plat), GFP_KERNEL);
177 plat->pins = ofnode_read_u32_default(node, "snps,nr-gpios", 0);
179 if (ofnode_read_string_index(node, "bank-name", 0,
182 * Fall back to node name. This means accessing pins
183 * via bank name won't work.
185 plat->name = ofnode_get_name(node);
188 ret = device_bind_ofnode(dev, dev->driver, plat->name,
189 plat, node, &subdev);
199 static int gpio_dwapb_remove(struct udevice *dev)
201 struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
202 struct gpio_dwapb_priv *priv = dev_get_priv(dev);
205 return reset_release_bulk(&priv->resets);
210 static const struct udevice_id gpio_dwapb_ids[] = {
211 { .compatible = "snps,dw-apb-gpio" },
215 U_BOOT_DRIVER(gpio_dwapb) = {
216 .name = "gpio-dwapb",
218 .of_match = gpio_dwapb_ids,
219 .ops = &gpio_dwapb_ops,
220 .bind = gpio_dwapb_bind,
221 .probe = gpio_dwapb_probe,
222 .remove = gpio_dwapb_remove,
223 .priv_auto_alloc_size = sizeof(struct gpio_dwapb_priv),