irq-imx-intmux.c 9.9 KB

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