irq-gic-pm.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. dev_err(dev, "clk_enable failed: %d\n", ret);
  31. return ret;
  32. }
  33. /*
  34. * On the very first resume, the pointer to chip_pm->chip_data
  35. * will be NULL and this is intentional, because we do not
  36. * want to restore the GIC on the very first resume. So if
  37. * the pointer is not valid just return.
  38. */
  39. if (!gic)
  40. return 0;
  41. gic_dist_restore(gic);
  42. gic_cpu_restore(gic);
  43. return 0;
  44. }
  45. static int gic_runtime_suspend(struct device *dev)
  46. {
  47. struct gic_chip_pm *chip_pm = dev_get_drvdata(dev);
  48. struct gic_chip_data *gic = chip_pm->chip_data;
  49. const struct gic_clk_data *data = chip_pm->clk_data;
  50. gic_dist_save(gic);
  51. gic_cpu_save(gic);
  52. clk_bulk_disable_unprepare(data->num_clocks, chip_pm->clks);
  53. return 0;
  54. }
  55. static int gic_probe(struct platform_device *pdev)
  56. {
  57. struct device *dev = &pdev->dev;
  58. const struct gic_clk_data *data;
  59. struct gic_chip_pm *chip_pm;
  60. int ret, irq, i;
  61. data = of_device_get_match_data(&pdev->dev);
  62. if (!data) {
  63. dev_err(&pdev->dev, "no device match found\n");
  64. return -ENODEV;
  65. }
  66. chip_pm = devm_kzalloc(dev, sizeof(*chip_pm), GFP_KERNEL);
  67. if (!chip_pm)
  68. return -ENOMEM;
  69. irq = irq_of_parse_and_map(dev->of_node, 0);
  70. if (!irq) {
  71. dev_err(dev, "no parent interrupt found!\n");
  72. return -EINVAL;
  73. }
  74. chip_pm->clks = devm_kcalloc(dev, data->num_clocks,
  75. sizeof(*chip_pm->clks), GFP_KERNEL);
  76. if (!chip_pm->clks)
  77. return -ENOMEM;
  78. for (i = 0; i < data->num_clocks; i++)
  79. chip_pm->clks[i].id = data->clocks[i];
  80. ret = devm_clk_bulk_get(dev, data->num_clocks, chip_pm->clks);
  81. if (ret)
  82. goto irq_dispose;
  83. chip_pm->clk_data = data;
  84. dev_set_drvdata(dev, chip_pm);
  85. pm_runtime_enable(dev);
  86. ret = pm_runtime_get_sync(dev);
  87. if (ret < 0)
  88. goto rpm_disable;
  89. ret = gic_of_init_child(dev, &chip_pm->chip_data, irq);
  90. if (ret)
  91. goto rpm_put;
  92. pm_runtime_put(dev);
  93. dev_info(dev, "GIC IRQ controller registered\n");
  94. return 0;
  95. rpm_put:
  96. pm_runtime_put_sync(dev);
  97. rpm_disable:
  98. pm_runtime_disable(dev);
  99. irq_dispose:
  100. irq_dispose_mapping(irq);
  101. return ret;
  102. }
  103. static const struct dev_pm_ops gic_pm_ops = {
  104. SET_RUNTIME_PM_OPS(gic_runtime_suspend,
  105. gic_runtime_resume, NULL)
  106. SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  107. pm_runtime_force_resume)
  108. };
  109. static const char * const gic400_clocks[] = {
  110. "clk",
  111. };
  112. static const struct gic_clk_data gic400_data = {
  113. .num_clocks = ARRAY_SIZE(gic400_clocks),
  114. .clocks = gic400_clocks,
  115. };
  116. static const struct of_device_id gic_match[] = {
  117. { .compatible = "nvidia,tegra210-agic", .data = &gic400_data },
  118. {},
  119. };
  120. MODULE_DEVICE_TABLE(of, gic_match);
  121. static struct platform_driver gic_driver = {
  122. .probe = gic_probe,
  123. .driver = {
  124. .name = "gic",
  125. .of_match_table = gic_match,
  126. .pm = &gic_pm_ops,
  127. }
  128. };
  129. builtin_platform_driver(gic_driver);