gpio.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * arch/arm/mach-ks8695/gpio.c
  3. *
  4. * Copyright (C) 2006 Andrew Victor
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/mm.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <asm/io.h>
  24. #include <asm/hardware.h>
  25. #include <asm/mach/irq.h>
  26. #include <asm/arch/regs-gpio.h>
  27. #include <asm/arch/gpio.h>
  28. /*
  29. * Configure a GPIO line for either GPIO function, or its internal
  30. * function (Interrupt, Timer, etc).
  31. */
  32. static void __init_or_module ks8695_gpio_mode(unsigned int pin, short gpio)
  33. {
  34. unsigned int enable[] = { IOPC_IOEINT0EN, IOPC_IOEINT1EN, IOPC_IOEINT2EN, IOPC_IOEINT3EN, IOPC_IOTIM0EN, IOPC_IOTIM1EN };
  35. unsigned long x, flags;
  36. if (pin > KS8695_GPIO_5) /* only GPIO 0..5 have internal functions */
  37. return;
  38. local_irq_save(flags);
  39. x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPC);
  40. if (gpio) /* GPIO: set bit to 0 */
  41. x &= ~enable[pin];
  42. else /* Internal function: set bit to 1 */
  43. x |= enable[pin];
  44. __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPC);
  45. local_irq_restore(flags);
  46. }
  47. static unsigned short gpio_irq[] = { KS8695_IRQ_EXTERN0, KS8695_IRQ_EXTERN1, KS8695_IRQ_EXTERN2, KS8695_IRQ_EXTERN3 };
  48. /*
  49. * Configure GPIO pin as external interrupt source.
  50. */
  51. int __init_or_module ks8695_gpio_interrupt(unsigned int pin, unsigned int type)
  52. {
  53. unsigned long x, flags;
  54. if (pin > KS8695_GPIO_3) /* only GPIO 0..3 can generate IRQ */
  55. return -EINVAL;
  56. local_irq_save(flags);
  57. /* set pin as input */
  58. x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
  59. x &= ~IOPM_(pin);
  60. __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
  61. local_irq_restore(flags);
  62. /* Set IRQ triggering type */
  63. set_irq_type(gpio_irq[pin], type);
  64. /* enable interrupt mode */
  65. ks8695_gpio_mode(pin, 0);
  66. return 0;
  67. }
  68. EXPORT_SYMBOL(ks8695_gpio_interrupt);
  69. /* .... Generic GPIO interface .............................................. */
  70. /*
  71. * Configure the GPIO line as an input.
  72. */
  73. int __init_or_module gpio_direction_input(unsigned int pin)
  74. {
  75. unsigned long x, flags;
  76. if (pin > KS8695_GPIO_15)
  77. return -EINVAL;
  78. /* set pin to GPIO mode */
  79. ks8695_gpio_mode(pin, 1);
  80. local_irq_save(flags);
  81. /* set pin as input */
  82. x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
  83. x &= ~IOPM_(pin);
  84. __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
  85. local_irq_restore(flags);
  86. return 0;
  87. }
  88. EXPORT_SYMBOL(gpio_direction_input);
  89. /*
  90. * Configure the GPIO line as an output, with default state.
  91. */
  92. int __init_or_module gpio_direction_output(unsigned int pin, unsigned int state)
  93. {
  94. unsigned long x, flags;
  95. if (pin > KS8695_GPIO_15)
  96. return -EINVAL;
  97. /* set pin to GPIO mode */
  98. ks8695_gpio_mode(pin, 1);
  99. local_irq_save(flags);
  100. /* set line state */
  101. x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
  102. if (state)
  103. x |= (1 << pin);
  104. else
  105. x &= ~(1 << pin);
  106. __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
  107. /* set pin as output */
  108. x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
  109. x |= IOPM_(pin);
  110. __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
  111. local_irq_restore(flags);
  112. return 0;
  113. }
  114. EXPORT_SYMBOL(gpio_direction_output);
  115. /*
  116. * Set the state of an output GPIO line.
  117. */
  118. void gpio_set_value(unsigned int pin, unsigned int state)
  119. {
  120. unsigned long x, flags;
  121. if (pin > KS8695_GPIO_15)
  122. return;
  123. local_irq_save(flags);
  124. /* set output line state */
  125. x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
  126. if (state)
  127. x |= (1 << pin);
  128. else
  129. x &= ~(1 << pin);
  130. __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
  131. local_irq_restore(flags);
  132. }
  133. EXPORT_SYMBOL(gpio_set_value);
  134. /*
  135. * Read the state of a GPIO line.
  136. */
  137. int gpio_get_value(unsigned int pin)
  138. {
  139. unsigned long x;
  140. if (pin > KS8695_GPIO_15)
  141. return -EINVAL;
  142. x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
  143. return (x & (1 << pin)) != 0;
  144. }
  145. EXPORT_SYMBOL(gpio_get_value);
  146. /*
  147. * Map GPIO line to IRQ number.
  148. */
  149. int gpio_to_irq(unsigned int pin)
  150. {
  151. if (pin > KS8695_GPIO_3) /* only GPIO 0..3 can generate IRQ */
  152. return -EINVAL;
  153. return gpio_irq[pin];
  154. }
  155. EXPORT_SYMBOL(gpio_to_irq);
  156. /*
  157. * Map IRQ number to GPIO line.
  158. */
  159. int irq_to_gpio(unsigned int irq)
  160. {
  161. if ((irq < KS8695_IRQ_EXTERN0) || (irq > KS8695_IRQ_EXTERN3))
  162. return -EINVAL;
  163. return (irq - KS8695_IRQ_EXTERN0);
  164. }
  165. EXPORT_SYMBOL(irq_to_gpio);