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 <asm/portmux.h>
35 #include <linux/irq.h>
37 static struct gpio_port_t *gpio_array[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
38 (struct gpio_port_t *)PORTA_FER,
39 (struct gpio_port_t *)PORTB_FER,
40 (struct gpio_port_t *)PORTC_FER,
41 (struct gpio_port_t *)PORTD_FER,
42 (struct gpio_port_t *)PORTE_FER,
43 (struct gpio_port_t *)PORTF_FER,
44 (struct gpio_port_t *)PORTG_FER,
45 (struct gpio_port_t *)PORTH_FER,
46 (struct gpio_port_t *)PORTI_FER,
47 (struct gpio_port_t *)PORTJ_FER,
50 static unsigned short reserved_gpio_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
51 static unsigned short reserved_peri_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
52 char *str_ident = NULL;
54 #define RESOURCE_LABEL_SIZE 16
56 inline int check_gpio(unsigned short gpio)
58 if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15
59 || gpio == GPIO_PH14 || gpio == GPIO_PH15
60 || gpio == GPIO_PJ14 || gpio == GPIO_PJ15
61 || gpio > MAX_BLACKFIN_GPIOS)
66 inline void portmux_setup(unsigned short portno, unsigned short function)
70 pmux = gpio_array[gpio_bank(portno)]->port_mux;
72 pmux &= ~(0x3 << (2 * gpio_sub_n(portno)));
73 pmux |= (function & 0x3) << (2 * gpio_sub_n(portno));
75 gpio_array[gpio_bank(portno)]->port_mux = pmux;
78 inline u16 get_portmux(unsigned short portno)
82 pmux = gpio_array[gpio_bank(portno)]->port_mux;
84 return (pmux >> (2 * gpio_sub_n(portno)) & 0x3);
87 static void port_setup(unsigned short gpio, unsigned short usage)
89 if (usage == GPIO_USAGE) {
90 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
92 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
96 static int __init bfin_gpio_init(void)
99 str_ident = kzalloc(RESOURCE_LABEL_SIZE * 256, GFP_KERNEL);
103 printk(KERN_INFO "Blackfin GPIO Controller\n");
108 arch_initcall(bfin_gpio_init);
110 static void set_label(unsigned short ident, const char *label)
113 if (label && str_ident) {
114 strncpy(str_ident + ident * RESOURCE_LABEL_SIZE, label,
115 RESOURCE_LABEL_SIZE);
116 str_ident[ident * RESOURCE_LABEL_SIZE +
117 RESOURCE_LABEL_SIZE - 1] = 0;
121 static char *get_label(unsigned short ident)
126 return (str_ident[ident * RESOURCE_LABEL_SIZE] ?
127 (str_ident + ident * RESOURCE_LABEL_SIZE) : "UNKNOWN");
130 static int cmp_label(unsigned short ident, const char *label)
132 if (label && str_ident)
133 return strncmp(str_ident + ident * RESOURCE_LABEL_SIZE,
134 label, strlen(label));
139 int peripheral_request(unsigned short per, const char *label)
142 unsigned short ident = P_IDENT(per);
145 * Don't cares are pins with only one dedicated function
148 if (per & P_DONTCARE)
151 if (!(per & P_DEFINED))
154 if (check_gpio(ident) < 0)
157 local_irq_save(flags);
159 if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
161 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
162 __FUNCTION__, ident, get_label(ident));
164 local_irq_restore(flags);
168 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
170 u16 funct = get_portmux(ident);
173 * Pin functions like AMC address strobes my
174 * be requested and used by several drivers
177 if (!((per & P_MAYSHARE) && (funct == P_FUNCT2MUX(per)))) {
180 * Allow that the identical pin function can
181 * be requested from the same driver twice
184 if (cmp_label(ident, label) == 0)
188 "%s: Peripheral %d function %d is already reserved by %s !\n",
189 __FUNCTION__, ident, P_FUNCT2MUX(per), get_label(ident));
191 local_irq_restore(flags);
197 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
199 portmux_setup(ident, P_FUNCT2MUX(per));
200 port_setup(ident, PERIPHERAL_USAGE);
202 local_irq_restore(flags);
203 set_label(ident, label);
207 EXPORT_SYMBOL(peripheral_request);
209 int peripheral_request_list(unsigned short per[], const char *label)
214 for (cnt = 0; per[cnt] != 0; cnt++) {
215 ret = peripheral_request(per[cnt], label);
222 EXPORT_SYMBOL(peripheral_request_list);
224 void peripheral_free(unsigned short per)
227 unsigned short ident = P_IDENT(per);
229 if (per & P_DONTCARE)
232 if (!(per & P_DEFINED))
235 if (check_gpio(ident) < 0)
238 local_irq_save(flags);
240 if (unlikely(!(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident)))) {
241 local_irq_restore(flags);
245 if (!(per & P_MAYSHARE)) {
246 port_setup(ident, GPIO_USAGE);
249 reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
251 local_irq_restore(flags);
253 EXPORT_SYMBOL(peripheral_free);
255 void peripheral_free_list(unsigned short per[])
259 for (cnt = 0; per[cnt] != 0; cnt++) {
260 peripheral_free(per[cnt]);
264 EXPORT_SYMBOL(peripheral_free_list);
266 /***********************************************************
268 * FUNCTIONS: Blackfin GPIO Driver
271 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
274 * DESCRIPTION: Blackfin GPIO Driver API
277 *************************************************************
278 * MODIFICATION HISTORY :
279 **************************************************************/
281 int gpio_request(unsigned short gpio, const char *label)
285 if (check_gpio(gpio) < 0)
288 local_irq_save(flags);
290 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
291 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
292 gpio, get_label(gpio));
294 local_irq_restore(flags);
298 if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
300 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
301 gpio, get_label(gpio));
303 local_irq_restore(flags);
307 reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
309 local_irq_restore(flags);
311 port_setup(gpio, GPIO_USAGE);
312 set_label(gpio, label);
316 EXPORT_SYMBOL(gpio_request);
318 void gpio_free(unsigned short gpio)
322 if (check_gpio(gpio) < 0)
325 local_irq_save(flags);
327 if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
328 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't reserved!\n", gpio);
330 local_irq_restore(flags);
334 reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
336 local_irq_restore(flags);
338 EXPORT_SYMBOL(gpio_free);
340 void gpio_direction_input(unsigned short gpio)
344 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
346 local_irq_save(flags);
347 gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
348 gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
349 local_irq_restore(flags);
351 EXPORT_SYMBOL(gpio_direction_input);
353 void gpio_direction_output(unsigned short gpio)
357 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
359 local_irq_save(flags);
360 gpio_array[gpio_bank(gpio)]->port_inen &= ~gpio_bit(gpio);
361 gpio_array[gpio_bank(gpio)]->port_dir_set = gpio_bit(gpio);
362 local_irq_restore(flags);
364 EXPORT_SYMBOL(gpio_direction_output);
366 void gpio_set_value(unsigned short gpio, unsigned short arg)
369 gpio_array[gpio_bank(gpio)]->port_set = gpio_bit(gpio);
371 gpio_array[gpio_bank(gpio)]->port_clear = gpio_bit(gpio);
374 EXPORT_SYMBOL(gpio_set_value);
376 unsigned short gpio_get_value(unsigned short gpio)
378 return (1 & (gpio_array[gpio_bank(gpio)]->port_data >> gpio_sub_n(gpio)));
380 EXPORT_SYMBOL(gpio_get_value);