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/driver.h>
15 /* FIXME: consumer API required for gpio_set_value() etc, get rid of this */
16 #include <linux/gpio.h>
17 #include <linux/irq.h>
19 #if ANOMALY_05000311 || ANOMALY_05000323
22 AWA_data_clear
= SYSCR
,
25 AWA_maska
= BFIN_UART_SCR
,
26 AWA_maska_clear
= BFIN_UART_SCR
,
27 AWA_maska_set
= BFIN_UART_SCR
,
28 AWA_maska_toggle
= BFIN_UART_SCR
,
29 AWA_maskb
= BFIN_UART_GCTL
,
30 AWA_maskb_clear
= BFIN_UART_GCTL
,
31 AWA_maskb_set
= BFIN_UART_GCTL
,
32 AWA_maskb_toggle
= BFIN_UART_GCTL
,
33 AWA_dir
= SPORT1_STAT
,
34 AWA_polar
= SPORT1_STAT
,
35 AWA_edge
= SPORT1_STAT
,
36 AWA_both
= SPORT1_STAT
,
38 AWA_inen
= TIMER_ENABLE
,
39 #elif ANOMALY_05000323
40 AWA_inen
= DMA1_1_CONFIG
,
43 /* Anomaly Workaround */
44 #define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
46 #define AWA_DUMMY_READ(...) do { } while (0)
49 static struct gpio_port_t
* const gpio_array
[] = {
50 #if defined(BF533_FAMILY) || defined(BF538_FAMILY)
51 (struct gpio_port_t
*) FIO_FLAG_D
,
52 #elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
53 (struct gpio_port_t
*) PORTFIO
,
54 (struct gpio_port_t
*) PORTGIO
,
55 (struct gpio_port_t
*) PORTHIO
,
56 #elif defined(BF561_FAMILY)
57 (struct gpio_port_t
*) FIO0_FLAG_D
,
58 (struct gpio_port_t
*) FIO1_FLAG_D
,
59 (struct gpio_port_t
*) FIO2_FLAG_D
,
61 # error no gpio arrays defined
65 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
66 static unsigned short * const port_fer
[] = {
67 (unsigned short *) PORTF_FER
,
68 (unsigned short *) PORTG_FER
,
69 (unsigned short *) PORTH_FER
,
72 # if !defined(BF537_FAMILY)
73 static unsigned short * const port_mux
[] = {
74 (unsigned short *) PORTF_MUX
,
75 (unsigned short *) PORTG_MUX
,
76 (unsigned short *) PORTH_MUX
,
80 u8 pmux_offset
[][16] = {
81 # if defined(CONFIG_BF52x)
82 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
83 { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
84 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
85 # elif defined(CONFIG_BF51x)
86 { 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
87 { 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
88 { 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
93 #elif defined(BF538_FAMILY)
94 static unsigned short * const port_fer
[] = {
95 (unsigned short *) PORTCIO_FER
,
96 (unsigned short *) PORTDIO_FER
,
97 (unsigned short *) PORTEIO_FER
,
101 #define RESOURCE_LABEL_SIZE 16
103 static struct str_ident
{
104 char name
[RESOURCE_LABEL_SIZE
];
105 } str_ident
[MAX_RESOURCES
];
107 #if defined(CONFIG_PM)
108 static struct gpio_port_s gpio_bank_saved
[GPIO_BANK_NUM
];
110 static unsigned short port_fer_saved
[3];
114 static void gpio_error(unsigned gpio
)
116 printk(KERN_ERR
"bfin-gpio: GPIO %d wasn't requested!\n", gpio
);
119 static void set_label(unsigned short ident
, const char *label
)
122 strncpy(str_ident
[ident
].name
, label
,
123 RESOURCE_LABEL_SIZE
);
124 str_ident
[ident
].name
[RESOURCE_LABEL_SIZE
- 1] = 0;
128 static char *get_label(unsigned short ident
)
130 return (*str_ident
[ident
].name
? str_ident
[ident
].name
: "UNKNOWN");
133 static int cmp_label(unsigned short ident
, const char *label
)
137 printk(KERN_ERR
"Please provide none-null label\n");
141 return strcmp(str_ident
[ident
].name
, label
);
146 #define map_entry(m, i) reserved_##m##_map[gpio_bank(i)]
147 #define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
148 #define reserve(m, i) (map_entry(m, i) |= gpio_bit(i))
149 #define unreserve(m, i) (map_entry(m, i) &= ~gpio_bit(i))
150 #define DECLARE_RESERVED_MAP(m, c) static unsigned short reserved_##m##_map[c]
152 DECLARE_RESERVED_MAP(gpio
, GPIO_BANK_NUM
);
153 DECLARE_RESERVED_MAP(peri
, DIV_ROUND_UP(MAX_RESOURCES
, GPIO_BANKSIZE
));
154 DECLARE_RESERVED_MAP(gpio_irq
, GPIO_BANK_NUM
);
156 inline int check_gpio(unsigned gpio
)
158 if (gpio
>= MAX_BLACKFIN_GPIOS
)
163 static void port_setup(unsigned gpio
, unsigned short usage
)
165 #if defined(BF538_FAMILY)
167 * BF538/9 Port C,D and E are special.
168 * Inverted PORT_FER polarity on CDE and no PORF_FER on F
169 * Regular PORT F GPIOs are handled here, CDE are exclusively
173 if (gpio
< MAX_BLACKFIN_GPIOS
|| gpio
>= MAX_RESOURCES
)
176 gpio
-= MAX_BLACKFIN_GPIOS
;
178 if (usage
== GPIO_USAGE
)
179 *port_fer
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
181 *port_fer
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
186 if (check_gpio(gpio
))
189 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
190 if (usage
== GPIO_USAGE
)
191 *port_fer
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
193 *port_fer
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
199 static const s8 port_mux
[] = {
208 [GPIO_PF8
... GPIO_PF15
] = -1,
209 [GPIO_PG0
... GPIO_PG7
] = -1,
218 [GPIO_PH0
... GPIO_PH15
] = -1,
219 [PORT_PJ0
... PORT_PJ3
] = -1,
222 [PORT_PJ6
... PORT_PJ9
] = -1,
227 static int portmux_group_check(unsigned short per
)
229 u16 ident
= P_IDENT(per
);
230 u16 function
= P_FUNCT2MUX(per
);
231 s8 offset
= port_mux
[ident
];
232 u16 m
, pmux
, pfunc
, mask
;
237 pmux
= bfin_read_PORT_MUX();
238 for (m
= 0; m
< ARRAY_SIZE(port_mux
); ++m
) {
241 if (port_mux
[m
] != offset
)
243 if (!is_reserved(peri
, m
, 1))
251 pfunc
= (pmux
>> offset
) & mask
;
252 if (pfunc
!= (function
& mask
)) {
253 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
254 ident
, function
, m
, pfunc
);
262 static void portmux_setup(unsigned short per
)
264 u16 ident
= P_IDENT(per
);
265 u16 function
= P_FUNCT2MUX(per
);
266 s8 offset
= port_mux
[ident
];
272 pmux
= bfin_read_PORT_MUX();
278 pmux
&= ~(mask
<< offset
);
279 pmux
|= ((function
& mask
) << offset
);
281 bfin_write_PORT_MUX(pmux
);
283 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
284 static int portmux_group_check(unsigned short per
)
286 u16 ident
= P_IDENT(per
);
287 u16 function
= P_FUNCT2MUX(per
);
288 u8 offset
= pmux_offset
[gpio_bank(ident
)][gpio_sub_n(ident
)];
289 u16 pin
, gpiopin
, pfunc
;
291 for (pin
= 0; pin
< GPIO_BANKSIZE
; ++pin
) {
292 if (offset
!= pmux_offset
[gpio_bank(ident
)][pin
])
295 gpiopin
= gpio_bank(ident
) * GPIO_BANKSIZE
+ pin
;
296 if (gpiopin
== ident
)
298 if (!is_reserved(peri
, gpiopin
, 1))
301 pfunc
= *port_mux
[gpio_bank(ident
)];
302 pfunc
= (pfunc
>> offset
) & 3;
303 if (pfunc
!= function
) {
304 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
305 ident
, function
, gpiopin
, pfunc
);
313 inline void portmux_setup(unsigned short per
)
315 u16 ident
= P_IDENT(per
);
316 u16 function
= P_FUNCT2MUX(per
);
317 u8 offset
= pmux_offset
[gpio_bank(ident
)][gpio_sub_n(ident
)];
320 pmux
= *port_mux
[gpio_bank(ident
)];
321 if (((pmux
>> offset
) & 3) == function
)
323 pmux
&= ~(3 << offset
);
324 pmux
|= (function
& 3) << offset
;
325 *port_mux
[gpio_bank(ident
)] = pmux
;
329 # define portmux_setup(...) do { } while (0)
330 static int portmux_group_check(unsigned short per
)
336 /***********************************************************
338 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
341 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
344 * DESCRIPTION: These functions abstract direct register access
345 * to Blackfin processor General Purpose
348 * CAUTION: These functions do not belong to the GPIO Driver API
349 *************************************************************
350 * MODIFICATION HISTORY :
351 **************************************************************/
353 /* Set a specific bit */
355 #define SET_GPIO(name) \
356 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
358 unsigned long flags; \
359 flags = hard_local_irq_save(); \
361 gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
363 gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
364 AWA_DUMMY_READ(name); \
365 hard_local_irq_restore(flags); \
367 EXPORT_SYMBOL(set_gpio_ ## name);
369 SET_GPIO(dir
) /* set_gpio_dir() */
370 SET_GPIO(inen
) /* set_gpio_inen() */
371 SET_GPIO(polar
) /* set_gpio_polar() */
372 SET_GPIO(edge
) /* set_gpio_edge() */
373 SET_GPIO(both
) /* set_gpio_both() */
376 #define SET_GPIO_SC(name) \
377 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
379 unsigned long flags; \
380 if (ANOMALY_05000311 || ANOMALY_05000323) \
381 flags = hard_local_irq_save(); \
383 gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
385 gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
386 if (ANOMALY_05000311 || ANOMALY_05000323) { \
387 AWA_DUMMY_READ(name); \
388 hard_local_irq_restore(flags); \
391 EXPORT_SYMBOL(set_gpio_ ## name);
397 void set_gpio_toggle(unsigned gpio
)
400 if (ANOMALY_05000311
|| ANOMALY_05000323
)
401 flags
= hard_local_irq_save();
402 gpio_array
[gpio_bank(gpio
)]->toggle
= gpio_bit(gpio
);
403 if (ANOMALY_05000311
|| ANOMALY_05000323
) {
404 AWA_DUMMY_READ(toggle
);
405 hard_local_irq_restore(flags
);
408 EXPORT_SYMBOL(set_gpio_toggle
);
411 /*Set current PORT date (16-bit word)*/
413 #define SET_GPIO_P(name) \
414 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
416 unsigned long flags; \
417 if (ANOMALY_05000311 || ANOMALY_05000323) \
418 flags = hard_local_irq_save(); \
419 gpio_array[gpio_bank(gpio)]->name = arg; \
420 if (ANOMALY_05000311 || ANOMALY_05000323) { \
421 AWA_DUMMY_READ(name); \
422 hard_local_irq_restore(flags); \
425 EXPORT_SYMBOL(set_gpiop_ ## name);
436 /* Get a specific bit */
437 #define GET_GPIO(name) \
438 unsigned short get_gpio_ ## name(unsigned gpio) \
440 unsigned long flags; \
441 unsigned short ret; \
442 if (ANOMALY_05000311 || ANOMALY_05000323) \
443 flags = hard_local_irq_save(); \
444 ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
445 if (ANOMALY_05000311 || ANOMALY_05000323) { \
446 AWA_DUMMY_READ(name); \
447 hard_local_irq_restore(flags); \
451 EXPORT_SYMBOL(get_gpio_ ## name);
462 /*Get current PORT date (16-bit word)*/
464 #define GET_GPIO_P(name) \
465 unsigned short get_gpiop_ ## name(unsigned gpio) \
467 unsigned long flags; \
468 unsigned short ret; \
469 if (ANOMALY_05000311 || ANOMALY_05000323) \
470 flags = hard_local_irq_save(); \
471 ret = (gpio_array[gpio_bank(gpio)]->name); \
472 if (ANOMALY_05000311 || ANOMALY_05000323) { \
473 AWA_DUMMY_READ(name); \
474 hard_local_irq_restore(flags); \
478 EXPORT_SYMBOL(get_gpiop_ ## name);
491 DECLARE_RESERVED_MAP(wakeup
, GPIO_BANK_NUM
);
493 static const unsigned int sic_iwr_irqs
[] = {
494 #if defined(BF533_FAMILY)
496 #elif defined(BF537_FAMILY)
497 IRQ_PF_INTB_WATCH
, IRQ_PORTG_INTB
, IRQ_PH_INTB_MAC_TX
498 #elif defined(BF538_FAMILY)
500 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
501 IRQ_PORTF_INTB
, IRQ_PORTG_INTB
, IRQ_PORTH_INTB
502 #elif defined(BF561_FAMILY)
503 IRQ_PROG0_INTB
, IRQ_PROG1_INTB
, IRQ_PROG2_INTB
505 # error no SIC_IWR defined
509 /***********************************************************
511 * FUNCTIONS: Blackfin PM Setup API
514 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
522 * DESCRIPTION: Blackfin PM Driver API
525 *************************************************************
526 * MODIFICATION HISTORY :
527 **************************************************************/
528 int bfin_gpio_pm_wakeup_ctrl(unsigned gpio
, unsigned ctrl
)
532 if (check_gpio(gpio
) < 0)
535 flags
= hard_local_irq_save();
537 reserve(wakeup
, gpio
);
539 unreserve(wakeup
, gpio
);
541 set_gpio_maskb(gpio
, ctrl
);
542 hard_local_irq_restore(flags
);
547 int bfin_gpio_pm_standby_ctrl(unsigned ctrl
)
551 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
552 mask
= map_entry(wakeup
, i
);
556 bfin_internal_set_wake(sic_iwr_irqs
[bank
], ctrl
);
561 void bfin_gpio_pm_hibernate_suspend(void)
566 for (i
= 0; i
< ARRAY_SIZE(port_fer_saved
); ++i
)
567 port_fer_saved
[i
] = *port_fer
[i
];
570 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
573 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
574 gpio_bank_saved
[bank
].fer
= *port_fer
[bank
];
575 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
576 gpio_bank_saved
[bank
].mux
= *port_mux
[bank
];
579 gpio_bank_saved
[bank
].mux
= bfin_read_PORT_MUX();
582 gpio_bank_saved
[bank
].data
= gpio_array
[bank
]->data
;
583 gpio_bank_saved
[bank
].inen
= gpio_array
[bank
]->inen
;
584 gpio_bank_saved
[bank
].polar
= gpio_array
[bank
]->polar
;
585 gpio_bank_saved
[bank
].dir
= gpio_array
[bank
]->dir
;
586 gpio_bank_saved
[bank
].edge
= gpio_array
[bank
]->edge
;
587 gpio_bank_saved
[bank
].both
= gpio_array
[bank
]->both
;
588 gpio_bank_saved
[bank
].maska
= gpio_array
[bank
]->maska
;
591 #ifdef BFIN_SPECIAL_GPIO_BANKS
592 bfin_special_gpio_pm_hibernate_suspend();
595 AWA_DUMMY_READ(maska
);
598 void bfin_gpio_pm_hibernate_restore(void)
603 for (i
= 0; i
< ARRAY_SIZE(port_fer_saved
); ++i
)
604 *port_fer
[i
] = port_fer_saved
[i
];
607 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
610 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
611 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
612 *port_mux
[bank
] = gpio_bank_saved
[bank
].mux
;
615 bfin_write_PORT_MUX(gpio_bank_saved
[bank
].mux
);
617 *port_fer
[bank
] = gpio_bank_saved
[bank
].fer
;
619 gpio_array
[bank
]->inen
= gpio_bank_saved
[bank
].inen
;
620 gpio_array
[bank
]->data_set
= gpio_bank_saved
[bank
].data
621 & gpio_bank_saved
[bank
].dir
;
622 gpio_array
[bank
]->dir
= gpio_bank_saved
[bank
].dir
;
623 gpio_array
[bank
]->polar
= gpio_bank_saved
[bank
].polar
;
624 gpio_array
[bank
]->edge
= gpio_bank_saved
[bank
].edge
;
625 gpio_array
[bank
]->both
= gpio_bank_saved
[bank
].both
;
626 gpio_array
[bank
]->maska
= gpio_bank_saved
[bank
].maska
;
629 #ifdef BFIN_SPECIAL_GPIO_BANKS
630 bfin_special_gpio_pm_hibernate_restore();
633 AWA_DUMMY_READ(maska
);
639 /***********************************************************
641 * FUNCTIONS: Blackfin Peripheral Resource Allocation
645 * per Peripheral Identifier
648 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
651 *************************************************************
652 * MODIFICATION HISTORY :
653 **************************************************************/
655 int peripheral_request(unsigned short per
, const char *label
)
658 unsigned short ident
= P_IDENT(per
);
661 * Don't cares are pins with only one dedicated function
664 if (per
& P_DONTCARE
)
667 if (!(per
& P_DEFINED
))
670 BUG_ON(ident
>= MAX_RESOURCES
);
672 flags
= hard_local_irq_save();
674 /* If a pin can be muxed as either GPIO or peripheral, make
675 * sure it is not already a GPIO pin when we request it.
677 if (unlikely(!check_gpio(ident
) && is_reserved(gpio
, ident
, 1))) {
678 if (system_state
== SYSTEM_BOOTING
)
681 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
682 __func__
, ident
, get_label(ident
));
683 hard_local_irq_restore(flags
);
687 if (unlikely(is_reserved(peri
, ident
, 1))) {
690 * Pin functions like AMC address strobes my
691 * be requested and used by several drivers
694 if (!(per
& P_MAYSHARE
)) {
696 * Allow that the identical pin function can
697 * be requested from the same driver twice
700 if (cmp_label(ident
, label
) == 0)
703 if (system_state
== SYSTEM_BOOTING
)
706 "%s: Peripheral %d function %d is already reserved by %s !\n",
707 __func__
, ident
, P_FUNCT2MUX(per
), get_label(ident
));
708 hard_local_irq_restore(flags
);
713 if (unlikely(portmux_group_check(per
))) {
714 hard_local_irq_restore(flags
);
718 reserve(peri
, ident
);
721 port_setup(ident
, PERIPHERAL_USAGE
);
723 hard_local_irq_restore(flags
);
724 set_label(ident
, label
);
728 EXPORT_SYMBOL(peripheral_request
);
730 int peripheral_request_list(const unsigned short per
[], const char *label
)
735 for (cnt
= 0; per
[cnt
] != 0; cnt
++) {
737 ret
= peripheral_request(per
[cnt
], label
);
740 for ( ; cnt
> 0; cnt
--)
741 peripheral_free(per
[cnt
- 1]);
749 EXPORT_SYMBOL(peripheral_request_list
);
751 void peripheral_free(unsigned short per
)
754 unsigned short ident
= P_IDENT(per
);
756 if (per
& P_DONTCARE
)
759 if (!(per
& P_DEFINED
))
762 flags
= hard_local_irq_save();
764 if (unlikely(!is_reserved(peri
, ident
, 0))) {
765 hard_local_irq_restore(flags
);
769 if (!(per
& P_MAYSHARE
))
770 port_setup(ident
, GPIO_USAGE
);
772 unreserve(peri
, ident
);
774 set_label(ident
, "free");
776 hard_local_irq_restore(flags
);
778 EXPORT_SYMBOL(peripheral_free
);
780 void peripheral_free_list(const unsigned short per
[])
783 for (cnt
= 0; per
[cnt
] != 0; cnt
++)
784 peripheral_free(per
[cnt
]);
786 EXPORT_SYMBOL(peripheral_free_list
);
788 /***********************************************************
790 * FUNCTIONS: Blackfin GPIO Driver
793 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
796 * DESCRIPTION: Blackfin GPIO Driver API
799 *************************************************************
800 * MODIFICATION HISTORY :
801 **************************************************************/
803 int bfin_gpio_request(unsigned gpio
, const char *label
)
807 if (check_gpio(gpio
) < 0)
810 flags
= hard_local_irq_save();
813 * Allow that the identical GPIO can
814 * be requested from the same driver twice
815 * Do nothing and return -
818 if (cmp_label(gpio
, label
) == 0) {
819 hard_local_irq_restore(flags
);
823 if (unlikely(is_reserved(gpio
, gpio
, 1))) {
824 if (system_state
== SYSTEM_BOOTING
)
826 printk(KERN_ERR
"bfin-gpio: GPIO %d is already reserved by %s !\n",
827 gpio
, get_label(gpio
));
828 hard_local_irq_restore(flags
);
831 if (unlikely(is_reserved(peri
, gpio
, 1))) {
832 if (system_state
== SYSTEM_BOOTING
)
835 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
836 gpio
, get_label(gpio
));
837 hard_local_irq_restore(flags
);
840 if (unlikely(is_reserved(gpio_irq
, gpio
, 1))) {
841 printk(KERN_NOTICE
"bfin-gpio: GPIO %d is already reserved as gpio-irq!"
842 " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio
);
843 } else { /* Reset POLAR setting when acquiring a gpio for the first time */
844 set_gpio_polar(gpio
, 0);
848 set_label(gpio
, label
);
850 hard_local_irq_restore(flags
);
852 port_setup(gpio
, GPIO_USAGE
);
856 EXPORT_SYMBOL(bfin_gpio_request
);
858 void bfin_gpio_free(unsigned gpio
)
862 if (check_gpio(gpio
) < 0)
867 flags
= hard_local_irq_save();
869 if (unlikely(!is_reserved(gpio
, gpio
, 0))) {
870 if (system_state
== SYSTEM_BOOTING
)
873 hard_local_irq_restore(flags
);
877 unreserve(gpio
, gpio
);
879 set_label(gpio
, "free");
881 hard_local_irq_restore(flags
);
883 EXPORT_SYMBOL(bfin_gpio_free
);
885 #ifdef BFIN_SPECIAL_GPIO_BANKS
886 DECLARE_RESERVED_MAP(special_gpio
, gpio_bank(MAX_RESOURCES
));
888 int bfin_special_gpio_request(unsigned gpio
, const char *label
)
892 flags
= hard_local_irq_save();
895 * Allow that the identical GPIO can
896 * be requested from the same driver twice
897 * Do nothing and return -
900 if (cmp_label(gpio
, label
) == 0) {
901 hard_local_irq_restore(flags
);
905 if (unlikely(is_reserved(special_gpio
, gpio
, 1))) {
906 hard_local_irq_restore(flags
);
907 printk(KERN_ERR
"bfin-gpio: GPIO %d is already reserved by %s !\n",
908 gpio
, get_label(gpio
));
912 if (unlikely(is_reserved(peri
, gpio
, 1))) {
913 hard_local_irq_restore(flags
);
915 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
916 gpio
, get_label(gpio
));
921 reserve(special_gpio
, gpio
);
924 set_label(gpio
, label
);
925 hard_local_irq_restore(flags
);
926 port_setup(gpio
, GPIO_USAGE
);
930 EXPORT_SYMBOL(bfin_special_gpio_request
);
932 void bfin_special_gpio_free(unsigned gpio
)
938 flags
= hard_local_irq_save();
940 if (unlikely(!is_reserved(special_gpio
, gpio
, 0))) {
942 hard_local_irq_restore(flags
);
946 unreserve(special_gpio
, gpio
);
947 unreserve(peri
, gpio
);
948 set_label(gpio
, "free");
949 hard_local_irq_restore(flags
);
951 EXPORT_SYMBOL(bfin_special_gpio_free
);
955 int bfin_gpio_irq_request(unsigned gpio
, const char *label
)
959 if (check_gpio(gpio
) < 0)
962 flags
= hard_local_irq_save();
964 if (unlikely(is_reserved(peri
, gpio
, 1))) {
965 if (system_state
== SYSTEM_BOOTING
)
968 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
969 gpio
, get_label(gpio
));
970 hard_local_irq_restore(flags
);
973 if (unlikely(is_reserved(gpio
, gpio
, 1)))
974 printk(KERN_NOTICE
"bfin-gpio: GPIO %d is already reserved by %s! "
975 "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
976 gpio
, get_label(gpio
));
978 reserve(gpio_irq
, gpio
);
979 set_label(gpio
, label
);
981 hard_local_irq_restore(flags
);
983 port_setup(gpio
, GPIO_USAGE
);
988 void bfin_gpio_irq_free(unsigned gpio
)
992 if (check_gpio(gpio
) < 0)
995 flags
= hard_local_irq_save();
997 if (unlikely(!is_reserved(gpio_irq
, gpio
, 0))) {
998 if (system_state
== SYSTEM_BOOTING
)
1001 hard_local_irq_restore(flags
);
1005 unreserve(gpio_irq
, gpio
);
1007 set_label(gpio
, "free");
1009 hard_local_irq_restore(flags
);
1012 static inline void __bfin_gpio_direction_input(unsigned gpio
)
1014 gpio_array
[gpio_bank(gpio
)]->dir
&= ~gpio_bit(gpio
);
1015 gpio_array
[gpio_bank(gpio
)]->inen
|= gpio_bit(gpio
);
1018 int bfin_gpio_direction_input(unsigned gpio
)
1020 unsigned long flags
;
1022 if (unlikely(!is_reserved(gpio
, gpio
, 0))) {
1027 flags
= hard_local_irq_save();
1028 __bfin_gpio_direction_input(gpio
);
1029 AWA_DUMMY_READ(inen
);
1030 hard_local_irq_restore(flags
);
1034 EXPORT_SYMBOL(bfin_gpio_direction_input
);
1036 void bfin_gpio_irq_prepare(unsigned gpio
)
1038 port_setup(gpio
, GPIO_USAGE
);
1041 void bfin_gpio_set_value(unsigned gpio
, int arg
)
1044 gpio_array
[gpio_bank(gpio
)]->data_set
= gpio_bit(gpio
);
1046 gpio_array
[gpio_bank(gpio
)]->data_clear
= gpio_bit(gpio
);
1048 EXPORT_SYMBOL(bfin_gpio_set_value
);
1050 int bfin_gpio_direction_output(unsigned gpio
, int value
)
1052 unsigned long flags
;
1054 if (unlikely(!is_reserved(gpio
, gpio
, 0))) {
1059 flags
= hard_local_irq_save();
1061 gpio_array
[gpio_bank(gpio
)]->inen
&= ~gpio_bit(gpio
);
1062 gpio_set_value(gpio
, value
);
1063 gpio_array
[gpio_bank(gpio
)]->dir
|= gpio_bit(gpio
);
1065 AWA_DUMMY_READ(dir
);
1066 hard_local_irq_restore(flags
);
1070 EXPORT_SYMBOL(bfin_gpio_direction_output
);
1072 int bfin_gpio_get_value(unsigned gpio
)
1074 unsigned long flags
;
1076 if (unlikely(get_gpio_edge(gpio
))) {
1078 flags
= hard_local_irq_save();
1079 set_gpio_edge(gpio
, 0);
1080 ret
= get_gpio_data(gpio
);
1081 set_gpio_edge(gpio
, 1);
1082 hard_local_irq_restore(flags
);
1085 return get_gpio_data(gpio
);
1087 EXPORT_SYMBOL(bfin_gpio_get_value
);
1089 /* If we are booting from SPI and our board lacks a strong enough pull up,
1090 * the core can reset and execute the bootrom faster than the resistor can
1091 * pull the signal logically high. To work around this (common) error in
1092 * board design, we explicitly set the pin back to GPIO mode, force /CS
1093 * high, and wait for the electrons to do their thing.
1095 * This function only makes sense to be called from reset code, but it
1096 * lives here as we need to force all the GPIO states w/out going through
1097 * BUG() checks and such.
1099 void bfin_reset_boot_spi_cs(unsigned short pin
)
1101 unsigned short gpio
= P_IDENT(pin
);
1102 port_setup(gpio
, GPIO_USAGE
);
1103 gpio_array
[gpio_bank(gpio
)]->data_set
= gpio_bit(gpio
);
1104 AWA_DUMMY_READ(data_set
);
1108 #if defined(CONFIG_PROC_FS)
1109 static int gpio_proc_show(struct seq_file
*m
, void *v
)
1113 for (c
= 0; c
< MAX_RESOURCES
; c
++) {
1114 irq
= is_reserved(gpio_irq
, c
, 1);
1115 gpio
= is_reserved(gpio
, c
, 1);
1116 if (!check_gpio(c
) && (gpio
|| irq
))
1117 seq_printf(m
, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c
,
1118 get_label(c
), (gpio
&& irq
) ? " *" : "",
1119 get_gpio_dir(c
) ? "OUTPUT" : "INPUT");
1120 else if (is_reserved(peri
, c
, 1))
1121 seq_printf(m
, "GPIO_%d: \t%s \t\tPeripheral\n", c
, get_label(c
));
1129 static int gpio_proc_open(struct inode
*inode
, struct file
*file
)
1131 return single_open(file
, gpio_proc_show
, NULL
);
1134 static const struct file_operations gpio_proc_ops
= {
1135 .open
= gpio_proc_open
,
1137 .llseek
= seq_lseek
,
1138 .release
= single_release
,
1141 static __init
int gpio_register_proc(void)
1143 struct proc_dir_entry
*proc_gpio
;
1145 proc_gpio
= proc_create("gpio", 0, NULL
, &gpio_proc_ops
);
1146 return proc_gpio
== NULL
;
1148 __initcall(gpio_register_proc
);
1151 #ifdef CONFIG_GPIOLIB
1152 static int bfin_gpiolib_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
1154 return bfin_gpio_direction_input(gpio
);
1157 static int bfin_gpiolib_direction_output(struct gpio_chip
*chip
, unsigned gpio
, int level
)
1159 return bfin_gpio_direction_output(gpio
, level
);
1162 static int bfin_gpiolib_get_value(struct gpio_chip
*chip
, unsigned gpio
)
1164 return !!bfin_gpio_get_value(gpio
);
1167 static void bfin_gpiolib_set_value(struct gpio_chip
*chip
, unsigned gpio
, int value
)
1169 return bfin_gpio_set_value(gpio
, value
);
1172 static int bfin_gpiolib_gpio_request(struct gpio_chip
*chip
, unsigned gpio
)
1174 return bfin_gpio_request(gpio
, chip
->label
);
1177 static void bfin_gpiolib_gpio_free(struct gpio_chip
*chip
, unsigned gpio
)
1179 return bfin_gpio_free(gpio
);
1182 static int bfin_gpiolib_gpio_to_irq(struct gpio_chip
*chip
, unsigned gpio
)
1184 return gpio
+ GPIO_IRQ_BASE
;
1187 static struct gpio_chip bfin_chip
= {
1188 .label
= "BFIN-GPIO",
1189 .direction_input
= bfin_gpiolib_direction_input
,
1190 .get
= bfin_gpiolib_get_value
,
1191 .direction_output
= bfin_gpiolib_direction_output
,
1192 .set
= bfin_gpiolib_set_value
,
1193 .request
= bfin_gpiolib_gpio_request
,
1194 .free
= bfin_gpiolib_gpio_free
,
1195 .to_irq
= bfin_gpiolib_gpio_to_irq
,
1197 .ngpio
= MAX_BLACKFIN_GPIOS
,
1200 static int __init
bfin_gpiolib_setup(void)
1202 return gpiochip_add_data(&bfin_chip
, NULL
);
1204 arch_initcall(bfin_gpiolib_setup
);