blob: f4c347941d6b20d5238f5f2709d3d1124935c0e5 [file] [log] [blame]
Tom Rix0c872ec2009-05-15 23:48:36 +02001/*
2 * Copyright (c) 2009 Wind River Systems, Inc.
3 * Tom Rix <Tom.Rix@windriver.com>
4 *
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.
9 *
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.
14 *
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,
18 * MA 02111-1307 USA
19 *
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.
24 *
25 * Below is the original's header including its copyright
26 *
27 * linux/arch/arm/plat-omap/gpio.c
28 *
29 * Support functions for OMAP GPIO
30 *
31 * Copyright (C) 2003-2005 Nokia Corporation
32 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
33 *
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.
37 */
38#include <common.h>
Aneesh V25223a62011-07-21 09:29:29 -040039#include <asm/omap_gpio.h>
Tom Rix0c872ec2009-05-15 23:48:36 +020040#include <asm/io.h>
41#include <asm/errno.h>
42
Aneesh V25223a62011-07-21 09:29:29 -040043static inline const struct gpio_bank *get_gpio_bank(int gpio)
Tom Rix0c872ec2009-05-15 23:48:36 +020044{
Aneesh V25223a62011-07-21 09:29:29 -040045 return &omap_gpio_bank[gpio >> 5];
Tom Rix0c872ec2009-05-15 23:48:36 +020046}
47
48static inline int get_gpio_index(int gpio)
49{
50 return gpio & 0x1f;
51}
52
53static inline int gpio_valid(int gpio)
54{
55 if (gpio < 0)
56 return -1;
57 if (gpio < 192)
58 return 0;
59 return -1;
60}
61
62static int check_gpio(int gpio)
63{
64 if (gpio_valid(gpio) < 0) {
65 printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
66 return -1;
67 }
68 return 0;
69}
70
Aneesh V25223a62011-07-21 09:29:29 -040071static void _set_gpio_direction(const struct gpio_bank *bank, int gpio,
72 int is_input)
Tom Rix0c872ec2009-05-15 23:48:36 +020073{
74 void *reg = bank->base;
75 u32 l;
76
77 switch (bank->method) {
78 case METHOD_GPIO_24XX:
Aneesh V25223a62011-07-21 09:29:29 -040079 reg += OMAP_GPIO_OE;
Tom Rix0c872ec2009-05-15 23:48:36 +020080 break;
81 default:
82 return;
83 }
84 l = __raw_readl(reg);
85 if (is_input)
86 l |= 1 << gpio;
87 else
88 l &= ~(1 << gpio);
89 __raw_writel(l, reg);
90}
91
92void omap_set_gpio_direction(int gpio, int is_input)
93{
Aneesh V25223a62011-07-21 09:29:29 -040094 const struct gpio_bank *bank;
Tom Rix0c872ec2009-05-15 23:48:36 +020095
96 if (check_gpio(gpio) < 0)
97 return;
98 bank = get_gpio_bank(gpio);
99 _set_gpio_direction(bank, get_gpio_index(gpio), is_input);
100}
101
Aneesh V25223a62011-07-21 09:29:29 -0400102static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio,
103 int enable)
Tom Rix0c872ec2009-05-15 23:48:36 +0200104{
105 void *reg = bank->base;
106 u32 l = 0;
107
108 switch (bank->method) {
109 case METHOD_GPIO_24XX:
110 if (enable)
Aneesh V25223a62011-07-21 09:29:29 -0400111 reg += OMAP_GPIO_SETDATAOUT;
Tom Rix0c872ec2009-05-15 23:48:36 +0200112 else
Aneesh V25223a62011-07-21 09:29:29 -0400113 reg += OMAP_GPIO_CLEARDATAOUT;
Tom Rix0c872ec2009-05-15 23:48:36 +0200114 l = 1 << gpio;
115 break;
116 default:
117 printf("omap3-gpio unknown bank method %s %d\n",
118 __FILE__, __LINE__);
119 return;
120 }
121 __raw_writel(l, reg);
122}
123
124void omap_set_gpio_dataout(int gpio, int enable)
125{
Aneesh V25223a62011-07-21 09:29:29 -0400126 const struct gpio_bank *bank;
Tom Rix0c872ec2009-05-15 23:48:36 +0200127
128 if (check_gpio(gpio) < 0)
129 return;
130 bank = get_gpio_bank(gpio);
131 _set_gpio_dataout(bank, get_gpio_index(gpio), enable);
132}
133
134int omap_get_gpio_datain(int gpio)
135{
Aneesh V25223a62011-07-21 09:29:29 -0400136 const struct gpio_bank *bank;
Tom Rix0c872ec2009-05-15 23:48:36 +0200137 void *reg;
138
139 if (check_gpio(gpio) < 0)
140 return -EINVAL;
141 bank = get_gpio_bank(gpio);
142 reg = bank->base;
143 switch (bank->method) {
144 case METHOD_GPIO_24XX:
Aneesh V25223a62011-07-21 09:29:29 -0400145 reg += OMAP_GPIO_DATAIN;
Tom Rix0c872ec2009-05-15 23:48:36 +0200146 break;
147 default:
148 return -EINVAL;
149 }
150 return (__raw_readl(reg)
151 & (1 << get_gpio_index(gpio))) != 0;
152}
153
Aneesh V25223a62011-07-21 09:29:29 -0400154static void _reset_gpio(const struct gpio_bank *bank, int gpio)
Tom Rix0c872ec2009-05-15 23:48:36 +0200155{
156 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
157}
158
159int omap_request_gpio(int gpio)
160{
161 if (check_gpio(gpio) < 0)
162 return -EINVAL;
163
164 return 0;
165}
166
167void omap_free_gpio(int gpio)
168{
Aneesh V25223a62011-07-21 09:29:29 -0400169 const struct gpio_bank *bank;
Tom Rix0c872ec2009-05-15 23:48:36 +0200170
171 if (check_gpio(gpio) < 0)
172 return;
173 bank = get_gpio_bank(gpio);
174
175 _reset_gpio(bank, gpio);
176}