platform.c 15 KB

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