irq-ts4800.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Multiplexed-IRQs driver for TS-4800's FPGA
  3. *
  4. * Copyright (c) 2015 - Savoir-faire Linux
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/irq.h>
  13. #include <linux/irqchip.h>
  14. #include <linux/irqchip/chained_irq.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/seq_file.h>
  22. #define IRQ_MASK 0x4
  23. #define IRQ_STATUS 0x8
  24. struct ts4800_irq_data {
  25. void __iomem *base;
  26. struct platform_device *pdev;
  27. struct irq_domain *domain;
  28. };
  29. static void ts4800_irq_mask(struct irq_data *d)
  30. {
  31. struct ts4800_irq_data *data = irq_data_get_irq_chip_data(d);
  32. u16 reg = readw(data->base + IRQ_MASK);
  33. u16 mask = 1 << d->hwirq;
  34. writew(reg | mask, data->base + IRQ_MASK);
  35. }
  36. static void ts4800_irq_unmask(struct irq_data *d)
  37. {
  38. struct ts4800_irq_data *data = irq_data_get_irq_chip_data(d);
  39. u16 reg = readw(data->base + IRQ_MASK);
  40. u16 mask = 1 << d->hwirq;
  41. writew(reg & ~mask, data->base + IRQ_MASK);
  42. }
  43. static void ts4800_irq_print_chip(struct irq_data *d, struct seq_file *p)
  44. {
  45. struct ts4800_irq_data *data = irq_data_get_irq_chip_data(d);
  46. seq_printf(p, "%s", dev_name(&data->pdev->dev));
  47. }
  48. static const struct irq_chip ts4800_chip = {
  49. .irq_mask = ts4800_irq_mask,
  50. .irq_unmask = ts4800_irq_unmask,
  51. .irq_print_chip = ts4800_irq_print_chip,
  52. };
  53. static int ts4800_irqdomain_map(struct irq_domain *d, unsigned int irq,
  54. irq_hw_number_t hwirq)
  55. {
  56. struct ts4800_irq_data *data = d->host_data;
  57. irq_set_chip_and_handler(irq, &ts4800_chip, handle_simple_irq);
  58. irq_set_chip_data(irq, data);
  59. irq_set_noprobe(irq);
  60. return 0;
  61. }
  62. static const struct irq_domain_ops ts4800_ic_ops = {
  63. .map = ts4800_irqdomain_map,
  64. .xlate = irq_domain_xlate_onecell,
  65. };
  66. static void ts4800_ic_chained_handle_irq(struct irq_desc *desc)
  67. {
  68. struct ts4800_irq_data *data = irq_desc_get_handler_data(desc);
  69. struct irq_chip *chip = irq_desc_get_chip(desc);
  70. u16 status = readw(data->base + IRQ_STATUS);
  71. chained_irq_enter(chip, desc);
  72. if (unlikely(status == 0)) {
  73. handle_bad_irq(desc);
  74. goto out;
  75. }
  76. do {
  77. unsigned int bit = __ffs(status);
  78. generic_handle_domain_irq(data->domain, bit);
  79. status &= ~(1 << bit);
  80. } while (status);
  81. out:
  82. chained_irq_exit(chip, desc);
  83. }
  84. static int ts4800_ic_probe(struct platform_device *pdev)
  85. {
  86. struct device_node *node = pdev->dev.of_node;
  87. struct ts4800_irq_data *data;
  88. int parent_irq;
  89. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  90. if (!data)
  91. return -ENOMEM;
  92. data->pdev = pdev;
  93. data->base = devm_platform_ioremap_resource(pdev, 0);
  94. if (IS_ERR(data->base))
  95. return PTR_ERR(data->base);
  96. writew(0xFFFF, data->base + IRQ_MASK);
  97. parent_irq = irq_of_parse_and_map(node, 0);
  98. if (!parent_irq) {
  99. dev_err(&pdev->dev, "failed to get parent IRQ\n");
  100. return -EINVAL;
  101. }
  102. data->domain = irq_domain_add_linear(node, 8, &ts4800_ic_ops, data);
  103. if (!data->domain) {
  104. dev_err(&pdev->dev, "cannot add IRQ domain\n");
  105. return -ENOMEM;
  106. }
  107. irq_set_chained_handler_and_data(parent_irq,
  108. ts4800_ic_chained_handle_irq, data);
  109. platform_set_drvdata(pdev, data);
  110. return 0;
  111. }
  112. static int ts4800_ic_remove(struct platform_device *pdev)
  113. {
  114. struct ts4800_irq_data *data = platform_get_drvdata(pdev);
  115. irq_domain_remove(data->domain);
  116. return 0;
  117. }
  118. static const struct of_device_id ts4800_ic_of_match[] = {
  119. { .compatible = "technologic,ts4800-irqc", },
  120. {},
  121. };
  122. MODULE_DEVICE_TABLE(of, ts4800_ic_of_match);
  123. static struct platform_driver ts4800_ic_driver = {
  124. .probe = ts4800_ic_probe,
  125. .remove = ts4800_ic_remove,
  126. .driver = {
  127. .name = "ts4800-irqc",
  128. .of_match_table = ts4800_ic_of_match,
  129. },
  130. };
  131. module_platform_driver(ts4800_ic_driver);
  132. MODULE_AUTHOR("Damien Riegel <[email protected]>");
  133. MODULE_LICENSE("GPL v2");
  134. MODULE_ALIAS("platform:ts4800_irqc");