pm_runtime.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * arch/sh/kernel/cpu/shmobile/pm_runtime.c
  3. *
  4. * Runtime PM support code for SuperH Mobile
  5. *
  6. * Copyright (C) 2009 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/mutex.h>
  18. #include <asm/hwblk.h>
  19. static DEFINE_SPINLOCK(hwblk_lock);
  20. static LIST_HEAD(hwblk_idle_list);
  21. static struct work_struct hwblk_work;
  22. extern struct hwblk_info *hwblk_info;
  23. static void platform_pm_runtime_not_idle(struct platform_device *pdev)
  24. {
  25. unsigned long flags;
  26. /* remove device from idle list */
  27. spin_lock_irqsave(&hwblk_lock, flags);
  28. if (test_bit(PDEV_ARCHDATA_FLAG_IDLE, &pdev->archdata.flags)) {
  29. list_del(&pdev->archdata.entry);
  30. __clear_bit(PDEV_ARCHDATA_FLAG_IDLE, &pdev->archdata.flags);
  31. }
  32. spin_unlock_irqrestore(&hwblk_lock, flags);
  33. }
  34. static int __platform_pm_runtime_resume(struct platform_device *pdev)
  35. {
  36. struct device *d = &pdev->dev;
  37. struct pdev_archdata *ad = &pdev->archdata;
  38. int hwblk = ad->hwblk_id;
  39. int ret = -ENOSYS;
  40. dev_dbg(d, "__platform_pm_runtime_resume() [%d]\n", hwblk);
  41. if (d->driver) {
  42. hwblk_enable(hwblk_info, hwblk);
  43. ret = 0;
  44. if (test_bit(PDEV_ARCHDATA_FLAG_SUSP, &ad->flags)) {
  45. if (d->driver->pm && d->driver->pm->runtime_resume)
  46. ret = d->driver->pm->runtime_resume(d);
  47. if (!ret)
  48. clear_bit(PDEV_ARCHDATA_FLAG_SUSP, &ad->flags);
  49. else
  50. hwblk_disable(hwblk_info, hwblk);
  51. }
  52. }
  53. dev_dbg(d, "__platform_pm_runtime_resume() [%d] - returns %d\n",
  54. hwblk, ret);
  55. return ret;
  56. }
  57. static int __platform_pm_runtime_suspend(struct platform_device *pdev)
  58. {
  59. struct device *d = &pdev->dev;
  60. struct pdev_archdata *ad = &pdev->archdata;
  61. int hwblk = ad->hwblk_id;
  62. int ret = -ENOSYS;
  63. dev_dbg(d, "__platform_pm_runtime_suspend() [%d]\n", hwblk);
  64. if (d->driver) {
  65. BUG_ON(!test_bit(PDEV_ARCHDATA_FLAG_IDLE, &ad->flags));
  66. ret = 0;
  67. if (d->driver->pm && d->driver->pm->runtime_suspend) {
  68. hwblk_enable(hwblk_info, hwblk);
  69. ret = d->driver->pm->runtime_suspend(d);
  70. hwblk_disable(hwblk_info, hwblk);
  71. }
  72. if (!ret) {
  73. set_bit(PDEV_ARCHDATA_FLAG_SUSP, &ad->flags);
  74. platform_pm_runtime_not_idle(pdev);
  75. hwblk_cnt_dec(hwblk_info, hwblk, HWBLK_CNT_IDLE);
  76. }
  77. }
  78. dev_dbg(d, "__platform_pm_runtime_suspend() [%d] - returns %d\n",
  79. hwblk, ret);
  80. return ret;
  81. }
  82. static void platform_pm_runtime_work(struct work_struct *work)
  83. {
  84. struct platform_device *pdev;
  85. unsigned long flags;
  86. int ret;
  87. /* go through the idle list and suspend one device at a time */
  88. do {
  89. spin_lock_irqsave(&hwblk_lock, flags);
  90. if (list_empty(&hwblk_idle_list))
  91. pdev = NULL;
  92. else
  93. pdev = list_first_entry(&hwblk_idle_list,
  94. struct platform_device,
  95. archdata.entry);
  96. spin_unlock_irqrestore(&hwblk_lock, flags);
  97. if (pdev) {
  98. mutex_lock(&pdev->archdata.mutex);
  99. ret = __platform_pm_runtime_suspend(pdev);
  100. /* at this point the platform device may be:
  101. * suspended: ret = 0, FLAG_SUSP set, clock stopped
  102. * failed: ret < 0, FLAG_IDLE set, clock stopped
  103. */
  104. mutex_unlock(&pdev->archdata.mutex);
  105. } else {
  106. ret = -ENODEV;
  107. }
  108. } while (!ret);
  109. }
  110. /* this function gets called from cpuidle context when all devices in the
  111. * main power domain are unused but some are counted as idle, ie the hwblk
  112. * counter values are (HWBLK_CNT_USAGE == 0) && (HWBLK_CNT_IDLE != 0)
  113. */
  114. void platform_pm_runtime_suspend_idle(void)
  115. {
  116. queue_work(pm_wq, &hwblk_work);
  117. }
  118. static int default_platform_runtime_suspend(struct device *dev)
  119. {
  120. struct platform_device *pdev = to_platform_device(dev);
  121. struct pdev_archdata *ad = &pdev->archdata;
  122. unsigned long flags;
  123. int hwblk = ad->hwblk_id;
  124. int ret = 0;
  125. dev_dbg(dev, "%s() [%d]\n", __func__, hwblk);
  126. /* ignore off-chip platform devices */
  127. if (!hwblk)
  128. goto out;
  129. /* interrupt context not allowed */
  130. might_sleep();
  131. /* catch misconfigured drivers not starting with resume */
  132. if (test_bit(PDEV_ARCHDATA_FLAG_INIT, &ad->flags)) {
  133. ret = -EINVAL;
  134. goto out;
  135. }
  136. /* serialize */
  137. mutex_lock(&ad->mutex);
  138. /* disable clock */
  139. hwblk_disable(hwblk_info, hwblk);
  140. /* put device on idle list */
  141. spin_lock_irqsave(&hwblk_lock, flags);
  142. list_add_tail(&ad->entry, &hwblk_idle_list);
  143. __set_bit(PDEV_ARCHDATA_FLAG_IDLE, &ad->flags);
  144. spin_unlock_irqrestore(&hwblk_lock, flags);
  145. /* increase idle count */
  146. hwblk_cnt_inc(hwblk_info, hwblk, HWBLK_CNT_IDLE);
  147. /* at this point the platform device is:
  148. * idle: ret = 0, FLAG_IDLE set, clock stopped
  149. */
  150. mutex_unlock(&ad->mutex);
  151. out:
  152. dev_dbg(dev, "%s() [%d] returns %d\n",
  153. __func__, hwblk, ret);
  154. return ret;
  155. }
  156. static int default_platform_runtime_resume(struct device *dev)
  157. {
  158. struct platform_device *pdev = to_platform_device(dev);
  159. struct pdev_archdata *ad = &pdev->archdata;
  160. int hwblk = ad->hwblk_id;
  161. int ret = 0;
  162. dev_dbg(dev, "%s() [%d]\n", __func__, hwblk);
  163. /* ignore off-chip platform devices */
  164. if (!hwblk)
  165. goto out;
  166. /* interrupt context not allowed */
  167. might_sleep();
  168. /* serialize */
  169. mutex_lock(&ad->mutex);
  170. /* make sure device is removed from idle list */
  171. platform_pm_runtime_not_idle(pdev);
  172. /* decrease idle count */
  173. if (!test_bit(PDEV_ARCHDATA_FLAG_INIT, &pdev->archdata.flags) &&
  174. !test_bit(PDEV_ARCHDATA_FLAG_SUSP, &pdev->archdata.flags))
  175. hwblk_cnt_dec(hwblk_info, hwblk, HWBLK_CNT_IDLE);
  176. /* resume the device if needed */
  177. ret = __platform_pm_runtime_resume(pdev);
  178. /* the driver has been initialized now, so clear the init flag */
  179. clear_bit(PDEV_ARCHDATA_FLAG_INIT, &pdev->archdata.flags);
  180. /* at this point the platform device may be:
  181. * resumed: ret = 0, flags = 0, clock started
  182. * failed: ret < 0, FLAG_SUSP set, clock stopped
  183. */
  184. mutex_unlock(&ad->mutex);
  185. out:
  186. dev_dbg(dev, "%s() [%d] returns %d\n",
  187. __func__, hwblk, ret);
  188. return ret;
  189. }
  190. static int default_platform_runtime_idle(struct device *dev)
  191. {
  192. struct platform_device *pdev = to_platform_device(dev);
  193. int hwblk = pdev->archdata.hwblk_id;
  194. int ret = 0;
  195. dev_dbg(dev, "%s() [%d]\n", __func__, hwblk);
  196. /* ignore off-chip platform devices */
  197. if (!hwblk)
  198. goto out;
  199. /* interrupt context not allowed, use pm_runtime_put()! */
  200. might_sleep();
  201. /* suspend synchronously to disable clocks immediately */
  202. ret = pm_runtime_suspend(dev);
  203. out:
  204. dev_dbg(dev, "%s() [%d] done!\n", __func__, hwblk);
  205. return ret;
  206. }
  207. static struct dev_power_domain default_power_domain = {
  208. .ops = {
  209. .runtime_suspend = default_platform_runtime_suspend,
  210. .runtime_resume = default_platform_runtime_resume,
  211. .runtime_idle = default_platform_runtime_idle,
  212. USE_PLATFORM_PM_SLEEP_OPS
  213. },
  214. };
  215. static int platform_bus_notify(struct notifier_block *nb,
  216. unsigned long action, void *data)
  217. {
  218. struct device *dev = data;
  219. struct platform_device *pdev = to_platform_device(dev);
  220. int hwblk = pdev->archdata.hwblk_id;
  221. /* ignore off-chip platform devices */
  222. if (!hwblk)
  223. return 0;
  224. switch (action) {
  225. case BUS_NOTIFY_ADD_DEVICE:
  226. INIT_LIST_HEAD(&pdev->archdata.entry);
  227. mutex_init(&pdev->archdata.mutex);
  228. /* platform devices without drivers should be disabled */
  229. hwblk_enable(hwblk_info, hwblk);
  230. hwblk_disable(hwblk_info, hwblk);
  231. /* make sure driver re-inits itself once */
  232. __set_bit(PDEV_ARCHDATA_FLAG_INIT, &pdev->archdata.flags);
  233. dev->pwr_domain = &default_power_domain;
  234. break;
  235. /* TODO: add BUS_NOTIFY_BIND_DRIVER and increase idle count */
  236. case BUS_NOTIFY_BOUND_DRIVER:
  237. /* keep track of number of devices in use per hwblk */
  238. hwblk_cnt_inc(hwblk_info, hwblk, HWBLK_CNT_DEVICES);
  239. break;
  240. case BUS_NOTIFY_UNBOUND_DRIVER:
  241. /* keep track of number of devices in use per hwblk */
  242. hwblk_cnt_dec(hwblk_info, hwblk, HWBLK_CNT_DEVICES);
  243. /* make sure driver re-inits itself once */
  244. __set_bit(PDEV_ARCHDATA_FLAG_INIT, &pdev->archdata.flags);
  245. break;
  246. case BUS_NOTIFY_DEL_DEVICE:
  247. dev->pwr_domain = NULL;
  248. break;
  249. }
  250. return 0;
  251. }
  252. static struct notifier_block platform_bus_notifier = {
  253. .notifier_call = platform_bus_notify
  254. };
  255. static int __init sh_pm_runtime_init(void)
  256. {
  257. INIT_WORK(&hwblk_work, platform_pm_runtime_work);
  258. bus_register_notifier(&platform_bus_type, &platform_bus_notifier);
  259. return 0;
  260. }
  261. core_initcall(sh_pm_runtime_init);