platform.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. #include <linux/errno.h>
  15. #include <linux/module.h>
  16. #include <linux/device.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/slab.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/of_platform.h>
  23. #if defined(CONFIG_PPC_DCR)
  24. #include <asm/dcr.h>
  25. #endif
  26. extern struct device_attribute of_platform_device_attrs[];
  27. static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
  28. {
  29. const struct of_device_id *matches = drv->of_match_table;
  30. if (!matches)
  31. return 0;
  32. return of_match_device(matches, dev) != NULL;
  33. }
  34. static int of_platform_device_probe(struct device *dev)
  35. {
  36. int error = -ENODEV;
  37. struct of_platform_driver *drv;
  38. struct of_device *of_dev;
  39. const struct of_device_id *match;
  40. drv = to_of_platform_driver(dev->driver);
  41. of_dev = to_of_device(dev);
  42. if (!drv->probe)
  43. return error;
  44. of_dev_get(of_dev);
  45. match = of_match_device(drv->driver.of_match_table, dev);
  46. if (match)
  47. error = drv->probe(of_dev, match);
  48. if (error)
  49. of_dev_put(of_dev);
  50. return error;
  51. }
  52. static int of_platform_device_remove(struct device *dev)
  53. {
  54. struct of_device *of_dev = to_of_device(dev);
  55. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  56. if (dev->driver && drv->remove)
  57. drv->remove(of_dev);
  58. return 0;
  59. }
  60. static void of_platform_device_shutdown(struct device *dev)
  61. {
  62. struct of_device *of_dev = to_of_device(dev);
  63. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  64. if (dev->driver && drv->shutdown)
  65. drv->shutdown(of_dev);
  66. }
  67. #ifdef CONFIG_PM_SLEEP
  68. static int of_platform_legacy_suspend(struct device *dev, pm_message_t mesg)
  69. {
  70. struct of_device *of_dev = to_of_device(dev);
  71. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  72. int ret = 0;
  73. if (dev->driver && drv->suspend)
  74. ret = drv->suspend(of_dev, mesg);
  75. return ret;
  76. }
  77. static int of_platform_legacy_resume(struct device *dev)
  78. {
  79. struct of_device *of_dev = to_of_device(dev);
  80. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  81. int ret = 0;
  82. if (dev->driver && drv->resume)
  83. ret = drv->resume(of_dev);
  84. return ret;
  85. }
  86. static int of_platform_pm_prepare(struct device *dev)
  87. {
  88. struct device_driver *drv = dev->driver;
  89. int ret = 0;
  90. if (drv && drv->pm && drv->pm->prepare)
  91. ret = drv->pm->prepare(dev);
  92. return ret;
  93. }
  94. static void of_platform_pm_complete(struct device *dev)
  95. {
  96. struct device_driver *drv = dev->driver;
  97. if (drv && drv->pm && drv->pm->complete)
  98. drv->pm->complete(dev);
  99. }
  100. #ifdef CONFIG_SUSPEND
  101. static int of_platform_pm_suspend(struct device *dev)
  102. {
  103. struct device_driver *drv = dev->driver;
  104. int ret = 0;
  105. if (!drv)
  106. return 0;
  107. if (drv->pm) {
  108. if (drv->pm->suspend)
  109. ret = drv->pm->suspend(dev);
  110. } else {
  111. ret = of_platform_legacy_suspend(dev, PMSG_SUSPEND);
  112. }
  113. return ret;
  114. }
  115. static int of_platform_pm_suspend_noirq(struct device *dev)
  116. {
  117. struct device_driver *drv = dev->driver;
  118. int ret = 0;
  119. if (!drv)
  120. return 0;
  121. if (drv->pm) {
  122. if (drv->pm->suspend_noirq)
  123. ret = drv->pm->suspend_noirq(dev);
  124. }
  125. return ret;
  126. }
  127. static int of_platform_pm_resume(struct device *dev)
  128. {
  129. struct device_driver *drv = dev->driver;
  130. int ret = 0;
  131. if (!drv)
  132. return 0;
  133. if (drv->pm) {
  134. if (drv->pm->resume)
  135. ret = drv->pm->resume(dev);
  136. } else {
  137. ret = of_platform_legacy_resume(dev);
  138. }
  139. return ret;
  140. }
  141. static int of_platform_pm_resume_noirq(struct device *dev)
  142. {
  143. struct device_driver *drv = dev->driver;
  144. int ret = 0;
  145. if (!drv)
  146. return 0;
  147. if (drv->pm) {
  148. if (drv->pm->resume_noirq)
  149. ret = drv->pm->resume_noirq(dev);
  150. }
  151. return ret;
  152. }
  153. #else /* !CONFIG_SUSPEND */
  154. #define of_platform_pm_suspend NULL
  155. #define of_platform_pm_resume NULL
  156. #define of_platform_pm_suspend_noirq NULL
  157. #define of_platform_pm_resume_noirq NULL
  158. #endif /* !CONFIG_SUSPEND */
  159. #ifdef CONFIG_HIBERNATION
  160. static int of_platform_pm_freeze(struct device *dev)
  161. {
  162. struct device_driver *drv = dev->driver;
  163. int ret = 0;
  164. if (!drv)
  165. return 0;
  166. if (drv->pm) {
  167. if (drv->pm->freeze)
  168. ret = drv->pm->freeze(dev);
  169. } else {
  170. ret = of_platform_legacy_suspend(dev, PMSG_FREEZE);
  171. }
  172. return ret;
  173. }
  174. static int of_platform_pm_freeze_noirq(struct device *dev)
  175. {
  176. struct device_driver *drv = dev->driver;
  177. int ret = 0;
  178. if (!drv)
  179. return 0;
  180. if (drv->pm) {
  181. if (drv->pm->freeze_noirq)
  182. ret = drv->pm->freeze_noirq(dev);
  183. }
  184. return ret;
  185. }
  186. static int of_platform_pm_thaw(struct device *dev)
  187. {
  188. struct device_driver *drv = dev->driver;
  189. int ret = 0;
  190. if (!drv)
  191. return 0;
  192. if (drv->pm) {
  193. if (drv->pm->thaw)
  194. ret = drv->pm->thaw(dev);
  195. } else {
  196. ret = of_platform_legacy_resume(dev);
  197. }
  198. return ret;
  199. }
  200. static int of_platform_pm_thaw_noirq(struct device *dev)
  201. {
  202. struct device_driver *drv = dev->driver;
  203. int ret = 0;
  204. if (!drv)
  205. return 0;
  206. if (drv->pm) {
  207. if (drv->pm->thaw_noirq)
  208. ret = drv->pm->thaw_noirq(dev);
  209. }
  210. return ret;
  211. }
  212. static int of_platform_pm_poweroff(struct device *dev)
  213. {
  214. struct device_driver *drv = dev->driver;
  215. int ret = 0;
  216. if (!drv)
  217. return 0;
  218. if (drv->pm) {
  219. if (drv->pm->poweroff)
  220. ret = drv->pm->poweroff(dev);
  221. } else {
  222. ret = of_platform_legacy_suspend(dev, PMSG_HIBERNATE);
  223. }
  224. return ret;
  225. }
  226. static int of_platform_pm_poweroff_noirq(struct device *dev)
  227. {
  228. struct device_driver *drv = dev->driver;
  229. int ret = 0;
  230. if (!drv)
  231. return 0;
  232. if (drv->pm) {
  233. if (drv->pm->poweroff_noirq)
  234. ret = drv->pm->poweroff_noirq(dev);
  235. }
  236. return ret;
  237. }
  238. static int of_platform_pm_restore(struct device *dev)
  239. {
  240. struct device_driver *drv = dev->driver;
  241. int ret = 0;
  242. if (!drv)
  243. return 0;
  244. if (drv->pm) {
  245. if (drv->pm->restore)
  246. ret = drv->pm->restore(dev);
  247. } else {
  248. ret = of_platform_legacy_resume(dev);
  249. }
  250. return ret;
  251. }
  252. static int of_platform_pm_restore_noirq(struct device *dev)
  253. {
  254. struct device_driver *drv = dev->driver;
  255. int ret = 0;
  256. if (!drv)
  257. return 0;
  258. if (drv->pm) {
  259. if (drv->pm->restore_noirq)
  260. ret = drv->pm->restore_noirq(dev);
  261. }
  262. return ret;
  263. }
  264. #else /* !CONFIG_HIBERNATION */
  265. #define of_platform_pm_freeze NULL
  266. #define of_platform_pm_thaw NULL
  267. #define of_platform_pm_poweroff NULL
  268. #define of_platform_pm_restore NULL
  269. #define of_platform_pm_freeze_noirq NULL
  270. #define of_platform_pm_thaw_noirq NULL
  271. #define of_platform_pm_poweroff_noirq NULL
  272. #define of_platform_pm_restore_noirq NULL
  273. #endif /* !CONFIG_HIBERNATION */
  274. static struct dev_pm_ops of_platform_dev_pm_ops = {
  275. .prepare = of_platform_pm_prepare,
  276. .complete = of_platform_pm_complete,
  277. .suspend = of_platform_pm_suspend,
  278. .resume = of_platform_pm_resume,
  279. .freeze = of_platform_pm_freeze,
  280. .thaw = of_platform_pm_thaw,
  281. .poweroff = of_platform_pm_poweroff,
  282. .restore = of_platform_pm_restore,
  283. .suspend_noirq = of_platform_pm_suspend_noirq,
  284. .resume_noirq = of_platform_pm_resume_noirq,
  285. .freeze_noirq = of_platform_pm_freeze_noirq,
  286. .thaw_noirq = of_platform_pm_thaw_noirq,
  287. .poweroff_noirq = of_platform_pm_poweroff_noirq,
  288. .restore_noirq = of_platform_pm_restore_noirq,
  289. };
  290. #define OF_PLATFORM_PM_OPS_PTR (&of_platform_dev_pm_ops)
  291. #else /* !CONFIG_PM_SLEEP */
  292. #define OF_PLATFORM_PM_OPS_PTR NULL
  293. #endif /* !CONFIG_PM_SLEEP */
  294. int of_bus_type_init(struct bus_type *bus, const char *name)
  295. {
  296. bus->name = name;
  297. bus->match = of_platform_bus_match;
  298. bus->probe = of_platform_device_probe;
  299. bus->remove = of_platform_device_remove;
  300. bus->shutdown = of_platform_device_shutdown;
  301. bus->dev_attrs = of_platform_device_attrs;
  302. bus->pm = OF_PLATFORM_PM_OPS_PTR;
  303. return bus_register(bus);
  304. }
  305. int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
  306. {
  307. drv->driver.bus = bus;
  308. /* register with core */
  309. return driver_register(&drv->driver);
  310. }
  311. EXPORT_SYMBOL(of_register_driver);
  312. void of_unregister_driver(struct of_platform_driver *drv)
  313. {
  314. driver_unregister(&drv->driver);
  315. }
  316. EXPORT_SYMBOL(of_unregister_driver);
  317. #if !defined(CONFIG_SPARC)
  318. /*
  319. * The following routines scan a subtree and registers a device for
  320. * each applicable node.
  321. *
  322. * Note: sparc doesn't use these routines because it has a different
  323. * mechanism for creating devices from device tree nodes.
  324. */
  325. /**
  326. * of_device_make_bus_id - Use the device node data to assign a unique name
  327. * @dev: pointer to device structure that is linked to a device tree node
  328. *
  329. * This routine will first try using either the dcr-reg or the reg property
  330. * value to derive a unique name. As a last resort it will use the node
  331. * name followed by a unique number.
  332. */
  333. static void of_device_make_bus_id(struct device *dev)
  334. {
  335. static atomic_t bus_no_reg_magic;
  336. struct device_node *node = dev->of_node;
  337. const u32 *reg;
  338. u64 addr;
  339. int magic;
  340. #ifdef CONFIG_PPC_DCR
  341. /*
  342. * If it's a DCR based device, use 'd' for native DCRs
  343. * and 'D' for MMIO DCRs.
  344. */
  345. reg = of_get_property(node, "dcr-reg", NULL);
  346. if (reg) {
  347. #ifdef CONFIG_PPC_DCR_NATIVE
  348. dev_set_name(dev, "d%x.%s", *reg, node->name);
  349. #else /* CONFIG_PPC_DCR_NATIVE */
  350. u64 addr = of_translate_dcr_address(node, *reg, NULL);
  351. if (addr != OF_BAD_ADDR) {
  352. dev_set_name(dev, "D%llx.%s",
  353. (unsigned long long)addr, node->name);
  354. return;
  355. }
  356. #endif /* !CONFIG_PPC_DCR_NATIVE */
  357. }
  358. #endif /* CONFIG_PPC_DCR */
  359. /*
  360. * For MMIO, get the physical address
  361. */
  362. reg = of_get_property(node, "reg", NULL);
  363. if (reg) {
  364. addr = of_translate_address(node, reg);
  365. if (addr != OF_BAD_ADDR) {
  366. dev_set_name(dev, "%llx.%s",
  367. (unsigned long long)addr, node->name);
  368. return;
  369. }
  370. }
  371. /*
  372. * No BusID, use the node name and add a globally incremented
  373. * counter (and pray...)
  374. */
  375. magic = atomic_add_return(1, &bus_no_reg_magic);
  376. dev_set_name(dev, "%s.%d", node->name, magic - 1);
  377. }
  378. /**
  379. * of_device_alloc - Allocate and initialize an of_device
  380. * @np: device node to assign to device
  381. * @bus_id: Name to assign to the device. May be null to use default name.
  382. * @parent: Parent device.
  383. */
  384. struct of_device *of_device_alloc(struct device_node *np,
  385. const char *bus_id,
  386. struct device *parent)
  387. {
  388. struct of_device *dev;
  389. int rc, i, num_reg = 0, num_irq = 0;
  390. struct resource *res, temp_res;
  391. /* First count how many resources are needed */
  392. while (of_address_to_resource(np, num_reg, &temp_res) == 0)
  393. num_reg++;
  394. while (of_irq_to_resource(np, num_irq, &temp_res) != NO_IRQ)
  395. num_irq++;
  396. /* Allocate memory for both the struct device and the resource table */
  397. dev = kzalloc(sizeof(*dev) + (sizeof(*res) * (num_reg + num_irq)),
  398. GFP_KERNEL);
  399. if (!dev)
  400. return NULL;
  401. res = (struct resource *) &dev[1];
  402. /* Populate the resource table */
  403. if (num_irq || num_reg) {
  404. dev->num_resources = num_reg + num_irq;
  405. dev->resource = res;
  406. for (i = 0; i < num_reg; i++, res++) {
  407. rc = of_address_to_resource(np, i, res);
  408. WARN_ON(rc);
  409. }
  410. for (i = 0; i < num_irq; i++, res++) {
  411. rc = of_irq_to_resource(np, i, res);
  412. WARN_ON(rc == NO_IRQ);
  413. }
  414. }
  415. dev->dev.of_node = of_node_get(np);
  416. #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
  417. dev->dev.dma_mask = &dev->archdata.dma_mask;
  418. #endif
  419. dev->dev.parent = parent;
  420. dev->dev.release = of_release_dev;
  421. if (bus_id)
  422. dev_set_name(&dev->dev, "%s", bus_id);
  423. else
  424. of_device_make_bus_id(&dev->dev);
  425. return dev;
  426. }
  427. EXPORT_SYMBOL(of_device_alloc);
  428. /**
  429. * of_platform_device_create - Alloc, initialize and register an of_device
  430. * @np: pointer to node to create device for
  431. * @bus_id: name to assign device
  432. * @parent: Linux device model parent device.
  433. */
  434. struct of_device *of_platform_device_create(struct device_node *np,
  435. const char *bus_id,
  436. struct device *parent)
  437. {
  438. struct of_device *dev;
  439. dev = of_device_alloc(np, bus_id, parent);
  440. if (!dev)
  441. return NULL;
  442. #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
  443. dev->archdata.dma_mask = 0xffffffffUL;
  444. #endif
  445. dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  446. dev->dev.bus = &of_platform_bus_type;
  447. /* We do not fill the DMA ops for platform devices by default.
  448. * This is currently the responsibility of the platform code
  449. * to do such, possibly using a device notifier
  450. */
  451. if (of_device_register(dev) != 0) {
  452. of_device_free(dev);
  453. return NULL;
  454. }
  455. return dev;
  456. }
  457. EXPORT_SYMBOL(of_platform_device_create);
  458. /**
  459. * of_platform_bus_create - Create an OF device for a bus node and all its
  460. * children. Optionally recursively instantiate matching busses.
  461. * @bus: device node of the bus to instantiate
  462. * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
  463. * disallow recursive creation of child busses
  464. */
  465. static int of_platform_bus_create(const struct device_node *bus,
  466. const struct of_device_id *matches,
  467. struct device *parent)
  468. {
  469. struct device_node *child;
  470. struct of_device *dev;
  471. int rc = 0;
  472. for_each_child_of_node(bus, child) {
  473. pr_debug(" create child: %s\n", child->full_name);
  474. dev = of_platform_device_create(child, NULL, parent);
  475. if (dev == NULL)
  476. rc = -ENOMEM;
  477. else if (!of_match_node(matches, child))
  478. continue;
  479. if (rc == 0) {
  480. pr_debug(" and sub busses\n");
  481. rc = of_platform_bus_create(child, matches, &dev->dev);
  482. }
  483. if (rc) {
  484. of_node_put(child);
  485. break;
  486. }
  487. }
  488. return rc;
  489. }
  490. /**
  491. * of_platform_bus_probe - Probe the device-tree for platform busses
  492. * @root: parent of the first level to probe or NULL for the root of the tree
  493. * @matches: match table, NULL to use the default
  494. * @parent: parent to hook devices from, NULL for toplevel
  495. *
  496. * Note that children of the provided root are not instantiated as devices
  497. * unless the specified root itself matches the bus list and is not NULL.
  498. */
  499. int of_platform_bus_probe(struct device_node *root,
  500. const struct of_device_id *matches,
  501. struct device *parent)
  502. {
  503. struct device_node *child;
  504. struct of_device *dev;
  505. int rc = 0;
  506. if (matches == NULL)
  507. matches = of_default_bus_ids;
  508. if (matches == OF_NO_DEEP_PROBE)
  509. return -EINVAL;
  510. if (root == NULL)
  511. root = of_find_node_by_path("/");
  512. else
  513. of_node_get(root);
  514. if (root == NULL)
  515. return -EINVAL;
  516. pr_debug("of_platform_bus_probe()\n");
  517. pr_debug(" starting at: %s\n", root->full_name);
  518. /* Do a self check of bus type, if there's a match, create
  519. * children
  520. */
  521. if (of_match_node(matches, root)) {
  522. pr_debug(" root match, create all sub devices\n");
  523. dev = of_platform_device_create(root, NULL, parent);
  524. if (dev == NULL) {
  525. rc = -ENOMEM;
  526. goto bail;
  527. }
  528. pr_debug(" create all sub busses\n");
  529. rc = of_platform_bus_create(root, matches, &dev->dev);
  530. goto bail;
  531. }
  532. for_each_child_of_node(root, child) {
  533. if (!of_match_node(matches, child))
  534. continue;
  535. pr_debug(" match: %s\n", child->full_name);
  536. dev = of_platform_device_create(child, NULL, parent);
  537. if (dev == NULL)
  538. rc = -ENOMEM;
  539. else
  540. rc = of_platform_bus_create(child, matches, &dev->dev);
  541. if (rc) {
  542. of_node_put(child);
  543. break;
  544. }
  545. }
  546. bail:
  547. of_node_put(root);
  548. return rc;
  549. }
  550. EXPORT_SYMBOL(of_platform_bus_probe);
  551. #endif /* !CONFIG_SPARC */