gpio.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * arch/arm/mach-ks8695/gpio.c
  3. *
  4. * Copyright (C) 2006 Andrew Victor
  5. * Updated to GPIOLIB, Copyright 2008 Simtec Electronics
  6. * Daniel Silverstone <dsilvers@simtec.co.uk>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/init.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/module.h>
  27. #include <linux/io.h>
  28. #include <mach/hardware.h>
  29. #include <asm/mach/irq.h>
  30. #include <mach/regs-gpio.h>
  31. #include <mach/gpio.h>
  32. /*
  33. * Configure a GPIO line for either GPIO function, or its internal
  34. * function (Interrupt, Timer, etc).
  35. */
  36. static void ks8695_gpio_mode(unsigned int pin, short gpio)
  37. {
  38. unsigned int enable[] = { IOPC_IOEINT0EN, IOPC_IOEINT1EN, IOPC_IOEINT2EN, IOPC_IOEINT3EN, IOPC_IOTIM0EN, IOPC_IOTIM1EN };
  39. unsigned long x, flags;
  40. if (pin > KS8695_GPIO_5) /* only GPIO 0..5 have internal functions */
  41. return;
  42. local_irq_save(flags);
  43. x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPC);
  44. if (gpio) /* GPIO: set bit to 0 */
  45. x &= ~enable[pin];
  46. else /* Internal function: set bit to 1 */
  47. x |= enable[pin];
  48. __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPC);
  49. local_irq_restore(flags);
  50. }
  51. static unsigned short gpio_irq[] = { KS8695_IRQ_EXTERN0, KS8695_IRQ_EXTERN1, KS8695_IRQ_EXTERN2, KS8695_IRQ_EXTERN3 };
  52. /*
  53. * Configure GPIO pin as external interrupt source.
  54. */
  55. int ks8695_gpio_interrupt(unsigned int pin, unsigned int type)
  56. {
  57. unsigned long x, flags;
  58. if (pin > KS8695_GPIO_3) /* only GPIO 0..3 can generate IRQ */
  59. return -EINVAL;
  60. local_irq_save(flags);
  61. /* set pin as input */
  62. x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
  63. x &= ~IOPM(pin);
  64. __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
  65. local_irq_restore(flags);
  66. /* Set IRQ triggering type */
  67. set_irq_type(gpio_irq[pin], type);
  68. /* enable interrupt mode */
  69. ks8695_gpio_mode(pin, 0);
  70. return 0;
  71. }
  72. EXPORT_SYMBOL(ks8695_gpio_interrupt);
  73. /* .... Generic GPIO interface .............................................. */
  74. /*
  75. * Configure the GPIO line as an input.
  76. */
  77. static int ks8695_gpio_direction_input(struct gpio_chip *gc, unsigned int pin)
  78. {
  79. unsigned long x, flags;
  80. if (pin > KS8695_GPIO_15)
  81. return -EINVAL;
  82. /* set pin to GPIO mode */
  83. ks8695_gpio_mode(pin, 1);
  84. local_irq_save(flags);
  85. /* set pin as input */
  86. x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
  87. x &= ~IOPM(pin);
  88. __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
  89. local_irq_restore(flags);
  90. return 0;
  91. }
  92. /*
  93. * Configure the GPIO line as an output, with default state.
  94. */
  95. static int ks8695_gpio_direction_output(struct gpio_chip *gc,
  96. unsigned int pin, int state)
  97. {
  98. unsigned long x, flags;
  99. if (pin > KS8695_GPIO_15)
  100. return -EINVAL;
  101. /* set pin to GPIO mode */
  102. ks8695_gpio_mode(pin, 1);
  103. local_irq_save(flags);
  104. /* set line state */
  105. x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
  106. if (state)
  107. x |= IOPD(pin);
  108. else
  109. x &= ~IOPD(pin);
  110. __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
  111. /* set pin as output */
  112. x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
  113. x |= IOPM(pin);
  114. __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
  115. local_irq_restore(flags);
  116. return 0;
  117. }
  118. /*
  119. * Set the state of an output GPIO line.
  120. */
  121. static void ks8695_gpio_set_value(struct gpio_chip *gc,
  122. unsigned int pin, int state)
  123. {
  124. unsigned long x, flags;
  125. if (pin > KS8695_GPIO_15)
  126. return;
  127. local_irq_save(flags);
  128. /* set output line state */
  129. x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
  130. if (state)
  131. x |= IOPD(pin);
  132. else
  133. x &= ~IOPD(pin);
  134. __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
  135. local_irq_restore(flags);
  136. }
  137. /*
  138. * Read the state of a GPIO line.
  139. */
  140. static int ks8695_gpio_get_value(struct gpio_chip *gc, unsigned int pin)
  141. {
  142. unsigned long x;
  143. if (pin > KS8695_GPIO_15)
  144. return -EINVAL;
  145. x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
  146. return (x & IOPD(pin)) != 0;
  147. }
  148. /*
  149. * Map GPIO line to IRQ number.
  150. */
  151. static int ks8695_gpio_to_irq(struct gpio_chip *gc, 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. /*
  158. * Map IRQ number to GPIO line.
  159. */
  160. int irq_to_gpio(unsigned int irq)
  161. {
  162. if ((irq < KS8695_IRQ_EXTERN0) || (irq > KS8695_IRQ_EXTERN3))
  163. return -EINVAL;
  164. return (irq - KS8695_IRQ_EXTERN0);
  165. }
  166. EXPORT_SYMBOL(irq_to_gpio);
  167. /* GPIOLIB interface */
  168. static struct gpio_chip ks8695_gpio_chip = {
  169. .label = "KS8695",
  170. .direction_input = ks8695_gpio_direction_input,
  171. .direction_output = ks8695_gpio_direction_output,
  172. .get = ks8695_gpio_get_value,
  173. .set = ks8695_gpio_set_value,
  174. .to_irq = ks8695_gpio_to_irq,
  175. .base = 0,
  176. .ngpio = 16,
  177. .can_sleep = 0,
  178. };
  179. /* Register the GPIOs */
  180. void ks8695_register_gpios(void)
  181. {
  182. if (gpiochip_add(&ks8695_gpio_chip))
  183. printk(KERN_ERR "Unable to register core GPIOs\n");
  184. }
  185. /* .... Debug interface ..................................................... */
  186. #ifdef CONFIG_DEBUG_FS
  187. static int ks8695_gpio_show(struct seq_file *s, void *unused)
  188. {
  189. unsigned int enable[] = { IOPC_IOEINT0EN, IOPC_IOEINT1EN, IOPC_IOEINT2EN, IOPC_IOEINT3EN, IOPC_IOTIM0EN, IOPC_IOTIM1EN };
  190. unsigned int intmask[] = { IOPC_IOEINT0TM, IOPC_IOEINT1TM, IOPC_IOEINT2TM, IOPC_IOEINT3TM };
  191. unsigned long mode, ctrl, data;
  192. int i;
  193. mode = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
  194. ctrl = __raw_readl(KS8695_GPIO_VA + KS8695_IOPC);
  195. data = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
  196. seq_printf(s, "Pin\tI/O\tFunction\tState\n\n");
  197. for (i = KS8695_GPIO_0; i <= KS8695_GPIO_15 ; i++) {
  198. seq_printf(s, "%i:\t", i);
  199. seq_printf(s, "%s\t", (mode & IOPM(i)) ? "Output" : "Input");
  200. if (i <= KS8695_GPIO_3) {
  201. if (ctrl & enable[i]) {
  202. seq_printf(s, "EXT%i ", i);
  203. switch ((ctrl & intmask[i]) >> (4 * i)) {
  204. case IOPC_TM_LOW:
  205. seq_printf(s, "(Low)"); break;
  206. case IOPC_TM_HIGH:
  207. seq_printf(s, "(High)"); break;
  208. case IOPC_TM_RISING:
  209. seq_printf(s, "(Rising)"); break;
  210. case IOPC_TM_FALLING:
  211. seq_printf(s, "(Falling)"); break;
  212. case IOPC_TM_EDGE:
  213. seq_printf(s, "(Edges)"); break;
  214. }
  215. }
  216. else
  217. seq_printf(s, "GPIO\t");
  218. }
  219. else if (i <= KS8695_GPIO_5) {
  220. if (ctrl & enable[i])
  221. seq_printf(s, "TOUT%i\t", i - KS8695_GPIO_4);
  222. else
  223. seq_printf(s, "GPIO\t");
  224. }
  225. else
  226. seq_printf(s, "GPIO\t");
  227. seq_printf(s, "\t");
  228. seq_printf(s, "%i\n", (data & IOPD(i)) ? 1 : 0);
  229. }
  230. return 0;
  231. }
  232. static int ks8695_gpio_open(struct inode *inode, struct file *file)
  233. {
  234. return single_open(file, ks8695_gpio_show, NULL);
  235. }
  236. static const struct file_operations ks8695_gpio_operations = {
  237. .open = ks8695_gpio_open,
  238. .read = seq_read,
  239. .llseek = seq_lseek,
  240. .release = single_release,
  241. };
  242. static int __init ks8695_gpio_debugfs_init(void)
  243. {
  244. /* /sys/kernel/debug/ks8695_gpio */
  245. (void) debugfs_create_file("ks8695_gpio", S_IFREG | S_IRUGO, NULL, NULL, &ks8695_gpio_operations);
  246. return 0;
  247. }
  248. postcore_initcall(ks8695_gpio_debugfs_init);
  249. #endif