irq-gpioint.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 <linux/slab.h>
  20. #include <mach/map.h>
  21. #include <plat/gpio-core.h>
  22. #include <plat/gpio-cfg.h>
  23. #define GPIO_BASE(chip) (((unsigned long)(chip)->base) & 0xFFFFF000u)
  24. #define CON_OFFSET 0x700
  25. #define MASK_OFFSET 0x900
  26. #define PEND_OFFSET 0xA00
  27. #define REG_OFFSET(x) ((x) << 2)
  28. struct s5p_gpioint_bank {
  29. struct list_head list;
  30. int start;
  31. int nr_groups;
  32. int irq;
  33. struct s3c_gpio_chip **chips;
  34. void (*handler)(unsigned int, struct irq_desc *);
  35. };
  36. LIST_HEAD(banks);
  37. static int s5p_gpioint_set_type(struct irq_data *d, unsigned int type)
  38. {
  39. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  40. struct irq_chip_type *ct = gc->chip_types;
  41. unsigned int shift = (d->irq - gc->irq_base) << 2;
  42. switch (type) {
  43. case IRQ_TYPE_EDGE_RISING:
  44. type = S5P_IRQ_TYPE_EDGE_RISING;
  45. break;
  46. case IRQ_TYPE_EDGE_FALLING:
  47. type = S5P_IRQ_TYPE_EDGE_FALLING;
  48. break;
  49. case IRQ_TYPE_EDGE_BOTH:
  50. type = S5P_IRQ_TYPE_EDGE_BOTH;
  51. break;
  52. case IRQ_TYPE_LEVEL_HIGH:
  53. type = S5P_IRQ_TYPE_LEVEL_HIGH;
  54. break;
  55. case IRQ_TYPE_LEVEL_LOW:
  56. type = S5P_IRQ_TYPE_LEVEL_LOW;
  57. break;
  58. case IRQ_TYPE_NONE:
  59. default:
  60. printk(KERN_WARNING "No irq type\n");
  61. return -EINVAL;
  62. }
  63. gc->type_cache &= ~(0x7 << shift);
  64. gc->type_cache |= type << shift;
  65. writel(gc->type_cache, gc->reg_base + ct->regs.type);
  66. return 0;
  67. }
  68. static void s5p_gpioint_handler(unsigned int irq, struct irq_desc *desc)
  69. {
  70. struct s5p_gpioint_bank *bank = irq_get_handler_data(irq);
  71. int group, pend_offset, mask_offset;
  72. unsigned int pend, mask;
  73. for (group = 0; group < bank->nr_groups; group++) {
  74. struct s3c_gpio_chip *chip = bank->chips[group];
  75. if (!chip)
  76. continue;
  77. pend_offset = REG_OFFSET(group);
  78. pend = __raw_readl(GPIO_BASE(chip) + PEND_OFFSET + pend_offset);
  79. if (!pend)
  80. continue;
  81. mask_offset = REG_OFFSET(group);
  82. mask = __raw_readl(GPIO_BASE(chip) + MASK_OFFSET + mask_offset);
  83. pend &= ~mask;
  84. while (pend) {
  85. int offset = fls(pend) - 1;
  86. int real_irq = chip->irq_base + offset;
  87. generic_handle_irq(real_irq);
  88. pend &= ~BIT(offset);
  89. }
  90. }
  91. }
  92. static __init int s5p_gpioint_add(struct s3c_gpio_chip *chip)
  93. {
  94. static int used_gpioint_groups = 0;
  95. int group = chip->group;
  96. struct s5p_gpioint_bank *bank = NULL;
  97. struct irq_chip_generic *gc;
  98. struct irq_chip_type *ct;
  99. if (used_gpioint_groups >= S5P_GPIOINT_GROUP_COUNT)
  100. return -ENOMEM;
  101. list_for_each_entry(bank, &banks, list) {
  102. if (group >= bank->start &&
  103. group < bank->start + bank->nr_groups)
  104. break;
  105. }
  106. if (!bank)
  107. return -EINVAL;
  108. if (!bank->handler) {
  109. bank->chips = kzalloc(sizeof(struct s3c_gpio_chip *) *
  110. bank->nr_groups, GFP_KERNEL);
  111. if (!bank->chips)
  112. return -ENOMEM;
  113. irq_set_chained_handler(bank->irq, s5p_gpioint_handler);
  114. irq_set_handler_data(bank->irq, bank);
  115. bank->handler = s5p_gpioint_handler;
  116. printk(KERN_INFO "Registered chained gpio int handler for interrupt %d.\n",
  117. bank->irq);
  118. }
  119. /*
  120. * chained GPIO irq has been successfully registered, allocate new gpio
  121. * int group and assign irq nubmers
  122. */
  123. chip->irq_base = S5P_GPIOINT_BASE +
  124. used_gpioint_groups * S5P_GPIOINT_GROUP_SIZE;
  125. used_gpioint_groups++;
  126. bank->chips[group - bank->start] = chip;
  127. gc = irq_alloc_generic_chip("s5p_gpioint", 1, chip->irq_base,
  128. (void __iomem *)GPIO_BASE(chip),
  129. handle_level_irq);
  130. if (!gc)
  131. return -ENOMEM;
  132. ct = gc->chip_types;
  133. ct->chip.irq_ack = irq_gc_ack_set_bit;
  134. ct->chip.irq_mask = irq_gc_mask_set_bit;
  135. ct->chip.irq_unmask = irq_gc_mask_clr_bit;
  136. ct->chip.irq_set_type = s5p_gpioint_set_type,
  137. ct->regs.ack = PEND_OFFSET + REG_OFFSET(chip->group);
  138. ct->regs.mask = MASK_OFFSET + REG_OFFSET(chip->group);
  139. ct->regs.type = CON_OFFSET + REG_OFFSET(chip->group);
  140. irq_setup_generic_chip(gc, IRQ_MSK(chip->chip.ngpio),
  141. IRQ_GC_INIT_MASK_CACHE,
  142. IRQ_NOREQUEST | IRQ_NOPROBE, 0);
  143. return 0;
  144. }
  145. int __init s5p_register_gpio_interrupt(int pin)
  146. {
  147. struct s3c_gpio_chip *my_chip = s3c_gpiolib_getchip(pin);
  148. int offset, group;
  149. int ret;
  150. if (!my_chip)
  151. return -EINVAL;
  152. offset = pin - my_chip->chip.base;
  153. group = my_chip->group;
  154. /* check if the group has been already registered */
  155. if (my_chip->irq_base)
  156. return my_chip->irq_base + offset;
  157. /* register gpio group */
  158. ret = s5p_gpioint_add(my_chip);
  159. if (ret == 0) {
  160. my_chip->chip.to_irq = samsung_gpiolib_to_irq;
  161. printk(KERN_INFO "Registered interrupt support for gpio group %d.\n",
  162. group);
  163. return my_chip->irq_base + offset;
  164. }
  165. return ret;
  166. }
  167. int __init s5p_register_gpioint_bank(int chain_irq, int start, int nr_groups)
  168. {
  169. struct s5p_gpioint_bank *bank;
  170. bank = kzalloc(sizeof(*bank), GFP_KERNEL);
  171. if (!bank)
  172. return -ENOMEM;
  173. bank->start = start;
  174. bank->nr_groups = nr_groups;
  175. bank->irq = chain_irq;
  176. list_add_tail(&bank->list, &banks);
  177. return 0;
  178. }