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 #include <asm/irq_handler.h>
20 #include <asm/portmux.h>
22 #if ANOMALY_05000311 || ANOMALY_05000323
25 AWA_data_clear
= SYSCR
,
28 AWA_maska
= BFIN_UART_SCR
,
29 AWA_maska_clear
= BFIN_UART_SCR
,
30 AWA_maska_set
= BFIN_UART_SCR
,
31 AWA_maska_toggle
= BFIN_UART_SCR
,
32 AWA_maskb
= BFIN_UART_GCTL
,
33 AWA_maskb_clear
= BFIN_UART_GCTL
,
34 AWA_maskb_set
= BFIN_UART_GCTL
,
35 AWA_maskb_toggle
= BFIN_UART_GCTL
,
36 AWA_dir
= SPORT1_STAT
,
37 AWA_polar
= SPORT1_STAT
,
38 AWA_edge
= SPORT1_STAT
,
39 AWA_both
= SPORT1_STAT
,
41 AWA_inen
= TIMER_ENABLE
,
42 #elif ANOMALY_05000323
43 AWA_inen
= DMA1_1_CONFIG
,
46 /* Anomaly Workaround */
47 #define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
49 #define AWA_DUMMY_READ(...) do { } while (0)
52 static struct gpio_port_t
* const gpio_array
[] = {
53 #if defined(BF533_FAMILY) || defined(BF538_FAMILY)
54 (struct gpio_port_t
*) FIO_FLAG_D
,
55 #elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
56 (struct gpio_port_t
*) PORTFIO
,
57 (struct gpio_port_t
*) PORTGIO
,
58 (struct gpio_port_t
*) PORTHIO
,
59 #elif defined(BF561_FAMILY)
60 (struct gpio_port_t
*) FIO0_FLAG_D
,
61 (struct gpio_port_t
*) FIO1_FLAG_D
,
62 (struct gpio_port_t
*) FIO2_FLAG_D
,
64 # error no gpio arrays defined
68 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
69 static unsigned short * const port_fer
[] = {
70 (unsigned short *) PORTF_FER
,
71 (unsigned short *) PORTG_FER
,
72 (unsigned short *) PORTH_FER
,
75 # if !defined(BF537_FAMILY)
76 static unsigned short * const port_mux
[] = {
77 (unsigned short *) PORTF_MUX
,
78 (unsigned short *) PORTG_MUX
,
79 (unsigned short *) PORTH_MUX
,
83 u8 pmux_offset
[][16] = {
84 # if defined(CONFIG_BF52x)
85 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
86 { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
87 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
88 # elif defined(CONFIG_BF51x)
89 { 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
90 { 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
91 { 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
96 #elif defined(BF538_FAMILY)
97 static unsigned short * const port_fer
[] = {
98 (unsigned short *) PORTCIO_FER
,
99 (unsigned short *) PORTDIO_FER
,
100 (unsigned short *) PORTEIO_FER
,
104 #define RESOURCE_LABEL_SIZE 16
106 static struct str_ident
{
107 char name
[RESOURCE_LABEL_SIZE
];
108 } str_ident
[MAX_RESOURCES
];
110 #if defined(CONFIG_PM)
111 static struct gpio_port_s gpio_bank_saved
[GPIO_BANK_NUM
];
113 static unsigned short port_fer_saved
[3];
117 static void gpio_error(unsigned gpio
)
119 printk(KERN_ERR
"bfin-gpio: GPIO %d wasn't requested!\n", gpio
);
122 static void set_label(unsigned short ident
, const char *label
)
125 strncpy(str_ident
[ident
].name
, label
,
126 RESOURCE_LABEL_SIZE
);
127 str_ident
[ident
].name
[RESOURCE_LABEL_SIZE
- 1] = 0;
131 static char *get_label(unsigned short ident
)
133 return (*str_ident
[ident
].name
? str_ident
[ident
].name
: "UNKNOWN");
136 static int cmp_label(unsigned short ident
, const char *label
)
140 printk(KERN_ERR
"Please provide none-null label\n");
144 return strcmp(str_ident
[ident
].name
, label
);
149 #define map_entry(m, i) reserved_##m##_map[gpio_bank(i)]
150 #define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
151 #define reserve(m, i) (map_entry(m, i) |= gpio_bit(i))
152 #define unreserve(m, i) (map_entry(m, i) &= ~gpio_bit(i))
153 #define DECLARE_RESERVED_MAP(m, c) static unsigned short reserved_##m##_map[c]
155 DECLARE_RESERVED_MAP(gpio
, GPIO_BANK_NUM
);
156 DECLARE_RESERVED_MAP(peri
, DIV_ROUND_UP(MAX_RESOURCES
, GPIO_BANKSIZE
));
157 DECLARE_RESERVED_MAP(gpio_irq
, GPIO_BANK_NUM
);
159 inline int check_gpio(unsigned gpio
)
161 if (gpio
>= MAX_BLACKFIN_GPIOS
)
166 static void port_setup(unsigned gpio
, unsigned short usage
)
168 #if defined(BF538_FAMILY)
170 * BF538/9 Port C,D and E are special.
171 * Inverted PORT_FER polarity on CDE and no PORF_FER on F
172 * Regular PORT F GPIOs are handled here, CDE are exclusively
176 if (gpio
< MAX_BLACKFIN_GPIOS
|| gpio
>= MAX_RESOURCES
)
179 gpio
-= MAX_BLACKFIN_GPIOS
;
181 if (usage
== GPIO_USAGE
)
182 *port_fer
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
184 *port_fer
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
189 if (check_gpio(gpio
))
192 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
193 if (usage
== GPIO_USAGE
)
194 *port_fer
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
196 *port_fer
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
202 static const s8 port_mux
[] = {
211 [GPIO_PF8
... GPIO_PF15
] = -1,
212 [GPIO_PG0
... GPIO_PG7
] = -1,
221 [GPIO_PH0
... GPIO_PH15
] = -1,
222 [PORT_PJ0
... PORT_PJ3
] = -1,
225 [PORT_PJ6
... PORT_PJ9
] = -1,
230 static int portmux_group_check(unsigned short per
)
232 u16 ident
= P_IDENT(per
);
233 u16 function
= P_FUNCT2MUX(per
);
234 s8 offset
= port_mux
[ident
];
235 u16 m
, pmux
, pfunc
, mask
;
240 pmux
= bfin_read_PORT_MUX();
241 for (m
= 0; m
< ARRAY_SIZE(port_mux
); ++m
) {
244 if (port_mux
[m
] != offset
)
246 if (!is_reserved(peri
, m
, 1))
254 pfunc
= (pmux
>> offset
) & mask
;
255 if (pfunc
!= (function
& mask
)) {
256 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
257 ident
, function
, m
, pfunc
);
265 static void portmux_setup(unsigned short per
)
267 u16 ident
= P_IDENT(per
);
268 u16 function
= P_FUNCT2MUX(per
);
269 s8 offset
= port_mux
[ident
];
275 pmux
= bfin_read_PORT_MUX();
281 pmux
&= ~(mask
<< offset
);
282 pmux
|= ((function
& mask
) << offset
);
284 bfin_write_PORT_MUX(pmux
);
286 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
287 static int portmux_group_check(unsigned short per
)
289 u16 ident
= P_IDENT(per
);
290 u16 function
= P_FUNCT2MUX(per
);
291 u8 offset
= pmux_offset
[gpio_bank(ident
)][gpio_sub_n(ident
)];
292 u16 pin
, gpiopin
, pfunc
;
294 for (pin
= 0; pin
< GPIO_BANKSIZE
; ++pin
) {
295 if (offset
!= pmux_offset
[gpio_bank(ident
)][pin
])
298 gpiopin
= gpio_bank(ident
) * GPIO_BANKSIZE
+ pin
;
299 if (gpiopin
== ident
)
301 if (!is_reserved(peri
, gpiopin
, 1))
304 pfunc
= *port_mux
[gpio_bank(ident
)];
305 pfunc
= (pfunc
>> offset
) & 3;
306 if (pfunc
!= function
) {
307 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
308 ident
, function
, gpiopin
, pfunc
);
316 inline void portmux_setup(unsigned short per
)
318 u16 ident
= P_IDENT(per
);
319 u16 function
= P_FUNCT2MUX(per
);
320 u8 offset
= pmux_offset
[gpio_bank(ident
)][gpio_sub_n(ident
)];
323 pmux
= *port_mux
[gpio_bank(ident
)];
324 if (((pmux
>> offset
) & 3) == function
)
326 pmux
&= ~(3 << offset
);
327 pmux
|= (function
& 3) << offset
;
328 *port_mux
[gpio_bank(ident
)] = pmux
;
332 # define portmux_setup(...) do { } while (0)
333 static int portmux_group_check(unsigned short per
)
339 /***********************************************************
341 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
344 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
347 * DESCRIPTION: These functions abstract direct register access
348 * to Blackfin processor General Purpose
351 * CAUTION: These functions do not belong to the GPIO Driver API
352 *************************************************************
353 * MODIFICATION HISTORY :
354 **************************************************************/
356 /* Set a specific bit */
358 #define SET_GPIO(name) \
359 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
361 unsigned long flags; \
362 flags = hard_local_irq_save(); \
364 gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
366 gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
367 AWA_DUMMY_READ(name); \
368 hard_local_irq_restore(flags); \
370 EXPORT_SYMBOL(set_gpio_ ## name);
372 SET_GPIO(dir
) /* set_gpio_dir() */
373 SET_GPIO(inen
) /* set_gpio_inen() */
374 SET_GPIO(polar
) /* set_gpio_polar() */
375 SET_GPIO(edge
) /* set_gpio_edge() */
376 SET_GPIO(both
) /* set_gpio_both() */
379 #define SET_GPIO_SC(name) \
380 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
382 unsigned long flags; \
383 if (ANOMALY_05000311 || ANOMALY_05000323) \
384 flags = hard_local_irq_save(); \
386 gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
388 gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
389 if (ANOMALY_05000311 || ANOMALY_05000323) { \
390 AWA_DUMMY_READ(name); \
391 hard_local_irq_restore(flags); \
394 EXPORT_SYMBOL(set_gpio_ ## name);
400 void set_gpio_toggle(unsigned gpio
)
403 if (ANOMALY_05000311
|| ANOMALY_05000323
)
404 flags
= hard_local_irq_save();
405 gpio_array
[gpio_bank(gpio
)]->toggle
= gpio_bit(gpio
);
406 if (ANOMALY_05000311
|| ANOMALY_05000323
) {
407 AWA_DUMMY_READ(toggle
);
408 hard_local_irq_restore(flags
);
411 EXPORT_SYMBOL(set_gpio_toggle
);
414 /*Set current PORT date (16-bit word)*/
416 #define SET_GPIO_P(name) \
417 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
419 unsigned long flags; \
420 if (ANOMALY_05000311 || ANOMALY_05000323) \
421 flags = hard_local_irq_save(); \
422 gpio_array[gpio_bank(gpio)]->name = arg; \
423 if (ANOMALY_05000311 || ANOMALY_05000323) { \
424 AWA_DUMMY_READ(name); \
425 hard_local_irq_restore(flags); \
428 EXPORT_SYMBOL(set_gpiop_ ## name);
439 /* Get a specific bit */
440 #define GET_GPIO(name) \
441 unsigned short get_gpio_ ## name(unsigned gpio) \
443 unsigned long flags; \
444 unsigned short ret; \
445 if (ANOMALY_05000311 || ANOMALY_05000323) \
446 flags = hard_local_irq_save(); \
447 ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
448 if (ANOMALY_05000311 || ANOMALY_05000323) { \
449 AWA_DUMMY_READ(name); \
450 hard_local_irq_restore(flags); \
454 EXPORT_SYMBOL(get_gpio_ ## name);
465 /*Get current PORT date (16-bit word)*/
467 #define GET_GPIO_P(name) \
468 unsigned short get_gpiop_ ## name(unsigned gpio) \
470 unsigned long flags; \
471 unsigned short ret; \
472 if (ANOMALY_05000311 || ANOMALY_05000323) \
473 flags = hard_local_irq_save(); \
474 ret = (gpio_array[gpio_bank(gpio)]->name); \
475 if (ANOMALY_05000311 || ANOMALY_05000323) { \
476 AWA_DUMMY_READ(name); \
477 hard_local_irq_restore(flags); \
481 EXPORT_SYMBOL(get_gpiop_ ## name);
494 DECLARE_RESERVED_MAP(wakeup
, GPIO_BANK_NUM
);
496 static const unsigned int sic_iwr_irqs
[] = {
497 #if defined(BF533_FAMILY)
499 #elif defined(BF537_FAMILY)
500 IRQ_PF_INTB_WATCH
, IRQ_PORTG_INTB
, IRQ_PH_INTB_MAC_TX
501 #elif defined(BF538_FAMILY)
503 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
504 IRQ_PORTF_INTB
, IRQ_PORTG_INTB
, IRQ_PORTH_INTB
505 #elif defined(BF561_FAMILY)
506 IRQ_PROG0_INTB
, IRQ_PROG1_INTB
, IRQ_PROG2_INTB
508 # error no SIC_IWR defined
512 /***********************************************************
514 * FUNCTIONS: Blackfin PM Setup API
517 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
525 * DESCRIPTION: Blackfin PM Driver API
528 *************************************************************
529 * MODIFICATION HISTORY :
530 **************************************************************/
531 int bfin_gpio_pm_wakeup_ctrl(unsigned gpio
, unsigned ctrl
)
535 if (check_gpio(gpio
) < 0)
538 flags
= hard_local_irq_save();
540 reserve(wakeup
, gpio
);
542 unreserve(wakeup
, gpio
);
544 set_gpio_maskb(gpio
, ctrl
);
545 hard_local_irq_restore(flags
);
550 int bfin_gpio_pm_standby_ctrl(unsigned ctrl
)
554 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
555 mask
= map_entry(wakeup
, i
);
559 bfin_internal_set_wake(sic_iwr_irqs
[bank
], ctrl
);
564 void bfin_gpio_pm_hibernate_suspend(void)
569 for (i
= 0; i
< ARRAY_SIZE(port_fer_saved
); ++i
)
570 port_fer_saved
[i
] = *port_fer
[i
];
573 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
576 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
577 gpio_bank_saved
[bank
].fer
= *port_fer
[bank
];
578 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
579 gpio_bank_saved
[bank
].mux
= *port_mux
[bank
];
582 gpio_bank_saved
[bank
].mux
= bfin_read_PORT_MUX();
585 gpio_bank_saved
[bank
].data
= gpio_array
[bank
]->data
;
586 gpio_bank_saved
[bank
].inen
= gpio_array
[bank
]->inen
;
587 gpio_bank_saved
[bank
].polar
= gpio_array
[bank
]->polar
;
588 gpio_bank_saved
[bank
].dir
= gpio_array
[bank
]->dir
;
589 gpio_bank_saved
[bank
].edge
= gpio_array
[bank
]->edge
;
590 gpio_bank_saved
[bank
].both
= gpio_array
[bank
]->both
;
591 gpio_bank_saved
[bank
].maska
= gpio_array
[bank
]->maska
;
594 #ifdef BFIN_SPECIAL_GPIO_BANKS
595 bfin_special_gpio_pm_hibernate_suspend();
598 AWA_DUMMY_READ(maska
);
601 void bfin_gpio_pm_hibernate_restore(void)
606 for (i
= 0; i
< ARRAY_SIZE(port_fer_saved
); ++i
)
607 *port_fer
[i
] = port_fer_saved
[i
];
610 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
613 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
614 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
615 *port_mux
[bank
] = gpio_bank_saved
[bank
].mux
;
618 bfin_write_PORT_MUX(gpio_bank_saved
[bank
].mux
);
620 *port_fer
[bank
] = gpio_bank_saved
[bank
].fer
;
622 gpio_array
[bank
]->inen
= gpio_bank_saved
[bank
].inen
;
623 gpio_array
[bank
]->data_set
= gpio_bank_saved
[bank
].data
624 & gpio_bank_saved
[bank
].dir
;
625 gpio_array
[bank
]->dir
= gpio_bank_saved
[bank
].dir
;
626 gpio_array
[bank
]->polar
= gpio_bank_saved
[bank
].polar
;
627 gpio_array
[bank
]->edge
= gpio_bank_saved
[bank
].edge
;
628 gpio_array
[bank
]->both
= gpio_bank_saved
[bank
].both
;
629 gpio_array
[bank
]->maska
= gpio_bank_saved
[bank
].maska
;
632 #ifdef BFIN_SPECIAL_GPIO_BANKS
633 bfin_special_gpio_pm_hibernate_restore();
636 AWA_DUMMY_READ(maska
);
642 /***********************************************************
644 * FUNCTIONS: Blackfin Peripheral Resource Allocation
648 * per Peripheral Identifier
651 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
654 *************************************************************
655 * MODIFICATION HISTORY :
656 **************************************************************/
658 int peripheral_request(unsigned short per
, const char *label
)
661 unsigned short ident
= P_IDENT(per
);
664 * Don't cares are pins with only one dedicated function
667 if (per
& P_DONTCARE
)
670 if (!(per
& P_DEFINED
))
673 BUG_ON(ident
>= MAX_RESOURCES
);
675 flags
= hard_local_irq_save();
677 /* If a pin can be muxed as either GPIO or peripheral, make
678 * sure it is not already a GPIO pin when we request it.
680 if (unlikely(!check_gpio(ident
) && is_reserved(gpio
, ident
, 1))) {
681 if (system_state
== SYSTEM_BOOTING
)
684 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
685 __func__
, ident
, get_label(ident
));
686 hard_local_irq_restore(flags
);
690 if (unlikely(is_reserved(peri
, ident
, 1))) {
693 * Pin functions like AMC address strobes my
694 * be requested and used by several drivers
697 if (!(per
& P_MAYSHARE
)) {
699 * Allow that the identical pin function can
700 * be requested from the same driver twice
703 if (cmp_label(ident
, label
) == 0)
706 if (system_state
== SYSTEM_BOOTING
)
709 "%s: Peripheral %d function %d is already reserved by %s !\n",
710 __func__
, ident
, P_FUNCT2MUX(per
), get_label(ident
));
711 hard_local_irq_restore(flags
);
716 if (unlikely(portmux_group_check(per
))) {
717 hard_local_irq_restore(flags
);
721 reserve(peri
, ident
);
724 port_setup(ident
, PERIPHERAL_USAGE
);
726 hard_local_irq_restore(flags
);
727 set_label(ident
, label
);
731 EXPORT_SYMBOL(peripheral_request
);
733 int peripheral_request_list(const unsigned short per
[], const char *label
)
738 for (cnt
= 0; per
[cnt
] != 0; cnt
++) {
740 ret
= peripheral_request(per
[cnt
], label
);
743 for ( ; cnt
> 0; cnt
--)
744 peripheral_free(per
[cnt
- 1]);
752 EXPORT_SYMBOL(peripheral_request_list
);
754 void peripheral_free(unsigned short per
)
757 unsigned short ident
= P_IDENT(per
);
759 if (per
& P_DONTCARE
)
762 if (!(per
& P_DEFINED
))
765 flags
= hard_local_irq_save();
767 if (unlikely(!is_reserved(peri
, ident
, 0))) {
768 hard_local_irq_restore(flags
);
772 if (!(per
& P_MAYSHARE
))
773 port_setup(ident
, GPIO_USAGE
);
775 unreserve(peri
, ident
);
777 set_label(ident
, "free");
779 hard_local_irq_restore(flags
);
781 EXPORT_SYMBOL(peripheral_free
);
783 void peripheral_free_list(const unsigned short per
[])
786 for (cnt
= 0; per
[cnt
] != 0; cnt
++)
787 peripheral_free(per
[cnt
]);
789 EXPORT_SYMBOL(peripheral_free_list
);
791 /***********************************************************
793 * FUNCTIONS: Blackfin GPIO Driver
796 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
799 * DESCRIPTION: Blackfin GPIO Driver API
802 *************************************************************
803 * MODIFICATION HISTORY :
804 **************************************************************/
806 int bfin_gpio_request(unsigned gpio
, const char *label
)
810 if (check_gpio(gpio
) < 0)
813 flags
= hard_local_irq_save();
816 * Allow that the identical GPIO can
817 * be requested from the same driver twice
818 * Do nothing and return -
821 if (cmp_label(gpio
, label
) == 0) {
822 hard_local_irq_restore(flags
);
826 if (unlikely(is_reserved(gpio
, gpio
, 1))) {
827 if (system_state
== SYSTEM_BOOTING
)
829 printk(KERN_ERR
"bfin-gpio: GPIO %d is already reserved by %s !\n",
830 gpio
, get_label(gpio
));
831 hard_local_irq_restore(flags
);
834 if (unlikely(is_reserved(peri
, gpio
, 1))) {
835 if (system_state
== SYSTEM_BOOTING
)
838 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
839 gpio
, get_label(gpio
));
840 hard_local_irq_restore(flags
);
843 if (unlikely(is_reserved(gpio_irq
, gpio
, 1))) {
844 printk(KERN_NOTICE
"bfin-gpio: GPIO %d is already reserved as gpio-irq!"
845 " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio
);
846 } else { /* Reset POLAR setting when acquiring a gpio for the first time */
847 set_gpio_polar(gpio
, 0);
851 set_label(gpio
, label
);
853 hard_local_irq_restore(flags
);
855 port_setup(gpio
, GPIO_USAGE
);
859 EXPORT_SYMBOL(bfin_gpio_request
);
861 void bfin_gpio_free(unsigned gpio
)
865 if (check_gpio(gpio
) < 0)
870 flags
= hard_local_irq_save();
872 if (unlikely(!is_reserved(gpio
, gpio
, 0))) {
873 if (system_state
== SYSTEM_BOOTING
)
876 hard_local_irq_restore(flags
);
880 unreserve(gpio
, gpio
);
882 set_label(gpio
, "free");
884 hard_local_irq_restore(flags
);
886 EXPORT_SYMBOL(bfin_gpio_free
);
888 #ifdef BFIN_SPECIAL_GPIO_BANKS
889 DECLARE_RESERVED_MAP(special_gpio
, gpio_bank(MAX_RESOURCES
));
891 int bfin_special_gpio_request(unsigned gpio
, const char *label
)
895 flags
= hard_local_irq_save();
898 * Allow that the identical GPIO can
899 * be requested from the same driver twice
900 * Do nothing and return -
903 if (cmp_label(gpio
, label
) == 0) {
904 hard_local_irq_restore(flags
);
908 if (unlikely(is_reserved(special_gpio
, gpio
, 1))) {
909 hard_local_irq_restore(flags
);
910 printk(KERN_ERR
"bfin-gpio: GPIO %d is already reserved by %s !\n",
911 gpio
, get_label(gpio
));
915 if (unlikely(is_reserved(peri
, gpio
, 1))) {
916 hard_local_irq_restore(flags
);
918 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
919 gpio
, get_label(gpio
));
924 reserve(special_gpio
, gpio
);
927 set_label(gpio
, label
);
928 hard_local_irq_restore(flags
);
929 port_setup(gpio
, GPIO_USAGE
);
933 EXPORT_SYMBOL(bfin_special_gpio_request
);
935 void bfin_special_gpio_free(unsigned gpio
)
941 flags
= hard_local_irq_save();
943 if (unlikely(!is_reserved(special_gpio
, gpio
, 0))) {
945 hard_local_irq_restore(flags
);
949 unreserve(special_gpio
, gpio
);
950 unreserve(peri
, gpio
);
951 set_label(gpio
, "free");
952 hard_local_irq_restore(flags
);
954 EXPORT_SYMBOL(bfin_special_gpio_free
);
958 int bfin_gpio_irq_request(unsigned gpio
, const char *label
)
962 if (check_gpio(gpio
) < 0)
965 flags
= hard_local_irq_save();
967 if (unlikely(is_reserved(peri
, gpio
, 1))) {
968 if (system_state
== SYSTEM_BOOTING
)
971 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
972 gpio
, get_label(gpio
));
973 hard_local_irq_restore(flags
);
976 if (unlikely(is_reserved(gpio
, gpio
, 1)))
977 printk(KERN_NOTICE
"bfin-gpio: GPIO %d is already reserved by %s! "
978 "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
979 gpio
, get_label(gpio
));
981 reserve(gpio_irq
, gpio
);
982 set_label(gpio
, label
);
984 hard_local_irq_restore(flags
);
986 port_setup(gpio
, GPIO_USAGE
);
991 void bfin_gpio_irq_free(unsigned gpio
)
995 if (check_gpio(gpio
) < 0)
998 flags
= hard_local_irq_save();
1000 if (unlikely(!is_reserved(gpio_irq
, gpio
, 0))) {
1001 if (system_state
== SYSTEM_BOOTING
)
1004 hard_local_irq_restore(flags
);
1008 unreserve(gpio_irq
, gpio
);
1010 set_label(gpio
, "free");
1012 hard_local_irq_restore(flags
);
1015 static inline void __bfin_gpio_direction_input(unsigned gpio
)
1017 gpio_array
[gpio_bank(gpio
)]->dir
&= ~gpio_bit(gpio
);
1018 gpio_array
[gpio_bank(gpio
)]->inen
|= gpio_bit(gpio
);
1021 int bfin_gpio_direction_input(unsigned gpio
)
1023 unsigned long flags
;
1025 if (unlikely(!is_reserved(gpio
, gpio
, 0))) {
1030 flags
= hard_local_irq_save();
1031 __bfin_gpio_direction_input(gpio
);
1032 AWA_DUMMY_READ(inen
);
1033 hard_local_irq_restore(flags
);
1037 EXPORT_SYMBOL(bfin_gpio_direction_input
);
1039 void bfin_gpio_irq_prepare(unsigned gpio
)
1041 port_setup(gpio
, GPIO_USAGE
);
1044 void bfin_gpio_set_value(unsigned gpio
, int arg
)
1047 gpio_array
[gpio_bank(gpio
)]->data_set
= gpio_bit(gpio
);
1049 gpio_array
[gpio_bank(gpio
)]->data_clear
= gpio_bit(gpio
);
1051 EXPORT_SYMBOL(bfin_gpio_set_value
);
1053 int bfin_gpio_direction_output(unsigned gpio
, int value
)
1055 unsigned long flags
;
1057 if (unlikely(!is_reserved(gpio
, gpio
, 0))) {
1062 flags
= hard_local_irq_save();
1064 gpio_array
[gpio_bank(gpio
)]->inen
&= ~gpio_bit(gpio
);
1065 gpio_set_value(gpio
, value
);
1066 gpio_array
[gpio_bank(gpio
)]->dir
|= gpio_bit(gpio
);
1068 AWA_DUMMY_READ(dir
);
1069 hard_local_irq_restore(flags
);
1073 EXPORT_SYMBOL(bfin_gpio_direction_output
);
1075 int bfin_gpio_get_value(unsigned gpio
)
1077 unsigned long flags
;
1079 if (unlikely(get_gpio_edge(gpio
))) {
1081 flags
= hard_local_irq_save();
1082 set_gpio_edge(gpio
, 0);
1083 ret
= get_gpio_data(gpio
);
1084 set_gpio_edge(gpio
, 1);
1085 hard_local_irq_restore(flags
);
1088 return get_gpio_data(gpio
);
1090 EXPORT_SYMBOL(bfin_gpio_get_value
);
1092 /* If we are booting from SPI and our board lacks a strong enough pull up,
1093 * the core can reset and execute the bootrom faster than the resistor can
1094 * pull the signal logically high. To work around this (common) error in
1095 * board design, we explicitly set the pin back to GPIO mode, force /CS
1096 * high, and wait for the electrons to do their thing.
1098 * This function only makes sense to be called from reset code, but it
1099 * lives here as we need to force all the GPIO states w/out going through
1100 * BUG() checks and such.
1102 void bfin_reset_boot_spi_cs(unsigned short pin
)
1104 unsigned short gpio
= P_IDENT(pin
);
1105 port_setup(gpio
, GPIO_USAGE
);
1106 gpio_array
[gpio_bank(gpio
)]->data_set
= gpio_bit(gpio
);
1107 AWA_DUMMY_READ(data_set
);
1111 #if defined(CONFIG_PROC_FS)
1112 static int gpio_proc_show(struct seq_file
*m
, void *v
)
1116 for (c
= 0; c
< MAX_RESOURCES
; c
++) {
1117 irq
= is_reserved(gpio_irq
, c
, 1);
1118 gpio
= is_reserved(gpio
, c
, 1);
1119 if (!check_gpio(c
) && (gpio
|| irq
))
1120 seq_printf(m
, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c
,
1121 get_label(c
), (gpio
&& irq
) ? " *" : "",
1122 get_gpio_dir(c
) ? "OUTPUT" : "INPUT");
1123 else if (is_reserved(peri
, c
, 1))
1124 seq_printf(m
, "GPIO_%d: \t%s \t\tPeripheral\n", c
, get_label(c
));
1132 static int gpio_proc_open(struct inode
*inode
, struct file
*file
)
1134 return single_open(file
, gpio_proc_show
, NULL
);
1137 static const struct file_operations gpio_proc_ops
= {
1138 .open
= gpio_proc_open
,
1140 .llseek
= seq_lseek
,
1141 .release
= single_release
,
1144 static __init
int gpio_register_proc(void)
1146 struct proc_dir_entry
*proc_gpio
;
1148 proc_gpio
= proc_create("gpio", 0, NULL
, &gpio_proc_ops
);
1149 return proc_gpio
== NULL
;
1151 __initcall(gpio_register_proc
);
1154 #ifdef CONFIG_GPIOLIB
1155 static int bfin_gpiolib_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
1157 return bfin_gpio_direction_input(gpio
);
1160 static int bfin_gpiolib_direction_output(struct gpio_chip
*chip
, unsigned gpio
, int level
)
1162 return bfin_gpio_direction_output(gpio
, level
);
1165 static int bfin_gpiolib_get_value(struct gpio_chip
*chip
, unsigned gpio
)
1167 return !!bfin_gpio_get_value(gpio
);
1170 static void bfin_gpiolib_set_value(struct gpio_chip
*chip
, unsigned gpio
, int value
)
1172 return bfin_gpio_set_value(gpio
, value
);
1175 static int bfin_gpiolib_gpio_request(struct gpio_chip
*chip
, unsigned gpio
)
1177 return bfin_gpio_request(gpio
, chip
->label
);
1180 static void bfin_gpiolib_gpio_free(struct gpio_chip
*chip
, unsigned gpio
)
1182 return bfin_gpio_free(gpio
);
1185 static int bfin_gpiolib_gpio_to_irq(struct gpio_chip
*chip
, unsigned gpio
)
1187 return gpio
+ GPIO_IRQ_BASE
;
1190 static struct gpio_chip bfin_chip
= {
1191 .label
= "BFIN-GPIO",
1192 .direction_input
= bfin_gpiolib_direction_input
,
1193 .get
= bfin_gpiolib_get_value
,
1194 .direction_output
= bfin_gpiolib_direction_output
,
1195 .set
= bfin_gpiolib_set_value
,
1196 .request
= bfin_gpiolib_gpio_request
,
1197 .free
= bfin_gpiolib_gpio_free
,
1198 .to_irq
= bfin_gpiolib_gpio_to_irq
,
1200 .ngpio
= MAX_BLACKFIN_GPIOS
,
1203 static int __init
bfin_gpiolib_setup(void)
1205 return gpiochip_add_data(&bfin_chip
, NULL
);
1207 arch_initcall(bfin_gpiolib_setup
);