Blackfin arch: add BF54x missing GPIO access functions
[linux-2.6] / arch / blackfin / mach-bf548 / gpio.c
1 /*
2  * File:         arch/blackfin/mach-bf548/gpio.c
3  * Based on:
4  * Author:       Michael Hennerich (hennerich@blackfin.uclinux.org)
5  *
6  * Created:
7  * Description:  GPIO Abstraction Layer
8  *
9  * Modified:
10  *               Copyright 2007 Analog Devices Inc.
11  *
12  * Bugs:         Enter bugs at http://blackfin.uclinux.org/
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, see the file COPYING, or write
26  * to the Free Software Foundation, Inc.,
27  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28  */
29
30 #include <linux/module.h>
31 #include <linux/err.h>
32 #include <asm/blackfin.h>
33 #include <asm/gpio.h>
34 #include <linux/irq.h>
35
36 static struct gpio_port_t *gpio_array[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
37         (struct gpio_port_t *) PORTA_FER,
38         (struct gpio_port_t *) PORTB_FER,
39         (struct gpio_port_t *) PORTC_FER,
40         (struct gpio_port_t *) PORTD_FER,
41         (struct gpio_port_t *) PORTE_FER,
42         (struct gpio_port_t *) PORTF_FER,
43         (struct gpio_port_t *) PORTG_FER,
44         (struct gpio_port_t *) PORTH_FER,
45         (struct gpio_port_t *) PORTI_FER,
46         (struct gpio_port_t *) PORTJ_FER,
47 };
48
49 static unsigned short reserved_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
50
51 inline int check_gpio(unsigned short gpio)
52 {
53         if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15 \
54                         || gpio == GPIO_PH14 || gpio == GPIO_PH15 \
55                         || gpio == GPIO_PJ14 || gpio == GPIO_PJ15 \
56                         || gpio > MAX_BLACKFIN_GPIOS)
57                 return -EINVAL;
58         return 0;
59 }
60
61 static void port_setup(unsigned short gpio, unsigned short usage)
62 {
63         if (usage == GPIO_USAGE) {
64                 if (gpio_array[gpio_bank(gpio)]->port_fer & gpio_bit(gpio))
65                         printk(KERN_WARNING "bfin-gpio: Possible Conflict with Peripheral "
66                                "usage and GPIO %d detected!\n", gpio);
67                 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
68         } else
69                 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
70         SSYNC();
71 }
72
73 static int __init bfin_gpio_init(void)
74 {
75         int i;
76
77         printk(KERN_INFO "Blackfin GPIO Controller\n");
78
79         for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE)
80                 reserved_map[gpio_bank(i)] = 0;
81
82         return 0;
83 }
84
85 arch_initcall(bfin_gpio_init);
86
87
88 /***********************************************************
89 *
90 * FUNCTIONS: Blackfin GPIO Driver
91 *
92 * INPUTS/OUTPUTS:
93 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
94 *
95 *
96 * DESCRIPTION: Blackfin GPIO Driver API
97 *
98 * CAUTION:
99 *************************************************************
100 * MODIFICATION HISTORY :
101 **************************************************************/
102
103 int gpio_request(unsigned short gpio, const char *label)
104 {
105         unsigned long flags;
106
107         if (check_gpio(gpio) < 0)
108                 return -EINVAL;
109
110         local_irq_save(flags);
111
112         if (unlikely(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
113                 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved!\n", gpio);
114                 dump_stack();
115                 local_irq_restore(flags);
116                 return -EBUSY;
117         }
118         reserved_map[gpio_bank(gpio)] |= gpio_bit(gpio);
119
120         local_irq_restore(flags);
121
122         port_setup(gpio, GPIO_USAGE);
123
124         return 0;
125 }
126 EXPORT_SYMBOL(gpio_request);
127
128
129 void gpio_free(unsigned short gpio)
130 {
131         unsigned long flags;
132
133         if (check_gpio(gpio) < 0)
134                 return;
135
136         local_irq_save(flags);
137
138         if (unlikely(!(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
139                 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't reserved!\n", gpio);
140                 dump_stack();
141                 local_irq_restore(flags);
142                 return;
143         }
144
145         reserved_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
146
147         local_irq_restore(flags);
148 }
149 EXPORT_SYMBOL(gpio_free);
150
151
152 void gpio_direction_input(unsigned short gpio)
153 {
154         unsigned long flags;
155
156         BUG_ON(!(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio)));
157
158         local_irq_save(flags);
159         gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
160         gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
161         local_irq_restore(flags);
162 }
163 EXPORT_SYMBOL(gpio_direction_input);
164
165 void gpio_direction_output(unsigned short gpio)
166 {
167         unsigned long flags;
168
169         BUG_ON(!(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio)));
170
171         local_irq_save(flags);
172         gpio_array[gpio_bank(gpio)]->port_inen &= ~gpio_bit(gpio);
173         gpio_array[gpio_bank(gpio)]->port_dir_set = gpio_bit(gpio);
174         local_irq_restore(flags);
175 }
176 EXPORT_SYMBOL(gpio_direction_output);
177
178 void gpio_set_value(unsigned short gpio, unsigned short arg)
179 {
180         if (arg)
181                 gpio_array[gpio_bank(gpio)]->port_set = gpio_bit(gpio);
182         else
183                 gpio_array[gpio_bank(gpio)]->port_clear = gpio_bit(gpio);
184
185 }
186 EXPORT_SYMBOL(gpio_set_value);
187
188 unsigned short gpio_get_value(unsigned short gpio)
189 {
190         return (1 & (gpio_array[gpio_bank(gpio)]->port_data >> gpio_sub_n(gpio)));
191 }
192 EXPORT_SYMBOL(gpio_get_value);