gpio-ks8695.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. /* GPIOLIB interface */
  159. static struct gpio_chip ks8695_gpio_chip = {
  160. .label = "KS8695",
  161. .direction_input = ks8695_gpio_direction_input,
  162. .direction_output = ks8695_gpio_direction_output,
  163. .get = ks8695_gpio_get_value,
  164. .set = ks8695_gpio_set_value,
  165. .to_irq = ks8695_gpio_to_irq,
  166. .base = 0,
  167. .ngpio = 16,
  168. .can_sleep = false,
  169. };
  170. /* Register the GPIOs */
  171. void ks8695_register_gpios(void)
  172. {
  173. if (gpiochip_add_data(&ks8695_gpio_chip, NULL))
  174. printk(KERN_ERR "Unable to register core GPIOs\n");
  175. }
  176. /* .... Debug interface ..................................................... */
  177. #ifdef CONFIG_DEBUG_FS
  178. static int ks8695_gpio_show(struct seq_file *s, void *unused)
  179. {
  180. unsigned int enable[] = { IOPC_IOEINT0EN, IOPC_IOEINT1EN, IOPC_IOEINT2EN, IOPC_IOEINT3EN, IOPC_IOTIM0EN, IOPC_IOTIM1EN };
  181. unsigned int intmask[] = { IOPC_IOEINT0TM, IOPC_IOEINT1TM, IOPC_IOEINT2TM, IOPC_IOEINT3TM };
  182. unsigned long mode, ctrl, data;
  183. int i;
  184. mode = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
  185. ctrl = __raw_readl(KS8695_GPIO_VA + KS8695_IOPC);
  186. data = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
  187. seq_printf(s, "Pin\tI/O\tFunction\tState\n\n");
  188. for (i = KS8695_GPIO_0; i <= KS8695_GPIO_15 ; i++) {
  189. seq_printf(s, "%i:\t", i);
  190. seq_printf(s, "%s\t", (mode & IOPM(i)) ? "Output" : "Input");
  191. if (i <= KS8695_GPIO_3) {
  192. if (ctrl & enable[i]) {
  193. seq_printf(s, "EXT%i ", i);
  194. switch ((ctrl & intmask[i]) >> (4 * i)) {
  195. case IOPC_TM_LOW:
  196. seq_printf(s, "(Low)"); break;
  197. case IOPC_TM_HIGH:
  198. seq_printf(s, "(High)"); break;
  199. case IOPC_TM_RISING:
  200. seq_printf(s, "(Rising)"); break;
  201. case IOPC_TM_FALLING:
  202. seq_printf(s, "(Falling)"); break;
  203. case IOPC_TM_EDGE:
  204. seq_printf(s, "(Edges)"); break;
  205. }
  206. } else
  207. seq_printf(s, "GPIO\t");
  208. } else if (i <= KS8695_GPIO_5) {
  209. if (ctrl & enable[i])
  210. seq_printf(s, "TOUT%i\t", i - KS8695_GPIO_4);
  211. else
  212. seq_printf(s, "GPIO\t");
  213. } else {
  214. seq_printf(s, "GPIO\t");
  215. }
  216. seq_printf(s, "\t");
  217. seq_printf(s, "%i\n", (data & IOPD(i)) ? 1 : 0);
  218. }
  219. return 0;
  220. }
  221. static int ks8695_gpio_open(struct inode *inode, struct file *file)
  222. {
  223. return single_open(file, ks8695_gpio_show, NULL);
  224. }
  225. static const struct file_operations ks8695_gpio_operations = {
  226. .open = ks8695_gpio_open,
  227. .read = seq_read,
  228. .llseek = seq_lseek,
  229. .release = single_release,
  230. };
  231. static int __init ks8695_gpio_debugfs_init(void)
  232. {
  233. /* /sys/kernel/debug/ks8695_gpio */
  234. (void) debugfs_create_file("ks8695_gpio", S_IFREG | S_IRUGO, NULL, NULL, &ks8695_gpio_operations);
  235. return 0;
  236. }
  237. postcore_initcall(ks8695_gpio_debugfs_init);
  238. #endif