irq-renesas-irqc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Renesas IRQC Driver
  3. *
  4. * Copyright (C) 2013 Magnus Damm
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/init.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/ioport.h>
  24. #include <linux/io.h>
  25. #include <linux/irq.h>
  26. #include <linux/irqdomain.h>
  27. #include <linux/err.h>
  28. #include <linux/slab.h>
  29. #include <linux/module.h>
  30. #include <linux/pm_runtime.h>
  31. #define IRQC_IRQ_MAX 32 /* maximum 32 interrupts per driver instance */
  32. #define IRQC_REQ_STS 0x00 /* Interrupt Request Status Register */
  33. #define IRQC_EN_STS 0x04 /* Interrupt Enable Status Register */
  34. #define IRQC_EN_SET 0x08 /* Interrupt Enable Set Register */
  35. #define IRQC_INT_CPU_BASE(n) (0x000 + ((n) * 0x10))
  36. /* SYS-CPU vs. RT-CPU */
  37. #define DETECT_STATUS 0x100 /* IRQn Detect Status Register */
  38. #define MONITOR 0x104 /* IRQn Signal Level Monitor Register */
  39. #define HLVL_STS 0x108 /* IRQn High Level Detect Status Register */
  40. #define LLVL_STS 0x10c /* IRQn Low Level Detect Status Register */
  41. #define S_R_EDGE_STS 0x110 /* IRQn Sync Rising Edge Detect Status Reg. */
  42. #define S_F_EDGE_STS 0x114 /* IRQn Sync Falling Edge Detect Status Reg. */
  43. #define A_R_EDGE_STS 0x118 /* IRQn Async Rising Edge Detect Status Reg. */
  44. #define A_F_EDGE_STS 0x11c /* IRQn Async Falling Edge Detect Status Reg. */
  45. #define CHTEN_STS 0x120 /* Chattering Reduction Status Register */
  46. #define IRQC_CONFIG(n) (0x180 + ((n) * 0x04))
  47. /* IRQn Configuration Register */
  48. struct irqc_irq {
  49. int hw_irq;
  50. int requested_irq;
  51. struct irqc_priv *p;
  52. };
  53. struct irqc_priv {
  54. void __iomem *iomem;
  55. void __iomem *cpu_int_base;
  56. struct irqc_irq irq[IRQC_IRQ_MAX];
  57. unsigned int number_of_irqs;
  58. struct platform_device *pdev;
  59. struct irq_chip_generic *gc;
  60. struct irq_domain *irq_domain;
  61. atomic_t wakeup_path;
  62. };
  63. static struct irqc_priv *irq_data_to_priv(struct irq_data *data)
  64. {
  65. return data->domain->host_data;
  66. }
  67. static void irqc_dbg(struct irqc_irq *i, char *str)
  68. {
  69. dev_dbg(&i->p->pdev->dev, "%s (%d:%d)\n",
  70. str, i->requested_irq, i->hw_irq);
  71. }
  72. static unsigned char irqc_sense[IRQ_TYPE_SENSE_MASK + 1] = {
  73. [IRQ_TYPE_LEVEL_LOW] = 0x01,
  74. [IRQ_TYPE_LEVEL_HIGH] = 0x02,
  75. [IRQ_TYPE_EDGE_FALLING] = 0x04, /* Synchronous */
  76. [IRQ_TYPE_EDGE_RISING] = 0x08, /* Synchronous */
  77. [IRQ_TYPE_EDGE_BOTH] = 0x0c, /* Synchronous */
  78. };
  79. static int irqc_irq_set_type(struct irq_data *d, unsigned int type)
  80. {
  81. struct irqc_priv *p = irq_data_to_priv(d);
  82. int hw_irq = irqd_to_hwirq(d);
  83. unsigned char value = irqc_sense[type & IRQ_TYPE_SENSE_MASK];
  84. u32 tmp;
  85. irqc_dbg(&p->irq[hw_irq], "sense");
  86. if (!value)
  87. return -EINVAL;
  88. tmp = ioread32(p->iomem + IRQC_CONFIG(hw_irq));
  89. tmp &= ~0x3f;
  90. tmp |= value;
  91. iowrite32(tmp, p->iomem + IRQC_CONFIG(hw_irq));
  92. return 0;
  93. }
  94. static int irqc_irq_set_wake(struct irq_data *d, unsigned int on)
  95. {
  96. struct irqc_priv *p = irq_data_to_priv(d);
  97. int hw_irq = irqd_to_hwirq(d);
  98. irq_set_irq_wake(p->irq[hw_irq].requested_irq, on);
  99. if (on)
  100. atomic_inc(&p->wakeup_path);
  101. else
  102. atomic_dec(&p->wakeup_path);
  103. return 0;
  104. }
  105. static irqreturn_t irqc_irq_handler(int irq, void *dev_id)
  106. {
  107. struct irqc_irq *i = dev_id;
  108. struct irqc_priv *p = i->p;
  109. u32 bit = BIT(i->hw_irq);
  110. irqc_dbg(i, "demux1");
  111. if (ioread32(p->iomem + DETECT_STATUS) & bit) {
  112. iowrite32(bit, p->iomem + DETECT_STATUS);
  113. irqc_dbg(i, "demux2");
  114. generic_handle_irq(irq_find_mapping(p->irq_domain, i->hw_irq));
  115. return IRQ_HANDLED;
  116. }
  117. return IRQ_NONE;
  118. }
  119. static int irqc_probe(struct platform_device *pdev)
  120. {
  121. struct irqc_priv *p;
  122. struct resource *io;
  123. struct resource *irq;
  124. const char *name = dev_name(&pdev->dev);
  125. int ret;
  126. int k;
  127. p = kzalloc(sizeof(*p), GFP_KERNEL);
  128. if (!p) {
  129. dev_err(&pdev->dev, "failed to allocate driver data\n");
  130. ret = -ENOMEM;
  131. goto err0;
  132. }
  133. p->pdev = pdev;
  134. platform_set_drvdata(pdev, p);
  135. pm_runtime_enable(&pdev->dev);
  136. pm_runtime_get_sync(&pdev->dev);
  137. /* get hold of manadatory IOMEM */
  138. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  139. if (!io) {
  140. dev_err(&pdev->dev, "not enough IOMEM resources\n");
  141. ret = -EINVAL;
  142. goto err1;
  143. }
  144. /* allow any number of IRQs between 1 and IRQC_IRQ_MAX */
  145. for (k = 0; k < IRQC_IRQ_MAX; k++) {
  146. irq = platform_get_resource(pdev, IORESOURCE_IRQ, k);
  147. if (!irq)
  148. break;
  149. p->irq[k].p = p;
  150. p->irq[k].hw_irq = k;
  151. p->irq[k].requested_irq = irq->start;
  152. }
  153. p->number_of_irqs = k;
  154. if (p->number_of_irqs < 1) {
  155. dev_err(&pdev->dev, "not enough IRQ resources\n");
  156. ret = -EINVAL;
  157. goto err1;
  158. }
  159. /* ioremap IOMEM and setup read/write callbacks */
  160. p->iomem = ioremap_nocache(io->start, resource_size(io));
  161. if (!p->iomem) {
  162. dev_err(&pdev->dev, "failed to remap IOMEM\n");
  163. ret = -ENXIO;
  164. goto err2;
  165. }
  166. p->cpu_int_base = p->iomem + IRQC_INT_CPU_BASE(0); /* SYS-SPI */
  167. p->irq_domain = irq_domain_add_linear(pdev->dev.of_node,
  168. p->number_of_irqs,
  169. &irq_generic_chip_ops, p);
  170. if (!p->irq_domain) {
  171. ret = -ENXIO;
  172. dev_err(&pdev->dev, "cannot initialize irq domain\n");
  173. goto err2;
  174. }
  175. ret = irq_alloc_domain_generic_chips(p->irq_domain, p->number_of_irqs,
  176. 1, name, handle_level_irq,
  177. 0, 0, IRQ_GC_INIT_NESTED_LOCK);
  178. if (ret) {
  179. dev_err(&pdev->dev, "cannot allocate generic chip\n");
  180. goto err3;
  181. }
  182. p->gc = irq_get_domain_generic_chip(p->irq_domain, 0);
  183. p->gc->reg_base = p->cpu_int_base;
  184. p->gc->chip_types[0].regs.enable = IRQC_EN_SET;
  185. p->gc->chip_types[0].regs.disable = IRQC_EN_STS;
  186. p->gc->chip_types[0].chip.irq_mask = irq_gc_mask_disable_reg;
  187. p->gc->chip_types[0].chip.irq_unmask = irq_gc_unmask_enable_reg;
  188. p->gc->chip_types[0].chip.irq_set_type = irqc_irq_set_type;
  189. p->gc->chip_types[0].chip.irq_set_wake = irqc_irq_set_wake;
  190. p->gc->chip_types[0].chip.flags = IRQCHIP_MASK_ON_SUSPEND;
  191. /* request interrupts one by one */
  192. for (k = 0; k < p->number_of_irqs; k++) {
  193. if (request_irq(p->irq[k].requested_irq, irqc_irq_handler,
  194. 0, name, &p->irq[k])) {
  195. dev_err(&pdev->dev, "failed to request IRQ\n");
  196. ret = -ENOENT;
  197. goto err4;
  198. }
  199. }
  200. dev_info(&pdev->dev, "driving %d irqs\n", p->number_of_irqs);
  201. return 0;
  202. err4:
  203. while (--k >= 0)
  204. free_irq(p->irq[k].requested_irq, &p->irq[k]);
  205. err3:
  206. irq_domain_remove(p->irq_domain);
  207. err2:
  208. iounmap(p->iomem);
  209. err1:
  210. pm_runtime_put(&pdev->dev);
  211. pm_runtime_disable(&pdev->dev);
  212. kfree(p);
  213. err0:
  214. return ret;
  215. }
  216. static int irqc_remove(struct platform_device *pdev)
  217. {
  218. struct irqc_priv *p = platform_get_drvdata(pdev);
  219. int k;
  220. for (k = 0; k < p->number_of_irqs; k++)
  221. free_irq(p->irq[k].requested_irq, &p->irq[k]);
  222. irq_domain_remove(p->irq_domain);
  223. iounmap(p->iomem);
  224. pm_runtime_put(&pdev->dev);
  225. pm_runtime_disable(&pdev->dev);
  226. kfree(p);
  227. return 0;
  228. }
  229. static int __maybe_unused irqc_suspend(struct device *dev)
  230. {
  231. struct irqc_priv *p = dev_get_drvdata(dev);
  232. if (atomic_read(&p->wakeup_path))
  233. device_set_wakeup_path(dev);
  234. return 0;
  235. }
  236. static SIMPLE_DEV_PM_OPS(irqc_pm_ops, irqc_suspend, NULL);
  237. static const struct of_device_id irqc_dt_ids[] = {
  238. { .compatible = "renesas,irqc", },
  239. {},
  240. };
  241. MODULE_DEVICE_TABLE(of, irqc_dt_ids);
  242. static struct platform_driver irqc_device_driver = {
  243. .probe = irqc_probe,
  244. .remove = irqc_remove,
  245. .driver = {
  246. .name = "renesas_irqc",
  247. .of_match_table = irqc_dt_ids,
  248. .pm = &irqc_pm_ops,
  249. }
  250. };
  251. static int __init irqc_init(void)
  252. {
  253. return platform_driver_register(&irqc_device_driver);
  254. }
  255. postcore_initcall(irqc_init);
  256. static void __exit irqc_exit(void)
  257. {
  258. platform_driver_unregister(&irqc_device_driver);
  259. }
  260. module_exit(irqc_exit);
  261. MODULE_AUTHOR("Magnus Damm");
  262. MODULE_DESCRIPTION("Renesas IRQC Driver");
  263. MODULE_LICENSE("GPL v2");