irq-gpioint.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* linux/arch/arm/plat-s5p/irq-gpioint.c
  2. *
  3. * Copyright (c) 2010 Samsung Electronics Co., Ltd.
  4. * Author: Kyungmin Park <kyungmin.park@samsung.com>
  5. * Author: Joonyoung Shim <jy0922.shim@samsung.com>
  6. * Author: Marek Szyprowski <m.szyprowski@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/io.h>
  18. #include <linux/gpio.h>
  19. #include <mach/map.h>
  20. #include <plat/gpio-core.h>
  21. #include <plat/gpio-cfg.h>
  22. #define S5P_GPIOREG(x) (S5P_VA_GPIO + (x))
  23. #define GPIOINT_CON_OFFSET 0x700
  24. #define GPIOINT_MASK_OFFSET 0x900
  25. #define GPIOINT_PEND_OFFSET 0xA00
  26. #define GPIOINT_LEVEL_LOW 0x0
  27. #define GPIOINT_LEVEL_HIGH 0x1
  28. #define GPIOINT_EDGE_FALLING 0x2
  29. #define GPIOINT_EDGE_RISING 0x3
  30. #define GPIOINT_EDGE_BOTH 0x4
  31. static struct s3c_gpio_chip *irq_chips[S5P_GPIOINT_GROUP_MAXNR];
  32. static int s5p_gpioint_get_group(unsigned int irq)
  33. {
  34. struct gpio_chip *chip = get_irq_data(irq);
  35. struct s3c_gpio_chip *s3c_chip = container_of(chip,
  36. struct s3c_gpio_chip, chip);
  37. int group;
  38. for (group = 0; group < S5P_GPIOINT_GROUP_MAXNR; group++)
  39. if (s3c_chip == irq_chips[group])
  40. break;
  41. return group;
  42. }
  43. static int s5p_gpioint_get_offset(unsigned int irq)
  44. {
  45. struct gpio_chip *chip = get_irq_data(irq);
  46. struct s3c_gpio_chip *s3c_chip = container_of(chip,
  47. struct s3c_gpio_chip, chip);
  48. return irq - s3c_chip->irq_base;
  49. }
  50. static void s5p_gpioint_ack(unsigned int irq)
  51. {
  52. int group, offset, pend_offset;
  53. unsigned int value;
  54. group = s5p_gpioint_get_group(irq);
  55. offset = s5p_gpioint_get_offset(irq);
  56. pend_offset = group << 2;
  57. value = __raw_readl(S5P_GPIOREG(GPIOINT_PEND_OFFSET) + pend_offset);
  58. value |= 1 << offset;
  59. __raw_writel(value, S5P_GPIOREG(GPIOINT_PEND_OFFSET) + pend_offset);
  60. }
  61. static void s5p_gpioint_mask(unsigned int irq)
  62. {
  63. int group, offset, mask_offset;
  64. unsigned int value;
  65. group = s5p_gpioint_get_group(irq);
  66. offset = s5p_gpioint_get_offset(irq);
  67. mask_offset = group << 2;
  68. value = __raw_readl(S5P_GPIOREG(GPIOINT_MASK_OFFSET) + mask_offset);
  69. value |= 1 << offset;
  70. __raw_writel(value, S5P_GPIOREG(GPIOINT_MASK_OFFSET) + mask_offset);
  71. }
  72. static void s5p_gpioint_unmask(unsigned int irq)
  73. {
  74. int group, offset, mask_offset;
  75. unsigned int value;
  76. group = s5p_gpioint_get_group(irq);
  77. offset = s5p_gpioint_get_offset(irq);
  78. mask_offset = group << 2;
  79. value = __raw_readl(S5P_GPIOREG(GPIOINT_MASK_OFFSET) + mask_offset);
  80. value &= ~(1 << offset);
  81. __raw_writel(value, S5P_GPIOREG(GPIOINT_MASK_OFFSET) + mask_offset);
  82. }
  83. static void s5p_gpioint_mask_ack(unsigned int irq)
  84. {
  85. s5p_gpioint_mask(irq);
  86. s5p_gpioint_ack(irq);
  87. }
  88. static int s5p_gpioint_set_type(unsigned int irq, unsigned int type)
  89. {
  90. int group, offset, con_offset;
  91. unsigned int value;
  92. group = s5p_gpioint_get_group(irq);
  93. offset = s5p_gpioint_get_offset(irq);
  94. con_offset = group << 2;
  95. switch (type) {
  96. case IRQ_TYPE_EDGE_RISING:
  97. type = GPIOINT_EDGE_RISING;
  98. break;
  99. case IRQ_TYPE_EDGE_FALLING:
  100. type = GPIOINT_EDGE_FALLING;
  101. break;
  102. case IRQ_TYPE_EDGE_BOTH:
  103. type = GPIOINT_EDGE_BOTH;
  104. break;
  105. case IRQ_TYPE_LEVEL_HIGH:
  106. type = GPIOINT_LEVEL_HIGH;
  107. break;
  108. case IRQ_TYPE_LEVEL_LOW:
  109. type = GPIOINT_LEVEL_LOW;
  110. break;
  111. case IRQ_TYPE_NONE:
  112. default:
  113. printk(KERN_WARNING "No irq type\n");
  114. return -EINVAL;
  115. }
  116. value = __raw_readl(S5P_GPIOREG(GPIOINT_CON_OFFSET) + con_offset);
  117. value &= ~(0x7 << (offset * 0x4));
  118. value |= (type << (offset * 0x4));
  119. __raw_writel(value, S5P_GPIOREG(GPIOINT_CON_OFFSET) + con_offset);
  120. return 0;
  121. }
  122. struct irq_chip s5p_gpioint = {
  123. .name = "s5p_gpioint",
  124. .ack = s5p_gpioint_ack,
  125. .mask = s5p_gpioint_mask,
  126. .mask_ack = s5p_gpioint_mask_ack,
  127. .unmask = s5p_gpioint_unmask,
  128. .set_type = s5p_gpioint_set_type,
  129. };
  130. static void s5p_gpioint_handler(unsigned int irq, struct irq_desc *desc)
  131. {
  132. int group, offset, pend_offset, mask_offset;
  133. int real_irq;
  134. unsigned int pend, mask;
  135. for (group = 0; group < S5P_GPIOINT_GROUP_MAXNR; group++) {
  136. pend_offset = group << 2;
  137. pend = __raw_readl(S5P_GPIOREG(GPIOINT_PEND_OFFSET) +
  138. pend_offset);
  139. if (!pend)
  140. continue;
  141. mask_offset = group << 2;
  142. mask = __raw_readl(S5P_GPIOREG(GPIOINT_MASK_OFFSET) +
  143. mask_offset);
  144. pend &= ~mask;
  145. for (offset = 0; offset < 8; offset++) {
  146. if (pend & (1 << offset)) {
  147. struct s3c_gpio_chip *chip = irq_chips[group];
  148. if (chip) {
  149. real_irq = chip->irq_base + offset;
  150. generic_handle_irq(real_irq);
  151. }
  152. }
  153. }
  154. }
  155. }
  156. static __init int s5p_gpioint_add(struct s3c_gpio_chip *chip)
  157. {
  158. static int used_gpioint_groups = 0;
  159. static bool handler_registered = 0;
  160. int irq, group = chip->group;
  161. int i;
  162. if (used_gpioint_groups >= S5P_GPIOINT_GROUP_COUNT)
  163. return -ENOMEM;
  164. chip->irq_base = S5P_GPIOINT_BASE +
  165. used_gpioint_groups * S5P_GPIOINT_GROUP_SIZE;
  166. used_gpioint_groups++;
  167. if (!handler_registered) {
  168. set_irq_chained_handler(IRQ_GPIOINT, s5p_gpioint_handler);
  169. handler_registered = 1;
  170. }
  171. irq_chips[group] = chip;
  172. for (i = 0; i < chip->chip.ngpio; i++) {
  173. irq = chip->irq_base + i;
  174. set_irq_chip(irq, &s5p_gpioint);
  175. set_irq_data(irq, &chip->chip);
  176. set_irq_handler(irq, handle_level_irq);
  177. set_irq_flags(irq, IRQF_VALID);
  178. }
  179. return 0;
  180. }
  181. int __init s5p_register_gpio_interrupt(int pin)
  182. {
  183. struct s3c_gpio_chip *my_chip = s3c_gpiolib_getchip(pin);
  184. int offset, group;
  185. int ret;
  186. if (!my_chip)
  187. return -EINVAL;
  188. offset = pin - my_chip->chip.base;
  189. group = my_chip->group;
  190. /* check if the group has been already registered */
  191. if (my_chip->irq_base)
  192. return my_chip->irq_base + offset;
  193. /* register gpio group */
  194. ret = s5p_gpioint_add(my_chip);
  195. if (ret == 0) {
  196. printk(KERN_INFO "Registered interrupt support for gpio group %d.\n",
  197. group);
  198. return my_chip->irq_base + offset;
  199. }
  200. return ret;
  201. }