blob: e62c6f4eb59bb41b4f9c609f60290d390d1abafd [file] [log] [blame]
Tom Rixdd2d1cc2009-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 V1843eca2011-07-31 20:30:53 +000039#include <asm/arch/gpio.h>
Tom Rixdd2d1cc2009-05-15 23:48:36 +020040#include <asm/io.h>
41#include <asm/errno.h>
42
Sanjeev Premiea2cf552011-09-08 10:47:25 -040043#define OMAP_GPIO_DIR_OUT 0
44#define OMAP_GPIO_DIR_IN 1
45
Aneesh V9a390882011-07-21 09:29:29 -040046static inline const struct gpio_bank *get_gpio_bank(int gpio)
Tom Rixdd2d1cc2009-05-15 23:48:36 +020047{
Aneesh V9a390882011-07-21 09:29:29 -040048 return &omap_gpio_bank[gpio >> 5];
Tom Rixdd2d1cc2009-05-15 23:48:36 +020049}
50
51static inline int get_gpio_index(int gpio)
52{
53 return gpio & 0x1f;
54}
55
56static inline int gpio_valid(int gpio)
57{
58 if (gpio < 0)
Sanjeev Premiea2cf552011-09-08 10:47:25 -040059 return -EINVAL;
Tom Rixdd2d1cc2009-05-15 23:48:36 +020060 if (gpio < 192)
61 return 0;
Sanjeev Premiea2cf552011-09-08 10:47:25 -040062 return -EINVAL;
Tom Rixdd2d1cc2009-05-15 23:48:36 +020063}
64
65static int check_gpio(int gpio)
66{
67 if (gpio_valid(gpio) < 0) {
68 printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
Sanjeev Premiea2cf552011-09-08 10:47:25 -040069 return -EINVAL;
Tom Rixdd2d1cc2009-05-15 23:48:36 +020070 }
71 return 0;
72}
73
Aneesh V9a390882011-07-21 09:29:29 -040074static void _set_gpio_direction(const struct gpio_bank *bank, int gpio,
75 int is_input)
Tom Rixdd2d1cc2009-05-15 23:48:36 +020076{
77 void *reg = bank->base;
78 u32 l;
79
80 switch (bank->method) {
81 case METHOD_GPIO_24XX:
Aneesh V9a390882011-07-21 09:29:29 -040082 reg += OMAP_GPIO_OE;
Tom Rixdd2d1cc2009-05-15 23:48:36 +020083 break;
84 default:
85 return;
86 }
87 l = __raw_readl(reg);
88 if (is_input)
89 l |= 1 << gpio;
90 else
91 l &= ~(1 << gpio);
92 __raw_writel(l, reg);
93}
94
Sanjeev Premiea2cf552011-09-08 10:47:25 -040095/**
96 * Get the direction of the GPIO by reading the GPIO_OE register
97 * corresponding to the specified bank.
98 */
99static int _get_gpio_direction(const struct gpio_bank *bank, int gpio)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200100{
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400101 void *reg = bank->base;
102 u32 v;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200103
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400104 switch (bank->method) {
105 case METHOD_GPIO_24XX:
106 reg += OMAP_GPIO_OE;
107 break;
108 default:
109 return -EINVAL;
110 }
111
112 v = __raw_readl(reg);
113
114 if (v & (1 << gpio))
115 return OMAP_GPIO_DIR_IN;
116 else
117 return OMAP_GPIO_DIR_OUT;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200118}
119
Aneesh V9a390882011-07-21 09:29:29 -0400120static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio,
121 int enable)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200122{
123 void *reg = bank->base;
124 u32 l = 0;
125
126 switch (bank->method) {
127 case METHOD_GPIO_24XX:
128 if (enable)
Aneesh V9a390882011-07-21 09:29:29 -0400129 reg += OMAP_GPIO_SETDATAOUT;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200130 else
Aneesh V9a390882011-07-21 09:29:29 -0400131 reg += OMAP_GPIO_CLEARDATAOUT;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200132 l = 1 << gpio;
133 break;
134 default:
135 printf("omap3-gpio unknown bank method %s %d\n",
136 __FILE__, __LINE__);
137 return;
138 }
139 __raw_writel(l, reg);
140}
141
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400142/**
143 * Set value of the specified gpio
144 */
145void gpio_set_value(int gpio, int value)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200146{
Aneesh V9a390882011-07-21 09:29:29 -0400147 const struct gpio_bank *bank;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200148
149 if (check_gpio(gpio) < 0)
150 return;
151 bank = get_gpio_bank(gpio);
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400152 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200153}
154
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400155/**
156 * Get value of the specified gpio
157 */
158int gpio_get_value(int gpio)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200159{
Aneesh V9a390882011-07-21 09:29:29 -0400160 const struct gpio_bank *bank;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200161 void *reg;
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400162 int input;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200163
164 if (check_gpio(gpio) < 0)
165 return -EINVAL;
166 bank = get_gpio_bank(gpio);
167 reg = bank->base;
168 switch (bank->method) {
169 case METHOD_GPIO_24XX:
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400170 input = _get_gpio_direction(bank, get_gpio_index(gpio));
171 switch (input) {
172 case OMAP_GPIO_DIR_IN:
173 reg += OMAP_GPIO_DATAIN;
174 break;
175 case OMAP_GPIO_DIR_OUT:
176 reg += OMAP_GPIO_DATAOUT;
177 break;
178 default:
179 return -EINVAL;
180 }
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200181 break;
182 default:
183 return -EINVAL;
184 }
185 return (__raw_readl(reg)
186 & (1 << get_gpio_index(gpio))) != 0;
187}
188
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400189/**
190 * Set gpio direction as input
191 */
192int gpio_direction_input(unsigned gpio)
Joel A Fernandes46b9c202011-09-04 11:10:03 -0500193{
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400194 const struct gpio_bank *bank;
Joel A Fernandes46b9c202011-09-04 11:10:03 -0500195
196 if (check_gpio(gpio) < 0)
197 return -EINVAL;
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400198
Joel A Fernandes46b9c202011-09-04 11:10:03 -0500199 bank = get_gpio_bank(gpio);
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400200 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
201
202 return 0;
Joel A Fernandes46b9c202011-09-04 11:10:03 -0500203}
204
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400205/**
206 * Set gpio direction as output
207 */
208int gpio_direction_output(unsigned gpio, int value)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200209{
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400210 const struct gpio_bank *bank;
211
212 if (check_gpio(gpio) < 0)
213 return -EINVAL;
214
215 bank = get_gpio_bank(gpio);
216 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
217 _set_gpio_direction(bank, get_gpio_index(gpio), 0);
218
219 return 0;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200220}
221
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400222/**
223 * Request a gpio before using it.
224 *
225 * NOTE: Argument 'label' is unused.
226 */
227int gpio_request(int gpio, const char *label)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200228{
229 if (check_gpio(gpio) < 0)
230 return -EINVAL;
231
232 return 0;
233}
234
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400235/**
236 * Reset and free the gpio after using it.
237 */
238void gpio_free(unsigned gpio)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200239{
Aneesh V9a390882011-07-21 09:29:29 -0400240 const struct gpio_bank *bank;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200241
242 if (check_gpio(gpio) < 0)
243 return;
244 bank = get_gpio_bank(gpio);
245
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400246 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200247}