platform.c 24 KB

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