exit: move taskstats_tgid_free() from __exit_signal() to free_signal_struct()
[linux-2.6.git] / arch / blackfin / kernel / bfin_gpio.c
blob42833ee2b308ba546b323823a6bc2d37e94481ba
1 /*
2 * GPIO Abstraction Layer
4 * Copyright 2006-2009 Analog Devices Inc.
6 * Licensed under the GPL-2 or later
7 */
9 #include <linux/delay.h>
10 #include <linux/module.h>
11 #include <linux/err.h>
12 #include <linux/proc_fs.h>
13 #include <asm/blackfin.h>
14 #include <asm/gpio.h>
15 #include <asm/portmux.h>
16 #include <linux/irq.h>
18 #if ANOMALY_05000311 || ANOMALY_05000323
19 enum {
20 AWA_data = SYSCR,
21 AWA_data_clear = SYSCR,
22 AWA_data_set = SYSCR,
23 AWA_toggle = SYSCR,
24 AWA_maska = BFIN_UART_SCR,
25 AWA_maska_clear = BFIN_UART_SCR,
26 AWA_maska_set = BFIN_UART_SCR,
27 AWA_maska_toggle = BFIN_UART_SCR,
28 AWA_maskb = BFIN_UART_GCTL,
29 AWA_maskb_clear = BFIN_UART_GCTL,
30 AWA_maskb_set = BFIN_UART_GCTL,
31 AWA_maskb_toggle = BFIN_UART_GCTL,
32 AWA_dir = SPORT1_STAT,
33 AWA_polar = SPORT1_STAT,
34 AWA_edge = SPORT1_STAT,
35 AWA_both = SPORT1_STAT,
36 #if ANOMALY_05000311
37 AWA_inen = TIMER_ENABLE,
38 #elif ANOMALY_05000323
39 AWA_inen = DMA1_1_CONFIG,
40 #endif
42 /* Anomaly Workaround */
43 #define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
44 #else
45 #define AWA_DUMMY_READ(...) do { } while (0)
46 #endif
48 static struct gpio_port_t * const gpio_array[] = {
49 #if defined(BF533_FAMILY) || defined(BF538_FAMILY)
50 (struct gpio_port_t *) FIO_FLAG_D,
51 #elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
52 (struct gpio_port_t *) PORTFIO,
53 (struct gpio_port_t *) PORTGIO,
54 (struct gpio_port_t *) PORTHIO,
55 #elif defined(BF561_FAMILY)
56 (struct gpio_port_t *) FIO0_FLAG_D,
57 (struct gpio_port_t *) FIO1_FLAG_D,
58 (struct gpio_port_t *) FIO2_FLAG_D,
59 #elif defined(CONFIG_BF54x)
60 (struct gpio_port_t *)PORTA_FER,
61 (struct gpio_port_t *)PORTB_FER,
62 (struct gpio_port_t *)PORTC_FER,
63 (struct gpio_port_t *)PORTD_FER,
64 (struct gpio_port_t *)PORTE_FER,
65 (struct gpio_port_t *)PORTF_FER,
66 (struct gpio_port_t *)PORTG_FER,
67 (struct gpio_port_t *)PORTH_FER,
68 (struct gpio_port_t *)PORTI_FER,
69 (struct gpio_port_t *)PORTJ_FER,
70 #else
71 # error no gpio arrays defined
72 #endif
75 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
76 static unsigned short * const port_fer[] = {
77 (unsigned short *) PORTF_FER,
78 (unsigned short *) PORTG_FER,
79 (unsigned short *) PORTH_FER,
82 # if !defined(BF537_FAMILY)
83 static unsigned short * const port_mux[] = {
84 (unsigned short *) PORTF_MUX,
85 (unsigned short *) PORTG_MUX,
86 (unsigned short *) PORTH_MUX,
89 static const
90 u8 pmux_offset[][16] = {
91 # if defined(CONFIG_BF52x)
92 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
93 { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
94 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
95 # elif defined(CONFIG_BF51x)
96 { 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
97 { 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
98 { 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
99 # endif
101 # endif
103 #elif defined(BF538_FAMILY)
104 static unsigned short * const port_fer[] = {
105 (unsigned short *) PORTCIO_FER,
106 (unsigned short *) PORTDIO_FER,
107 (unsigned short *) PORTEIO_FER,
109 #endif
111 static unsigned short reserved_gpio_map[GPIO_BANK_NUM];
112 static unsigned short reserved_peri_map[gpio_bank(MAX_RESOURCES)];
113 static unsigned short reserved_gpio_irq_map[GPIO_BANK_NUM];
115 #define RESOURCE_LABEL_SIZE 16
117 static struct str_ident {
118 char name[RESOURCE_LABEL_SIZE];
119 } str_ident[MAX_RESOURCES];
121 #if defined(CONFIG_PM)
122 static struct gpio_port_s gpio_bank_saved[GPIO_BANK_NUM];
123 #endif
125 inline int check_gpio(unsigned gpio)
127 #if defined(CONFIG_BF54x)
128 if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15
129 || gpio == GPIO_PH14 || gpio == GPIO_PH15
130 || gpio == GPIO_PJ14 || gpio == GPIO_PJ15)
131 return -EINVAL;
132 #endif
133 if (gpio >= MAX_BLACKFIN_GPIOS)
134 return -EINVAL;
135 return 0;
138 static void gpio_error(unsigned gpio)
140 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
143 static void set_label(unsigned short ident, const char *label)
145 if (label) {
146 strncpy(str_ident[ident].name, label,
147 RESOURCE_LABEL_SIZE);
148 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
152 static char *get_label(unsigned short ident)
154 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
157 static int cmp_label(unsigned short ident, const char *label)
159 if (label == NULL) {
160 dump_stack();
161 printk(KERN_ERR "Please provide none-null label\n");
164 if (label)
165 return strcmp(str_ident[ident].name, label);
166 else
167 return -EINVAL;
170 static void port_setup(unsigned gpio, unsigned short usage)
172 #if defined(BF538_FAMILY)
174 * BF538/9 Port C,D and E are special.
175 * Inverted PORT_FER polarity on CDE and no PORF_FER on F
176 * Regular PORT F GPIOs are handled here, CDE are exclusively
177 * managed by GPIOLIB
180 if (gpio < MAX_BLACKFIN_GPIOS || gpio >= MAX_RESOURCES)
181 return;
183 gpio -= MAX_BLACKFIN_GPIOS;
185 if (usage == GPIO_USAGE)
186 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
187 else
188 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
189 SSYNC();
190 return;
191 #endif
193 if (check_gpio(gpio))
194 return;
196 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
197 if (usage == GPIO_USAGE)
198 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
199 else
200 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
201 SSYNC();
202 #elif defined(CONFIG_BF54x)
203 if (usage == GPIO_USAGE)
204 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
205 else
206 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
207 SSYNC();
208 #endif
211 #ifdef BF537_FAMILY
212 static struct {
213 unsigned short res;
214 unsigned short offset;
215 } port_mux_lut[] = {
216 {.res = P_PPI0_D13, .offset = 11},
217 {.res = P_PPI0_D14, .offset = 11},
218 {.res = P_PPI0_D15, .offset = 11},
219 {.res = P_SPORT1_TFS, .offset = 11},
220 {.res = P_SPORT1_TSCLK, .offset = 11},
221 {.res = P_SPORT1_DTPRI, .offset = 11},
222 {.res = P_PPI0_D10, .offset = 10},
223 {.res = P_PPI0_D11, .offset = 10},
224 {.res = P_PPI0_D12, .offset = 10},
225 {.res = P_SPORT1_RSCLK, .offset = 10},
226 {.res = P_SPORT1_RFS, .offset = 10},
227 {.res = P_SPORT1_DRPRI, .offset = 10},
228 {.res = P_PPI0_D8, .offset = 9},
229 {.res = P_PPI0_D9, .offset = 9},
230 {.res = P_SPORT1_DRSEC, .offset = 9},
231 {.res = P_SPORT1_DTSEC, .offset = 9},
232 {.res = P_TMR2, .offset = 8},
233 {.res = P_PPI0_FS3, .offset = 8},
234 {.res = P_TMR3, .offset = 7},
235 {.res = P_SPI0_SSEL4, .offset = 7},
236 {.res = P_TMR4, .offset = 6},
237 {.res = P_SPI0_SSEL5, .offset = 6},
238 {.res = P_TMR5, .offset = 5},
239 {.res = P_SPI0_SSEL6, .offset = 5},
240 {.res = P_UART1_RX, .offset = 4},
241 {.res = P_UART1_TX, .offset = 4},
242 {.res = P_TMR6, .offset = 4},
243 {.res = P_TMR7, .offset = 4},
244 {.res = P_UART0_RX, .offset = 3},
245 {.res = P_UART0_TX, .offset = 3},
246 {.res = P_DMAR0, .offset = 3},
247 {.res = P_DMAR1, .offset = 3},
248 {.res = P_SPORT0_DTSEC, .offset = 1},
249 {.res = P_SPORT0_DRSEC, .offset = 1},
250 {.res = P_CAN0_RX, .offset = 1},
251 {.res = P_CAN0_TX, .offset = 1},
252 {.res = P_SPI0_SSEL7, .offset = 1},
253 {.res = P_SPORT0_TFS, .offset = 0},
254 {.res = P_SPORT0_DTPRI, .offset = 0},
255 {.res = P_SPI0_SSEL2, .offset = 0},
256 {.res = P_SPI0_SSEL3, .offset = 0},
259 static void portmux_setup(unsigned short per)
261 u16 y, offset, muxreg;
262 u16 function = P_FUNCT2MUX(per);
264 for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
265 if (port_mux_lut[y].res == per) {
267 /* SET PORTMUX REG */
269 offset = port_mux_lut[y].offset;
270 muxreg = bfin_read_PORT_MUX();
272 if (offset != 1)
273 muxreg &= ~(1 << offset);
274 else
275 muxreg &= ~(3 << 1);
277 muxreg |= (function << offset);
278 bfin_write_PORT_MUX(muxreg);
282 #elif defined(CONFIG_BF54x)
283 inline void portmux_setup(unsigned short per)
285 u32 pmux;
286 u16 ident = P_IDENT(per);
287 u16 function = P_FUNCT2MUX(per);
289 pmux = gpio_array[gpio_bank(ident)]->port_mux;
291 pmux &= ~(0x3 << (2 * gpio_sub_n(ident)));
292 pmux |= (function & 0x3) << (2 * gpio_sub_n(ident));
294 gpio_array[gpio_bank(ident)]->port_mux = pmux;
297 inline u16 get_portmux(unsigned short per)
299 u32 pmux;
300 u16 ident = P_IDENT(per);
302 pmux = gpio_array[gpio_bank(ident)]->port_mux;
304 return (pmux >> (2 * gpio_sub_n(ident)) & 0x3);
306 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
307 inline void portmux_setup(unsigned short per)
309 u16 pmux, ident = P_IDENT(per), function = P_FUNCT2MUX(per);
310 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
312 pmux = *port_mux[gpio_bank(ident)];
313 pmux &= ~(3 << offset);
314 pmux |= (function & 3) << offset;
315 *port_mux[gpio_bank(ident)] = pmux;
316 SSYNC();
318 #else
319 # define portmux_setup(...) do { } while (0)
320 #endif
322 #ifndef CONFIG_BF54x
323 /***********************************************************
325 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
327 * INPUTS/OUTPUTS:
328 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
331 * DESCRIPTION: These functions abstract direct register access
332 * to Blackfin processor General Purpose
333 * Ports Regsiters
335 * CAUTION: These functions do not belong to the GPIO Driver API
336 *************************************************************
337 * MODIFICATION HISTORY :
338 **************************************************************/
340 /* Set a specific bit */
342 #define SET_GPIO(name) \
343 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
345 unsigned long flags; \
346 local_irq_save_hw(flags); \
347 if (arg) \
348 gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
349 else \
350 gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
351 AWA_DUMMY_READ(name); \
352 local_irq_restore_hw(flags); \
354 EXPORT_SYMBOL(set_gpio_ ## name);
356 SET_GPIO(dir) /* set_gpio_dir() */
357 SET_GPIO(inen) /* set_gpio_inen() */
358 SET_GPIO(polar) /* set_gpio_polar() */
359 SET_GPIO(edge) /* set_gpio_edge() */
360 SET_GPIO(both) /* set_gpio_both() */
363 #define SET_GPIO_SC(name) \
364 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
366 unsigned long flags; \
367 if (ANOMALY_05000311 || ANOMALY_05000323) \
368 local_irq_save_hw(flags); \
369 if (arg) \
370 gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
371 else \
372 gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
373 if (ANOMALY_05000311 || ANOMALY_05000323) { \
374 AWA_DUMMY_READ(name); \
375 local_irq_restore_hw(flags); \
378 EXPORT_SYMBOL(set_gpio_ ## name);
380 SET_GPIO_SC(maska)
381 SET_GPIO_SC(maskb)
382 SET_GPIO_SC(data)
384 void set_gpio_toggle(unsigned gpio)
386 unsigned long flags;
387 if (ANOMALY_05000311 || ANOMALY_05000323)
388 local_irq_save_hw(flags);
389 gpio_array[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
390 if (ANOMALY_05000311 || ANOMALY_05000323) {
391 AWA_DUMMY_READ(toggle);
392 local_irq_restore_hw(flags);
395 EXPORT_SYMBOL(set_gpio_toggle);
398 /*Set current PORT date (16-bit word)*/
400 #define SET_GPIO_P(name) \
401 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
403 unsigned long flags; \
404 if (ANOMALY_05000311 || ANOMALY_05000323) \
405 local_irq_save_hw(flags); \
406 gpio_array[gpio_bank(gpio)]->name = arg; \
407 if (ANOMALY_05000311 || ANOMALY_05000323) { \
408 AWA_DUMMY_READ(name); \
409 local_irq_restore_hw(flags); \
412 EXPORT_SYMBOL(set_gpiop_ ## name);
414 SET_GPIO_P(data)
415 SET_GPIO_P(dir)
416 SET_GPIO_P(inen)
417 SET_GPIO_P(polar)
418 SET_GPIO_P(edge)
419 SET_GPIO_P(both)
420 SET_GPIO_P(maska)
421 SET_GPIO_P(maskb)
423 /* Get a specific bit */
424 #define GET_GPIO(name) \
425 unsigned short get_gpio_ ## name(unsigned gpio) \
427 unsigned long flags; \
428 unsigned short ret; \
429 if (ANOMALY_05000311 || ANOMALY_05000323) \
430 local_irq_save_hw(flags); \
431 ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
432 if (ANOMALY_05000311 || ANOMALY_05000323) { \
433 AWA_DUMMY_READ(name); \
434 local_irq_restore_hw(flags); \
436 return ret; \
438 EXPORT_SYMBOL(get_gpio_ ## name);
440 GET_GPIO(data)
441 GET_GPIO(dir)
442 GET_GPIO(inen)
443 GET_GPIO(polar)
444 GET_GPIO(edge)
445 GET_GPIO(both)
446 GET_GPIO(maska)
447 GET_GPIO(maskb)
449 /*Get current PORT date (16-bit word)*/
451 #define GET_GPIO_P(name) \
452 unsigned short get_gpiop_ ## name(unsigned gpio) \
454 unsigned long flags; \
455 unsigned short ret; \
456 if (ANOMALY_05000311 || ANOMALY_05000323) \
457 local_irq_save_hw(flags); \
458 ret = (gpio_array[gpio_bank(gpio)]->name); \
459 if (ANOMALY_05000311 || ANOMALY_05000323) { \
460 AWA_DUMMY_READ(name); \
461 local_irq_restore_hw(flags); \
463 return ret; \
465 EXPORT_SYMBOL(get_gpiop_ ## name);
467 GET_GPIO_P(data)
468 GET_GPIO_P(dir)
469 GET_GPIO_P(inen)
470 GET_GPIO_P(polar)
471 GET_GPIO_P(edge)
472 GET_GPIO_P(both)
473 GET_GPIO_P(maska)
474 GET_GPIO_P(maskb)
477 #ifdef CONFIG_PM
478 static unsigned short wakeup_map[GPIO_BANK_NUM];
480 static const unsigned int sic_iwr_irqs[] = {
481 #if defined(BF533_FAMILY)
482 IRQ_PROG_INTB
483 #elif defined(BF537_FAMILY)
484 IRQ_PROG_INTB, IRQ_PORTG_INTB, IRQ_MAC_TX
485 #elif defined(BF538_FAMILY)
486 IRQ_PORTF_INTB
487 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
488 IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB
489 #elif defined(BF561_FAMILY)
490 IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB
491 #else
492 # error no SIC_IWR defined
493 #endif
496 /***********************************************************
498 * FUNCTIONS: Blackfin PM Setup API
500 * INPUTS/OUTPUTS:
501 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
502 * type -
503 * PM_WAKE_RISING
504 * PM_WAKE_FALLING
505 * PM_WAKE_HIGH
506 * PM_WAKE_LOW
507 * PM_WAKE_BOTH_EDGES
509 * DESCRIPTION: Blackfin PM Driver API
511 * CAUTION:
512 *************************************************************
513 * MODIFICATION HISTORY :
514 **************************************************************/
515 int gpio_pm_wakeup_ctrl(unsigned gpio, unsigned ctrl)
517 unsigned long flags;
519 if (check_gpio(gpio) < 0)
520 return -EINVAL;
522 local_irq_save_hw(flags);
523 if (ctrl)
524 wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio);
525 else
526 wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
528 set_gpio_maskb(gpio, ctrl);
529 local_irq_restore_hw(flags);
531 return 0;
534 int bfin_pm_standby_ctrl(unsigned ctrl)
536 u16 bank, mask, i;
538 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
539 mask = wakeup_map[gpio_bank(i)];
540 bank = gpio_bank(i);
542 if (mask)
543 bfin_internal_set_wake(sic_iwr_irqs[bank], ctrl);
545 return 0;
548 void bfin_gpio_pm_hibernate_suspend(void)
550 int i, bank;
552 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
553 bank = gpio_bank(i);
555 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
556 gpio_bank_saved[bank].fer = *port_fer[bank];
557 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
558 gpio_bank_saved[bank].mux = *port_mux[bank];
559 #else
560 if (bank == 0)
561 gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
562 #endif
563 #endif
564 gpio_bank_saved[bank].data = gpio_array[bank]->data;
565 gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
566 gpio_bank_saved[bank].polar = gpio_array[bank]->polar;
567 gpio_bank_saved[bank].dir = gpio_array[bank]->dir;
568 gpio_bank_saved[bank].edge = gpio_array[bank]->edge;
569 gpio_bank_saved[bank].both = gpio_array[bank]->both;
570 gpio_bank_saved[bank].maska = gpio_array[bank]->maska;
573 AWA_DUMMY_READ(maska);
576 void bfin_gpio_pm_hibernate_restore(void)
578 int i, bank;
580 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
581 bank = gpio_bank(i);
583 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
584 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
585 *port_mux[bank] = gpio_bank_saved[bank].mux;
586 #else
587 if (bank == 0)
588 bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
589 #endif
590 *port_fer[bank] = gpio_bank_saved[bank].fer;
591 #endif
592 gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
593 gpio_array[bank]->data_set = gpio_bank_saved[bank].data
594 & gpio_bank_saved[bank].dir;
595 gpio_array[bank]->dir = gpio_bank_saved[bank].dir;
596 gpio_array[bank]->polar = gpio_bank_saved[bank].polar;
597 gpio_array[bank]->edge = gpio_bank_saved[bank].edge;
598 gpio_array[bank]->both = gpio_bank_saved[bank].both;
599 gpio_array[bank]->maska = gpio_bank_saved[bank].maska;
601 AWA_DUMMY_READ(maska);
605 #endif
606 #else /* CONFIG_BF54x */
607 #ifdef CONFIG_PM
609 int bfin_pm_standby_ctrl(unsigned ctrl)
611 return 0;
614 void bfin_gpio_pm_hibernate_suspend(void)
616 int i, bank;
618 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
619 bank = gpio_bank(i);
621 gpio_bank_saved[bank].fer = gpio_array[bank]->port_fer;
622 gpio_bank_saved[bank].mux = gpio_array[bank]->port_mux;
623 gpio_bank_saved[bank].data = gpio_array[bank]->data;
624 gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
625 gpio_bank_saved[bank].dir = gpio_array[bank]->dir_set;
629 void bfin_gpio_pm_hibernate_restore(void)
631 int i, bank;
633 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
634 bank = gpio_bank(i);
636 gpio_array[bank]->port_mux = gpio_bank_saved[bank].mux;
637 gpio_array[bank]->port_fer = gpio_bank_saved[bank].fer;
638 gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
639 gpio_array[bank]->dir_set = gpio_bank_saved[bank].dir;
640 gpio_array[bank]->data_set = gpio_bank_saved[bank].data
641 | gpio_bank_saved[bank].dir;
644 #endif
646 unsigned short get_gpio_dir(unsigned gpio)
648 return (0x01 & (gpio_array[gpio_bank(gpio)]->dir_clear >> gpio_sub_n(gpio)));
650 EXPORT_SYMBOL(get_gpio_dir);
652 #endif /* CONFIG_BF54x */
654 /***********************************************************
656 * FUNCTIONS: Blackfin Peripheral Resource Allocation
657 * and PortMux Setup
659 * INPUTS/OUTPUTS:
660 * per Peripheral Identifier
661 * label String
663 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
665 * CAUTION:
666 *************************************************************
667 * MODIFICATION HISTORY :
668 **************************************************************/
670 int peripheral_request(unsigned short per, const char *label)
672 unsigned long flags;
673 unsigned short ident = P_IDENT(per);
676 * Don't cares are pins with only one dedicated function
679 if (per & P_DONTCARE)
680 return 0;
682 if (!(per & P_DEFINED))
683 return -ENODEV;
685 BUG_ON(ident >= MAX_RESOURCES);
687 local_irq_save_hw(flags);
689 /* If a pin can be muxed as either GPIO or peripheral, make
690 * sure it is not already a GPIO pin when we request it.
692 if (unlikely(!check_gpio(ident) &&
693 reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
694 if (system_state == SYSTEM_BOOTING)
695 dump_stack();
696 printk(KERN_ERR
697 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
698 __func__, ident, get_label(ident));
699 local_irq_restore_hw(flags);
700 return -EBUSY;
703 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
706 * Pin functions like AMC address strobes my
707 * be requested and used by several drivers
710 #ifdef CONFIG_BF54x
711 if (!((per & P_MAYSHARE) && get_portmux(per) == P_FUNCT2MUX(per))) {
712 #else
713 if (!(per & P_MAYSHARE)) {
714 #endif
716 * Allow that the identical pin function can
717 * be requested from the same driver twice
720 if (cmp_label(ident, label) == 0)
721 goto anyway;
723 if (system_state == SYSTEM_BOOTING)
724 dump_stack();
725 printk(KERN_ERR
726 "%s: Peripheral %d function %d is already reserved by %s !\n",
727 __func__, ident, P_FUNCT2MUX(per), get_label(ident));
728 local_irq_restore_hw(flags);
729 return -EBUSY;
733 anyway:
734 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
736 portmux_setup(per);
737 port_setup(ident, PERIPHERAL_USAGE);
739 local_irq_restore_hw(flags);
740 set_label(ident, label);
742 return 0;
744 EXPORT_SYMBOL(peripheral_request);
746 int peripheral_request_list(const unsigned short per[], const char *label)
748 u16 cnt;
749 int ret;
751 for (cnt = 0; per[cnt] != 0; cnt++) {
753 ret = peripheral_request(per[cnt], label);
755 if (ret < 0) {
756 for ( ; cnt > 0; cnt--)
757 peripheral_free(per[cnt - 1]);
759 return ret;
763 return 0;
765 EXPORT_SYMBOL(peripheral_request_list);
767 void peripheral_free(unsigned short per)
769 unsigned long flags;
770 unsigned short ident = P_IDENT(per);
772 if (per & P_DONTCARE)
773 return;
775 if (!(per & P_DEFINED))
776 return;
778 local_irq_save_hw(flags);
780 if (unlikely(!(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident)))) {
781 local_irq_restore_hw(flags);
782 return;
785 if (!(per & P_MAYSHARE))
786 port_setup(ident, GPIO_USAGE);
788 reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
790 set_label(ident, "free");
792 local_irq_restore_hw(flags);
794 EXPORT_SYMBOL(peripheral_free);
796 void peripheral_free_list(const unsigned short per[])
798 u16 cnt;
799 for (cnt = 0; per[cnt] != 0; cnt++)
800 peripheral_free(per[cnt]);
802 EXPORT_SYMBOL(peripheral_free_list);
804 /***********************************************************
806 * FUNCTIONS: Blackfin GPIO Driver
808 * INPUTS/OUTPUTS:
809 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
810 * label String
812 * DESCRIPTION: Blackfin GPIO Driver API
814 * CAUTION:
815 *************************************************************
816 * MODIFICATION HISTORY :
817 **************************************************************/
819 int bfin_gpio_request(unsigned gpio, const char *label)
821 unsigned long flags;
823 if (check_gpio(gpio) < 0)
824 return -EINVAL;
826 local_irq_save_hw(flags);
829 * Allow that the identical GPIO can
830 * be requested from the same driver twice
831 * Do nothing and return -
834 if (cmp_label(gpio, label) == 0) {
835 local_irq_restore_hw(flags);
836 return 0;
839 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
840 if (system_state == SYSTEM_BOOTING)
841 dump_stack();
842 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
843 gpio, get_label(gpio));
844 local_irq_restore_hw(flags);
845 return -EBUSY;
847 if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
848 if (system_state == SYSTEM_BOOTING)
849 dump_stack();
850 printk(KERN_ERR
851 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
852 gpio, get_label(gpio));
853 local_irq_restore_hw(flags);
854 return -EBUSY;
856 if (unlikely(reserved_gpio_irq_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
857 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved as gpio-irq!"
858 " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio);
860 #ifndef CONFIG_BF54x
861 else { /* Reset POLAR setting when acquiring a gpio for the first time */
862 set_gpio_polar(gpio, 0);
864 #endif
866 reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
867 set_label(gpio, label);
869 local_irq_restore_hw(flags);
871 port_setup(gpio, GPIO_USAGE);
873 return 0;
875 EXPORT_SYMBOL(bfin_gpio_request);
877 void bfin_gpio_free(unsigned gpio)
879 unsigned long flags;
881 if (check_gpio(gpio) < 0)
882 return;
884 might_sleep();
886 local_irq_save_hw(flags);
888 if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
889 if (system_state == SYSTEM_BOOTING)
890 dump_stack();
891 gpio_error(gpio);
892 local_irq_restore_hw(flags);
893 return;
896 reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
898 set_label(gpio, "free");
900 local_irq_restore_hw(flags);
902 EXPORT_SYMBOL(bfin_gpio_free);
904 #ifdef BFIN_SPECIAL_GPIO_BANKS
905 static unsigned short reserved_special_gpio_map[gpio_bank(MAX_RESOURCES)];
907 int bfin_special_gpio_request(unsigned gpio, const char *label)
909 unsigned long flags;
911 local_irq_save_hw(flags);
914 * Allow that the identical GPIO can
915 * be requested from the same driver twice
916 * Do nothing and return -
919 if (cmp_label(gpio, label) == 0) {
920 local_irq_restore_hw(flags);
921 return 0;
924 if (unlikely(reserved_special_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
925 local_irq_restore_hw(flags);
926 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
927 gpio, get_label(gpio));
929 return -EBUSY;
931 if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
932 local_irq_restore_hw(flags);
933 printk(KERN_ERR
934 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
935 gpio, get_label(gpio));
937 return -EBUSY;
940 reserved_special_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
941 reserved_peri_map[gpio_bank(gpio)] |= gpio_bit(gpio);
943 set_label(gpio, label);
944 local_irq_restore_hw(flags);
945 port_setup(gpio, GPIO_USAGE);
947 return 0;
949 EXPORT_SYMBOL(bfin_special_gpio_request);
951 void bfin_special_gpio_free(unsigned gpio)
953 unsigned long flags;
955 might_sleep();
957 local_irq_save_hw(flags);
959 if (unlikely(!(reserved_special_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
960 gpio_error(gpio);
961 local_irq_restore_hw(flags);
962 return;
965 reserved_special_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
966 reserved_peri_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
967 set_label(gpio, "free");
968 local_irq_restore_hw(flags);
970 EXPORT_SYMBOL(bfin_special_gpio_free);
971 #endif
974 int bfin_gpio_irq_request(unsigned gpio, const char *label)
976 unsigned long flags;
978 if (check_gpio(gpio) < 0)
979 return -EINVAL;
981 local_irq_save_hw(flags);
983 if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
984 if (system_state == SYSTEM_BOOTING)
985 dump_stack();
986 printk(KERN_ERR
987 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
988 gpio, get_label(gpio));
989 local_irq_restore_hw(flags);
990 return -EBUSY;
992 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))
993 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved by %s! "
994 "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
995 gpio, get_label(gpio));
997 reserved_gpio_irq_map[gpio_bank(gpio)] |= gpio_bit(gpio);
998 set_label(gpio, label);
1000 local_irq_restore_hw(flags);
1002 port_setup(gpio, GPIO_USAGE);
1004 return 0;
1007 void bfin_gpio_irq_free(unsigned gpio)
1009 unsigned long flags;
1011 if (check_gpio(gpio) < 0)
1012 return;
1014 local_irq_save_hw(flags);
1016 if (unlikely(!(reserved_gpio_irq_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
1017 if (system_state == SYSTEM_BOOTING)
1018 dump_stack();
1019 gpio_error(gpio);
1020 local_irq_restore_hw(flags);
1021 return;
1024 reserved_gpio_irq_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
1026 set_label(gpio, "free");
1028 local_irq_restore_hw(flags);
1031 static inline void __bfin_gpio_direction_input(unsigned gpio)
1033 #ifdef CONFIG_BF54x
1034 gpio_array[gpio_bank(gpio)]->dir_clear = gpio_bit(gpio);
1035 #else
1036 gpio_array[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1037 #endif
1038 gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
1041 int bfin_gpio_direction_input(unsigned gpio)
1043 unsigned long flags;
1045 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1046 gpio_error(gpio);
1047 return -EINVAL;
1050 local_irq_save_hw(flags);
1051 __bfin_gpio_direction_input(gpio);
1052 AWA_DUMMY_READ(inen);
1053 local_irq_restore_hw(flags);
1055 return 0;
1057 EXPORT_SYMBOL(bfin_gpio_direction_input);
1059 void bfin_gpio_irq_prepare(unsigned gpio)
1061 #ifdef CONFIG_BF54x
1062 unsigned long flags;
1063 #endif
1065 port_setup(gpio, GPIO_USAGE);
1067 #ifdef CONFIG_BF54x
1068 local_irq_save_hw(flags);
1069 __bfin_gpio_direction_input(gpio);
1070 local_irq_restore_hw(flags);
1071 #endif
1074 void bfin_gpio_set_value(unsigned gpio, int arg)
1076 if (arg)
1077 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1078 else
1079 gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1081 EXPORT_SYMBOL(bfin_gpio_set_value);
1083 int bfin_gpio_direction_output(unsigned gpio, int value)
1085 unsigned long flags;
1087 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1088 gpio_error(gpio);
1089 return -EINVAL;
1092 local_irq_save_hw(flags);
1094 gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
1095 gpio_set_value(gpio, value);
1096 #ifdef CONFIG_BF54x
1097 gpio_array[gpio_bank(gpio)]->dir_set = gpio_bit(gpio);
1098 #else
1099 gpio_array[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
1100 #endif
1102 AWA_DUMMY_READ(dir);
1103 local_irq_restore_hw(flags);
1105 return 0;
1107 EXPORT_SYMBOL(bfin_gpio_direction_output);
1109 int bfin_gpio_get_value(unsigned gpio)
1111 #ifdef CONFIG_BF54x
1112 return (1 & (gpio_array[gpio_bank(gpio)]->data >> gpio_sub_n(gpio)));
1113 #else
1114 unsigned long flags;
1116 if (unlikely(get_gpio_edge(gpio))) {
1117 int ret;
1118 local_irq_save_hw(flags);
1119 set_gpio_edge(gpio, 0);
1120 ret = get_gpio_data(gpio);
1121 set_gpio_edge(gpio, 1);
1122 local_irq_restore_hw(flags);
1123 return ret;
1124 } else
1125 return get_gpio_data(gpio);
1126 #endif
1128 EXPORT_SYMBOL(bfin_gpio_get_value);
1130 /* If we are booting from SPI and our board lacks a strong enough pull up,
1131 * the core can reset and execute the bootrom faster than the resistor can
1132 * pull the signal logically high. To work around this (common) error in
1133 * board design, we explicitly set the pin back to GPIO mode, force /CS
1134 * high, and wait for the electrons to do their thing.
1136 * This function only makes sense to be called from reset code, but it
1137 * lives here as we need to force all the GPIO states w/out going through
1138 * BUG() checks and such.
1140 void bfin_reset_boot_spi_cs(unsigned short pin)
1142 unsigned short gpio = P_IDENT(pin);
1143 port_setup(gpio, GPIO_USAGE);
1144 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1145 AWA_DUMMY_READ(data_set);
1146 udelay(1);
1149 #if defined(CONFIG_PROC_FS)
1150 static int gpio_proc_read(char *buf, char **start, off_t offset,
1151 int len, int *unused_i, void *unused_v)
1153 int c, irq, gpio, outlen = 0;
1155 for (c = 0; c < MAX_RESOURCES; c++) {
1156 irq = reserved_gpio_irq_map[gpio_bank(c)] & gpio_bit(c);
1157 gpio = reserved_gpio_map[gpio_bank(c)] & gpio_bit(c);
1158 if (!check_gpio(c) && (gpio || irq))
1159 len = sprintf(buf, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c,
1160 get_label(c), (gpio && irq) ? " *" : "",
1161 get_gpio_dir(c) ? "OUTPUT" : "INPUT");
1162 else if (reserved_peri_map[gpio_bank(c)] & gpio_bit(c))
1163 len = sprintf(buf, "GPIO_%d: \t%s \t\tPeripheral\n", c, get_label(c));
1164 else
1165 continue;
1166 buf += len;
1167 outlen += len;
1169 return outlen;
1172 static __init int gpio_register_proc(void)
1174 struct proc_dir_entry *proc_gpio;
1176 proc_gpio = create_proc_entry("gpio", S_IRUGO, NULL);
1177 if (proc_gpio)
1178 proc_gpio->read_proc = gpio_proc_read;
1179 return proc_gpio != NULL;
1181 __initcall(gpio_register_proc);
1182 #endif
1184 #ifdef CONFIG_GPIOLIB
1185 static int bfin_gpiolib_direction_input(struct gpio_chip *chip, unsigned gpio)
1187 return bfin_gpio_direction_input(gpio);
1190 static int bfin_gpiolib_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
1192 return bfin_gpio_direction_output(gpio, level);
1195 static int bfin_gpiolib_get_value(struct gpio_chip *chip, unsigned gpio)
1197 return bfin_gpio_get_value(gpio);
1200 static void bfin_gpiolib_set_value(struct gpio_chip *chip, unsigned gpio, int value)
1202 return bfin_gpio_set_value(gpio, value);
1205 static int bfin_gpiolib_gpio_request(struct gpio_chip *chip, unsigned gpio)
1207 return bfin_gpio_request(gpio, chip->label);
1210 static void bfin_gpiolib_gpio_free(struct gpio_chip *chip, unsigned gpio)
1212 return bfin_gpio_free(gpio);
1215 static int bfin_gpiolib_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
1217 return gpio + GPIO_IRQ_BASE;
1220 static struct gpio_chip bfin_chip = {
1221 .label = "BFIN-GPIO",
1222 .direction_input = bfin_gpiolib_direction_input,
1223 .get = bfin_gpiolib_get_value,
1224 .direction_output = bfin_gpiolib_direction_output,
1225 .set = bfin_gpiolib_set_value,
1226 .request = bfin_gpiolib_gpio_request,
1227 .free = bfin_gpiolib_gpio_free,
1228 .to_irq = bfin_gpiolib_gpio_to_irq,
1229 .base = 0,
1230 .ngpio = MAX_BLACKFIN_GPIOS,
1233 static int __init bfin_gpiolib_setup(void)
1235 return gpiochip_add(&bfin_chip);
1237 arch_initcall(bfin_gpiolib_setup);
1238 #endif