gpio.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * File: arch/blackfin/mach-bf548/gpio.c
  3. * Based on:
  4. * Author: Michael Hennerich (hennerich@blackfin.uclinux.org)
  5. *
  6. * Created:
  7. * Description: GPIO Abstraction Layer
  8. *
  9. * Modified:
  10. * Copyright 2007 Analog Devices Inc.
  11. *
  12. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  13. *
  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.
  18. *
  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.
  23. *
  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
  28. */
  29. #include <linux/module.h>
  30. #include <linux/err.h>
  31. #include <asm/blackfin.h>
  32. #include <asm/gpio.h>
  33. #include <linux/irq.h>
  34. static struct gpio_port_t *gpio_array[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
  35. (struct gpio_port_t *) PORTA_FER,
  36. (struct gpio_port_t *) PORTB_FER,
  37. (struct gpio_port_t *) PORTC_FER,
  38. (struct gpio_port_t *) PORTD_FER,
  39. (struct gpio_port_t *) PORTE_FER,
  40. (struct gpio_port_t *) PORTF_FER,
  41. (struct gpio_port_t *) PORTG_FER,
  42. (struct gpio_port_t *) PORTH_FER,
  43. (struct gpio_port_t *) PORTI_FER,
  44. (struct gpio_port_t *) PORTJ_FER,
  45. };
  46. static unsigned short reserved_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
  47. inline int check_gpio(unsigned short gpio)
  48. {
  49. if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15 \
  50. || gpio == GPIO_PH14 || gpio == GPIO_PH15 \
  51. || gpio == GPIO_PJ14 || gpio == GPIO_PJ15 \
  52. || gpio > MAX_BLACKFIN_GPIOS)
  53. return -EINVAL;
  54. return 0;
  55. }
  56. static void port_setup(unsigned short gpio, unsigned short usage)
  57. {
  58. if (usage == GPIO_USAGE) {
  59. if (gpio_array[gpio_bank(gpio)]->port_fer & gpio_bit(gpio))
  60. printk(KERN_WARNING "bfin-gpio: Possible Conflict with Peripheral "
  61. "usage and GPIO %d detected!\n", gpio);
  62. gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
  63. } else
  64. gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
  65. SSYNC();
  66. }
  67. static int __init bfin_gpio_init(void)
  68. {
  69. int i;
  70. printk(KERN_INFO "Blackfin GPIO Controller\n");
  71. for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE)
  72. reserved_map[gpio_bank(i)] = 0;
  73. return 0;
  74. }
  75. arch_initcall(bfin_gpio_init);
  76. /***********************************************************
  77. *
  78. * FUNCTIONS: Blackfin GPIO Driver
  79. *
  80. * INPUTS/OUTPUTS:
  81. * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
  82. *
  83. *
  84. * DESCRIPTION: Blackfin GPIO Driver API
  85. *
  86. * CAUTION:
  87. *************************************************************
  88. * MODIFICATION HISTORY :
  89. **************************************************************/
  90. int gpio_request(unsigned short gpio, const char *label)
  91. {
  92. unsigned long flags;
  93. if (check_gpio(gpio) < 0)
  94. return -EINVAL;
  95. local_irq_save(flags);
  96. if (unlikely(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
  97. printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved!\n", gpio);
  98. dump_stack();
  99. local_irq_restore(flags);
  100. return -EBUSY;
  101. }
  102. reserved_map[gpio_bank(gpio)] |= gpio_bit(gpio);
  103. local_irq_restore(flags);
  104. port_setup(gpio, GPIO_USAGE);
  105. return 0;
  106. }
  107. EXPORT_SYMBOL(gpio_request);
  108. void gpio_free(unsigned short gpio)
  109. {
  110. unsigned long flags;
  111. if (check_gpio(gpio) < 0)
  112. return;
  113. local_irq_save(flags);
  114. if (unlikely(!(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
  115. printk(KERN_ERR "bfin-gpio: GPIO %d wasn't reserved!\n", gpio);
  116. dump_stack();
  117. local_irq_restore(flags);
  118. return;
  119. }
  120. reserved_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
  121. local_irq_restore(flags);
  122. }
  123. EXPORT_SYMBOL(gpio_free);
  124. void gpio_direction_input(unsigned short gpio)
  125. {
  126. unsigned long flags;
  127. BUG_ON(!(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio)));
  128. local_irq_save(flags);
  129. gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
  130. gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
  131. local_irq_restore(flags);
  132. }
  133. EXPORT_SYMBOL(gpio_direction_input);
  134. void gpio_direction_output(unsigned short gpio)
  135. {
  136. unsigned long flags;
  137. BUG_ON(!(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio)));
  138. local_irq_save(flags);
  139. gpio_array[gpio_bank(gpio)]->port_inen &= ~gpio_bit(gpio);
  140. gpio_array[gpio_bank(gpio)]->port_dir_set = gpio_bit(gpio);
  141. local_irq_restore(flags);
  142. }
  143. EXPORT_SYMBOL(gpio_direction_output);
  144. void gpio_set_value(unsigned short gpio, unsigned short arg)
  145. {
  146. if (arg)
  147. gpio_array[gpio_bank(gpio)]->port_set = gpio_bit(gpio);
  148. else
  149. gpio_array[gpio_bank(gpio)]->port_clear = gpio_bit(gpio);
  150. }
  151. EXPORT_SYMBOL(gpio_set_value);
  152. unsigned short gpio_get_value(unsigned short gpio)
  153. {
  154. return (1 & (gpio_array[gpio_bank(gpio)]->port_data >> gpio_sub_n(gpio)));
  155. }
  156. EXPORT_SYMBOL(gpio_get_value);