platform.c 22 KB

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