pm_runtime.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * arch/arm/mach-shmobile/pm_runtime.c
  3. *
  4. * Runtime PM support code for SuperH Mobile ARM
  5. *
  6. * Copyright (C) 2009-2010 Magnus Damm
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/io.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/clk.h>
  18. #include <linux/sh_clk.h>
  19. #include <linux/bitmap.h>
  20. #include <linux/slab.h>
  21. #ifdef CONFIG_PM_RUNTIME
  22. #define BIT_ONCE 0
  23. #define BIT_ACTIVE 1
  24. #define BIT_CLK_ENABLED 2
  25. struct pm_runtime_data {
  26. unsigned long flags;
  27. struct clk *clk;
  28. };
  29. static struct pm_runtime_data *__to_prd(struct device *dev)
  30. {
  31. return dev ? dev->power.subsys_data : NULL;
  32. }
  33. static void platform_pm_runtime_init(struct device *dev,
  34. struct pm_runtime_data *prd)
  35. {
  36. if (prd && !test_and_set_bit(BIT_ONCE, &prd->flags)) {
  37. prd->clk = clk_get(dev, NULL);
  38. if (!IS_ERR(prd->clk)) {
  39. set_bit(BIT_ACTIVE, &prd->flags);
  40. dev_info(dev, "clocks managed by runtime pm\n");
  41. }
  42. }
  43. }
  44. static void platform_pm_runtime_bug(struct device *dev,
  45. struct pm_runtime_data *prd)
  46. {
  47. if (prd && !test_and_set_bit(BIT_ONCE, &prd->flags))
  48. dev_err(dev, "runtime pm suspend before resume\n");
  49. }
  50. static int default_platform_runtime_suspend(struct device *dev)
  51. {
  52. struct pm_runtime_data *prd = __to_prd(dev);
  53. dev_dbg(dev, "%s()\n", __func__);
  54. platform_pm_runtime_bug(dev, prd);
  55. if (prd && test_bit(BIT_ACTIVE, &prd->flags)) {
  56. clk_disable(prd->clk);
  57. clear_bit(BIT_CLK_ENABLED, &prd->flags);
  58. }
  59. return 0;
  60. }
  61. static int default_platform_runtime_resume(struct device *dev)
  62. {
  63. struct pm_runtime_data *prd = __to_prd(dev);
  64. dev_dbg(dev, "%s()\n", __func__);
  65. platform_pm_runtime_init(dev, prd);
  66. if (prd && test_bit(BIT_ACTIVE, &prd->flags)) {
  67. clk_enable(prd->clk);
  68. set_bit(BIT_CLK_ENABLED, &prd->flags);
  69. }
  70. return 0;
  71. }
  72. static int default_platform_runtime_idle(struct device *dev)
  73. {
  74. /* suspend synchronously to disable clocks immediately */
  75. return pm_runtime_suspend(dev);
  76. }
  77. static struct dev_power_domain default_power_domain = {
  78. .ops = {
  79. .runtime_suspend = default_platform_runtime_suspend,
  80. .runtime_resume = default_platform_runtime_resume,
  81. .runtime_idle = default_platform_runtime_idle,
  82. USE_PLATFORM_PM_SLEEP_OPS
  83. },
  84. };
  85. static int platform_bus_notify(struct notifier_block *nb,
  86. unsigned long action, void *data)
  87. {
  88. struct device *dev = data;
  89. struct pm_runtime_data *prd;
  90. dev_dbg(dev, "platform_bus_notify() %ld !\n", action);
  91. switch (action) {
  92. case BUS_NOTIFY_BIND_DRIVER:
  93. prd = kzalloc(sizeof(*prd), GFP_KERNEL);
  94. if (prd) {
  95. dev->power.subsys_data = prd;
  96. dev->pwr_domain = &default_power_domain;
  97. } else {
  98. dev_err(dev, "unable to alloc memory for runtime pm\n");
  99. }
  100. break;
  101. case BUS_NOTIFY_UNBOUND_DRIVER:
  102. prd = __to_prd(dev);
  103. if (prd) {
  104. if (test_bit(BIT_CLK_ENABLED, &prd->flags))
  105. clk_disable(prd->clk);
  106. if (test_bit(BIT_ACTIVE, &prd->flags))
  107. clk_put(prd->clk);
  108. }
  109. break;
  110. }
  111. return 0;
  112. }
  113. #else /* CONFIG_PM_RUNTIME */
  114. static int platform_bus_notify(struct notifier_block *nb,
  115. unsigned long action, void *data)
  116. {
  117. struct device *dev = data;
  118. struct clk *clk;
  119. dev_dbg(dev, "platform_bus_notify() %ld !\n", action);
  120. switch (action) {
  121. case BUS_NOTIFY_BIND_DRIVER:
  122. clk = clk_get(dev, NULL);
  123. if (!IS_ERR(clk)) {
  124. clk_enable(clk);
  125. clk_put(clk);
  126. dev_info(dev, "runtime pm disabled, clock forced on\n");
  127. }
  128. break;
  129. case BUS_NOTIFY_UNBOUND_DRIVER:
  130. clk = clk_get(dev, NULL);
  131. if (!IS_ERR(clk)) {
  132. clk_disable(clk);
  133. clk_put(clk);
  134. dev_info(dev, "runtime pm disabled, clock forced off\n");
  135. }
  136. break;
  137. }
  138. return 0;
  139. }
  140. #endif /* CONFIG_PM_RUNTIME */
  141. static struct notifier_block platform_bus_notifier = {
  142. .notifier_call = platform_bus_notify
  143. };
  144. static int __init sh_pm_runtime_init(void)
  145. {
  146. bus_register_notifier(&platform_bus_type, &platform_bus_notifier);
  147. return 0;
  148. }
  149. core_initcall(sh_pm_runtime_init);