blob: 1e85db179a65cd483e290f00c07d5d8b3e6563bb [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ian Campbellaf471472014-06-05 19:00:15 +01002/*
3 * (C) Copyright 2012 Henrik Nordstrom <henrik@henriknordstrom.net>
4 *
5 * Based on earlier arch/arm/cpu/armv7/sunxi/gpio.c:
6 *
7 * (C) Copyright 2007-2011
8 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
9 * Tom Cubie <tangliang@allwinnertech.com>
Ian Campbellaf471472014-06-05 19:00:15 +010010 */
11
12#include <common.h>
Simon Glass78304532014-10-30 20:25:49 -060013#include <dm.h>
14#include <errno.h>
15#include <fdtdec.h>
16#include <malloc.h>
Ian Campbellaf471472014-06-05 19:00:15 +010017#include <asm/io.h>
18#include <asm/gpio.h>
Chen-Yu Tsaif3aa2822016-07-22 16:12:59 +080019#include <dt-bindings/gpio/gpio.h>
Ian Campbellaf471472014-06-05 19:00:15 +010020
Simon Glassfa4689a2019-12-06 21:41:35 -070021#if !CONFIG_IS_ENABLED(DM_GPIO)
Ian Campbellaf471472014-06-05 19:00:15 +010022static int sunxi_gpio_output(u32 pin, u32 val)
23{
24 u32 dat;
25 u32 bank = GPIO_BANK(pin);
26 u32 num = GPIO_NUM(pin);
27 struct sunxi_gpio *pio = BANK_TO_GPIO(bank);
28
29 dat = readl(&pio->dat);
30 if (val)
31 dat |= 0x1 << num;
32 else
33 dat &= ~(0x1 << num);
34
35 writel(dat, &pio->dat);
36
37 return 0;
38}
39
40static int sunxi_gpio_input(u32 pin)
41{
42 u32 dat;
43 u32 bank = GPIO_BANK(pin);
44 u32 num = GPIO_NUM(pin);
45 struct sunxi_gpio *pio = BANK_TO_GPIO(bank);
46
47 dat = readl(&pio->dat);
48 dat >>= num;
49
50 return dat & 0x1;
51}
52
53int gpio_request(unsigned gpio, const char *label)
54{
55 return 0;
56}
57
58int gpio_free(unsigned gpio)
59{
60 return 0;
61}
62
63int gpio_direction_input(unsigned gpio)
64{
65 sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_INPUT);
66
Axel Lin06da3462014-12-20 11:41:25 +080067 return 0;
Ian Campbellaf471472014-06-05 19:00:15 +010068}
69
70int gpio_direction_output(unsigned gpio, int value)
71{
72 sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_OUTPUT);
73
74 return sunxi_gpio_output(gpio, value);
75}
76
77int gpio_get_value(unsigned gpio)
78{
79 return sunxi_gpio_input(gpio);
80}
81
82int gpio_set_value(unsigned gpio, int value)
83{
84 return sunxi_gpio_output(gpio, value);
85}
86
87int sunxi_name_to_gpio(const char *name)
88{
89 int group = 0;
90 int groupsize = 9 * 32;
91 long pin;
92 char *eptr;
Hans de Goede1fc9c4a2014-12-24 19:34:38 +010093
Ian Campbellaf471472014-06-05 19:00:15 +010094 if (*name == 'P' || *name == 'p')
95 name++;
96 if (*name >= 'A') {
97 group = *name - (*name > 'a' ? 'a' : 'A');
98 groupsize = 32;
99 name++;
100 }
101
102 pin = simple_strtol(name, &eptr, 10);
103 if (!*name || *eptr)
104 return -1;
105 if (pin < 0 || pin > groupsize || group >= 9)
106 return -1;
107 return group * 32 + pin;
108}
Simon Glassfa4689a2019-12-06 21:41:35 -0700109#endif /* DM_GPIO */
Simon Glass78304532014-10-30 20:25:49 -0600110
Simon Glassfa4689a2019-12-06 21:41:35 -0700111#if CONFIG_IS_ENABLED(DM_GPIO)
Simon Glass9754d932015-04-18 11:33:43 -0600112/* TODO(sjg@chromium.org): Remove this function and use device tree */
113int sunxi_name_to_gpio(const char *name)
114{
115 unsigned int gpio;
116 int ret;
Hans de Goede08607d12015-04-22 11:31:22 +0200117#if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO
118 char lookup[8];
Simon Glass9754d932015-04-18 11:33:43 -0600119
Hans de Goede08607d12015-04-22 11:31:22 +0200120 if (strcasecmp(name, "AXP0-VBUS-DETECT") == 0) {
121 sprintf(lookup, SUNXI_GPIO_AXP0_PREFIX "%d",
122 SUNXI_GPIO_AXP0_VBUS_DETECT);
123 name = lookup;
124 } else if (strcasecmp(name, "AXP0-VBUS-ENABLE") == 0) {
125 sprintf(lookup, SUNXI_GPIO_AXP0_PREFIX "%d",
126 SUNXI_GPIO_AXP0_VBUS_ENABLE);
127 name = lookup;
128 }
129#endif
Simon Glass9754d932015-04-18 11:33:43 -0600130 ret = gpio_lookup_name(name, NULL, NULL, &gpio);
131
132 return ret ? ret : gpio;
133}
134
Simon Glass78304532014-10-30 20:25:49 -0600135static int sunxi_gpio_get_value(struct udevice *dev, unsigned offset)
136{
Simon Glassb75b15b2020-12-03 16:55:23 -0700137 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
Simon Glass78304532014-10-30 20:25:49 -0600138 u32 num = GPIO_NUM(offset);
139 unsigned dat;
140
141 dat = readl(&plat->regs->dat);
142 dat >>= num;
143
144 return dat & 0x1;
145}
146
Simon Glass78304532014-10-30 20:25:49 -0600147static int sunxi_gpio_get_function(struct udevice *dev, unsigned offset)
148{
Simon Glassb75b15b2020-12-03 16:55:23 -0700149 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
Simon Glass78304532014-10-30 20:25:49 -0600150 int func;
151
152 func = sunxi_gpio_get_cfgbank(plat->regs, offset);
153 if (func == SUNXI_GPIO_OUTPUT)
154 return GPIOF_OUTPUT;
155 else if (func == SUNXI_GPIO_INPUT)
156 return GPIOF_INPUT;
157 else
158 return GPIOF_FUNC;
159}
160
Chen-Yu Tsaif3aa2822016-07-22 16:12:59 +0800161static int sunxi_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600162 struct ofnode_phandle_args *args)
Chen-Yu Tsaif3aa2822016-07-22 16:12:59 +0800163{
164 int ret;
165
166 ret = device_get_child(dev, args->args[0], &desc->dev);
167 if (ret)
168 return ret;
169 desc->offset = args->args[1];
Samuel Hollandbfda9492021-10-20 23:52:56 -0500170 desc->flags = gpio_flags_xlate(args->args[2]);
171
172 return 0;
173}
174
175static int sunxi_gpio_set_flags(struct udevice *dev, unsigned int offset,
176 ulong flags)
177{
178 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
179
180 if (flags & GPIOD_IS_OUT) {
181 u32 value = !!(flags & GPIOD_IS_OUT_ACTIVE);
182 u32 num = GPIO_NUM(offset);
183
184 clrsetbits_le32(&plat->regs->dat, 1 << num, value << num);
185 sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_OUTPUT);
186 } else if (flags & GPIOD_IS_IN) {
187 u32 pull = 0;
188
189 if (flags & GPIOD_PULL_UP)
190 pull = 1;
191 else if (flags & GPIOD_PULL_DOWN)
192 pull = 2;
193 sunxi_gpio_set_pull_bank(plat->regs, offset, pull);
194 sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_INPUT);
195 }
Chen-Yu Tsaif3aa2822016-07-22 16:12:59 +0800196
197 return 0;
198}
199
Simon Glass78304532014-10-30 20:25:49 -0600200static const struct dm_gpio_ops gpio_sunxi_ops = {
Simon Glass78304532014-10-30 20:25:49 -0600201 .get_value = sunxi_gpio_get_value,
Simon Glass78304532014-10-30 20:25:49 -0600202 .get_function = sunxi_gpio_get_function,
Chen-Yu Tsaif3aa2822016-07-22 16:12:59 +0800203 .xlate = sunxi_gpio_xlate,
Samuel Hollandbfda9492021-10-20 23:52:56 -0500204 .set_flags = sunxi_gpio_set_flags,
Simon Glass78304532014-10-30 20:25:49 -0600205};
206
Simon Glass78304532014-10-30 20:25:49 -0600207static int gpio_sunxi_probe(struct udevice *dev)
208{
Simon Glassb75b15b2020-12-03 16:55:23 -0700209 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
Simon Glassde0977b2015-03-05 12:25:20 -0700210 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass78304532014-10-30 20:25:49 -0600211
212 /* Tell the uclass how many GPIOs we have */
213 if (plat) {
Samuel Hollande3095022021-08-12 20:09:43 -0500214 uc_priv->gpio_count = SUNXI_GPIOS_PER_BANK;
Simon Glass78304532014-10-30 20:25:49 -0600215 uc_priv->bank_name = plat->bank_name;
216 }
217
218 return 0;
219}
Stephen Warrenb56989e2016-05-11 15:26:25 -0600220
Simon Glass78304532014-10-30 20:25:49 -0600221U_BOOT_DRIVER(gpio_sunxi) = {
222 .name = "gpio_sunxi",
223 .id = UCLASS_GPIO,
Simon Glass78304532014-10-30 20:25:49 -0600224 .probe = gpio_sunxi_probe,
Samuel Hollande3095022021-08-12 20:09:43 -0500225 .ops = &gpio_sunxi_ops,
Simon Glass78304532014-10-30 20:25:49 -0600226};
Simon Glassfa4689a2019-12-06 21:41:35 -0700227#endif /* DM_GPIO */