platform.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
  3. * <benh@kernel.crashing.org>
  4. * and Arnd Bergmann, IBM Corp.
  5. * Merged from powerpc/kernel/of_platform.c and
  6. * sparc{,64}/kernel/of_device.c by Stephen Rothwell
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. */
  14. #include <linux/errno.h>
  15. #include <linux/module.h>
  16. #include <linux/device.h>
  17. #include <linux/of_device.h>
  18. #include <linux/of_platform.h>
  19. extern struct device_attribute of_platform_device_attrs[];
  20. static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
  21. {
  22. const struct of_device_id *matches = drv->of_match_table;
  23. if (!matches)
  24. return 0;
  25. return of_match_device(matches, dev) != NULL;
  26. }
  27. static int of_platform_device_probe(struct device *dev)
  28. {
  29. int error = -ENODEV;
  30. struct of_platform_driver *drv;
  31. struct of_device *of_dev;
  32. const struct of_device_id *match;
  33. drv = to_of_platform_driver(dev->driver);
  34. of_dev = to_of_device(dev);
  35. if (!drv->probe)
  36. return error;
  37. of_dev_get(of_dev);
  38. match = of_match_device(drv->driver.of_match_table, dev);
  39. if (match)
  40. error = drv->probe(of_dev, match);
  41. if (error)
  42. of_dev_put(of_dev);
  43. return error;
  44. }
  45. static int of_platform_device_remove(struct device *dev)
  46. {
  47. struct of_device *of_dev = to_of_device(dev);
  48. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  49. if (dev->driver && drv->remove)
  50. drv->remove(of_dev);
  51. return 0;
  52. }
  53. static void of_platform_device_shutdown(struct device *dev)
  54. {
  55. struct of_device *of_dev = to_of_device(dev);
  56. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  57. if (dev->driver && drv->shutdown)
  58. drv->shutdown(of_dev);
  59. }
  60. #ifdef CONFIG_PM_SLEEP
  61. static int of_platform_legacy_suspend(struct device *dev, pm_message_t mesg)
  62. {
  63. struct of_device *of_dev = to_of_device(dev);
  64. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  65. int ret = 0;
  66. if (dev->driver && drv->suspend)
  67. ret = drv->suspend(of_dev, mesg);
  68. return ret;
  69. }
  70. static int of_platform_legacy_resume(struct device *dev)
  71. {
  72. struct of_device *of_dev = to_of_device(dev);
  73. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  74. int ret = 0;
  75. if (dev->driver && drv->resume)
  76. ret = drv->resume(of_dev);
  77. return ret;
  78. }
  79. static int of_platform_pm_prepare(struct device *dev)
  80. {
  81. struct device_driver *drv = dev->driver;
  82. int ret = 0;
  83. if (drv && drv->pm && drv->pm->prepare)
  84. ret = drv->pm->prepare(dev);
  85. return ret;
  86. }
  87. static void of_platform_pm_complete(struct device *dev)
  88. {
  89. struct device_driver *drv = dev->driver;
  90. if (drv && drv->pm && drv->pm->complete)
  91. drv->pm->complete(dev);
  92. }
  93. #ifdef CONFIG_SUSPEND
  94. static int of_platform_pm_suspend(struct device *dev)
  95. {
  96. struct device_driver *drv = dev->driver;
  97. int ret = 0;
  98. if (!drv)
  99. return 0;
  100. if (drv->pm) {
  101. if (drv->pm->suspend)
  102. ret = drv->pm->suspend(dev);
  103. } else {
  104. ret = of_platform_legacy_suspend(dev, PMSG_SUSPEND);
  105. }
  106. return ret;
  107. }
  108. static int of_platform_pm_suspend_noirq(struct device *dev)
  109. {
  110. struct device_driver *drv = dev->driver;
  111. int ret = 0;
  112. if (!drv)
  113. return 0;
  114. if (drv->pm) {
  115. if (drv->pm->suspend_noirq)
  116. ret = drv->pm->suspend_noirq(dev);
  117. }
  118. return ret;
  119. }
  120. static int of_platform_pm_resume(struct device *dev)
  121. {
  122. struct device_driver *drv = dev->driver;
  123. int ret = 0;
  124. if (!drv)
  125. return 0;
  126. if (drv->pm) {
  127. if (drv->pm->resume)
  128. ret = drv->pm->resume(dev);
  129. } else {
  130. ret = of_platform_legacy_resume(dev);
  131. }
  132. return ret;
  133. }
  134. static int of_platform_pm_resume_noirq(struct device *dev)
  135. {
  136. struct device_driver *drv = dev->driver;
  137. int ret = 0;
  138. if (!drv)
  139. return 0;
  140. if (drv->pm) {
  141. if (drv->pm->resume_noirq)
  142. ret = drv->pm->resume_noirq(dev);
  143. }
  144. return ret;
  145. }
  146. #else /* !CONFIG_SUSPEND */
  147. #define of_platform_pm_suspend NULL
  148. #define of_platform_pm_resume NULL
  149. #define of_platform_pm_suspend_noirq NULL
  150. #define of_platform_pm_resume_noirq NULL
  151. #endif /* !CONFIG_SUSPEND */
  152. #ifdef CONFIG_HIBERNATION
  153. static int of_platform_pm_freeze(struct device *dev)
  154. {
  155. struct device_driver *drv = dev->driver;
  156. int ret = 0;
  157. if (!drv)
  158. return 0;
  159. if (drv->pm) {
  160. if (drv->pm->freeze)
  161. ret = drv->pm->freeze(dev);
  162. } else {
  163. ret = of_platform_legacy_suspend(dev, PMSG_FREEZE);
  164. }
  165. return ret;
  166. }
  167. static int of_platform_pm_freeze_noirq(struct device *dev)
  168. {
  169. struct device_driver *drv = dev->driver;
  170. int ret = 0;
  171. if (!drv)
  172. return 0;
  173. if (drv->pm) {
  174. if (drv->pm->freeze_noirq)
  175. ret = drv->pm->freeze_noirq(dev);
  176. }
  177. return ret;
  178. }
  179. static int of_platform_pm_thaw(struct device *dev)
  180. {
  181. struct device_driver *drv = dev->driver;
  182. int ret = 0;
  183. if (!drv)
  184. return 0;
  185. if (drv->pm) {
  186. if (drv->pm->thaw)
  187. ret = drv->pm->thaw(dev);
  188. } else {
  189. ret = of_platform_legacy_resume(dev);
  190. }
  191. return ret;
  192. }
  193. static int of_platform_pm_thaw_noirq(struct device *dev)
  194. {
  195. struct device_driver *drv = dev->driver;
  196. int ret = 0;
  197. if (!drv)
  198. return 0;
  199. if (drv->pm) {
  200. if (drv->pm->thaw_noirq)
  201. ret = drv->pm->thaw_noirq(dev);
  202. }
  203. return ret;
  204. }
  205. static int of_platform_pm_poweroff(struct device *dev)
  206. {
  207. struct device_driver *drv = dev->driver;
  208. int ret = 0;
  209. if (!drv)
  210. return 0;
  211. if (drv->pm) {
  212. if (drv->pm->poweroff)
  213. ret = drv->pm->poweroff(dev);
  214. } else {
  215. ret = of_platform_legacy_suspend(dev, PMSG_HIBERNATE);
  216. }
  217. return ret;
  218. }
  219. static int of_platform_pm_poweroff_noirq(struct device *dev)
  220. {
  221. struct device_driver *drv = dev->driver;
  222. int ret = 0;
  223. if (!drv)
  224. return 0;
  225. if (drv->pm) {
  226. if (drv->pm->poweroff_noirq)
  227. ret = drv->pm->poweroff_noirq(dev);
  228. }
  229. return ret;
  230. }
  231. static int of_platform_pm_restore(struct device *dev)
  232. {
  233. struct device_driver *drv = dev->driver;
  234. int ret = 0;
  235. if (!drv)
  236. return 0;
  237. if (drv->pm) {
  238. if (drv->pm->restore)
  239. ret = drv->pm->restore(dev);
  240. } else {
  241. ret = of_platform_legacy_resume(dev);
  242. }
  243. return ret;
  244. }
  245. static int of_platform_pm_restore_noirq(struct device *dev)
  246. {
  247. struct device_driver *drv = dev->driver;
  248. int ret = 0;
  249. if (!drv)
  250. return 0;
  251. if (drv->pm) {
  252. if (drv->pm->restore_noirq)
  253. ret = drv->pm->restore_noirq(dev);
  254. }
  255. return ret;
  256. }
  257. #else /* !CONFIG_HIBERNATION */
  258. #define of_platform_pm_freeze NULL
  259. #define of_platform_pm_thaw NULL
  260. #define of_platform_pm_poweroff NULL
  261. #define of_platform_pm_restore NULL
  262. #define of_platform_pm_freeze_noirq NULL
  263. #define of_platform_pm_thaw_noirq NULL
  264. #define of_platform_pm_poweroff_noirq NULL
  265. #define of_platform_pm_restore_noirq NULL
  266. #endif /* !CONFIG_HIBERNATION */
  267. static struct dev_pm_ops of_platform_dev_pm_ops = {
  268. .prepare = of_platform_pm_prepare,
  269. .complete = of_platform_pm_complete,
  270. .suspend = of_platform_pm_suspend,
  271. .resume = of_platform_pm_resume,
  272. .freeze = of_platform_pm_freeze,
  273. .thaw = of_platform_pm_thaw,
  274. .poweroff = of_platform_pm_poweroff,
  275. .restore = of_platform_pm_restore,
  276. .suspend_noirq = of_platform_pm_suspend_noirq,
  277. .resume_noirq = of_platform_pm_resume_noirq,
  278. .freeze_noirq = of_platform_pm_freeze_noirq,
  279. .thaw_noirq = of_platform_pm_thaw_noirq,
  280. .poweroff_noirq = of_platform_pm_poweroff_noirq,
  281. .restore_noirq = of_platform_pm_restore_noirq,
  282. };
  283. #define OF_PLATFORM_PM_OPS_PTR (&of_platform_dev_pm_ops)
  284. #else /* !CONFIG_PM_SLEEP */
  285. #define OF_PLATFORM_PM_OPS_PTR NULL
  286. #endif /* !CONFIG_PM_SLEEP */
  287. int of_bus_type_init(struct bus_type *bus, const char *name)
  288. {
  289. bus->name = name;
  290. bus->match = of_platform_bus_match;
  291. bus->probe = of_platform_device_probe;
  292. bus->remove = of_platform_device_remove;
  293. bus->shutdown = of_platform_device_shutdown;
  294. bus->dev_attrs = of_platform_device_attrs;
  295. bus->pm = OF_PLATFORM_PM_OPS_PTR;
  296. return bus_register(bus);
  297. }
  298. int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
  299. {
  300. drv->driver.bus = bus;
  301. /* register with core */
  302. return driver_register(&drv->driver);
  303. }
  304. EXPORT_SYMBOL(of_register_driver);
  305. void of_unregister_driver(struct of_platform_driver *drv)
  306. {
  307. driver_unregister(&drv->driver);
  308. }
  309. EXPORT_SYMBOL(of_unregister_driver);