platform.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /*
  2. * platform.c - platform 'pseudo' bus for legacy devices
  3. *
  4. * Copyright (c) 2002-3 Patrick Mochel
  5. * Copyright (c) 2002-3 Open Source Development Labs
  6. *
  7. * This file is released under the GPLv2
  8. *
  9. * Please see Documentation/driver-model/platform.txt for more
  10. * information.
  11. */
  12. #include <linux/platform_device.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/bootmem.h>
  17. #include <linux/err.h>
  18. #include <linux/slab.h>
  19. #include "base.h"
  20. #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
  21. driver))
  22. struct device platform_bus = {
  23. .bus_id = "platform",
  24. };
  25. EXPORT_SYMBOL_GPL(platform_bus);
  26. /**
  27. * platform_get_resource - get a resource for a device
  28. * @dev: platform device
  29. * @type: resource type
  30. * @num: resource index
  31. */
  32. struct resource *platform_get_resource(struct platform_device *dev,
  33. unsigned int type, unsigned int num)
  34. {
  35. int i;
  36. for (i = 0; i < dev->num_resources; i++) {
  37. struct resource *r = &dev->resource[i];
  38. if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
  39. IORESOURCE_IRQ|IORESOURCE_DMA)) == type)
  40. if (num-- == 0)
  41. return r;
  42. }
  43. return NULL;
  44. }
  45. EXPORT_SYMBOL_GPL(platform_get_resource);
  46. /**
  47. * platform_get_irq - get an IRQ for a device
  48. * @dev: platform device
  49. * @num: IRQ number index
  50. */
  51. int platform_get_irq(struct platform_device *dev, unsigned int num)
  52. {
  53. struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
  54. return r ? r->start : -ENXIO;
  55. }
  56. EXPORT_SYMBOL_GPL(platform_get_irq);
  57. /**
  58. * platform_get_resource_byname - get a resource for a device by name
  59. * @dev: platform device
  60. * @type: resource type
  61. * @name: resource name
  62. */
  63. struct resource *platform_get_resource_byname(struct platform_device *dev,
  64. unsigned int type, char *name)
  65. {
  66. int i;
  67. for (i = 0; i < dev->num_resources; i++) {
  68. struct resource *r = &dev->resource[i];
  69. if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
  70. IORESOURCE_IRQ|IORESOURCE_DMA)) == type)
  71. if (!strcmp(r->name, name))
  72. return r;
  73. }
  74. return NULL;
  75. }
  76. EXPORT_SYMBOL_GPL(platform_get_resource_byname);
  77. /**
  78. * platform_get_irq - get an IRQ for a device
  79. * @dev: platform device
  80. * @name: IRQ name
  81. */
  82. int platform_get_irq_byname(struct platform_device *dev, char *name)
  83. {
  84. struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ,
  85. name);
  86. return r ? r->start : -ENXIO;
  87. }
  88. EXPORT_SYMBOL_GPL(platform_get_irq_byname);
  89. /**
  90. * platform_add_devices - add a numbers of platform devices
  91. * @devs: array of platform devices to add
  92. * @num: number of platform devices in array
  93. */
  94. int platform_add_devices(struct platform_device **devs, int num)
  95. {
  96. int i, ret = 0;
  97. for (i = 0; i < num; i++) {
  98. ret = platform_device_register(devs[i]);
  99. if (ret) {
  100. while (--i >= 0)
  101. platform_device_unregister(devs[i]);
  102. break;
  103. }
  104. }
  105. return ret;
  106. }
  107. EXPORT_SYMBOL_GPL(platform_add_devices);
  108. struct platform_object {
  109. struct platform_device pdev;
  110. char name[1];
  111. };
  112. /**
  113. * platform_device_put
  114. * @pdev: platform device to free
  115. *
  116. * Free all memory associated with a platform device. This function must
  117. * _only_ be externally called in error cases. All other usage is a bug.
  118. */
  119. void platform_device_put(struct platform_device *pdev)
  120. {
  121. if (pdev)
  122. put_device(&pdev->dev);
  123. }
  124. EXPORT_SYMBOL_GPL(platform_device_put);
  125. static void platform_device_release(struct device *dev)
  126. {
  127. struct platform_object *pa = container_of(dev, struct platform_object,
  128. pdev.dev);
  129. kfree(pa->pdev.dev.platform_data);
  130. kfree(pa->pdev.resource);
  131. kfree(pa);
  132. }
  133. /**
  134. * platform_device_alloc
  135. * @name: base name of the device we're adding
  136. * @id: instance id
  137. *
  138. * Create a platform device object which can have other objects attached
  139. * to it, and which will have attached objects freed when it is released.
  140. */
  141. struct platform_device *platform_device_alloc(const char *name, int id)
  142. {
  143. struct platform_object *pa;
  144. pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
  145. if (pa) {
  146. strcpy(pa->name, name);
  147. pa->pdev.name = pa->name;
  148. pa->pdev.id = id;
  149. device_initialize(&pa->pdev.dev);
  150. pa->pdev.dev.release = platform_device_release;
  151. }
  152. return pa ? &pa->pdev : NULL;
  153. }
  154. EXPORT_SYMBOL_GPL(platform_device_alloc);
  155. /**
  156. * platform_device_add_resources
  157. * @pdev: platform device allocated by platform_device_alloc to add resources to
  158. * @res: set of resources that needs to be allocated for the device
  159. * @num: number of resources
  160. *
  161. * Add a copy of the resources to the platform device. The memory
  162. * associated with the resources will be freed when the platform device is
  163. * released.
  164. */
  165. int platform_device_add_resources(struct platform_device *pdev,
  166. struct resource *res, unsigned int num)
  167. {
  168. struct resource *r;
  169. r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL);
  170. if (r) {
  171. memcpy(r, res, sizeof(struct resource) * num);
  172. pdev->resource = r;
  173. pdev->num_resources = num;
  174. }
  175. return r ? 0 : -ENOMEM;
  176. }
  177. EXPORT_SYMBOL_GPL(platform_device_add_resources);
  178. /**
  179. * platform_device_add_data
  180. * @pdev: platform device allocated by platform_device_alloc to add resources to
  181. * @data: platform specific data for this platform device
  182. * @size: size of platform specific data
  183. *
  184. * Add a copy of platform specific data to the platform device's
  185. * platform_data pointer. The memory associated with the platform data
  186. * will be freed when the platform device is released.
  187. */
  188. int platform_device_add_data(struct platform_device *pdev, const void *data,
  189. size_t size)
  190. {
  191. void *d;
  192. d = kmalloc(size, GFP_KERNEL);
  193. if (d) {
  194. memcpy(d, data, size);
  195. pdev->dev.platform_data = d;
  196. }
  197. return d ? 0 : -ENOMEM;
  198. }
  199. EXPORT_SYMBOL_GPL(platform_device_add_data);
  200. /**
  201. * platform_device_add - add a platform device to device hierarchy
  202. * @pdev: platform device we're adding
  203. *
  204. * This is part 2 of platform_device_register(), though may be called
  205. * separately _iff_ pdev was allocated by platform_device_alloc().
  206. */
  207. int platform_device_add(struct platform_device *pdev)
  208. {
  209. int i, ret = 0;
  210. if (!pdev)
  211. return -EINVAL;
  212. if (!pdev->dev.parent)
  213. pdev->dev.parent = &platform_bus;
  214. pdev->dev.bus = &platform_bus_type;
  215. if (pdev->id != -1)
  216. snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%d", pdev->name,
  217. pdev->id);
  218. else
  219. strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE);
  220. for (i = 0; i < pdev->num_resources; i++) {
  221. struct resource *p, *r = &pdev->resource[i];
  222. if (r->name == NULL)
  223. r->name = pdev->dev.bus_id;
  224. p = r->parent;
  225. if (!p) {
  226. if (r->flags & IORESOURCE_MEM)
  227. p = &iomem_resource;
  228. else if (r->flags & IORESOURCE_IO)
  229. p = &ioport_resource;
  230. }
  231. if (p && insert_resource(p, r)) {
  232. printk(KERN_ERR
  233. "%s: failed to claim resource %d\n",
  234. pdev->dev.bus_id, i);
  235. ret = -EBUSY;
  236. goto failed;
  237. }
  238. }
  239. pr_debug("Registering platform device '%s'. Parent at %s\n",
  240. pdev->dev.bus_id, pdev->dev.parent->bus_id);
  241. ret = device_add(&pdev->dev);
  242. if (ret == 0)
  243. return ret;
  244. failed:
  245. while (--i >= 0)
  246. if (pdev->resource[i].flags & (IORESOURCE_MEM|IORESOURCE_IO))
  247. release_resource(&pdev->resource[i]);
  248. return ret;
  249. }
  250. EXPORT_SYMBOL_GPL(platform_device_add);
  251. /**
  252. * platform_device_del - remove a platform-level device
  253. * @pdev: platform device we're removing
  254. *
  255. * Note that this function will also release all memory- and port-based
  256. * resources owned by the device (@dev->resource). This function must
  257. * _only_ be externally called in error cases. All other usage is a bug.
  258. */
  259. void platform_device_del(struct platform_device *pdev)
  260. {
  261. int i;
  262. if (pdev) {
  263. device_del(&pdev->dev);
  264. for (i = 0; i < pdev->num_resources; i++) {
  265. struct resource *r = &pdev->resource[i];
  266. if (r->flags & (IORESOURCE_MEM|IORESOURCE_IO))
  267. release_resource(r);
  268. }
  269. }
  270. }
  271. EXPORT_SYMBOL_GPL(platform_device_del);
  272. /**
  273. * platform_device_register - add a platform-level device
  274. * @pdev: platform device we're adding
  275. */
  276. int platform_device_register(struct platform_device *pdev)
  277. {
  278. device_initialize(&pdev->dev);
  279. return platform_device_add(pdev);
  280. }
  281. EXPORT_SYMBOL_GPL(platform_device_register);
  282. /**
  283. * platform_device_unregister - unregister a platform-level device
  284. * @pdev: platform device we're unregistering
  285. *
  286. * Unregistration is done in 2 steps. First we release all resources
  287. * and remove it from the subsystem, then we drop reference count by
  288. * calling platform_device_put().
  289. */
  290. void platform_device_unregister(struct platform_device *pdev)
  291. {
  292. platform_device_del(pdev);
  293. platform_device_put(pdev);
  294. }
  295. EXPORT_SYMBOL_GPL(platform_device_unregister);
  296. /**
  297. * platform_device_register_simple
  298. * @name: base name of the device we're adding
  299. * @id: instance id
  300. * @res: set of resources that needs to be allocated for the device
  301. * @num: number of resources
  302. *
  303. * This function creates a simple platform device that requires minimal
  304. * resource and memory management. Canned release function freeing memory
  305. * allocated for the device allows drivers using such devices to be
  306. * unloaded without waiting for the last reference to the device to be
  307. * dropped.
  308. *
  309. * This interface is primarily intended for use with legacy drivers which
  310. * probe hardware directly. Because such drivers create sysfs device nodes
  311. * themselves, rather than letting system infrastructure handle such device
  312. * enumeration tasks, they don't fully conform to the Linux driver model.
  313. * In particular, when such drivers are built as modules, they can't be
  314. * "hotplugged".
  315. */
  316. struct platform_device *platform_device_register_simple(const char *name,
  317. int id,
  318. struct resource *res,
  319. unsigned int num)
  320. {
  321. struct platform_device *pdev;
  322. int retval;
  323. pdev = platform_device_alloc(name, id);
  324. if (!pdev) {
  325. retval = -ENOMEM;
  326. goto error;
  327. }
  328. if (num) {
  329. retval = platform_device_add_resources(pdev, res, num);
  330. if (retval)
  331. goto error;
  332. }
  333. retval = platform_device_add(pdev);
  334. if (retval)
  335. goto error;
  336. return pdev;
  337. error:
  338. platform_device_put(pdev);
  339. return ERR_PTR(retval);
  340. }
  341. EXPORT_SYMBOL_GPL(platform_device_register_simple);
  342. static int platform_drv_probe(struct device *_dev)
  343. {
  344. struct platform_driver *drv = to_platform_driver(_dev->driver);
  345. struct platform_device *dev = to_platform_device(_dev);
  346. return drv->probe(dev);
  347. }
  348. static int platform_drv_probe_fail(struct device *_dev)
  349. {
  350. return -ENXIO;
  351. }
  352. static int platform_drv_remove(struct device *_dev)
  353. {
  354. struct platform_driver *drv = to_platform_driver(_dev->driver);
  355. struct platform_device *dev = to_platform_device(_dev);
  356. return drv->remove(dev);
  357. }
  358. static void platform_drv_shutdown(struct device *_dev)
  359. {
  360. struct platform_driver *drv = to_platform_driver(_dev->driver);
  361. struct platform_device *dev = to_platform_device(_dev);
  362. drv->shutdown(dev);
  363. }
  364. static int platform_drv_suspend(struct device *_dev, pm_message_t state)
  365. {
  366. struct platform_driver *drv = to_platform_driver(_dev->driver);
  367. struct platform_device *dev = to_platform_device(_dev);
  368. return drv->suspend(dev, state);
  369. }
  370. static int platform_drv_resume(struct device *_dev)
  371. {
  372. struct platform_driver *drv = to_platform_driver(_dev->driver);
  373. struct platform_device *dev = to_platform_device(_dev);
  374. return drv->resume(dev);
  375. }
  376. /**
  377. * platform_driver_register
  378. * @drv: platform driver structure
  379. */
  380. int platform_driver_register(struct platform_driver *drv)
  381. {
  382. drv->driver.bus = &platform_bus_type;
  383. if (drv->probe)
  384. drv->driver.probe = platform_drv_probe;
  385. if (drv->remove)
  386. drv->driver.remove = platform_drv_remove;
  387. if (drv->shutdown)
  388. drv->driver.shutdown = platform_drv_shutdown;
  389. if (drv->suspend)
  390. drv->driver.suspend = platform_drv_suspend;
  391. if (drv->resume)
  392. drv->driver.resume = platform_drv_resume;
  393. if (drv->pm)
  394. drv->driver.pm = &drv->pm->base;
  395. return driver_register(&drv->driver);
  396. }
  397. EXPORT_SYMBOL_GPL(platform_driver_register);
  398. /**
  399. * platform_driver_unregister
  400. * @drv: platform driver structure
  401. */
  402. void platform_driver_unregister(struct platform_driver *drv)
  403. {
  404. driver_unregister(&drv->driver);
  405. }
  406. EXPORT_SYMBOL_GPL(platform_driver_unregister);
  407. /**
  408. * platform_driver_probe - register driver for non-hotpluggable device
  409. * @drv: platform driver structure
  410. * @probe: the driver probe routine, probably from an __init section
  411. *
  412. * Use this instead of platform_driver_register() when you know the device
  413. * is not hotpluggable and has already been registered, and you want to
  414. * remove its run-once probe() infrastructure from memory after the driver
  415. * has bound to the device.
  416. *
  417. * One typical use for this would be with drivers for controllers integrated
  418. * into system-on-chip processors, where the controller devices have been
  419. * configured as part of board setup.
  420. *
  421. * Returns zero if the driver registered and bound to a device, else returns
  422. * a negative error code and with the driver not registered.
  423. */
  424. int __init_or_module platform_driver_probe(struct platform_driver *drv,
  425. int (*probe)(struct platform_device *))
  426. {
  427. int retval, code;
  428. /* temporary section violation during probe() */
  429. drv->probe = probe;
  430. retval = code = platform_driver_register(drv);
  431. /* Fixup that section violation, being paranoid about code scanning
  432. * the list of drivers in order to probe new devices. Check to see
  433. * if the probe was successful, and make sure any forced probes of
  434. * new devices fail.
  435. */
  436. spin_lock(&platform_bus_type.p->klist_drivers.k_lock);
  437. drv->probe = NULL;
  438. if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
  439. retval = -ENODEV;
  440. drv->driver.probe = platform_drv_probe_fail;
  441. spin_unlock(&platform_bus_type.p->klist_drivers.k_lock);
  442. if (code != retval)
  443. platform_driver_unregister(drv);
  444. return retval;
  445. }
  446. EXPORT_SYMBOL_GPL(platform_driver_probe);
  447. /* modalias support enables more hands-off userspace setup:
  448. * (a) environment variable lets new-style hotplug events work once system is
  449. * fully running: "modprobe $MODALIAS"
  450. * (b) sysfs attribute lets new-style coldplug recover from hotplug events
  451. * mishandled before system is fully running: "modprobe $(cat modalias)"
  452. */
  453. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  454. char *buf)
  455. {
  456. struct platform_device *pdev = to_platform_device(dev);
  457. int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
  458. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  459. }
  460. static struct device_attribute platform_dev_attrs[] = {
  461. __ATTR_RO(modalias),
  462. __ATTR_NULL,
  463. };
  464. static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
  465. {
  466. struct platform_device *pdev = to_platform_device(dev);
  467. add_uevent_var(env, "MODALIAS=platform:%s", pdev->name);
  468. return 0;
  469. }
  470. /**
  471. * platform_match - bind platform device to platform driver.
  472. * @dev: device.
  473. * @drv: driver.
  474. *
  475. * Platform device IDs are assumed to be encoded like this:
  476. * "<name><instance>", where <name> is a short description of the type of
  477. * device, like "pci" or "floppy", and <instance> is the enumerated
  478. * instance of the device, like '0' or '42'. Driver IDs are simply
  479. * "<name>". So, extract the <name> from the platform_device structure,
  480. * and compare it against the name of the driver. Return whether they match
  481. * or not.
  482. */
  483. static int platform_match(struct device *dev, struct device_driver *drv)
  484. {
  485. struct platform_device *pdev;
  486. pdev = container_of(dev, struct platform_device, dev);
  487. return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
  488. }
  489. #ifdef CONFIG_PM_SLEEP
  490. static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
  491. {
  492. int ret = 0;
  493. if (dev->driver && dev->driver->suspend)
  494. ret = dev->driver->suspend(dev, mesg);
  495. return ret;
  496. }
  497. static int platform_legacy_suspend_late(struct device *dev, pm_message_t mesg)
  498. {
  499. struct platform_driver *drv = to_platform_driver(dev->driver);
  500. struct platform_device *pdev;
  501. int ret = 0;
  502. pdev = container_of(dev, struct platform_device, dev);
  503. if (dev->driver && drv->suspend_late)
  504. ret = drv->suspend_late(pdev, mesg);
  505. return ret;
  506. }
  507. static int platform_legacy_resume_early(struct device *dev)
  508. {
  509. struct platform_driver *drv = to_platform_driver(dev->driver);
  510. struct platform_device *pdev;
  511. int ret = 0;
  512. pdev = container_of(dev, struct platform_device, dev);
  513. if (dev->driver && drv->resume_early)
  514. ret = drv->resume_early(pdev);
  515. return ret;
  516. }
  517. static int platform_legacy_resume(struct device *dev)
  518. {
  519. int ret = 0;
  520. if (dev->driver && dev->driver->resume)
  521. ret = dev->driver->resume(dev);
  522. return ret;
  523. }
  524. static int platform_pm_prepare(struct device *dev)
  525. {
  526. struct device_driver *drv = dev->driver;
  527. int ret = 0;
  528. if (drv && drv->pm && drv->pm->prepare)
  529. ret = drv->pm->prepare(dev);
  530. return ret;
  531. }
  532. static void platform_pm_complete(struct device *dev)
  533. {
  534. struct device_driver *drv = dev->driver;
  535. if (drv && drv->pm && drv->pm->complete)
  536. drv->pm->complete(dev);
  537. }
  538. #ifdef CONFIG_SUSPEND
  539. static int platform_pm_suspend(struct device *dev)
  540. {
  541. struct device_driver *drv = dev->driver;
  542. int ret = 0;
  543. if (drv && drv->pm) {
  544. if (drv->pm->suspend)
  545. ret = drv->pm->suspend(dev);
  546. } else {
  547. ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
  548. }
  549. return ret;
  550. }
  551. static int platform_pm_suspend_noirq(struct device *dev)
  552. {
  553. struct platform_driver *pdrv;
  554. int ret = 0;
  555. if (!dev->driver)
  556. return 0;
  557. pdrv = to_platform_driver(dev->driver);
  558. if (pdrv->pm) {
  559. if (pdrv->pm->suspend_noirq)
  560. ret = pdrv->pm->suspend_noirq(dev);
  561. } else {
  562. ret = platform_legacy_suspend_late(dev, PMSG_SUSPEND);
  563. }
  564. return ret;
  565. }
  566. static int platform_pm_resume(struct device *dev)
  567. {
  568. struct device_driver *drv = dev->driver;
  569. int ret = 0;
  570. if (drv && drv->pm) {
  571. if (drv->pm->resume)
  572. ret = drv->pm->resume(dev);
  573. } else {
  574. ret = platform_legacy_resume(dev);
  575. }
  576. return ret;
  577. }
  578. static int platform_pm_resume_noirq(struct device *dev)
  579. {
  580. struct platform_driver *pdrv;
  581. int ret = 0;
  582. if (!dev->driver)
  583. return 0;
  584. pdrv = to_platform_driver(dev->driver);
  585. if (pdrv->pm) {
  586. if (pdrv->pm->resume_noirq)
  587. ret = pdrv->pm->resume_noirq(dev);
  588. } else {
  589. ret = platform_legacy_resume_early(dev);
  590. }
  591. return ret;
  592. }
  593. #else /* !CONFIG_SUSPEND */
  594. #define platform_pm_suspend NULL
  595. #define platform_pm_resume NULL
  596. #define platform_pm_suspend_noirq NULL
  597. #define platform_pm_resume_noirq NULL
  598. #endif /* !CONFIG_SUSPEND */
  599. #ifdef CONFIG_HIBERNATION
  600. static int platform_pm_freeze(struct device *dev)
  601. {
  602. struct device_driver *drv = dev->driver;
  603. int ret = 0;
  604. if (!drv)
  605. return 0;
  606. if (drv->pm) {
  607. if (drv->pm->freeze)
  608. ret = drv->pm->freeze(dev);
  609. } else {
  610. ret = platform_legacy_suspend(dev, PMSG_FREEZE);
  611. }
  612. return ret;
  613. }
  614. static int platform_pm_freeze_noirq(struct device *dev)
  615. {
  616. struct platform_driver *pdrv;
  617. int ret = 0;
  618. if (!dev->driver)
  619. return 0;
  620. pdrv = to_platform_driver(dev->driver);
  621. if (pdrv->pm) {
  622. if (pdrv->pm->freeze_noirq)
  623. ret = pdrv->pm->freeze_noirq(dev);
  624. } else {
  625. ret = platform_legacy_suspend_late(dev, PMSG_FREEZE);
  626. }
  627. return ret;
  628. }
  629. static int platform_pm_thaw(struct device *dev)
  630. {
  631. struct device_driver *drv = dev->driver;
  632. int ret = 0;
  633. if (drv && drv->pm) {
  634. if (drv->pm->thaw)
  635. ret = drv->pm->thaw(dev);
  636. } else {
  637. ret = platform_legacy_resume(dev);
  638. }
  639. return ret;
  640. }
  641. static int platform_pm_thaw_noirq(struct device *dev)
  642. {
  643. struct platform_driver *pdrv;
  644. int ret = 0;
  645. if (!dev->driver)
  646. return 0;
  647. pdrv = to_platform_driver(dev->driver);
  648. if (pdrv->pm) {
  649. if (pdrv->pm->thaw_noirq)
  650. ret = pdrv->pm->thaw_noirq(dev);
  651. } else {
  652. ret = platform_legacy_resume_early(dev);
  653. }
  654. return ret;
  655. }
  656. static int platform_pm_poweroff(struct device *dev)
  657. {
  658. struct device_driver *drv = dev->driver;
  659. int ret = 0;
  660. if (drv && drv->pm) {
  661. if (drv->pm->poweroff)
  662. ret = drv->pm->poweroff(dev);
  663. } else {
  664. ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
  665. }
  666. return ret;
  667. }
  668. static int platform_pm_poweroff_noirq(struct device *dev)
  669. {
  670. struct platform_driver *pdrv;
  671. int ret = 0;
  672. if (!dev->driver)
  673. return 0;
  674. pdrv = to_platform_driver(dev->driver);
  675. if (pdrv->pm) {
  676. if (pdrv->pm->poweroff_noirq)
  677. ret = pdrv->pm->poweroff_noirq(dev);
  678. } else {
  679. ret = platform_legacy_suspend_late(dev, PMSG_HIBERNATE);
  680. }
  681. return ret;
  682. }
  683. static int platform_pm_restore(struct device *dev)
  684. {
  685. struct device_driver *drv = dev->driver;
  686. int ret = 0;
  687. if (drv && drv->pm) {
  688. if (drv->pm->restore)
  689. ret = drv->pm->restore(dev);
  690. } else {
  691. ret = platform_legacy_resume(dev);
  692. }
  693. return ret;
  694. }
  695. static int platform_pm_restore_noirq(struct device *dev)
  696. {
  697. struct platform_driver *pdrv;
  698. int ret = 0;
  699. if (!dev->driver)
  700. return 0;
  701. pdrv = to_platform_driver(dev->driver);
  702. if (pdrv->pm) {
  703. if (pdrv->pm->restore_noirq)
  704. ret = pdrv->pm->restore_noirq(dev);
  705. } else {
  706. ret = platform_legacy_resume_early(dev);
  707. }
  708. return ret;
  709. }
  710. #else /* !CONFIG_HIBERNATION */
  711. #define platform_pm_freeze NULL
  712. #define platform_pm_thaw NULL
  713. #define platform_pm_poweroff NULL
  714. #define platform_pm_restore NULL
  715. #define platform_pm_freeze_noirq NULL
  716. #define platform_pm_thaw_noirq NULL
  717. #define platform_pm_poweroff_noirq NULL
  718. #define platform_pm_restore_noirq NULL
  719. #endif /* !CONFIG_HIBERNATION */
  720. struct pm_ext_ops platform_pm_ops = {
  721. .base = {
  722. .prepare = platform_pm_prepare,
  723. .complete = platform_pm_complete,
  724. .suspend = platform_pm_suspend,
  725. .resume = platform_pm_resume,
  726. .freeze = platform_pm_freeze,
  727. .thaw = platform_pm_thaw,
  728. .poweroff = platform_pm_poweroff,
  729. .restore = platform_pm_restore,
  730. },
  731. .suspend_noirq = platform_pm_suspend_noirq,
  732. .resume_noirq = platform_pm_resume_noirq,
  733. .freeze_noirq = platform_pm_freeze_noirq,
  734. .thaw_noirq = platform_pm_thaw_noirq,
  735. .poweroff_noirq = platform_pm_poweroff_noirq,
  736. .restore_noirq = platform_pm_restore_noirq,
  737. };
  738. #define PLATFORM_PM_OPS_PTR &platform_pm_ops
  739. #else /* !CONFIG_PM_SLEEP */
  740. #define PLATFORM_PM_OPS_PTR NULL
  741. #endif /* !CONFIG_PM_SLEEP */
  742. struct bus_type platform_bus_type = {
  743. .name = "platform",
  744. .dev_attrs = platform_dev_attrs,
  745. .match = platform_match,
  746. .uevent = platform_uevent,
  747. .pm = PLATFORM_PM_OPS_PTR,
  748. };
  749. EXPORT_SYMBOL_GPL(platform_bus_type);
  750. int __init platform_bus_init(void)
  751. {
  752. int error;
  753. error = device_register(&platform_bus);
  754. if (error)
  755. return error;
  756. error = bus_register(&platform_bus_type);
  757. if (error)
  758. device_unregister(&platform_bus);
  759. return error;
  760. }
  761. #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
  762. u64 dma_get_required_mask(struct device *dev)
  763. {
  764. u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
  765. u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
  766. u64 mask;
  767. if (!high_totalram) {
  768. /* convert to mask just covering totalram */
  769. low_totalram = (1 << (fls(low_totalram) - 1));
  770. low_totalram += low_totalram - 1;
  771. mask = low_totalram;
  772. } else {
  773. high_totalram = (1 << (fls(high_totalram) - 1));
  774. high_totalram += high_totalram - 1;
  775. mask = (((u64)high_totalram) << 32) + 0xffffffff;
  776. }
  777. return mask;
  778. }
  779. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  780. #endif