simple-pm-bus.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Simple Power-Managed Bus Driver
  4. *
  5. * Copyright (C) 2014-2015 Glider bvba
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_device.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_runtime.h>
  18. struct simple_pm_bus {
  19. struct clk_bulk_data *clks;
  20. int num_clks;
  21. };
  22. static int simple_pm_bus_probe(struct platform_device *pdev)
  23. {
  24. const struct device *dev = &pdev->dev;
  25. const struct of_dev_auxdata *lookup = dev_get_platdata(dev);
  26. struct device_node *np = dev->of_node;
  27. const struct of_device_id *match;
  28. struct simple_pm_bus *bus;
  29. /*
  30. * Allow user to use driver_override to bind this driver to a
  31. * transparent bus device which has a different compatible string
  32. * that's not listed in simple_pm_bus_of_match. We don't want to do any
  33. * of the simple-pm-bus tasks for these devices, so return early.
  34. */
  35. if (device_has_driver_override(&pdev->dev))
  36. return 0;
  37. match = of_match_device(dev->driver->of_match_table, dev);
  38. /*
  39. * These are transparent bus devices (not simple-pm-bus matches) that
  40. * have their child nodes populated automatically. So, don't need to
  41. * do anything more. We only match with the device if this driver is
  42. * the most specific match because we don't want to incorrectly bind to
  43. * a device that has a more specific driver.
  44. */
  45. if (match && match->data) {
  46. if (of_property_match_string(np, "compatible", match->compatible) == 0)
  47. return 0;
  48. else
  49. return -ENODEV;
  50. }
  51. bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL);
  52. if (!bus)
  53. return -ENOMEM;
  54. bus->num_clks = devm_clk_bulk_get_all(&pdev->dev, &bus->clks);
  55. if (bus->num_clks < 0)
  56. return dev_err_probe(&pdev->dev, bus->num_clks, "failed to get clocks\n");
  57. dev_set_drvdata(&pdev->dev, bus);
  58. dev_dbg(&pdev->dev, "%s\n", __func__);
  59. pm_runtime_enable(&pdev->dev);
  60. if (np)
  61. of_platform_populate(np, NULL, lookup, &pdev->dev);
  62. return 0;
  63. }
  64. static void simple_pm_bus_remove(struct platform_device *pdev)
  65. {
  66. const void *data = of_device_get_match_data(&pdev->dev);
  67. if (device_has_driver_override(&pdev->dev) || data)
  68. return;
  69. dev_dbg(&pdev->dev, "%s\n", __func__);
  70. pm_runtime_disable(&pdev->dev);
  71. }
  72. static int simple_pm_bus_runtime_suspend(struct device *dev)
  73. {
  74. struct simple_pm_bus *bus = dev_get_drvdata(dev);
  75. clk_bulk_disable_unprepare(bus->num_clks, bus->clks);
  76. return 0;
  77. }
  78. static int simple_pm_bus_runtime_resume(struct device *dev)
  79. {
  80. struct simple_pm_bus *bus = dev_get_drvdata(dev);
  81. int ret;
  82. ret = clk_bulk_prepare_enable(bus->num_clks, bus->clks);
  83. if (ret) {
  84. dev_err(dev, "failed to enable clocks: %d\n", ret);
  85. return ret;
  86. }
  87. return 0;
  88. }
  89. static int simple_pm_bus_suspend(struct device *dev)
  90. {
  91. struct simple_pm_bus *bus = dev_get_drvdata(dev);
  92. if (!bus)
  93. return 0;
  94. return pm_runtime_force_suspend(dev);
  95. }
  96. static int simple_pm_bus_resume(struct device *dev)
  97. {
  98. struct simple_pm_bus *bus = dev_get_drvdata(dev);
  99. if (!bus)
  100. return 0;
  101. return pm_runtime_force_resume(dev);
  102. }
  103. static const struct dev_pm_ops simple_pm_bus_pm_ops = {
  104. RUNTIME_PM_OPS(simple_pm_bus_runtime_suspend, simple_pm_bus_runtime_resume, NULL)
  105. NOIRQ_SYSTEM_SLEEP_PM_OPS(simple_pm_bus_suspend, simple_pm_bus_resume)
  106. };
  107. #define ONLY_BUS ((void *) 1) /* Match if the device is only a bus. */
  108. static const struct of_device_id simple_pm_bus_of_match[] = {
  109. { .compatible = "simple-pm-bus", },
  110. { .compatible = "simple-bus", .data = ONLY_BUS },
  111. { .compatible = "simple-mfd", .data = ONLY_BUS },
  112. { .compatible = "isa", .data = ONLY_BUS },
  113. { .compatible = "arm,amba-bus", .data = ONLY_BUS },
  114. { .compatible = "fsl,ls1021a-scfg", },
  115. { .compatible = "fsl,ls1043a-scfg", },
  116. { .compatible = "fsl,ls1046a-scfg", },
  117. { .compatible = "fsl,ls1088a-isc", },
  118. { .compatible = "fsl,ls2080a-isc", },
  119. { .compatible = "fsl,lx2160a-isc", },
  120. { /* sentinel */ }
  121. };
  122. MODULE_DEVICE_TABLE(of, simple_pm_bus_of_match);
  123. static struct platform_driver simple_pm_bus_driver = {
  124. .probe = simple_pm_bus_probe,
  125. .remove = simple_pm_bus_remove,
  126. .driver = {
  127. .name = "simple-pm-bus",
  128. .of_match_table = simple_pm_bus_of_match,
  129. .pm = pm_ptr(&simple_pm_bus_pm_ops),
  130. },
  131. };
  132. module_platform_driver(simple_pm_bus_driver);
  133. MODULE_DESCRIPTION("Simple Power-Managed Bus Driver");
  134. MODULE_AUTHOR("Geert Uytterhoeven <geert+renesas@glider.be>");