pxa_cplds_irqs.c 4.8 KB

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