platform.c 19 KB

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