platform.c 22 KB

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