fix a kmap leak in virtio_console
[linux/fpc-iii.git] / arch / blackfin / kernel / bfin_gpio.c
bloba017359c182668f71583ce013ae92263b95c69e6
1 /*
2 * GPIO Abstraction Layer
4 * Copyright 2006-2010 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 <linux/seq_file.h>
14 #include <linux/gpio.h>
15 #include <linux/irq.h>
17 #if ANOMALY_05000311 || ANOMALY_05000323
18 enum {
19 AWA_data = SYSCR,
20 AWA_data_clear = SYSCR,
21 AWA_data_set = SYSCR,
22 AWA_toggle = SYSCR,
23 AWA_maska = BFIN_UART_SCR,
24 AWA_maska_clear = BFIN_UART_SCR,
25 AWA_maska_set = BFIN_UART_SCR,
26 AWA_maska_toggle = BFIN_UART_SCR,
27 AWA_maskb = BFIN_UART_GCTL,
28 AWA_maskb_clear = BFIN_UART_GCTL,
29 AWA_maskb_set = BFIN_UART_GCTL,
30 AWA_maskb_toggle = BFIN_UART_GCTL,
31 AWA_dir = SPORT1_STAT,
32 AWA_polar = SPORT1_STAT,
33 AWA_edge = SPORT1_STAT,
34 AWA_both = SPORT1_STAT,
35 #if ANOMALY_05000311
36 AWA_inen = TIMER_ENABLE,
37 #elif ANOMALY_05000323
38 AWA_inen = DMA1_1_CONFIG,
39 #endif
41 /* Anomaly Workaround */
42 #define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
43 #else
44 #define AWA_DUMMY_READ(...) do { } while (0)
45 #endif
47 static struct gpio_port_t * const gpio_array[] = {
48 #if defined(BF533_FAMILY) || defined(BF538_FAMILY)
49 (struct gpio_port_t *) FIO_FLAG_D,
50 #elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
51 (struct gpio_port_t *) PORTFIO,
52 (struct gpio_port_t *) PORTGIO,
53 (struct gpio_port_t *) PORTHIO,
54 #elif defined(BF561_FAMILY)
55 (struct gpio_port_t *) FIO0_FLAG_D,
56 (struct gpio_port_t *) FIO1_FLAG_D,
57 (struct gpio_port_t *) FIO2_FLAG_D,
58 #else
59 # error no gpio arrays defined
60 #endif
63 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
64 static unsigned short * const port_fer[] = {
65 (unsigned short *) PORTF_FER,
66 (unsigned short *) PORTG_FER,
67 (unsigned short *) PORTH_FER,
70 # if !defined(BF537_FAMILY)
71 static unsigned short * const port_mux[] = {
72 (unsigned short *) PORTF_MUX,
73 (unsigned short *) PORTG_MUX,
74 (unsigned short *) PORTH_MUX,
77 static const
78 u8 pmux_offset[][16] = {
79 # if defined(CONFIG_BF52x)
80 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
81 { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
82 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
83 # elif defined(CONFIG_BF51x)
84 { 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
85 { 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
86 { 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
87 # endif
89 # endif
91 #elif defined(BF538_FAMILY)
92 static unsigned short * const port_fer[] = {
93 (unsigned short *) PORTCIO_FER,
94 (unsigned short *) PORTDIO_FER,
95 (unsigned short *) PORTEIO_FER,
97 #endif
99 #define RESOURCE_LABEL_SIZE 16
101 static struct str_ident {
102 char name[RESOURCE_LABEL_SIZE];
103 } str_ident[MAX_RESOURCES];
105 #if defined(CONFIG_PM)
106 static struct gpio_port_s gpio_bank_saved[GPIO_BANK_NUM];
107 # ifdef BF538_FAMILY
108 static unsigned short port_fer_saved[3];
109 # endif
110 #endif
112 static void gpio_error(unsigned gpio)
114 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
117 static void set_label(unsigned short ident, const char *label)
119 if (label) {
120 strncpy(str_ident[ident].name, label,
121 RESOURCE_LABEL_SIZE);
122 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
126 static char *get_label(unsigned short ident)
128 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
131 static int cmp_label(unsigned short ident, const char *label)
133 if (label == NULL) {
134 dump_stack();
135 printk(KERN_ERR "Please provide none-null label\n");
138 if (label)
139 return strcmp(str_ident[ident].name, label);
140 else
141 return -EINVAL;
144 #define map_entry(m, i) reserved_##m##_map[gpio_bank(i)]
145 #define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
146 #define reserve(m, i) (map_entry(m, i) |= gpio_bit(i))
147 #define unreserve(m, i) (map_entry(m, i) &= ~gpio_bit(i))
148 #define DECLARE_RESERVED_MAP(m, c) static unsigned short reserved_##m##_map[c]
150 DECLARE_RESERVED_MAP(gpio, GPIO_BANK_NUM);
151 DECLARE_RESERVED_MAP(peri, DIV_ROUND_UP(MAX_RESOURCES, GPIO_BANKSIZE));
152 DECLARE_RESERVED_MAP(gpio_irq, GPIO_BANK_NUM);
154 inline int check_gpio(unsigned gpio)
156 if (gpio >= MAX_BLACKFIN_GPIOS)
157 return -EINVAL;
158 return 0;
161 static void port_setup(unsigned gpio, unsigned short usage)
163 #if defined(BF538_FAMILY)
165 * BF538/9 Port C,D and E are special.
166 * Inverted PORT_FER polarity on CDE and no PORF_FER on F
167 * Regular PORT F GPIOs are handled here, CDE are exclusively
168 * managed by GPIOLIB
171 if (gpio < MAX_BLACKFIN_GPIOS || gpio >= MAX_RESOURCES)
172 return;
174 gpio -= MAX_BLACKFIN_GPIOS;
176 if (usage == GPIO_USAGE)
177 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
178 else
179 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
180 SSYNC();
181 return;
182 #endif
184 if (check_gpio(gpio))
185 return;
187 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
188 if (usage == GPIO_USAGE)
189 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
190 else
191 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
192 SSYNC();
193 #endif
196 #ifdef BF537_FAMILY
197 static const s8 port_mux[] = {
198 [GPIO_PF0] = 3,
199 [GPIO_PF1] = 3,
200 [GPIO_PF2] = 4,
201 [GPIO_PF3] = 4,
202 [GPIO_PF4] = 5,
203 [GPIO_PF5] = 6,
204 [GPIO_PF6] = 7,
205 [GPIO_PF7] = 8,
206 [GPIO_PF8 ... GPIO_PF15] = -1,
207 [GPIO_PG0 ... GPIO_PG7] = -1,
208 [GPIO_PG8] = 9,
209 [GPIO_PG9] = 9,
210 [GPIO_PG10] = 10,
211 [GPIO_PG11] = 10,
212 [GPIO_PG12] = 10,
213 [GPIO_PG13] = 11,
214 [GPIO_PG14] = 11,
215 [GPIO_PG15] = 11,
216 [GPIO_PH0 ... GPIO_PH15] = -1,
217 [PORT_PJ0 ... PORT_PJ3] = -1,
218 [PORT_PJ4] = 1,
219 [PORT_PJ5] = 1,
220 [PORT_PJ6 ... PORT_PJ9] = -1,
221 [PORT_PJ10] = 0,
222 [PORT_PJ11] = 0,
225 static int portmux_group_check(unsigned short per)
227 u16 ident = P_IDENT(per);
228 u16 function = P_FUNCT2MUX(per);
229 s8 offset = port_mux[ident];
230 u16 m, pmux, pfunc, mask;
232 if (offset < 0)
233 return 0;
235 pmux = bfin_read_PORT_MUX();
236 for (m = 0; m < ARRAY_SIZE(port_mux); ++m) {
237 if (m == ident)
238 continue;
239 if (port_mux[m] != offset)
240 continue;
241 if (!is_reserved(peri, m, 1))
242 continue;
244 if (offset == 1)
245 mask = 3;
246 else
247 mask = 1;
249 pfunc = (pmux >> offset) & mask;
250 if (pfunc != (function & mask)) {
251 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
252 ident, function, m, pfunc);
253 return -EINVAL;
257 return 0;
260 static void portmux_setup(unsigned short per)
262 u16 ident = P_IDENT(per);
263 u16 function = P_FUNCT2MUX(per);
264 s8 offset = port_mux[ident];
265 u16 pmux, mask;
267 if (offset == -1)
268 return;
270 pmux = bfin_read_PORT_MUX();
271 if (offset == 1)
272 mask = 3;
273 else
274 mask = 1;
276 pmux &= ~(mask << offset);
277 pmux |= ((function & mask) << offset);
279 bfin_write_PORT_MUX(pmux);
281 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
282 static int portmux_group_check(unsigned short per)
284 u16 ident = P_IDENT(per);
285 u16 function = P_FUNCT2MUX(per);
286 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
287 u16 pin, gpiopin, pfunc;
289 for (pin = 0; pin < GPIO_BANKSIZE; ++pin) {
290 if (offset != pmux_offset[gpio_bank(ident)][pin])
291 continue;
293 gpiopin = gpio_bank(ident) * GPIO_BANKSIZE + pin;
294 if (gpiopin == ident)
295 continue;
296 if (!is_reserved(peri, gpiopin, 1))
297 continue;
299 pfunc = *port_mux[gpio_bank(ident)];
300 pfunc = (pfunc >> offset) & 3;
301 if (pfunc != function) {
302 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
303 ident, function, gpiopin, pfunc);
304 return -EINVAL;
308 return 0;
311 inline void portmux_setup(unsigned short per)
313 u16 ident = P_IDENT(per);
314 u16 function = P_FUNCT2MUX(per);
315 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
316 u16 pmux;
318 pmux = *port_mux[gpio_bank(ident)];
319 if (((pmux >> offset) & 3) == function)
320 return;
321 pmux &= ~(3 << offset);
322 pmux |= (function & 3) << offset;
323 *port_mux[gpio_bank(ident)] = pmux;
324 SSYNC();
326 #else
327 # define portmux_setup(...) do { } while (0)
328 static int portmux_group_check(unsigned short per)
330 return 0;
332 #endif
334 /***********************************************************
336 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
338 * INPUTS/OUTPUTS:
339 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
342 * DESCRIPTION: These functions abstract direct register access
343 * to Blackfin processor General Purpose
344 * Ports Regsiters
346 * CAUTION: These functions do not belong to the GPIO Driver API
347 *************************************************************
348 * MODIFICATION HISTORY :
349 **************************************************************/
351 /* Set a specific bit */
353 #define SET_GPIO(name) \
354 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
356 unsigned long flags; \
357 flags = hard_local_irq_save(); \
358 if (arg) \
359 gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
360 else \
361 gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
362 AWA_DUMMY_READ(name); \
363 hard_local_irq_restore(flags); \
365 EXPORT_SYMBOL(set_gpio_ ## name);
367 SET_GPIO(dir) /* set_gpio_dir() */
368 SET_GPIO(inen) /* set_gpio_inen() */
369 SET_GPIO(polar) /* set_gpio_polar() */
370 SET_GPIO(edge) /* set_gpio_edge() */
371 SET_GPIO(both) /* set_gpio_both() */
374 #define SET_GPIO_SC(name) \
375 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
377 unsigned long flags; \
378 if (ANOMALY_05000311 || ANOMALY_05000323) \
379 flags = hard_local_irq_save(); \
380 if (arg) \
381 gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
382 else \
383 gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
384 if (ANOMALY_05000311 || ANOMALY_05000323) { \
385 AWA_DUMMY_READ(name); \
386 hard_local_irq_restore(flags); \
389 EXPORT_SYMBOL(set_gpio_ ## name);
391 SET_GPIO_SC(maska)
392 SET_GPIO_SC(maskb)
393 SET_GPIO_SC(data)
395 void set_gpio_toggle(unsigned gpio)
397 unsigned long flags;
398 if (ANOMALY_05000311 || ANOMALY_05000323)
399 flags = hard_local_irq_save();
400 gpio_array[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
401 if (ANOMALY_05000311 || ANOMALY_05000323) {
402 AWA_DUMMY_READ(toggle);
403 hard_local_irq_restore(flags);
406 EXPORT_SYMBOL(set_gpio_toggle);
409 /*Set current PORT date (16-bit word)*/
411 #define SET_GPIO_P(name) \
412 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
414 unsigned long flags; \
415 if (ANOMALY_05000311 || ANOMALY_05000323) \
416 flags = hard_local_irq_save(); \
417 gpio_array[gpio_bank(gpio)]->name = arg; \
418 if (ANOMALY_05000311 || ANOMALY_05000323) { \
419 AWA_DUMMY_READ(name); \
420 hard_local_irq_restore(flags); \
423 EXPORT_SYMBOL(set_gpiop_ ## name);
425 SET_GPIO_P(data)
426 SET_GPIO_P(dir)
427 SET_GPIO_P(inen)
428 SET_GPIO_P(polar)
429 SET_GPIO_P(edge)
430 SET_GPIO_P(both)
431 SET_GPIO_P(maska)
432 SET_GPIO_P(maskb)
434 /* Get a specific bit */
435 #define GET_GPIO(name) \
436 unsigned short get_gpio_ ## name(unsigned gpio) \
438 unsigned long flags; \
439 unsigned short ret; \
440 if (ANOMALY_05000311 || ANOMALY_05000323) \
441 flags = hard_local_irq_save(); \
442 ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
443 if (ANOMALY_05000311 || ANOMALY_05000323) { \
444 AWA_DUMMY_READ(name); \
445 hard_local_irq_restore(flags); \
447 return ret; \
449 EXPORT_SYMBOL(get_gpio_ ## name);
451 GET_GPIO(data)
452 GET_GPIO(dir)
453 GET_GPIO(inen)
454 GET_GPIO(polar)
455 GET_GPIO(edge)
456 GET_GPIO(both)
457 GET_GPIO(maska)
458 GET_GPIO(maskb)
460 /*Get current PORT date (16-bit word)*/
462 #define GET_GPIO_P(name) \
463 unsigned short get_gpiop_ ## name(unsigned gpio) \
465 unsigned long flags; \
466 unsigned short ret; \
467 if (ANOMALY_05000311 || ANOMALY_05000323) \
468 flags = hard_local_irq_save(); \
469 ret = (gpio_array[gpio_bank(gpio)]->name); \
470 if (ANOMALY_05000311 || ANOMALY_05000323) { \
471 AWA_DUMMY_READ(name); \
472 hard_local_irq_restore(flags); \
474 return ret; \
476 EXPORT_SYMBOL(get_gpiop_ ## name);
478 GET_GPIO_P(data)
479 GET_GPIO_P(dir)
480 GET_GPIO_P(inen)
481 GET_GPIO_P(polar)
482 GET_GPIO_P(edge)
483 GET_GPIO_P(both)
484 GET_GPIO_P(maska)
485 GET_GPIO_P(maskb)
488 #ifdef CONFIG_PM
489 DECLARE_RESERVED_MAP(wakeup, GPIO_BANK_NUM);
491 static const unsigned int sic_iwr_irqs[] = {
492 #if defined(BF533_FAMILY)
493 IRQ_PROG_INTB
494 #elif defined(BF537_FAMILY)
495 IRQ_PF_INTB_WATCH, IRQ_PORTG_INTB, IRQ_PH_INTB_MAC_TX
496 #elif defined(BF538_FAMILY)
497 IRQ_PORTF_INTB
498 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
499 IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB
500 #elif defined(BF561_FAMILY)
501 IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB
502 #else
503 # error no SIC_IWR defined
504 #endif
507 /***********************************************************
509 * FUNCTIONS: Blackfin PM Setup API
511 * INPUTS/OUTPUTS:
512 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
513 * type -
514 * PM_WAKE_RISING
515 * PM_WAKE_FALLING
516 * PM_WAKE_HIGH
517 * PM_WAKE_LOW
518 * PM_WAKE_BOTH_EDGES
520 * DESCRIPTION: Blackfin PM Driver API
522 * CAUTION:
523 *************************************************************
524 * MODIFICATION HISTORY :
525 **************************************************************/
526 int bfin_gpio_pm_wakeup_ctrl(unsigned gpio, unsigned ctrl)
528 unsigned long flags;
530 if (check_gpio(gpio) < 0)
531 return -EINVAL;
533 flags = hard_local_irq_save();
534 if (ctrl)
535 reserve(wakeup, gpio);
536 else
537 unreserve(wakeup, gpio);
539 set_gpio_maskb(gpio, ctrl);
540 hard_local_irq_restore(flags);
542 return 0;
545 int bfin_gpio_pm_standby_ctrl(unsigned ctrl)
547 u16 bank, mask, i;
549 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
550 mask = map_entry(wakeup, i);
551 bank = gpio_bank(i);
553 if (mask)
554 bfin_internal_set_wake(sic_iwr_irqs[bank], ctrl);
556 return 0;
559 void bfin_gpio_pm_hibernate_suspend(void)
561 int i, bank;
563 #ifdef BF538_FAMILY
564 for (i = 0; i < ARRAY_SIZE(port_fer_saved); ++i)
565 port_fer_saved[i] = *port_fer[i];
566 #endif
568 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
569 bank = gpio_bank(i);
571 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
572 gpio_bank_saved[bank].fer = *port_fer[bank];
573 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
574 gpio_bank_saved[bank].mux = *port_mux[bank];
575 #else
576 if (bank == 0)
577 gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
578 #endif
579 #endif
580 gpio_bank_saved[bank].data = gpio_array[bank]->data;
581 gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
582 gpio_bank_saved[bank].polar = gpio_array[bank]->polar;
583 gpio_bank_saved[bank].dir = gpio_array[bank]->dir;
584 gpio_bank_saved[bank].edge = gpio_array[bank]->edge;
585 gpio_bank_saved[bank].both = gpio_array[bank]->both;
586 gpio_bank_saved[bank].maska = gpio_array[bank]->maska;
589 #ifdef BFIN_SPECIAL_GPIO_BANKS
590 bfin_special_gpio_pm_hibernate_suspend();
591 #endif
593 AWA_DUMMY_READ(maska);
596 void bfin_gpio_pm_hibernate_restore(void)
598 int i, bank;
600 #ifdef BF538_FAMILY
601 for (i = 0; i < ARRAY_SIZE(port_fer_saved); ++i)
602 *port_fer[i] = port_fer_saved[i];
603 #endif
605 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
606 bank = gpio_bank(i);
608 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
609 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
610 *port_mux[bank] = gpio_bank_saved[bank].mux;
611 #else
612 if (bank == 0)
613 bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
614 #endif
615 *port_fer[bank] = gpio_bank_saved[bank].fer;
616 #endif
617 gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
618 gpio_array[bank]->data_set = gpio_bank_saved[bank].data
619 & gpio_bank_saved[bank].dir;
620 gpio_array[bank]->dir = gpio_bank_saved[bank].dir;
621 gpio_array[bank]->polar = gpio_bank_saved[bank].polar;
622 gpio_array[bank]->edge = gpio_bank_saved[bank].edge;
623 gpio_array[bank]->both = gpio_bank_saved[bank].both;
624 gpio_array[bank]->maska = gpio_bank_saved[bank].maska;
627 #ifdef BFIN_SPECIAL_GPIO_BANKS
628 bfin_special_gpio_pm_hibernate_restore();
629 #endif
631 AWA_DUMMY_READ(maska);
635 #endif
637 /***********************************************************
639 * FUNCTIONS: Blackfin Peripheral Resource Allocation
640 * and PortMux Setup
642 * INPUTS/OUTPUTS:
643 * per Peripheral Identifier
644 * label String
646 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
648 * CAUTION:
649 *************************************************************
650 * MODIFICATION HISTORY :
651 **************************************************************/
653 int peripheral_request(unsigned short per, const char *label)
655 unsigned long flags;
656 unsigned short ident = P_IDENT(per);
659 * Don't cares are pins with only one dedicated function
662 if (per & P_DONTCARE)
663 return 0;
665 if (!(per & P_DEFINED))
666 return -ENODEV;
668 BUG_ON(ident >= MAX_RESOURCES);
670 flags = hard_local_irq_save();
672 /* If a pin can be muxed as either GPIO or peripheral, make
673 * sure it is not already a GPIO pin when we request it.
675 if (unlikely(!check_gpio(ident) && is_reserved(gpio, ident, 1))) {
676 if (system_state == SYSTEM_BOOTING)
677 dump_stack();
678 printk(KERN_ERR
679 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
680 __func__, ident, get_label(ident));
681 hard_local_irq_restore(flags);
682 return -EBUSY;
685 if (unlikely(is_reserved(peri, ident, 1))) {
688 * Pin functions like AMC address strobes my
689 * be requested and used by several drivers
692 if (!(per & P_MAYSHARE)) {
694 * Allow that the identical pin function can
695 * be requested from the same driver twice
698 if (cmp_label(ident, label) == 0)
699 goto anyway;
701 if (system_state == SYSTEM_BOOTING)
702 dump_stack();
703 printk(KERN_ERR
704 "%s: Peripheral %d function %d is already reserved by %s !\n",
705 __func__, ident, P_FUNCT2MUX(per), get_label(ident));
706 hard_local_irq_restore(flags);
707 return -EBUSY;
711 if (unlikely(portmux_group_check(per))) {
712 hard_local_irq_restore(flags);
713 return -EBUSY;
715 anyway:
716 reserve(peri, ident);
718 portmux_setup(per);
719 port_setup(ident, PERIPHERAL_USAGE);
721 hard_local_irq_restore(flags);
722 set_label(ident, label);
724 return 0;
726 EXPORT_SYMBOL(peripheral_request);
728 int peripheral_request_list(const unsigned short per[], const char *label)
730 u16 cnt;
731 int ret;
733 for (cnt = 0; per[cnt] != 0; cnt++) {
735 ret = peripheral_request(per[cnt], label);
737 if (ret < 0) {
738 for ( ; cnt > 0; cnt--)
739 peripheral_free(per[cnt - 1]);
741 return ret;
745 return 0;
747 EXPORT_SYMBOL(peripheral_request_list);
749 void peripheral_free(unsigned short per)
751 unsigned long flags;
752 unsigned short ident = P_IDENT(per);
754 if (per & P_DONTCARE)
755 return;
757 if (!(per & P_DEFINED))
758 return;
760 flags = hard_local_irq_save();
762 if (unlikely(!is_reserved(peri, ident, 0))) {
763 hard_local_irq_restore(flags);
764 return;
767 if (!(per & P_MAYSHARE))
768 port_setup(ident, GPIO_USAGE);
770 unreserve(peri, ident);
772 set_label(ident, "free");
774 hard_local_irq_restore(flags);
776 EXPORT_SYMBOL(peripheral_free);
778 void peripheral_free_list(const unsigned short per[])
780 u16 cnt;
781 for (cnt = 0; per[cnt] != 0; cnt++)
782 peripheral_free(per[cnt]);
784 EXPORT_SYMBOL(peripheral_free_list);
786 /***********************************************************
788 * FUNCTIONS: Blackfin GPIO Driver
790 * INPUTS/OUTPUTS:
791 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
792 * label String
794 * DESCRIPTION: Blackfin GPIO Driver API
796 * CAUTION:
797 *************************************************************
798 * MODIFICATION HISTORY :
799 **************************************************************/
801 int bfin_gpio_request(unsigned gpio, const char *label)
803 unsigned long flags;
805 if (check_gpio(gpio) < 0)
806 return -EINVAL;
808 flags = hard_local_irq_save();
811 * Allow that the identical GPIO can
812 * be requested from the same driver twice
813 * Do nothing and return -
816 if (cmp_label(gpio, label) == 0) {
817 hard_local_irq_restore(flags);
818 return 0;
821 if (unlikely(is_reserved(gpio, gpio, 1))) {
822 if (system_state == SYSTEM_BOOTING)
823 dump_stack();
824 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
825 gpio, get_label(gpio));
826 hard_local_irq_restore(flags);
827 return -EBUSY;
829 if (unlikely(is_reserved(peri, gpio, 1))) {
830 if (system_state == SYSTEM_BOOTING)
831 dump_stack();
832 printk(KERN_ERR
833 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
834 gpio, get_label(gpio));
835 hard_local_irq_restore(flags);
836 return -EBUSY;
838 if (unlikely(is_reserved(gpio_irq, gpio, 1))) {
839 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved as gpio-irq!"
840 " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio);
841 } else { /* Reset POLAR setting when acquiring a gpio for the first time */
842 set_gpio_polar(gpio, 0);
845 reserve(gpio, gpio);
846 set_label(gpio, label);
848 hard_local_irq_restore(flags);
850 port_setup(gpio, GPIO_USAGE);
852 return 0;
854 EXPORT_SYMBOL(bfin_gpio_request);
856 void bfin_gpio_free(unsigned gpio)
858 unsigned long flags;
860 if (check_gpio(gpio) < 0)
861 return;
863 might_sleep();
865 flags = hard_local_irq_save();
867 if (unlikely(!is_reserved(gpio, gpio, 0))) {
868 if (system_state == SYSTEM_BOOTING)
869 dump_stack();
870 gpio_error(gpio);
871 hard_local_irq_restore(flags);
872 return;
875 unreserve(gpio, gpio);
877 set_label(gpio, "free");
879 hard_local_irq_restore(flags);
881 EXPORT_SYMBOL(bfin_gpio_free);
883 #ifdef BFIN_SPECIAL_GPIO_BANKS
884 DECLARE_RESERVED_MAP(special_gpio, gpio_bank(MAX_RESOURCES));
886 int bfin_special_gpio_request(unsigned gpio, const char *label)
888 unsigned long flags;
890 flags = hard_local_irq_save();
893 * Allow that the identical GPIO can
894 * be requested from the same driver twice
895 * Do nothing and return -
898 if (cmp_label(gpio, label) == 0) {
899 hard_local_irq_restore(flags);
900 return 0;
903 if (unlikely(is_reserved(special_gpio, gpio, 1))) {
904 hard_local_irq_restore(flags);
905 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
906 gpio, get_label(gpio));
908 return -EBUSY;
910 if (unlikely(is_reserved(peri, gpio, 1))) {
911 hard_local_irq_restore(flags);
912 printk(KERN_ERR
913 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
914 gpio, get_label(gpio));
916 return -EBUSY;
919 reserve(special_gpio, gpio);
920 reserve(peri, gpio);
922 set_label(gpio, label);
923 hard_local_irq_restore(flags);
924 port_setup(gpio, GPIO_USAGE);
926 return 0;
928 EXPORT_SYMBOL(bfin_special_gpio_request);
930 void bfin_special_gpio_free(unsigned gpio)
932 unsigned long flags;
934 might_sleep();
936 flags = hard_local_irq_save();
938 if (unlikely(!is_reserved(special_gpio, gpio, 0))) {
939 gpio_error(gpio);
940 hard_local_irq_restore(flags);
941 return;
944 unreserve(special_gpio, gpio);
945 unreserve(peri, gpio);
946 set_label(gpio, "free");
947 hard_local_irq_restore(flags);
949 EXPORT_SYMBOL(bfin_special_gpio_free);
950 #endif
953 int bfin_gpio_irq_request(unsigned gpio, const char *label)
955 unsigned long flags;
957 if (check_gpio(gpio) < 0)
958 return -EINVAL;
960 flags = hard_local_irq_save();
962 if (unlikely(is_reserved(peri, gpio, 1))) {
963 if (system_state == SYSTEM_BOOTING)
964 dump_stack();
965 printk(KERN_ERR
966 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
967 gpio, get_label(gpio));
968 hard_local_irq_restore(flags);
969 return -EBUSY;
971 if (unlikely(is_reserved(gpio, gpio, 1)))
972 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved by %s! "
973 "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
974 gpio, get_label(gpio));
976 reserve(gpio_irq, gpio);
977 set_label(gpio, label);
979 hard_local_irq_restore(flags);
981 port_setup(gpio, GPIO_USAGE);
983 return 0;
986 void bfin_gpio_irq_free(unsigned gpio)
988 unsigned long flags;
990 if (check_gpio(gpio) < 0)
991 return;
993 flags = hard_local_irq_save();
995 if (unlikely(!is_reserved(gpio_irq, gpio, 0))) {
996 if (system_state == SYSTEM_BOOTING)
997 dump_stack();
998 gpio_error(gpio);
999 hard_local_irq_restore(flags);
1000 return;
1003 unreserve(gpio_irq, gpio);
1005 set_label(gpio, "free");
1007 hard_local_irq_restore(flags);
1010 static inline void __bfin_gpio_direction_input(unsigned gpio)
1012 gpio_array[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1013 gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
1016 int bfin_gpio_direction_input(unsigned gpio)
1018 unsigned long flags;
1020 if (unlikely(!is_reserved(gpio, gpio, 0))) {
1021 gpio_error(gpio);
1022 return -EINVAL;
1025 flags = hard_local_irq_save();
1026 __bfin_gpio_direction_input(gpio);
1027 AWA_DUMMY_READ(inen);
1028 hard_local_irq_restore(flags);
1030 return 0;
1032 EXPORT_SYMBOL(bfin_gpio_direction_input);
1034 void bfin_gpio_irq_prepare(unsigned gpio)
1036 port_setup(gpio, GPIO_USAGE);
1039 void bfin_gpio_set_value(unsigned gpio, int arg)
1041 if (arg)
1042 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1043 else
1044 gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1046 EXPORT_SYMBOL(bfin_gpio_set_value);
1048 int bfin_gpio_direction_output(unsigned gpio, int value)
1050 unsigned long flags;
1052 if (unlikely(!is_reserved(gpio, gpio, 0))) {
1053 gpio_error(gpio);
1054 return -EINVAL;
1057 flags = hard_local_irq_save();
1059 gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
1060 gpio_set_value(gpio, value);
1061 gpio_array[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
1063 AWA_DUMMY_READ(dir);
1064 hard_local_irq_restore(flags);
1066 return 0;
1068 EXPORT_SYMBOL(bfin_gpio_direction_output);
1070 int bfin_gpio_get_value(unsigned gpio)
1072 unsigned long flags;
1074 if (unlikely(get_gpio_edge(gpio))) {
1075 int ret;
1076 flags = hard_local_irq_save();
1077 set_gpio_edge(gpio, 0);
1078 ret = get_gpio_data(gpio);
1079 set_gpio_edge(gpio, 1);
1080 hard_local_irq_restore(flags);
1081 return ret;
1082 } else
1083 return get_gpio_data(gpio);
1085 EXPORT_SYMBOL(bfin_gpio_get_value);
1087 /* If we are booting from SPI and our board lacks a strong enough pull up,
1088 * the core can reset and execute the bootrom faster than the resistor can
1089 * pull the signal logically high. To work around this (common) error in
1090 * board design, we explicitly set the pin back to GPIO mode, force /CS
1091 * high, and wait for the electrons to do their thing.
1093 * This function only makes sense to be called from reset code, but it
1094 * lives here as we need to force all the GPIO states w/out going through
1095 * BUG() checks and such.
1097 void bfin_reset_boot_spi_cs(unsigned short pin)
1099 unsigned short gpio = P_IDENT(pin);
1100 port_setup(gpio, GPIO_USAGE);
1101 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1102 AWA_DUMMY_READ(data_set);
1103 udelay(1);
1106 #if defined(CONFIG_PROC_FS)
1107 static int gpio_proc_show(struct seq_file *m, void *v)
1109 int c, irq, gpio;
1111 for (c = 0; c < MAX_RESOURCES; c++) {
1112 irq = is_reserved(gpio_irq, c, 1);
1113 gpio = is_reserved(gpio, c, 1);
1114 if (!check_gpio(c) && (gpio || irq))
1115 seq_printf(m, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c,
1116 get_label(c), (gpio && irq) ? " *" : "",
1117 get_gpio_dir(c) ? "OUTPUT" : "INPUT");
1118 else if (is_reserved(peri, c, 1))
1119 seq_printf(m, "GPIO_%d: \t%s \t\tPeripheral\n", c, get_label(c));
1120 else
1121 continue;
1124 return 0;
1127 static int gpio_proc_open(struct inode *inode, struct file *file)
1129 return single_open(file, gpio_proc_show, NULL);
1132 static const struct file_operations gpio_proc_ops = {
1133 .open = gpio_proc_open,
1134 .read = seq_read,
1135 .llseek = seq_lseek,
1136 .release = single_release,
1139 static __init int gpio_register_proc(void)
1141 struct proc_dir_entry *proc_gpio;
1143 proc_gpio = proc_create("gpio", 0, NULL, &gpio_proc_ops);
1144 return proc_gpio == NULL;
1146 __initcall(gpio_register_proc);
1147 #endif
1149 #ifdef CONFIG_GPIOLIB
1150 static int bfin_gpiolib_direction_input(struct gpio_chip *chip, unsigned gpio)
1152 return bfin_gpio_direction_input(gpio);
1155 static int bfin_gpiolib_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
1157 return bfin_gpio_direction_output(gpio, level);
1160 static int bfin_gpiolib_get_value(struct gpio_chip *chip, unsigned gpio)
1162 return bfin_gpio_get_value(gpio);
1165 static void bfin_gpiolib_set_value(struct gpio_chip *chip, unsigned gpio, int value)
1167 return bfin_gpio_set_value(gpio, value);
1170 static int bfin_gpiolib_gpio_request(struct gpio_chip *chip, unsigned gpio)
1172 return bfin_gpio_request(gpio, chip->label);
1175 static void bfin_gpiolib_gpio_free(struct gpio_chip *chip, unsigned gpio)
1177 return bfin_gpio_free(gpio);
1180 static int bfin_gpiolib_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
1182 return gpio + GPIO_IRQ_BASE;
1185 static struct gpio_chip bfin_chip = {
1186 .label = "BFIN-GPIO",
1187 .direction_input = bfin_gpiolib_direction_input,
1188 .get = bfin_gpiolib_get_value,
1189 .direction_output = bfin_gpiolib_direction_output,
1190 .set = bfin_gpiolib_set_value,
1191 .request = bfin_gpiolib_gpio_request,
1192 .free = bfin_gpiolib_gpio_free,
1193 .to_irq = bfin_gpiolib_gpio_to_irq,
1194 .base = 0,
1195 .ngpio = MAX_BLACKFIN_GPIOS,
1198 static int __init bfin_gpiolib_setup(void)
1200 return gpiochip_add(&bfin_chip);
1202 arch_initcall(bfin_gpiolib_setup);
1203 #endif