pm_runtime.c 7.9 KB

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