irq-renesas-irqc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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/clk.h>
  20. #include <linux/init.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/ioport.h>
  25. #include <linux/io.h>
  26. #include <linux/irq.h>
  27. #include <linux/irqdomain.h>
  28. #include <linux/err.h>
  29. #include <linux/slab.h>
  30. #include <linux/module.h>
  31. #include <linux/pm_runtime.h>
  32. #define IRQC_IRQ_MAX 32 /* maximum 32 interrupts per driver instance */
  33. #define IRQC_REQ_STS 0x00 /* Interrupt Request Status Register */
  34. #define IRQC_EN_STS 0x04 /* Interrupt Enable Status Register */
  35. #define IRQC_EN_SET 0x08 /* Interrupt Enable Set Register */
  36. #define IRQC_INT_CPU_BASE(n) (0x000 + ((n) * 0x10))
  37. /* SYS-CPU vs. RT-CPU */
  38. #define DETECT_STATUS 0x100 /* IRQn Detect Status Register */
  39. #define MONITOR 0x104 /* IRQn Signal Level Monitor Register */
  40. #define HLVL_STS 0x108 /* IRQn High Level Detect Status Register */
  41. #define LLVL_STS 0x10c /* IRQn Low Level Detect Status Register */
  42. #define S_R_EDGE_STS 0x110 /* IRQn Sync Rising Edge Detect Status Reg. */
  43. #define S_F_EDGE_STS 0x114 /* IRQn Sync Falling Edge Detect Status Reg. */
  44. #define A_R_EDGE_STS 0x118 /* IRQn Async Rising Edge Detect Status Reg. */
  45. #define A_F_EDGE_STS 0x11c /* IRQn Async Falling Edge Detect Status Reg. */
  46. #define CHTEN_STS 0x120 /* Chattering Reduction Status Register */
  47. #define IRQC_CONFIG(n) (0x180 + ((n) * 0x04))
  48. /* IRQn Configuration Register */
  49. struct irqc_irq {
  50. int hw_irq;
  51. int requested_irq;
  52. struct irqc_priv *p;
  53. };
  54. struct irqc_priv {
  55. void __iomem *iomem;
  56. void __iomem *cpu_int_base;
  57. struct irqc_irq irq[IRQC_IRQ_MAX];
  58. unsigned int number_of_irqs;
  59. struct platform_device *pdev;
  60. struct irq_chip irq_chip;
  61. struct irq_domain *irq_domain;
  62. struct clk *clk;
  63. };
  64. static void irqc_dbg(struct irqc_irq *i, char *str)
  65. {
  66. dev_dbg(&i->p->pdev->dev, "%s (%d:%d)\n",
  67. str, i->requested_irq, i->hw_irq);
  68. }
  69. static void irqc_irq_enable(struct irq_data *d)
  70. {
  71. struct irqc_priv *p = irq_data_get_irq_chip_data(d);
  72. int hw_irq = irqd_to_hwirq(d);
  73. irqc_dbg(&p->irq[hw_irq], "enable");
  74. iowrite32(BIT(hw_irq), p->cpu_int_base + IRQC_EN_SET);
  75. }
  76. static void irqc_irq_disable(struct irq_data *d)
  77. {
  78. struct irqc_priv *p = irq_data_get_irq_chip_data(d);
  79. int hw_irq = irqd_to_hwirq(d);
  80. irqc_dbg(&p->irq[hw_irq], "disable");
  81. iowrite32(BIT(hw_irq), p->cpu_int_base + IRQC_EN_STS);
  82. }
  83. static unsigned char irqc_sense[IRQ_TYPE_SENSE_MASK + 1] = {
  84. [IRQ_TYPE_LEVEL_LOW] = 0x01,
  85. [IRQ_TYPE_LEVEL_HIGH] = 0x02,
  86. [IRQ_TYPE_EDGE_FALLING] = 0x04, /* Synchronous */
  87. [IRQ_TYPE_EDGE_RISING] = 0x08, /* Synchronous */
  88. [IRQ_TYPE_EDGE_BOTH] = 0x0c, /* Synchronous */
  89. };
  90. static int irqc_irq_set_type(struct irq_data *d, unsigned int type)
  91. {
  92. struct irqc_priv *p = irq_data_get_irq_chip_data(d);
  93. int hw_irq = irqd_to_hwirq(d);
  94. unsigned char value = irqc_sense[type & IRQ_TYPE_SENSE_MASK];
  95. u32 tmp;
  96. irqc_dbg(&p->irq[hw_irq], "sense");
  97. if (!value)
  98. return -EINVAL;
  99. tmp = ioread32(p->iomem + IRQC_CONFIG(hw_irq));
  100. tmp &= ~0x3f;
  101. tmp |= value;
  102. iowrite32(tmp, p->iomem + IRQC_CONFIG(hw_irq));
  103. return 0;
  104. }
  105. static int irqc_irq_set_wake(struct irq_data *d, unsigned int on)
  106. {
  107. struct irqc_priv *p = irq_data_get_irq_chip_data(d);
  108. int hw_irq = irqd_to_hwirq(d);
  109. irq_set_irq_wake(p->irq[hw_irq].requested_irq, on);
  110. if (!p->clk)
  111. return 0;
  112. if (on)
  113. clk_enable(p->clk);
  114. else
  115. clk_disable(p->clk);
  116. return 0;
  117. }
  118. static irqreturn_t irqc_irq_handler(int irq, void *dev_id)
  119. {
  120. struct irqc_irq *i = dev_id;
  121. struct irqc_priv *p = i->p;
  122. u32 bit = BIT(i->hw_irq);
  123. irqc_dbg(i, "demux1");
  124. if (ioread32(p->iomem + DETECT_STATUS) & bit) {
  125. iowrite32(bit, p->iomem + DETECT_STATUS);
  126. irqc_dbg(i, "demux2");
  127. generic_handle_irq(irq_find_mapping(p->irq_domain, i->hw_irq));
  128. return IRQ_HANDLED;
  129. }
  130. return IRQ_NONE;
  131. }
  132. /*
  133. * This lock class tells lockdep that IRQC irqs are in a different
  134. * category than their parents, so it won't report false recursion.
  135. */
  136. static struct lock_class_key irqc_irq_lock_class;
  137. static int irqc_irq_domain_map(struct irq_domain *h, unsigned int virq,
  138. irq_hw_number_t hw)
  139. {
  140. struct irqc_priv *p = h->host_data;
  141. irqc_dbg(&p->irq[hw], "map");
  142. irq_set_chip_data(virq, h->host_data);
  143. irq_set_lockdep_class(virq, &irqc_irq_lock_class);
  144. irq_set_chip_and_handler(virq, &p->irq_chip, handle_level_irq);
  145. return 0;
  146. }
  147. static const struct irq_domain_ops irqc_irq_domain_ops = {
  148. .map = irqc_irq_domain_map,
  149. .xlate = irq_domain_xlate_twocell,
  150. };
  151. static int irqc_probe(struct platform_device *pdev)
  152. {
  153. struct irqc_priv *p;
  154. struct resource *io;
  155. struct resource *irq;
  156. struct irq_chip *irq_chip;
  157. const char *name = dev_name(&pdev->dev);
  158. int ret;
  159. int k;
  160. p = kzalloc(sizeof(*p), GFP_KERNEL);
  161. if (!p) {
  162. dev_err(&pdev->dev, "failed to allocate driver data\n");
  163. ret = -ENOMEM;
  164. goto err0;
  165. }
  166. p->pdev = pdev;
  167. platform_set_drvdata(pdev, p);
  168. p->clk = devm_clk_get(&pdev->dev, NULL);
  169. if (IS_ERR(p->clk)) {
  170. dev_warn(&pdev->dev, "unable to get clock\n");
  171. p->clk = NULL;
  172. }
  173. pm_runtime_enable(&pdev->dev);
  174. pm_runtime_get_sync(&pdev->dev);
  175. /* get hold of manadatory IOMEM */
  176. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  177. if (!io) {
  178. dev_err(&pdev->dev, "not enough IOMEM resources\n");
  179. ret = -EINVAL;
  180. goto err1;
  181. }
  182. /* allow any number of IRQs between 1 and IRQC_IRQ_MAX */
  183. for (k = 0; k < IRQC_IRQ_MAX; k++) {
  184. irq = platform_get_resource(pdev, IORESOURCE_IRQ, k);
  185. if (!irq)
  186. break;
  187. p->irq[k].p = p;
  188. p->irq[k].hw_irq = k;
  189. p->irq[k].requested_irq = irq->start;
  190. }
  191. p->number_of_irqs = k;
  192. if (p->number_of_irqs < 1) {
  193. dev_err(&pdev->dev, "not enough IRQ resources\n");
  194. ret = -EINVAL;
  195. goto err1;
  196. }
  197. /* ioremap IOMEM and setup read/write callbacks */
  198. p->iomem = ioremap_nocache(io->start, resource_size(io));
  199. if (!p->iomem) {
  200. dev_err(&pdev->dev, "failed to remap IOMEM\n");
  201. ret = -ENXIO;
  202. goto err2;
  203. }
  204. p->cpu_int_base = p->iomem + IRQC_INT_CPU_BASE(0); /* SYS-SPI */
  205. irq_chip = &p->irq_chip;
  206. irq_chip->name = name;
  207. irq_chip->irq_mask = irqc_irq_disable;
  208. irq_chip->irq_unmask = irqc_irq_enable;
  209. irq_chip->irq_set_type = irqc_irq_set_type;
  210. irq_chip->irq_set_wake = irqc_irq_set_wake;
  211. irq_chip->flags = IRQCHIP_MASK_ON_SUSPEND;
  212. p->irq_domain = irq_domain_add_linear(pdev->dev.of_node,
  213. p->number_of_irqs,
  214. &irqc_irq_domain_ops, p);
  215. if (!p->irq_domain) {
  216. ret = -ENXIO;
  217. dev_err(&pdev->dev, "cannot initialize irq domain\n");
  218. goto err2;
  219. }
  220. /* request interrupts one by one */
  221. for (k = 0; k < p->number_of_irqs; k++) {
  222. if (request_irq(p->irq[k].requested_irq, irqc_irq_handler,
  223. 0, name, &p->irq[k])) {
  224. dev_err(&pdev->dev, "failed to request IRQ\n");
  225. ret = -ENOENT;
  226. goto err3;
  227. }
  228. }
  229. dev_info(&pdev->dev, "driving %d irqs\n", p->number_of_irqs);
  230. return 0;
  231. err3:
  232. while (--k >= 0)
  233. free_irq(p->irq[k].requested_irq, &p->irq[k]);
  234. irq_domain_remove(p->irq_domain);
  235. err2:
  236. iounmap(p->iomem);
  237. err1:
  238. pm_runtime_put(&pdev->dev);
  239. pm_runtime_disable(&pdev->dev);
  240. kfree(p);
  241. err0:
  242. return ret;
  243. }
  244. static int irqc_remove(struct platform_device *pdev)
  245. {
  246. struct irqc_priv *p = platform_get_drvdata(pdev);
  247. int k;
  248. for (k = 0; k < p->number_of_irqs; k++)
  249. free_irq(p->irq[k].requested_irq, &p->irq[k]);
  250. irq_domain_remove(p->irq_domain);
  251. iounmap(p->iomem);
  252. pm_runtime_put(&pdev->dev);
  253. pm_runtime_disable(&pdev->dev);
  254. kfree(p);
  255. return 0;
  256. }
  257. static const struct of_device_id irqc_dt_ids[] = {
  258. { .compatible = "renesas,irqc", },
  259. {},
  260. };
  261. MODULE_DEVICE_TABLE(of, irqc_dt_ids);
  262. static struct platform_driver irqc_device_driver = {
  263. .probe = irqc_probe,
  264. .remove = irqc_remove,
  265. .driver = {
  266. .name = "renesas_irqc",
  267. .of_match_table = irqc_dt_ids,
  268. }
  269. };
  270. static int __init irqc_init(void)
  271. {
  272. return platform_driver_register(&irqc_device_driver);
  273. }
  274. postcore_initcall(irqc_init);
  275. static void __exit irqc_exit(void)
  276. {
  277. platform_driver_unregister(&irqc_device_driver);
  278. }
  279. module_exit(irqc_exit);
  280. MODULE_AUTHOR("Magnus Damm");
  281. MODULE_DESCRIPTION("Renesas IRQC Driver");
  282. MODULE_LICENSE("GPL v2");