pcie-pdc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2019, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/err.h>
  7. #include <linux/init.h>
  8. #include <linux/irq.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_device.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/types.h>
  15. #include <soc/qcom/pcie-pdc.h>
  16. #define IRQ_i_CFG 0x110
  17. #define IRQ_i_CFG_IRQ_ENABLE BIT(3)
  18. #define IRQ_i_CFG_TYPE_MASK 0x7
  19. #define IRQ_i_CFG_DISTANCE 0x4
  20. #define IRQ_i_GP_IRQ_SELECT 0x4900
  21. #define IRQ_i_GP_IRQ_DISTANCE 0x14
  22. struct irq_map {
  23. u32 gpio;
  24. u32 mux;
  25. u32 irq;
  26. };
  27. struct pdc_match_data {
  28. const struct irq_map *map;
  29. u32 size;
  30. };
  31. static const struct pdc_match_data *d;
  32. static DEFINE_RAW_SPINLOCK(pdc_lock);
  33. static void __iomem *pcie_pdc_base;
  34. enum pdc_irq_config_bits {
  35. PDC_LEVEL_LOW = 0b000,
  36. PDC_EDGE_FALLING = 0b010,
  37. PDC_LEVEL_HIGH = 0b100,
  38. PDC_EDGE_RISING = 0b110,
  39. PDC_EDGE_DUAL = 0b111,
  40. };
  41. static int pdc_cfg_irq(u32 irq, u32 mux, int mux_select, unsigned int type, bool enable)
  42. {
  43. unsigned long flags;
  44. u32 value;
  45. enum pdc_irq_config_bits pdc_type;
  46. switch (type) {
  47. case IRQ_TYPE_EDGE_RISING:
  48. pdc_type = PDC_EDGE_RISING;
  49. break;
  50. case IRQ_TYPE_EDGE_FALLING:
  51. pdc_type = PDC_EDGE_FALLING;
  52. break;
  53. case IRQ_TYPE_EDGE_BOTH:
  54. pdc_type = PDC_EDGE_DUAL;
  55. break;
  56. case IRQ_TYPE_LEVEL_HIGH:
  57. pdc_type = PDC_LEVEL_HIGH;
  58. break;
  59. case IRQ_TYPE_LEVEL_LOW:
  60. pdc_type = PDC_LEVEL_LOW;
  61. break;
  62. default:
  63. WARN_ON(1);
  64. return -EINVAL;
  65. }
  66. if (enable) {
  67. value = mux;
  68. pdc_type |= IRQ_i_CFG_IRQ_ENABLE;
  69. } else {
  70. value = 0;
  71. pdc_type &= ~IRQ_i_CFG_IRQ_ENABLE;
  72. }
  73. raw_spin_lock_irqsave(&pdc_lock, flags);
  74. writel_relaxed(value, pcie_pdc_base + IRQ_i_GP_IRQ_SELECT +
  75. mux_select * IRQ_i_GP_IRQ_DISTANCE);
  76. writel_relaxed(pdc_type, pcie_pdc_base + IRQ_i_CFG + irq * IRQ_i_CFG_DISTANCE);
  77. raw_spin_unlock_irqrestore(&pdc_lock, flags);
  78. return 0;
  79. }
  80. /**
  81. * pcie_pdc_cfg_irq() - Configure the GPIO interrupt at PCIe PDC
  82. * @gpio: The wake capable GPIO number
  83. * @type: The interrupt type for GPIO
  84. * @enable: Enable or disable GPIO interrupt
  85. *
  86. * Configures the GPIO interrupt at PCIe PDC.
  87. *
  88. * Return:
  89. * * 0 - Success
  90. * * -Error - Error code
  91. */
  92. int pcie_pdc_cfg_irq(u32 gpio, unsigned int type, bool enable)
  93. {
  94. int i;
  95. if (!d)
  96. return -ENODEV;
  97. for (i = 0; i < d->size; i++) {
  98. if (gpio == d->map[i].gpio)
  99. return pdc_cfg_irq(d->map[i].irq, d->map[i].mux, i, type, enable);
  100. }
  101. return -EINVAL;
  102. }
  103. EXPORT_SYMBOL(pcie_pdc_cfg_irq);
  104. static int qcom_pcie_pdc_probe(struct platform_device *pdev)
  105. {
  106. pcie_pdc_base = devm_platform_ioremap_resource(pdev, 0);
  107. if (!pcie_pdc_base)
  108. return -ENXIO;
  109. d = of_device_get_match_data(&pdev->dev);
  110. if (!d)
  111. return -EINVAL;
  112. return 0;
  113. }
  114. static const struct irq_map pineapple_irq_map[] = {
  115. { 95, 67, 10 },
  116. { 98, 50, 11 },
  117. };
  118. static const struct pdc_match_data pineapple_pdc_match_data = {
  119. .map = pineapple_irq_map,
  120. .size = ARRAY_SIZE(pineapple_irq_map),
  121. };
  122. static const struct irq_map cliffs_irq_map[] = {
  123. { 118, 95, 10 },
  124. };
  125. static const struct pdc_match_data cliffs_pdc_match_data = {
  126. .map = cliffs_irq_map,
  127. .size = ARRAY_SIZE(cliffs_irq_map),
  128. };
  129. static const struct of_device_id qcom_pcie_pdc_match_table[] = {
  130. { .compatible = "qcom,pineapple-pcie-pdc", .data = &pineapple_pdc_match_data },
  131. { .compatible = "qcom,cliffs-pcie-pdc", .data = &cliffs_pdc_match_data },
  132. {}
  133. };
  134. MODULE_DEVICE_TABLE(of, qcom_pcie_pdc_match_table);
  135. static struct platform_driver qcom_pcie_pdc_driver = {
  136. .probe = qcom_pcie_pdc_probe,
  137. .driver = {
  138. .name = "qcom-pcie-pdc",
  139. .of_match_table = qcom_pcie_pdc_match_table,
  140. },
  141. };
  142. module_platform_driver(qcom_pcie_pdc_driver);
  143. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Power Domain Controller for PCIe");
  144. MODULE_LICENSE("GPL");