1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Stefan Roese <sr@denx.de>
5 * Based on the Linux driver version which is:
6 * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
7 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
15 #include <linux/bitops.h>
19 #include <dm/device-internal.h>
20 #include <dt-bindings/gpio/gpio.h>
22 #define MTK_MAX_BANK 3
23 #define MTK_BANK_WIDTH 32
25 enum mediatek_gpio_reg {
39 static void __iomem *mediatek_gpio_membase;
41 struct mediatek_gpio_plat {
42 char bank_name[3]; /* Name of bank, e.g. "PA", "PB" etc */
47 static u32 reg_offs(struct mediatek_gpio_plat *plat, int reg)
49 return (reg * 0x10) + (plat->bank * 0x4);
52 static int mediatek_gpio_get_value(struct udevice *dev, unsigned int offset)
54 struct mediatek_gpio_plat *plat = dev_get_plat(dev);
56 return !!(ioread32(mediatek_gpio_membase +
57 reg_offs(plat, GPIO_REG_DATA)) & BIT(offset));
60 static int mediatek_gpio_set_value(struct udevice *dev, unsigned int offset,
63 struct mediatek_gpio_plat *plat = dev_get_plat(dev);
65 iowrite32(BIT(offset), mediatek_gpio_membase +
66 reg_offs(plat, value ? GPIO_REG_DSET : GPIO_REG_DCLR));
71 static int mediatek_gpio_direction_input(struct udevice *dev, unsigned int offset)
73 struct mediatek_gpio_plat *plat = dev_get_plat(dev);
75 clrbits_le32(mediatek_gpio_membase + reg_offs(plat, GPIO_REG_CTRL),
81 static int mediatek_gpio_direction_output(struct udevice *dev, unsigned int offset,
84 struct mediatek_gpio_plat *plat = dev_get_plat(dev);
86 setbits_le32(mediatek_gpio_membase + reg_offs(plat, GPIO_REG_CTRL),
88 mediatek_gpio_set_value(dev, offset, value);
93 static int mediatek_gpio_get_function(struct udevice *dev, unsigned int offset)
95 struct mediatek_gpio_plat *plat = dev_get_plat(dev);
98 t = ioread32(mediatek_gpio_membase + reg_offs(plat, GPIO_REG_CTRL));
105 static const struct dm_gpio_ops gpio_mediatek_ops = {
106 .direction_input = mediatek_gpio_direction_input,
107 .direction_output = mediatek_gpio_direction_output,
108 .get_value = mediatek_gpio_get_value,
109 .set_value = mediatek_gpio_set_value,
110 .get_function = mediatek_gpio_get_function,
113 static int gpio_mediatek_probe(struct udevice *dev)
115 struct mediatek_gpio_plat *plat = dev_get_plat(dev);
116 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
118 /* Tell the uclass how many GPIOs we have */
120 uc_priv->gpio_count = plat->gpio_count;
121 uc_priv->bank_name = plat->bank_name;
128 * We have a top-level GPIO device with no actual GPIOs. It has a child
129 * device for each Mediatek bank.
131 static int gpio_mediatek_bind(struct udevice *parent)
133 struct mediatek_gpio_plat *plat = dev_get_plat(parent);
138 /* If this is a child device, there is nothing to do here */
142 mediatek_gpio_membase = dev_remap_addr(parent);
143 if (!mediatek_gpio_membase)
146 for (node = dev_read_first_subnode(parent); ofnode_valid(node);
147 node = dev_read_next_subnode(node)) {
148 struct mediatek_gpio_plat *plat;
151 plat = calloc(1, sizeof(*plat));
154 plat->bank_name[0] = 'P';
155 plat->bank_name[1] = 'A' + bank;
156 plat->bank_name[2] = '\0';
157 plat->gpio_count = MTK_BANK_WIDTH;
160 ret = device_bind(parent, parent->driver, plat->bank_name, plat,
171 static const struct udevice_id mediatek_gpio_ids[] = {
172 { .compatible = "mtk,mt7621-gpio" },
176 U_BOOT_DRIVER(gpio_mediatek) = {
177 .name = "gpio_mediatek",
179 .ops = &gpio_mediatek_ops,
180 .of_match = mediatek_gpio_ids,
181 .bind = gpio_mediatek_bind,
182 .probe = gpio_mediatek_probe,