platform.c 20 KB

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