blob: 6c3c10862c474d849c5edbd87f77ae998c705aa0 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ian Campbellabce2c62014-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 Campbellabce2c62014-06-05 19:00:15 +010010 */
11
12#include <common.h>
Simon Glass7aa97482014-10-30 20:25:49 -060013#include <dm.h>
14#include <errno.h>
15#include <fdtdec.h>
16#include <malloc.h>
Ian Campbellabce2c62014-06-05 19:00:15 +010017#include <asm/io.h>
18#include <asm/gpio.h>
Simon Glass7aa97482014-10-30 20:25:49 -060019#include <dm/device-internal.h>
Chen-Yu Tsai4694dc52016-07-22 16:12:59 +080020#include <dt-bindings/gpio/gpio.h>
Ian Campbellabce2c62014-06-05 19:00:15 +010021
Simon Glass8a8d24b2020-12-03 16:55:23 -070022struct sunxi_gpio_plat {
Simon Glass7aa97482014-10-30 20:25:49 -060023 struct sunxi_gpio *regs;
24 const char *bank_name; /* Name of bank, e.g. "B" */
25 int gpio_count;
26};
27
Simon Glassbcee8d62019-12-06 21:41:35 -070028#if !CONFIG_IS_ENABLED(DM_GPIO)
Ian Campbellabce2c62014-06-05 19:00:15 +010029static int sunxi_gpio_output(u32 pin, u32 val)
30{
31 u32 dat;
32 u32 bank = GPIO_BANK(pin);
33 u32 num = GPIO_NUM(pin);
34 struct sunxi_gpio *pio = BANK_TO_GPIO(bank);
35
36 dat = readl(&pio->dat);
37 if (val)
38 dat |= 0x1 << num;
39 else
40 dat &= ~(0x1 << num);
41
42 writel(dat, &pio->dat);
43
44 return 0;
45}
46
47static int sunxi_gpio_input(u32 pin)
48{
49 u32 dat;
50 u32 bank = GPIO_BANK(pin);
51 u32 num = GPIO_NUM(pin);
52 struct sunxi_gpio *pio = BANK_TO_GPIO(bank);
53
54 dat = readl(&pio->dat);
55 dat >>= num;
56
57 return dat & 0x1;
58}
59
60int gpio_request(unsigned gpio, const char *label)
61{
62 return 0;
63}
64
65int gpio_free(unsigned gpio)
66{
67 return 0;
68}
69
70int gpio_direction_input(unsigned gpio)
71{
72 sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_INPUT);
73
Axel Linb0c4ae12014-12-20 11:41:25 +080074 return 0;
Ian Campbellabce2c62014-06-05 19:00:15 +010075}
76
77int gpio_direction_output(unsigned gpio, int value)
78{
79 sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_OUTPUT);
80
81 return sunxi_gpio_output(gpio, value);
82}
83
84int gpio_get_value(unsigned gpio)
85{
86 return sunxi_gpio_input(gpio);
87}
88
89int gpio_set_value(unsigned gpio, int value)
90{
91 return sunxi_gpio_output(gpio, value);
92}
93
94int sunxi_name_to_gpio(const char *name)
95{
96 int group = 0;
97 int groupsize = 9 * 32;
98 long pin;
99 char *eptr;
Hans de Goede6c727e02014-12-24 19:34:38 +0100100
Ian Campbellabce2c62014-06-05 19:00:15 +0100101 if (*name == 'P' || *name == 'p')
102 name++;
103 if (*name >= 'A') {
104 group = *name - (*name > 'a' ? 'a' : 'A');
105 groupsize = 32;
106 name++;
107 }
108
109 pin = simple_strtol(name, &eptr, 10);
110 if (!*name || *eptr)
111 return -1;
112 if (pin < 0 || pin > groupsize || group >= 9)
113 return -1;
114 return group * 32 + pin;
115}
Simon Glassbcee8d62019-12-06 21:41:35 -0700116#endif /* DM_GPIO */
Simon Glass7aa97482014-10-30 20:25:49 -0600117
Simon Glassbcee8d62019-12-06 21:41:35 -0700118#if CONFIG_IS_ENABLED(DM_GPIO)
Simon Glassa5ab8832015-04-18 11:33:43 -0600119/* TODO(sjg@chromium.org): Remove this function and use device tree */
120int sunxi_name_to_gpio(const char *name)
121{
122 unsigned int gpio;
123 int ret;
Hans de Goedef9b7a042015-04-22 11:31:22 +0200124#if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO
125 char lookup[8];
Simon Glassa5ab8832015-04-18 11:33:43 -0600126
Hans de Goedef9b7a042015-04-22 11:31:22 +0200127 if (strcasecmp(name, "AXP0-VBUS-DETECT") == 0) {
128 sprintf(lookup, SUNXI_GPIO_AXP0_PREFIX "%d",
129 SUNXI_GPIO_AXP0_VBUS_DETECT);
130 name = lookup;
131 } else if (strcasecmp(name, "AXP0-VBUS-ENABLE") == 0) {
132 sprintf(lookup, SUNXI_GPIO_AXP0_PREFIX "%d",
133 SUNXI_GPIO_AXP0_VBUS_ENABLE);
134 name = lookup;
135 }
136#endif
Simon Glassa5ab8832015-04-18 11:33:43 -0600137 ret = gpio_lookup_name(name, NULL, NULL, &gpio);
138
139 return ret ? ret : gpio;
140}
141
Simon Glass7aa97482014-10-30 20:25:49 -0600142static int sunxi_gpio_get_value(struct udevice *dev, unsigned offset)
143{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700144 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
Simon Glass7aa97482014-10-30 20:25:49 -0600145 u32 num = GPIO_NUM(offset);
146 unsigned dat;
147
148 dat = readl(&plat->regs->dat);
149 dat >>= num;
150
151 return dat & 0x1;
152}
153
Simon Glass7aa97482014-10-30 20:25:49 -0600154static int sunxi_gpio_get_function(struct udevice *dev, unsigned offset)
155{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700156 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
Simon Glass7aa97482014-10-30 20:25:49 -0600157 int func;
158
159 func = sunxi_gpio_get_cfgbank(plat->regs, offset);
160 if (func == SUNXI_GPIO_OUTPUT)
161 return GPIOF_OUTPUT;
162 else if (func == SUNXI_GPIO_INPUT)
163 return GPIOF_INPUT;
164 else
165 return GPIOF_FUNC;
166}
167
Chen-Yu Tsai4694dc52016-07-22 16:12:59 +0800168static int sunxi_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
Simon Glass3a571232017-05-18 20:09:18 -0600169 struct ofnode_phandle_args *args)
Chen-Yu Tsai4694dc52016-07-22 16:12:59 +0800170{
171 int ret;
172
173 ret = device_get_child(dev, args->args[0], &desc->dev);
174 if (ret)
175 return ret;
176 desc->offset = args->args[1];
Samuel Holland35ae1262021-10-20 23:52:56 -0500177 desc->flags = gpio_flags_xlate(args->args[2]);
178
179 return 0;
180}
181
182static int sunxi_gpio_set_flags(struct udevice *dev, unsigned int offset,
183 ulong flags)
184{
185 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
186
187 if (flags & GPIOD_IS_OUT) {
188 u32 value = !!(flags & GPIOD_IS_OUT_ACTIVE);
189 u32 num = GPIO_NUM(offset);
190
191 clrsetbits_le32(&plat->regs->dat, 1 << num, value << num);
192 sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_OUTPUT);
193 } else if (flags & GPIOD_IS_IN) {
194 u32 pull = 0;
195
196 if (flags & GPIOD_PULL_UP)
197 pull = 1;
198 else if (flags & GPIOD_PULL_DOWN)
199 pull = 2;
200 sunxi_gpio_set_pull_bank(plat->regs, offset, pull);
201 sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_INPUT);
202 }
Chen-Yu Tsai4694dc52016-07-22 16:12:59 +0800203
204 return 0;
205}
206
Simon Glass7aa97482014-10-30 20:25:49 -0600207static const struct dm_gpio_ops gpio_sunxi_ops = {
Simon Glass7aa97482014-10-30 20:25:49 -0600208 .get_value = sunxi_gpio_get_value,
Simon Glass7aa97482014-10-30 20:25:49 -0600209 .get_function = sunxi_gpio_get_function,
Chen-Yu Tsai4694dc52016-07-22 16:12:59 +0800210 .xlate = sunxi_gpio_xlate,
Samuel Holland35ae1262021-10-20 23:52:56 -0500211 .set_flags = sunxi_gpio_set_flags,
Simon Glass7aa97482014-10-30 20:25:49 -0600212};
213
214/**
215 * Returns the name of a GPIO bank
216 *
217 * GPIO banks are named A, B, C, ...
218 *
219 * @bank: Bank number (0, 1..n-1)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100220 * Return: allocated string containing the name
Simon Glass7aa97482014-10-30 20:25:49 -0600221 */
222static char *gpio_bank_name(int bank)
223{
224 char *name;
225
Simon Glass07ce60f2015-04-18 11:33:44 -0600226 name = malloc(3);
Simon Glass7aa97482014-10-30 20:25:49 -0600227 if (name) {
Simon Glass07ce60f2015-04-18 11:33:44 -0600228 name[0] = 'P';
229 name[1] = 'A' + bank;
230 name[2] = '\0';
Simon Glass7aa97482014-10-30 20:25:49 -0600231 }
232
233 return name;
234}
235
236static int gpio_sunxi_probe(struct udevice *dev)
237{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700238 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
Simon Glasse564f052015-03-05 12:25:20 -0700239 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass7aa97482014-10-30 20:25:49 -0600240
241 /* Tell the uclass how many GPIOs we have */
242 if (plat) {
243 uc_priv->gpio_count = plat->gpio_count;
244 uc_priv->bank_name = plat->bank_name;
245 }
246
247 return 0;
248}
Stephen Warren6f82fac2016-05-11 15:26:25 -0600249
250struct sunxi_gpio_soc_data {
251 int start;
252 int no_banks;
253};
254
Simon Glass7aa97482014-10-30 20:25:49 -0600255/**
256 * We have a top-level GPIO device with no actual GPIOs. It has a child
257 * device for each Sunxi bank.
258 */
259static int gpio_sunxi_bind(struct udevice *parent)
260{
Stephen Warren6f82fac2016-05-11 15:26:25 -0600261 struct sunxi_gpio_soc_data *soc_data =
262 (struct sunxi_gpio_soc_data *)dev_get_driver_data(parent);
Simon Glass0fd3d912020-12-22 19:30:28 -0700263 struct sunxi_gpio_plat *plat = dev_get_plat(parent);
Simon Glass7aa97482014-10-30 20:25:49 -0600264 struct sunxi_gpio_reg *ctlr;
Stephen Warren6f82fac2016-05-11 15:26:25 -0600265 int bank, ret;
Simon Glass7aa97482014-10-30 20:25:49 -0600266
267 /* If this is a child device, there is nothing to do here */
268 if (plat)
269 return 0;
270
Masahiro Yamada8613c8d2020-07-17 14:36:46 +0900271 ctlr = dev_read_addr_ptr(parent);
Stephen Warren6f82fac2016-05-11 15:26:25 -0600272 for (bank = 0; bank < soc_data->no_banks; bank++) {
Simon Glass8a8d24b2020-12-03 16:55:23 -0700273 struct sunxi_gpio_plat *plat;
Simon Glass7aa97482014-10-30 20:25:49 -0600274 struct udevice *dev;
275
276 plat = calloc(1, sizeof(*plat));
277 if (!plat)
278 return -ENOMEM;
279 plat->regs = &ctlr->gpio_bank[bank];
Stephen Warren6f82fac2016-05-11 15:26:25 -0600280 plat->bank_name = gpio_bank_name(soc_data->start + bank);
Simon Glass7aa97482014-10-30 20:25:49 -0600281 plat->gpio_count = SUNXI_GPIOS_PER_BANK;
282
Simon Glassa2703ce2020-11-28 17:50:03 -0700283 ret = device_bind(parent, parent->driver, plat->bank_name, plat,
Simon Glass20da4e02020-11-28 17:50:04 -0700284 dev_ofnode(parent), &dev);
Simon Glass7aa97482014-10-30 20:25:49 -0600285 if (ret)
286 return ret;
Simon Glass7aa97482014-10-30 20:25:49 -0600287 }
288
289 return 0;
290}
291
Stephen Warren6f82fac2016-05-11 15:26:25 -0600292static const struct sunxi_gpio_soc_data soc_data_a_all = {
293 .start = 0,
294 .no_banks = SUNXI_GPIO_BANKS,
295};
296
297static const struct sunxi_gpio_soc_data soc_data_l_1 = {
298 .start = 'L' - 'A',
299 .no_banks = 1,
300};
301
302static const struct sunxi_gpio_soc_data soc_data_l_2 = {
303 .start = 'L' - 'A',
304 .no_banks = 2,
305};
306
307static const struct sunxi_gpio_soc_data soc_data_l_3 = {
308 .start = 'L' - 'A',
309 .no_banks = 3,
310};
311
312#define ID(_compat_, _soc_data_) \
313 { .compatible = _compat_, .data = (ulong)&soc_data_##_soc_data_ }
314
Simon Glass7aa97482014-10-30 20:25:49 -0600315static const struct udevice_id sunxi_gpio_ids[] = {
Stephen Warren6f82fac2016-05-11 15:26:25 -0600316 ID("allwinner,sun4i-a10-pinctrl", a_all),
317 ID("allwinner,sun5i-a10s-pinctrl", a_all),
318 ID("allwinner,sun5i-a13-pinctrl", a_all),
Chris Blakea6968ec2018-01-15 16:28:18 -0600319 ID("allwinner,sun50i-h5-pinctrl", a_all),
Stephen Warren6f82fac2016-05-11 15:26:25 -0600320 ID("allwinner,sun6i-a31-pinctrl", a_all),
321 ID("allwinner,sun6i-a31s-pinctrl", a_all),
322 ID("allwinner,sun7i-a20-pinctrl", a_all),
323 ID("allwinner,sun8i-a23-pinctrl", a_all),
324 ID("allwinner,sun8i-a33-pinctrl", a_all),
325 ID("allwinner,sun8i-a83t-pinctrl", a_all),
326 ID("allwinner,sun8i-h3-pinctrl", a_all),
Chen-Yu Tsai33559ff2016-11-30 17:23:52 +0800327 ID("allwinner,sun8i-r40-pinctrl", a_all),
Icenowy Zheng889116b2020-10-26 22:18:01 +0800328 ID("allwinner,sun8i-v3-pinctrl", a_all),
Andre Przywara381996c2018-04-04 01:31:14 +0100329 ID("allwinner,sun8i-v3s-pinctrl", a_all),
Stephen Warren6f82fac2016-05-11 15:26:25 -0600330 ID("allwinner,sun9i-a80-pinctrl", a_all),
Andre Przywara381996c2018-04-04 01:31:14 +0100331 ID("allwinner,sun50i-a64-pinctrl", a_all),
Andre Przywara0d3c77f2019-06-23 15:09:47 +0100332 ID("allwinner,sun50i-h6-pinctrl", a_all),
Jernej Skrabec7836ccf2021-01-11 21:11:51 +0100333 ID("allwinner,sun50i-h616-pinctrl", a_all),
Stephen Warren6f82fac2016-05-11 15:26:25 -0600334 ID("allwinner,sun6i-a31-r-pinctrl", l_2),
335 ID("allwinner,sun8i-a23-r-pinctrl", l_1),
336 ID("allwinner,sun8i-a83t-r-pinctrl", l_1),
337 ID("allwinner,sun8i-h3-r-pinctrl", l_1),
338 ID("allwinner,sun9i-a80-r-pinctrl", l_3),
Andre Przywara381996c2018-04-04 01:31:14 +0100339 ID("allwinner,sun50i-a64-r-pinctrl", l_1),
Andre Przywara0d3c77f2019-06-23 15:09:47 +0100340 ID("allwinner,sun50i-h6-r-pinctrl", l_2),
Jernej Skrabec7836ccf2021-01-11 21:11:51 +0100341 ID("allwinner,sun50i-h616-r-pinctrl", l_1),
Simon Glass7aa97482014-10-30 20:25:49 -0600342 { }
343};
344
345U_BOOT_DRIVER(gpio_sunxi) = {
346 .name = "gpio_sunxi",
347 .id = UCLASS_GPIO,
348 .ops = &gpio_sunxi_ops,
349 .of_match = sunxi_gpio_ids,
350 .bind = gpio_sunxi_bind,
351 .probe = gpio_sunxi_probe,
352};
Simon Glassbcee8d62019-12-06 21:41:35 -0700353#endif /* DM_GPIO */