simple-pm-bus.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 (pdev->driver_override)
  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 (pdev->driver_override || 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 const struct dev_pm_ops simple_pm_bus_pm_ops = {
  90. RUNTIME_PM_OPS(simple_pm_bus_runtime_suspend, simple_pm_bus_runtime_resume, NULL)
  91. NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
  92. };
  93. #define ONLY_BUS ((void *) 1) /* Match if the device is only a bus. */
  94. static const struct of_device_id simple_pm_bus_of_match[] = {
  95. { .compatible = "simple-pm-bus", },
  96. { .compatible = "simple-bus", .data = ONLY_BUS },
  97. { .compatible = "simple-mfd", .data = ONLY_BUS },
  98. { .compatible = "isa", .data = ONLY_BUS },
  99. { .compatible = "arm,amba-bus", .data = ONLY_BUS },
  100. { /* sentinel */ }
  101. };
  102. MODULE_DEVICE_TABLE(of, simple_pm_bus_of_match);
  103. static struct platform_driver simple_pm_bus_driver = {
  104. .probe = simple_pm_bus_probe,
  105. .remove_new = simple_pm_bus_remove,
  106. .driver = {
  107. .name = "simple-pm-bus",
  108. .of_match_table = simple_pm_bus_of_match,
  109. .pm = pm_ptr(&simple_pm_bus_pm_ops),
  110. },
  111. };
  112. module_platform_driver(simple_pm_bus_driver);
  113. MODULE_DESCRIPTION("Simple Power-Managed Bus Driver");
  114. MODULE_AUTHOR("Geert Uytterhoeven <geert+renesas@glider.be>");