irq-gpioint.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. static struct s3c_gpio_chip *irq_chips[S5P_GPIOINT_GROUP_MAXNR];
  27. static int s5p_gpioint_get_group(unsigned int irq)
  28. {
  29. struct gpio_chip *chip = get_irq_data(irq);
  30. struct s3c_gpio_chip *s3c_chip = container_of(chip,
  31. struct s3c_gpio_chip, chip);
  32. int group;
  33. for (group = 0; group < S5P_GPIOINT_GROUP_MAXNR; group++)
  34. if (s3c_chip == irq_chips[group])
  35. break;
  36. return group;
  37. }
  38. static int s5p_gpioint_get_offset(unsigned int irq)
  39. {
  40. struct gpio_chip *chip = get_irq_data(irq);
  41. struct s3c_gpio_chip *s3c_chip = container_of(chip,
  42. struct s3c_gpio_chip, chip);
  43. return irq - s3c_chip->irq_base;
  44. }
  45. static void s5p_gpioint_ack(unsigned int irq)
  46. {
  47. int group, offset, pend_offset;
  48. unsigned int value;
  49. group = s5p_gpioint_get_group(irq);
  50. offset = s5p_gpioint_get_offset(irq);
  51. pend_offset = group << 2;
  52. value = __raw_readl(S5P_GPIOREG(GPIOINT_PEND_OFFSET) + pend_offset);
  53. value |= 1 << offset;
  54. __raw_writel(value, S5P_GPIOREG(GPIOINT_PEND_OFFSET) + pend_offset);
  55. }
  56. static void s5p_gpioint_mask(unsigned int irq)
  57. {
  58. int group, offset, mask_offset;
  59. unsigned int value;
  60. group = s5p_gpioint_get_group(irq);
  61. offset = s5p_gpioint_get_offset(irq);
  62. mask_offset = group << 2;
  63. value = __raw_readl(S5P_GPIOREG(GPIOINT_MASK_OFFSET) + mask_offset);
  64. value |= 1 << offset;
  65. __raw_writel(value, S5P_GPIOREG(GPIOINT_MASK_OFFSET) + mask_offset);
  66. }
  67. static void s5p_gpioint_unmask(unsigned int irq)
  68. {
  69. int group, offset, mask_offset;
  70. unsigned int value;
  71. group = s5p_gpioint_get_group(irq);
  72. offset = s5p_gpioint_get_offset(irq);
  73. mask_offset = group << 2;
  74. value = __raw_readl(S5P_GPIOREG(GPIOINT_MASK_OFFSET) + mask_offset);
  75. value &= ~(1 << offset);
  76. __raw_writel(value, S5P_GPIOREG(GPIOINT_MASK_OFFSET) + mask_offset);
  77. }
  78. static void s5p_gpioint_mask_ack(unsigned int irq)
  79. {
  80. s5p_gpioint_mask(irq);
  81. s5p_gpioint_ack(irq);
  82. }
  83. static int s5p_gpioint_set_type(unsigned int irq, unsigned int type)
  84. {
  85. int group, offset, con_offset;
  86. unsigned int value;
  87. group = s5p_gpioint_get_group(irq);
  88. offset = s5p_gpioint_get_offset(irq);
  89. con_offset = group << 2;
  90. switch (type) {
  91. case IRQ_TYPE_EDGE_RISING:
  92. type = S5P_IRQ_TYPE_EDGE_RISING;
  93. break;
  94. case IRQ_TYPE_EDGE_FALLING:
  95. type = S5P_IRQ_TYPE_EDGE_FALLING;
  96. break;
  97. case IRQ_TYPE_EDGE_BOTH:
  98. type = S5P_IRQ_TYPE_EDGE_BOTH;
  99. break;
  100. case IRQ_TYPE_LEVEL_HIGH:
  101. type = S5P_IRQ_TYPE_LEVEL_HIGH;
  102. break;
  103. case IRQ_TYPE_LEVEL_LOW:
  104. type = S5P_IRQ_TYPE_LEVEL_LOW;
  105. break;
  106. case IRQ_TYPE_NONE:
  107. default:
  108. printk(KERN_WARNING "No irq type\n");
  109. return -EINVAL;
  110. }
  111. value = __raw_readl(S5P_GPIOREG(GPIOINT_CON_OFFSET) + con_offset);
  112. value &= ~(0x7 << (offset * 0x4));
  113. value |= (type << (offset * 0x4));
  114. __raw_writel(value, S5P_GPIOREG(GPIOINT_CON_OFFSET) + con_offset);
  115. return 0;
  116. }
  117. struct irq_chip s5p_gpioint = {
  118. .name = "s5p_gpioint",
  119. .ack = s5p_gpioint_ack,
  120. .mask = s5p_gpioint_mask,
  121. .mask_ack = s5p_gpioint_mask_ack,
  122. .unmask = s5p_gpioint_unmask,
  123. .set_type = s5p_gpioint_set_type,
  124. };
  125. static void s5p_gpioint_handler(unsigned int irq, struct irq_desc *desc)
  126. {
  127. int group, offset, pend_offset, mask_offset;
  128. int real_irq;
  129. unsigned int pend, mask;
  130. for (group = 0; group < S5P_GPIOINT_GROUP_MAXNR; group++) {
  131. pend_offset = group << 2;
  132. pend = __raw_readl(S5P_GPIOREG(GPIOINT_PEND_OFFSET) +
  133. pend_offset);
  134. if (!pend)
  135. continue;
  136. mask_offset = group << 2;
  137. mask = __raw_readl(S5P_GPIOREG(GPIOINT_MASK_OFFSET) +
  138. mask_offset);
  139. pend &= ~mask;
  140. for (offset = 0; offset < 8; offset++) {
  141. if (pend & (1 << offset)) {
  142. struct s3c_gpio_chip *chip = irq_chips[group];
  143. if (chip) {
  144. real_irq = chip->irq_base + offset;
  145. generic_handle_irq(real_irq);
  146. }
  147. }
  148. }
  149. }
  150. }
  151. static __init int s5p_gpioint_add(struct s3c_gpio_chip *chip)
  152. {
  153. static int used_gpioint_groups = 0;
  154. static bool handler_registered = 0;
  155. int irq, group = chip->group;
  156. int i;
  157. if (used_gpioint_groups >= S5P_GPIOINT_GROUP_COUNT)
  158. return -ENOMEM;
  159. chip->irq_base = S5P_GPIOINT_BASE +
  160. used_gpioint_groups * S5P_GPIOINT_GROUP_SIZE;
  161. used_gpioint_groups++;
  162. if (!handler_registered) {
  163. set_irq_chained_handler(IRQ_GPIOINT, s5p_gpioint_handler);
  164. handler_registered = 1;
  165. }
  166. irq_chips[group] = chip;
  167. for (i = 0; i < chip->chip.ngpio; i++) {
  168. irq = chip->irq_base + i;
  169. set_irq_chip(irq, &s5p_gpioint);
  170. set_irq_data(irq, &chip->chip);
  171. set_irq_handler(irq, handle_level_irq);
  172. set_irq_flags(irq, IRQF_VALID);
  173. }
  174. return 0;
  175. }
  176. int __init s5p_register_gpio_interrupt(int pin)
  177. {
  178. struct s3c_gpio_chip *my_chip = s3c_gpiolib_getchip(pin);
  179. int offset, group;
  180. int ret;
  181. if (!my_chip)
  182. return -EINVAL;
  183. offset = pin - my_chip->chip.base;
  184. group = my_chip->group;
  185. /* check if the group has been already registered */
  186. if (my_chip->irq_base)
  187. return my_chip->irq_base + offset;
  188. /* register gpio group */
  189. ret = s5p_gpioint_add(my_chip);
  190. if (ret == 0) {
  191. my_chip->chip.to_irq = samsung_gpiolib_to_irq;
  192. printk(KERN_INFO "Registered interrupt support for gpio group %d.\n",
  193. group);
  194. return my_chip->irq_base + offset;
  195. }
  196. return ret;
  197. }