irq-mtk-sysirq.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2014 MediaTek Inc.
  3. * Author: Joe.C <yingjoe.chen@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/irq.h>
  15. #include <linux/irqchip.h>
  16. #include <linux/irqdomain.h>
  17. #include <linux/of.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_address.h>
  20. #include <linux/io.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. struct mtk_sysirq_chip_data {
  24. spinlock_t lock;
  25. void __iomem *intpol_base;
  26. };
  27. static int mtk_sysirq_set_type(struct irq_data *data, unsigned int type)
  28. {
  29. irq_hw_number_t hwirq = data->hwirq;
  30. struct mtk_sysirq_chip_data *chip_data = data->chip_data;
  31. u32 offset, reg_index, value;
  32. unsigned long flags;
  33. int ret;
  34. offset = hwirq & 0x1f;
  35. reg_index = hwirq >> 5;
  36. spin_lock_irqsave(&chip_data->lock, flags);
  37. value = readl_relaxed(chip_data->intpol_base + reg_index * 4);
  38. if (type == IRQ_TYPE_LEVEL_LOW || type == IRQ_TYPE_EDGE_FALLING) {
  39. if (type == IRQ_TYPE_LEVEL_LOW)
  40. type = IRQ_TYPE_LEVEL_HIGH;
  41. else
  42. type = IRQ_TYPE_EDGE_RISING;
  43. value |= (1 << offset);
  44. } else {
  45. value &= ~(1 << offset);
  46. }
  47. writel(value, chip_data->intpol_base + reg_index * 4);
  48. data = data->parent_data;
  49. ret = data->chip->irq_set_type(data, type);
  50. spin_unlock_irqrestore(&chip_data->lock, flags);
  51. return ret;
  52. }
  53. static struct irq_chip mtk_sysirq_chip = {
  54. .name = "MT_SYSIRQ",
  55. .irq_mask = irq_chip_mask_parent,
  56. .irq_unmask = irq_chip_unmask_parent,
  57. .irq_eoi = irq_chip_eoi_parent,
  58. .irq_set_type = mtk_sysirq_set_type,
  59. .irq_retrigger = irq_chip_retrigger_hierarchy,
  60. .irq_set_affinity = irq_chip_set_affinity_parent,
  61. };
  62. static int mtk_sysirq_domain_translate(struct irq_domain *d,
  63. struct irq_fwspec *fwspec,
  64. unsigned long *hwirq,
  65. unsigned int *type)
  66. {
  67. if (is_of_node(fwspec->fwnode)) {
  68. if (fwspec->param_count != 3)
  69. return -EINVAL;
  70. /* No PPI should point to this domain */
  71. if (fwspec->param[0] != 0)
  72. return -EINVAL;
  73. *hwirq = fwspec->param[1];
  74. *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
  75. return 0;
  76. }
  77. return -EINVAL;
  78. }
  79. static int mtk_sysirq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  80. unsigned int nr_irqs, void *arg)
  81. {
  82. int i;
  83. irq_hw_number_t hwirq;
  84. struct irq_fwspec *fwspec = arg;
  85. struct irq_fwspec gic_fwspec = *fwspec;
  86. if (fwspec->param_count != 3)
  87. return -EINVAL;
  88. /* sysirq doesn't support PPI */
  89. if (fwspec->param[0])
  90. return -EINVAL;
  91. hwirq = fwspec->param[1];
  92. for (i = 0; i < nr_irqs; i++)
  93. irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
  94. &mtk_sysirq_chip,
  95. domain->host_data);
  96. gic_fwspec.fwnode = domain->parent->fwnode;
  97. return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, &gic_fwspec);
  98. }
  99. static const struct irq_domain_ops sysirq_domain_ops = {
  100. .translate = mtk_sysirq_domain_translate,
  101. .alloc = mtk_sysirq_domain_alloc,
  102. .free = irq_domain_free_irqs_common,
  103. };
  104. static int __init mtk_sysirq_of_init(struct device_node *node,
  105. struct device_node *parent)
  106. {
  107. struct irq_domain *domain, *domain_parent;
  108. struct mtk_sysirq_chip_data *chip_data;
  109. int ret, size, intpol_num;
  110. struct resource res;
  111. domain_parent = irq_find_host(parent);
  112. if (!domain_parent) {
  113. pr_err("mtk_sysirq: interrupt-parent not found\n");
  114. return -EINVAL;
  115. }
  116. ret = of_address_to_resource(node, 0, &res);
  117. if (ret)
  118. return ret;
  119. chip_data = kzalloc(sizeof(*chip_data), GFP_KERNEL);
  120. if (!chip_data)
  121. return -ENOMEM;
  122. size = resource_size(&res);
  123. intpol_num = size * 8;
  124. chip_data->intpol_base = ioremap(res.start, size);
  125. if (!chip_data->intpol_base) {
  126. pr_err("mtk_sysirq: unable to map sysirq register\n");
  127. ret = -ENXIO;
  128. goto out_free;
  129. }
  130. domain = irq_domain_add_hierarchy(domain_parent, 0, intpol_num, node,
  131. &sysirq_domain_ops, chip_data);
  132. if (!domain) {
  133. ret = -ENOMEM;
  134. goto out_unmap;
  135. }
  136. spin_lock_init(&chip_data->lock);
  137. return 0;
  138. out_unmap:
  139. iounmap(chip_data->intpol_base);
  140. out_free:
  141. kfree(chip_data);
  142. return ret;
  143. }
  144. IRQCHIP_DECLARE(mtk_sysirq, "mediatek,mt6577-sysirq", mtk_sysirq_of_init);