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 <asm/blackfin.h>
15 #include <asm/portmux.h>
16 #include <linux/irq.h>
18 #if ANOMALY_05000311 || ANOMALY_05000323
21 AWA_data_clear
= SYSCR
,
24 AWA_maska
= BFIN_UART_SCR
,
25 AWA_maska_clear
= BFIN_UART_SCR
,
26 AWA_maska_set
= BFIN_UART_SCR
,
27 AWA_maska_toggle
= BFIN_UART_SCR
,
28 AWA_maskb
= BFIN_UART_GCTL
,
29 AWA_maskb_clear
= BFIN_UART_GCTL
,
30 AWA_maskb_set
= BFIN_UART_GCTL
,
31 AWA_maskb_toggle
= BFIN_UART_GCTL
,
32 AWA_dir
= SPORT1_STAT
,
33 AWA_polar
= SPORT1_STAT
,
34 AWA_edge
= SPORT1_STAT
,
35 AWA_both
= SPORT1_STAT
,
37 AWA_inen
= TIMER_ENABLE
,
38 #elif ANOMALY_05000323
39 AWA_inen
= DMA1_1_CONFIG
,
42 /* Anomaly Workaround */
43 #define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
45 #define AWA_DUMMY_READ(...) do { } while (0)
48 static struct gpio_port_t
* const gpio_array
[] = {
49 #if defined(BF533_FAMILY) || defined(BF538_FAMILY)
50 (struct gpio_port_t
*) FIO_FLAG_D
,
51 #elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
52 (struct gpio_port_t
*) PORTFIO
,
53 (struct gpio_port_t
*) PORTGIO
,
54 (struct gpio_port_t
*) PORTHIO
,
55 #elif defined(BF561_FAMILY)
56 (struct gpio_port_t
*) FIO0_FLAG_D
,
57 (struct gpio_port_t
*) FIO1_FLAG_D
,
58 (struct gpio_port_t
*) FIO2_FLAG_D
,
59 #elif defined(CONFIG_BF54x)
60 (struct gpio_port_t
*)PORTA_FER
,
61 (struct gpio_port_t
*)PORTB_FER
,
62 (struct gpio_port_t
*)PORTC_FER
,
63 (struct gpio_port_t
*)PORTD_FER
,
64 (struct gpio_port_t
*)PORTE_FER
,
65 (struct gpio_port_t
*)PORTF_FER
,
66 (struct gpio_port_t
*)PORTG_FER
,
67 (struct gpio_port_t
*)PORTH_FER
,
68 (struct gpio_port_t
*)PORTI_FER
,
69 (struct gpio_port_t
*)PORTJ_FER
,
71 # error no gpio arrays defined
75 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
76 static unsigned short * const port_fer
[] = {
77 (unsigned short *) PORTF_FER
,
78 (unsigned short *) PORTG_FER
,
79 (unsigned short *) PORTH_FER
,
82 # if !defined(BF537_FAMILY)
83 static unsigned short * const port_mux
[] = {
84 (unsigned short *) PORTF_MUX
,
85 (unsigned short *) PORTG_MUX
,
86 (unsigned short *) PORTH_MUX
,
90 u8 pmux_offset
[][16] = {
91 # if defined(CONFIG_BF52x)
92 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
93 { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
94 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
95 # elif defined(CONFIG_BF51x)
96 { 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
97 { 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
98 { 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
103 #elif defined(BF538_FAMILY)
104 static unsigned short * const port_fer
[] = {
105 (unsigned short *) PORTCIO_FER
,
106 (unsigned short *) PORTDIO_FER
,
107 (unsigned short *) PORTEIO_FER
,
111 #define RESOURCE_LABEL_SIZE 16
113 static struct str_ident
{
114 char name
[RESOURCE_LABEL_SIZE
];
115 } str_ident
[MAX_RESOURCES
];
117 #if defined(CONFIG_PM)
118 static struct gpio_port_s gpio_bank_saved
[GPIO_BANK_NUM
];
121 static void gpio_error(unsigned gpio
)
123 printk(KERN_ERR
"bfin-gpio: GPIO %d wasn't requested!\n", gpio
);
126 static void set_label(unsigned short ident
, const char *label
)
129 strncpy(str_ident
[ident
].name
, label
,
130 RESOURCE_LABEL_SIZE
);
131 str_ident
[ident
].name
[RESOURCE_LABEL_SIZE
- 1] = 0;
135 static char *get_label(unsigned short ident
)
137 return (*str_ident
[ident
].name
? str_ident
[ident
].name
: "UNKNOWN");
140 static int cmp_label(unsigned short ident
, const char *label
)
144 printk(KERN_ERR
"Please provide none-null label\n");
148 return strcmp(str_ident
[ident
].name
, label
);
153 #define map_entry(m, i) reserved_##m##_map[gpio_bank(i)]
154 #define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
155 #define reserve(m, i) (map_entry(m, i) |= gpio_bit(i))
156 #define unreserve(m, i) (map_entry(m, i) &= ~gpio_bit(i))
157 #define DECLARE_RESERVED_MAP(m, c) static unsigned short reserved_##m##_map[c]
159 DECLARE_RESERVED_MAP(gpio
, GPIO_BANK_NUM
);
160 DECLARE_RESERVED_MAP(peri
, DIV_ROUND_UP(MAX_RESOURCES
, GPIO_BANKSIZE
));
161 DECLARE_RESERVED_MAP(gpio_irq
, GPIO_BANK_NUM
);
163 inline int check_gpio(unsigned gpio
)
165 #if defined(CONFIG_BF54x)
166 if (gpio
== GPIO_PB15
|| gpio
== GPIO_PC14
|| gpio
== GPIO_PC15
167 || gpio
== GPIO_PH14
|| gpio
== GPIO_PH15
168 || gpio
== GPIO_PJ14
|| gpio
== GPIO_PJ15
)
171 if (gpio
>= MAX_BLACKFIN_GPIOS
)
176 static void port_setup(unsigned gpio
, unsigned short usage
)
178 #if defined(BF538_FAMILY)
180 * BF538/9 Port C,D and E are special.
181 * Inverted PORT_FER polarity on CDE and no PORF_FER on F
182 * Regular PORT F GPIOs are handled here, CDE are exclusively
186 if (gpio
< MAX_BLACKFIN_GPIOS
|| gpio
>= MAX_RESOURCES
)
189 gpio
-= MAX_BLACKFIN_GPIOS
;
191 if (usage
== GPIO_USAGE
)
192 *port_fer
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
194 *port_fer
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
199 if (check_gpio(gpio
))
202 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
203 if (usage
== GPIO_USAGE
)
204 *port_fer
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
206 *port_fer
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
208 #elif defined(CONFIG_BF54x)
209 if (usage
== GPIO_USAGE
)
210 gpio_array
[gpio_bank(gpio
)]->port_fer
&= ~gpio_bit(gpio
);
212 gpio_array
[gpio_bank(gpio
)]->port_fer
|= gpio_bit(gpio
);
218 static const s8 port_mux
[] = {
227 [GPIO_PF8
... GPIO_PF15
] = -1,
228 [GPIO_PG0
... GPIO_PG7
] = -1,
237 [GPIO_PH0
... GPIO_PH15
] = -1,
238 [PORT_PJ0
... PORT_PJ3
] = -1,
241 [PORT_PJ6
... PORT_PJ9
] = -1,
246 static int portmux_group_check(unsigned short per
)
248 u16 ident
= P_IDENT(per
);
249 u16 function
= P_FUNCT2MUX(per
);
250 s8 offset
= port_mux
[ident
];
256 pmux
= bfin_read_PORT_MUX();
257 for (m
= 0; m
< ARRAY_SIZE(port_mux
); ++m
) {
260 if (port_mux
[m
] != offset
)
262 if (!is_reserved(peri
, m
, 1))
266 pfunc
= (pmux
>> offset
) & 3;
268 pfunc
= (pmux
>> offset
) & 1;
269 if (pfunc
!= function
) {
270 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
271 ident
, function
, m
, pfunc
);
279 static void portmux_setup(unsigned short per
)
281 u16 ident
= P_IDENT(per
);
282 u16 function
= P_FUNCT2MUX(per
);
283 s8 offset
= port_mux
[ident
];
289 pmux
= bfin_read_PORT_MUX();
291 pmux
&= ~(1 << offset
);
294 pmux
|= (function
<< offset
);
295 bfin_write_PORT_MUX(pmux
);
297 #elif defined(CONFIG_BF54x)
298 inline void portmux_setup(unsigned short per
)
300 u16 ident
= P_IDENT(per
);
301 u16 function
= P_FUNCT2MUX(per
);
304 pmux
= gpio_array
[gpio_bank(ident
)]->port_mux
;
306 pmux
&= ~(0x3 << (2 * gpio_sub_n(ident
)));
307 pmux
|= (function
& 0x3) << (2 * gpio_sub_n(ident
));
309 gpio_array
[gpio_bank(ident
)]->port_mux
= pmux
;
312 inline u16
get_portmux(unsigned short per
)
314 u16 ident
= P_IDENT(per
);
315 u32 pmux
= gpio_array
[gpio_bank(ident
)]->port_mux
;
316 return (pmux
>> (2 * gpio_sub_n(ident
)) & 0x3);
318 static int portmux_group_check(unsigned short per
)
322 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
323 static int portmux_group_check(unsigned short per
)
325 u16 ident
= P_IDENT(per
);
326 u16 function
= P_FUNCT2MUX(per
);
327 u8 offset
= pmux_offset
[gpio_bank(ident
)][gpio_sub_n(ident
)];
328 u16 pin
, gpiopin
, pfunc
;
330 for (pin
= 0; pin
< GPIO_BANKSIZE
; ++pin
) {
331 if (offset
!= pmux_offset
[gpio_bank(ident
)][pin
])
334 gpiopin
= gpio_bank(ident
) * GPIO_BANKSIZE
+ pin
;
335 if (gpiopin
== ident
)
337 if (!is_reserved(peri
, gpiopin
, 1))
340 pfunc
= *port_mux
[gpio_bank(ident
)];
341 pfunc
= (pfunc
>> offset
) & 3;
342 if (pfunc
!= function
) {
343 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
344 ident
, function
, gpiopin
, pfunc
);
352 inline void portmux_setup(unsigned short per
)
354 u16 ident
= P_IDENT(per
);
355 u16 function
= P_FUNCT2MUX(per
);
356 u8 offset
= pmux_offset
[gpio_bank(ident
)][gpio_sub_n(ident
)];
359 pmux
= *port_mux
[gpio_bank(ident
)];
360 if (((pmux
>> offset
) & 3) == function
)
362 pmux
&= ~(3 << offset
);
363 pmux
|= (function
& 3) << offset
;
364 *port_mux
[gpio_bank(ident
)] = pmux
;
368 # define portmux_setup(...) do { } while (0)
369 static int portmux_group_check(unsigned short per
)
376 /***********************************************************
378 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
381 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
384 * DESCRIPTION: These functions abstract direct register access
385 * to Blackfin processor General Purpose
388 * CAUTION: These functions do not belong to the GPIO Driver API
389 *************************************************************
390 * MODIFICATION HISTORY :
391 **************************************************************/
393 /* Set a specific bit */
395 #define SET_GPIO(name) \
396 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
398 unsigned long flags; \
399 flags = hard_local_irq_save(); \
401 gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
403 gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
404 AWA_DUMMY_READ(name); \
405 hard_local_irq_restore(flags); \
407 EXPORT_SYMBOL(set_gpio_ ## name);
409 SET_GPIO(dir
) /* set_gpio_dir() */
410 SET_GPIO(inen
) /* set_gpio_inen() */
411 SET_GPIO(polar
) /* set_gpio_polar() */
412 SET_GPIO(edge
) /* set_gpio_edge() */
413 SET_GPIO(both
) /* set_gpio_both() */
416 #define SET_GPIO_SC(name) \
417 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
419 unsigned long flags; \
420 if (ANOMALY_05000311 || ANOMALY_05000323) \
421 flags = hard_local_irq_save(); \
423 gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
425 gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
426 if (ANOMALY_05000311 || ANOMALY_05000323) { \
427 AWA_DUMMY_READ(name); \
428 hard_local_irq_restore(flags); \
431 EXPORT_SYMBOL(set_gpio_ ## name);
437 void set_gpio_toggle(unsigned gpio
)
440 if (ANOMALY_05000311
|| ANOMALY_05000323
)
441 flags
= hard_local_irq_save();
442 gpio_array
[gpio_bank(gpio
)]->toggle
= gpio_bit(gpio
);
443 if (ANOMALY_05000311
|| ANOMALY_05000323
) {
444 AWA_DUMMY_READ(toggle
);
445 hard_local_irq_restore(flags
);
448 EXPORT_SYMBOL(set_gpio_toggle
);
451 /*Set current PORT date (16-bit word)*/
453 #define SET_GPIO_P(name) \
454 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
456 unsigned long flags; \
457 if (ANOMALY_05000311 || ANOMALY_05000323) \
458 flags = hard_local_irq_save(); \
459 gpio_array[gpio_bank(gpio)]->name = arg; \
460 if (ANOMALY_05000311 || ANOMALY_05000323) { \
461 AWA_DUMMY_READ(name); \
462 hard_local_irq_restore(flags); \
465 EXPORT_SYMBOL(set_gpiop_ ## name);
476 /* Get a specific bit */
477 #define GET_GPIO(name) \
478 unsigned short get_gpio_ ## name(unsigned gpio) \
480 unsigned long flags; \
481 unsigned short ret; \
482 if (ANOMALY_05000311 || ANOMALY_05000323) \
483 flags = hard_local_irq_save(); \
484 ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
485 if (ANOMALY_05000311 || ANOMALY_05000323) { \
486 AWA_DUMMY_READ(name); \
487 hard_local_irq_restore(flags); \
491 EXPORT_SYMBOL(get_gpio_ ## name);
502 /*Get current PORT date (16-bit word)*/
504 #define GET_GPIO_P(name) \
505 unsigned short get_gpiop_ ## name(unsigned gpio) \
507 unsigned long flags; \
508 unsigned short ret; \
509 if (ANOMALY_05000311 || ANOMALY_05000323) \
510 flags = hard_local_irq_save(); \
511 ret = (gpio_array[gpio_bank(gpio)]->name); \
512 if (ANOMALY_05000311 || ANOMALY_05000323) { \
513 AWA_DUMMY_READ(name); \
514 hard_local_irq_restore(flags); \
518 EXPORT_SYMBOL(get_gpiop_ ## name);
531 DECLARE_RESERVED_MAP(wakeup
, GPIO_BANK_NUM
);
533 static const unsigned int sic_iwr_irqs
[] = {
534 #if defined(BF533_FAMILY)
536 #elif defined(BF537_FAMILY)
537 IRQ_PROG_INTB
, IRQ_PORTG_INTB
, IRQ_MAC_TX
538 #elif defined(BF538_FAMILY)
540 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
541 IRQ_PORTF_INTB
, IRQ_PORTG_INTB
, IRQ_PORTH_INTB
542 #elif defined(BF561_FAMILY)
543 IRQ_PROG0_INTB
, IRQ_PROG1_INTB
, IRQ_PROG2_INTB
545 # error no SIC_IWR defined
549 /***********************************************************
551 * FUNCTIONS: Blackfin PM Setup API
554 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
562 * DESCRIPTION: Blackfin PM Driver API
565 *************************************************************
566 * MODIFICATION HISTORY :
567 **************************************************************/
568 int gpio_pm_wakeup_ctrl(unsigned gpio
, unsigned ctrl
)
572 if (check_gpio(gpio
) < 0)
575 flags
= hard_local_irq_save();
577 reserve(wakeup
, gpio
);
579 unreserve(wakeup
, gpio
);
581 set_gpio_maskb(gpio
, ctrl
);
582 hard_local_irq_restore(flags
);
587 int bfin_pm_standby_ctrl(unsigned ctrl
)
591 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
592 mask
= map_entry(wakeup
, i
);
596 bfin_internal_set_wake(sic_iwr_irqs
[bank
], ctrl
);
601 void bfin_gpio_pm_hibernate_suspend(void)
605 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
608 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
609 gpio_bank_saved
[bank
].fer
= *port_fer
[bank
];
610 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
611 gpio_bank_saved
[bank
].mux
= *port_mux
[bank
];
614 gpio_bank_saved
[bank
].mux
= bfin_read_PORT_MUX();
617 gpio_bank_saved
[bank
].data
= gpio_array
[bank
]->data
;
618 gpio_bank_saved
[bank
].inen
= gpio_array
[bank
]->inen
;
619 gpio_bank_saved
[bank
].polar
= gpio_array
[bank
]->polar
;
620 gpio_bank_saved
[bank
].dir
= gpio_array
[bank
]->dir
;
621 gpio_bank_saved
[bank
].edge
= gpio_array
[bank
]->edge
;
622 gpio_bank_saved
[bank
].both
= gpio_array
[bank
]->both
;
623 gpio_bank_saved
[bank
].maska
= gpio_array
[bank
]->maska
;
626 AWA_DUMMY_READ(maska
);
629 void bfin_gpio_pm_hibernate_restore(void)
633 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
636 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
637 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
638 *port_mux
[bank
] = gpio_bank_saved
[bank
].mux
;
641 bfin_write_PORT_MUX(gpio_bank_saved
[bank
].mux
);
643 *port_fer
[bank
] = gpio_bank_saved
[bank
].fer
;
645 gpio_array
[bank
]->inen
= gpio_bank_saved
[bank
].inen
;
646 gpio_array
[bank
]->data_set
= gpio_bank_saved
[bank
].data
647 & gpio_bank_saved
[bank
].dir
;
648 gpio_array
[bank
]->dir
= gpio_bank_saved
[bank
].dir
;
649 gpio_array
[bank
]->polar
= gpio_bank_saved
[bank
].polar
;
650 gpio_array
[bank
]->edge
= gpio_bank_saved
[bank
].edge
;
651 gpio_array
[bank
]->both
= gpio_bank_saved
[bank
].both
;
652 gpio_array
[bank
]->maska
= gpio_bank_saved
[bank
].maska
;
654 AWA_DUMMY_READ(maska
);
659 #else /* CONFIG_BF54x */
662 int bfin_pm_standby_ctrl(unsigned ctrl
)
667 void bfin_gpio_pm_hibernate_suspend(void)
671 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
674 gpio_bank_saved
[bank
].fer
= gpio_array
[bank
]->port_fer
;
675 gpio_bank_saved
[bank
].mux
= gpio_array
[bank
]->port_mux
;
676 gpio_bank_saved
[bank
].data
= gpio_array
[bank
]->data
;
677 gpio_bank_saved
[bank
].inen
= gpio_array
[bank
]->inen
;
678 gpio_bank_saved
[bank
].dir
= gpio_array
[bank
]->dir_set
;
682 void bfin_gpio_pm_hibernate_restore(void)
686 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
689 gpio_array
[bank
]->port_mux
= gpio_bank_saved
[bank
].mux
;
690 gpio_array
[bank
]->port_fer
= gpio_bank_saved
[bank
].fer
;
691 gpio_array
[bank
]->inen
= gpio_bank_saved
[bank
].inen
;
692 gpio_array
[bank
]->dir_set
= gpio_bank_saved
[bank
].dir
;
693 gpio_array
[bank
]->data_set
= gpio_bank_saved
[bank
].data
694 | gpio_bank_saved
[bank
].dir
;
699 unsigned short get_gpio_dir(unsigned gpio
)
701 return (0x01 & (gpio_array
[gpio_bank(gpio
)]->dir_clear
>> gpio_sub_n(gpio
)));
703 EXPORT_SYMBOL(get_gpio_dir
);
705 #endif /* CONFIG_BF54x */
707 /***********************************************************
709 * FUNCTIONS: Blackfin Peripheral Resource Allocation
713 * per Peripheral Identifier
716 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
719 *************************************************************
720 * MODIFICATION HISTORY :
721 **************************************************************/
723 int peripheral_request(unsigned short per
, const char *label
)
726 unsigned short ident
= P_IDENT(per
);
729 * Don't cares are pins with only one dedicated function
732 if (per
& P_DONTCARE
)
735 if (!(per
& P_DEFINED
))
738 BUG_ON(ident
>= MAX_RESOURCES
);
740 flags
= hard_local_irq_save();
742 /* If a pin can be muxed as either GPIO or peripheral, make
743 * sure it is not already a GPIO pin when we request it.
745 if (unlikely(!check_gpio(ident
) && is_reserved(gpio
, ident
, 1))) {
746 if (system_state
== SYSTEM_BOOTING
)
749 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
750 __func__
, ident
, get_label(ident
));
751 hard_local_irq_restore(flags
);
755 if (unlikely(is_reserved(peri
, ident
, 1))) {
758 * Pin functions like AMC address strobes my
759 * be requested and used by several drivers
763 if (!((per
& P_MAYSHARE
) && get_portmux(per
) == P_FUNCT2MUX(per
))) {
765 if (!(per
& P_MAYSHARE
)) {
768 * Allow that the identical pin function can
769 * be requested from the same driver twice
772 if (cmp_label(ident
, label
) == 0)
775 if (system_state
== SYSTEM_BOOTING
)
778 "%s: Peripheral %d function %d is already reserved by %s !\n",
779 __func__
, ident
, P_FUNCT2MUX(per
), get_label(ident
));
780 hard_local_irq_restore(flags
);
785 if (unlikely(portmux_group_check(per
))) {
786 hard_local_irq_restore(flags
);
790 reserve(peri
, ident
);
793 port_setup(ident
, PERIPHERAL_USAGE
);
795 hard_local_irq_restore(flags
);
796 set_label(ident
, label
);
800 EXPORT_SYMBOL(peripheral_request
);
802 int peripheral_request_list(const unsigned short per
[], const char *label
)
807 for (cnt
= 0; per
[cnt
] != 0; cnt
++) {
809 ret
= peripheral_request(per
[cnt
], label
);
812 for ( ; cnt
> 0; cnt
--)
813 peripheral_free(per
[cnt
- 1]);
821 EXPORT_SYMBOL(peripheral_request_list
);
823 void peripheral_free(unsigned short per
)
826 unsigned short ident
= P_IDENT(per
);
828 if (per
& P_DONTCARE
)
831 if (!(per
& P_DEFINED
))
834 flags
= hard_local_irq_save();
836 if (unlikely(!is_reserved(peri
, ident
, 0))) {
837 hard_local_irq_restore(flags
);
841 if (!(per
& P_MAYSHARE
))
842 port_setup(ident
, GPIO_USAGE
);
844 unreserve(peri
, ident
);
846 set_label(ident
, "free");
848 hard_local_irq_restore(flags
);
850 EXPORT_SYMBOL(peripheral_free
);
852 void peripheral_free_list(const unsigned short per
[])
855 for (cnt
= 0; per
[cnt
] != 0; cnt
++)
856 peripheral_free(per
[cnt
]);
858 EXPORT_SYMBOL(peripheral_free_list
);
860 /***********************************************************
862 * FUNCTIONS: Blackfin GPIO Driver
865 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
868 * DESCRIPTION: Blackfin GPIO Driver API
871 *************************************************************
872 * MODIFICATION HISTORY :
873 **************************************************************/
875 int bfin_gpio_request(unsigned gpio
, const char *label
)
879 if (check_gpio(gpio
) < 0)
882 flags
= hard_local_irq_save();
885 * Allow that the identical GPIO can
886 * be requested from the same driver twice
887 * Do nothing and return -
890 if (cmp_label(gpio
, label
) == 0) {
891 hard_local_irq_restore(flags
);
895 if (unlikely(is_reserved(gpio
, gpio
, 1))) {
896 if (system_state
== SYSTEM_BOOTING
)
898 printk(KERN_ERR
"bfin-gpio: GPIO %d is already reserved by %s !\n",
899 gpio
, get_label(gpio
));
900 hard_local_irq_restore(flags
);
903 if (unlikely(is_reserved(peri
, gpio
, 1))) {
904 if (system_state
== SYSTEM_BOOTING
)
907 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
908 gpio
, get_label(gpio
));
909 hard_local_irq_restore(flags
);
912 if (unlikely(is_reserved(gpio_irq
, gpio
, 1))) {
913 printk(KERN_NOTICE
"bfin-gpio: GPIO %d is already reserved as gpio-irq!"
914 " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio
);
917 else { /* Reset POLAR setting when acquiring a gpio for the first time */
918 set_gpio_polar(gpio
, 0);
923 set_label(gpio
, label
);
925 hard_local_irq_restore(flags
);
927 port_setup(gpio
, GPIO_USAGE
);
931 EXPORT_SYMBOL(bfin_gpio_request
);
933 void bfin_gpio_free(unsigned gpio
)
937 if (check_gpio(gpio
) < 0)
942 flags
= hard_local_irq_save();
944 if (unlikely(!is_reserved(gpio
, gpio
, 0))) {
945 if (system_state
== SYSTEM_BOOTING
)
948 hard_local_irq_restore(flags
);
952 unreserve(gpio
, gpio
);
954 set_label(gpio
, "free");
956 hard_local_irq_restore(flags
);
958 EXPORT_SYMBOL(bfin_gpio_free
);
960 #ifdef BFIN_SPECIAL_GPIO_BANKS
961 DECLARE_RESERVED_MAP(special_gpio
, gpio_bank(MAX_RESOURCES
));
963 int bfin_special_gpio_request(unsigned gpio
, const char *label
)
967 flags
= hard_local_irq_save();
970 * Allow that the identical GPIO can
971 * be requested from the same driver twice
972 * Do nothing and return -
975 if (cmp_label(gpio
, label
) == 0) {
976 hard_local_irq_restore(flags
);
980 if (unlikely(is_reserved(special_gpio
, gpio
, 1))) {
981 hard_local_irq_restore(flags
);
982 printk(KERN_ERR
"bfin-gpio: GPIO %d is already reserved by %s !\n",
983 gpio
, get_label(gpio
));
987 if (unlikely(is_reserved(peri
, gpio
, 1))) {
988 hard_local_irq_restore(flags
);
990 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
991 gpio
, get_label(gpio
));
996 reserve(special_gpio
, gpio
);
999 set_label(gpio
, label
);
1000 hard_local_irq_restore(flags
);
1001 port_setup(gpio
, GPIO_USAGE
);
1005 EXPORT_SYMBOL(bfin_special_gpio_request
);
1007 void bfin_special_gpio_free(unsigned gpio
)
1009 unsigned long flags
;
1013 flags
= hard_local_irq_save();
1015 if (unlikely(!is_reserved(special_gpio
, gpio
, 0))) {
1017 hard_local_irq_restore(flags
);
1021 unreserve(special_gpio
, gpio
);
1022 unreserve(peri
, gpio
);
1023 set_label(gpio
, "free");
1024 hard_local_irq_restore(flags
);
1026 EXPORT_SYMBOL(bfin_special_gpio_free
);
1030 int bfin_gpio_irq_request(unsigned gpio
, const char *label
)
1032 unsigned long flags
;
1034 if (check_gpio(gpio
) < 0)
1037 flags
= hard_local_irq_save();
1039 if (unlikely(is_reserved(peri
, gpio
, 1))) {
1040 if (system_state
== SYSTEM_BOOTING
)
1043 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1044 gpio
, get_label(gpio
));
1045 hard_local_irq_restore(flags
);
1048 if (unlikely(is_reserved(gpio
, gpio
, 1)))
1049 printk(KERN_NOTICE
"bfin-gpio: GPIO %d is already reserved by %s! "
1050 "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
1051 gpio
, get_label(gpio
));
1053 reserve(gpio_irq
, gpio
);
1054 set_label(gpio
, label
);
1056 hard_local_irq_restore(flags
);
1058 port_setup(gpio
, GPIO_USAGE
);
1063 void bfin_gpio_irq_free(unsigned gpio
)
1065 unsigned long flags
;
1067 if (check_gpio(gpio
) < 0)
1070 flags
= hard_local_irq_save();
1072 if (unlikely(!is_reserved(gpio_irq
, gpio
, 0))) {
1073 if (system_state
== SYSTEM_BOOTING
)
1076 hard_local_irq_restore(flags
);
1080 unreserve(gpio_irq
, gpio
);
1082 set_label(gpio
, "free");
1084 hard_local_irq_restore(flags
);
1087 static inline void __bfin_gpio_direction_input(unsigned gpio
)
1090 gpio_array
[gpio_bank(gpio
)]->dir_clear
= gpio_bit(gpio
);
1092 gpio_array
[gpio_bank(gpio
)]->dir
&= ~gpio_bit(gpio
);
1094 gpio_array
[gpio_bank(gpio
)]->inen
|= gpio_bit(gpio
);
1097 int bfin_gpio_direction_input(unsigned gpio
)
1099 unsigned long flags
;
1101 if (unlikely(!is_reserved(gpio
, gpio
, 0))) {
1106 flags
= hard_local_irq_save();
1107 __bfin_gpio_direction_input(gpio
);
1108 AWA_DUMMY_READ(inen
);
1109 hard_local_irq_restore(flags
);
1113 EXPORT_SYMBOL(bfin_gpio_direction_input
);
1115 void bfin_gpio_irq_prepare(unsigned gpio
)
1118 unsigned long flags
;
1121 port_setup(gpio
, GPIO_USAGE
);
1124 flags
= hard_local_irq_save();
1125 __bfin_gpio_direction_input(gpio
);
1126 hard_local_irq_restore(flags
);
1130 void bfin_gpio_set_value(unsigned gpio
, int arg
)
1133 gpio_array
[gpio_bank(gpio
)]->data_set
= gpio_bit(gpio
);
1135 gpio_array
[gpio_bank(gpio
)]->data_clear
= gpio_bit(gpio
);
1137 EXPORT_SYMBOL(bfin_gpio_set_value
);
1139 int bfin_gpio_direction_output(unsigned gpio
, int value
)
1141 unsigned long flags
;
1143 if (unlikely(!is_reserved(gpio
, gpio
, 0))) {
1148 flags
= hard_local_irq_save();
1150 gpio_array
[gpio_bank(gpio
)]->inen
&= ~gpio_bit(gpio
);
1151 gpio_set_value(gpio
, value
);
1153 gpio_array
[gpio_bank(gpio
)]->dir_set
= gpio_bit(gpio
);
1155 gpio_array
[gpio_bank(gpio
)]->dir
|= gpio_bit(gpio
);
1158 AWA_DUMMY_READ(dir
);
1159 hard_local_irq_restore(flags
);
1163 EXPORT_SYMBOL(bfin_gpio_direction_output
);
1165 int bfin_gpio_get_value(unsigned gpio
)
1168 return (1 & (gpio_array
[gpio_bank(gpio
)]->data
>> gpio_sub_n(gpio
)));
1170 unsigned long flags
;
1172 if (unlikely(get_gpio_edge(gpio
))) {
1174 flags
= hard_local_irq_save();
1175 set_gpio_edge(gpio
, 0);
1176 ret
= get_gpio_data(gpio
);
1177 set_gpio_edge(gpio
, 1);
1178 hard_local_irq_restore(flags
);
1181 return get_gpio_data(gpio
);
1184 EXPORT_SYMBOL(bfin_gpio_get_value
);
1186 /* If we are booting from SPI and our board lacks a strong enough pull up,
1187 * the core can reset and execute the bootrom faster than the resistor can
1188 * pull the signal logically high. To work around this (common) error in
1189 * board design, we explicitly set the pin back to GPIO mode, force /CS
1190 * high, and wait for the electrons to do their thing.
1192 * This function only makes sense to be called from reset code, but it
1193 * lives here as we need to force all the GPIO states w/out going through
1194 * BUG() checks and such.
1196 void bfin_reset_boot_spi_cs(unsigned short pin
)
1198 unsigned short gpio
= P_IDENT(pin
);
1199 port_setup(gpio
, GPIO_USAGE
);
1200 gpio_array
[gpio_bank(gpio
)]->data_set
= gpio_bit(gpio
);
1201 AWA_DUMMY_READ(data_set
);
1205 #if defined(CONFIG_PROC_FS)
1206 static int gpio_proc_read(char *buf
, char **start
, off_t offset
,
1207 int len
, int *unused_i
, void *unused_v
)
1209 int c
, irq
, gpio
, outlen
= 0;
1211 for (c
= 0; c
< MAX_RESOURCES
; c
++) {
1212 irq
= is_reserved(gpio_irq
, c
, 1);
1213 gpio
= is_reserved(gpio
, c
, 1);
1214 if (!check_gpio(c
) && (gpio
|| irq
))
1215 len
= sprintf(buf
, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c
,
1216 get_label(c
), (gpio
&& irq
) ? " *" : "",
1217 get_gpio_dir(c
) ? "OUTPUT" : "INPUT");
1218 else if (is_reserved(peri
, c
, 1))
1219 len
= sprintf(buf
, "GPIO_%d: \t%s \t\tPeripheral\n", c
, get_label(c
));
1228 static __init
int gpio_register_proc(void)
1230 struct proc_dir_entry
*proc_gpio
;
1232 proc_gpio
= create_proc_entry("gpio", S_IRUGO
, NULL
);
1234 proc_gpio
->read_proc
= gpio_proc_read
;
1235 return proc_gpio
!= NULL
;
1237 __initcall(gpio_register_proc
);
1240 #ifdef CONFIG_GPIOLIB
1241 static int bfin_gpiolib_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
1243 return bfin_gpio_direction_input(gpio
);
1246 static int bfin_gpiolib_direction_output(struct gpio_chip
*chip
, unsigned gpio
, int level
)
1248 return bfin_gpio_direction_output(gpio
, level
);
1251 static int bfin_gpiolib_get_value(struct gpio_chip
*chip
, unsigned gpio
)
1253 return bfin_gpio_get_value(gpio
);
1256 static void bfin_gpiolib_set_value(struct gpio_chip
*chip
, unsigned gpio
, int value
)
1258 return bfin_gpio_set_value(gpio
, value
);
1261 static int bfin_gpiolib_gpio_request(struct gpio_chip
*chip
, unsigned gpio
)
1263 return bfin_gpio_request(gpio
, chip
->label
);
1266 static void bfin_gpiolib_gpio_free(struct gpio_chip
*chip
, unsigned gpio
)
1268 return bfin_gpio_free(gpio
);
1271 static int bfin_gpiolib_gpio_to_irq(struct gpio_chip
*chip
, unsigned gpio
)
1273 return gpio
+ GPIO_IRQ_BASE
;
1276 static struct gpio_chip bfin_chip
= {
1277 .label
= "BFIN-GPIO",
1278 .direction_input
= bfin_gpiolib_direction_input
,
1279 .get
= bfin_gpiolib_get_value
,
1280 .direction_output
= bfin_gpiolib_direction_output
,
1281 .set
= bfin_gpiolib_set_value
,
1282 .request
= bfin_gpiolib_gpio_request
,
1283 .free
= bfin_gpiolib_gpio_free
,
1284 .to_irq
= bfin_gpiolib_gpio_to_irq
,
1286 .ngpio
= MAX_BLACKFIN_GPIOS
,
1289 static int __init
bfin_gpiolib_setup(void)
1291 return gpiochip_add(&bfin_chip
);
1293 arch_initcall(bfin_gpiolib_setup
);