irq-st.c 5.2 KB

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