2 * Copyright (C) 2012 Lothar Waßmann <LW@KARO-electronics.de>
4 * See file CREDITS for list of people who contributed to this
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
21 #include <asm/bitops.h>
22 #include <asm/sizes.h>
23 #include <asm/arch/hardware.h>
24 #include <asm/arch/gpio.h>
27 unsigned int res1[0x134 / 4];
28 unsigned int oe; /* 0x134 */
29 unsigned int datain; /* 0x138 */
30 unsigned int res2[0x54 / 4];
31 unsigned int cleardataout; /* 0x190 */
32 unsigned int setdataout; /* 0x194 */
35 static const struct gpio_regs *gpio_base[] = {
36 (struct gpio_regs *)AM33XX_GPIO0_BASE,
37 (struct gpio_regs *)AM33XX_GPIO1_BASE,
38 (struct gpio_regs *)AM33XX_GPIO2_BASE,
39 (struct gpio_regs *)AM33XX_GPIO3_BASE,
42 static unsigned long gpio_map[ARRAY_SIZE(gpio_base)];
44 #define MAX_GPIO (ARRAY_SIZE(gpio_base) * 32)
46 int gpio_request(unsigned gpio, const char *name)
50 if (test_and_set_bit(gpio, gpio_map))
55 int gpio_free(unsigned gpio)
60 if (test_bit(gpio, gpio_map))
61 __clear_bit(gpio, gpio_map);
63 printf("ERROR: trying to free unclaimed GPIO %u\n", gpio);
68 int gpio_set_value(unsigned gpio, int val)
71 int mask = 1 << (gpio % 32);
73 if (bank >= ARRAY_SIZE(gpio_base))
77 writel(mask, &gpio_base[bank]->setdataout);
79 writel(mask, &gpio_base[bank]->cleardataout);
83 int gpio_direction_input(unsigned gpio)
86 int mask = 1 << (gpio % 32);
88 if (bank >= ARRAY_SIZE(gpio_base))
91 writel(readl(&gpio_base[bank]->oe) | mask, &gpio_base[bank]->oe);
95 int gpio_direction_output(unsigned gpio, int val)
98 int mask = 1 << (gpio % 32);
100 if (bank >= ARRAY_SIZE(gpio_base))
103 gpio_set_value(gpio, val);
104 writel(readl(&gpio_base[bank]->oe) & ~mask, &gpio_base[bank]->oe);