2 * File: arch/blackfin/mach-bf548/gpio.c
4 * Author: Michael Hennerich (hennerich@blackfin.uclinux.org)
7 * Description: GPIO Abstraction Layer
10 * Copyright 2007 Analog Devices Inc.
12 * Bugs: Enter bugs at http://blackfin.uclinux.org/
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.
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.
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
30 #include <linux/module.h>
31 #include <linux/err.h>
32 #include <asm/blackfin.h>
34 #include <linux/irq.h>
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,
49 static unsigned short reserved_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
51 inline int check_gpio(unsigned short gpio)
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)
61 static void port_setup(unsigned short gpio, unsigned short usage)
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);
69 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
73 static int __init bfin_gpio_init(void)
77 printk(KERN_INFO "Blackfin GPIO Controller\n");
79 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE)
80 reserved_map[gpio_bank(i)] = 0;
85 arch_initcall(bfin_gpio_init);
88 /***********************************************************
90 * FUNCTIONS: Blackfin GPIO Driver
93 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
96 * DESCRIPTION: Blackfin GPIO Driver API
99 *************************************************************
100 * MODIFICATION HISTORY :
101 **************************************************************/
103 int gpio_request(unsigned short gpio, const char *label)
107 if (check_gpio(gpio) < 0)
110 local_irq_save(flags);
112 if (unlikely(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
113 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved!\n", gpio);
115 local_irq_restore(flags);
118 reserved_map[gpio_bank(gpio)] |= gpio_bit(gpio);
120 local_irq_restore(flags);
122 port_setup(gpio, GPIO_USAGE);
126 EXPORT_SYMBOL(gpio_request);
129 void gpio_free(unsigned short gpio)
133 if (check_gpio(gpio) < 0)
136 local_irq_save(flags);
138 if (unlikely(!(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
139 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't reserved!\n", gpio);
141 local_irq_restore(flags);
145 reserved_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
147 local_irq_restore(flags);
149 EXPORT_SYMBOL(gpio_free);
152 void gpio_direction_input(unsigned short gpio)
156 BUG_ON(!(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio)));
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);
163 EXPORT_SYMBOL(gpio_direction_input);
165 void gpio_direction_output(unsigned short gpio)
169 BUG_ON(!(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio)));
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);
176 EXPORT_SYMBOL(gpio_direction_output);
178 void gpio_set_value(unsigned short gpio, unsigned short arg)
181 gpio_array[gpio_bank(gpio)]->port_set = gpio_bit(gpio);
183 gpio_array[gpio_bank(gpio)]->port_clear = gpio_bit(gpio);
186 EXPORT_SYMBOL(gpio_set_value);
188 unsigned short gpio_get_value(unsigned short gpio)
190 return (1 & (gpio_array[gpio_bank(gpio)]->port_data >> gpio_sub_n(gpio)));
192 EXPORT_SYMBOL(gpio_get_value);