irq-gpioint.c 5.5 KB

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