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
];
122 static unsigned short port_fer_saved
[3];
126 static void gpio_error(unsigned gpio
)
128 printk(KERN_ERR
"bfin-gpio: GPIO %d wasn't requested!\n", gpio
);
131 static void set_label(unsigned short ident
, const char *label
)
134 strncpy(str_ident
[ident
].name
, label
,
135 RESOURCE_LABEL_SIZE
);
136 str_ident
[ident
].name
[RESOURCE_LABEL_SIZE
- 1] = 0;
140 static char *get_label(unsigned short ident
)
142 return (*str_ident
[ident
].name
? str_ident
[ident
].name
: "UNKNOWN");
145 static int cmp_label(unsigned short ident
, const char *label
)
149 printk(KERN_ERR
"Please provide none-null label\n");
153 return strcmp(str_ident
[ident
].name
, label
);
158 #define map_entry(m, i) reserved_##m##_map[gpio_bank(i)]
159 #define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
160 #define reserve(m, i) (map_entry(m, i) |= gpio_bit(i))
161 #define unreserve(m, i) (map_entry(m, i) &= ~gpio_bit(i))
162 #define DECLARE_RESERVED_MAP(m, c) static unsigned short reserved_##m##_map[c]
164 DECLARE_RESERVED_MAP(gpio
, GPIO_BANK_NUM
);
165 DECLARE_RESERVED_MAP(peri
, DIV_ROUND_UP(MAX_RESOURCES
, GPIO_BANKSIZE
));
166 DECLARE_RESERVED_MAP(gpio_irq
, GPIO_BANK_NUM
);
168 inline int check_gpio(unsigned gpio
)
170 #if defined(CONFIG_BF54x)
171 if (gpio
== GPIO_PB15
|| gpio
== GPIO_PC14
|| gpio
== GPIO_PC15
172 || gpio
== GPIO_PH14
|| gpio
== GPIO_PH15
173 || gpio
== GPIO_PJ14
|| gpio
== GPIO_PJ15
)
176 if (gpio
>= MAX_BLACKFIN_GPIOS
)
181 static void port_setup(unsigned gpio
, unsigned short usage
)
183 #if defined(BF538_FAMILY)
185 * BF538/9 Port C,D and E are special.
186 * Inverted PORT_FER polarity on CDE and no PORF_FER on F
187 * Regular PORT F GPIOs are handled here, CDE are exclusively
191 if (gpio
< MAX_BLACKFIN_GPIOS
|| gpio
>= MAX_RESOURCES
)
194 gpio
-= MAX_BLACKFIN_GPIOS
;
196 if (usage
== GPIO_USAGE
)
197 *port_fer
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
199 *port_fer
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
204 if (check_gpio(gpio
))
207 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
208 if (usage
== GPIO_USAGE
)
209 *port_fer
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
211 *port_fer
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
213 #elif defined(CONFIG_BF54x)
214 if (usage
== GPIO_USAGE
)
215 gpio_array
[gpio_bank(gpio
)]->port_fer
&= ~gpio_bit(gpio
);
217 gpio_array
[gpio_bank(gpio
)]->port_fer
|= gpio_bit(gpio
);
223 static const s8 port_mux
[] = {
232 [GPIO_PF8
... GPIO_PF15
] = -1,
233 [GPIO_PG0
... GPIO_PG7
] = -1,
242 [GPIO_PH0
... GPIO_PH15
] = -1,
243 [PORT_PJ0
... PORT_PJ3
] = -1,
246 [PORT_PJ6
... PORT_PJ9
] = -1,
251 static int portmux_group_check(unsigned short per
)
253 u16 ident
= P_IDENT(per
);
254 u16 function
= P_FUNCT2MUX(per
);
255 s8 offset
= port_mux
[ident
];
261 pmux
= bfin_read_PORT_MUX();
262 for (m
= 0; m
< ARRAY_SIZE(port_mux
); ++m
) {
265 if (port_mux
[m
] != offset
)
267 if (!is_reserved(peri
, m
, 1))
271 pfunc
= (pmux
>> offset
) & 3;
273 pfunc
= (pmux
>> offset
) & 1;
274 if (pfunc
!= function
) {
275 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
276 ident
, function
, m
, pfunc
);
284 static void portmux_setup(unsigned short per
)
286 u16 ident
= P_IDENT(per
);
287 u16 function
= P_FUNCT2MUX(per
);
288 s8 offset
= port_mux
[ident
];
294 pmux
= bfin_read_PORT_MUX();
296 pmux
&= ~(1 << offset
);
299 pmux
|= (function
<< offset
);
300 bfin_write_PORT_MUX(pmux
);
302 #elif defined(CONFIG_BF54x)
303 inline void portmux_setup(unsigned short per
)
305 u16 ident
= P_IDENT(per
);
306 u16 function
= P_FUNCT2MUX(per
);
309 pmux
= gpio_array
[gpio_bank(ident
)]->port_mux
;
311 pmux
&= ~(0x3 << (2 * gpio_sub_n(ident
)));
312 pmux
|= (function
& 0x3) << (2 * gpio_sub_n(ident
));
314 gpio_array
[gpio_bank(ident
)]->port_mux
= pmux
;
317 inline u16
get_portmux(unsigned short per
)
319 u16 ident
= P_IDENT(per
);
320 u32 pmux
= gpio_array
[gpio_bank(ident
)]->port_mux
;
321 return (pmux
>> (2 * gpio_sub_n(ident
)) & 0x3);
323 static int portmux_group_check(unsigned short per
)
327 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
328 static int portmux_group_check(unsigned short per
)
330 u16 ident
= P_IDENT(per
);
331 u16 function
= P_FUNCT2MUX(per
);
332 u8 offset
= pmux_offset
[gpio_bank(ident
)][gpio_sub_n(ident
)];
333 u16 pin
, gpiopin
, pfunc
;
335 for (pin
= 0; pin
< GPIO_BANKSIZE
; ++pin
) {
336 if (offset
!= pmux_offset
[gpio_bank(ident
)][pin
])
339 gpiopin
= gpio_bank(ident
) * GPIO_BANKSIZE
+ pin
;
340 if (gpiopin
== ident
)
342 if (!is_reserved(peri
, gpiopin
, 1))
345 pfunc
= *port_mux
[gpio_bank(ident
)];
346 pfunc
= (pfunc
>> offset
) & 3;
347 if (pfunc
!= function
) {
348 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
349 ident
, function
, gpiopin
, pfunc
);
357 inline void portmux_setup(unsigned short per
)
359 u16 ident
= P_IDENT(per
);
360 u16 function
= P_FUNCT2MUX(per
);
361 u8 offset
= pmux_offset
[gpio_bank(ident
)][gpio_sub_n(ident
)];
364 pmux
= *port_mux
[gpio_bank(ident
)];
365 if (((pmux
>> offset
) & 3) == function
)
367 pmux
&= ~(3 << offset
);
368 pmux
|= (function
& 3) << offset
;
369 *port_mux
[gpio_bank(ident
)] = pmux
;
373 # define portmux_setup(...) do { } while (0)
374 static int portmux_group_check(unsigned short per
)
381 /***********************************************************
383 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
386 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
389 * DESCRIPTION: These functions abstract direct register access
390 * to Blackfin processor General Purpose
393 * CAUTION: These functions do not belong to the GPIO Driver API
394 *************************************************************
395 * MODIFICATION HISTORY :
396 **************************************************************/
398 /* Set a specific bit */
400 #define SET_GPIO(name) \
401 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
403 unsigned long flags; \
404 flags = hard_local_irq_save(); \
406 gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
408 gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
409 AWA_DUMMY_READ(name); \
410 hard_local_irq_restore(flags); \
412 EXPORT_SYMBOL(set_gpio_ ## name);
414 SET_GPIO(dir
) /* set_gpio_dir() */
415 SET_GPIO(inen
) /* set_gpio_inen() */
416 SET_GPIO(polar
) /* set_gpio_polar() */
417 SET_GPIO(edge
) /* set_gpio_edge() */
418 SET_GPIO(both
) /* set_gpio_both() */
421 #define SET_GPIO_SC(name) \
422 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
424 unsigned long flags; \
425 if (ANOMALY_05000311 || ANOMALY_05000323) \
426 flags = hard_local_irq_save(); \
428 gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
430 gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
431 if (ANOMALY_05000311 || ANOMALY_05000323) { \
432 AWA_DUMMY_READ(name); \
433 hard_local_irq_restore(flags); \
436 EXPORT_SYMBOL(set_gpio_ ## name);
442 void set_gpio_toggle(unsigned gpio
)
445 if (ANOMALY_05000311
|| ANOMALY_05000323
)
446 flags
= hard_local_irq_save();
447 gpio_array
[gpio_bank(gpio
)]->toggle
= gpio_bit(gpio
);
448 if (ANOMALY_05000311
|| ANOMALY_05000323
) {
449 AWA_DUMMY_READ(toggle
);
450 hard_local_irq_restore(flags
);
453 EXPORT_SYMBOL(set_gpio_toggle
);
456 /*Set current PORT date (16-bit word)*/
458 #define SET_GPIO_P(name) \
459 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
461 unsigned long flags; \
462 if (ANOMALY_05000311 || ANOMALY_05000323) \
463 flags = hard_local_irq_save(); \
464 gpio_array[gpio_bank(gpio)]->name = arg; \
465 if (ANOMALY_05000311 || ANOMALY_05000323) { \
466 AWA_DUMMY_READ(name); \
467 hard_local_irq_restore(flags); \
470 EXPORT_SYMBOL(set_gpiop_ ## name);
481 /* Get a specific bit */
482 #define GET_GPIO(name) \
483 unsigned short get_gpio_ ## name(unsigned gpio) \
485 unsigned long flags; \
486 unsigned short ret; \
487 if (ANOMALY_05000311 || ANOMALY_05000323) \
488 flags = hard_local_irq_save(); \
489 ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
490 if (ANOMALY_05000311 || ANOMALY_05000323) { \
491 AWA_DUMMY_READ(name); \
492 hard_local_irq_restore(flags); \
496 EXPORT_SYMBOL(get_gpio_ ## name);
507 /*Get current PORT date (16-bit word)*/
509 #define GET_GPIO_P(name) \
510 unsigned short get_gpiop_ ## name(unsigned gpio) \
512 unsigned long flags; \
513 unsigned short ret; \
514 if (ANOMALY_05000311 || ANOMALY_05000323) \
515 flags = hard_local_irq_save(); \
516 ret = (gpio_array[gpio_bank(gpio)]->name); \
517 if (ANOMALY_05000311 || ANOMALY_05000323) { \
518 AWA_DUMMY_READ(name); \
519 hard_local_irq_restore(flags); \
523 EXPORT_SYMBOL(get_gpiop_ ## name);
536 DECLARE_RESERVED_MAP(wakeup
, GPIO_BANK_NUM
);
538 static const unsigned int sic_iwr_irqs
[] = {
539 #if defined(BF533_FAMILY)
541 #elif defined(BF537_FAMILY)
542 IRQ_PF_INTB_WATCH
, IRQ_PORTG_INTB
, IRQ_PH_INTB_MAC_TX
543 #elif defined(BF538_FAMILY)
545 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
546 IRQ_PORTF_INTB
, IRQ_PORTG_INTB
, IRQ_PORTH_INTB
547 #elif defined(BF561_FAMILY)
548 IRQ_PROG0_INTB
, IRQ_PROG1_INTB
, IRQ_PROG2_INTB
550 # error no SIC_IWR defined
554 /***********************************************************
556 * FUNCTIONS: Blackfin PM Setup API
559 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
567 * DESCRIPTION: Blackfin PM Driver API
570 *************************************************************
571 * MODIFICATION HISTORY :
572 **************************************************************/
573 int gpio_pm_wakeup_ctrl(unsigned gpio
, unsigned ctrl
)
577 if (check_gpio(gpio
) < 0)
580 flags
= hard_local_irq_save();
582 reserve(wakeup
, gpio
);
584 unreserve(wakeup
, gpio
);
586 set_gpio_maskb(gpio
, ctrl
);
587 hard_local_irq_restore(flags
);
592 int bfin_pm_standby_ctrl(unsigned ctrl
)
596 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
597 mask
= map_entry(wakeup
, i
);
601 bfin_internal_set_wake(sic_iwr_irqs
[bank
], ctrl
);
606 void bfin_gpio_pm_hibernate_suspend(void)
611 for (i
= 0; i
< ARRAY_SIZE(port_fer_saved
); ++i
)
612 port_fer_saved
[i
] = *port_fer
[i
];
615 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
618 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
619 gpio_bank_saved
[bank
].fer
= *port_fer
[bank
];
620 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
621 gpio_bank_saved
[bank
].mux
= *port_mux
[bank
];
624 gpio_bank_saved
[bank
].mux
= bfin_read_PORT_MUX();
627 gpio_bank_saved
[bank
].data
= gpio_array
[bank
]->data
;
628 gpio_bank_saved
[bank
].inen
= gpio_array
[bank
]->inen
;
629 gpio_bank_saved
[bank
].polar
= gpio_array
[bank
]->polar
;
630 gpio_bank_saved
[bank
].dir
= gpio_array
[bank
]->dir
;
631 gpio_bank_saved
[bank
].edge
= gpio_array
[bank
]->edge
;
632 gpio_bank_saved
[bank
].both
= gpio_array
[bank
]->both
;
633 gpio_bank_saved
[bank
].maska
= gpio_array
[bank
]->maska
;
636 #ifdef BFIN_SPECIAL_GPIO_BANKS
637 bfin_special_gpio_pm_hibernate_suspend();
640 AWA_DUMMY_READ(maska
);
643 void bfin_gpio_pm_hibernate_restore(void)
648 for (i
= 0; i
< ARRAY_SIZE(port_fer_saved
); ++i
)
649 *port_fer
[i
] = port_fer_saved
[i
];
652 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
655 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
656 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
657 *port_mux
[bank
] = gpio_bank_saved
[bank
].mux
;
660 bfin_write_PORT_MUX(gpio_bank_saved
[bank
].mux
);
662 *port_fer
[bank
] = gpio_bank_saved
[bank
].fer
;
664 gpio_array
[bank
]->inen
= gpio_bank_saved
[bank
].inen
;
665 gpio_array
[bank
]->data_set
= gpio_bank_saved
[bank
].data
666 & gpio_bank_saved
[bank
].dir
;
667 gpio_array
[bank
]->dir
= gpio_bank_saved
[bank
].dir
;
668 gpio_array
[bank
]->polar
= gpio_bank_saved
[bank
].polar
;
669 gpio_array
[bank
]->edge
= gpio_bank_saved
[bank
].edge
;
670 gpio_array
[bank
]->both
= gpio_bank_saved
[bank
].both
;
671 gpio_array
[bank
]->maska
= gpio_bank_saved
[bank
].maska
;
674 #ifdef BFIN_SPECIAL_GPIO_BANKS
675 bfin_special_gpio_pm_hibernate_restore();
678 AWA_DUMMY_READ(maska
);
683 #else /* CONFIG_BF54x */
686 int bfin_pm_standby_ctrl(unsigned ctrl
)
691 void bfin_gpio_pm_hibernate_suspend(void)
695 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
698 gpio_bank_saved
[bank
].fer
= gpio_array
[bank
]->port_fer
;
699 gpio_bank_saved
[bank
].mux
= gpio_array
[bank
]->port_mux
;
700 gpio_bank_saved
[bank
].data
= gpio_array
[bank
]->data
;
701 gpio_bank_saved
[bank
].inen
= gpio_array
[bank
]->inen
;
702 gpio_bank_saved
[bank
].dir
= gpio_array
[bank
]->dir_set
;
706 void bfin_gpio_pm_hibernate_restore(void)
710 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
713 gpio_array
[bank
]->port_mux
= gpio_bank_saved
[bank
].mux
;
714 gpio_array
[bank
]->port_fer
= gpio_bank_saved
[bank
].fer
;
715 gpio_array
[bank
]->inen
= gpio_bank_saved
[bank
].inen
;
716 gpio_array
[bank
]->data_set
= gpio_bank_saved
[bank
].data
717 & gpio_bank_saved
[bank
].dir
;
718 gpio_array
[bank
]->dir_set
= gpio_bank_saved
[bank
].dir
;
723 unsigned short get_gpio_dir(unsigned gpio
)
725 return (0x01 & (gpio_array
[gpio_bank(gpio
)]->dir_clear
>> gpio_sub_n(gpio
)));
727 EXPORT_SYMBOL(get_gpio_dir
);
729 #endif /* CONFIG_BF54x */
731 /***********************************************************
733 * FUNCTIONS: Blackfin Peripheral Resource Allocation
737 * per Peripheral Identifier
740 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
743 *************************************************************
744 * MODIFICATION HISTORY :
745 **************************************************************/
747 int peripheral_request(unsigned short per
, const char *label
)
750 unsigned short ident
= P_IDENT(per
);
753 * Don't cares are pins with only one dedicated function
756 if (per
& P_DONTCARE
)
759 if (!(per
& P_DEFINED
))
762 BUG_ON(ident
>= MAX_RESOURCES
);
764 flags
= hard_local_irq_save();
766 /* If a pin can be muxed as either GPIO or peripheral, make
767 * sure it is not already a GPIO pin when we request it.
769 if (unlikely(!check_gpio(ident
) && is_reserved(gpio
, ident
, 1))) {
770 if (system_state
== SYSTEM_BOOTING
)
773 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
774 __func__
, ident
, get_label(ident
));
775 hard_local_irq_restore(flags
);
779 if (unlikely(is_reserved(peri
, ident
, 1))) {
782 * Pin functions like AMC address strobes my
783 * be requested and used by several drivers
787 if (!((per
& P_MAYSHARE
) && get_portmux(per
) == P_FUNCT2MUX(per
))) {
789 if (!(per
& P_MAYSHARE
)) {
792 * Allow that the identical pin function can
793 * be requested from the same driver twice
796 if (cmp_label(ident
, label
) == 0)
799 if (system_state
== SYSTEM_BOOTING
)
802 "%s: Peripheral %d function %d is already reserved by %s !\n",
803 __func__
, ident
, P_FUNCT2MUX(per
), get_label(ident
));
804 hard_local_irq_restore(flags
);
809 if (unlikely(portmux_group_check(per
))) {
810 hard_local_irq_restore(flags
);
814 reserve(peri
, ident
);
817 port_setup(ident
, PERIPHERAL_USAGE
);
819 hard_local_irq_restore(flags
);
820 set_label(ident
, label
);
824 EXPORT_SYMBOL(peripheral_request
);
826 int peripheral_request_list(const unsigned short per
[], const char *label
)
831 for (cnt
= 0; per
[cnt
] != 0; cnt
++) {
833 ret
= peripheral_request(per
[cnt
], label
);
836 for ( ; cnt
> 0; cnt
--)
837 peripheral_free(per
[cnt
- 1]);
845 EXPORT_SYMBOL(peripheral_request_list
);
847 void peripheral_free(unsigned short per
)
850 unsigned short ident
= P_IDENT(per
);
852 if (per
& P_DONTCARE
)
855 if (!(per
& P_DEFINED
))
858 flags
= hard_local_irq_save();
860 if (unlikely(!is_reserved(peri
, ident
, 0))) {
861 hard_local_irq_restore(flags
);
865 if (!(per
& P_MAYSHARE
))
866 port_setup(ident
, GPIO_USAGE
);
868 unreserve(peri
, ident
);
870 set_label(ident
, "free");
872 hard_local_irq_restore(flags
);
874 EXPORT_SYMBOL(peripheral_free
);
876 void peripheral_free_list(const unsigned short per
[])
879 for (cnt
= 0; per
[cnt
] != 0; cnt
++)
880 peripheral_free(per
[cnt
]);
882 EXPORT_SYMBOL(peripheral_free_list
);
884 /***********************************************************
886 * FUNCTIONS: Blackfin GPIO Driver
889 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
892 * DESCRIPTION: Blackfin GPIO Driver API
895 *************************************************************
896 * MODIFICATION HISTORY :
897 **************************************************************/
899 int bfin_gpio_request(unsigned gpio
, const char *label
)
903 if (check_gpio(gpio
) < 0)
906 flags
= hard_local_irq_save();
909 * Allow that the identical GPIO can
910 * be requested from the same driver twice
911 * Do nothing and return -
914 if (cmp_label(gpio
, label
) == 0) {
915 hard_local_irq_restore(flags
);
919 if (unlikely(is_reserved(gpio
, gpio
, 1))) {
920 if (system_state
== SYSTEM_BOOTING
)
922 printk(KERN_ERR
"bfin-gpio: GPIO %d is already reserved by %s !\n",
923 gpio
, get_label(gpio
));
924 hard_local_irq_restore(flags
);
927 if (unlikely(is_reserved(peri
, gpio
, 1))) {
928 if (system_state
== SYSTEM_BOOTING
)
931 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
932 gpio
, get_label(gpio
));
933 hard_local_irq_restore(flags
);
936 if (unlikely(is_reserved(gpio_irq
, gpio
, 1))) {
937 printk(KERN_NOTICE
"bfin-gpio: GPIO %d is already reserved as gpio-irq!"
938 " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio
);
941 else { /* Reset POLAR setting when acquiring a gpio for the first time */
942 set_gpio_polar(gpio
, 0);
947 set_label(gpio
, label
);
949 hard_local_irq_restore(flags
);
951 port_setup(gpio
, GPIO_USAGE
);
955 EXPORT_SYMBOL(bfin_gpio_request
);
957 void bfin_gpio_free(unsigned gpio
)
961 if (check_gpio(gpio
) < 0)
966 flags
= hard_local_irq_save();
968 if (unlikely(!is_reserved(gpio
, gpio
, 0))) {
969 if (system_state
== SYSTEM_BOOTING
)
972 hard_local_irq_restore(flags
);
976 unreserve(gpio
, gpio
);
978 set_label(gpio
, "free");
980 hard_local_irq_restore(flags
);
982 EXPORT_SYMBOL(bfin_gpio_free
);
984 #ifdef BFIN_SPECIAL_GPIO_BANKS
985 DECLARE_RESERVED_MAP(special_gpio
, gpio_bank(MAX_RESOURCES
));
987 int bfin_special_gpio_request(unsigned gpio
, const char *label
)
991 flags
= hard_local_irq_save();
994 * Allow that the identical GPIO can
995 * be requested from the same driver twice
996 * Do nothing and return -
999 if (cmp_label(gpio
, label
) == 0) {
1000 hard_local_irq_restore(flags
);
1004 if (unlikely(is_reserved(special_gpio
, gpio
, 1))) {
1005 hard_local_irq_restore(flags
);
1006 printk(KERN_ERR
"bfin-gpio: GPIO %d is already reserved by %s !\n",
1007 gpio
, get_label(gpio
));
1011 if (unlikely(is_reserved(peri
, gpio
, 1))) {
1012 hard_local_irq_restore(flags
);
1014 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1015 gpio
, get_label(gpio
));
1020 reserve(special_gpio
, gpio
);
1021 reserve(peri
, gpio
);
1023 set_label(gpio
, label
);
1024 hard_local_irq_restore(flags
);
1025 port_setup(gpio
, GPIO_USAGE
);
1029 EXPORT_SYMBOL(bfin_special_gpio_request
);
1031 void bfin_special_gpio_free(unsigned gpio
)
1033 unsigned long flags
;
1037 flags
= hard_local_irq_save();
1039 if (unlikely(!is_reserved(special_gpio
, gpio
, 0))) {
1041 hard_local_irq_restore(flags
);
1045 unreserve(special_gpio
, gpio
);
1046 unreserve(peri
, gpio
);
1047 set_label(gpio
, "free");
1048 hard_local_irq_restore(flags
);
1050 EXPORT_SYMBOL(bfin_special_gpio_free
);
1054 int bfin_gpio_irq_request(unsigned gpio
, const char *label
)
1056 unsigned long flags
;
1058 if (check_gpio(gpio
) < 0)
1061 flags
= hard_local_irq_save();
1063 if (unlikely(is_reserved(peri
, gpio
, 1))) {
1064 if (system_state
== SYSTEM_BOOTING
)
1067 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1068 gpio
, get_label(gpio
));
1069 hard_local_irq_restore(flags
);
1072 if (unlikely(is_reserved(gpio
, gpio
, 1)))
1073 printk(KERN_NOTICE
"bfin-gpio: GPIO %d is already reserved by %s! "
1074 "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
1075 gpio
, get_label(gpio
));
1077 reserve(gpio_irq
, gpio
);
1078 set_label(gpio
, label
);
1080 hard_local_irq_restore(flags
);
1082 port_setup(gpio
, GPIO_USAGE
);
1087 void bfin_gpio_irq_free(unsigned gpio
)
1089 unsigned long flags
;
1091 if (check_gpio(gpio
) < 0)
1094 flags
= hard_local_irq_save();
1096 if (unlikely(!is_reserved(gpio_irq
, gpio
, 0))) {
1097 if (system_state
== SYSTEM_BOOTING
)
1100 hard_local_irq_restore(flags
);
1104 unreserve(gpio_irq
, gpio
);
1106 set_label(gpio
, "free");
1108 hard_local_irq_restore(flags
);
1111 static inline void __bfin_gpio_direction_input(unsigned gpio
)
1114 gpio_array
[gpio_bank(gpio
)]->dir_clear
= gpio_bit(gpio
);
1116 gpio_array
[gpio_bank(gpio
)]->dir
&= ~gpio_bit(gpio
);
1118 gpio_array
[gpio_bank(gpio
)]->inen
|= gpio_bit(gpio
);
1121 int bfin_gpio_direction_input(unsigned gpio
)
1123 unsigned long flags
;
1125 if (unlikely(!is_reserved(gpio
, gpio
, 0))) {
1130 flags
= hard_local_irq_save();
1131 __bfin_gpio_direction_input(gpio
);
1132 AWA_DUMMY_READ(inen
);
1133 hard_local_irq_restore(flags
);
1137 EXPORT_SYMBOL(bfin_gpio_direction_input
);
1139 void bfin_gpio_irq_prepare(unsigned gpio
)
1142 unsigned long flags
;
1145 port_setup(gpio
, GPIO_USAGE
);
1148 flags
= hard_local_irq_save();
1149 __bfin_gpio_direction_input(gpio
);
1150 hard_local_irq_restore(flags
);
1154 void bfin_gpio_set_value(unsigned gpio
, int arg
)
1157 gpio_array
[gpio_bank(gpio
)]->data_set
= gpio_bit(gpio
);
1159 gpio_array
[gpio_bank(gpio
)]->data_clear
= gpio_bit(gpio
);
1161 EXPORT_SYMBOL(bfin_gpio_set_value
);
1163 int bfin_gpio_direction_output(unsigned gpio
, int value
)
1165 unsigned long flags
;
1167 if (unlikely(!is_reserved(gpio
, gpio
, 0))) {
1172 flags
= hard_local_irq_save();
1174 gpio_array
[gpio_bank(gpio
)]->inen
&= ~gpio_bit(gpio
);
1175 gpio_set_value(gpio
, value
);
1177 gpio_array
[gpio_bank(gpio
)]->dir_set
= gpio_bit(gpio
);
1179 gpio_array
[gpio_bank(gpio
)]->dir
|= gpio_bit(gpio
);
1182 AWA_DUMMY_READ(dir
);
1183 hard_local_irq_restore(flags
);
1187 EXPORT_SYMBOL(bfin_gpio_direction_output
);
1189 int bfin_gpio_get_value(unsigned gpio
)
1192 return (1 & (gpio_array
[gpio_bank(gpio
)]->data
>> gpio_sub_n(gpio
)));
1194 unsigned long flags
;
1196 if (unlikely(get_gpio_edge(gpio
))) {
1198 flags
= hard_local_irq_save();
1199 set_gpio_edge(gpio
, 0);
1200 ret
= get_gpio_data(gpio
);
1201 set_gpio_edge(gpio
, 1);
1202 hard_local_irq_restore(flags
);
1205 return get_gpio_data(gpio
);
1208 EXPORT_SYMBOL(bfin_gpio_get_value
);
1210 /* If we are booting from SPI and our board lacks a strong enough pull up,
1211 * the core can reset and execute the bootrom faster than the resistor can
1212 * pull the signal logically high. To work around this (common) error in
1213 * board design, we explicitly set the pin back to GPIO mode, force /CS
1214 * high, and wait for the electrons to do their thing.
1216 * This function only makes sense to be called from reset code, but it
1217 * lives here as we need to force all the GPIO states w/out going through
1218 * BUG() checks and such.
1220 void bfin_reset_boot_spi_cs(unsigned short pin
)
1222 unsigned short gpio
= P_IDENT(pin
);
1223 port_setup(gpio
, GPIO_USAGE
);
1224 gpio_array
[gpio_bank(gpio
)]->data_set
= gpio_bit(gpio
);
1225 AWA_DUMMY_READ(data_set
);
1229 #if defined(CONFIG_PROC_FS)
1230 static int gpio_proc_show(struct seq_file
*m
, void *v
)
1234 for (c
= 0; c
< MAX_RESOURCES
; c
++) {
1235 irq
= is_reserved(gpio_irq
, c
, 1);
1236 gpio
= is_reserved(gpio
, c
, 1);
1237 if (!check_gpio(c
) && (gpio
|| irq
))
1238 seq_printf(m
, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c
,
1239 get_label(c
), (gpio
&& irq
) ? " *" : "",
1240 get_gpio_dir(c
) ? "OUTPUT" : "INPUT");
1241 else if (is_reserved(peri
, c
, 1))
1242 seq_printf(m
, "GPIO_%d: \t%s \t\tPeripheral\n", c
, get_label(c
));
1250 static int gpio_proc_open(struct inode
*inode
, struct file
*file
)
1252 return single_open(file
, gpio_proc_show
, NULL
);
1255 static const struct file_operations gpio_proc_ops
= {
1256 .open
= gpio_proc_open
,
1258 .llseek
= seq_lseek
,
1259 .release
= single_release
,
1262 static __init
int gpio_register_proc(void)
1264 struct proc_dir_entry
*proc_gpio
;
1266 proc_gpio
= proc_create("gpio", S_IRUGO
, NULL
, &gpio_proc_ops
);
1267 return proc_gpio
!= NULL
;
1269 __initcall(gpio_register_proc
);
1272 #ifdef CONFIG_GPIOLIB
1273 static int bfin_gpiolib_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
1275 return bfin_gpio_direction_input(gpio
);
1278 static int bfin_gpiolib_direction_output(struct gpio_chip
*chip
, unsigned gpio
, int level
)
1280 return bfin_gpio_direction_output(gpio
, level
);
1283 static int bfin_gpiolib_get_value(struct gpio_chip
*chip
, unsigned gpio
)
1285 return bfin_gpio_get_value(gpio
);
1288 static void bfin_gpiolib_set_value(struct gpio_chip
*chip
, unsigned gpio
, int value
)
1290 return bfin_gpio_set_value(gpio
, value
);
1293 static int bfin_gpiolib_gpio_request(struct gpio_chip
*chip
, unsigned gpio
)
1295 return bfin_gpio_request(gpio
, chip
->label
);
1298 static void bfin_gpiolib_gpio_free(struct gpio_chip
*chip
, unsigned gpio
)
1300 return bfin_gpio_free(gpio
);
1303 static int bfin_gpiolib_gpio_to_irq(struct gpio_chip
*chip
, unsigned gpio
)
1305 return gpio
+ GPIO_IRQ_BASE
;
1308 static struct gpio_chip bfin_chip
= {
1309 .label
= "BFIN-GPIO",
1310 .direction_input
= bfin_gpiolib_direction_input
,
1311 .get
= bfin_gpiolib_get_value
,
1312 .direction_output
= bfin_gpiolib_direction_output
,
1313 .set
= bfin_gpiolib_set_value
,
1314 .request
= bfin_gpiolib_gpio_request
,
1315 .free
= bfin_gpiolib_gpio_free
,
1316 .to_irq
= bfin_gpiolib_gpio_to_irq
,
1318 .ngpio
= MAX_BLACKFIN_GPIOS
,
1321 static int __init
bfin_gpiolib_setup(void)
1323 return gpiochip_add(&bfin_chip
);
1325 arch_initcall(bfin_gpiolib_setup
);