2 * File: arch/blackfin/kernel/bfin_gpio.c
4 * Author: Michael Hennerich (hennerich@blackfin.uclinux.org)
7 * Description: GPIO Abstraction Layer
10 * Copyright 2006 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
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/module.h>
84 #include <linux/err.h>
85 #include <asm/blackfin.h>
87 #include <asm/portmux.h>
88 #include <linux/irq.h>
91 static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
92 (struct gpio_port_t *) FIO_FLAG_D,
97 static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
98 (struct gpio_port_t *) PORTFIO,
99 (struct gpio_port_t *) PORTGIO,
100 (struct gpio_port_t *) PORTHIO,
103 static unsigned short *port_fer[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
104 (unsigned short *) PORTF_FER,
105 (unsigned short *) PORTG_FER,
106 (unsigned short *) PORTH_FER,
112 static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
113 (struct gpio_port_t *) FIO0_FLAG_D,
114 (struct gpio_port_t *) FIO1_FLAG_D,
115 (struct gpio_port_t *) FIO2_FLAG_D,
119 static unsigned short reserved_gpio_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
120 static unsigned short reserved_peri_map[gpio_bank(MAX_BLACKFIN_GPIOS + 16)];
122 #define MAX_RESOURCES 256
123 #define RESOURCE_LABEL_SIZE 16
126 char name[RESOURCE_LABEL_SIZE];
131 static unsigned short wakeup_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
132 static unsigned char wakeup_flags_map[MAX_BLACKFIN_GPIOS];
133 static struct gpio_port_s gpio_bank_saved[gpio_bank(MAX_BLACKFIN_GPIOS)];
136 static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB};
140 static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB, IRQ_PORTG_INTB, IRQ_MAC_TX};
144 static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB};
147 #endif /* CONFIG_PM */
149 inline int check_gpio(unsigned short gpio)
151 if (gpio >= MAX_BLACKFIN_GPIOS)
156 static void set_label(unsigned short ident, const char *label)
159 if (label && str_ident) {
160 strncpy(str_ident[ident].name, label,
161 RESOURCE_LABEL_SIZE);
162 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
166 static char *get_label(unsigned short ident)
171 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
174 static int cmp_label(unsigned short ident, const char *label)
176 if (label && str_ident)
177 return strncmp(str_ident[ident].name,
178 label, strlen(label));
184 static void port_setup(unsigned short gpio, unsigned short usage)
186 if (!check_gpio(gpio)) {
187 if (usage == GPIO_USAGE) {
188 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
190 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
195 # define port_setup(...) do { } while (0)
202 unsigned short offset;
204 {.res = P_PPI0_D13, .offset = 11},
205 {.res = P_PPI0_D14, .offset = 11},
206 {.res = P_PPI0_D15, .offset = 11},
207 {.res = P_SPORT1_TFS, .offset = 11},
208 {.res = P_SPORT1_TSCLK, .offset = 11},
209 {.res = P_SPORT1_DTPRI, .offset = 11},
210 {.res = P_PPI0_D10, .offset = 10},
211 {.res = P_PPI0_D11, .offset = 10},
212 {.res = P_PPI0_D12, .offset = 10},
213 {.res = P_SPORT1_RSCLK, .offset = 10},
214 {.res = P_SPORT1_RFS, .offset = 10},
215 {.res = P_SPORT1_DRPRI, .offset = 10},
216 {.res = P_PPI0_D8, .offset = 9},
217 {.res = P_PPI0_D9, .offset = 9},
218 {.res = P_SPORT1_DRSEC, .offset = 9},
219 {.res = P_SPORT1_DTSEC, .offset = 9},
220 {.res = P_TMR2, .offset = 8},
221 {.res = P_PPI0_FS3, .offset = 8},
222 {.res = P_TMR3, .offset = 7},
223 {.res = P_SPI0_SSEL4, .offset = 7},
224 {.res = P_TMR4, .offset = 6},
225 {.res = P_SPI0_SSEL5, .offset = 6},
226 {.res = P_TMR5, .offset = 5},
227 {.res = P_SPI0_SSEL6, .offset = 5},
228 {.res = P_UART1_RX, .offset = 4},
229 {.res = P_UART1_TX, .offset = 4},
230 {.res = P_TMR6, .offset = 4},
231 {.res = P_TMR7, .offset = 4},
232 {.res = P_UART0_RX, .offset = 3},
233 {.res = P_UART0_TX, .offset = 3},
234 {.res = P_DMAR0, .offset = 3},
235 {.res = P_DMAR1, .offset = 3},
236 {.res = P_SPORT0_DTSEC, .offset = 1},
237 {.res = P_SPORT0_DRSEC, .offset = 1},
238 {.res = P_CAN0_RX, .offset = 1},
239 {.res = P_CAN0_TX, .offset = 1},
240 {.res = P_SPI0_SSEL7, .offset = 1},
241 {.res = P_SPORT0_TFS, .offset = 0},
242 {.res = P_SPORT0_DTPRI, .offset = 0},
243 {.res = P_SPI0_SSEL2, .offset = 0},
244 {.res = P_SPI0_SSEL3, .offset = 0},
247 static void portmux_setup(unsigned short per, unsigned short function)
249 u16 y, offset, muxreg;
251 for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
252 if (port_mux_lut[y].res == per) {
254 /* SET PORTMUX REG */
256 offset = port_mux_lut[y].offset;
257 muxreg = bfin_read_PORT_MUX();
260 muxreg &= ~(1 << offset);
265 muxreg |= (function << offset);
266 bfin_write_PORT_MUX(muxreg);
272 # define portmux_setup(...) do { } while (0)
275 static void default_gpio(unsigned short gpio)
277 unsigned short bank, bitmask;
279 bank = gpio_bank(gpio);
280 bitmask = gpio_bit(gpio);
282 gpio_bankb[bank]->maska_clear = bitmask;
283 gpio_bankb[bank]->maskb_clear = bitmask;
285 gpio_bankb[bank]->inen &= ~bitmask;
286 gpio_bankb[bank]->dir &= ~bitmask;
287 gpio_bankb[bank]->polar &= ~bitmask;
288 gpio_bankb[bank]->both &= ~bitmask;
289 gpio_bankb[bank]->edge &= ~bitmask;
292 static int __init bfin_gpio_init(void)
294 str_ident = kcalloc(MAX_RESOURCES,
295 sizeof(struct str_ident), GFP_KERNEL);
296 if (str_ident == NULL)
299 memset(str_ident, 0, MAX_RESOURCES * sizeof(struct str_ident));
301 printk(KERN_INFO "Blackfin GPIO Controller\n");
306 arch_initcall(bfin_gpio_init);
309 /***********************************************************
311 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
314 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
317 * DESCRIPTION: These functions abstract direct register access
318 * to Blackfin processor General Purpose
321 * CAUTION: These functions do not belong to the GPIO Driver API
322 *************************************************************
323 * MODIFICATION HISTORY :
324 **************************************************************/
326 /* Set a specific bit */
328 #define SET_GPIO(name) \
329 void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \
331 unsigned long flags; \
332 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
333 local_irq_save(flags); \
335 gpio_bankb[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
337 gpio_bankb[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
338 local_irq_restore(flags); \
340 EXPORT_SYMBOL(set_gpio_ ## name);
349 #define SET_GPIO_SC(name) \
350 void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \
352 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
354 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
356 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
358 EXPORT_SYMBOL(set_gpio_ ## name);
364 void set_gpio_data(unsigned short gpio, unsigned short arg)
367 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
368 local_irq_save(flags);
370 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
372 gpio_bankb[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
374 local_irq_restore(flags);
376 EXPORT_SYMBOL(set_gpio_data);
383 void set_gpio_toggle(unsigned short gpio)
386 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
387 local_irq_save(flags);
388 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
390 local_irq_restore(flags);
393 void set_gpio_toggle(unsigned short gpio)
395 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
396 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
399 EXPORT_SYMBOL(set_gpio_toggle);
402 /*Set current PORT date (16-bit word)*/
404 #define SET_GPIO_P(name) \
405 void set_gpiop_ ## name(unsigned short gpio, unsigned short arg) \
407 gpio_bankb[gpio_bank(gpio)]->name = arg; \
409 EXPORT_SYMBOL(set_gpiop_ ## name);
421 void set_gpiop_data(unsigned short gpio, unsigned short arg)
424 local_irq_save(flags);
425 gpio_bankb[gpio_bank(gpio)]->data = arg;
427 local_irq_restore(flags);
429 EXPORT_SYMBOL(set_gpiop_data);
436 /* Get a specific bit */
438 #define GET_GPIO(name) \
439 unsigned short get_gpio_ ## name(unsigned short gpio) \
441 return (0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio))); \
443 EXPORT_SYMBOL(get_gpio_ ## name);
455 unsigned short get_gpio_data(unsigned short gpio)
459 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
460 local_irq_save(flags);
461 ret = 0x01 & (gpio_bankb[gpio_bank(gpio)]->data >> gpio_sub_n(gpio));
463 local_irq_restore(flags);
466 EXPORT_SYMBOL(get_gpio_data);
471 /*Get current PORT date (16-bit word)*/
473 #define GET_GPIO_P(name) \
474 unsigned short get_gpiop_ ## name(unsigned short gpio) \
476 return (gpio_bankb[gpio_bank(gpio)]->name);\
478 EXPORT_SYMBOL(get_gpiop_ ## name);
489 unsigned short get_gpiop_data(unsigned short gpio)
493 local_irq_save(flags);
494 ret = gpio_bankb[gpio_bank(gpio)]->data;
496 local_irq_restore(flags);
499 EXPORT_SYMBOL(get_gpiop_data);
505 /***********************************************************
507 * FUNCTIONS: Blackfin PM Setup API
510 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
518 * DESCRIPTION: Blackfin PM Driver API
521 *************************************************************
522 * MODIFICATION HISTORY :
523 **************************************************************/
524 int gpio_pm_wakeup_request(unsigned short gpio, unsigned char type)
528 if ((check_gpio(gpio) < 0) || !type)
531 local_irq_save(flags);
533 wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio);
534 wakeup_flags_map[gpio] = type;
535 local_irq_restore(flags);
539 EXPORT_SYMBOL(gpio_pm_wakeup_request);
541 void gpio_pm_wakeup_free(unsigned short gpio)
545 if (check_gpio(gpio) < 0)
548 local_irq_save(flags);
550 wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
552 local_irq_restore(flags);
554 EXPORT_SYMBOL(gpio_pm_wakeup_free);
556 static int bfin_gpio_wakeup_type(unsigned short gpio, unsigned char type)
558 port_setup(gpio, GPIO_USAGE);
559 set_gpio_dir(gpio, 0);
560 set_gpio_inen(gpio, 1);
562 if (type & (PM_WAKE_RISING | PM_WAKE_FALLING))
563 set_gpio_edge(gpio, 1);
565 set_gpio_edge(gpio, 0);
567 if ((type & (PM_WAKE_BOTH_EDGES)) == (PM_WAKE_BOTH_EDGES))
568 set_gpio_both(gpio, 1);
570 set_gpio_both(gpio, 0);
572 if ((type & (PM_WAKE_FALLING | PM_WAKE_LOW)))
573 set_gpio_polar(gpio, 1);
575 set_gpio_polar(gpio, 0);
582 u32 gpio_pm_setup(void)
585 u16 bank, mask, i, gpio;
587 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
588 mask = wakeup_map[gpio_bank(i)];
591 gpio_bank_saved[bank].maskb = gpio_bankb[bank]->maskb;
592 gpio_bankb[bank]->maskb = 0;
596 gpio_bank_saved[bank].fer = *port_fer[bank];
598 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
599 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
600 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
601 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
602 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
603 gpio_bank_saved[bank].reserved =
604 reserved_gpio_map[bank];
610 reserved_gpio_map[gpio_bank(gpio)] |=
612 bfin_gpio_wakeup_type(gpio,
613 wakeup_flags_map[gpio]);
614 set_gpio_data(gpio, 0); /*Clear*/
621 (sic_iwr_irqs[bank] - (IRQ_CORETMR + 1));
622 gpio_bankb[bank]->maskb_set = wakeup_map[gpio_bank(i)];
629 return IWR_ENABLE_ALL;
632 void gpio_pm_restore(void)
636 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
637 mask = wakeup_map[gpio_bank(i)];
642 *port_fer[bank] = gpio_bank_saved[bank].fer;
644 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
645 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
646 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
647 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
648 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
650 reserved_gpio_map[bank] =
651 gpio_bank_saved[bank].reserved;
655 gpio_bankb[bank]->maskb = gpio_bank_saved[bank].maskb;
664 int peripheral_request(unsigned short per, const char *label)
667 unsigned short ident = P_IDENT(per);
670 * Don't cares are pins with only one dedicated function
673 if (per & P_DONTCARE)
676 if (!(per & P_DEFINED))
679 local_irq_save(flags);
681 if (!check_gpio(ident)) {
683 if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
685 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
686 __FUNCTION__, ident, get_label(ident));
688 local_irq_restore(flags);
694 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
697 * Pin functions like AMC address strobes my
698 * be requested and used by several drivers
701 if (!(per & P_MAYSHARE)) {
704 * Allow that the identical pin function can
705 * be requested from the same driver twice
708 if (cmp_label(ident, label) == 0)
712 "%s: Peripheral %d function %d is already"
713 " reserved by %s !\n",
714 __FUNCTION__, ident, P_FUNCT2MUX(per),
717 local_irq_restore(flags);
726 portmux_setup(per, P_FUNCT2MUX(per));
728 port_setup(ident, PERIPHERAL_USAGE);
730 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
731 local_irq_restore(flags);
732 set_label(ident, label);
736 EXPORT_SYMBOL(peripheral_request);
738 int peripheral_request_list(unsigned short per[], const char *label)
743 for (cnt = 0; per[cnt] != 0; cnt++) {
745 ret = peripheral_request(per[cnt], label);
748 for ( ; cnt > 0; cnt--) {
749 peripheral_free(per[cnt - 1]);
757 EXPORT_SYMBOL(peripheral_request_list);
759 void peripheral_free(unsigned short per)
762 unsigned short ident = P_IDENT(per);
764 if (per & P_DONTCARE)
767 if (!(per & P_DEFINED))
770 if (check_gpio(ident) < 0)
773 local_irq_save(flags);
775 if (unlikely(!(reserved_peri_map[gpio_bank(ident)]
776 & gpio_bit(ident)))) {
777 local_irq_restore(flags);
781 if (!(per & P_MAYSHARE)) {
782 port_setup(ident, GPIO_USAGE);
785 reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
787 local_irq_restore(flags);
789 EXPORT_SYMBOL(peripheral_free);
791 void peripheral_free_list(unsigned short per[])
795 for (cnt = 0; per[cnt] != 0; cnt++) {
796 peripheral_free(per[cnt]);
800 EXPORT_SYMBOL(peripheral_free_list);
802 /***********************************************************
804 * FUNCTIONS: Blackfin GPIO Driver
807 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
810 * DESCRIPTION: Blackfin GPIO Driver API
813 *************************************************************
814 * MODIFICATION HISTORY :
815 **************************************************************/
817 int gpio_request(unsigned short gpio, const char *label)
821 if (check_gpio(gpio) < 0)
824 local_irq_save(flags);
826 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
827 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved!\n", gpio);
829 local_irq_restore(flags);
832 reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
834 local_irq_restore(flags);
836 port_setup(gpio, GPIO_USAGE);
840 EXPORT_SYMBOL(gpio_request);
842 void gpio_free(unsigned short gpio)
846 if (check_gpio(gpio) < 0)
849 local_irq_save(flags);
851 if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
852 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't reserved!\n", gpio);
854 local_irq_restore(flags);
860 reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
862 local_irq_restore(flags);
864 EXPORT_SYMBOL(gpio_free);
866 void gpio_direction_input(unsigned short gpio)
870 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
872 local_irq_save(flags);
873 gpio_bankb[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
874 gpio_bankb[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
875 local_irq_restore(flags);
877 EXPORT_SYMBOL(gpio_direction_input);
879 void gpio_direction_output(unsigned short gpio)
883 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
885 local_irq_save(flags);
886 gpio_bankb[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
887 gpio_bankb[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
888 local_irq_restore(flags);
890 EXPORT_SYMBOL(gpio_direction_output);