platform.c 19 KB

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