2 * (C) Copyright 2012 Henrik Nordstrom <henrik@henriknordstrom.net>
4 * Based on earlier arch/arm/cpu/armv7/sunxi/gpio.c:
6 * (C) Copyright 2007-2011
7 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
8 * Tom Cubie <tangliang@allwinnertech.com>
10 * SPDX-License-Identifier: GPL-2.0+
20 #include <dm/device-internal.h>
21 #ifdef CONFIG_AXP209_POWER
24 #ifdef CONFIG_AXP221_POWER
28 DECLARE_GLOBAL_DATA_PTR;
30 #define SUNXI_GPIOS_PER_BANK SUNXI_GPIO_A_NR
32 struct sunxi_gpio_platdata {
33 struct sunxi_gpio *regs;
34 const char *bank_name; /* Name of bank, e.g. "B" */
38 #ifndef CONFIG_DM_GPIO
39 static int sunxi_gpio_output(u32 pin, u32 val)
42 u32 bank = GPIO_BANK(pin);
43 u32 num = GPIO_NUM(pin);
44 struct sunxi_gpio *pio = BANK_TO_GPIO(bank);
46 dat = readl(&pio->dat);
52 writel(dat, &pio->dat);
57 static int sunxi_gpio_input(u32 pin)
60 u32 bank = GPIO_BANK(pin);
61 u32 num = GPIO_NUM(pin);
62 struct sunxi_gpio *pio = BANK_TO_GPIO(bank);
64 dat = readl(&pio->dat);
70 int gpio_request(unsigned gpio, const char *label)
75 int gpio_free(unsigned gpio)
80 int gpio_direction_input(unsigned gpio)
83 if (gpio >= SUNXI_GPIO_AXP0_START)
84 return axp_gpio_direction_input(gpio - SUNXI_GPIO_AXP0_START);
86 sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_INPUT);
91 int gpio_direction_output(unsigned gpio, int value)
94 if (gpio >= SUNXI_GPIO_AXP0_START)
95 return axp_gpio_direction_output(gpio - SUNXI_GPIO_AXP0_START,
98 sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_OUTPUT);
100 return sunxi_gpio_output(gpio, value);
103 int gpio_get_value(unsigned gpio)
106 if (gpio >= SUNXI_GPIO_AXP0_START)
107 return axp_gpio_get_value(gpio - SUNXI_GPIO_AXP0_START);
109 return sunxi_gpio_input(gpio);
112 int gpio_set_value(unsigned gpio, int value)
115 if (gpio >= SUNXI_GPIO_AXP0_START)
116 return axp_gpio_set_value(gpio - SUNXI_GPIO_AXP0_START, value);
118 return sunxi_gpio_output(gpio, value);
121 int sunxi_name_to_gpio_bank(const char *name)
125 if (*name == 'P' || *name == 'p')
128 group = *name - (*name > 'a' ? 'a' : 'A');
135 int sunxi_name_to_gpio(const char *name)
138 int groupsize = 9 * 32;
143 if (strncasecmp(name, "AXP0-", 5) == 0) {
145 if (strcmp(name, "VBUS-DETECT") == 0)
146 return SUNXI_GPIO_AXP0_START +
147 SUNXI_GPIO_AXP0_VBUS_DETECT;
148 if (strcmp(name, "VBUS-ENABLE") == 0)
149 return SUNXI_GPIO_AXP0_START +
150 SUNXI_GPIO_AXP0_VBUS_ENABLE;
151 pin = simple_strtol(name, &eptr, 10);
154 return SUNXI_GPIO_AXP0_START + pin;
157 if (*name == 'P' || *name == 'p')
160 group = *name - (*name > 'a' ? 'a' : 'A');
165 pin = simple_strtol(name, &eptr, 10);
168 if (pin < 0 || pin > groupsize || group >= 9)
170 return group * 32 + pin;
174 #ifdef CONFIG_DM_GPIO
175 static int sunxi_gpio_direction_input(struct udevice *dev, unsigned offset)
177 struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
179 sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_INPUT);
184 static int sunxi_gpio_direction_output(struct udevice *dev, unsigned offset,
187 struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
188 u32 num = GPIO_NUM(offset);
190 sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_OUTPUT);
191 clrsetbits_le32(&plat->regs->dat, 1 << num, value ? (1 << num) : 0);
196 static int sunxi_gpio_get_value(struct udevice *dev, unsigned offset)
198 struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
199 u32 num = GPIO_NUM(offset);
202 dat = readl(&plat->regs->dat);
208 static int sunxi_gpio_set_value(struct udevice *dev, unsigned offset,
211 struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
212 u32 num = GPIO_NUM(offset);
214 clrsetbits_le32(&plat->regs->dat, 1 << num, value ? (1 << num) : 0);
218 static int sunxi_gpio_get_function(struct udevice *dev, unsigned offset)
220 struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
223 func = sunxi_gpio_get_cfgbank(plat->regs, offset);
224 if (func == SUNXI_GPIO_OUTPUT)
226 else if (func == SUNXI_GPIO_INPUT)
232 static const struct dm_gpio_ops gpio_sunxi_ops = {
233 .direction_input = sunxi_gpio_direction_input,
234 .direction_output = sunxi_gpio_direction_output,
235 .get_value = sunxi_gpio_get_value,
236 .set_value = sunxi_gpio_set_value,
237 .get_function = sunxi_gpio_get_function,
241 * Returns the name of a GPIO bank
243 * GPIO banks are named A, B, C, ...
245 * @bank: Bank number (0, 1..n-1)
246 * @return allocated string containing the name
248 static char *gpio_bank_name(int bank)
254 name[0] = 'A' + bank;
261 static int gpio_sunxi_probe(struct udevice *dev)
263 struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
264 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
266 /* Tell the uclass how many GPIOs we have */
268 uc_priv->gpio_count = plat->gpio_count;
269 uc_priv->bank_name = plat->bank_name;
275 * We have a top-level GPIO device with no actual GPIOs. It has a child
276 * device for each Sunxi bank.
278 static int gpio_sunxi_bind(struct udevice *parent)
280 struct sunxi_gpio_platdata *plat = parent->platdata;
281 struct sunxi_gpio_reg *ctlr;
285 /* If this is a child device, there is nothing to do here */
289 ctlr = (struct sunxi_gpio_reg *)fdtdec_get_addr(gd->fdt_blob,
290 parent->of_offset, "reg");
291 for (bank = 0; bank < SUNXI_GPIO_BANKS; bank++) {
292 struct sunxi_gpio_platdata *plat;
295 plat = calloc(1, sizeof(*plat));
298 plat->regs = &ctlr->gpio_bank[bank];
299 plat->bank_name = gpio_bank_name(bank);
300 plat->gpio_count = SUNXI_GPIOS_PER_BANK;
302 ret = device_bind(parent, parent->driver,
303 plat->bank_name, plat, -1, &dev);
306 dev->of_offset = parent->of_offset;
312 static const struct udevice_id sunxi_gpio_ids[] = {
313 { .compatible = "allwinner,sun7i-a20-pinctrl" },
317 U_BOOT_DRIVER(gpio_sunxi) = {
318 .name = "gpio_sunxi",
320 .ops = &gpio_sunxi_ops,
321 .of_match = sunxi_gpio_ids,
322 .bind = gpio_sunxi_bind,
323 .probe = gpio_sunxi_probe,