irq-gic-pm.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2016 NVIDIA CORPORATION, All Rights Reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/clk.h>
  7. #include <linux/of_device.h>
  8. #include <linux/of_irq.h>
  9. #include <linux/irqchip/arm-gic.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/pm_runtime.h>
  12. #include <linux/slab.h>
  13. struct gic_clk_data {
  14. unsigned int num_clocks;
  15. const char *const *clocks;
  16. };
  17. struct gic_chip_pm {
  18. struct gic_chip_data *chip_data;
  19. const struct gic_clk_data *clk_data;
  20. struct clk_bulk_data *clks;
  21. };
  22. static int gic_runtime_resume(struct device *dev)
  23. {
  24. struct gic_chip_pm *chip_pm = dev_get_drvdata(dev);
  25. struct gic_chip_data *gic = chip_pm->chip_data;
  26. const struct gic_clk_data *data = chip_pm->clk_data;
  27. int ret;
  28. ret = clk_bulk_prepare_enable(data->num_clocks, chip_pm->clks);
  29. if (ret)
  30. return ret;
  31. /*
  32. * On the very first resume, the pointer to chip_pm->chip_data
  33. * will be NULL and this is intentional, because we do not
  34. * want to restore the GIC on the very first resume. So if
  35. * the pointer is not valid just return.
  36. */
  37. if (!gic)
  38. return 0;
  39. gic_dist_restore(gic);
  40. gic_cpu_restore(gic);
  41. return 0;
  42. }
  43. static int gic_runtime_suspend(struct device *dev)
  44. {
  45. struct gic_chip_pm *chip_pm = dev_get_drvdata(dev);
  46. struct gic_chip_data *gic = chip_pm->chip_data;
  47. const struct gic_clk_data *data = chip_pm->clk_data;
  48. gic_dist_save(gic);
  49. gic_cpu_save(gic);
  50. clk_bulk_disable_unprepare(data->num_clocks, chip_pm->clks);
  51. return 0;
  52. }
  53. static int gic_probe(struct platform_device *pdev)
  54. {
  55. struct device *dev = &pdev->dev;
  56. const struct gic_clk_data *data;
  57. struct gic_chip_pm *chip_pm;
  58. int ret, irq, i;
  59. data = of_device_get_match_data(&pdev->dev);
  60. if (!data) {
  61. dev_err(&pdev->dev, "no device match found\n");
  62. return -ENODEV;
  63. }
  64. chip_pm = devm_kzalloc(dev, sizeof(*chip_pm), GFP_KERNEL);
  65. if (!chip_pm)
  66. return -ENOMEM;
  67. irq = irq_of_parse_and_map(dev->of_node, 0);
  68. if (!irq) {
  69. dev_err(dev, "no parent interrupt found!\n");
  70. return -EINVAL;
  71. }
  72. chip_pm->clks = devm_kcalloc(dev, data->num_clocks,
  73. sizeof(*chip_pm->clks), GFP_KERNEL);
  74. if (!chip_pm->clks)
  75. return -ENOMEM;
  76. for (i = 0; i < data->num_clocks; i++)
  77. chip_pm->clks[i].id = data->clocks[i];
  78. ret = devm_clk_bulk_get(dev, data->num_clocks, chip_pm->clks);
  79. if (ret)
  80. goto irq_dispose;
  81. chip_pm->clk_data = data;
  82. dev_set_drvdata(dev, chip_pm);
  83. pm_runtime_enable(dev);
  84. ret = pm_runtime_resume_and_get(dev);
  85. if (ret < 0)
  86. goto rpm_disable;
  87. ret = gic_of_init_child(dev, &chip_pm->chip_data, irq);
  88. if (ret)
  89. goto rpm_put;
  90. pm_runtime_put(dev);
  91. dev_info(dev, "GIC IRQ controller registered\n");
  92. return 0;
  93. rpm_put:
  94. pm_runtime_put_sync(dev);
  95. rpm_disable:
  96. pm_runtime_disable(dev);
  97. irq_dispose:
  98. irq_dispose_mapping(irq);
  99. return ret;
  100. }
  101. static const struct dev_pm_ops gic_pm_ops = {
  102. SET_RUNTIME_PM_OPS(gic_runtime_suspend,
  103. gic_runtime_resume, NULL)
  104. SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  105. pm_runtime_force_resume)
  106. };
  107. static const char * const gic400_clocks[] = {
  108. "clk",
  109. };
  110. static const struct gic_clk_data gic400_data = {
  111. .num_clocks = ARRAY_SIZE(gic400_clocks),
  112. .clocks = gic400_clocks,
  113. };
  114. static const struct of_device_id gic_match[] = {
  115. { .compatible = "nvidia,tegra210-agic", .data = &gic400_data },
  116. {},
  117. };
  118. MODULE_DEVICE_TABLE(of, gic_match);
  119. static struct platform_driver gic_driver = {
  120. .probe = gic_probe,
  121. .driver = {
  122. .name = "gic",
  123. .of_match_table = gic_match,
  124. .pm = &gic_pm_ops,
  125. }
  126. };
  127. builtin_platform_driver(gic_driver);