2 * GPIO Abstraction Layer
4 * Copyright 2006-2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later
9 #include <linux/delay.h>
10 #include <linux/module.h>
11 #include <linux/err.h>
12 #include <linux/proc_fs.h>
13 #include <linux/seq_file.h>
14 #include <asm/blackfin.h>
16 #include <asm/portmux.h>
17 #include <linux/irq.h>
18 #include <asm/irq_handler.h>
20 #if ANOMALY_05000311 || ANOMALY_05000323
23 AWA_data_clear = SYSCR,
26 AWA_maska = BFIN_UART_SCR,
27 AWA_maska_clear = BFIN_UART_SCR,
28 AWA_maska_set = BFIN_UART_SCR,
29 AWA_maska_toggle = BFIN_UART_SCR,
30 AWA_maskb = BFIN_UART_GCTL,
31 AWA_maskb_clear = BFIN_UART_GCTL,
32 AWA_maskb_set = BFIN_UART_GCTL,
33 AWA_maskb_toggle = BFIN_UART_GCTL,
34 AWA_dir = SPORT1_STAT,
35 AWA_polar = SPORT1_STAT,
36 AWA_edge = SPORT1_STAT,
37 AWA_both = SPORT1_STAT,
39 AWA_inen = TIMER_ENABLE,
40 #elif ANOMALY_05000323
41 AWA_inen = DMA1_1_CONFIG,
44 /* Anomaly Workaround */
45 #define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
47 #define AWA_DUMMY_READ(...) do { } while (0)
50 static struct gpio_port_t * const gpio_array[] = {
51 #if defined(BF533_FAMILY) || defined(BF538_FAMILY)
52 (struct gpio_port_t *) FIO_FLAG_D,
53 #elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
54 (struct gpio_port_t *) PORTFIO,
55 (struct gpio_port_t *) PORTGIO,
56 (struct gpio_port_t *) PORTHIO,
57 #elif defined(BF561_FAMILY)
58 (struct gpio_port_t *) FIO0_FLAG_D,
59 (struct gpio_port_t *) FIO1_FLAG_D,
60 (struct gpio_port_t *) FIO2_FLAG_D,
61 #elif defined(CONFIG_BF54x)
62 (struct gpio_port_t *)PORTA_FER,
63 (struct gpio_port_t *)PORTB_FER,
64 (struct gpio_port_t *)PORTC_FER,
65 (struct gpio_port_t *)PORTD_FER,
66 (struct gpio_port_t *)PORTE_FER,
67 (struct gpio_port_t *)PORTF_FER,
68 (struct gpio_port_t *)PORTG_FER,
69 (struct gpio_port_t *)PORTH_FER,
70 (struct gpio_port_t *)PORTI_FER,
71 (struct gpio_port_t *)PORTJ_FER,
73 # error no gpio arrays defined
77 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
78 static unsigned short * const port_fer[] = {
79 (unsigned short *) PORTF_FER,
80 (unsigned short *) PORTG_FER,
81 (unsigned short *) PORTH_FER,
84 # if !defined(BF537_FAMILY)
85 static unsigned short * const port_mux[] = {
86 (unsigned short *) PORTF_MUX,
87 (unsigned short *) PORTG_MUX,
88 (unsigned short *) PORTH_MUX,
92 u8 pmux_offset[][16] = {
93 # if defined(CONFIG_BF52x)
94 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
95 { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
96 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
97 # elif defined(CONFIG_BF51x)
98 { 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
99 { 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
100 { 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
105 #elif defined(BF538_FAMILY)
106 static unsigned short * const port_fer[] = {
107 (unsigned short *) PORTCIO_FER,
108 (unsigned short *) PORTDIO_FER,
109 (unsigned short *) PORTEIO_FER,
113 #define RESOURCE_LABEL_SIZE 16
115 static struct str_ident {
116 char name[RESOURCE_LABEL_SIZE];
117 } str_ident[MAX_RESOURCES];
119 #if defined(CONFIG_PM)
120 static struct gpio_port_s gpio_bank_saved[GPIO_BANK_NUM];
123 static void gpio_error(unsigned gpio)
125 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
128 static void set_label(unsigned short ident, const char *label)
131 strncpy(str_ident[ident].name, label,
132 RESOURCE_LABEL_SIZE);
133 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
137 static char *get_label(unsigned short ident)
139 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
142 static int cmp_label(unsigned short ident, const char *label)
146 printk(KERN_ERR "Please provide none-null label\n");
150 return strcmp(str_ident[ident].name, label);
155 #define map_entry(m, i) reserved_##m##_map[gpio_bank(i)]
156 #define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
157 #define reserve(m, i) (map_entry(m, i) |= gpio_bit(i))
158 #define unreserve(m, i) (map_entry(m, i) &= ~gpio_bit(i))
159 #define DECLARE_RESERVED_MAP(m, c) static unsigned short reserved_##m##_map[c]
161 DECLARE_RESERVED_MAP(gpio, GPIO_BANK_NUM);
162 DECLARE_RESERVED_MAP(peri, DIV_ROUND_UP(MAX_RESOURCES, GPIO_BANKSIZE));
163 DECLARE_RESERVED_MAP(gpio_irq, GPIO_BANK_NUM);
165 inline int check_gpio(unsigned gpio)
167 #if defined(CONFIG_BF54x)
168 if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15
169 || gpio == GPIO_PH14 || gpio == GPIO_PH15
170 || gpio == GPIO_PJ14 || gpio == GPIO_PJ15)
173 if (gpio >= MAX_BLACKFIN_GPIOS)
178 static void port_setup(unsigned gpio, unsigned short usage)
180 #if defined(BF538_FAMILY)
182 * BF538/9 Port C,D and E are special.
183 * Inverted PORT_FER polarity on CDE and no PORF_FER on F
184 * Regular PORT F GPIOs are handled here, CDE are exclusively
188 if (gpio < MAX_BLACKFIN_GPIOS || gpio >= MAX_RESOURCES)
191 gpio -= MAX_BLACKFIN_GPIOS;
193 if (usage == GPIO_USAGE)
194 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
196 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
201 if (check_gpio(gpio))
204 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
205 if (usage == GPIO_USAGE)
206 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
208 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
210 #elif defined(CONFIG_BF54x)
211 if (usage == GPIO_USAGE)
212 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
214 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
220 static const s8 port_mux[] = {
229 [GPIO_PF8 ... GPIO_PF15] = -1,
230 [GPIO_PG0 ... GPIO_PG7] = -1,
239 [GPIO_PH0 ... GPIO_PH15] = -1,
240 [PORT_PJ0 ... PORT_PJ3] = -1,
243 [PORT_PJ6 ... PORT_PJ9] = -1,
248 static int portmux_group_check(unsigned short per)
250 u16 ident = P_IDENT(per);
251 u16 function = P_FUNCT2MUX(per);
252 s8 offset = port_mux[ident];
258 pmux = bfin_read_PORT_MUX();
259 for (m = 0; m < ARRAY_SIZE(port_mux); ++m) {
262 if (port_mux[m] != offset)
264 if (!is_reserved(peri, m, 1))
268 pfunc = (pmux >> offset) & 3;
270 pfunc = (pmux >> offset) & 1;
271 if (pfunc != function) {
272 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
273 ident, function, m, pfunc);
281 static void portmux_setup(unsigned short per)
283 u16 ident = P_IDENT(per);
284 u16 function = P_FUNCT2MUX(per);
285 s8 offset = port_mux[ident];
291 pmux = bfin_read_PORT_MUX();
293 pmux &= ~(1 << offset);
296 pmux |= (function << offset);
297 bfin_write_PORT_MUX(pmux);
299 #elif defined(CONFIG_BF54x)
300 inline void portmux_setup(unsigned short per)
302 u16 ident = P_IDENT(per);
303 u16 function = P_FUNCT2MUX(per);
306 pmux = gpio_array[gpio_bank(ident)]->port_mux;
308 pmux &= ~(0x3 << (2 * gpio_sub_n(ident)));
309 pmux |= (function & 0x3) << (2 * gpio_sub_n(ident));
311 gpio_array[gpio_bank(ident)]->port_mux = pmux;
314 inline u16 get_portmux(unsigned short per)
316 u16 ident = P_IDENT(per);
317 u32 pmux = gpio_array[gpio_bank(ident)]->port_mux;
318 return (pmux >> (2 * gpio_sub_n(ident)) & 0x3);
320 static int portmux_group_check(unsigned short per)
324 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
325 static int portmux_group_check(unsigned short per)
327 u16 ident = P_IDENT(per);
328 u16 function = P_FUNCT2MUX(per);
329 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
330 u16 pin, gpiopin, pfunc;
332 for (pin = 0; pin < GPIO_BANKSIZE; ++pin) {
333 if (offset != pmux_offset[gpio_bank(ident)][pin])
336 gpiopin = gpio_bank(ident) * GPIO_BANKSIZE + pin;
337 if (gpiopin == ident)
339 if (!is_reserved(peri, gpiopin, 1))
342 pfunc = *port_mux[gpio_bank(ident)];
343 pfunc = (pfunc >> offset) & 3;
344 if (pfunc != function) {
345 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
346 ident, function, gpiopin, pfunc);
354 inline void portmux_setup(unsigned short per)
356 u16 ident = P_IDENT(per);
357 u16 function = P_FUNCT2MUX(per);
358 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
361 pmux = *port_mux[gpio_bank(ident)];
362 if (((pmux >> offset) & 3) == function)
364 pmux &= ~(3 << offset);
365 pmux |= (function & 3) << offset;
366 *port_mux[gpio_bank(ident)] = pmux;
370 # define portmux_setup(...) do { } while (0)
371 static int portmux_group_check(unsigned short per)
378 /***********************************************************
380 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
383 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
386 * DESCRIPTION: These functions abstract direct register access
387 * to Blackfin processor General Purpose
390 * CAUTION: These functions do not belong to the GPIO Driver API
391 *************************************************************
392 * MODIFICATION HISTORY :
393 **************************************************************/
395 /* Set a specific bit */
397 #define SET_GPIO(name) \
398 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
400 unsigned long flags; \
401 flags = hard_local_irq_save(); \
403 gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
405 gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
406 AWA_DUMMY_READ(name); \
407 hard_local_irq_restore(flags); \
409 EXPORT_SYMBOL(set_gpio_ ## name);
411 SET_GPIO(dir) /* set_gpio_dir() */
412 SET_GPIO(inen) /* set_gpio_inen() */
413 SET_GPIO(polar) /* set_gpio_polar() */
414 SET_GPIO(edge) /* set_gpio_edge() */
415 SET_GPIO(both) /* set_gpio_both() */
418 #define SET_GPIO_SC(name) \
419 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
421 unsigned long flags; \
422 if (ANOMALY_05000311 || ANOMALY_05000323) \
423 flags = hard_local_irq_save(); \
425 gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
427 gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
428 if (ANOMALY_05000311 || ANOMALY_05000323) { \
429 AWA_DUMMY_READ(name); \
430 hard_local_irq_restore(flags); \
433 EXPORT_SYMBOL(set_gpio_ ## name);
439 void set_gpio_toggle(unsigned gpio)
442 if (ANOMALY_05000311 || ANOMALY_05000323)
443 flags = hard_local_irq_save();
444 gpio_array[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
445 if (ANOMALY_05000311 || ANOMALY_05000323) {
446 AWA_DUMMY_READ(toggle);
447 hard_local_irq_restore(flags);
450 EXPORT_SYMBOL(set_gpio_toggle);
453 /*Set current PORT date (16-bit word)*/
455 #define SET_GPIO_P(name) \
456 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
458 unsigned long flags; \
459 if (ANOMALY_05000311 || ANOMALY_05000323) \
460 flags = hard_local_irq_save(); \
461 gpio_array[gpio_bank(gpio)]->name = arg; \
462 if (ANOMALY_05000311 || ANOMALY_05000323) { \
463 AWA_DUMMY_READ(name); \
464 hard_local_irq_restore(flags); \
467 EXPORT_SYMBOL(set_gpiop_ ## name);
478 /* Get a specific bit */
479 #define GET_GPIO(name) \
480 unsigned short get_gpio_ ## name(unsigned gpio) \
482 unsigned long flags; \
483 unsigned short ret; \
484 if (ANOMALY_05000311 || ANOMALY_05000323) \
485 flags = hard_local_irq_save(); \
486 ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
487 if (ANOMALY_05000311 || ANOMALY_05000323) { \
488 AWA_DUMMY_READ(name); \
489 hard_local_irq_restore(flags); \
493 EXPORT_SYMBOL(get_gpio_ ## name);
504 /*Get current PORT date (16-bit word)*/
506 #define GET_GPIO_P(name) \
507 unsigned short get_gpiop_ ## name(unsigned gpio) \
509 unsigned long flags; \
510 unsigned short ret; \
511 if (ANOMALY_05000311 || ANOMALY_05000323) \
512 flags = hard_local_irq_save(); \
513 ret = (gpio_array[gpio_bank(gpio)]->name); \
514 if (ANOMALY_05000311 || ANOMALY_05000323) { \
515 AWA_DUMMY_READ(name); \
516 hard_local_irq_restore(flags); \
520 EXPORT_SYMBOL(get_gpiop_ ## name);
533 DECLARE_RESERVED_MAP(wakeup, GPIO_BANK_NUM);
535 static const unsigned int sic_iwr_irqs[] = {
536 #if defined(BF533_FAMILY)
538 #elif defined(BF537_FAMILY)
539 IRQ_PF_INTB_WATCH, IRQ_PORTG_INTB, IRQ_PH_INTB_MAC_TX
540 #elif defined(BF538_FAMILY)
542 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
543 IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB
544 #elif defined(BF561_FAMILY)
545 IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB
547 # error no SIC_IWR defined
551 /***********************************************************
553 * FUNCTIONS: Blackfin PM Setup API
556 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
564 * DESCRIPTION: Blackfin PM Driver API
567 *************************************************************
568 * MODIFICATION HISTORY :
569 **************************************************************/
570 int gpio_pm_wakeup_ctrl(unsigned gpio, unsigned ctrl)
574 if (check_gpio(gpio) < 0)
577 flags = hard_local_irq_save();
579 reserve(wakeup, gpio);
581 unreserve(wakeup, gpio);
583 set_gpio_maskb(gpio, ctrl);
584 hard_local_irq_restore(flags);
589 int bfin_pm_standby_ctrl(unsigned ctrl)
593 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
594 mask = map_entry(wakeup, i);
598 bfin_internal_set_wake(sic_iwr_irqs[bank], ctrl);
603 void bfin_gpio_pm_hibernate_suspend(void)
607 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
610 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
611 gpio_bank_saved[bank].fer = *port_fer[bank];
612 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
613 gpio_bank_saved[bank].mux = *port_mux[bank];
616 gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
619 gpio_bank_saved[bank].data = gpio_array[bank]->data;
620 gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
621 gpio_bank_saved[bank].polar = gpio_array[bank]->polar;
622 gpio_bank_saved[bank].dir = gpio_array[bank]->dir;
623 gpio_bank_saved[bank].edge = gpio_array[bank]->edge;
624 gpio_bank_saved[bank].both = gpio_array[bank]->both;
625 gpio_bank_saved[bank].maska = gpio_array[bank]->maska;
628 AWA_DUMMY_READ(maska);
631 void bfin_gpio_pm_hibernate_restore(void)
635 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
638 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
639 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
640 *port_mux[bank] = gpio_bank_saved[bank].mux;
643 bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
645 *port_fer[bank] = gpio_bank_saved[bank].fer;
647 gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
648 gpio_array[bank]->data_set = gpio_bank_saved[bank].data
649 & gpio_bank_saved[bank].dir;
650 gpio_array[bank]->dir = gpio_bank_saved[bank].dir;
651 gpio_array[bank]->polar = gpio_bank_saved[bank].polar;
652 gpio_array[bank]->edge = gpio_bank_saved[bank].edge;
653 gpio_array[bank]->both = gpio_bank_saved[bank].both;
654 gpio_array[bank]->maska = gpio_bank_saved[bank].maska;
656 AWA_DUMMY_READ(maska);
661 #else /* CONFIG_BF54x */
664 int bfin_pm_standby_ctrl(unsigned ctrl)
669 void bfin_gpio_pm_hibernate_suspend(void)
673 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
676 gpio_bank_saved[bank].fer = gpio_array[bank]->port_fer;
677 gpio_bank_saved[bank].mux = gpio_array[bank]->port_mux;
678 gpio_bank_saved[bank].data = gpio_array[bank]->data;
679 gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
680 gpio_bank_saved[bank].dir = gpio_array[bank]->dir_set;
684 void bfin_gpio_pm_hibernate_restore(void)
688 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
691 gpio_array[bank]->port_mux = gpio_bank_saved[bank].mux;
692 gpio_array[bank]->port_fer = gpio_bank_saved[bank].fer;
693 gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
694 gpio_array[bank]->dir_set = gpio_bank_saved[bank].dir;
695 gpio_array[bank]->data_set = gpio_bank_saved[bank].data
696 | gpio_bank_saved[bank].dir;
701 unsigned short get_gpio_dir(unsigned gpio)
703 return (0x01 & (gpio_array[gpio_bank(gpio)]->dir_clear >> gpio_sub_n(gpio)));
705 EXPORT_SYMBOL(get_gpio_dir);
707 #endif /* CONFIG_BF54x */
709 /***********************************************************
711 * FUNCTIONS: Blackfin Peripheral Resource Allocation
715 * per Peripheral Identifier
718 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
721 *************************************************************
722 * MODIFICATION HISTORY :
723 **************************************************************/
725 int peripheral_request(unsigned short per, const char *label)
728 unsigned short ident = P_IDENT(per);
731 * Don't cares are pins with only one dedicated function
734 if (per & P_DONTCARE)
737 if (!(per & P_DEFINED))
740 BUG_ON(ident >= MAX_RESOURCES);
742 flags = hard_local_irq_save();
744 /* If a pin can be muxed as either GPIO or peripheral, make
745 * sure it is not already a GPIO pin when we request it.
747 if (unlikely(!check_gpio(ident) && is_reserved(gpio, ident, 1))) {
748 if (system_state == SYSTEM_BOOTING)
751 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
752 __func__, ident, get_label(ident));
753 hard_local_irq_restore(flags);
757 if (unlikely(is_reserved(peri, ident, 1))) {
760 * Pin functions like AMC address strobes my
761 * be requested and used by several drivers
765 if (!((per & P_MAYSHARE) && get_portmux(per) == P_FUNCT2MUX(per))) {
767 if (!(per & P_MAYSHARE)) {
770 * Allow that the identical pin function can
771 * be requested from the same driver twice
774 if (cmp_label(ident, label) == 0)
777 if (system_state == SYSTEM_BOOTING)
780 "%s: Peripheral %d function %d is already reserved by %s !\n",
781 __func__, ident, P_FUNCT2MUX(per), get_label(ident));
782 hard_local_irq_restore(flags);
787 if (unlikely(portmux_group_check(per))) {
788 hard_local_irq_restore(flags);
792 reserve(peri, ident);
795 port_setup(ident, PERIPHERAL_USAGE);
797 hard_local_irq_restore(flags);
798 set_label(ident, label);
802 EXPORT_SYMBOL(peripheral_request);
804 int peripheral_request_list(const unsigned short per[], const char *label)
809 for (cnt = 0; per[cnt] != 0; cnt++) {
811 ret = peripheral_request(per[cnt], label);
814 for ( ; cnt > 0; cnt--)
815 peripheral_free(per[cnt - 1]);
823 EXPORT_SYMBOL(peripheral_request_list);
825 void peripheral_free(unsigned short per)
828 unsigned short ident = P_IDENT(per);
830 if (per & P_DONTCARE)
833 if (!(per & P_DEFINED))
836 flags = hard_local_irq_save();
838 if (unlikely(!is_reserved(peri, ident, 0))) {
839 hard_local_irq_restore(flags);
843 if (!(per & P_MAYSHARE))
844 port_setup(ident, GPIO_USAGE);
846 unreserve(peri, ident);
848 set_label(ident, "free");
850 hard_local_irq_restore(flags);
852 EXPORT_SYMBOL(peripheral_free);
854 void peripheral_free_list(const unsigned short per[])
857 for (cnt = 0; per[cnt] != 0; cnt++)
858 peripheral_free(per[cnt]);
860 EXPORT_SYMBOL(peripheral_free_list);
862 /***********************************************************
864 * FUNCTIONS: Blackfin GPIO Driver
867 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
870 * DESCRIPTION: Blackfin GPIO Driver API
873 *************************************************************
874 * MODIFICATION HISTORY :
875 **************************************************************/
877 int bfin_gpio_request(unsigned gpio, const char *label)
881 if (check_gpio(gpio) < 0)
884 flags = hard_local_irq_save();
887 * Allow that the identical GPIO can
888 * be requested from the same driver twice
889 * Do nothing and return -
892 if (cmp_label(gpio, label) == 0) {
893 hard_local_irq_restore(flags);
897 if (unlikely(is_reserved(gpio, gpio, 1))) {
898 if (system_state == SYSTEM_BOOTING)
900 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
901 gpio, get_label(gpio));
902 hard_local_irq_restore(flags);
905 if (unlikely(is_reserved(peri, gpio, 1))) {
906 if (system_state == SYSTEM_BOOTING)
909 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
910 gpio, get_label(gpio));
911 hard_local_irq_restore(flags);
914 if (unlikely(is_reserved(gpio_irq, gpio, 1))) {
915 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved as gpio-irq!"
916 " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio);
919 else { /* Reset POLAR setting when acquiring a gpio for the first time */
920 set_gpio_polar(gpio, 0);
925 set_label(gpio, label);
927 hard_local_irq_restore(flags);
929 port_setup(gpio, GPIO_USAGE);
933 EXPORT_SYMBOL(bfin_gpio_request);
935 void bfin_gpio_free(unsigned gpio)
939 if (check_gpio(gpio) < 0)
944 flags = hard_local_irq_save();
946 if (unlikely(!is_reserved(gpio, gpio, 0))) {
947 if (system_state == SYSTEM_BOOTING)
950 hard_local_irq_restore(flags);
954 unreserve(gpio, gpio);
956 set_label(gpio, "free");
958 hard_local_irq_restore(flags);
960 EXPORT_SYMBOL(bfin_gpio_free);
962 #ifdef BFIN_SPECIAL_GPIO_BANKS
963 DECLARE_RESERVED_MAP(special_gpio, gpio_bank(MAX_RESOURCES));
965 int bfin_special_gpio_request(unsigned gpio, const char *label)
969 flags = hard_local_irq_save();
972 * Allow that the identical GPIO can
973 * be requested from the same driver twice
974 * Do nothing and return -
977 if (cmp_label(gpio, label) == 0) {
978 hard_local_irq_restore(flags);
982 if (unlikely(is_reserved(special_gpio, gpio, 1))) {
983 hard_local_irq_restore(flags);
984 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
985 gpio, get_label(gpio));
989 if (unlikely(is_reserved(peri, gpio, 1))) {
990 hard_local_irq_restore(flags);
992 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
993 gpio, get_label(gpio));
998 reserve(special_gpio, gpio);
1001 set_label(gpio, label);
1002 hard_local_irq_restore(flags);
1003 port_setup(gpio, GPIO_USAGE);
1007 EXPORT_SYMBOL(bfin_special_gpio_request);
1009 void bfin_special_gpio_free(unsigned gpio)
1011 unsigned long flags;
1015 flags = hard_local_irq_save();
1017 if (unlikely(!is_reserved(special_gpio, gpio, 0))) {
1019 hard_local_irq_restore(flags);
1023 unreserve(special_gpio, gpio);
1024 unreserve(peri, gpio);
1025 set_label(gpio, "free");
1026 hard_local_irq_restore(flags);
1028 EXPORT_SYMBOL(bfin_special_gpio_free);
1032 int bfin_gpio_irq_request(unsigned gpio, const char *label)
1034 unsigned long flags;
1036 if (check_gpio(gpio) < 0)
1039 flags = hard_local_irq_save();
1041 if (unlikely(is_reserved(peri, gpio, 1))) {
1042 if (system_state == SYSTEM_BOOTING)
1045 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1046 gpio, get_label(gpio));
1047 hard_local_irq_restore(flags);
1050 if (unlikely(is_reserved(gpio, gpio, 1)))
1051 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved by %s! "
1052 "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
1053 gpio, get_label(gpio));
1055 reserve(gpio_irq, gpio);
1056 set_label(gpio, label);
1058 hard_local_irq_restore(flags);
1060 port_setup(gpio, GPIO_USAGE);
1065 void bfin_gpio_irq_free(unsigned gpio)
1067 unsigned long flags;
1069 if (check_gpio(gpio) < 0)
1072 flags = hard_local_irq_save();
1074 if (unlikely(!is_reserved(gpio_irq, gpio, 0))) {
1075 if (system_state == SYSTEM_BOOTING)
1078 hard_local_irq_restore(flags);
1082 unreserve(gpio_irq, gpio);
1084 set_label(gpio, "free");
1086 hard_local_irq_restore(flags);
1089 static inline void __bfin_gpio_direction_input(unsigned gpio)
1092 gpio_array[gpio_bank(gpio)]->dir_clear = gpio_bit(gpio);
1094 gpio_array[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1096 gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
1099 int bfin_gpio_direction_input(unsigned gpio)
1101 unsigned long flags;
1103 if (unlikely(!is_reserved(gpio, gpio, 0))) {
1108 flags = hard_local_irq_save();
1109 __bfin_gpio_direction_input(gpio);
1110 AWA_DUMMY_READ(inen);
1111 hard_local_irq_restore(flags);
1115 EXPORT_SYMBOL(bfin_gpio_direction_input);
1117 void bfin_gpio_irq_prepare(unsigned gpio)
1120 unsigned long flags;
1123 port_setup(gpio, GPIO_USAGE);
1126 flags = hard_local_irq_save();
1127 __bfin_gpio_direction_input(gpio);
1128 hard_local_irq_restore(flags);
1132 void bfin_gpio_set_value(unsigned gpio, int arg)
1135 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1137 gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1139 EXPORT_SYMBOL(bfin_gpio_set_value);
1141 int bfin_gpio_direction_output(unsigned gpio, int value)
1143 unsigned long flags;
1145 if (unlikely(!is_reserved(gpio, gpio, 0))) {
1150 flags = hard_local_irq_save();
1152 gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
1153 gpio_set_value(gpio, value);
1155 gpio_array[gpio_bank(gpio)]->dir_set = gpio_bit(gpio);
1157 gpio_array[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
1160 AWA_DUMMY_READ(dir);
1161 hard_local_irq_restore(flags);
1165 EXPORT_SYMBOL(bfin_gpio_direction_output);
1167 int bfin_gpio_get_value(unsigned gpio)
1170 return (1 & (gpio_array[gpio_bank(gpio)]->data >> gpio_sub_n(gpio)));
1172 unsigned long flags;
1174 if (unlikely(get_gpio_edge(gpio))) {
1176 flags = hard_local_irq_save();
1177 set_gpio_edge(gpio, 0);
1178 ret = get_gpio_data(gpio);
1179 set_gpio_edge(gpio, 1);
1180 hard_local_irq_restore(flags);
1183 return get_gpio_data(gpio);
1186 EXPORT_SYMBOL(bfin_gpio_get_value);
1188 /* If we are booting from SPI and our board lacks a strong enough pull up,
1189 * the core can reset and execute the bootrom faster than the resistor can
1190 * pull the signal logically high. To work around this (common) error in
1191 * board design, we explicitly set the pin back to GPIO mode, force /CS
1192 * high, and wait for the electrons to do their thing.
1194 * This function only makes sense to be called from reset code, but it
1195 * lives here as we need to force all the GPIO states w/out going through
1196 * BUG() checks and such.
1198 void bfin_reset_boot_spi_cs(unsigned short pin)
1200 unsigned short gpio = P_IDENT(pin);
1201 port_setup(gpio, GPIO_USAGE);
1202 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1203 AWA_DUMMY_READ(data_set);
1207 #if defined(CONFIG_PROC_FS)
1208 static int gpio_proc_show(struct seq_file *m, void *v)
1212 for (c = 0; c < MAX_RESOURCES; c++) {
1213 irq = is_reserved(gpio_irq, c, 1);
1214 gpio = is_reserved(gpio, c, 1);
1215 if (!check_gpio(c) && (gpio || irq))
1216 seq_printf(m, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c,
1217 get_label(c), (gpio && irq) ? " *" : "",
1218 get_gpio_dir(c) ? "OUTPUT" : "INPUT");
1219 else if (is_reserved(peri, c, 1))
1220 seq_printf(m, "GPIO_%d: \t%s \t\tPeripheral\n", c, get_label(c));
1228 static int gpio_proc_open(struct inode *inode, struct file *file)
1230 return single_open(file, gpio_proc_show, NULL);
1233 static const struct file_operations gpio_proc_ops = {
1234 .open = gpio_proc_open,
1236 .llseek = seq_lseek,
1237 .release = single_release,
1240 static __init int gpio_register_proc(void)
1242 struct proc_dir_entry *proc_gpio;
1244 proc_gpio = proc_create("gpio", S_IRUGO, NULL, &gpio_proc_ops);
1245 return proc_gpio != NULL;
1247 __initcall(gpio_register_proc);
1250 #ifdef CONFIG_GPIOLIB
1251 static int bfin_gpiolib_direction_input(struct gpio_chip *chip, unsigned gpio)
1253 return bfin_gpio_direction_input(gpio);
1256 static int bfin_gpiolib_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
1258 return bfin_gpio_direction_output(gpio, level);
1261 static int bfin_gpiolib_get_value(struct gpio_chip *chip, unsigned gpio)
1263 return bfin_gpio_get_value(gpio);
1266 static void bfin_gpiolib_set_value(struct gpio_chip *chip, unsigned gpio, int value)
1268 return bfin_gpio_set_value(gpio, value);
1271 static int bfin_gpiolib_gpio_request(struct gpio_chip *chip, unsigned gpio)
1273 return bfin_gpio_request(gpio, chip->label);
1276 static void bfin_gpiolib_gpio_free(struct gpio_chip *chip, unsigned gpio)
1278 return bfin_gpio_free(gpio);
1281 static int bfin_gpiolib_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
1283 return gpio + GPIO_IRQ_BASE;
1286 static struct gpio_chip bfin_chip = {
1287 .label = "BFIN-GPIO",
1288 .direction_input = bfin_gpiolib_direction_input,
1289 .get = bfin_gpiolib_get_value,
1290 .direction_output = bfin_gpiolib_direction_output,
1291 .set = bfin_gpiolib_set_value,
1292 .request = bfin_gpiolib_gpio_request,
1293 .free = bfin_gpiolib_gpio_free,
1294 .to_irq = bfin_gpiolib_gpio_to_irq,
1296 .ngpio = MAX_BLACKFIN_GPIOS,
1299 static int __init bfin_gpiolib_setup(void)
1301 return gpiochip_add(&bfin_chip);
1303 arch_initcall(bfin_gpiolib_setup);