irq-st.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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_device.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regmap.h>
  15. #include <linux/slab.h>
  16. #define STIH415_SYSCFG_642 0x0a8
  17. #define STIH416_SYSCFG_7543 0x87c
  18. #define STIH407_SYSCFG_5102 0x198
  19. #define STID127_SYSCFG_734 0x088
  20. #define ST_A9_IRQ_MASK 0x001FFFFF
  21. #define ST_A9_IRQ_MAX_CHANS 2
  22. #define ST_A9_IRQ_EN_CTI_0 BIT(0)
  23. #define ST_A9_IRQ_EN_CTI_1 BIT(1)
  24. #define ST_A9_IRQ_EN_PMU_0 BIT(2)
  25. #define ST_A9_IRQ_EN_PMU_1 BIT(3)
  26. #define ST_A9_IRQ_EN_PL310_L2 BIT(4)
  27. #define ST_A9_IRQ_EN_EXT_0 BIT(5)
  28. #define ST_A9_IRQ_EN_EXT_1 BIT(6)
  29. #define ST_A9_IRQ_EN_EXT_2 BIT(7)
  30. #define ST_A9_FIQ_N_SEL(dev, chan) (dev << (8 + (chan * 3)))
  31. #define ST_A9_IRQ_N_SEL(dev, chan) (dev << (14 + (chan * 3)))
  32. #define ST_A9_EXTIRQ_INV_SEL(dev) (dev << 20)
  33. struct st_irq_syscfg {
  34. struct regmap *regmap;
  35. unsigned int syscfg;
  36. unsigned int config;
  37. bool ext_inverted;
  38. };
  39. static const struct of_device_id st_irq_syscfg_match[] = {
  40. {
  41. .compatible = "st,stih415-irq-syscfg",
  42. .data = (void *)STIH415_SYSCFG_642,
  43. },
  44. {
  45. .compatible = "st,stih416-irq-syscfg",
  46. .data = (void *)STIH416_SYSCFG_7543,
  47. },
  48. {
  49. .compatible = "st,stih407-irq-syscfg",
  50. .data = (void *)STIH407_SYSCFG_5102,
  51. },
  52. {
  53. .compatible = "st,stid127-irq-syscfg",
  54. .data = (void *)STID127_SYSCFG_734,
  55. },
  56. {}
  57. };
  58. static int st_irq_xlate(struct platform_device *pdev,
  59. int device, int channel, bool irq)
  60. {
  61. struct st_irq_syscfg *ddata = dev_get_drvdata(&pdev->dev);
  62. /* Set the device enable bit. */
  63. switch (device) {
  64. case ST_IRQ_SYSCFG_EXT_0:
  65. ddata->config |= ST_A9_IRQ_EN_EXT_0;
  66. break;
  67. case ST_IRQ_SYSCFG_EXT_1:
  68. ddata->config |= ST_A9_IRQ_EN_EXT_1;
  69. break;
  70. case ST_IRQ_SYSCFG_EXT_2:
  71. ddata->config |= ST_A9_IRQ_EN_EXT_2;
  72. break;
  73. case ST_IRQ_SYSCFG_CTI_0:
  74. ddata->config |= ST_A9_IRQ_EN_CTI_0;
  75. break;
  76. case ST_IRQ_SYSCFG_CTI_1:
  77. ddata->config |= ST_A9_IRQ_EN_CTI_1;
  78. break;
  79. case ST_IRQ_SYSCFG_PMU_0:
  80. ddata->config |= ST_A9_IRQ_EN_PMU_0;
  81. break;
  82. case ST_IRQ_SYSCFG_PMU_1:
  83. ddata->config |= ST_A9_IRQ_EN_PMU_1;
  84. break;
  85. case ST_IRQ_SYSCFG_pl310_L2:
  86. ddata->config |= ST_A9_IRQ_EN_PL310_L2;
  87. break;
  88. case ST_IRQ_SYSCFG_DISABLED:
  89. return 0;
  90. default:
  91. dev_err(&pdev->dev, "Unrecognised device %d\n", device);
  92. return -EINVAL;
  93. }
  94. /* Select IRQ/FIQ channel for device. */
  95. ddata->config |= irq ?
  96. ST_A9_IRQ_N_SEL(device, channel) :
  97. ST_A9_FIQ_N_SEL(device, channel);
  98. return 0;
  99. }
  100. static int st_irq_syscfg_enable(struct platform_device *pdev)
  101. {
  102. struct device_node *np = pdev->dev.of_node;
  103. struct st_irq_syscfg *ddata = dev_get_drvdata(&pdev->dev);
  104. int channels, ret, i;
  105. u32 device, invert;
  106. channels = of_property_count_u32_elems(np, "st,irq-device");
  107. if (channels != ST_A9_IRQ_MAX_CHANS) {
  108. dev_err(&pdev->dev, "st,enable-irq-device must have 2 elems\n");
  109. return -EINVAL;
  110. }
  111. channels = of_property_count_u32_elems(np, "st,fiq-device");
  112. if (channels != ST_A9_IRQ_MAX_CHANS) {
  113. dev_err(&pdev->dev, "st,enable-fiq-device must have 2 elems\n");
  114. return -EINVAL;
  115. }
  116. for (i = 0; i < ST_A9_IRQ_MAX_CHANS; i++) {
  117. of_property_read_u32_index(np, "st,irq-device", i, &device);
  118. ret = st_irq_xlate(pdev, device, i, true);
  119. if (ret)
  120. return ret;
  121. of_property_read_u32_index(np, "st,fiq-device", i, &device);
  122. ret = st_irq_xlate(pdev, device, i, false);
  123. if (ret)
  124. return ret;
  125. }
  126. /* External IRQs may be inverted. */
  127. of_property_read_u32(np, "st,invert-ext", &invert);
  128. ddata->config |= ST_A9_EXTIRQ_INV_SEL(invert);
  129. return regmap_update_bits(ddata->regmap, ddata->syscfg,
  130. ST_A9_IRQ_MASK, ddata->config);
  131. }
  132. static int st_irq_syscfg_probe(struct platform_device *pdev)
  133. {
  134. struct device_node *np = pdev->dev.of_node;
  135. const struct of_device_id *match;
  136. struct st_irq_syscfg *ddata;
  137. ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
  138. if (!ddata)
  139. return -ENOMEM;
  140. match = of_match_device(st_irq_syscfg_match, &pdev->dev);
  141. if (!match)
  142. return -ENODEV;
  143. ddata->syscfg = (unsigned int)match->data;
  144. ddata->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  145. if (IS_ERR(ddata->regmap)) {
  146. dev_err(&pdev->dev, "syscfg phandle missing\n");
  147. return PTR_ERR(ddata->regmap);
  148. }
  149. dev_set_drvdata(&pdev->dev, ddata);
  150. return st_irq_syscfg_enable(pdev);
  151. }
  152. static int __maybe_unused st_irq_syscfg_resume(struct device *dev)
  153. {
  154. struct st_irq_syscfg *ddata = dev_get_drvdata(dev);
  155. return regmap_update_bits(ddata->regmap, ddata->syscfg,
  156. ST_A9_IRQ_MASK, ddata->config);
  157. }
  158. static SIMPLE_DEV_PM_OPS(st_irq_syscfg_pm_ops, NULL, st_irq_syscfg_resume);
  159. static struct platform_driver st_irq_syscfg_driver = {
  160. .driver = {
  161. .name = "st_irq_syscfg",
  162. .pm = &st_irq_syscfg_pm_ops,
  163. .of_match_table = st_irq_syscfg_match,
  164. },
  165. .probe = st_irq_syscfg_probe,
  166. };
  167. static int __init st_irq_syscfg_init(void)
  168. {
  169. return platform_driver_register(&st_irq_syscfg_driver);
  170. }
  171. core_initcall(st_irq_syscfg_init);