platform.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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/dma-mapping.h>
  18. #include <linux/slab.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/platform_device.h>
  24. static int of_dev_node_match(struct device *dev, void *data)
  25. {
  26. return dev->of_node == data;
  27. }
  28. /**
  29. * of_find_device_by_node - Find the platform_device associated with a node
  30. * @np: Pointer to device tree node
  31. *
  32. * Returns platform_device pointer, or NULL if not found
  33. */
  34. struct platform_device *of_find_device_by_node(struct device_node *np)
  35. {
  36. struct device *dev;
  37. dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
  38. return dev ? to_platform_device(dev) : NULL;
  39. }
  40. EXPORT_SYMBOL(of_find_device_by_node);
  41. static int platform_driver_probe_shim(struct platform_device *pdev)
  42. {
  43. struct platform_driver *pdrv;
  44. struct of_platform_driver *ofpdrv;
  45. const struct of_device_id *match;
  46. pdrv = container_of(pdev->dev.driver, struct platform_driver, driver);
  47. ofpdrv = container_of(pdrv, struct of_platform_driver, platform_driver);
  48. /* There is an unlikely chance that an of_platform driver might match
  49. * on a non-OF platform device. If so, then of_match_device() will
  50. * come up empty. Return -EINVAL in this case so other drivers get
  51. * the chance to bind. */
  52. match = of_match_device(pdev->dev.driver->of_match_table, &pdev->dev);
  53. return match ? ofpdrv->probe(pdev, match) : -EINVAL;
  54. }
  55. static void platform_driver_shutdown_shim(struct platform_device *pdev)
  56. {
  57. struct platform_driver *pdrv;
  58. struct of_platform_driver *ofpdrv;
  59. pdrv = container_of(pdev->dev.driver, struct platform_driver, driver);
  60. ofpdrv = container_of(pdrv, struct of_platform_driver, platform_driver);
  61. ofpdrv->shutdown(pdev);
  62. }
  63. /**
  64. * of_register_platform_driver
  65. */
  66. int of_register_platform_driver(struct of_platform_driver *drv)
  67. {
  68. char *of_name;
  69. /* setup of_platform_driver to platform_driver adaptors */
  70. drv->platform_driver.driver = drv->driver;
  71. /* Prefix the driver name with 'of:' to avoid namespace collisions
  72. * and bogus matches. There are some drivers in the tree that
  73. * register both an of_platform_driver and a platform_driver with
  74. * the same name. This is a temporary measure until they are all
  75. * cleaned up --gcl July 29, 2010 */
  76. of_name = kmalloc(strlen(drv->driver.name) + 5, GFP_KERNEL);
  77. if (!of_name)
  78. return -ENOMEM;
  79. sprintf(of_name, "of:%s", drv->driver.name);
  80. drv->platform_driver.driver.name = of_name;
  81. if (drv->probe)
  82. drv->platform_driver.probe = platform_driver_probe_shim;
  83. drv->platform_driver.remove = drv->remove;
  84. if (drv->shutdown)
  85. drv->platform_driver.shutdown = platform_driver_shutdown_shim;
  86. drv->platform_driver.suspend = drv->suspend;
  87. drv->platform_driver.resume = drv->resume;
  88. return platform_driver_register(&drv->platform_driver);
  89. }
  90. EXPORT_SYMBOL(of_register_platform_driver);
  91. void of_unregister_platform_driver(struct of_platform_driver *drv)
  92. {
  93. platform_driver_unregister(&drv->platform_driver);
  94. kfree(drv->platform_driver.driver.name);
  95. drv->platform_driver.driver.name = NULL;
  96. }
  97. EXPORT_SYMBOL(of_unregister_platform_driver);
  98. #if defined(CONFIG_PPC_DCR)
  99. #include <asm/dcr.h>
  100. #endif
  101. extern struct device_attribute of_platform_device_attrs[];
  102. static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
  103. {
  104. const struct of_device_id *matches = drv->of_match_table;
  105. if (!matches)
  106. return 0;
  107. return of_match_device(matches, dev) != NULL;
  108. }
  109. static int of_platform_device_probe(struct device *dev)
  110. {
  111. int error = -ENODEV;
  112. struct of_platform_driver *drv;
  113. struct platform_device *of_dev;
  114. const struct of_device_id *match;
  115. drv = to_of_platform_driver(dev->driver);
  116. of_dev = to_platform_device(dev);
  117. if (!drv->probe)
  118. return error;
  119. of_dev_get(of_dev);
  120. match = of_match_device(drv->driver.of_match_table, dev);
  121. if (match)
  122. error = drv->probe(of_dev, match);
  123. if (error)
  124. of_dev_put(of_dev);
  125. return error;
  126. }
  127. static int of_platform_device_remove(struct device *dev)
  128. {
  129. struct platform_device *of_dev = to_platform_device(dev);
  130. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  131. if (dev->driver && drv->remove)
  132. drv->remove(of_dev);
  133. return 0;
  134. }
  135. static void of_platform_device_shutdown(struct device *dev)
  136. {
  137. struct platform_device *of_dev = to_platform_device(dev);
  138. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  139. if (dev->driver && drv->shutdown)
  140. drv->shutdown(of_dev);
  141. }
  142. #ifdef CONFIG_PM_SLEEP
  143. static int of_platform_legacy_suspend(struct device *dev, pm_message_t mesg)
  144. {
  145. struct platform_device *of_dev = to_platform_device(dev);
  146. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  147. int ret = 0;
  148. if (dev->driver && drv->suspend)
  149. ret = drv->suspend(of_dev, mesg);
  150. return ret;
  151. }
  152. static int of_platform_legacy_resume(struct device *dev)
  153. {
  154. struct platform_device *of_dev = to_platform_device(dev);
  155. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  156. int ret = 0;
  157. if (dev->driver && drv->resume)
  158. ret = drv->resume(of_dev);
  159. return ret;
  160. }
  161. static int of_platform_pm_prepare(struct device *dev)
  162. {
  163. struct device_driver *drv = dev->driver;
  164. int ret = 0;
  165. if (drv && drv->pm && drv->pm->prepare)
  166. ret = drv->pm->prepare(dev);
  167. return ret;
  168. }
  169. static void of_platform_pm_complete(struct device *dev)
  170. {
  171. struct device_driver *drv = dev->driver;
  172. if (drv && drv->pm && drv->pm->complete)
  173. drv->pm->complete(dev);
  174. }
  175. #ifdef CONFIG_SUSPEND
  176. static int of_platform_pm_suspend(struct device *dev)
  177. {
  178. struct device_driver *drv = dev->driver;
  179. int ret = 0;
  180. if (!drv)
  181. return 0;
  182. if (drv->pm) {
  183. if (drv->pm->suspend)
  184. ret = drv->pm->suspend(dev);
  185. } else {
  186. ret = of_platform_legacy_suspend(dev, PMSG_SUSPEND);
  187. }
  188. return ret;
  189. }
  190. static int of_platform_pm_suspend_noirq(struct device *dev)
  191. {
  192. struct device_driver *drv = dev->driver;
  193. int ret = 0;
  194. if (!drv)
  195. return 0;
  196. if (drv->pm) {
  197. if (drv->pm->suspend_noirq)
  198. ret = drv->pm->suspend_noirq(dev);
  199. }
  200. return ret;
  201. }
  202. static int of_platform_pm_resume(struct device *dev)
  203. {
  204. struct device_driver *drv = dev->driver;
  205. int ret = 0;
  206. if (!drv)
  207. return 0;
  208. if (drv->pm) {
  209. if (drv->pm->resume)
  210. ret = drv->pm->resume(dev);
  211. } else {
  212. ret = of_platform_legacy_resume(dev);
  213. }
  214. return ret;
  215. }
  216. static int of_platform_pm_resume_noirq(struct device *dev)
  217. {
  218. struct device_driver *drv = dev->driver;
  219. int ret = 0;
  220. if (!drv)
  221. return 0;
  222. if (drv->pm) {
  223. if (drv->pm->resume_noirq)
  224. ret = drv->pm->resume_noirq(dev);
  225. }
  226. return ret;
  227. }
  228. #else /* !CONFIG_SUSPEND */
  229. #define of_platform_pm_suspend NULL
  230. #define of_platform_pm_resume NULL
  231. #define of_platform_pm_suspend_noirq NULL
  232. #define of_platform_pm_resume_noirq NULL
  233. #endif /* !CONFIG_SUSPEND */
  234. #ifdef CONFIG_HIBERNATION
  235. static int of_platform_pm_freeze(struct device *dev)
  236. {
  237. struct device_driver *drv = dev->driver;
  238. int ret = 0;
  239. if (!drv)
  240. return 0;
  241. if (drv->pm) {
  242. if (drv->pm->freeze)
  243. ret = drv->pm->freeze(dev);
  244. } else {
  245. ret = of_platform_legacy_suspend(dev, PMSG_FREEZE);
  246. }
  247. return ret;
  248. }
  249. static int of_platform_pm_freeze_noirq(struct device *dev)
  250. {
  251. struct device_driver *drv = dev->driver;
  252. int ret = 0;
  253. if (!drv)
  254. return 0;
  255. if (drv->pm) {
  256. if (drv->pm->freeze_noirq)
  257. ret = drv->pm->freeze_noirq(dev);
  258. }
  259. return ret;
  260. }
  261. static int of_platform_pm_thaw(struct device *dev)
  262. {
  263. struct device_driver *drv = dev->driver;
  264. int ret = 0;
  265. if (!drv)
  266. return 0;
  267. if (drv->pm) {
  268. if (drv->pm->thaw)
  269. ret = drv->pm->thaw(dev);
  270. } else {
  271. ret = of_platform_legacy_resume(dev);
  272. }
  273. return ret;
  274. }
  275. static int of_platform_pm_thaw_noirq(struct device *dev)
  276. {
  277. struct device_driver *drv = dev->driver;
  278. int ret = 0;
  279. if (!drv)
  280. return 0;
  281. if (drv->pm) {
  282. if (drv->pm->thaw_noirq)
  283. ret = drv->pm->thaw_noirq(dev);
  284. }
  285. return ret;
  286. }
  287. static int of_platform_pm_poweroff(struct device *dev)
  288. {
  289. struct device_driver *drv = dev->driver;
  290. int ret = 0;
  291. if (!drv)
  292. return 0;
  293. if (drv->pm) {
  294. if (drv->pm->poweroff)
  295. ret = drv->pm->poweroff(dev);
  296. } else {
  297. ret = of_platform_legacy_suspend(dev, PMSG_HIBERNATE);
  298. }
  299. return ret;
  300. }
  301. static int of_platform_pm_poweroff_noirq(struct device *dev)
  302. {
  303. struct device_driver *drv = dev->driver;
  304. int ret = 0;
  305. if (!drv)
  306. return 0;
  307. if (drv->pm) {
  308. if (drv->pm->poweroff_noirq)
  309. ret = drv->pm->poweroff_noirq(dev);
  310. }
  311. return ret;
  312. }
  313. static int of_platform_pm_restore(struct device *dev)
  314. {
  315. struct device_driver *drv = dev->driver;
  316. int ret = 0;
  317. if (!drv)
  318. return 0;
  319. if (drv->pm) {
  320. if (drv->pm->restore)
  321. ret = drv->pm->restore(dev);
  322. } else {
  323. ret = of_platform_legacy_resume(dev);
  324. }
  325. return ret;
  326. }
  327. static int of_platform_pm_restore_noirq(struct device *dev)
  328. {
  329. struct device_driver *drv = dev->driver;
  330. int ret = 0;
  331. if (!drv)
  332. return 0;
  333. if (drv->pm) {
  334. if (drv->pm->restore_noirq)
  335. ret = drv->pm->restore_noirq(dev);
  336. }
  337. return ret;
  338. }
  339. #else /* !CONFIG_HIBERNATION */
  340. #define of_platform_pm_freeze NULL
  341. #define of_platform_pm_thaw NULL
  342. #define of_platform_pm_poweroff NULL
  343. #define of_platform_pm_restore NULL
  344. #define of_platform_pm_freeze_noirq NULL
  345. #define of_platform_pm_thaw_noirq NULL
  346. #define of_platform_pm_poweroff_noirq NULL
  347. #define of_platform_pm_restore_noirq NULL
  348. #endif /* !CONFIG_HIBERNATION */
  349. static struct dev_pm_ops of_platform_dev_pm_ops = {
  350. .prepare = of_platform_pm_prepare,
  351. .complete = of_platform_pm_complete,
  352. .suspend = of_platform_pm_suspend,
  353. .resume = of_platform_pm_resume,
  354. .freeze = of_platform_pm_freeze,
  355. .thaw = of_platform_pm_thaw,
  356. .poweroff = of_platform_pm_poweroff,
  357. .restore = of_platform_pm_restore,
  358. .suspend_noirq = of_platform_pm_suspend_noirq,
  359. .resume_noirq = of_platform_pm_resume_noirq,
  360. .freeze_noirq = of_platform_pm_freeze_noirq,
  361. .thaw_noirq = of_platform_pm_thaw_noirq,
  362. .poweroff_noirq = of_platform_pm_poweroff_noirq,
  363. .restore_noirq = of_platform_pm_restore_noirq,
  364. };
  365. #define OF_PLATFORM_PM_OPS_PTR (&of_platform_dev_pm_ops)
  366. #else /* !CONFIG_PM_SLEEP */
  367. #define OF_PLATFORM_PM_OPS_PTR NULL
  368. #endif /* !CONFIG_PM_SLEEP */
  369. int of_bus_type_init(struct bus_type *bus, const char *name)
  370. {
  371. bus->name = name;
  372. bus->match = of_platform_bus_match;
  373. bus->probe = of_platform_device_probe;
  374. bus->remove = of_platform_device_remove;
  375. bus->shutdown = of_platform_device_shutdown;
  376. bus->dev_attrs = of_platform_device_attrs;
  377. bus->pm = OF_PLATFORM_PM_OPS_PTR;
  378. return bus_register(bus);
  379. }
  380. int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
  381. {
  382. /*
  383. * Temporary: of_platform_bus used to be distinct from the platform
  384. * bus. It isn't anymore, and so drivers on the platform bus need
  385. * to be registered in a special way.
  386. *
  387. * After all of_platform_bus_type drivers are converted to
  388. * platform_drivers, this exception can be removed.
  389. */
  390. if (bus == &platform_bus_type)
  391. return of_register_platform_driver(drv);
  392. /* register with core */
  393. drv->driver.bus = bus;
  394. return driver_register(&drv->driver);
  395. }
  396. EXPORT_SYMBOL(of_register_driver);
  397. void of_unregister_driver(struct of_platform_driver *drv)
  398. {
  399. if (drv->driver.bus == &platform_bus_type)
  400. of_unregister_platform_driver(drv);
  401. else
  402. driver_unregister(&drv->driver);
  403. }
  404. EXPORT_SYMBOL(of_unregister_driver);
  405. #if !defined(CONFIG_SPARC)
  406. /*
  407. * The following routines scan a subtree and registers a device for
  408. * each applicable node.
  409. *
  410. * Note: sparc doesn't use these routines because it has a different
  411. * mechanism for creating devices from device tree nodes.
  412. */
  413. /**
  414. * of_device_make_bus_id - Use the device node data to assign a unique name
  415. * @dev: pointer to device structure that is linked to a device tree node
  416. *
  417. * This routine will first try using either the dcr-reg or the reg property
  418. * value to derive a unique name. As a last resort it will use the node
  419. * name followed by a unique number.
  420. */
  421. void of_device_make_bus_id(struct device *dev)
  422. {
  423. static atomic_t bus_no_reg_magic;
  424. struct device_node *node = dev->of_node;
  425. const u32 *reg;
  426. u64 addr;
  427. int magic;
  428. #ifdef CONFIG_PPC_DCR
  429. /*
  430. * If it's a DCR based device, use 'd' for native DCRs
  431. * and 'D' for MMIO DCRs.
  432. */
  433. reg = of_get_property(node, "dcr-reg", NULL);
  434. if (reg) {
  435. #ifdef CONFIG_PPC_DCR_NATIVE
  436. dev_set_name(dev, "d%x.%s", *reg, node->name);
  437. #else /* CONFIG_PPC_DCR_NATIVE */
  438. u64 addr = of_translate_dcr_address(node, *reg, NULL);
  439. if (addr != OF_BAD_ADDR) {
  440. dev_set_name(dev, "D%llx.%s",
  441. (unsigned long long)addr, node->name);
  442. return;
  443. }
  444. #endif /* !CONFIG_PPC_DCR_NATIVE */
  445. }
  446. #endif /* CONFIG_PPC_DCR */
  447. /*
  448. * For MMIO, get the physical address
  449. */
  450. reg = of_get_property(node, "reg", NULL);
  451. if (reg) {
  452. addr = of_translate_address(node, reg);
  453. if (addr != OF_BAD_ADDR) {
  454. dev_set_name(dev, "%llx.%s",
  455. (unsigned long long)addr, node->name);
  456. return;
  457. }
  458. }
  459. /*
  460. * No BusID, use the node name and add a globally incremented
  461. * counter (and pray...)
  462. */
  463. magic = atomic_add_return(1, &bus_no_reg_magic);
  464. dev_set_name(dev, "%s.%d", node->name, magic - 1);
  465. }
  466. /**
  467. * of_device_alloc - Allocate and initialize an of_device
  468. * @np: device node to assign to device
  469. * @bus_id: Name to assign to the device. May be null to use default name.
  470. * @parent: Parent device.
  471. */
  472. struct platform_device *of_device_alloc(struct device_node *np,
  473. const char *bus_id,
  474. struct device *parent)
  475. {
  476. struct platform_device *dev;
  477. int rc, i, num_reg = 0, num_irq;
  478. struct resource *res, temp_res;
  479. dev = platform_device_alloc("", -1);
  480. if (!dev)
  481. return NULL;
  482. /* count the io and irq resources */
  483. while (of_address_to_resource(np, num_reg, &temp_res) == 0)
  484. num_reg++;
  485. num_irq = of_irq_count(np);
  486. /* Populate the resource table */
  487. if (num_irq || num_reg) {
  488. res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
  489. if (!res) {
  490. platform_device_put(dev);
  491. return NULL;
  492. }
  493. dev->num_resources = num_reg + num_irq;
  494. dev->resource = res;
  495. for (i = 0; i < num_reg; i++, res++) {
  496. rc = of_address_to_resource(np, i, res);
  497. WARN_ON(rc);
  498. }
  499. WARN_ON(of_irq_to_resource_table(np, res, num_irq) != num_irq);
  500. }
  501. dev->dev.of_node = of_node_get(np);
  502. #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
  503. dev->dev.dma_mask = &dev->archdata.dma_mask;
  504. #endif
  505. dev->dev.parent = parent;
  506. if (bus_id)
  507. dev_set_name(&dev->dev, "%s", bus_id);
  508. else
  509. of_device_make_bus_id(&dev->dev);
  510. return dev;
  511. }
  512. EXPORT_SYMBOL(of_device_alloc);
  513. /**
  514. * of_platform_device_create - Alloc, initialize and register an of_device
  515. * @np: pointer to node to create device for
  516. * @bus_id: name to assign device
  517. * @parent: Linux device model parent device.
  518. *
  519. * Returns pointer to created platform device, or NULL if a device was not
  520. * registered. Unavailable devices will not get registered.
  521. */
  522. struct platform_device *of_platform_device_create(struct device_node *np,
  523. const char *bus_id,
  524. struct device *parent)
  525. {
  526. struct platform_device *dev;
  527. if (!of_device_is_available(np))
  528. return NULL;
  529. dev = of_device_alloc(np, bus_id, parent);
  530. if (!dev)
  531. return NULL;
  532. #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
  533. dev->archdata.dma_mask = 0xffffffffUL;
  534. #endif
  535. dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  536. dev->dev.bus = &platform_bus_type;
  537. /* We do not fill the DMA ops for platform devices by default.
  538. * This is currently the responsibility of the platform code
  539. * to do such, possibly using a device notifier
  540. */
  541. if (of_device_add(dev) != 0) {
  542. platform_device_put(dev);
  543. return NULL;
  544. }
  545. return dev;
  546. }
  547. EXPORT_SYMBOL(of_platform_device_create);
  548. /**
  549. * of_platform_bus_create - Create an OF device for a bus node and all its
  550. * children. Optionally recursively instantiate matching busses.
  551. * @bus: device node of the bus to instantiate
  552. * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
  553. * disallow recursive creation of child busses
  554. */
  555. static int of_platform_bus_create(const struct device_node *bus,
  556. const struct of_device_id *matches,
  557. struct device *parent)
  558. {
  559. struct device_node *child;
  560. struct platform_device *dev;
  561. int rc = 0;
  562. for_each_child_of_node(bus, child) {
  563. pr_debug(" create child: %s\n", child->full_name);
  564. dev = of_platform_device_create(child, NULL, parent);
  565. if (dev == NULL)
  566. continue;
  567. if (!of_match_node(matches, child))
  568. continue;
  569. if (rc == 0) {
  570. pr_debug(" and sub busses\n");
  571. rc = of_platform_bus_create(child, matches, &dev->dev);
  572. }
  573. if (rc) {
  574. of_node_put(child);
  575. break;
  576. }
  577. }
  578. return rc;
  579. }
  580. /**
  581. * of_platform_bus_probe - Probe the device-tree for platform busses
  582. * @root: parent of the first level to probe or NULL for the root of the tree
  583. * @matches: match table, NULL to use the default
  584. * @parent: parent to hook devices from, NULL for toplevel
  585. *
  586. * Note that children of the provided root are not instantiated as devices
  587. * unless the specified root itself matches the bus list and is not NULL.
  588. */
  589. int of_platform_bus_probe(struct device_node *root,
  590. const struct of_device_id *matches,
  591. struct device *parent)
  592. {
  593. struct device_node *child;
  594. struct platform_device *dev;
  595. int rc = 0;
  596. if (WARN_ON(!matches || matches == OF_NO_DEEP_PROBE))
  597. return -EINVAL;
  598. if (root == NULL)
  599. root = of_find_node_by_path("/");
  600. else
  601. of_node_get(root);
  602. if (root == NULL)
  603. return -EINVAL;
  604. pr_debug("of_platform_bus_probe()\n");
  605. pr_debug(" starting at: %s\n", root->full_name);
  606. /* Do a self check of bus type, if there's a match, create
  607. * children
  608. */
  609. if (of_match_node(matches, root)) {
  610. pr_debug(" root match, create all sub devices\n");
  611. dev = of_platform_device_create(root, NULL, parent);
  612. if (dev == NULL)
  613. goto bail;
  614. pr_debug(" create all sub busses\n");
  615. rc = of_platform_bus_create(root, matches, &dev->dev);
  616. goto bail;
  617. }
  618. for_each_child_of_node(root, child) {
  619. if (!of_match_node(matches, child))
  620. continue;
  621. pr_debug(" match: %s\n", child->full_name);
  622. dev = of_platform_device_create(child, NULL, parent);
  623. if (dev == NULL)
  624. continue;
  625. rc = of_platform_bus_create(child, matches, &dev->dev);
  626. if (rc) {
  627. of_node_put(child);
  628. break;
  629. }
  630. }
  631. bail:
  632. of_node_put(root);
  633. return rc;
  634. }
  635. EXPORT_SYMBOL(of_platform_bus_probe);
  636. #endif /* !CONFIG_SPARC */