s5p-irq-gpioint.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2010 Samsung Electronics Co., Ltd.
  3. * Author: Kyungmin Park <kyungmin.park@samsung.com>
  4. * Author: Joonyoung Shim <jy0922.shim@samsung.com>
  5. * Author: Marek Szyprowski <m.szyprowski@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/io.h>
  17. #include <linux/gpio.h>
  18. #include <linux/slab.h>
  19. #include <mach/map.h>
  20. #include <plat/gpio-core.h>
  21. #include <plat/gpio-cfg.h>
  22. #include <asm/mach/irq.h>
  23. #define GPIO_BASE(chip) ((void __iomem *)((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 samsung_gpio_chip **chips;
  34. void (*handler)(unsigned int, struct irq_desc *);
  35. };
  36. static 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. struct irq_chip *chip = irq_get_chip(irq);
  74. chained_irq_enter(chip, desc);
  75. for (group = 0; group < bank->nr_groups; group++) {
  76. struct samsung_gpio_chip *chip = bank->chips[group];
  77. if (!chip)
  78. continue;
  79. pend_offset = REG_OFFSET(group);
  80. pend = __raw_readl(GPIO_BASE(chip) + PEND_OFFSET + pend_offset);
  81. if (!pend)
  82. continue;
  83. mask_offset = REG_OFFSET(group);
  84. mask = __raw_readl(GPIO_BASE(chip) + MASK_OFFSET + mask_offset);
  85. pend &= ~mask;
  86. while (pend) {
  87. int offset = fls(pend) - 1;
  88. int real_irq = chip->irq_base + offset;
  89. generic_handle_irq(real_irq);
  90. pend &= ~BIT(offset);
  91. }
  92. }
  93. chained_irq_exit(chip, desc);
  94. }
  95. static __init int s5p_gpioint_add(struct samsung_gpio_chip *chip)
  96. {
  97. static int used_gpioint_groups = 0;
  98. int group = chip->group;
  99. struct s5p_gpioint_bank *b, *bank = NULL;
  100. struct irq_chip_generic *gc;
  101. struct irq_chip_type *ct;
  102. if (used_gpioint_groups >= S5P_GPIOINT_GROUP_COUNT)
  103. return -ENOMEM;
  104. list_for_each_entry(b, &banks, list) {
  105. if (group >= b->start && group < b->start + b->nr_groups) {
  106. bank = b;
  107. break;
  108. }
  109. }
  110. if (!bank)
  111. return -EINVAL;
  112. if (!bank->handler) {
  113. bank->chips = kzalloc(sizeof(struct samsung_gpio_chip *) *
  114. bank->nr_groups, GFP_KERNEL);
  115. if (!bank->chips)
  116. return -ENOMEM;
  117. irq_set_chained_handler(bank->irq, s5p_gpioint_handler);
  118. irq_set_handler_data(bank->irq, bank);
  119. bank->handler = s5p_gpioint_handler;
  120. printk(KERN_INFO "Registered chained gpio int handler for interrupt %d.\n",
  121. bank->irq);
  122. }
  123. /*
  124. * chained GPIO irq has been successfully registered, allocate new gpio
  125. * int group and assign irq nubmers
  126. */
  127. chip->irq_base = S5P_GPIOINT_BASE +
  128. used_gpioint_groups * S5P_GPIOINT_GROUP_SIZE;
  129. used_gpioint_groups++;
  130. bank->chips[group - bank->start] = chip;
  131. gc = irq_alloc_generic_chip("s5p_gpioint", 1, chip->irq_base,
  132. GPIO_BASE(chip),
  133. handle_level_irq);
  134. if (!gc)
  135. return -ENOMEM;
  136. ct = gc->chip_types;
  137. ct->chip.irq_ack = irq_gc_ack_set_bit;
  138. ct->chip.irq_mask = irq_gc_mask_set_bit;
  139. ct->chip.irq_unmask = irq_gc_mask_clr_bit;
  140. ct->chip.irq_set_type = s5p_gpioint_set_type,
  141. ct->regs.ack = PEND_OFFSET + REG_OFFSET(group - bank->start);
  142. ct->regs.mask = MASK_OFFSET + REG_OFFSET(group - bank->start);
  143. ct->regs.type = CON_OFFSET + REG_OFFSET(group - bank->start);
  144. irq_setup_generic_chip(gc, IRQ_MSK(chip->chip.ngpio),
  145. IRQ_GC_INIT_MASK_CACHE,
  146. IRQ_NOREQUEST | IRQ_NOPROBE, 0);
  147. return 0;
  148. }
  149. int __init s5p_register_gpio_interrupt(int pin)
  150. {
  151. struct samsung_gpio_chip *my_chip = samsung_gpiolib_getchip(pin);
  152. int offset, group;
  153. int ret;
  154. if (!my_chip)
  155. return -EINVAL;
  156. offset = pin - my_chip->chip.base;
  157. group = my_chip->group;
  158. /* check if the group has been already registered */
  159. if (my_chip->irq_base)
  160. goto success;
  161. /* register gpio group */
  162. ret = s5p_gpioint_add(my_chip);
  163. if (ret == 0) {
  164. my_chip->chip.to_irq = samsung_gpiolib_to_irq;
  165. printk(KERN_INFO "Registered interrupt support for gpio group %d.\n",
  166. group);
  167. goto success;
  168. }
  169. return ret;
  170. success:
  171. my_chip->bitmap_gpio_int |= BIT(offset);
  172. return my_chip->irq_base + offset;
  173. }
  174. int __init s5p_register_gpioint_bank(int chain_irq, int start, int nr_groups)
  175. {
  176. struct s5p_gpioint_bank *bank;
  177. bank = kzalloc(sizeof(*bank), GFP_KERNEL);
  178. if (!bank)
  179. return -ENOMEM;
  180. bank->start = start;
  181. bank->nr_groups = nr_groups;
  182. bank->irq = chain_irq;
  183. list_add_tail(&bank->list, &banks);
  184. return 0;
  185. }