blob: 5a0b5e4aee0ae3a70e252b61b1c6e818282bbcba [file] [log] [blame]
Ian Campbellaf471472014-06-05 19:00:15 +01001/*
2 * (C) Copyright 2012 Henrik Nordstrom <henrik@henriknordstrom.net>
3 *
4 * Based on earlier arch/arm/cpu/armv7/sunxi/gpio.c:
5 *
6 * (C) Copyright 2007-2011
7 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
8 * Tom Cubie <tangliang@allwinnertech.com>
9 *
10 * SPDX-License-Identifier: GPL-2.0+
11 */
12
13#include <common.h>
Simon Glass78304532014-10-30 20:25:49 -060014#include <dm.h>
15#include <errno.h>
16#include <fdtdec.h>
17#include <malloc.h>
Hans de Goede3ae1d132015-04-25 17:25:14 +020018#include <asm/arch/gpio.h>
Ian Campbellaf471472014-06-05 19:00:15 +010019#include <asm/io.h>
20#include <asm/gpio.h>
Simon Glass78304532014-10-30 20:25:49 -060021#include <dm/device-internal.h>
Ian Campbellaf471472014-06-05 19:00:15 +010022
Simon Glass78304532014-10-30 20:25:49 -060023DECLARE_GLOBAL_DATA_PTR;
24
25#define SUNXI_GPIOS_PER_BANK SUNXI_GPIO_A_NR
26
27struct sunxi_gpio_platdata {
28 struct sunxi_gpio *regs;
29 const char *bank_name; /* Name of bank, e.g. "B" */
30 int gpio_count;
31};
32
33#ifndef CONFIG_DM_GPIO
Ian Campbellaf471472014-06-05 19:00:15 +010034static int sunxi_gpio_output(u32 pin, u32 val)
35{
36 u32 dat;
37 u32 bank = GPIO_BANK(pin);
38 u32 num = GPIO_NUM(pin);
39 struct sunxi_gpio *pio = BANK_TO_GPIO(bank);
40
41 dat = readl(&pio->dat);
42 if (val)
43 dat |= 0x1 << num;
44 else
45 dat &= ~(0x1 << num);
46
47 writel(dat, &pio->dat);
48
49 return 0;
50}
51
52static int sunxi_gpio_input(u32 pin)
53{
54 u32 dat;
55 u32 bank = GPIO_BANK(pin);
56 u32 num = GPIO_NUM(pin);
57 struct sunxi_gpio *pio = BANK_TO_GPIO(bank);
58
59 dat = readl(&pio->dat);
60 dat >>= num;
61
62 return dat & 0x1;
63}
64
65int gpio_request(unsigned gpio, const char *label)
66{
67 return 0;
68}
69
70int gpio_free(unsigned gpio)
71{
72 return 0;
73}
74
75int gpio_direction_input(unsigned gpio)
76{
Hans de Goede3ae1d132015-04-25 17:25:14 +020077#if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO
Hans de Goede1fc9c4a2014-12-24 19:34:38 +010078 if (gpio >= SUNXI_GPIO_AXP0_START)
Hans de Goede551ea282015-04-22 16:27:01 +020079 return axp_gpio_direction_input(NULL, gpio - SUNXI_GPIO_AXP0_START);
Hans de Goede1fc9c4a2014-12-24 19:34:38 +010080#endif
Ian Campbellaf471472014-06-05 19:00:15 +010081 sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_INPUT);
82
Axel Lin06da3462014-12-20 11:41:25 +080083 return 0;
Ian Campbellaf471472014-06-05 19:00:15 +010084}
85
86int gpio_direction_output(unsigned gpio, int value)
87{
Hans de Goede3ae1d132015-04-25 17:25:14 +020088#if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO
Hans de Goede1fc9c4a2014-12-24 19:34:38 +010089 if (gpio >= SUNXI_GPIO_AXP0_START)
Hans de Goede551ea282015-04-22 16:27:01 +020090 return axp_gpio_direction_output(NULL, gpio - SUNXI_GPIO_AXP0_START,
Hans de Goede1fc9c4a2014-12-24 19:34:38 +010091 value);
92#endif
Ian Campbellaf471472014-06-05 19:00:15 +010093 sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_OUTPUT);
94
95 return sunxi_gpio_output(gpio, value);
96}
97
98int gpio_get_value(unsigned gpio)
99{
Hans de Goede3ae1d132015-04-25 17:25:14 +0200100#if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO
Hans de Goede1fc9c4a2014-12-24 19:34:38 +0100101 if (gpio >= SUNXI_GPIO_AXP0_START)
Hans de Goede551ea282015-04-22 16:27:01 +0200102 return axp_gpio_get_value(NULL, gpio - SUNXI_GPIO_AXP0_START);
Hans de Goede1fc9c4a2014-12-24 19:34:38 +0100103#endif
Ian Campbellaf471472014-06-05 19:00:15 +0100104 return sunxi_gpio_input(gpio);
105}
106
107int gpio_set_value(unsigned gpio, int value)
108{
Hans de Goede3ae1d132015-04-25 17:25:14 +0200109#if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO
Hans de Goede1fc9c4a2014-12-24 19:34:38 +0100110 if (gpio >= SUNXI_GPIO_AXP0_START)
Hans de Goede551ea282015-04-22 16:27:01 +0200111 return axp_gpio_set_value(NULL, gpio - SUNXI_GPIO_AXP0_START, value);
Hans de Goede1fc9c4a2014-12-24 19:34:38 +0100112#endif
Ian Campbellaf471472014-06-05 19:00:15 +0100113 return sunxi_gpio_output(gpio, value);
114}
115
116int sunxi_name_to_gpio(const char *name)
117{
118 int group = 0;
119 int groupsize = 9 * 32;
120 long pin;
121 char *eptr;
Hans de Goede1fc9c4a2014-12-24 19:34:38 +0100122
Hans de Goede3ae1d132015-04-25 17:25:14 +0200123#if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO
Hans de Goede1fc9c4a2014-12-24 19:34:38 +0100124 if (strncasecmp(name, "AXP0-", 5) == 0) {
125 name += 5;
Paul Kocialkowski6604a132015-03-22 18:07:09 +0100126 if (strcmp(name, "VBUS-DETECT") == 0)
127 return SUNXI_GPIO_AXP0_START +
128 SUNXI_GPIO_AXP0_VBUS_DETECT;
129 if (strcmp(name, "VBUS-ENABLE") == 0)
130 return SUNXI_GPIO_AXP0_START +
131 SUNXI_GPIO_AXP0_VBUS_ENABLE;
Hans de Goede1fc9c4a2014-12-24 19:34:38 +0100132 pin = simple_strtol(name, &eptr, 10);
133 if (!*name || *eptr)
134 return -1;
135 return SUNXI_GPIO_AXP0_START + pin;
136 }
137#endif
Ian Campbellaf471472014-06-05 19:00:15 +0100138 if (*name == 'P' || *name == 'p')
139 name++;
140 if (*name >= 'A') {
141 group = *name - (*name > 'a' ? 'a' : 'A');
142 groupsize = 32;
143 name++;
144 }
145
146 pin = simple_strtol(name, &eptr, 10);
147 if (!*name || *eptr)
148 return -1;
149 if (pin < 0 || pin > groupsize || group >= 9)
150 return -1;
151 return group * 32 + pin;
152}
Simon Glass78304532014-10-30 20:25:49 -0600153#endif
154
Hans de Goededfa5b9f2015-04-22 17:59:01 +0200155int sunxi_name_to_gpio_bank(const char *name)
156{
157 int group = 0;
158
159 if (*name == 'P' || *name == 'p')
160 name++;
161 if (*name >= 'A') {
162 group = *name - (*name > 'a' ? 'a' : 'A');
163 return group;
164 }
165
166 return -1;
167}
168
Simon Glass78304532014-10-30 20:25:49 -0600169#ifdef CONFIG_DM_GPIO
Simon Glass9754d932015-04-18 11:33:43 -0600170/* TODO(sjg@chromium.org): Remove this function and use device tree */
171int sunxi_name_to_gpio(const char *name)
172{
173 unsigned int gpio;
174 int ret;
175
176 ret = gpio_lookup_name(name, NULL, NULL, &gpio);
177
178 return ret ? ret : gpio;
179}
180
Simon Glass78304532014-10-30 20:25:49 -0600181static int sunxi_gpio_direction_input(struct udevice *dev, unsigned offset)
182{
183 struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
184
185 sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_INPUT);
186
187 return 0;
188}
189
190static int sunxi_gpio_direction_output(struct udevice *dev, unsigned offset,
191 int value)
192{
193 struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
194 u32 num = GPIO_NUM(offset);
195
196 sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_OUTPUT);
197 clrsetbits_le32(&plat->regs->dat, 1 << num, value ? (1 << num) : 0);
198
199 return 0;
200}
201
202static int sunxi_gpio_get_value(struct udevice *dev, unsigned offset)
203{
204 struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
205 u32 num = GPIO_NUM(offset);
206 unsigned dat;
207
208 dat = readl(&plat->regs->dat);
209 dat >>= num;
210
211 return dat & 0x1;
212}
213
214static int sunxi_gpio_set_value(struct udevice *dev, unsigned offset,
215 int value)
216{
217 struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
218 u32 num = GPIO_NUM(offset);
219
220 clrsetbits_le32(&plat->regs->dat, 1 << num, value ? (1 << num) : 0);
221 return 0;
222}
223
224static int sunxi_gpio_get_function(struct udevice *dev, unsigned offset)
225{
226 struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
227 int func;
228
229 func = sunxi_gpio_get_cfgbank(plat->regs, offset);
230 if (func == SUNXI_GPIO_OUTPUT)
231 return GPIOF_OUTPUT;
232 else if (func == SUNXI_GPIO_INPUT)
233 return GPIOF_INPUT;
234 else
235 return GPIOF_FUNC;
236}
237
238static const struct dm_gpio_ops gpio_sunxi_ops = {
239 .direction_input = sunxi_gpio_direction_input,
240 .direction_output = sunxi_gpio_direction_output,
241 .get_value = sunxi_gpio_get_value,
242 .set_value = sunxi_gpio_set_value,
243 .get_function = sunxi_gpio_get_function,
244};
245
246/**
247 * Returns the name of a GPIO bank
248 *
249 * GPIO banks are named A, B, C, ...
250 *
251 * @bank: Bank number (0, 1..n-1)
252 * @return allocated string containing the name
253 */
254static char *gpio_bank_name(int bank)
255{
256 char *name;
257
Simon Glasse2d30542015-04-18 11:33:44 -0600258 name = malloc(3);
Simon Glass78304532014-10-30 20:25:49 -0600259 if (name) {
Simon Glasse2d30542015-04-18 11:33:44 -0600260 name[0] = 'P';
261 name[1] = 'A' + bank;
262 name[2] = '\0';
Simon Glass78304532014-10-30 20:25:49 -0600263 }
264
265 return name;
266}
267
268static int gpio_sunxi_probe(struct udevice *dev)
269{
270 struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
Simon Glassde0977b2015-03-05 12:25:20 -0700271 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass78304532014-10-30 20:25:49 -0600272
273 /* Tell the uclass how many GPIOs we have */
274 if (plat) {
275 uc_priv->gpio_count = plat->gpio_count;
276 uc_priv->bank_name = plat->bank_name;
277 }
278
279 return 0;
280}
281/**
282 * We have a top-level GPIO device with no actual GPIOs. It has a child
283 * device for each Sunxi bank.
284 */
285static int gpio_sunxi_bind(struct udevice *parent)
286{
287 struct sunxi_gpio_platdata *plat = parent->platdata;
288 struct sunxi_gpio_reg *ctlr;
289 int bank;
290 int ret;
291
292 /* If this is a child device, there is nothing to do here */
293 if (plat)
294 return 0;
295
296 ctlr = (struct sunxi_gpio_reg *)fdtdec_get_addr(gd->fdt_blob,
297 parent->of_offset, "reg");
298 for (bank = 0; bank < SUNXI_GPIO_BANKS; bank++) {
299 struct sunxi_gpio_platdata *plat;
300 struct udevice *dev;
301
302 plat = calloc(1, sizeof(*plat));
303 if (!plat)
304 return -ENOMEM;
305 plat->regs = &ctlr->gpio_bank[bank];
306 plat->bank_name = gpio_bank_name(bank);
307 plat->gpio_count = SUNXI_GPIOS_PER_BANK;
308
309 ret = device_bind(parent, parent->driver,
310 plat->bank_name, plat, -1, &dev);
311 if (ret)
312 return ret;
313 dev->of_offset = parent->of_offset;
314 }
315
316 return 0;
317}
318
319static const struct udevice_id sunxi_gpio_ids[] = {
Hans de Goedeced57562015-04-22 11:29:38 +0200320 { .compatible = "allwinner,sun4i-a10-pinctrl" },
321 { .compatible = "allwinner,sun5i-a10s-pinctrl" },
322 { .compatible = "allwinner,sun5i-a13-pinctrl" },
323 { .compatible = "allwinner,sun6i-a31-pinctrl" },
324 { .compatible = "allwinner,sun6i-a31s-pinctrl" },
Simon Glass78304532014-10-30 20:25:49 -0600325 { .compatible = "allwinner,sun7i-a20-pinctrl" },
Hans de Goedeced57562015-04-22 11:29:38 +0200326 { .compatible = "allwinner,sun8i-a23-pinctrl" },
327 { .compatible = "allwinner,sun9i-a80-pinctrl" },
Simon Glass78304532014-10-30 20:25:49 -0600328 { }
329};
330
331U_BOOT_DRIVER(gpio_sunxi) = {
332 .name = "gpio_sunxi",
333 .id = UCLASS_GPIO,
334 .ops = &gpio_sunxi_ops,
335 .of_match = sunxi_gpio_ids,
336 .bind = gpio_sunxi_bind,
337 .probe = gpio_sunxi_probe,
338};
339#endif