irq-gpioint.c 5.6 KB

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