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 <linux/gpio.h>
15 #include <linux/irq.h>
17 #if ANOMALY_05000311 || ANOMALY_05000323
20 AWA_data_clear
= SYSCR
,
23 AWA_maska
= BFIN_UART_SCR
,
24 AWA_maska_clear
= BFIN_UART_SCR
,
25 AWA_maska_set
= BFIN_UART_SCR
,
26 AWA_maska_toggle
= BFIN_UART_SCR
,
27 AWA_maskb
= BFIN_UART_GCTL
,
28 AWA_maskb_clear
= BFIN_UART_GCTL
,
29 AWA_maskb_set
= BFIN_UART_GCTL
,
30 AWA_maskb_toggle
= BFIN_UART_GCTL
,
31 AWA_dir
= SPORT1_STAT
,
32 AWA_polar
= SPORT1_STAT
,
33 AWA_edge
= SPORT1_STAT
,
34 AWA_both
= SPORT1_STAT
,
36 AWA_inen
= TIMER_ENABLE
,
37 #elif ANOMALY_05000323
38 AWA_inen
= DMA1_1_CONFIG
,
41 /* Anomaly Workaround */
42 #define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
44 #define AWA_DUMMY_READ(...) do { } while (0)
47 static struct gpio_port_t
* const gpio_array
[] = {
48 #if defined(BF533_FAMILY) || defined(BF538_FAMILY)
49 (struct gpio_port_t
*) FIO_FLAG_D
,
50 #elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
51 (struct gpio_port_t
*) PORTFIO
,
52 (struct gpio_port_t
*) PORTGIO
,
53 (struct gpio_port_t
*) PORTHIO
,
54 #elif defined(BF561_FAMILY)
55 (struct gpio_port_t
*) FIO0_FLAG_D
,
56 (struct gpio_port_t
*) FIO1_FLAG_D
,
57 (struct gpio_port_t
*) FIO2_FLAG_D
,
59 # error no gpio arrays defined
63 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
64 static unsigned short * const port_fer
[] = {
65 (unsigned short *) PORTF_FER
,
66 (unsigned short *) PORTG_FER
,
67 (unsigned short *) PORTH_FER
,
70 # if !defined(BF537_FAMILY)
71 static unsigned short * const port_mux
[] = {
72 (unsigned short *) PORTF_MUX
,
73 (unsigned short *) PORTG_MUX
,
74 (unsigned short *) PORTH_MUX
,
78 u8 pmux_offset
[][16] = {
79 # if defined(CONFIG_BF52x)
80 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
81 { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
82 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
83 # elif defined(CONFIG_BF51x)
84 { 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
85 { 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
86 { 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
91 #elif defined(BF538_FAMILY)
92 static unsigned short * const port_fer
[] = {
93 (unsigned short *) PORTCIO_FER
,
94 (unsigned short *) PORTDIO_FER
,
95 (unsigned short *) PORTEIO_FER
,
99 #define RESOURCE_LABEL_SIZE 16
101 static struct str_ident
{
102 char name
[RESOURCE_LABEL_SIZE
];
103 } str_ident
[MAX_RESOURCES
];
105 #if defined(CONFIG_PM)
106 static struct gpio_port_s gpio_bank_saved
[GPIO_BANK_NUM
];
108 static unsigned short port_fer_saved
[3];
112 static void gpio_error(unsigned gpio
)
114 printk(KERN_ERR
"bfin-gpio: GPIO %d wasn't requested!\n", gpio
);
117 static void set_label(unsigned short ident
, const char *label
)
120 strncpy(str_ident
[ident
].name
, label
,
121 RESOURCE_LABEL_SIZE
);
122 str_ident
[ident
].name
[RESOURCE_LABEL_SIZE
- 1] = 0;
126 static char *get_label(unsigned short ident
)
128 return (*str_ident
[ident
].name
? str_ident
[ident
].name
: "UNKNOWN");
131 static int cmp_label(unsigned short ident
, const char *label
)
135 printk(KERN_ERR
"Please provide none-null label\n");
139 return strcmp(str_ident
[ident
].name
, label
);
144 #define map_entry(m, i) reserved_##m##_map[gpio_bank(i)]
145 #define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
146 #define reserve(m, i) (map_entry(m, i) |= gpio_bit(i))
147 #define unreserve(m, i) (map_entry(m, i) &= ~gpio_bit(i))
148 #define DECLARE_RESERVED_MAP(m, c) static unsigned short reserved_##m##_map[c]
150 DECLARE_RESERVED_MAP(gpio
, GPIO_BANK_NUM
);
151 DECLARE_RESERVED_MAP(peri
, DIV_ROUND_UP(MAX_RESOURCES
, GPIO_BANKSIZE
));
152 DECLARE_RESERVED_MAP(gpio_irq
, GPIO_BANK_NUM
);
154 inline int check_gpio(unsigned gpio
)
156 if (gpio
>= MAX_BLACKFIN_GPIOS
)
161 static void port_setup(unsigned gpio
, unsigned short usage
)
163 #if defined(BF538_FAMILY)
165 * BF538/9 Port C,D and E are special.
166 * Inverted PORT_FER polarity on CDE and no PORF_FER on F
167 * Regular PORT F GPIOs are handled here, CDE are exclusively
171 if (gpio
< MAX_BLACKFIN_GPIOS
|| gpio
>= MAX_RESOURCES
)
174 gpio
-= MAX_BLACKFIN_GPIOS
;
176 if (usage
== GPIO_USAGE
)
177 *port_fer
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
179 *port_fer
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
184 if (check_gpio(gpio
))
187 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
188 if (usage
== GPIO_USAGE
)
189 *port_fer
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
191 *port_fer
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
197 static const s8 port_mux
[] = {
206 [GPIO_PF8
... GPIO_PF15
] = -1,
207 [GPIO_PG0
... GPIO_PG7
] = -1,
216 [GPIO_PH0
... GPIO_PH15
] = -1,
217 [PORT_PJ0
... PORT_PJ3
] = -1,
220 [PORT_PJ6
... PORT_PJ9
] = -1,
225 static int portmux_group_check(unsigned short per
)
227 u16 ident
= P_IDENT(per
);
228 u16 function
= P_FUNCT2MUX(per
);
229 s8 offset
= port_mux
[ident
];
230 u16 m
, pmux
, pfunc
, mask
;
235 pmux
= bfin_read_PORT_MUX();
236 for (m
= 0; m
< ARRAY_SIZE(port_mux
); ++m
) {
239 if (port_mux
[m
] != offset
)
241 if (!is_reserved(peri
, m
, 1))
249 pfunc
= (pmux
>> offset
) & mask
;
250 if (pfunc
!= (function
& mask
)) {
251 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
252 ident
, function
, m
, pfunc
);
260 static void portmux_setup(unsigned short per
)
262 u16 ident
= P_IDENT(per
);
263 u16 function
= P_FUNCT2MUX(per
);
264 s8 offset
= port_mux
[ident
];
270 pmux
= bfin_read_PORT_MUX();
276 pmux
&= ~(mask
<< offset
);
277 pmux
|= ((function
& mask
) << offset
);
279 bfin_write_PORT_MUX(pmux
);
281 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
282 static int portmux_group_check(unsigned short per
)
284 u16 ident
= P_IDENT(per
);
285 u16 function
= P_FUNCT2MUX(per
);
286 u8 offset
= pmux_offset
[gpio_bank(ident
)][gpio_sub_n(ident
)];
287 u16 pin
, gpiopin
, pfunc
;
289 for (pin
= 0; pin
< GPIO_BANKSIZE
; ++pin
) {
290 if (offset
!= pmux_offset
[gpio_bank(ident
)][pin
])
293 gpiopin
= gpio_bank(ident
) * GPIO_BANKSIZE
+ pin
;
294 if (gpiopin
== ident
)
296 if (!is_reserved(peri
, gpiopin
, 1))
299 pfunc
= *port_mux
[gpio_bank(ident
)];
300 pfunc
= (pfunc
>> offset
) & 3;
301 if (pfunc
!= function
) {
302 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
303 ident
, function
, gpiopin
, pfunc
);
311 inline void portmux_setup(unsigned short per
)
313 u16 ident
= P_IDENT(per
);
314 u16 function
= P_FUNCT2MUX(per
);
315 u8 offset
= pmux_offset
[gpio_bank(ident
)][gpio_sub_n(ident
)];
318 pmux
= *port_mux
[gpio_bank(ident
)];
319 if (((pmux
>> offset
) & 3) == function
)
321 pmux
&= ~(3 << offset
);
322 pmux
|= (function
& 3) << offset
;
323 *port_mux
[gpio_bank(ident
)] = pmux
;
327 # define portmux_setup(...) do { } while (0)
328 static int portmux_group_check(unsigned short per
)
334 /***********************************************************
336 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
339 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
342 * DESCRIPTION: These functions abstract direct register access
343 * to Blackfin processor General Purpose
346 * CAUTION: These functions do not belong to the GPIO Driver API
347 *************************************************************
348 * MODIFICATION HISTORY :
349 **************************************************************/
351 /* Set a specific bit */
353 #define SET_GPIO(name) \
354 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
356 unsigned long flags; \
357 flags = hard_local_irq_save(); \
359 gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
361 gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
362 AWA_DUMMY_READ(name); \
363 hard_local_irq_restore(flags); \
365 EXPORT_SYMBOL(set_gpio_ ## name);
367 SET_GPIO(dir
) /* set_gpio_dir() */
368 SET_GPIO(inen
) /* set_gpio_inen() */
369 SET_GPIO(polar
) /* set_gpio_polar() */
370 SET_GPIO(edge
) /* set_gpio_edge() */
371 SET_GPIO(both
) /* set_gpio_both() */
374 #define SET_GPIO_SC(name) \
375 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
377 unsigned long flags; \
378 if (ANOMALY_05000311 || ANOMALY_05000323) \
379 flags = hard_local_irq_save(); \
381 gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
383 gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
384 if (ANOMALY_05000311 || ANOMALY_05000323) { \
385 AWA_DUMMY_READ(name); \
386 hard_local_irq_restore(flags); \
389 EXPORT_SYMBOL(set_gpio_ ## name);
395 void set_gpio_toggle(unsigned gpio
)
398 if (ANOMALY_05000311
|| ANOMALY_05000323
)
399 flags
= hard_local_irq_save();
400 gpio_array
[gpio_bank(gpio
)]->toggle
= gpio_bit(gpio
);
401 if (ANOMALY_05000311
|| ANOMALY_05000323
) {
402 AWA_DUMMY_READ(toggle
);
403 hard_local_irq_restore(flags
);
406 EXPORT_SYMBOL(set_gpio_toggle
);
409 /*Set current PORT date (16-bit word)*/
411 #define SET_GPIO_P(name) \
412 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
414 unsigned long flags; \
415 if (ANOMALY_05000311 || ANOMALY_05000323) \
416 flags = hard_local_irq_save(); \
417 gpio_array[gpio_bank(gpio)]->name = arg; \
418 if (ANOMALY_05000311 || ANOMALY_05000323) { \
419 AWA_DUMMY_READ(name); \
420 hard_local_irq_restore(flags); \
423 EXPORT_SYMBOL(set_gpiop_ ## name);
434 /* Get a specific bit */
435 #define GET_GPIO(name) \
436 unsigned short get_gpio_ ## name(unsigned gpio) \
438 unsigned long flags; \
439 unsigned short ret; \
440 if (ANOMALY_05000311 || ANOMALY_05000323) \
441 flags = hard_local_irq_save(); \
442 ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
443 if (ANOMALY_05000311 || ANOMALY_05000323) { \
444 AWA_DUMMY_READ(name); \
445 hard_local_irq_restore(flags); \
449 EXPORT_SYMBOL(get_gpio_ ## name);
460 /*Get current PORT date (16-bit word)*/
462 #define GET_GPIO_P(name) \
463 unsigned short get_gpiop_ ## name(unsigned gpio) \
465 unsigned long flags; \
466 unsigned short ret; \
467 if (ANOMALY_05000311 || ANOMALY_05000323) \
468 flags = hard_local_irq_save(); \
469 ret = (gpio_array[gpio_bank(gpio)]->name); \
470 if (ANOMALY_05000311 || ANOMALY_05000323) { \
471 AWA_DUMMY_READ(name); \
472 hard_local_irq_restore(flags); \
476 EXPORT_SYMBOL(get_gpiop_ ## name);
489 DECLARE_RESERVED_MAP(wakeup
, GPIO_BANK_NUM
);
491 static const unsigned int sic_iwr_irqs
[] = {
492 #if defined(BF533_FAMILY)
494 #elif defined(BF537_FAMILY)
495 IRQ_PF_INTB_WATCH
, IRQ_PORTG_INTB
, IRQ_PH_INTB_MAC_TX
496 #elif defined(BF538_FAMILY)
498 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
499 IRQ_PORTF_INTB
, IRQ_PORTG_INTB
, IRQ_PORTH_INTB
500 #elif defined(BF561_FAMILY)
501 IRQ_PROG0_INTB
, IRQ_PROG1_INTB
, IRQ_PROG2_INTB
503 # error no SIC_IWR defined
507 /***********************************************************
509 * FUNCTIONS: Blackfin PM Setup API
512 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
520 * DESCRIPTION: Blackfin PM Driver API
523 *************************************************************
524 * MODIFICATION HISTORY :
525 **************************************************************/
526 int bfin_gpio_pm_wakeup_ctrl(unsigned gpio
, unsigned ctrl
)
530 if (check_gpio(gpio
) < 0)
533 flags
= hard_local_irq_save();
535 reserve(wakeup
, gpio
);
537 unreserve(wakeup
, gpio
);
539 set_gpio_maskb(gpio
, ctrl
);
540 hard_local_irq_restore(flags
);
545 int bfin_gpio_pm_standby_ctrl(unsigned ctrl
)
549 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
550 mask
= map_entry(wakeup
, i
);
554 bfin_internal_set_wake(sic_iwr_irqs
[bank
], ctrl
);
559 void bfin_gpio_pm_hibernate_suspend(void)
564 for (i
= 0; i
< ARRAY_SIZE(port_fer_saved
); ++i
)
565 port_fer_saved
[i
] = *port_fer
[i
];
568 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
571 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
572 gpio_bank_saved
[bank
].fer
= *port_fer
[bank
];
573 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
574 gpio_bank_saved
[bank
].mux
= *port_mux
[bank
];
577 gpio_bank_saved
[bank
].mux
= bfin_read_PORT_MUX();
580 gpio_bank_saved
[bank
].data
= gpio_array
[bank
]->data
;
581 gpio_bank_saved
[bank
].inen
= gpio_array
[bank
]->inen
;
582 gpio_bank_saved
[bank
].polar
= gpio_array
[bank
]->polar
;
583 gpio_bank_saved
[bank
].dir
= gpio_array
[bank
]->dir
;
584 gpio_bank_saved
[bank
].edge
= gpio_array
[bank
]->edge
;
585 gpio_bank_saved
[bank
].both
= gpio_array
[bank
]->both
;
586 gpio_bank_saved
[bank
].maska
= gpio_array
[bank
]->maska
;
589 #ifdef BFIN_SPECIAL_GPIO_BANKS
590 bfin_special_gpio_pm_hibernate_suspend();
593 AWA_DUMMY_READ(maska
);
596 void bfin_gpio_pm_hibernate_restore(void)
601 for (i
= 0; i
< ARRAY_SIZE(port_fer_saved
); ++i
)
602 *port_fer
[i
] = port_fer_saved
[i
];
605 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
608 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
609 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
610 *port_mux
[bank
] = gpio_bank_saved
[bank
].mux
;
613 bfin_write_PORT_MUX(gpio_bank_saved
[bank
].mux
);
615 *port_fer
[bank
] = gpio_bank_saved
[bank
].fer
;
617 gpio_array
[bank
]->inen
= gpio_bank_saved
[bank
].inen
;
618 gpio_array
[bank
]->data_set
= gpio_bank_saved
[bank
].data
619 & gpio_bank_saved
[bank
].dir
;
620 gpio_array
[bank
]->dir
= gpio_bank_saved
[bank
].dir
;
621 gpio_array
[bank
]->polar
= gpio_bank_saved
[bank
].polar
;
622 gpio_array
[bank
]->edge
= gpio_bank_saved
[bank
].edge
;
623 gpio_array
[bank
]->both
= gpio_bank_saved
[bank
].both
;
624 gpio_array
[bank
]->maska
= gpio_bank_saved
[bank
].maska
;
627 #ifdef BFIN_SPECIAL_GPIO_BANKS
628 bfin_special_gpio_pm_hibernate_restore();
631 AWA_DUMMY_READ(maska
);
637 /***********************************************************
639 * FUNCTIONS: Blackfin Peripheral Resource Allocation
643 * per Peripheral Identifier
646 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
649 *************************************************************
650 * MODIFICATION HISTORY :
651 **************************************************************/
653 int peripheral_request(unsigned short per
, const char *label
)
656 unsigned short ident
= P_IDENT(per
);
659 * Don't cares are pins with only one dedicated function
662 if (per
& P_DONTCARE
)
665 if (!(per
& P_DEFINED
))
668 BUG_ON(ident
>= MAX_RESOURCES
);
670 flags
= hard_local_irq_save();
672 /* If a pin can be muxed as either GPIO or peripheral, make
673 * sure it is not already a GPIO pin when we request it.
675 if (unlikely(!check_gpio(ident
) && is_reserved(gpio
, ident
, 1))) {
676 if (system_state
== SYSTEM_BOOTING
)
679 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
680 __func__
, ident
, get_label(ident
));
681 hard_local_irq_restore(flags
);
685 if (unlikely(is_reserved(peri
, ident
, 1))) {
688 * Pin functions like AMC address strobes my
689 * be requested and used by several drivers
692 if (!(per
& P_MAYSHARE
)) {
694 * Allow that the identical pin function can
695 * be requested from the same driver twice
698 if (cmp_label(ident
, label
) == 0)
701 if (system_state
== SYSTEM_BOOTING
)
704 "%s: Peripheral %d function %d is already reserved by %s !\n",
705 __func__
, ident
, P_FUNCT2MUX(per
), get_label(ident
));
706 hard_local_irq_restore(flags
);
711 if (unlikely(portmux_group_check(per
))) {
712 hard_local_irq_restore(flags
);
716 reserve(peri
, ident
);
719 port_setup(ident
, PERIPHERAL_USAGE
);
721 hard_local_irq_restore(flags
);
722 set_label(ident
, label
);
726 EXPORT_SYMBOL(peripheral_request
);
728 int peripheral_request_list(const unsigned short per
[], const char *label
)
733 for (cnt
= 0; per
[cnt
] != 0; cnt
++) {
735 ret
= peripheral_request(per
[cnt
], label
);
738 for ( ; cnt
> 0; cnt
--)
739 peripheral_free(per
[cnt
- 1]);
747 EXPORT_SYMBOL(peripheral_request_list
);
749 void peripheral_free(unsigned short per
)
752 unsigned short ident
= P_IDENT(per
);
754 if (per
& P_DONTCARE
)
757 if (!(per
& P_DEFINED
))
760 flags
= hard_local_irq_save();
762 if (unlikely(!is_reserved(peri
, ident
, 0))) {
763 hard_local_irq_restore(flags
);
767 if (!(per
& P_MAYSHARE
))
768 port_setup(ident
, GPIO_USAGE
);
770 unreserve(peri
, ident
);
772 set_label(ident
, "free");
774 hard_local_irq_restore(flags
);
776 EXPORT_SYMBOL(peripheral_free
);
778 void peripheral_free_list(const unsigned short per
[])
781 for (cnt
= 0; per
[cnt
] != 0; cnt
++)
782 peripheral_free(per
[cnt
]);
784 EXPORT_SYMBOL(peripheral_free_list
);
786 /***********************************************************
788 * FUNCTIONS: Blackfin GPIO Driver
791 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
794 * DESCRIPTION: Blackfin GPIO Driver API
797 *************************************************************
798 * MODIFICATION HISTORY :
799 **************************************************************/
801 int bfin_gpio_request(unsigned gpio
, const char *label
)
805 if (check_gpio(gpio
) < 0)
808 flags
= hard_local_irq_save();
811 * Allow that the identical GPIO can
812 * be requested from the same driver twice
813 * Do nothing and return -
816 if (cmp_label(gpio
, label
) == 0) {
817 hard_local_irq_restore(flags
);
821 if (unlikely(is_reserved(gpio
, gpio
, 1))) {
822 if (system_state
== SYSTEM_BOOTING
)
824 printk(KERN_ERR
"bfin-gpio: GPIO %d is already reserved by %s !\n",
825 gpio
, get_label(gpio
));
826 hard_local_irq_restore(flags
);
829 if (unlikely(is_reserved(peri
, gpio
, 1))) {
830 if (system_state
== SYSTEM_BOOTING
)
833 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
834 gpio
, get_label(gpio
));
835 hard_local_irq_restore(flags
);
838 if (unlikely(is_reserved(gpio_irq
, gpio
, 1))) {
839 printk(KERN_NOTICE
"bfin-gpio: GPIO %d is already reserved as gpio-irq!"
840 " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio
);
841 } else { /* Reset POLAR setting when acquiring a gpio for the first time */
842 set_gpio_polar(gpio
, 0);
846 set_label(gpio
, label
);
848 hard_local_irq_restore(flags
);
850 port_setup(gpio
, GPIO_USAGE
);
854 EXPORT_SYMBOL(bfin_gpio_request
);
856 void bfin_gpio_free(unsigned gpio
)
860 if (check_gpio(gpio
) < 0)
865 flags
= hard_local_irq_save();
867 if (unlikely(!is_reserved(gpio
, gpio
, 0))) {
868 if (system_state
== SYSTEM_BOOTING
)
871 hard_local_irq_restore(flags
);
875 unreserve(gpio
, gpio
);
877 set_label(gpio
, "free");
879 hard_local_irq_restore(flags
);
881 EXPORT_SYMBOL(bfin_gpio_free
);
883 #ifdef BFIN_SPECIAL_GPIO_BANKS
884 DECLARE_RESERVED_MAP(special_gpio
, gpio_bank(MAX_RESOURCES
));
886 int bfin_special_gpio_request(unsigned gpio
, const char *label
)
890 flags
= hard_local_irq_save();
893 * Allow that the identical GPIO can
894 * be requested from the same driver twice
895 * Do nothing and return -
898 if (cmp_label(gpio
, label
) == 0) {
899 hard_local_irq_restore(flags
);
903 if (unlikely(is_reserved(special_gpio
, gpio
, 1))) {
904 hard_local_irq_restore(flags
);
905 printk(KERN_ERR
"bfin-gpio: GPIO %d is already reserved by %s !\n",
906 gpio
, get_label(gpio
));
910 if (unlikely(is_reserved(peri
, gpio
, 1))) {
911 hard_local_irq_restore(flags
);
913 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
914 gpio
, get_label(gpio
));
919 reserve(special_gpio
, gpio
);
922 set_label(gpio
, label
);
923 hard_local_irq_restore(flags
);
924 port_setup(gpio
, GPIO_USAGE
);
928 EXPORT_SYMBOL(bfin_special_gpio_request
);
930 void bfin_special_gpio_free(unsigned gpio
)
936 flags
= hard_local_irq_save();
938 if (unlikely(!is_reserved(special_gpio
, gpio
, 0))) {
940 hard_local_irq_restore(flags
);
944 unreserve(special_gpio
, gpio
);
945 unreserve(peri
, gpio
);
946 set_label(gpio
, "free");
947 hard_local_irq_restore(flags
);
949 EXPORT_SYMBOL(bfin_special_gpio_free
);
953 int bfin_gpio_irq_request(unsigned gpio
, const char *label
)
957 if (check_gpio(gpio
) < 0)
960 flags
= hard_local_irq_save();
962 if (unlikely(is_reserved(peri
, gpio
, 1))) {
963 if (system_state
== SYSTEM_BOOTING
)
966 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
967 gpio
, get_label(gpio
));
968 hard_local_irq_restore(flags
);
971 if (unlikely(is_reserved(gpio
, gpio
, 1)))
972 printk(KERN_NOTICE
"bfin-gpio: GPIO %d is already reserved by %s! "
973 "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
974 gpio
, get_label(gpio
));
976 reserve(gpio_irq
, gpio
);
977 set_label(gpio
, label
);
979 hard_local_irq_restore(flags
);
981 port_setup(gpio
, GPIO_USAGE
);
986 void bfin_gpio_irq_free(unsigned gpio
)
990 if (check_gpio(gpio
) < 0)
993 flags
= hard_local_irq_save();
995 if (unlikely(!is_reserved(gpio_irq
, gpio
, 0))) {
996 if (system_state
== SYSTEM_BOOTING
)
999 hard_local_irq_restore(flags
);
1003 unreserve(gpio_irq
, gpio
);
1005 set_label(gpio
, "free");
1007 hard_local_irq_restore(flags
);
1010 static inline void __bfin_gpio_direction_input(unsigned gpio
)
1012 gpio_array
[gpio_bank(gpio
)]->dir
&= ~gpio_bit(gpio
);
1013 gpio_array
[gpio_bank(gpio
)]->inen
|= gpio_bit(gpio
);
1016 int bfin_gpio_direction_input(unsigned gpio
)
1018 unsigned long flags
;
1020 if (unlikely(!is_reserved(gpio
, gpio
, 0))) {
1025 flags
= hard_local_irq_save();
1026 __bfin_gpio_direction_input(gpio
);
1027 AWA_DUMMY_READ(inen
);
1028 hard_local_irq_restore(flags
);
1032 EXPORT_SYMBOL(bfin_gpio_direction_input
);
1034 void bfin_gpio_irq_prepare(unsigned gpio
)
1036 port_setup(gpio
, GPIO_USAGE
);
1039 void bfin_gpio_set_value(unsigned gpio
, int arg
)
1042 gpio_array
[gpio_bank(gpio
)]->data_set
= gpio_bit(gpio
);
1044 gpio_array
[gpio_bank(gpio
)]->data_clear
= gpio_bit(gpio
);
1046 EXPORT_SYMBOL(bfin_gpio_set_value
);
1048 int bfin_gpio_direction_output(unsigned gpio
, int value
)
1050 unsigned long flags
;
1052 if (unlikely(!is_reserved(gpio
, gpio
, 0))) {
1057 flags
= hard_local_irq_save();
1059 gpio_array
[gpio_bank(gpio
)]->inen
&= ~gpio_bit(gpio
);
1060 gpio_set_value(gpio
, value
);
1061 gpio_array
[gpio_bank(gpio
)]->dir
|= gpio_bit(gpio
);
1063 AWA_DUMMY_READ(dir
);
1064 hard_local_irq_restore(flags
);
1068 EXPORT_SYMBOL(bfin_gpio_direction_output
);
1070 int bfin_gpio_get_value(unsigned gpio
)
1072 unsigned long flags
;
1074 if (unlikely(get_gpio_edge(gpio
))) {
1076 flags
= hard_local_irq_save();
1077 set_gpio_edge(gpio
, 0);
1078 ret
= get_gpio_data(gpio
);
1079 set_gpio_edge(gpio
, 1);
1080 hard_local_irq_restore(flags
);
1083 return get_gpio_data(gpio
);
1085 EXPORT_SYMBOL(bfin_gpio_get_value
);
1087 /* If we are booting from SPI and our board lacks a strong enough pull up,
1088 * the core can reset and execute the bootrom faster than the resistor can
1089 * pull the signal logically high. To work around this (common) error in
1090 * board design, we explicitly set the pin back to GPIO mode, force /CS
1091 * high, and wait for the electrons to do their thing.
1093 * This function only makes sense to be called from reset code, but it
1094 * lives here as we need to force all the GPIO states w/out going through
1095 * BUG() checks and such.
1097 void bfin_reset_boot_spi_cs(unsigned short pin
)
1099 unsigned short gpio
= P_IDENT(pin
);
1100 port_setup(gpio
, GPIO_USAGE
);
1101 gpio_array
[gpio_bank(gpio
)]->data_set
= gpio_bit(gpio
);
1102 AWA_DUMMY_READ(data_set
);
1106 #if defined(CONFIG_PROC_FS)
1107 static int gpio_proc_show(struct seq_file
*m
, void *v
)
1111 for (c
= 0; c
< MAX_RESOURCES
; c
++) {
1112 irq
= is_reserved(gpio_irq
, c
, 1);
1113 gpio
= is_reserved(gpio
, c
, 1);
1114 if (!check_gpio(c
) && (gpio
|| irq
))
1115 seq_printf(m
, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c
,
1116 get_label(c
), (gpio
&& irq
) ? " *" : "",
1117 get_gpio_dir(c
) ? "OUTPUT" : "INPUT");
1118 else if (is_reserved(peri
, c
, 1))
1119 seq_printf(m
, "GPIO_%d: \t%s \t\tPeripheral\n", c
, get_label(c
));
1127 static int gpio_proc_open(struct inode
*inode
, struct file
*file
)
1129 return single_open(file
, gpio_proc_show
, NULL
);
1132 static const struct file_operations gpio_proc_ops
= {
1133 .open
= gpio_proc_open
,
1135 .llseek
= seq_lseek
,
1136 .release
= single_release
,
1139 static __init
int gpio_register_proc(void)
1141 struct proc_dir_entry
*proc_gpio
;
1143 proc_gpio
= proc_create("gpio", 0, NULL
, &gpio_proc_ops
);
1144 return proc_gpio
== NULL
;
1146 __initcall(gpio_register_proc
);
1149 #ifdef CONFIG_GPIOLIB
1150 static int bfin_gpiolib_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
1152 return bfin_gpio_direction_input(gpio
);
1155 static int bfin_gpiolib_direction_output(struct gpio_chip
*chip
, unsigned gpio
, int level
)
1157 return bfin_gpio_direction_output(gpio
, level
);
1160 static int bfin_gpiolib_get_value(struct gpio_chip
*chip
, unsigned gpio
)
1162 return bfin_gpio_get_value(gpio
);
1165 static void bfin_gpiolib_set_value(struct gpio_chip
*chip
, unsigned gpio
, int value
)
1167 return bfin_gpio_set_value(gpio
, value
);
1170 static int bfin_gpiolib_gpio_request(struct gpio_chip
*chip
, unsigned gpio
)
1172 return bfin_gpio_request(gpio
, chip
->label
);
1175 static void bfin_gpiolib_gpio_free(struct gpio_chip
*chip
, unsigned gpio
)
1177 return bfin_gpio_free(gpio
);
1180 static int bfin_gpiolib_gpio_to_irq(struct gpio_chip
*chip
, unsigned gpio
)
1182 return gpio
+ GPIO_IRQ_BASE
;
1185 static struct gpio_chip bfin_chip
= {
1186 .label
= "BFIN-GPIO",
1187 .direction_input
= bfin_gpiolib_direction_input
,
1188 .get
= bfin_gpiolib_get_value
,
1189 .direction_output
= bfin_gpiolib_direction_output
,
1190 .set
= bfin_gpiolib_set_value
,
1191 .request
= bfin_gpiolib_gpio_request
,
1192 .free
= bfin_gpiolib_gpio_free
,
1193 .to_irq
= bfin_gpiolib_gpio_to_irq
,
1195 .ngpio
= MAX_BLACKFIN_GPIOS
,
1198 static int __init
bfin_gpiolib_setup(void)
1200 return gpiochip_add(&bfin_chip
);
1202 arch_initcall(bfin_gpiolib_setup
);