2 * File: arch/blackfin/kernel/bfin_gpio.c
4 * Author: Michael Hennerich (hennerich@blackfin.uclinux.org)
7 * Description: GPIO Abstraction Layer
10 * Copyright 2007 Analog Devices Inc.
12 * Bugs: Enter bugs at http://blackfin.uclinux.org/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see the file COPYING, or write
26 * to the Free Software Foundation, Inc.,
27 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 * Number BF537/6/4 BF561 BF533/2/1 BF549/8/4/2
33 * GPIO_0 PF0 PF0 PF0 PA0...PJ13
43 * GPIO_10 PF10 PF10 PF10
44 * GPIO_11 PF11 PF11 PF11
45 * GPIO_12 PF12 PF12 PF12
46 * GPIO_13 PF13 PF13 PF13
47 * GPIO_14 PF14 PF14 PF14
48 * GPIO_15 PF15 PF15 PF15
83 #include <linux/delay.h>
84 #include <linux/module.h>
85 #include <linux/err.h>
86 #include <asm/blackfin.h>
88 #include <asm/portmux.h>
89 #include <linux/irq.h>
92 static struct gpio_port_t
*gpio_bankb
[gpio_bank(MAX_BLACKFIN_GPIOS
)] = {
93 (struct gpio_port_t
*) FIO_FLAG_D
,
98 static struct gpio_port_t
*gpio_bankb
[gpio_bank(MAX_BLACKFIN_GPIOS
)] = {
99 (struct gpio_port_t
*) PORTFIO
,
100 (struct gpio_port_t
*) PORTGIO
,
101 (struct gpio_port_t
*) PORTHIO
,
104 static unsigned short *port_fer
[gpio_bank(MAX_BLACKFIN_GPIOS
)] = {
105 (unsigned short *) PORTF_FER
,
106 (unsigned short *) PORTG_FER
,
107 (unsigned short *) PORTH_FER
,
113 static struct gpio_port_t
*gpio_bankb
[gpio_bank(MAX_BLACKFIN_GPIOS
)] = {
114 (struct gpio_port_t
*) FIO0_FLAG_D
,
115 (struct gpio_port_t
*) FIO1_FLAG_D
,
116 (struct gpio_port_t
*) FIO2_FLAG_D
,
121 static struct gpio_port_t
*gpio_array
[gpio_bank(MAX_BLACKFIN_GPIOS
)] = {
122 (struct gpio_port_t
*)PORTA_FER
,
123 (struct gpio_port_t
*)PORTB_FER
,
124 (struct gpio_port_t
*)PORTC_FER
,
125 (struct gpio_port_t
*)PORTD_FER
,
126 (struct gpio_port_t
*)PORTE_FER
,
127 (struct gpio_port_t
*)PORTF_FER
,
128 (struct gpio_port_t
*)PORTG_FER
,
129 (struct gpio_port_t
*)PORTH_FER
,
130 (struct gpio_port_t
*)PORTI_FER
,
131 (struct gpio_port_t
*)PORTJ_FER
,
135 static unsigned short reserved_gpio_map
[gpio_bank(MAX_BLACKFIN_GPIOS
)];
136 static unsigned short reserved_peri_map
[gpio_bank(MAX_BLACKFIN_GPIOS
+ 16)];
138 #define MAX_RESOURCES 256
139 #define RESOURCE_LABEL_SIZE 16
142 char name
[RESOURCE_LABEL_SIZE
];
147 static unsigned short wakeup_map
[gpio_bank(MAX_BLACKFIN_GPIOS
)];
148 static unsigned char wakeup_flags_map
[MAX_BLACKFIN_GPIOS
];
149 static struct gpio_port_s gpio_bank_saved
[gpio_bank(MAX_BLACKFIN_GPIOS
)];
152 static unsigned int sic_iwr_irqs
[gpio_bank(MAX_BLACKFIN_GPIOS
)] = {IRQ_PROG_INTB
};
156 static unsigned int sic_iwr_irqs
[gpio_bank(MAX_BLACKFIN_GPIOS
)] = {IRQ_PROG_INTB
, IRQ_PORTG_INTB
, IRQ_MAC_TX
};
160 static unsigned int sic_iwr_irqs
[gpio_bank(MAX_BLACKFIN_GPIOS
)] = {IRQ_PROG0_INTB
, IRQ_PROG1_INTB
, IRQ_PROG2_INTB
};
163 #endif /* CONFIG_PM */
165 #if defined(BF548_FAMILY)
166 inline int check_gpio(unsigned short gpio
)
168 if (gpio
== GPIO_PB15
|| gpio
== GPIO_PC14
|| gpio
== GPIO_PC15
169 || gpio
== GPIO_PH14
|| gpio
== GPIO_PH15
170 || gpio
== GPIO_PJ14
|| gpio
== GPIO_PJ15
171 || gpio
> MAX_BLACKFIN_GPIOS
)
176 inline int check_gpio(unsigned short gpio
)
178 if (gpio
>= MAX_BLACKFIN_GPIOS
)
184 static void set_label(unsigned short ident
, const char *label
)
187 if (label
&& str_ident
) {
188 strncpy(str_ident
[ident
].name
, label
,
189 RESOURCE_LABEL_SIZE
);
190 str_ident
[ident
].name
[RESOURCE_LABEL_SIZE
- 1] = 0;
194 static char *get_label(unsigned short ident
)
199 return (*str_ident
[ident
].name
? str_ident
[ident
].name
: "UNKNOWN");
202 static int cmp_label(unsigned short ident
, const char *label
)
204 if (label
&& str_ident
)
205 return strncmp(str_ident
[ident
].name
,
206 label
, strlen(label
));
212 static void port_setup(unsigned short gpio
, unsigned short usage
)
214 if (!check_gpio(gpio
)) {
215 if (usage
== GPIO_USAGE
)
216 *port_fer
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
218 *port_fer
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
222 #elif defined(BF548_FAMILY)
223 static void port_setup(unsigned short gpio
, unsigned short usage
)
225 if (usage
== GPIO_USAGE
)
226 gpio_array
[gpio_bank(gpio
)]->port_fer
&= ~gpio_bit(gpio
);
228 gpio_array
[gpio_bank(gpio
)]->port_fer
|= gpio_bit(gpio
);
232 # define port_setup(...) do { } while (0)
238 unsigned short offset
;
240 {.res
= P_PPI0_D13
, .offset
= 11},
241 {.res
= P_PPI0_D14
, .offset
= 11},
242 {.res
= P_PPI0_D15
, .offset
= 11},
243 {.res
= P_SPORT1_TFS
, .offset
= 11},
244 {.res
= P_SPORT1_TSCLK
, .offset
= 11},
245 {.res
= P_SPORT1_DTPRI
, .offset
= 11},
246 {.res
= P_PPI0_D10
, .offset
= 10},
247 {.res
= P_PPI0_D11
, .offset
= 10},
248 {.res
= P_PPI0_D12
, .offset
= 10},
249 {.res
= P_SPORT1_RSCLK
, .offset
= 10},
250 {.res
= P_SPORT1_RFS
, .offset
= 10},
251 {.res
= P_SPORT1_DRPRI
, .offset
= 10},
252 {.res
= P_PPI0_D8
, .offset
= 9},
253 {.res
= P_PPI0_D9
, .offset
= 9},
254 {.res
= P_SPORT1_DRSEC
, .offset
= 9},
255 {.res
= P_SPORT1_DTSEC
, .offset
= 9},
256 {.res
= P_TMR2
, .offset
= 8},
257 {.res
= P_PPI0_FS3
, .offset
= 8},
258 {.res
= P_TMR3
, .offset
= 7},
259 {.res
= P_SPI0_SSEL4
, .offset
= 7},
260 {.res
= P_TMR4
, .offset
= 6},
261 {.res
= P_SPI0_SSEL5
, .offset
= 6},
262 {.res
= P_TMR5
, .offset
= 5},
263 {.res
= P_SPI0_SSEL6
, .offset
= 5},
264 {.res
= P_UART1_RX
, .offset
= 4},
265 {.res
= P_UART1_TX
, .offset
= 4},
266 {.res
= P_TMR6
, .offset
= 4},
267 {.res
= P_TMR7
, .offset
= 4},
268 {.res
= P_UART0_RX
, .offset
= 3},
269 {.res
= P_UART0_TX
, .offset
= 3},
270 {.res
= P_DMAR0
, .offset
= 3},
271 {.res
= P_DMAR1
, .offset
= 3},
272 {.res
= P_SPORT0_DTSEC
, .offset
= 1},
273 {.res
= P_SPORT0_DRSEC
, .offset
= 1},
274 {.res
= P_CAN0_RX
, .offset
= 1},
275 {.res
= P_CAN0_TX
, .offset
= 1},
276 {.res
= P_SPI0_SSEL7
, .offset
= 1},
277 {.res
= P_SPORT0_TFS
, .offset
= 0},
278 {.res
= P_SPORT0_DTPRI
, .offset
= 0},
279 {.res
= P_SPI0_SSEL2
, .offset
= 0},
280 {.res
= P_SPI0_SSEL3
, .offset
= 0},
283 static void portmux_setup(unsigned short per
, unsigned short function
)
285 u16 y
, offset
, muxreg
;
287 for (y
= 0; y
< ARRAY_SIZE(port_mux_lut
); y
++) {
288 if (port_mux_lut
[y
].res
== per
) {
290 /* SET PORTMUX REG */
292 offset
= port_mux_lut
[y
].offset
;
293 muxreg
= bfin_read_PORT_MUX();
296 muxreg
&= ~(1 << offset
);
301 muxreg
|= (function
<< offset
);
302 bfin_write_PORT_MUX(muxreg
);
306 #elif defined(BF548_FAMILY)
307 inline void portmux_setup(unsigned short portno
, unsigned short function
)
311 pmux
= gpio_array
[gpio_bank(portno
)]->port_mux
;
313 pmux
&= ~(0x3 << (2 * gpio_sub_n(portno
)));
314 pmux
|= (function
& 0x3) << (2 * gpio_sub_n(portno
));
316 gpio_array
[gpio_bank(portno
)]->port_mux
= pmux
;
319 inline u16
get_portmux(unsigned short portno
)
323 pmux
= gpio_array
[gpio_bank(portno
)]->port_mux
;
325 return (pmux
>> (2 * gpio_sub_n(portno
)) & 0x3);
328 # define portmux_setup(...) do { } while (0)
332 static void default_gpio(unsigned short gpio
)
334 unsigned short bank
, bitmask
;
336 bank
= gpio_bank(gpio
);
337 bitmask
= gpio_bit(gpio
);
339 gpio_bankb
[bank
]->maska_clear
= bitmask
;
340 gpio_bankb
[bank
]->maskb_clear
= bitmask
;
342 gpio_bankb
[bank
]->inen
&= ~bitmask
;
343 gpio_bankb
[bank
]->dir
&= ~bitmask
;
344 gpio_bankb
[bank
]->polar
&= ~bitmask
;
345 gpio_bankb
[bank
]->both
&= ~bitmask
;
346 gpio_bankb
[bank
]->edge
&= ~bitmask
;
349 # define default_gpio(...) do { } while (0)
352 static int __init
bfin_gpio_init(void)
354 str_ident
= kcalloc(MAX_RESOURCES
,
355 sizeof(struct str_ident
), GFP_KERNEL
);
356 if (str_ident
== NULL
)
359 memset(str_ident
, 0, MAX_RESOURCES
* sizeof(struct str_ident
));
361 printk(KERN_INFO
"Blackfin GPIO Controller\n");
366 arch_initcall(bfin_gpio_init
);
370 /***********************************************************
372 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
375 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
378 * DESCRIPTION: These functions abstract direct register access
379 * to Blackfin processor General Purpose
382 * CAUTION: These functions do not belong to the GPIO Driver API
383 *************************************************************
384 * MODIFICATION HISTORY :
385 **************************************************************/
387 /* Set a specific bit */
389 #define SET_GPIO(name) \
390 void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \
392 unsigned long flags; \
393 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
394 local_irq_save(flags); \
396 gpio_bankb[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
398 gpio_bankb[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
399 local_irq_restore(flags); \
401 EXPORT_SYMBOL(set_gpio_ ## name);
410 #define SET_GPIO_SC(name) \
411 void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \
413 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
415 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
417 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
419 EXPORT_SYMBOL(set_gpio_ ## name);
425 void set_gpio_data(unsigned short gpio
, unsigned short arg
)
428 BUG_ON(!(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
)));
429 local_irq_save(flags
);
431 gpio_bankb
[gpio_bank(gpio
)]->data_set
= gpio_bit(gpio
);
433 gpio_bankb
[gpio_bank(gpio
)]->data_clear
= gpio_bit(gpio
);
435 local_irq_restore(flags
);
437 EXPORT_SYMBOL(set_gpio_data
);
444 void set_gpio_toggle(unsigned short gpio
)
447 BUG_ON(!(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
)));
448 local_irq_save(flags
);
449 gpio_bankb
[gpio_bank(gpio
)]->toggle
= gpio_bit(gpio
);
451 local_irq_restore(flags
);
454 void set_gpio_toggle(unsigned short gpio
)
456 BUG_ON(!(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
)));
457 gpio_bankb
[gpio_bank(gpio
)]->toggle
= gpio_bit(gpio
);
460 EXPORT_SYMBOL(set_gpio_toggle
);
463 /*Set current PORT date (16-bit word)*/
465 #define SET_GPIO_P(name) \
466 void set_gpiop_ ## name(unsigned short gpio, unsigned short arg) \
468 gpio_bankb[gpio_bank(gpio)]->name = arg; \
470 EXPORT_SYMBOL(set_gpiop_ ## name);
482 void set_gpiop_data(unsigned short gpio
, unsigned short arg
)
485 local_irq_save(flags
);
486 gpio_bankb
[gpio_bank(gpio
)]->data
= arg
;
488 local_irq_restore(flags
);
490 EXPORT_SYMBOL(set_gpiop_data
);
497 /* Get a specific bit */
499 #define GET_GPIO(name) \
500 unsigned short get_gpio_ ## name(unsigned short gpio) \
502 return (0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio))); \
504 EXPORT_SYMBOL(get_gpio_ ## name);
516 unsigned short get_gpio_data(unsigned short gpio
)
520 BUG_ON(!(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
)));
521 local_irq_save(flags
);
522 ret
= 0x01 & (gpio_bankb
[gpio_bank(gpio
)]->data
>> gpio_sub_n(gpio
));
524 local_irq_restore(flags
);
527 EXPORT_SYMBOL(get_gpio_data
);
532 /*Get current PORT date (16-bit word)*/
534 #define GET_GPIO_P(name) \
535 unsigned short get_gpiop_ ## name(unsigned short gpio) \
537 return (gpio_bankb[gpio_bank(gpio)]->name);\
539 EXPORT_SYMBOL(get_gpiop_ ## name);
550 unsigned short get_gpiop_data(unsigned short gpio
)
554 local_irq_save(flags
);
555 ret
= gpio_bankb
[gpio_bank(gpio
)]->data
;
557 local_irq_restore(flags
);
560 EXPORT_SYMBOL(get_gpiop_data
);
566 /***********************************************************
568 * FUNCTIONS: Blackfin PM Setup API
571 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
579 * DESCRIPTION: Blackfin PM Driver API
582 *************************************************************
583 * MODIFICATION HISTORY :
584 **************************************************************/
585 int gpio_pm_wakeup_request(unsigned short gpio
, unsigned char type
)
589 if ((check_gpio(gpio
) < 0) || !type
)
592 local_irq_save(flags
);
594 wakeup_map
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
595 wakeup_flags_map
[gpio
] = type
;
596 local_irq_restore(flags
);
600 EXPORT_SYMBOL(gpio_pm_wakeup_request
);
602 void gpio_pm_wakeup_free(unsigned short gpio
)
606 if (check_gpio(gpio
) < 0)
609 local_irq_save(flags
);
611 wakeup_map
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
613 local_irq_restore(flags
);
615 EXPORT_SYMBOL(gpio_pm_wakeup_free
);
617 static int bfin_gpio_wakeup_type(unsigned short gpio
, unsigned char type
)
619 port_setup(gpio
, GPIO_USAGE
);
620 set_gpio_dir(gpio
, 0);
621 set_gpio_inen(gpio
, 1);
623 if (type
& (PM_WAKE_RISING
| PM_WAKE_FALLING
))
624 set_gpio_edge(gpio
, 1);
626 set_gpio_edge(gpio
, 0);
628 if ((type
& (PM_WAKE_BOTH_EDGES
)) == (PM_WAKE_BOTH_EDGES
))
629 set_gpio_both(gpio
, 1);
631 set_gpio_both(gpio
, 0);
633 if ((type
& (PM_WAKE_FALLING
| PM_WAKE_LOW
)))
634 set_gpio_polar(gpio
, 1);
636 set_gpio_polar(gpio
, 0);
643 u32
gpio_pm_setup(void)
646 u16 bank
, mask
, i
, gpio
;
648 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
649 mask
= wakeup_map
[gpio_bank(i
)];
652 gpio_bank_saved
[bank
].maskb
= gpio_bankb
[bank
]->maskb
;
653 gpio_bankb
[bank
]->maskb
= 0;
657 gpio_bank_saved
[bank
].fer
= *port_fer
[bank
];
659 gpio_bank_saved
[bank
].inen
= gpio_bankb
[bank
]->inen
;
660 gpio_bank_saved
[bank
].polar
= gpio_bankb
[bank
]->polar
;
661 gpio_bank_saved
[bank
].dir
= gpio_bankb
[bank
]->dir
;
662 gpio_bank_saved
[bank
].edge
= gpio_bankb
[bank
]->edge
;
663 gpio_bank_saved
[bank
].both
= gpio_bankb
[bank
]->both
;
664 gpio_bank_saved
[bank
].reserved
=
665 reserved_gpio_map
[bank
];
671 reserved_gpio_map
[gpio_bank(gpio
)] |=
673 bfin_gpio_wakeup_type(gpio
,
674 wakeup_flags_map
[gpio
]);
675 set_gpio_data(gpio
, 0); /*Clear*/
682 (sic_iwr_irqs
[bank
] - (IRQ_CORETMR
+ 1));
683 gpio_bankb
[bank
]->maskb_set
= wakeup_map
[gpio_bank(i
)];
690 return IWR_ENABLE_ALL
;
693 void gpio_pm_restore(void)
697 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
698 mask
= wakeup_map
[gpio_bank(i
)];
703 *port_fer
[bank
] = gpio_bank_saved
[bank
].fer
;
705 gpio_bankb
[bank
]->inen
= gpio_bank_saved
[bank
].inen
;
706 gpio_bankb
[bank
]->dir
= gpio_bank_saved
[bank
].dir
;
707 gpio_bankb
[bank
]->polar
= gpio_bank_saved
[bank
].polar
;
708 gpio_bankb
[bank
]->edge
= gpio_bank_saved
[bank
].edge
;
709 gpio_bankb
[bank
]->both
= gpio_bank_saved
[bank
].both
;
711 reserved_gpio_map
[bank
] =
712 gpio_bank_saved
[bank
].reserved
;
716 gpio_bankb
[bank
]->maskb
= gpio_bank_saved
[bank
].maskb
;
721 #endif /* BF548_FAMILY */
723 /***********************************************************
725 * FUNCTIONS: Blackfin Peripheral Resource Allocation
729 * per Peripheral Identifier
732 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
735 *************************************************************
736 * MODIFICATION HISTORY :
737 **************************************************************/
740 int peripheral_request(unsigned short per
, const char *label
)
743 unsigned short ident
= P_IDENT(per
);
746 * Don't cares are pins with only one dedicated function
749 if (per
& P_DONTCARE
)
752 if (!(per
& P_DEFINED
))
755 if (check_gpio(ident
) < 0)
758 local_irq_save(flags
);
760 if (unlikely(reserved_gpio_map
[gpio_bank(ident
)] & gpio_bit(ident
))) {
762 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
763 __FUNCTION__
, ident
, get_label(ident
));
765 local_irq_restore(flags
);
769 if (unlikely(reserved_peri_map
[gpio_bank(ident
)] & gpio_bit(ident
))) {
771 u16 funct
= get_portmux(ident
);
774 * Pin functions like AMC address strobes my
775 * be requested and used by several drivers
778 if (!((per
& P_MAYSHARE
) && (funct
== P_FUNCT2MUX(per
)))) {
781 * Allow that the identical pin function can
782 * be requested from the same driver twice
785 if (cmp_label(ident
, label
) == 0)
789 "%s: Peripheral %d function %d is already reserved by %s !\n",
790 __FUNCTION__
, ident
, P_FUNCT2MUX(per
), get_label(ident
));
792 local_irq_restore(flags
);
798 reserved_peri_map
[gpio_bank(ident
)] |= gpio_bit(ident
);
800 portmux_setup(ident
, P_FUNCT2MUX(per
));
801 port_setup(ident
, PERIPHERAL_USAGE
);
803 local_irq_restore(flags
);
804 set_label(ident
, label
);
808 EXPORT_SYMBOL(peripheral_request
);
811 int peripheral_request(unsigned short per
, const char *label
)
814 unsigned short ident
= P_IDENT(per
);
817 * Don't cares are pins with only one dedicated function
820 if (per
& P_DONTCARE
)
823 if (!(per
& P_DEFINED
))
826 local_irq_save(flags
);
828 if (!check_gpio(ident
)) {
830 if (unlikely(reserved_gpio_map
[gpio_bank(ident
)] & gpio_bit(ident
))) {
832 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
833 __FUNCTION__
, ident
, get_label(ident
));
835 local_irq_restore(flags
);
841 if (unlikely(reserved_peri_map
[gpio_bank(ident
)] & gpio_bit(ident
))) {
844 * Pin functions like AMC address strobes my
845 * be requested and used by several drivers
848 if (!(per
& P_MAYSHARE
)) {
851 * Allow that the identical pin function can
852 * be requested from the same driver twice
855 if (cmp_label(ident
, label
) == 0)
859 "%s: Peripheral %d function %d is already"
860 " reserved by %s !\n",
861 __FUNCTION__
, ident
, P_FUNCT2MUX(per
),
864 local_irq_restore(flags
);
871 portmux_setup(per
, P_FUNCT2MUX(per
));
873 port_setup(ident
, PERIPHERAL_USAGE
);
875 reserved_peri_map
[gpio_bank(ident
)] |= gpio_bit(ident
);
876 local_irq_restore(flags
);
877 set_label(ident
, label
);
881 EXPORT_SYMBOL(peripheral_request
);
884 int peripheral_request_list(unsigned short per
[], const char *label
)
889 for (cnt
= 0; per
[cnt
] != 0; cnt
++) {
891 ret
= peripheral_request(per
[cnt
], label
);
894 for ( ; cnt
> 0; cnt
--) {
895 peripheral_free(per
[cnt
- 1]);
903 EXPORT_SYMBOL(peripheral_request_list
);
905 void peripheral_free(unsigned short per
)
908 unsigned short ident
= P_IDENT(per
);
910 if (per
& P_DONTCARE
)
913 if (!(per
& P_DEFINED
))
916 if (check_gpio(ident
) < 0)
919 local_irq_save(flags
);
921 if (unlikely(!(reserved_peri_map
[gpio_bank(ident
)]
922 & gpio_bit(ident
)))) {
923 local_irq_restore(flags
);
927 if (!(per
& P_MAYSHARE
)) {
928 port_setup(ident
, GPIO_USAGE
);
931 reserved_peri_map
[gpio_bank(ident
)] &= ~gpio_bit(ident
);
933 set_label(ident
, "free");
935 local_irq_restore(flags
);
937 EXPORT_SYMBOL(peripheral_free
);
939 void peripheral_free_list(unsigned short per
[])
943 for (cnt
= 0; per
[cnt
] != 0; cnt
++) {
944 peripheral_free(per
[cnt
]);
948 EXPORT_SYMBOL(peripheral_free_list
);
950 /***********************************************************
952 * FUNCTIONS: Blackfin GPIO Driver
955 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
958 * DESCRIPTION: Blackfin GPIO Driver API
961 *************************************************************
962 * MODIFICATION HISTORY :
963 **************************************************************/
965 int gpio_request(unsigned short gpio
, const char *label
)
969 if (check_gpio(gpio
) < 0)
972 local_irq_save(flags
);
975 * Allow that the identical GPIO can
976 * be requested from the same driver twice
977 * Do nothing and return -
980 if (cmp_label(gpio
, label
) == 0) {
981 local_irq_restore(flags
);
985 if (unlikely(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
986 printk(KERN_ERR
"bfin-gpio: GPIO %d is already reserved by %s !\n",
987 gpio
, get_label(gpio
));
989 local_irq_restore(flags
);
992 if (unlikely(reserved_peri_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
994 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
995 gpio
, get_label(gpio
));
997 local_irq_restore(flags
);
1001 reserved_gpio_map
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
1003 local_irq_restore(flags
);
1005 port_setup(gpio
, GPIO_USAGE
);
1006 set_label(gpio
, label
);
1010 EXPORT_SYMBOL(gpio_request
);
1012 void gpio_free(unsigned short gpio
)
1014 unsigned long flags
;
1016 if (check_gpio(gpio
) < 0)
1019 local_irq_save(flags
);
1021 if (unlikely(!(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
)))) {
1022 printk(KERN_ERR
"bfin-gpio: GPIO %d wasn't reserved!\n", gpio
);
1024 local_irq_restore(flags
);
1030 reserved_gpio_map
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
1032 set_label(gpio
, "free");
1034 local_irq_restore(flags
);
1036 EXPORT_SYMBOL(gpio_free
);
1039 void gpio_direction_input(unsigned short gpio
)
1041 unsigned long flags
;
1043 BUG_ON(!(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
)));
1045 local_irq_save(flags
);
1046 gpio_array
[gpio_bank(gpio
)]->port_dir_clear
= gpio_bit(gpio
);
1047 gpio_array
[gpio_bank(gpio
)]->port_inen
|= gpio_bit(gpio
);
1048 local_irq_restore(flags
);
1050 EXPORT_SYMBOL(gpio_direction_input
);
1052 void gpio_direction_output(unsigned short gpio
)
1054 unsigned long flags
;
1056 BUG_ON(!(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
)));
1058 local_irq_save(flags
);
1059 gpio_array
[gpio_bank(gpio
)]->port_inen
&= ~gpio_bit(gpio
);
1060 gpio_array
[gpio_bank(gpio
)]->port_dir_set
= gpio_bit(gpio
);
1061 local_irq_restore(flags
);
1063 EXPORT_SYMBOL(gpio_direction_output
);
1065 void gpio_set_value(unsigned short gpio
, unsigned short arg
)
1068 gpio_array
[gpio_bank(gpio
)]->port_set
= gpio_bit(gpio
);
1070 gpio_array
[gpio_bank(gpio
)]->port_clear
= gpio_bit(gpio
);
1073 EXPORT_SYMBOL(gpio_set_value
);
1075 unsigned short gpio_get_value(unsigned short gpio
)
1077 return (1 & (gpio_array
[gpio_bank(gpio
)]->port_data
>> gpio_sub_n(gpio
)));
1079 EXPORT_SYMBOL(gpio_get_value
);
1083 void gpio_direction_input(unsigned short gpio
)
1085 unsigned long flags
;
1087 BUG_ON(!(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
)));
1089 local_irq_save(flags
);
1090 gpio_bankb
[gpio_bank(gpio
)]->dir
&= ~gpio_bit(gpio
);
1091 gpio_bankb
[gpio_bank(gpio
)]->inen
|= gpio_bit(gpio
);
1092 local_irq_restore(flags
);
1094 EXPORT_SYMBOL(gpio_direction_input
);
1096 void gpio_direction_output(unsigned short gpio
)
1098 unsigned long flags
;
1100 BUG_ON(!(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
)));
1102 local_irq_save(flags
);
1103 gpio_bankb
[gpio_bank(gpio
)]->inen
&= ~gpio_bit(gpio
);
1104 gpio_bankb
[gpio_bank(gpio
)]->dir
|= gpio_bit(gpio
);
1105 local_irq_restore(flags
);
1107 EXPORT_SYMBOL(gpio_direction_output
);
1109 /* If we are booting from SPI and our board lacks a strong enough pull up,
1110 * the core can reset and execute the bootrom faster than the resistor can
1111 * pull the signal logically high. To work around this (common) error in
1112 * board design, we explicitly set the pin back to GPIO mode, force /CS
1113 * high, and wait for the electrons to do their thing.
1115 * This function only makes sense to be called from reset code, but it
1116 * lives here as we need to force all the GPIO states w/out going through
1117 * BUG() checks and such.
1119 void bfin_gpio_reset_spi0_ssel1(void)
1121 u16 gpio
= P_IDENT(P_SPI0_SSEL1
);
1123 port_setup(gpio
, GPIO_USAGE
);
1124 gpio_bankb
[gpio_bank(gpio
)]->data_set
= gpio_bit(gpio
);
1128 #endif /*BF548_FAMILY */