irq-st.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2014 STMicroelectronics – All Rights Reserved
  4. *
  5. * Author: Lee Jones <lee.jones@linaro.org>
  6. *
  7. * This is a re-write of Christophe Kerello's PMU driver.
  8. */
  9. #include <dt-bindings/interrupt-controller/irq-st.h>
  10. #include <linux/err.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regmap.h>
  15. #include <linux/slab.h>
  16. #define STIH407_SYSCFG_5102 0x198
  17. #define ST_A9_IRQ_MASK 0x001FFFFF
  18. #define ST_A9_IRQ_MAX_CHANS 2
  19. #define ST_A9_IRQ_EN_CTI_0 BIT(0)
  20. #define ST_A9_IRQ_EN_CTI_1 BIT(1)
  21. #define ST_A9_IRQ_EN_PMU_0 BIT(2)
  22. #define ST_A9_IRQ_EN_PMU_1 BIT(3)
  23. #define ST_A9_IRQ_EN_PL310_L2 BIT(4)
  24. #define ST_A9_IRQ_EN_EXT_0 BIT(5)
  25. #define ST_A9_IRQ_EN_EXT_1 BIT(6)
  26. #define ST_A9_IRQ_EN_EXT_2 BIT(7)
  27. #define ST_A9_FIQ_N_SEL(dev, chan) (dev << (8 + (chan * 3)))
  28. #define ST_A9_IRQ_N_SEL(dev, chan) (dev << (14 + (chan * 3)))
  29. #define ST_A9_EXTIRQ_INV_SEL(dev) (dev << 20)
  30. struct st_irq_syscfg {
  31. struct regmap *regmap;
  32. unsigned int syscfg;
  33. unsigned int config;
  34. bool ext_inverted;
  35. };
  36. static const struct of_device_id st_irq_syscfg_match[] = {
  37. {
  38. .compatible = "st,stih407-irq-syscfg",
  39. .data = (void *)STIH407_SYSCFG_5102,
  40. },
  41. {}
  42. };
  43. static int st_irq_xlate(struct platform_device *pdev,
  44. int device, int channel, bool irq)
  45. {
  46. struct st_irq_syscfg *ddata = dev_get_drvdata(&pdev->dev);
  47. /* Set the device enable bit. */
  48. switch (device) {
  49. case ST_IRQ_SYSCFG_EXT_0:
  50. ddata->config |= ST_A9_IRQ_EN_EXT_0;
  51. break;
  52. case ST_IRQ_SYSCFG_EXT_1:
  53. ddata->config |= ST_A9_IRQ_EN_EXT_1;
  54. break;
  55. case ST_IRQ_SYSCFG_EXT_2:
  56. ddata->config |= ST_A9_IRQ_EN_EXT_2;
  57. break;
  58. case ST_IRQ_SYSCFG_CTI_0:
  59. ddata->config |= ST_A9_IRQ_EN_CTI_0;
  60. break;
  61. case ST_IRQ_SYSCFG_CTI_1:
  62. ddata->config |= ST_A9_IRQ_EN_CTI_1;
  63. break;
  64. case ST_IRQ_SYSCFG_PMU_0:
  65. ddata->config |= ST_A9_IRQ_EN_PMU_0;
  66. break;
  67. case ST_IRQ_SYSCFG_PMU_1:
  68. ddata->config |= ST_A9_IRQ_EN_PMU_1;
  69. break;
  70. case ST_IRQ_SYSCFG_pl310_L2:
  71. ddata->config |= ST_A9_IRQ_EN_PL310_L2;
  72. break;
  73. case ST_IRQ_SYSCFG_DISABLED:
  74. return 0;
  75. default:
  76. dev_err(&pdev->dev, "Unrecognised device %d\n", device);
  77. return -EINVAL;
  78. }
  79. /* Select IRQ/FIQ channel for device. */
  80. ddata->config |= irq ?
  81. ST_A9_IRQ_N_SEL(device, channel) :
  82. ST_A9_FIQ_N_SEL(device, channel);
  83. return 0;
  84. }
  85. static int st_irq_syscfg_enable(struct platform_device *pdev)
  86. {
  87. struct device_node *np = pdev->dev.of_node;
  88. struct st_irq_syscfg *ddata = dev_get_drvdata(&pdev->dev);
  89. int channels, ret, i;
  90. u32 device, invert;
  91. channels = of_property_count_u32_elems(np, "st,irq-device");
  92. if (channels != ST_A9_IRQ_MAX_CHANS) {
  93. dev_err(&pdev->dev, "st,enable-irq-device must have 2 elems\n");
  94. return -EINVAL;
  95. }
  96. channels = of_property_count_u32_elems(np, "st,fiq-device");
  97. if (channels != ST_A9_IRQ_MAX_CHANS) {
  98. dev_err(&pdev->dev, "st,enable-fiq-device must have 2 elems\n");
  99. return -EINVAL;
  100. }
  101. for (i = 0; i < ST_A9_IRQ_MAX_CHANS; i++) {
  102. of_property_read_u32_index(np, "st,irq-device", i, &device);
  103. ret = st_irq_xlate(pdev, device, i, true);
  104. if (ret)
  105. return ret;
  106. of_property_read_u32_index(np, "st,fiq-device", i, &device);
  107. ret = st_irq_xlate(pdev, device, i, false);
  108. if (ret)
  109. return ret;
  110. }
  111. /* External IRQs may be inverted. */
  112. of_property_read_u32(np, "st,invert-ext", &invert);
  113. ddata->config |= ST_A9_EXTIRQ_INV_SEL(invert);
  114. return regmap_update_bits(ddata->regmap, ddata->syscfg,
  115. ST_A9_IRQ_MASK, ddata->config);
  116. }
  117. static int st_irq_syscfg_probe(struct platform_device *pdev)
  118. {
  119. struct device_node *np = pdev->dev.of_node;
  120. struct st_irq_syscfg *ddata;
  121. ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
  122. if (!ddata)
  123. return -ENOMEM;
  124. ddata->syscfg = (unsigned int) device_get_match_data(&pdev->dev);
  125. ddata->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  126. if (IS_ERR(ddata->regmap)) {
  127. dev_err(&pdev->dev, "syscfg phandle missing\n");
  128. return PTR_ERR(ddata->regmap);
  129. }
  130. dev_set_drvdata(&pdev->dev, ddata);
  131. return st_irq_syscfg_enable(pdev);
  132. }
  133. static int __maybe_unused st_irq_syscfg_resume(struct device *dev)
  134. {
  135. struct st_irq_syscfg *ddata = dev_get_drvdata(dev);
  136. return regmap_update_bits(ddata->regmap, ddata->syscfg,
  137. ST_A9_IRQ_MASK, ddata->config);
  138. }
  139. static SIMPLE_DEV_PM_OPS(st_irq_syscfg_pm_ops, NULL, st_irq_syscfg_resume);
  140. static struct platform_driver st_irq_syscfg_driver = {
  141. .driver = {
  142. .name = "st_irq_syscfg",
  143. .pm = &st_irq_syscfg_pm_ops,
  144. .of_match_table = st_irq_syscfg_match,
  145. },
  146. .probe = st_irq_syscfg_probe,
  147. };
  148. static int __init st_irq_syscfg_init(void)
  149. {
  150. return platform_driver_register(&st_irq_syscfg_driver);
  151. }
  152. core_initcall(st_irq_syscfg_init);