gpio.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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/debugfs.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/module.h>
  25. #include <asm/io.h>
  26. #include <asm/hardware.h>
  27. #include <asm/mach/irq.h>
  28. #include <asm/arch/regs-gpio.h>
  29. #include <asm/arch/gpio.h>
  30. /*
  31. * Configure a GPIO line for either GPIO function, or its internal
  32. * function (Interrupt, Timer, etc).
  33. */
  34. static void __init_or_module ks8695_gpio_mode(unsigned int pin, short gpio)
  35. {
  36. unsigned int enable[] = { IOPC_IOEINT0EN, IOPC_IOEINT1EN, IOPC_IOEINT2EN, IOPC_IOEINT3EN, IOPC_IOTIM0EN, IOPC_IOTIM1EN };
  37. unsigned long x, flags;
  38. if (pin > KS8695_GPIO_5) /* only GPIO 0..5 have internal functions */
  39. return;
  40. local_irq_save(flags);
  41. x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPC);
  42. if (gpio) /* GPIO: set bit to 0 */
  43. x &= ~enable[pin];
  44. else /* Internal function: set bit to 1 */
  45. x |= enable[pin];
  46. __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPC);
  47. local_irq_restore(flags);
  48. }
  49. static unsigned short gpio_irq[] = { KS8695_IRQ_EXTERN0, KS8695_IRQ_EXTERN1, KS8695_IRQ_EXTERN2, KS8695_IRQ_EXTERN3 };
  50. /*
  51. * Configure GPIO pin as external interrupt source.
  52. */
  53. int __init_or_module ks8695_gpio_interrupt(unsigned int pin, unsigned int type)
  54. {
  55. unsigned long x, flags;
  56. if (pin > KS8695_GPIO_3) /* only GPIO 0..3 can generate IRQ */
  57. return -EINVAL;
  58. local_irq_save(flags);
  59. /* set pin as input */
  60. x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
  61. x &= ~IOPM_(pin);
  62. __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
  63. local_irq_restore(flags);
  64. /* Set IRQ triggering type */
  65. set_irq_type(gpio_irq[pin], type);
  66. /* enable interrupt mode */
  67. ks8695_gpio_mode(pin, 0);
  68. return 0;
  69. }
  70. EXPORT_SYMBOL(ks8695_gpio_interrupt);
  71. /* .... Generic GPIO interface .............................................. */
  72. /*
  73. * Configure the GPIO line as an input.
  74. */
  75. int __init_or_module gpio_direction_input(unsigned int pin)
  76. {
  77. unsigned long x, flags;
  78. if (pin > KS8695_GPIO_15)
  79. return -EINVAL;
  80. /* set pin to GPIO mode */
  81. ks8695_gpio_mode(pin, 1);
  82. local_irq_save(flags);
  83. /* set pin as input */
  84. x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
  85. x &= ~IOPM_(pin);
  86. __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
  87. local_irq_restore(flags);
  88. return 0;
  89. }
  90. EXPORT_SYMBOL(gpio_direction_input);
  91. /*
  92. * Configure the GPIO line as an output, with default state.
  93. */
  94. int __init_or_module gpio_direction_output(unsigned int pin, unsigned int state)
  95. {
  96. unsigned long x, flags;
  97. if (pin > KS8695_GPIO_15)
  98. return -EINVAL;
  99. /* set pin to GPIO mode */
  100. ks8695_gpio_mode(pin, 1);
  101. local_irq_save(flags);
  102. /* set line state */
  103. x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
  104. if (state)
  105. x |= (1 << pin);
  106. else
  107. x &= ~(1 << pin);
  108. __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
  109. /* set pin as output */
  110. x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
  111. x |= IOPM_(pin);
  112. __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
  113. local_irq_restore(flags);
  114. return 0;
  115. }
  116. EXPORT_SYMBOL(gpio_direction_output);
  117. /*
  118. * Set the state of an output GPIO line.
  119. */
  120. void gpio_set_value(unsigned int pin, unsigned int state)
  121. {
  122. unsigned long x, flags;
  123. if (pin > KS8695_GPIO_15)
  124. return;
  125. local_irq_save(flags);
  126. /* set output line state */
  127. x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
  128. if (state)
  129. x |= (1 << pin);
  130. else
  131. x &= ~(1 << pin);
  132. __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
  133. local_irq_restore(flags);
  134. }
  135. EXPORT_SYMBOL(gpio_set_value);
  136. /*
  137. * Read the state of a GPIO line.
  138. */
  139. int gpio_get_value(unsigned int pin)
  140. {
  141. unsigned long x;
  142. if (pin > KS8695_GPIO_15)
  143. return -EINVAL;
  144. x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
  145. return (x & (1 << pin)) != 0;
  146. }
  147. EXPORT_SYMBOL(gpio_get_value);
  148. /*
  149. * Map GPIO line to IRQ number.
  150. */
  151. int gpio_to_irq(unsigned int pin)
  152. {
  153. if (pin > KS8695_GPIO_3) /* only GPIO 0..3 can generate IRQ */
  154. return -EINVAL;
  155. return gpio_irq[pin];
  156. }
  157. EXPORT_SYMBOL(gpio_to_irq);
  158. /*
  159. * Map IRQ number to GPIO line.
  160. */
  161. int irq_to_gpio(unsigned int irq)
  162. {
  163. if ((irq < KS8695_IRQ_EXTERN0) || (irq > KS8695_IRQ_EXTERN3))
  164. return -EINVAL;
  165. return (irq - KS8695_IRQ_EXTERN0);
  166. }
  167. EXPORT_SYMBOL(irq_to_gpio);
  168. /* .... Debug interface ..................................................... */
  169. #ifdef CONFIG_DEBUG_FS
  170. static int ks8695_gpio_show(struct seq_file *s, void *unused)
  171. {
  172. unsigned int enable[] = { IOPC_IOEINT0EN, IOPC_IOEINT1EN, IOPC_IOEINT2EN, IOPC_IOEINT3EN, IOPC_IOTIM0EN, IOPC_IOTIM1EN };
  173. unsigned int intmask[] = { IOPC_IOEINT0TM, IOPC_IOEINT1TM, IOPC_IOEINT2TM, IOPC_IOEINT3TM };
  174. unsigned long mode, ctrl, data;
  175. int i;
  176. mode = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
  177. ctrl = __raw_readl(KS8695_GPIO_VA + KS8695_IOPC);
  178. data = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
  179. seq_printf(s, "Pin\tI/O\tFunction\tState\n\n");
  180. for (i = KS8695_GPIO_0; i <= KS8695_GPIO_15 ; i++) {
  181. seq_printf(s, "%i:\t", i);
  182. seq_printf(s, "%s\t", (mode & IOPM_(i)) ? "Output" : "Input");
  183. if (i <= KS8695_GPIO_3) {
  184. if (ctrl & enable[i]) {
  185. seq_printf(s, "EXT%i ", i);
  186. switch ((ctrl & intmask[i]) >> (4 * i)) {
  187. case IOPC_TM_LOW:
  188. seq_printf(s, "(Low)"); break;
  189. case IOPC_TM_HIGH:
  190. seq_printf(s, "(High)"); break;
  191. case IOPC_TM_RISING:
  192. seq_printf(s, "(Rising)"); break;
  193. case IOPC_TM_FALLING:
  194. seq_printf(s, "(Falling)"); break;
  195. case IOPC_TM_EDGE:
  196. seq_printf(s, "(Edges)"); break;
  197. }
  198. }
  199. else
  200. seq_printf(s, "GPIO\t");
  201. }
  202. else if (i <= KS8695_GPIO_5) {
  203. if (ctrl & enable[i])
  204. seq_printf(s, "TOUT%i\t", i - KS8695_GPIO_4);
  205. else
  206. seq_printf(s, "GPIO\t");
  207. }
  208. else
  209. seq_printf(s, "GPIO\t");
  210. seq_printf(s, "\t");
  211. seq_printf(s, "%i\n", (data & IOPD_(i)) ? 1 : 0);
  212. }
  213. return 0;
  214. }
  215. static int ks8695_gpio_open(struct inode *inode, struct file *file)
  216. {
  217. return single_open(file, ks8695_gpio_show, NULL);
  218. }
  219. static const struct file_operations ks8695_gpio_operations = {
  220. .open = ks8695_gpio_open,
  221. .read = seq_read,
  222. .llseek = seq_lseek,
  223. .release = single_release,
  224. };
  225. static int __init ks8695_gpio_debugfs_init(void)
  226. {
  227. /* /sys/kernel/debug/ks8695_gpio */
  228. (void) debugfs_create_file("ks8695_gpio", S_IFREG | S_IRUGO, NULL, NULL, &ks8695_gpio_operations);
  229. return 0;
  230. }
  231. postcore_initcall(ks8695_gpio_debugfs_init);
  232. #endif