irq-imx-intmux.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright 2017 NXP
  3. /* INTMUX Block Diagram
  4. *
  5. * ________________
  6. * interrupt source # 0 +---->| |
  7. * | | |
  8. * interrupt source # 1 +++-->| |
  9. * ... | | | channel # 0 |--------->interrupt out # 0
  10. * ... | | | |
  11. * ... | | | |
  12. * interrupt source # X-1 +++-->|________________|
  13. * | | |
  14. * | | |
  15. * | | | ________________
  16. * +---->| |
  17. * | | | | |
  18. * | +-->| |
  19. * | | | | channel # 1 |--------->interrupt out # 1
  20. * | | +>| |
  21. * | | | | |
  22. * | | | |________________|
  23. * | | |
  24. * | | |
  25. * | | | ...
  26. * | | | ...
  27. * | | |
  28. * | | | ________________
  29. * +---->| |
  30. * | | | |
  31. * +-->| |
  32. * | | channel # N |--------->interrupt out # N
  33. * +>| |
  34. * | |
  35. * |________________|
  36. *
  37. *
  38. * N: Interrupt Channel Instance Number (N=7)
  39. * X: Interrupt Source Number for each channel (X=32)
  40. *
  41. * The INTMUX interrupt multiplexer has 8 channels, each channel receives 32
  42. * interrupt sources and generates 1 interrupt output.
  43. *
  44. */
  45. #include <linux/clk.h>
  46. #include <linux/interrupt.h>
  47. #include <linux/irq.h>
  48. #include <linux/irqchip/chained_irq.h>
  49. #include <linux/irqdomain.h>
  50. #include <linux/kernel.h>
  51. #include <linux/of_irq.h>
  52. #include <linux/of_platform.h>
  53. #include <linux/spinlock.h>
  54. #include <linux/pm_runtime.h>
  55. #define CHANIER(n) (0x10 + (0x40 * n))
  56. #define CHANIPR(n) (0x20 + (0x40 * n))
  57. #define CHAN_MAX_NUM 0x8
  58. struct intmux_irqchip_data {
  59. u32 saved_reg;
  60. int chanidx;
  61. int irq;
  62. struct irq_domain *domain;
  63. };
  64. struct intmux_data {
  65. raw_spinlock_t lock;
  66. void __iomem *regs;
  67. struct clk *ipg_clk;
  68. int channum;
  69. struct intmux_irqchip_data irqchip_data[];
  70. };
  71. static void imx_intmux_irq_mask(struct irq_data *d)
  72. {
  73. struct intmux_irqchip_data *irqchip_data = d->chip_data;
  74. int idx = irqchip_data->chanidx;
  75. struct intmux_data *data = container_of(irqchip_data, struct intmux_data,
  76. irqchip_data[idx]);
  77. unsigned long flags;
  78. void __iomem *reg;
  79. u32 val;
  80. raw_spin_lock_irqsave(&data->lock, flags);
  81. reg = data->regs + CHANIER(idx);
  82. val = readl_relaxed(reg);
  83. /* disable the interrupt source of this channel */
  84. val &= ~BIT(d->hwirq);
  85. writel_relaxed(val, reg);
  86. raw_spin_unlock_irqrestore(&data->lock, flags);
  87. }
  88. static void imx_intmux_irq_unmask(struct irq_data *d)
  89. {
  90. struct intmux_irqchip_data *irqchip_data = d->chip_data;
  91. int idx = irqchip_data->chanidx;
  92. struct intmux_data *data = container_of(irqchip_data, struct intmux_data,
  93. irqchip_data[idx]);
  94. unsigned long flags;
  95. void __iomem *reg;
  96. u32 val;
  97. raw_spin_lock_irqsave(&data->lock, flags);
  98. reg = data->regs + CHANIER(idx);
  99. val = readl_relaxed(reg);
  100. /* enable the interrupt source of this channel */
  101. val |= BIT(d->hwirq);
  102. writel_relaxed(val, reg);
  103. raw_spin_unlock_irqrestore(&data->lock, flags);
  104. }
  105. static struct irq_chip imx_intmux_irq_chip __ro_after_init = {
  106. .name = "intmux",
  107. .irq_mask = imx_intmux_irq_mask,
  108. .irq_unmask = imx_intmux_irq_unmask,
  109. };
  110. static int imx_intmux_irq_map(struct irq_domain *h, unsigned int irq,
  111. irq_hw_number_t hwirq)
  112. {
  113. struct intmux_irqchip_data *data = h->host_data;
  114. irq_set_chip_data(irq, data);
  115. irq_set_chip_and_handler(irq, &imx_intmux_irq_chip, handle_level_irq);
  116. return 0;
  117. }
  118. static int imx_intmux_irq_xlate(struct irq_domain *d, struct device_node *node,
  119. const u32 *intspec, unsigned int intsize,
  120. unsigned long *out_hwirq, unsigned int *out_type)
  121. {
  122. struct intmux_irqchip_data *irqchip_data = d->host_data;
  123. int idx = irqchip_data->chanidx;
  124. struct intmux_data *data = container_of(irqchip_data, struct intmux_data,
  125. irqchip_data[idx]);
  126. /*
  127. * two cells needed in interrupt specifier:
  128. * the 1st cell: hw interrupt number
  129. * the 2nd cell: channel index
  130. */
  131. if (WARN_ON(intsize != 2))
  132. return -EINVAL;
  133. if (WARN_ON(intspec[1] >= data->channum))
  134. return -EINVAL;
  135. *out_hwirq = intspec[0];
  136. *out_type = IRQ_TYPE_LEVEL_HIGH;
  137. return 0;
  138. }
  139. static int imx_intmux_irq_select(struct irq_domain *d, struct irq_fwspec *fwspec,
  140. enum irq_domain_bus_token bus_token)
  141. {
  142. struct intmux_irqchip_data *irqchip_data = d->host_data;
  143. /* Not for us */
  144. if (fwspec->fwnode != d->fwnode)
  145. return false;
  146. return irqchip_data->chanidx == fwspec->param[1];
  147. }
  148. static const struct irq_domain_ops imx_intmux_domain_ops = {
  149. .map = imx_intmux_irq_map,
  150. .xlate = imx_intmux_irq_xlate,
  151. .select = imx_intmux_irq_select,
  152. };
  153. static void imx_intmux_irq_handler(struct irq_desc *desc)
  154. {
  155. struct intmux_irqchip_data *irqchip_data = irq_desc_get_handler_data(desc);
  156. int idx = irqchip_data->chanidx;
  157. struct intmux_data *data = container_of(irqchip_data, struct intmux_data,
  158. irqchip_data[idx]);
  159. unsigned long irqstat;
  160. int pos;
  161. chained_irq_enter(irq_desc_get_chip(desc), desc);
  162. /* read the interrupt source pending status of this channel */
  163. irqstat = readl_relaxed(data->regs + CHANIPR(idx));
  164. for_each_set_bit(pos, &irqstat, 32)
  165. generic_handle_domain_irq(irqchip_data->domain, pos);
  166. chained_irq_exit(irq_desc_get_chip(desc), desc);
  167. }
  168. static int imx_intmux_probe(struct platform_device *pdev)
  169. {
  170. struct device_node *np = pdev->dev.of_node;
  171. struct irq_domain *domain;
  172. struct intmux_data *data;
  173. int channum;
  174. int i, ret;
  175. channum = platform_irq_count(pdev);
  176. if (channum == -EPROBE_DEFER) {
  177. return -EPROBE_DEFER;
  178. } else if (channum > CHAN_MAX_NUM) {
  179. dev_err(&pdev->dev, "supports up to %d multiplex channels\n",
  180. CHAN_MAX_NUM);
  181. return -EINVAL;
  182. }
  183. data = devm_kzalloc(&pdev->dev, struct_size(data, irqchip_data, channum), GFP_KERNEL);
  184. if (!data)
  185. return -ENOMEM;
  186. data->regs = devm_platform_ioremap_resource(pdev, 0);
  187. if (IS_ERR(data->regs)) {
  188. dev_err(&pdev->dev, "failed to initialize reg\n");
  189. return PTR_ERR(data->regs);
  190. }
  191. data->ipg_clk = devm_clk_get(&pdev->dev, "ipg");
  192. if (IS_ERR(data->ipg_clk))
  193. return dev_err_probe(&pdev->dev, PTR_ERR(data->ipg_clk),
  194. "failed to get ipg clk\n");
  195. data->channum = channum;
  196. raw_spin_lock_init(&data->lock);
  197. pm_runtime_get_noresume(&pdev->dev);
  198. pm_runtime_set_active(&pdev->dev);
  199. pm_runtime_enable(&pdev->dev);
  200. ret = clk_prepare_enable(data->ipg_clk);
  201. if (ret) {
  202. dev_err(&pdev->dev, "failed to enable ipg clk: %d\n", ret);
  203. return ret;
  204. }
  205. for (i = 0; i < channum; i++) {
  206. data->irqchip_data[i].chanidx = i;
  207. data->irqchip_data[i].irq = irq_of_parse_and_map(np, i);
  208. if (data->irqchip_data[i].irq <= 0) {
  209. ret = -EINVAL;
  210. dev_err(&pdev->dev, "failed to get irq\n");
  211. goto out;
  212. }
  213. domain = irq_domain_add_linear(np, 32, &imx_intmux_domain_ops,
  214. &data->irqchip_data[i]);
  215. if (!domain) {
  216. ret = -ENOMEM;
  217. dev_err(&pdev->dev, "failed to create IRQ domain\n");
  218. goto out;
  219. }
  220. data->irqchip_data[i].domain = domain;
  221. irq_domain_set_pm_device(domain, &pdev->dev);
  222. /* disable all interrupt sources of this channel firstly */
  223. writel_relaxed(0, data->regs + CHANIER(i));
  224. irq_set_chained_handler_and_data(data->irqchip_data[i].irq,
  225. imx_intmux_irq_handler,
  226. &data->irqchip_data[i]);
  227. }
  228. platform_set_drvdata(pdev, data);
  229. /*
  230. * Let pm_runtime_put() disable clock.
  231. * If CONFIG_PM is not enabled, the clock will stay powered.
  232. */
  233. pm_runtime_put(&pdev->dev);
  234. return 0;
  235. out:
  236. clk_disable_unprepare(data->ipg_clk);
  237. return ret;
  238. }
  239. static int imx_intmux_remove(struct platform_device *pdev)
  240. {
  241. struct intmux_data *data = platform_get_drvdata(pdev);
  242. int i;
  243. for (i = 0; i < data->channum; i++) {
  244. /* disable all interrupt sources of this channel */
  245. writel_relaxed(0, data->regs + CHANIER(i));
  246. irq_set_chained_handler_and_data(data->irqchip_data[i].irq,
  247. NULL, NULL);
  248. irq_domain_remove(data->irqchip_data[i].domain);
  249. }
  250. pm_runtime_disable(&pdev->dev);
  251. return 0;
  252. }
  253. #ifdef CONFIG_PM
  254. static int imx_intmux_runtime_suspend(struct device *dev)
  255. {
  256. struct intmux_data *data = dev_get_drvdata(dev);
  257. struct intmux_irqchip_data *irqchip_data;
  258. int i;
  259. for (i = 0; i < data->channum; i++) {
  260. irqchip_data = &data->irqchip_data[i];
  261. irqchip_data->saved_reg = readl_relaxed(data->regs + CHANIER(i));
  262. }
  263. clk_disable_unprepare(data->ipg_clk);
  264. return 0;
  265. }
  266. static int imx_intmux_runtime_resume(struct device *dev)
  267. {
  268. struct intmux_data *data = dev_get_drvdata(dev);
  269. struct intmux_irqchip_data *irqchip_data;
  270. int ret, i;
  271. ret = clk_prepare_enable(data->ipg_clk);
  272. if (ret) {
  273. dev_err(dev, "failed to enable ipg clk: %d\n", ret);
  274. return ret;
  275. }
  276. for (i = 0; i < data->channum; i++) {
  277. irqchip_data = &data->irqchip_data[i];
  278. writel_relaxed(irqchip_data->saved_reg, data->regs + CHANIER(i));
  279. }
  280. return 0;
  281. }
  282. #endif
  283. static const struct dev_pm_ops imx_intmux_pm_ops = {
  284. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  285. pm_runtime_force_resume)
  286. SET_RUNTIME_PM_OPS(imx_intmux_runtime_suspend,
  287. imx_intmux_runtime_resume, NULL)
  288. };
  289. static const struct of_device_id imx_intmux_id[] = {
  290. { .compatible = "fsl,imx-intmux", },
  291. { /* sentinel */ },
  292. };
  293. static struct platform_driver imx_intmux_driver = {
  294. .driver = {
  295. .name = "imx-intmux",
  296. .of_match_table = imx_intmux_id,
  297. .pm = &imx_intmux_pm_ops,
  298. },
  299. .probe = imx_intmux_probe,
  300. .remove = imx_intmux_remove,
  301. };
  302. builtin_platform_driver(imx_intmux_driver);