platform.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
  4. * <benh@kernel.crashing.org>
  5. * and Arnd Bergmann, IBM Corp.
  6. * Merged from powerpc/kernel/of_platform.c and
  7. * sparc{,64}/kernel/of_device.c by Stephen Rothwell
  8. */
  9. #define pr_fmt(fmt) "OF: " fmt
  10. #include <linux/errno.h>
  11. #include <linux/module.h>
  12. #include <linux/amba/bus.h>
  13. #include <linux/device.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/slab.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_device.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/sysfb.h>
  22. #include "of_private.h"
  23. /**
  24. * of_find_device_by_node - Find the platform_device associated with a node
  25. * @np: Pointer to device tree node
  26. *
  27. * Takes a reference to the embedded struct device which needs to be dropped
  28. * after use.
  29. *
  30. * Return: platform_device pointer, or NULL if not found
  31. */
  32. struct platform_device *of_find_device_by_node(struct device_node *np)
  33. {
  34. struct device *dev;
  35. dev = bus_find_device_by_of_node(&platform_bus_type, np);
  36. return dev ? to_platform_device(dev) : NULL;
  37. }
  38. EXPORT_SYMBOL(of_find_device_by_node);
  39. int of_device_add(struct platform_device *ofdev)
  40. {
  41. BUG_ON(ofdev->dev.of_node == NULL);
  42. /* name and id have to be set so that the platform bus doesn't get
  43. * confused on matching */
  44. ofdev->name = dev_name(&ofdev->dev);
  45. ofdev->id = PLATFORM_DEVID_NONE;
  46. /*
  47. * If this device has not binding numa node in devicetree, that is
  48. * of_node_to_nid returns NUMA_NO_NODE. device_add will assume that this
  49. * device is on the same node as the parent.
  50. */
  51. set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
  52. return device_add(&ofdev->dev);
  53. }
  54. int of_device_register(struct platform_device *pdev)
  55. {
  56. device_initialize(&pdev->dev);
  57. return of_device_add(pdev);
  58. }
  59. EXPORT_SYMBOL(of_device_register);
  60. void of_device_unregister(struct platform_device *ofdev)
  61. {
  62. device_unregister(&ofdev->dev);
  63. }
  64. EXPORT_SYMBOL(of_device_unregister);
  65. #ifdef CONFIG_OF_ADDRESS
  66. static const struct of_device_id of_skipped_node_table[] = {
  67. { .compatible = "operating-points-v2", },
  68. {} /* Empty terminated list */
  69. };
  70. /*
  71. * The following routines scan a subtree and registers a device for
  72. * each applicable node.
  73. *
  74. * Note: sparc doesn't use these routines because it has a different
  75. * mechanism for creating devices from device tree nodes.
  76. */
  77. /**
  78. * of_device_alloc - Allocate and initialize an of_device
  79. * @np: device node to assign to device
  80. * @bus_id: Name to assign to the device. May be null to use default name.
  81. * @parent: Parent device.
  82. */
  83. struct platform_device *of_device_alloc(struct device_node *np,
  84. const char *bus_id,
  85. struct device *parent)
  86. {
  87. struct platform_device *dev;
  88. int rc, i, num_reg = 0;
  89. struct resource *res;
  90. dev = platform_device_alloc("", PLATFORM_DEVID_NONE);
  91. if (!dev)
  92. return NULL;
  93. /* count the io resources */
  94. num_reg = of_address_count(np);
  95. /* Populate the resource table */
  96. if (num_reg) {
  97. res = kzalloc_objs(*res, num_reg);
  98. if (!res) {
  99. platform_device_put(dev);
  100. return NULL;
  101. }
  102. dev->num_resources = num_reg;
  103. dev->resource = res;
  104. for (i = 0; i < num_reg; i++, res++) {
  105. rc = of_address_to_resource(np, i, res);
  106. WARN_ON(rc);
  107. }
  108. }
  109. /* setup generic device info */
  110. device_set_node(&dev->dev, of_fwnode_handle(of_node_get(np)));
  111. dev->dev.parent = parent ? : &platform_bus;
  112. if (bus_id)
  113. dev_set_name(&dev->dev, "%s", bus_id);
  114. else
  115. of_device_make_bus_id(&dev->dev);
  116. return dev;
  117. }
  118. EXPORT_SYMBOL(of_device_alloc);
  119. /**
  120. * of_platform_device_create_pdata - Alloc, initialize and register an of_device
  121. * @np: pointer to node to create device for
  122. * @bus_id: name to assign device
  123. * @platform_data: pointer to populate platform_data pointer with
  124. * @parent: Linux device model parent device.
  125. *
  126. * Return: Pointer to created platform device, or NULL if a device was not
  127. * registered. Unavailable devices will not get registered.
  128. */
  129. static struct platform_device *of_platform_device_create_pdata(
  130. struct device_node *np,
  131. const char *bus_id,
  132. void *platform_data,
  133. struct device *parent)
  134. {
  135. struct platform_device *dev;
  136. pr_debug("create platform device: %pOF\n", np);
  137. if (!of_device_is_available(np) ||
  138. of_node_test_and_set_flag(np, OF_POPULATED))
  139. return NULL;
  140. dev = of_device_alloc(np, bus_id, parent);
  141. if (!dev)
  142. goto err_clear_flag;
  143. dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  144. if (!dev->dev.dma_mask)
  145. dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
  146. dev->dev.bus = &platform_bus_type;
  147. dev->dev.platform_data = platform_data;
  148. of_msi_configure(&dev->dev, dev->dev.of_node);
  149. if (of_device_add(dev) != 0) {
  150. platform_device_put(dev);
  151. goto err_clear_flag;
  152. }
  153. return dev;
  154. err_clear_flag:
  155. of_node_clear_flag(np, OF_POPULATED);
  156. return NULL;
  157. }
  158. /**
  159. * of_platform_device_create - Alloc, initialize and register an of_device
  160. * @np: pointer to node to create device for
  161. * @bus_id: name to assign device
  162. * @parent: Linux device model parent device.
  163. *
  164. * Return: Pointer to created platform device, or NULL if a device was not
  165. * registered. Unavailable devices will not get registered.
  166. */
  167. struct platform_device *of_platform_device_create(struct device_node *np,
  168. const char *bus_id,
  169. struct device *parent)
  170. {
  171. return of_platform_device_create_pdata(np, bus_id, NULL, parent);
  172. }
  173. EXPORT_SYMBOL(of_platform_device_create);
  174. #ifdef CONFIG_ARM_AMBA
  175. static struct amba_device *of_amba_device_create(struct device_node *node,
  176. const char *bus_id,
  177. void *platform_data,
  178. struct device *parent)
  179. {
  180. struct amba_device *dev;
  181. int ret;
  182. pr_debug("Creating amba device %pOF\n", node);
  183. if (!of_device_is_available(node) ||
  184. of_node_test_and_set_flag(node, OF_POPULATED))
  185. return NULL;
  186. dev = amba_device_alloc(NULL, 0, 0);
  187. if (!dev)
  188. goto err_clear_flag;
  189. /* AMBA devices only support a single DMA mask */
  190. dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  191. dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
  192. /* setup generic device info */
  193. device_set_node(&dev->dev, of_fwnode_handle(node));
  194. dev->dev.parent = parent ? : &platform_bus;
  195. dev->dev.platform_data = platform_data;
  196. if (bus_id)
  197. dev_set_name(&dev->dev, "%s", bus_id);
  198. else
  199. of_device_make_bus_id(&dev->dev);
  200. /* Allow the HW Peripheral ID to be overridden */
  201. of_property_read_u32(node, "arm,primecell-periphid", &dev->periphid);
  202. ret = of_address_to_resource(node, 0, &dev->res);
  203. if (ret) {
  204. pr_err("amba: of_address_to_resource() failed (%d) for %pOF\n",
  205. ret, node);
  206. goto err_free;
  207. }
  208. ret = amba_device_add(dev, &iomem_resource);
  209. if (ret) {
  210. pr_err("amba_device_add() failed (%d) for %pOF\n",
  211. ret, node);
  212. goto err_free;
  213. }
  214. return dev;
  215. err_free:
  216. amba_device_put(dev);
  217. err_clear_flag:
  218. of_node_clear_flag(node, OF_POPULATED);
  219. return NULL;
  220. }
  221. #else /* CONFIG_ARM_AMBA */
  222. static struct amba_device *of_amba_device_create(struct device_node *node,
  223. const char *bus_id,
  224. void *platform_data,
  225. struct device *parent)
  226. {
  227. return NULL;
  228. }
  229. #endif /* CONFIG_ARM_AMBA */
  230. /*
  231. * of_dev_lookup() - Given a device node, lookup the preferred Linux name
  232. */
  233. static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
  234. struct device_node *np)
  235. {
  236. const struct of_dev_auxdata *auxdata;
  237. struct resource res;
  238. int compatible = 0;
  239. if (!lookup)
  240. return NULL;
  241. auxdata = lookup;
  242. for (; auxdata->compatible; auxdata++) {
  243. if (!of_device_is_compatible(np, auxdata->compatible))
  244. continue;
  245. compatible++;
  246. if (!of_address_to_resource(np, 0, &res))
  247. if (res.start != auxdata->phys_addr)
  248. continue;
  249. pr_debug("%pOF: devname=%s\n", np, auxdata->name);
  250. return auxdata;
  251. }
  252. if (!compatible)
  253. return NULL;
  254. /* Try compatible match if no phys_addr and name are specified */
  255. auxdata = lookup;
  256. for (; auxdata->compatible; auxdata++) {
  257. if (!of_device_is_compatible(np, auxdata->compatible))
  258. continue;
  259. if (!auxdata->phys_addr && !auxdata->name) {
  260. pr_debug("%pOF: compatible match\n", np);
  261. return auxdata;
  262. }
  263. }
  264. return NULL;
  265. }
  266. /**
  267. * of_platform_bus_create() - Create a device for a node and its children.
  268. * @bus: device node of the bus to instantiate
  269. * @matches: match table for bus nodes
  270. * @lookup: auxdata table for matching id and platform_data with device nodes
  271. * @parent: parent for new device, or NULL for top level.
  272. * @strict: require compatible property
  273. *
  274. * Creates a platform_device for the provided device_node, and optionally
  275. * recursively create devices for all the child nodes.
  276. */
  277. static int of_platform_bus_create(struct device_node *bus,
  278. const struct of_device_id *matches,
  279. const struct of_dev_auxdata *lookup,
  280. struct device *parent, bool strict)
  281. {
  282. const struct of_dev_auxdata *auxdata;
  283. struct platform_device *dev;
  284. const char *bus_id = NULL;
  285. void *platform_data = NULL;
  286. int rc = 0;
  287. /* Make sure it has a compatible property */
  288. if (strict && (!of_property_present(bus, "compatible"))) {
  289. pr_debug("%s() - skipping %pOF, no compatible prop\n",
  290. __func__, bus);
  291. return 0;
  292. }
  293. /* Skip nodes for which we don't want to create devices */
  294. if (unlikely(of_match_node(of_skipped_node_table, bus))) {
  295. pr_debug("%s() - skipping %pOF node\n", __func__, bus);
  296. return 0;
  297. }
  298. if (of_node_check_flag(bus, OF_POPULATED_BUS)) {
  299. pr_debug("%s() - skipping %pOF, already populated\n",
  300. __func__, bus);
  301. return 0;
  302. }
  303. auxdata = of_dev_lookup(lookup, bus);
  304. if (auxdata) {
  305. bus_id = auxdata->name;
  306. platform_data = auxdata->platform_data;
  307. }
  308. if (of_device_is_compatible(bus, "arm,primecell")) {
  309. /*
  310. * Don't return an error here to keep compatibility with older
  311. * device tree files.
  312. */
  313. of_amba_device_create(bus, bus_id, platform_data, parent);
  314. return 0;
  315. }
  316. dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
  317. if (!dev || !of_match_node(matches, bus))
  318. return 0;
  319. for_each_child_of_node_scoped(bus, child) {
  320. pr_debug(" create child: %pOF\n", child);
  321. rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
  322. if (rc)
  323. break;
  324. }
  325. of_node_set_flag(bus, OF_POPULATED_BUS);
  326. return rc;
  327. }
  328. /**
  329. * of_platform_bus_probe() - Probe the device-tree for platform buses
  330. * @root: parent of the first level to probe or NULL for the root of the tree
  331. * @matches: match table for bus nodes
  332. * @parent: parent to hook devices from, NULL for toplevel
  333. *
  334. * Note that children of the provided root are not instantiated as devices
  335. * unless the specified root itself matches the bus list and is not NULL.
  336. */
  337. int of_platform_bus_probe(struct device_node *root,
  338. const struct of_device_id *matches,
  339. struct device *parent)
  340. {
  341. int rc = 0;
  342. root = root ? of_node_get(root) : of_find_node_by_path("/");
  343. if (!root)
  344. return -EINVAL;
  345. pr_debug("%s()\n", __func__);
  346. pr_debug(" starting at: %pOF\n", root);
  347. /* Do a self check of bus type, if there's a match, create children */
  348. if (of_match_node(matches, root)) {
  349. rc = of_platform_bus_create(root, matches, NULL, parent, false);
  350. } else {
  351. for_each_child_of_node_scoped(root, child) {
  352. if (!of_match_node(matches, child))
  353. continue;
  354. rc = of_platform_bus_create(child, matches, NULL, parent, false);
  355. if (rc)
  356. break;
  357. }
  358. }
  359. of_node_put(root);
  360. return rc;
  361. }
  362. EXPORT_SYMBOL(of_platform_bus_probe);
  363. /**
  364. * of_platform_populate() - Populate platform_devices from device tree data
  365. * @root: parent of the first level to probe or NULL for the root of the tree
  366. * @matches: match table, NULL to use the default
  367. * @lookup: auxdata table for matching id and platform_data with device nodes
  368. * @parent: parent to hook devices from, NULL for toplevel
  369. *
  370. * Similar to of_platform_bus_probe(), this function walks the device tree
  371. * and creates devices from nodes. It differs in that it follows the modern
  372. * convention of requiring all device nodes to have a 'compatible' property,
  373. * and it is suitable for creating devices which are children of the root
  374. * node (of_platform_bus_probe will only create children of the root which
  375. * are selected by the @matches argument).
  376. *
  377. * New board support should be using this function instead of
  378. * of_platform_bus_probe().
  379. *
  380. * Return: 0 on success, < 0 on failure.
  381. */
  382. int of_platform_populate(struct device_node *root,
  383. const struct of_device_id *matches,
  384. const struct of_dev_auxdata *lookup,
  385. struct device *parent)
  386. {
  387. int rc = 0;
  388. root = root ? of_node_get(root) : of_find_node_by_path("/");
  389. if (!root)
  390. return -EINVAL;
  391. pr_debug("%s()\n", __func__);
  392. pr_debug(" starting at: %pOF\n", root);
  393. device_links_supplier_sync_state_pause();
  394. for_each_child_of_node_scoped(root, child) {
  395. rc = of_platform_bus_create(child, matches, lookup, parent, true);
  396. if (rc)
  397. break;
  398. }
  399. device_links_supplier_sync_state_resume();
  400. of_node_set_flag(root, OF_POPULATED_BUS);
  401. of_node_put(root);
  402. return rc;
  403. }
  404. EXPORT_SYMBOL_GPL(of_platform_populate);
  405. int of_platform_default_populate(struct device_node *root,
  406. const struct of_dev_auxdata *lookup,
  407. struct device *parent)
  408. {
  409. static const struct of_device_id match_table[] = {
  410. { .compatible = "simple-bus", },
  411. { .compatible = "simple-mfd", },
  412. { .compatible = "isa", },
  413. #ifdef CONFIG_ARM_AMBA
  414. { .compatible = "arm,amba-bus", },
  415. #endif /* CONFIG_ARM_AMBA */
  416. {} /* Empty terminated list */
  417. };
  418. return of_platform_populate(root, match_table, lookup, parent);
  419. }
  420. EXPORT_SYMBOL_GPL(of_platform_default_populate);
  421. static const struct of_device_id reserved_mem_matches[] = {
  422. { .compatible = "phram" },
  423. { .compatible = "qcom,rmtfs-mem" },
  424. { .compatible = "qcom,cmd-db" },
  425. { .compatible = "qcom,smem" },
  426. { .compatible = "ramoops" },
  427. { .compatible = "nvmem-rmem" },
  428. { .compatible = "google,open-dice" },
  429. {}
  430. };
  431. static int __init of_platform_default_populate_init(void)
  432. {
  433. struct device_node *node;
  434. device_links_supplier_sync_state_pause();
  435. if (IS_ENABLED(CONFIG_PPC)) {
  436. struct device_node *boot_display = NULL;
  437. struct platform_device *dev;
  438. int display_number = 0;
  439. int ret;
  440. /* Check if we have a MacOS display without a node spec */
  441. if (of_property_present(of_chosen, "linux,bootx-noscreen")) {
  442. /*
  443. * The old code tried to work out which node was the MacOS
  444. * display based on the address. I'm dropping that since the
  445. * lack of a node spec only happens with old BootX versions
  446. * (users can update) and with this code, they'll still get
  447. * a display (just not the palette hacks).
  448. */
  449. dev = platform_device_alloc("bootx-noscreen", 0);
  450. if (WARN_ON(!dev))
  451. return -ENOMEM;
  452. ret = platform_device_add(dev);
  453. if (WARN_ON(ret)) {
  454. platform_device_put(dev);
  455. return ret;
  456. }
  457. }
  458. /*
  459. * For OF framebuffers, first create the device for the boot display,
  460. * then for the other framebuffers. Only fail for the boot display;
  461. * ignore errors for the rest.
  462. */
  463. for_each_node_by_type(node, "display") {
  464. if (!of_property_read_bool(node, "linux,opened") ||
  465. !of_property_read_bool(node, "linux,boot-display"))
  466. continue;
  467. dev = of_platform_device_create(node, "of-display", NULL);
  468. of_node_put(node);
  469. if (WARN_ON(!dev))
  470. return -ENOMEM;
  471. boot_display = node;
  472. display_number++;
  473. break;
  474. }
  475. for_each_node_by_type(node, "display") {
  476. char buf[14];
  477. const char *of_display_format = "of-display.%d";
  478. if (!of_property_read_bool(node, "linux,opened") || node == boot_display)
  479. continue;
  480. ret = snprintf(buf, sizeof(buf), of_display_format, display_number++);
  481. if (ret < sizeof(buf))
  482. of_platform_device_create(node, buf, NULL);
  483. }
  484. } else {
  485. /*
  486. * Handle certain compatibles explicitly, since we don't want to create
  487. * platform_devices for every node in /reserved-memory with a
  488. * "compatible",
  489. */
  490. for_each_matching_node(node, reserved_mem_matches)
  491. of_platform_device_create(node, NULL, NULL);
  492. node = of_find_node_by_path("/firmware");
  493. if (node) {
  494. of_platform_default_populate(node, NULL, NULL);
  495. of_node_put(node);
  496. }
  497. node = of_get_compatible_child(of_chosen, "simple-framebuffer");
  498. if (node) {
  499. /*
  500. * Since a "simple-framebuffer" device is already added
  501. * here, disable the Generic System Framebuffers (sysfb)
  502. * to prevent it from registering another device for the
  503. * system framebuffer later (e.g: using the screen_info
  504. * data that may had been filled as well).
  505. *
  506. * This can happen for example on DT systems that do EFI
  507. * booting and may provide a GOP handle to the EFI stub.
  508. */
  509. sysfb_disable(NULL);
  510. of_platform_device_create(node, NULL, NULL);
  511. of_node_put(node);
  512. }
  513. /* Populate everything else. */
  514. of_platform_default_populate(NULL, NULL, NULL);
  515. }
  516. return 0;
  517. }
  518. arch_initcall_sync(of_platform_default_populate_init);
  519. static int __init of_platform_sync_state_init(void)
  520. {
  521. device_links_supplier_sync_state_resume();
  522. return 0;
  523. }
  524. late_initcall_sync(of_platform_sync_state_init);
  525. int of_platform_device_destroy(struct device *dev, void *data)
  526. {
  527. /* Do not touch devices not populated from the device tree */
  528. if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
  529. return 0;
  530. /* Recurse for any nodes that were treated as busses */
  531. if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
  532. device_for_each_child(dev, NULL, of_platform_device_destroy);
  533. of_node_clear_flag(dev->of_node, OF_POPULATED);
  534. of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
  535. if (dev->bus == &platform_bus_type)
  536. platform_device_unregister(to_platform_device(dev));
  537. #ifdef CONFIG_ARM_AMBA
  538. else if (dev->bus == &amba_bustype)
  539. amba_device_unregister(to_amba_device(dev));
  540. #endif
  541. return 0;
  542. }
  543. EXPORT_SYMBOL_GPL(of_platform_device_destroy);
  544. /**
  545. * of_platform_depopulate() - Remove devices populated from device tree
  546. * @parent: device which children will be removed
  547. *
  548. * Complementary to of_platform_populate(), this function removes children
  549. * of the given device (and, recursively, their children) that have been
  550. * created from their respective device tree nodes (and only those,
  551. * leaving others - eg. manually created - unharmed).
  552. */
  553. void of_platform_depopulate(struct device *parent)
  554. {
  555. if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
  556. device_for_each_child_reverse(parent, NULL, of_platform_device_destroy);
  557. of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
  558. }
  559. }
  560. EXPORT_SYMBOL_GPL(of_platform_depopulate);
  561. static void devm_of_platform_populate_release(struct device *dev, void *res)
  562. {
  563. of_platform_depopulate(*(struct device **)res);
  564. }
  565. /**
  566. * devm_of_platform_populate() - Populate platform_devices from device tree data
  567. * @dev: device that requested to populate from device tree data
  568. *
  569. * Similar to of_platform_populate(), but will automatically call
  570. * of_platform_depopulate() when the device is unbound from the bus.
  571. *
  572. * Return: 0 on success, < 0 on failure.
  573. */
  574. int devm_of_platform_populate(struct device *dev)
  575. {
  576. struct device **ptr;
  577. int ret;
  578. if (!dev)
  579. return -EINVAL;
  580. ptr = devres_alloc(devm_of_platform_populate_release,
  581. sizeof(*ptr), GFP_KERNEL);
  582. if (!ptr)
  583. return -ENOMEM;
  584. ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
  585. if (ret) {
  586. devres_free(ptr);
  587. } else {
  588. *ptr = dev;
  589. devres_add(dev, ptr);
  590. }
  591. return ret;
  592. }
  593. EXPORT_SYMBOL_GPL(devm_of_platform_populate);
  594. static int devm_of_platform_match(struct device *dev, void *res, void *data)
  595. {
  596. struct device **ptr = res;
  597. if (!ptr) {
  598. WARN_ON(!ptr);
  599. return 0;
  600. }
  601. return *ptr == data;
  602. }
  603. /**
  604. * devm_of_platform_depopulate() - Remove devices populated from device tree
  605. * @dev: device that requested to depopulate from device tree data
  606. *
  607. * Complementary to devm_of_platform_populate(), this function removes children
  608. * of the given device (and, recursively, their children) that have been
  609. * created from their respective device tree nodes (and only those,
  610. * leaving others - eg. manually created - unharmed).
  611. */
  612. void devm_of_platform_depopulate(struct device *dev)
  613. {
  614. int ret;
  615. ret = devres_release(dev, devm_of_platform_populate_release,
  616. devm_of_platform_match, dev);
  617. WARN_ON(ret);
  618. }
  619. EXPORT_SYMBOL_GPL(devm_of_platform_depopulate);
  620. #ifdef CONFIG_OF_DYNAMIC
  621. static int of_platform_notify(struct notifier_block *nb,
  622. unsigned long action, void *arg)
  623. {
  624. struct of_reconfig_data *rd = arg;
  625. struct platform_device *pdev_parent, *pdev;
  626. bool children_left;
  627. struct device_node *parent;
  628. switch (of_reconfig_get_state_change(action, rd)) {
  629. case OF_RECONFIG_CHANGE_ADD:
  630. parent = rd->dn->parent;
  631. /* verify that the parent is a bus (or the root node) */
  632. if (!of_node_is_root(parent) &&
  633. !of_node_check_flag(parent, OF_POPULATED_BUS))
  634. return NOTIFY_OK; /* not for us */
  635. /* already populated? (driver using of_populate manually) */
  636. if (of_node_check_flag(rd->dn, OF_POPULATED))
  637. return NOTIFY_OK;
  638. /*
  639. * Clear the flag before adding the device so that fw_devlink
  640. * doesn't skip adding consumers to this device.
  641. */
  642. rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
  643. /* pdev_parent may be NULL when no bus platform device */
  644. pdev_parent = of_find_device_by_node(parent);
  645. pdev = of_platform_device_create(rd->dn, NULL,
  646. pdev_parent ? &pdev_parent->dev : NULL);
  647. platform_device_put(pdev_parent);
  648. if (pdev == NULL) {
  649. pr_err("%s: failed to create for '%pOF'\n",
  650. __func__, rd->dn);
  651. /* of_platform_device_create tosses the error code */
  652. return notifier_from_errno(-EINVAL);
  653. }
  654. break;
  655. case OF_RECONFIG_CHANGE_REMOVE:
  656. /* already depopulated? */
  657. if (!of_node_check_flag(rd->dn, OF_POPULATED))
  658. return NOTIFY_OK;
  659. /* find our device by node */
  660. pdev = of_find_device_by_node(rd->dn);
  661. if (pdev == NULL)
  662. return NOTIFY_OK; /* no? not meant for us */
  663. /* unregister takes one ref away */
  664. of_platform_device_destroy(&pdev->dev, &children_left);
  665. /* and put the reference of the find */
  666. platform_device_put(pdev);
  667. break;
  668. }
  669. return NOTIFY_OK;
  670. }
  671. static struct notifier_block platform_of_notifier = {
  672. .notifier_call = of_platform_notify,
  673. };
  674. void of_platform_register_reconfig_notifier(void)
  675. {
  676. WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
  677. }
  678. #endif /* CONFIG_OF_DYNAMIC */
  679. #endif /* CONFIG_OF_ADDRESS */