gpio-ks8695.c 7.4 KB

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