2 * Copyright (c) 2009 Wind River Systems, Inc.
3 * Tom Rix <Tom.Rix@windriver.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * This work is derived from the linux 2.6.27 kernel source
21 * To fetch, use the kernel repository
22 * git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
23 * Use the v2.6.27 tag.
25 * Below is the original's header including its copyright
27 * linux/arch/arm/plat-omap/gpio.c
29 * Support functions for OMAP GPIO
31 * Copyright (C) 2003-2005 Nokia Corporation
32 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
34 * This program is free software; you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License version 2 as
36 * published by the Free Software Foundation.
39 #include <asm/arch/gpio.h>
41 #include <asm/errno.h>
43 static struct gpio_bank gpio_bank_34xx[6] = {
44 { (void *)OMAP34XX_GPIO1_BASE, METHOD_GPIO_24XX },
45 { (void *)OMAP34XX_GPIO2_BASE, METHOD_GPIO_24XX },
46 { (void *)OMAP34XX_GPIO3_BASE, METHOD_GPIO_24XX },
47 { (void *)OMAP34XX_GPIO4_BASE, METHOD_GPIO_24XX },
48 { (void *)OMAP34XX_GPIO5_BASE, METHOD_GPIO_24XX },
49 { (void *)OMAP34XX_GPIO6_BASE, METHOD_GPIO_24XX },
52 static struct gpio_bank *gpio_bank = &gpio_bank_34xx[0];
54 static inline struct gpio_bank *get_gpio_bank(int gpio)
56 return &gpio_bank[gpio >> 5];
59 static inline int get_gpio_index(int gpio)
64 static inline int gpio_valid(int gpio)
73 static int check_gpio(int gpio)
75 if (gpio_valid(gpio) < 0) {
76 printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
82 static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
84 void *reg = bank->base;
87 switch (bank->method) {
88 case METHOD_GPIO_24XX:
89 reg += OMAP24XX_GPIO_OE;
102 void omap_set_gpio_direction(int gpio, int is_input)
104 struct gpio_bank *bank;
106 if (check_gpio(gpio) < 0)
108 bank = get_gpio_bank(gpio);
109 _set_gpio_direction(bank, get_gpio_index(gpio), is_input);
112 static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
114 void *reg = bank->base;
117 switch (bank->method) {
118 case METHOD_GPIO_24XX:
120 reg += OMAP24XX_GPIO_SETDATAOUT;
122 reg += OMAP24XX_GPIO_CLEARDATAOUT;
126 printf("omap3-gpio unknown bank method %s %d\n",
130 __raw_writel(l, reg);
133 void omap_set_gpio_dataout(int gpio, int enable)
135 struct gpio_bank *bank;
137 if (check_gpio(gpio) < 0)
139 bank = get_gpio_bank(gpio);
140 _set_gpio_dataout(bank, get_gpio_index(gpio), enable);
143 int omap_get_gpio_datain(int gpio)
145 struct gpio_bank *bank;
148 if (check_gpio(gpio) < 0)
150 bank = get_gpio_bank(gpio);
152 switch (bank->method) {
153 case METHOD_GPIO_24XX:
154 reg += OMAP24XX_GPIO_DATAIN;
159 return (__raw_readl(reg)
160 & (1 << get_gpio_index(gpio))) != 0;
163 static void _reset_gpio(struct gpio_bank *bank, int gpio)
165 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
168 int omap_request_gpio(int gpio)
170 if (check_gpio(gpio) < 0)
176 void omap_free_gpio(int gpio)
178 struct gpio_bank *bank;
180 if (check_gpio(gpio) < 0)
182 bank = get_gpio_bank(gpio);
184 _reset_gpio(bank, gpio);