pxa_cplds_irqs.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Intel Reference Systems cplds
  4. *
  5. * Copyright (C) 2014 Robert Jarzmik
  6. *
  7. * Cplds motherboard driver, supporting lubbock and mainstone SoC board.
  8. */
  9. #include <linux/bitops.h>
  10. #include <linux/gpio.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/irq.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/mfd/core.h>
  17. #include <linux/module.h>
  18. #include <linux/of_platform.h>
  19. #define FPGA_IRQ_MASK_EN 0x0
  20. #define FPGA_IRQ_SET_CLR 0x10
  21. #define CPLDS_NB_IRQ 32
  22. struct cplds {
  23. void __iomem *base;
  24. int irq;
  25. unsigned int irq_mask;
  26. struct gpio_desc *gpio0;
  27. struct irq_domain *irqdomain;
  28. };
  29. static irqreturn_t cplds_irq_handler(int in_irq, void *d)
  30. {
  31. struct cplds *fpga = d;
  32. unsigned long pending;
  33. unsigned int bit;
  34. do {
  35. pending = readl(fpga->base + FPGA_IRQ_SET_CLR) & fpga->irq_mask;
  36. for_each_set_bit(bit, &pending, CPLDS_NB_IRQ)
  37. generic_handle_domain_irq(fpga->irqdomain, bit);
  38. } while (pending);
  39. return IRQ_HANDLED;
  40. }
  41. static void cplds_irq_mask(struct irq_data *d)
  42. {
  43. struct cplds *fpga = irq_data_get_irq_chip_data(d);
  44. unsigned int cplds_irq = irqd_to_hwirq(d);
  45. unsigned int bit = BIT(cplds_irq);
  46. fpga->irq_mask &= ~bit;
  47. writel(fpga->irq_mask, fpga->base + FPGA_IRQ_MASK_EN);
  48. }
  49. static void cplds_irq_unmask(struct irq_data *d)
  50. {
  51. struct cplds *fpga = irq_data_get_irq_chip_data(d);
  52. unsigned int cplds_irq = irqd_to_hwirq(d);
  53. unsigned int set, bit = BIT(cplds_irq);
  54. set = readl(fpga->base + FPGA_IRQ_SET_CLR);
  55. writel(set & ~bit, fpga->base + FPGA_IRQ_SET_CLR);
  56. fpga->irq_mask |= bit;
  57. writel(fpga->irq_mask, fpga->base + FPGA_IRQ_MASK_EN);
  58. }
  59. static struct irq_chip cplds_irq_chip = {
  60. .name = "pxa_cplds",
  61. .irq_ack = cplds_irq_mask,
  62. .irq_mask = cplds_irq_mask,
  63. .irq_unmask = cplds_irq_unmask,
  64. .flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_SKIP_SET_WAKE,
  65. };
  66. static int cplds_irq_domain_map(struct irq_domain *d, unsigned int irq,
  67. irq_hw_number_t hwirq)
  68. {
  69. struct cplds *fpga = d->host_data;
  70. irq_set_chip_and_handler(irq, &cplds_irq_chip, handle_level_irq);
  71. irq_set_chip_data(irq, fpga);
  72. return 0;
  73. }
  74. static const struct irq_domain_ops cplds_irq_domain_ops = {
  75. .xlate = irq_domain_xlate_twocell,
  76. .map = cplds_irq_domain_map,
  77. };
  78. static int cplds_resume(struct platform_device *pdev)
  79. {
  80. struct cplds *fpga = platform_get_drvdata(pdev);
  81. writel(fpga->irq_mask, fpga->base + FPGA_IRQ_MASK_EN);
  82. return 0;
  83. }
  84. static int cplds_probe(struct platform_device *pdev)
  85. {
  86. struct resource *res;
  87. struct cplds *fpga;
  88. int ret;
  89. int base_irq;
  90. unsigned long irqflags = 0;
  91. fpga = devm_kzalloc(&pdev->dev, sizeof(*fpga), GFP_KERNEL);
  92. if (!fpga)
  93. return -ENOMEM;
  94. fpga->irq = platform_get_irq(pdev, 0);
  95. if (fpga->irq <= 0)
  96. return fpga->irq;
  97. base_irq = platform_get_irq(pdev, 1);
  98. if (base_irq < 0) {
  99. base_irq = 0;
  100. } else {
  101. ret = devm_irq_alloc_descs(&pdev->dev, base_irq, base_irq, CPLDS_NB_IRQ, 0);
  102. if (ret < 0)
  103. return ret;
  104. }
  105. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  106. fpga->base = devm_ioremap_resource(&pdev->dev, res);
  107. if (IS_ERR(fpga->base))
  108. return PTR_ERR(fpga->base);
  109. platform_set_drvdata(pdev, fpga);
  110. writel(fpga->irq_mask, fpga->base + FPGA_IRQ_MASK_EN);
  111. writel(0, fpga->base + FPGA_IRQ_SET_CLR);
  112. irqflags = irq_get_trigger_type(fpga->irq);
  113. ret = devm_request_irq(&pdev->dev, fpga->irq, cplds_irq_handler,
  114. irqflags, dev_name(&pdev->dev), fpga);
  115. if (ret == -ENOSYS)
  116. return -EPROBE_DEFER;
  117. if (ret) {
  118. dev_err(&pdev->dev, "couldn't request main irq%d: %d\n",
  119. fpga->irq, ret);
  120. return ret;
  121. }
  122. irq_set_irq_wake(fpga->irq, 1);
  123. if (base_irq)
  124. fpga->irqdomain = irq_domain_add_legacy(pdev->dev.of_node,
  125. CPLDS_NB_IRQ,
  126. base_irq, 0,
  127. &cplds_irq_domain_ops,
  128. fpga);
  129. else
  130. fpga->irqdomain = irq_domain_add_linear(pdev->dev.of_node,
  131. CPLDS_NB_IRQ,
  132. &cplds_irq_domain_ops,
  133. fpga);
  134. if (!fpga->irqdomain)
  135. return -ENODEV;
  136. return 0;
  137. }
  138. static int cplds_remove(struct platform_device *pdev)
  139. {
  140. struct cplds *fpga = platform_get_drvdata(pdev);
  141. irq_set_chip_and_handler(fpga->irq, NULL, NULL);
  142. return 0;
  143. }
  144. static const struct of_device_id cplds_id_table[] = {
  145. { .compatible = "intel,lubbock-cplds-irqs", },
  146. { .compatible = "intel,mainstone-cplds-irqs", },
  147. { }
  148. };
  149. MODULE_DEVICE_TABLE(of, cplds_id_table);
  150. static struct platform_driver cplds_driver = {
  151. .driver = {
  152. .name = "pxa_cplds_irqs",
  153. .of_match_table = of_match_ptr(cplds_id_table),
  154. },
  155. .probe = cplds_probe,
  156. .remove = cplds_remove,
  157. .resume = cplds_resume,
  158. };
  159. module_platform_driver(cplds_driver);
  160. MODULE_DESCRIPTION("PXA Cplds interrupts driver");
  161. MODULE_AUTHOR("Robert Jarzmik <[email protected]>");
  162. MODULE_LICENSE("GPL");