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) || defined(CONFIG_BF60x)
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 # if defined(CONFIG_BF54x)
70 (struct gpio_port_t
*)PORTH_FER
,
71 (struct gpio_port_t
*)PORTI_FER
,
72 (struct gpio_port_t
*)PORTJ_FER
,
75 # error no gpio arrays defined
79 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
80 static unsigned short * const port_fer
[] = {
81 (unsigned short *) PORTF_FER
,
82 (unsigned short *) PORTG_FER
,
83 (unsigned short *) PORTH_FER
,
86 # if !defined(BF537_FAMILY)
87 static unsigned short * const port_mux
[] = {
88 (unsigned short *) PORTF_MUX
,
89 (unsigned short *) PORTG_MUX
,
90 (unsigned short *) PORTH_MUX
,
94 u8 pmux_offset
[][16] = {
95 # if defined(CONFIG_BF52x)
96 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
97 { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
98 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
99 # elif defined(CONFIG_BF51x)
100 { 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
101 { 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
102 { 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
107 #elif defined(BF538_FAMILY)
108 static unsigned short * const port_fer
[] = {
109 (unsigned short *) PORTCIO_FER
,
110 (unsigned short *) PORTDIO_FER
,
111 (unsigned short *) PORTEIO_FER
,
115 #define RESOURCE_LABEL_SIZE 16
117 static struct str_ident
{
118 char name
[RESOURCE_LABEL_SIZE
];
119 } str_ident
[MAX_RESOURCES
];
121 #if defined(CONFIG_PM)
122 static struct gpio_port_s gpio_bank_saved
[GPIO_BANK_NUM
];
124 static unsigned short port_fer_saved
[3];
128 static void gpio_error(unsigned gpio
)
130 printk(KERN_ERR
"bfin-gpio: GPIO %d wasn't requested!\n", gpio
);
133 static void set_label(unsigned short ident
, const char *label
)
136 strncpy(str_ident
[ident
].name
, label
,
137 RESOURCE_LABEL_SIZE
);
138 str_ident
[ident
].name
[RESOURCE_LABEL_SIZE
- 1] = 0;
142 static char *get_label(unsigned short ident
)
144 return (*str_ident
[ident
].name
? str_ident
[ident
].name
: "UNKNOWN");
147 static int cmp_label(unsigned short ident
, const char *label
)
151 printk(KERN_ERR
"Please provide none-null label\n");
155 return strcmp(str_ident
[ident
].name
, label
);
160 #define map_entry(m, i) reserved_##m##_map[gpio_bank(i)]
161 #define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
162 #define reserve(m, i) (map_entry(m, i) |= gpio_bit(i))
163 #define unreserve(m, i) (map_entry(m, i) &= ~gpio_bit(i))
164 #define DECLARE_RESERVED_MAP(m, c) static unsigned short reserved_##m##_map[c]
166 DECLARE_RESERVED_MAP(gpio
, GPIO_BANK_NUM
);
167 DECLARE_RESERVED_MAP(peri
, DIV_ROUND_UP(MAX_RESOURCES
, GPIO_BANKSIZE
));
168 DECLARE_RESERVED_MAP(gpio_irq
, GPIO_BANK_NUM
);
170 inline int check_gpio(unsigned gpio
)
172 #if defined(CONFIG_BF54x)
173 if (gpio
== GPIO_PB15
|| gpio
== GPIO_PC14
|| gpio
== GPIO_PC15
174 || gpio
== GPIO_PH14
|| gpio
== GPIO_PH15
175 || gpio
== GPIO_PJ14
|| gpio
== GPIO_PJ15
)
178 if (gpio
>= MAX_BLACKFIN_GPIOS
)
183 static void port_setup(unsigned gpio
, unsigned short usage
)
185 #if defined(BF538_FAMILY)
187 * BF538/9 Port C,D and E are special.
188 * Inverted PORT_FER polarity on CDE and no PORF_FER on F
189 * Regular PORT F GPIOs are handled here, CDE are exclusively
193 if (gpio
< MAX_BLACKFIN_GPIOS
|| gpio
>= MAX_RESOURCES
)
196 gpio
-= MAX_BLACKFIN_GPIOS
;
198 if (usage
== GPIO_USAGE
)
199 *port_fer
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
201 *port_fer
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
206 if (check_gpio(gpio
))
209 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
210 if (usage
== GPIO_USAGE
)
211 *port_fer
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
213 *port_fer
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
215 #elif defined(CONFIG_BF54x) || defined(CONFIG_BF60x)
216 if (usage
== GPIO_USAGE
)
217 gpio_array
[gpio_bank(gpio
)]->port_fer
&= ~gpio_bit(gpio
);
219 gpio_array
[gpio_bank(gpio
)]->port_fer
|= gpio_bit(gpio
);
225 static const s8 port_mux
[] = {
234 [GPIO_PF8
... GPIO_PF15
] = -1,
235 [GPIO_PG0
... GPIO_PG7
] = -1,
244 [GPIO_PH0
... GPIO_PH15
] = -1,
245 [PORT_PJ0
... PORT_PJ3
] = -1,
248 [PORT_PJ6
... PORT_PJ9
] = -1,
253 static int portmux_group_check(unsigned short per
)
255 u16 ident
= P_IDENT(per
);
256 u16 function
= P_FUNCT2MUX(per
);
257 s8 offset
= port_mux
[ident
];
263 pmux
= bfin_read_PORT_MUX();
264 for (m
= 0; m
< ARRAY_SIZE(port_mux
); ++m
) {
267 if (port_mux
[m
] != offset
)
269 if (!is_reserved(peri
, m
, 1))
273 pfunc
= (pmux
>> offset
) & 3;
275 pfunc
= (pmux
>> offset
) & 1;
276 if (pfunc
!= function
) {
277 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
278 ident
, function
, m
, pfunc
);
286 static void portmux_setup(unsigned short per
)
288 u16 ident
= P_IDENT(per
);
289 u16 function
= P_FUNCT2MUX(per
);
290 s8 offset
= port_mux
[ident
];
296 pmux
= bfin_read_PORT_MUX();
298 pmux
&= ~(1 << offset
);
301 pmux
|= (function
<< offset
);
302 bfin_write_PORT_MUX(pmux
);
304 #elif defined(CONFIG_BF54x) || defined(CONFIG_BF60x)
305 inline void portmux_setup(unsigned short per
)
307 u16 ident
= P_IDENT(per
);
308 u16 function
= P_FUNCT2MUX(per
);
311 pmux
= gpio_array
[gpio_bank(ident
)]->port_mux
;
313 pmux
&= ~(0x3 << (2 * gpio_sub_n(ident
)));
314 pmux
|= (function
& 0x3) << (2 * gpio_sub_n(ident
));
316 gpio_array
[gpio_bank(ident
)]->port_mux
= pmux
;
319 inline u16
get_portmux(unsigned short per
)
321 u16 ident
= P_IDENT(per
);
322 u32 pmux
= gpio_array
[gpio_bank(ident
)]->port_mux
;
323 return (pmux
>> (2 * gpio_sub_n(ident
)) & 0x3);
325 static int portmux_group_check(unsigned short per
)
329 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
330 static int portmux_group_check(unsigned short per
)
332 u16 ident
= P_IDENT(per
);
333 u16 function
= P_FUNCT2MUX(per
);
334 u8 offset
= pmux_offset
[gpio_bank(ident
)][gpio_sub_n(ident
)];
335 u16 pin
, gpiopin
, pfunc
;
337 for (pin
= 0; pin
< GPIO_BANKSIZE
; ++pin
) {
338 if (offset
!= pmux_offset
[gpio_bank(ident
)][pin
])
341 gpiopin
= gpio_bank(ident
) * GPIO_BANKSIZE
+ pin
;
342 if (gpiopin
== ident
)
344 if (!is_reserved(peri
, gpiopin
, 1))
347 pfunc
= *port_mux
[gpio_bank(ident
)];
348 pfunc
= (pfunc
>> offset
) & 3;
349 if (pfunc
!= function
) {
350 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
351 ident
, function
, gpiopin
, pfunc
);
359 inline void portmux_setup(unsigned short per
)
361 u16 ident
= P_IDENT(per
);
362 u16 function
= P_FUNCT2MUX(per
);
363 u8 offset
= pmux_offset
[gpio_bank(ident
)][gpio_sub_n(ident
)];
366 pmux
= *port_mux
[gpio_bank(ident
)];
367 if (((pmux
>> offset
) & 3) == function
)
369 pmux
&= ~(3 << offset
);
370 pmux
|= (function
& 3) << offset
;
371 *port_mux
[gpio_bank(ident
)] = pmux
;
375 # define portmux_setup(...) do { } while (0)
376 static int portmux_group_check(unsigned short per
)
382 #if !(defined(CONFIG_BF54x) || defined(CONFIG_BF60x))
383 /***********************************************************
385 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
388 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
391 * DESCRIPTION: These functions abstract direct register access
392 * to Blackfin processor General Purpose
395 * CAUTION: These functions do not belong to the GPIO Driver API
396 *************************************************************
397 * MODIFICATION HISTORY :
398 **************************************************************/
400 /* Set a specific bit */
402 #define SET_GPIO(name) \
403 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
405 unsigned long flags; \
406 flags = hard_local_irq_save(); \
408 gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
410 gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
411 AWA_DUMMY_READ(name); \
412 hard_local_irq_restore(flags); \
414 EXPORT_SYMBOL(set_gpio_ ## name);
416 SET_GPIO(dir
) /* set_gpio_dir() */
417 SET_GPIO(inen
) /* set_gpio_inen() */
418 SET_GPIO(polar
) /* set_gpio_polar() */
419 SET_GPIO(edge
) /* set_gpio_edge() */
420 SET_GPIO(both
) /* set_gpio_both() */
423 #define SET_GPIO_SC(name) \
424 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
426 unsigned long flags; \
427 if (ANOMALY_05000311 || ANOMALY_05000323) \
428 flags = hard_local_irq_save(); \
430 gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
432 gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
433 if (ANOMALY_05000311 || ANOMALY_05000323) { \
434 AWA_DUMMY_READ(name); \
435 hard_local_irq_restore(flags); \
438 EXPORT_SYMBOL(set_gpio_ ## name);
444 void set_gpio_toggle(unsigned gpio
)
447 if (ANOMALY_05000311
|| ANOMALY_05000323
)
448 flags
= hard_local_irq_save();
449 gpio_array
[gpio_bank(gpio
)]->toggle
= gpio_bit(gpio
);
450 if (ANOMALY_05000311
|| ANOMALY_05000323
) {
451 AWA_DUMMY_READ(toggle
);
452 hard_local_irq_restore(flags
);
455 EXPORT_SYMBOL(set_gpio_toggle
);
458 /*Set current PORT date (16-bit word)*/
460 #define SET_GPIO_P(name) \
461 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
463 unsigned long flags; \
464 if (ANOMALY_05000311 || ANOMALY_05000323) \
465 flags = hard_local_irq_save(); \
466 gpio_array[gpio_bank(gpio)]->name = arg; \
467 if (ANOMALY_05000311 || ANOMALY_05000323) { \
468 AWA_DUMMY_READ(name); \
469 hard_local_irq_restore(flags); \
472 EXPORT_SYMBOL(set_gpiop_ ## name);
483 /* Get a specific bit */
484 #define GET_GPIO(name) \
485 unsigned short get_gpio_ ## name(unsigned gpio) \
487 unsigned long flags; \
488 unsigned short ret; \
489 if (ANOMALY_05000311 || ANOMALY_05000323) \
490 flags = hard_local_irq_save(); \
491 ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
492 if (ANOMALY_05000311 || ANOMALY_05000323) { \
493 AWA_DUMMY_READ(name); \
494 hard_local_irq_restore(flags); \
498 EXPORT_SYMBOL(get_gpio_ ## name);
509 /*Get current PORT date (16-bit word)*/
511 #define GET_GPIO_P(name) \
512 unsigned short get_gpiop_ ## name(unsigned gpio) \
514 unsigned long flags; \
515 unsigned short ret; \
516 if (ANOMALY_05000311 || ANOMALY_05000323) \
517 flags = hard_local_irq_save(); \
518 ret = (gpio_array[gpio_bank(gpio)]->name); \
519 if (ANOMALY_05000311 || ANOMALY_05000323) { \
520 AWA_DUMMY_READ(name); \
521 hard_local_irq_restore(flags); \
525 EXPORT_SYMBOL(get_gpiop_ ## name);
538 DECLARE_RESERVED_MAP(wakeup
, GPIO_BANK_NUM
);
540 static const unsigned int sic_iwr_irqs
[] = {
541 #if defined(BF533_FAMILY)
543 #elif defined(BF537_FAMILY)
544 IRQ_PF_INTB_WATCH
, IRQ_PORTG_INTB
, IRQ_PH_INTB_MAC_TX
545 #elif defined(BF538_FAMILY)
547 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
548 IRQ_PORTF_INTB
, IRQ_PORTG_INTB
, IRQ_PORTH_INTB
549 #elif defined(BF561_FAMILY)
550 IRQ_PROG0_INTB
, IRQ_PROG1_INTB
, IRQ_PROG2_INTB
552 # error no SIC_IWR defined
556 /***********************************************************
558 * FUNCTIONS: Blackfin PM Setup API
561 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
569 * DESCRIPTION: Blackfin PM Driver API
572 *************************************************************
573 * MODIFICATION HISTORY :
574 **************************************************************/
575 int gpio_pm_wakeup_ctrl(unsigned gpio
, unsigned ctrl
)
579 if (check_gpio(gpio
) < 0)
582 flags
= hard_local_irq_save();
584 reserve(wakeup
, gpio
);
586 unreserve(wakeup
, gpio
);
588 set_gpio_maskb(gpio
, ctrl
);
589 hard_local_irq_restore(flags
);
594 int bfin_pm_standby_ctrl(unsigned ctrl
)
598 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
599 mask
= map_entry(wakeup
, i
);
603 bfin_internal_set_wake(sic_iwr_irqs
[bank
], ctrl
);
608 void bfin_gpio_pm_hibernate_suspend(void)
613 for (i
= 0; i
< ARRAY_SIZE(port_fer_saved
); ++i
)
614 port_fer_saved
[i
] = *port_fer
[i
];
617 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
620 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
621 gpio_bank_saved
[bank
].fer
= *port_fer
[bank
];
622 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
623 gpio_bank_saved
[bank
].mux
= *port_mux
[bank
];
626 gpio_bank_saved
[bank
].mux
= bfin_read_PORT_MUX();
629 gpio_bank_saved
[bank
].data
= gpio_array
[bank
]->data
;
630 gpio_bank_saved
[bank
].inen
= gpio_array
[bank
]->inen
;
631 gpio_bank_saved
[bank
].polar
= gpio_array
[bank
]->polar
;
632 gpio_bank_saved
[bank
].dir
= gpio_array
[bank
]->dir
;
633 gpio_bank_saved
[bank
].edge
= gpio_array
[bank
]->edge
;
634 gpio_bank_saved
[bank
].both
= gpio_array
[bank
]->both
;
635 gpio_bank_saved
[bank
].maska
= gpio_array
[bank
]->maska
;
638 #ifdef BFIN_SPECIAL_GPIO_BANKS
639 bfin_special_gpio_pm_hibernate_suspend();
642 AWA_DUMMY_READ(maska
);
645 void bfin_gpio_pm_hibernate_restore(void)
650 for (i
= 0; i
< ARRAY_SIZE(port_fer_saved
); ++i
)
651 *port_fer
[i
] = port_fer_saved
[i
];
654 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
657 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
658 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
659 *port_mux
[bank
] = gpio_bank_saved
[bank
].mux
;
662 bfin_write_PORT_MUX(gpio_bank_saved
[bank
].mux
);
664 *port_fer
[bank
] = gpio_bank_saved
[bank
].fer
;
666 gpio_array
[bank
]->inen
= gpio_bank_saved
[bank
].inen
;
667 gpio_array
[bank
]->data_set
= gpio_bank_saved
[bank
].data
668 & gpio_bank_saved
[bank
].dir
;
669 gpio_array
[bank
]->dir
= gpio_bank_saved
[bank
].dir
;
670 gpio_array
[bank
]->polar
= gpio_bank_saved
[bank
].polar
;
671 gpio_array
[bank
]->edge
= gpio_bank_saved
[bank
].edge
;
672 gpio_array
[bank
]->both
= gpio_bank_saved
[bank
].both
;
673 gpio_array
[bank
]->maska
= gpio_bank_saved
[bank
].maska
;
676 #ifdef BFIN_SPECIAL_GPIO_BANKS
677 bfin_special_gpio_pm_hibernate_restore();
680 AWA_DUMMY_READ(maska
);
685 #else /* CONFIG_BF54x || CONFIG_BF60x */
688 int bfin_pm_standby_ctrl(unsigned ctrl
)
693 void bfin_gpio_pm_hibernate_suspend(void)
697 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
700 gpio_bank_saved
[bank
].fer
= gpio_array
[bank
]->port_fer
;
701 gpio_bank_saved
[bank
].mux
= gpio_array
[bank
]->port_mux
;
702 gpio_bank_saved
[bank
].data
= gpio_array
[bank
]->data
;
703 gpio_bank_saved
[bank
].inen
= gpio_array
[bank
]->inen
;
704 gpio_bank_saved
[bank
].dir
= gpio_array
[bank
]->dir_set
;
708 void bfin_gpio_pm_hibernate_restore(void)
712 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
715 gpio_array
[bank
]->port_mux
= gpio_bank_saved
[bank
].mux
;
716 gpio_array
[bank
]->port_fer
= gpio_bank_saved
[bank
].fer
;
717 gpio_array
[bank
]->inen
= gpio_bank_saved
[bank
].inen
;
718 gpio_array
[bank
]->data_set
= gpio_bank_saved
[bank
].data
719 & gpio_bank_saved
[bank
].dir
;
720 gpio_array
[bank
]->dir_set
= gpio_bank_saved
[bank
].dir
;
725 unsigned short get_gpio_dir(unsigned gpio
)
727 return (0x01 & (gpio_array
[gpio_bank(gpio
)]->dir_clear
>> gpio_sub_n(gpio
)));
729 EXPORT_SYMBOL(get_gpio_dir
);
731 #endif /* CONFIG_BF54x || CONFIG_BF60x */
733 /***********************************************************
735 * FUNCTIONS: Blackfin Peripheral Resource Allocation
739 * per Peripheral Identifier
742 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
745 *************************************************************
746 * MODIFICATION HISTORY :
747 **************************************************************/
749 int peripheral_request(unsigned short per
, const char *label
)
752 unsigned short ident
= P_IDENT(per
);
755 * Don't cares are pins with only one dedicated function
758 if (per
& P_DONTCARE
)
761 if (!(per
& P_DEFINED
))
764 BUG_ON(ident
>= MAX_RESOURCES
);
766 flags
= hard_local_irq_save();
768 /* If a pin can be muxed as either GPIO or peripheral, make
769 * sure it is not already a GPIO pin when we request it.
771 if (unlikely(!check_gpio(ident
) && is_reserved(gpio
, ident
, 1))) {
772 if (system_state
== SYSTEM_BOOTING
)
775 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
776 __func__
, ident
, get_label(ident
));
777 hard_local_irq_restore(flags
);
781 if (unlikely(is_reserved(peri
, ident
, 1))) {
784 * Pin functions like AMC address strobes my
785 * be requested and used by several drivers
788 #if defined(CONFIG_BF54x) || defined(CONFIG_BF60x)
789 if (!((per
& P_MAYSHARE
) && get_portmux(per
) == P_FUNCT2MUX(per
))) {
791 if (!(per
& P_MAYSHARE
)) {
794 * Allow that the identical pin function can
795 * be requested from the same driver twice
798 if (cmp_label(ident
, label
) == 0)
801 if (system_state
== SYSTEM_BOOTING
)
804 "%s: Peripheral %d function %d is already reserved by %s !\n",
805 __func__
, ident
, P_FUNCT2MUX(per
), get_label(ident
));
806 hard_local_irq_restore(flags
);
811 if (unlikely(portmux_group_check(per
))) {
812 hard_local_irq_restore(flags
);
816 reserve(peri
, ident
);
819 port_setup(ident
, PERIPHERAL_USAGE
);
821 hard_local_irq_restore(flags
);
822 set_label(ident
, label
);
826 EXPORT_SYMBOL(peripheral_request
);
828 int peripheral_request_list(const unsigned short per
[], const char *label
)
833 for (cnt
= 0; per
[cnt
] != 0; cnt
++) {
835 ret
= peripheral_request(per
[cnt
], label
);
838 for ( ; cnt
> 0; cnt
--)
839 peripheral_free(per
[cnt
- 1]);
847 EXPORT_SYMBOL(peripheral_request_list
);
849 void peripheral_free(unsigned short per
)
852 unsigned short ident
= P_IDENT(per
);
854 if (per
& P_DONTCARE
)
857 if (!(per
& P_DEFINED
))
860 flags
= hard_local_irq_save();
862 if (unlikely(!is_reserved(peri
, ident
, 0))) {
863 hard_local_irq_restore(flags
);
867 if (!(per
& P_MAYSHARE
))
868 port_setup(ident
, GPIO_USAGE
);
870 unreserve(peri
, ident
);
872 set_label(ident
, "free");
874 hard_local_irq_restore(flags
);
876 EXPORT_SYMBOL(peripheral_free
);
878 void peripheral_free_list(const unsigned short per
[])
881 for (cnt
= 0; per
[cnt
] != 0; cnt
++)
882 peripheral_free(per
[cnt
]);
884 EXPORT_SYMBOL(peripheral_free_list
);
886 /***********************************************************
888 * FUNCTIONS: Blackfin GPIO Driver
891 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
894 * DESCRIPTION: Blackfin GPIO Driver API
897 *************************************************************
898 * MODIFICATION HISTORY :
899 **************************************************************/
901 int bfin_gpio_request(unsigned gpio
, const char *label
)
905 if (check_gpio(gpio
) < 0)
908 flags
= hard_local_irq_save();
911 * Allow that the identical GPIO can
912 * be requested from the same driver twice
913 * Do nothing and return -
916 if (cmp_label(gpio
, label
) == 0) {
917 hard_local_irq_restore(flags
);
921 if (unlikely(is_reserved(gpio
, gpio
, 1))) {
922 if (system_state
== SYSTEM_BOOTING
)
924 printk(KERN_ERR
"bfin-gpio: GPIO %d is already reserved by %s !\n",
925 gpio
, get_label(gpio
));
926 hard_local_irq_restore(flags
);
929 if (unlikely(is_reserved(peri
, gpio
, 1))) {
930 if (system_state
== SYSTEM_BOOTING
)
933 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
934 gpio
, get_label(gpio
));
935 hard_local_irq_restore(flags
);
938 if (unlikely(is_reserved(gpio_irq
, gpio
, 1))) {
939 printk(KERN_NOTICE
"bfin-gpio: GPIO %d is already reserved as gpio-irq!"
940 " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio
);
942 #if !(defined(CONFIG_BF54x) || defined(CONFIG_BF60x))
943 else { /* Reset POLAR setting when acquiring a gpio for the first time */
944 set_gpio_polar(gpio
, 0);
949 set_label(gpio
, label
);
951 hard_local_irq_restore(flags
);
953 port_setup(gpio
, GPIO_USAGE
);
957 EXPORT_SYMBOL(bfin_gpio_request
);
959 void bfin_gpio_free(unsigned gpio
)
963 if (check_gpio(gpio
) < 0)
968 flags
= hard_local_irq_save();
970 if (unlikely(!is_reserved(gpio
, gpio
, 0))) {
971 if (system_state
== SYSTEM_BOOTING
)
974 hard_local_irq_restore(flags
);
978 unreserve(gpio
, gpio
);
980 set_label(gpio
, "free");
982 hard_local_irq_restore(flags
);
984 EXPORT_SYMBOL(bfin_gpio_free
);
986 #ifdef BFIN_SPECIAL_GPIO_BANKS
987 DECLARE_RESERVED_MAP(special_gpio
, gpio_bank(MAX_RESOURCES
));
989 int bfin_special_gpio_request(unsigned gpio
, const char *label
)
993 flags
= hard_local_irq_save();
996 * Allow that the identical GPIO can
997 * be requested from the same driver twice
998 * Do nothing and return -
1001 if (cmp_label(gpio
, label
) == 0) {
1002 hard_local_irq_restore(flags
);
1006 if (unlikely(is_reserved(special_gpio
, gpio
, 1))) {
1007 hard_local_irq_restore(flags
);
1008 printk(KERN_ERR
"bfin-gpio: GPIO %d is already reserved by %s !\n",
1009 gpio
, get_label(gpio
));
1013 if (unlikely(is_reserved(peri
, gpio
, 1))) {
1014 hard_local_irq_restore(flags
);
1016 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1017 gpio
, get_label(gpio
));
1022 reserve(special_gpio
, gpio
);
1023 reserve(peri
, gpio
);
1025 set_label(gpio
, label
);
1026 hard_local_irq_restore(flags
);
1027 port_setup(gpio
, GPIO_USAGE
);
1031 EXPORT_SYMBOL(bfin_special_gpio_request
);
1033 void bfin_special_gpio_free(unsigned gpio
)
1035 unsigned long flags
;
1039 flags
= hard_local_irq_save();
1041 if (unlikely(!is_reserved(special_gpio
, gpio
, 0))) {
1043 hard_local_irq_restore(flags
);
1047 unreserve(special_gpio
, gpio
);
1048 unreserve(peri
, gpio
);
1049 set_label(gpio
, "free");
1050 hard_local_irq_restore(flags
);
1052 EXPORT_SYMBOL(bfin_special_gpio_free
);
1056 int bfin_gpio_irq_request(unsigned gpio
, const char *label
)
1058 unsigned long flags
;
1060 if (check_gpio(gpio
) < 0)
1063 flags
= hard_local_irq_save();
1065 if (unlikely(is_reserved(peri
, gpio
, 1))) {
1066 if (system_state
== SYSTEM_BOOTING
)
1069 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1070 gpio
, get_label(gpio
));
1071 hard_local_irq_restore(flags
);
1074 if (unlikely(is_reserved(gpio
, gpio
, 1)))
1075 printk(KERN_NOTICE
"bfin-gpio: GPIO %d is already reserved by %s! "
1076 "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
1077 gpio
, get_label(gpio
));
1079 reserve(gpio_irq
, gpio
);
1080 set_label(gpio
, label
);
1082 hard_local_irq_restore(flags
);
1084 port_setup(gpio
, GPIO_USAGE
);
1089 void bfin_gpio_irq_free(unsigned gpio
)
1091 unsigned long flags
;
1093 if (check_gpio(gpio
) < 0)
1096 flags
= hard_local_irq_save();
1098 if (unlikely(!is_reserved(gpio_irq
, gpio
, 0))) {
1099 if (system_state
== SYSTEM_BOOTING
)
1102 hard_local_irq_restore(flags
);
1106 unreserve(gpio_irq
, gpio
);
1108 set_label(gpio
, "free");
1110 hard_local_irq_restore(flags
);
1113 static inline void __bfin_gpio_direction_input(unsigned gpio
)
1115 #if defined(CONFIG_BF54x) || defined(CONFIG_BF60x)
1116 gpio_array
[gpio_bank(gpio
)]->dir_clear
= gpio_bit(gpio
);
1118 gpio_array
[gpio_bank(gpio
)]->dir
&= ~gpio_bit(gpio
);
1120 gpio_array
[gpio_bank(gpio
)]->inen
|= gpio_bit(gpio
);
1123 int bfin_gpio_direction_input(unsigned gpio
)
1125 unsigned long flags
;
1127 if (unlikely(!is_reserved(gpio
, gpio
, 0))) {
1132 flags
= hard_local_irq_save();
1133 __bfin_gpio_direction_input(gpio
);
1134 AWA_DUMMY_READ(inen
);
1135 hard_local_irq_restore(flags
);
1139 EXPORT_SYMBOL(bfin_gpio_direction_input
);
1141 void bfin_gpio_irq_prepare(unsigned gpio
)
1143 #if defined(CONFIG_BF54x) || defined(CONFIG_BF60x)
1144 unsigned long flags
;
1147 port_setup(gpio
, GPIO_USAGE
);
1149 #if defined(CONFIG_BF54x) || defined(CONFIG_BF60x)
1150 flags
= hard_local_irq_save();
1151 __bfin_gpio_direction_input(gpio
);
1152 hard_local_irq_restore(flags
);
1156 void bfin_gpio_set_value(unsigned gpio
, int arg
)
1159 gpio_array
[gpio_bank(gpio
)]->data_set
= gpio_bit(gpio
);
1161 gpio_array
[gpio_bank(gpio
)]->data_clear
= gpio_bit(gpio
);
1163 EXPORT_SYMBOL(bfin_gpio_set_value
);
1165 int bfin_gpio_direction_output(unsigned gpio
, int value
)
1167 unsigned long flags
;
1169 if (unlikely(!is_reserved(gpio
, gpio
, 0))) {
1174 flags
= hard_local_irq_save();
1176 gpio_array
[gpio_bank(gpio
)]->inen
&= ~gpio_bit(gpio
);
1177 gpio_set_value(gpio
, value
);
1178 #if defined(CONFIG_BF54x) || defined(CONFIG_BF60x)
1179 gpio_array
[gpio_bank(gpio
)]->dir_set
= gpio_bit(gpio
);
1181 gpio_array
[gpio_bank(gpio
)]->dir
|= gpio_bit(gpio
);
1184 AWA_DUMMY_READ(dir
);
1185 hard_local_irq_restore(flags
);
1189 EXPORT_SYMBOL(bfin_gpio_direction_output
);
1191 int bfin_gpio_get_value(unsigned gpio
)
1193 #if defined(CONFIG_BF54x) || defined(CONFIG_BF60x)
1194 return (1 & (gpio_array
[gpio_bank(gpio
)]->data
>> gpio_sub_n(gpio
)));
1196 unsigned long flags
;
1198 if (unlikely(get_gpio_edge(gpio
))) {
1200 flags
= hard_local_irq_save();
1201 set_gpio_edge(gpio
, 0);
1202 ret
= get_gpio_data(gpio
);
1203 set_gpio_edge(gpio
, 1);
1204 hard_local_irq_restore(flags
);
1207 return get_gpio_data(gpio
);
1210 EXPORT_SYMBOL(bfin_gpio_get_value
);
1212 /* If we are booting from SPI and our board lacks a strong enough pull up,
1213 * the core can reset and execute the bootrom faster than the resistor can
1214 * pull the signal logically high. To work around this (common) error in
1215 * board design, we explicitly set the pin back to GPIO mode, force /CS
1216 * high, and wait for the electrons to do their thing.
1218 * This function only makes sense to be called from reset code, but it
1219 * lives here as we need to force all the GPIO states w/out going through
1220 * BUG() checks and such.
1222 void bfin_reset_boot_spi_cs(unsigned short pin
)
1224 unsigned short gpio
= P_IDENT(pin
);
1225 port_setup(gpio
, GPIO_USAGE
);
1226 gpio_array
[gpio_bank(gpio
)]->data_set
= gpio_bit(gpio
);
1227 AWA_DUMMY_READ(data_set
);
1231 #if defined(CONFIG_PROC_FS)
1232 static int gpio_proc_show(struct seq_file
*m
, void *v
)
1236 for (c
= 0; c
< MAX_RESOURCES
; c
++) {
1237 irq
= is_reserved(gpio_irq
, c
, 1);
1238 gpio
= is_reserved(gpio
, c
, 1);
1239 if (!check_gpio(c
) && (gpio
|| irq
))
1240 seq_printf(m
, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c
,
1241 get_label(c
), (gpio
&& irq
) ? " *" : "",
1242 get_gpio_dir(c
) ? "OUTPUT" : "INPUT");
1243 else if (is_reserved(peri
, c
, 1))
1244 seq_printf(m
, "GPIO_%d: \t%s \t\tPeripheral\n", c
, get_label(c
));
1252 static int gpio_proc_open(struct inode
*inode
, struct file
*file
)
1254 return single_open(file
, gpio_proc_show
, NULL
);
1257 static const struct file_operations gpio_proc_ops
= {
1258 .open
= gpio_proc_open
,
1260 .llseek
= seq_lseek
,
1261 .release
= single_release
,
1264 static __init
int gpio_register_proc(void)
1266 struct proc_dir_entry
*proc_gpio
;
1268 proc_gpio
= proc_create("gpio", S_IRUGO
, NULL
, &gpio_proc_ops
);
1269 return proc_gpio
!= NULL
;
1271 __initcall(gpio_register_proc
);
1274 #ifdef CONFIG_GPIOLIB
1275 static int bfin_gpiolib_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
1277 return bfin_gpio_direction_input(gpio
);
1280 static int bfin_gpiolib_direction_output(struct gpio_chip
*chip
, unsigned gpio
, int level
)
1282 return bfin_gpio_direction_output(gpio
, level
);
1285 static int bfin_gpiolib_get_value(struct gpio_chip
*chip
, unsigned gpio
)
1287 return bfin_gpio_get_value(gpio
);
1290 static void bfin_gpiolib_set_value(struct gpio_chip
*chip
, unsigned gpio
, int value
)
1292 return bfin_gpio_set_value(gpio
, value
);
1295 static int bfin_gpiolib_gpio_request(struct gpio_chip
*chip
, unsigned gpio
)
1297 return bfin_gpio_request(gpio
, chip
->label
);
1300 static void bfin_gpiolib_gpio_free(struct gpio_chip
*chip
, unsigned gpio
)
1302 return bfin_gpio_free(gpio
);
1305 static int bfin_gpiolib_gpio_to_irq(struct gpio_chip
*chip
, unsigned gpio
)
1307 return gpio
+ GPIO_IRQ_BASE
;
1310 static struct gpio_chip bfin_chip
= {
1311 .label
= "BFIN-GPIO",
1312 .direction_input
= bfin_gpiolib_direction_input
,
1313 .get
= bfin_gpiolib_get_value
,
1314 .direction_output
= bfin_gpiolib_direction_output
,
1315 .set
= bfin_gpiolib_set_value
,
1316 .request
= bfin_gpiolib_gpio_request
,
1317 .free
= bfin_gpiolib_gpio_free
,
1318 .to_irq
= bfin_gpiolib_gpio_to_irq
,
1320 .ngpio
= MAX_BLACKFIN_GPIOS
,
1323 static int __init
bfin_gpiolib_setup(void)
1325 return gpiochip_add(&bfin_chip
);
1327 arch_initcall(bfin_gpiolib_setup
);