platform.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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. /* We will remove platform_data field from struct device
  217. * if all platform devices pass its platform specific data
  218. * from platform_device. The conversion is going to be a
  219. * long time, so we allow the two cases coexist to make
  220. * this kind of fix more easily*/
  221. if (pdev->platform_data && pdev->dev.platform_data) {
  222. printk(KERN_ERR
  223. "%s: use which platform_data?\n",
  224. dev_name(&pdev->dev));
  225. } else if (pdev->platform_data) {
  226. pdev->dev.platform_data = pdev->platform_data;
  227. } else if (pdev->dev.platform_data) {
  228. pdev->platform_data = pdev->dev.platform_data;
  229. }
  230. for (i = 0; i < pdev->num_resources; i++) {
  231. struct resource *p, *r = &pdev->resource[i];
  232. if (r->name == NULL)
  233. r->name = dev_name(&pdev->dev);
  234. p = r->parent;
  235. if (!p) {
  236. if (resource_type(r) == IORESOURCE_MEM)
  237. p = &iomem_resource;
  238. else if (resource_type(r) == IORESOURCE_IO)
  239. p = &ioport_resource;
  240. }
  241. if (p && insert_resource(p, r)) {
  242. printk(KERN_ERR
  243. "%s: failed to claim resource %d\n",
  244. dev_name(&pdev->dev), i);
  245. ret = -EBUSY;
  246. goto failed;
  247. }
  248. }
  249. pr_debug("Registering platform device '%s'. Parent at %s\n",
  250. dev_name(&pdev->dev), dev_name(pdev->dev.parent));
  251. ret = device_add(&pdev->dev);
  252. if (ret == 0)
  253. return ret;
  254. failed:
  255. while (--i >= 0) {
  256. struct resource *r = &pdev->resource[i];
  257. unsigned long type = resource_type(r);
  258. if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
  259. release_resource(r);
  260. }
  261. return ret;
  262. }
  263. EXPORT_SYMBOL_GPL(platform_device_add);
  264. /**
  265. * platform_device_del - remove a platform-level device
  266. * @pdev: platform device we're removing
  267. *
  268. * Note that this function will also release all memory- and port-based
  269. * resources owned by the device (@dev->resource). This function must
  270. * _only_ be externally called in error cases. All other usage is a bug.
  271. */
  272. void platform_device_del(struct platform_device *pdev)
  273. {
  274. int i;
  275. if (pdev) {
  276. device_del(&pdev->dev);
  277. for (i = 0; i < pdev->num_resources; i++) {
  278. struct resource *r = &pdev->resource[i];
  279. unsigned long type = resource_type(r);
  280. if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
  281. release_resource(r);
  282. }
  283. }
  284. }
  285. EXPORT_SYMBOL_GPL(platform_device_del);
  286. /**
  287. * platform_device_register - add a platform-level device
  288. * @pdev: platform device we're adding
  289. */
  290. int platform_device_register(struct platform_device *pdev)
  291. {
  292. device_initialize(&pdev->dev);
  293. return platform_device_add(pdev);
  294. }
  295. EXPORT_SYMBOL_GPL(platform_device_register);
  296. /**
  297. * platform_device_unregister - unregister a platform-level device
  298. * @pdev: platform device we're unregistering
  299. *
  300. * Unregistration is done in 2 steps. First we release all resources
  301. * and remove it from the subsystem, then we drop reference count by
  302. * calling platform_device_put().
  303. */
  304. void platform_device_unregister(struct platform_device *pdev)
  305. {
  306. platform_device_del(pdev);
  307. platform_device_put(pdev);
  308. }
  309. EXPORT_SYMBOL_GPL(platform_device_unregister);
  310. /**
  311. * platform_device_register_simple
  312. * @name: base name of the device we're adding
  313. * @id: instance id
  314. * @res: set of resources that needs to be allocated for the device
  315. * @num: number of resources
  316. *
  317. * This function creates a simple platform device that requires minimal
  318. * resource and memory management. Canned release function freeing memory
  319. * allocated for the device allows drivers using such devices to be
  320. * unloaded without waiting for the last reference to the device to be
  321. * dropped.
  322. *
  323. * This interface is primarily intended for use with legacy drivers which
  324. * probe hardware directly. Because such drivers create sysfs device nodes
  325. * themselves, rather than letting system infrastructure handle such device
  326. * enumeration tasks, they don't fully conform to the Linux driver model.
  327. * In particular, when such drivers are built as modules, they can't be
  328. * "hotplugged".
  329. */
  330. struct platform_device *platform_device_register_simple(const char *name,
  331. int id,
  332. struct resource *res,
  333. unsigned int num)
  334. {
  335. struct platform_device *pdev;
  336. int retval;
  337. pdev = platform_device_alloc(name, id);
  338. if (!pdev) {
  339. retval = -ENOMEM;
  340. goto error;
  341. }
  342. if (num) {
  343. retval = platform_device_add_resources(pdev, res, num);
  344. if (retval)
  345. goto error;
  346. }
  347. retval = platform_device_add(pdev);
  348. if (retval)
  349. goto error;
  350. return pdev;
  351. error:
  352. platform_device_put(pdev);
  353. return ERR_PTR(retval);
  354. }
  355. EXPORT_SYMBOL_GPL(platform_device_register_simple);
  356. /**
  357. * platform_device_register_data
  358. * @parent: parent device for the device we're adding
  359. * @name: base name of the device we're adding
  360. * @id: instance id
  361. * @data: platform specific data for this platform device
  362. * @size: size of platform specific data
  363. *
  364. * This function creates a simple platform device that requires minimal
  365. * resource and memory management. Canned release function freeing memory
  366. * allocated for the device allows drivers using such devices to be
  367. * unloaded without waiting for the last reference to the device to be
  368. * dropped.
  369. */
  370. struct platform_device *platform_device_register_data(
  371. struct device *parent,
  372. const char *name, int id,
  373. const void *data, size_t size)
  374. {
  375. struct platform_device *pdev;
  376. int retval;
  377. pdev = platform_device_alloc(name, id);
  378. if (!pdev) {
  379. retval = -ENOMEM;
  380. goto error;
  381. }
  382. pdev->dev.parent = parent;
  383. if (size) {
  384. retval = platform_device_add_data(pdev, data, size);
  385. if (retval)
  386. goto error;
  387. }
  388. retval = platform_device_add(pdev);
  389. if (retval)
  390. goto error;
  391. return pdev;
  392. error:
  393. platform_device_put(pdev);
  394. return ERR_PTR(retval);
  395. }
  396. static int platform_drv_probe(struct device *_dev)
  397. {
  398. struct platform_driver *drv = to_platform_driver(_dev->driver);
  399. struct platform_device *dev = to_platform_device(_dev);
  400. return drv->probe(dev);
  401. }
  402. static int platform_drv_probe_fail(struct device *_dev)
  403. {
  404. return -ENXIO;
  405. }
  406. static int platform_drv_remove(struct device *_dev)
  407. {
  408. struct platform_driver *drv = to_platform_driver(_dev->driver);
  409. struct platform_device *dev = to_platform_device(_dev);
  410. return drv->remove(dev);
  411. }
  412. static void platform_drv_shutdown(struct device *_dev)
  413. {
  414. struct platform_driver *drv = to_platform_driver(_dev->driver);
  415. struct platform_device *dev = to_platform_device(_dev);
  416. drv->shutdown(dev);
  417. }
  418. static int platform_drv_suspend(struct device *_dev, pm_message_t state)
  419. {
  420. struct platform_driver *drv = to_platform_driver(_dev->driver);
  421. struct platform_device *dev = to_platform_device(_dev);
  422. return drv->suspend(dev, state);
  423. }
  424. static int platform_drv_resume(struct device *_dev)
  425. {
  426. struct platform_driver *drv = to_platform_driver(_dev->driver);
  427. struct platform_device *dev = to_platform_device(_dev);
  428. return drv->resume(dev);
  429. }
  430. /**
  431. * platform_driver_register
  432. * @drv: platform driver structure
  433. */
  434. int platform_driver_register(struct platform_driver *drv)
  435. {
  436. drv->driver.bus = &platform_bus_type;
  437. if (drv->probe)
  438. drv->driver.probe = platform_drv_probe;
  439. if (drv->remove)
  440. drv->driver.remove = platform_drv_remove;
  441. if (drv->shutdown)
  442. drv->driver.shutdown = platform_drv_shutdown;
  443. if (drv->suspend)
  444. drv->driver.suspend = platform_drv_suspend;
  445. if (drv->resume)
  446. drv->driver.resume = platform_drv_resume;
  447. return driver_register(&drv->driver);
  448. }
  449. EXPORT_SYMBOL_GPL(platform_driver_register);
  450. /**
  451. * platform_driver_unregister
  452. * @drv: platform driver structure
  453. */
  454. void platform_driver_unregister(struct platform_driver *drv)
  455. {
  456. driver_unregister(&drv->driver);
  457. }
  458. EXPORT_SYMBOL_GPL(platform_driver_unregister);
  459. /**
  460. * platform_driver_probe - register driver for non-hotpluggable device
  461. * @drv: platform driver structure
  462. * @probe: the driver probe routine, probably from an __init section
  463. *
  464. * Use this instead of platform_driver_register() when you know the device
  465. * is not hotpluggable and has already been registered, and you want to
  466. * remove its run-once probe() infrastructure from memory after the driver
  467. * has bound to the device.
  468. *
  469. * One typical use for this would be with drivers for controllers integrated
  470. * into system-on-chip processors, where the controller devices have been
  471. * configured as part of board setup.
  472. *
  473. * Returns zero if the driver registered and bound to a device, else returns
  474. * a negative error code and with the driver not registered.
  475. */
  476. int __init_or_module platform_driver_probe(struct platform_driver *drv,
  477. int (*probe)(struct platform_device *))
  478. {
  479. int retval, code;
  480. /* temporary section violation during probe() */
  481. drv->probe = probe;
  482. retval = code = platform_driver_register(drv);
  483. /* Fixup that section violation, being paranoid about code scanning
  484. * the list of drivers in order to probe new devices. Check to see
  485. * if the probe was successful, and make sure any forced probes of
  486. * new devices fail.
  487. */
  488. spin_lock(&platform_bus_type.p->klist_drivers.k_lock);
  489. drv->probe = NULL;
  490. if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
  491. retval = -ENODEV;
  492. drv->driver.probe = platform_drv_probe_fail;
  493. spin_unlock(&platform_bus_type.p->klist_drivers.k_lock);
  494. if (code != retval)
  495. platform_driver_unregister(drv);
  496. return retval;
  497. }
  498. EXPORT_SYMBOL_GPL(platform_driver_probe);
  499. /* modalias support enables more hands-off userspace setup:
  500. * (a) environment variable lets new-style hotplug events work once system is
  501. * fully running: "modprobe $MODALIAS"
  502. * (b) sysfs attribute lets new-style coldplug recover from hotplug events
  503. * mishandled before system is fully running: "modprobe $(cat modalias)"
  504. */
  505. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  506. char *buf)
  507. {
  508. struct platform_device *pdev = to_platform_device(dev);
  509. int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
  510. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  511. }
  512. static struct device_attribute platform_dev_attrs[] = {
  513. __ATTR_RO(modalias),
  514. __ATTR_NULL,
  515. };
  516. static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
  517. {
  518. struct platform_device *pdev = to_platform_device(dev);
  519. add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
  520. (pdev->id_entry) ? pdev->id_entry->name : pdev->name);
  521. return 0;
  522. }
  523. static const struct platform_device_id *platform_match_id(
  524. struct platform_device_id *id,
  525. struct platform_device *pdev)
  526. {
  527. while (id->name[0]) {
  528. if (strcmp(pdev->name, id->name) == 0) {
  529. pdev->id_entry = id;
  530. return id;
  531. }
  532. id++;
  533. }
  534. return NULL;
  535. }
  536. /**
  537. * platform_match - bind platform device to platform driver.
  538. * @dev: device.
  539. * @drv: driver.
  540. *
  541. * Platform device IDs are assumed to be encoded like this:
  542. * "<name><instance>", where <name> is a short description of the type of
  543. * device, like "pci" or "floppy", and <instance> is the enumerated
  544. * instance of the device, like '0' or '42'. Driver IDs are simply
  545. * "<name>". So, extract the <name> from the platform_device structure,
  546. * and compare it against the name of the driver. Return whether they match
  547. * or not.
  548. */
  549. static int platform_match(struct device *dev, struct device_driver *drv)
  550. {
  551. struct platform_device *pdev = to_platform_device(dev);
  552. struct platform_driver *pdrv = to_platform_driver(drv);
  553. /* match against the id table first */
  554. if (pdrv->id_table)
  555. return platform_match_id(pdrv->id_table, pdev) != NULL;
  556. /* fall-back to driver name match */
  557. return (strcmp(pdev->name, drv->name) == 0);
  558. }
  559. #ifdef CONFIG_PM_SLEEP
  560. static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
  561. {
  562. int ret = 0;
  563. if (dev->driver && dev->driver->suspend)
  564. ret = dev->driver->suspend(dev, mesg);
  565. return ret;
  566. }
  567. static int platform_legacy_suspend_late(struct device *dev, pm_message_t mesg)
  568. {
  569. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  570. struct platform_device *pdev = to_platform_device(dev);
  571. int ret = 0;
  572. if (dev->driver && pdrv->suspend_late)
  573. ret = pdrv->suspend_late(pdev, mesg);
  574. return ret;
  575. }
  576. static int platform_legacy_resume_early(struct device *dev)
  577. {
  578. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  579. struct platform_device *pdev = to_platform_device(dev);
  580. int ret = 0;
  581. if (dev->driver && pdrv->resume_early)
  582. ret = pdrv->resume_early(pdev);
  583. return ret;
  584. }
  585. static int platform_legacy_resume(struct device *dev)
  586. {
  587. int ret = 0;
  588. if (dev->driver && dev->driver->resume)
  589. ret = dev->driver->resume(dev);
  590. return ret;
  591. }
  592. static int platform_pm_prepare(struct device *dev)
  593. {
  594. struct device_driver *drv = dev->driver;
  595. int ret = 0;
  596. if (drv && drv->pm && drv->pm->prepare)
  597. ret = drv->pm->prepare(dev);
  598. return ret;
  599. }
  600. static void platform_pm_complete(struct device *dev)
  601. {
  602. struct device_driver *drv = dev->driver;
  603. if (drv && drv->pm && drv->pm->complete)
  604. drv->pm->complete(dev);
  605. }
  606. #ifdef CONFIG_SUSPEND
  607. static int platform_pm_suspend(struct device *dev)
  608. {
  609. struct device_driver *drv = dev->driver;
  610. int ret = 0;
  611. if (!drv)
  612. return 0;
  613. if (drv->pm) {
  614. if (drv->pm->suspend)
  615. ret = drv->pm->suspend(dev);
  616. } else {
  617. ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
  618. }
  619. return ret;
  620. }
  621. static int platform_pm_suspend_noirq(struct device *dev)
  622. {
  623. struct device_driver *drv = dev->driver;
  624. int ret = 0;
  625. if (!drv)
  626. return 0;
  627. if (drv->pm) {
  628. if (drv->pm->suspend_noirq)
  629. ret = drv->pm->suspend_noirq(dev);
  630. } else {
  631. ret = platform_legacy_suspend_late(dev, PMSG_SUSPEND);
  632. }
  633. return ret;
  634. }
  635. static int platform_pm_resume(struct device *dev)
  636. {
  637. struct device_driver *drv = dev->driver;
  638. int ret = 0;
  639. if (!drv)
  640. return 0;
  641. if (drv->pm) {
  642. if (drv->pm->resume)
  643. ret = drv->pm->resume(dev);
  644. } else {
  645. ret = platform_legacy_resume(dev);
  646. }
  647. return ret;
  648. }
  649. static int platform_pm_resume_noirq(struct device *dev)
  650. {
  651. struct device_driver *drv = dev->driver;
  652. int ret = 0;
  653. if (!drv)
  654. return 0;
  655. if (drv->pm) {
  656. if (drv->pm->resume_noirq)
  657. ret = drv->pm->resume_noirq(dev);
  658. } else {
  659. ret = platform_legacy_resume_early(dev);
  660. }
  661. return ret;
  662. }
  663. #else /* !CONFIG_SUSPEND */
  664. #define platform_pm_suspend NULL
  665. #define platform_pm_resume NULL
  666. #define platform_pm_suspend_noirq NULL
  667. #define platform_pm_resume_noirq NULL
  668. #endif /* !CONFIG_SUSPEND */
  669. #ifdef CONFIG_HIBERNATION
  670. static int platform_pm_freeze(struct device *dev)
  671. {
  672. struct device_driver *drv = dev->driver;
  673. int ret = 0;
  674. if (!drv)
  675. return 0;
  676. if (drv->pm) {
  677. if (drv->pm->freeze)
  678. ret = drv->pm->freeze(dev);
  679. } else {
  680. ret = platform_legacy_suspend(dev, PMSG_FREEZE);
  681. }
  682. return ret;
  683. }
  684. static int platform_pm_freeze_noirq(struct device *dev)
  685. {
  686. struct device_driver *drv = dev->driver;
  687. int ret = 0;
  688. if (!drv)
  689. return 0;
  690. if (drv->pm) {
  691. if (drv->pm->freeze_noirq)
  692. ret = drv->pm->freeze_noirq(dev);
  693. } else {
  694. ret = platform_legacy_suspend_late(dev, PMSG_FREEZE);
  695. }
  696. return ret;
  697. }
  698. static int platform_pm_thaw(struct device *dev)
  699. {
  700. struct device_driver *drv = dev->driver;
  701. int ret = 0;
  702. if (!drv)
  703. return 0;
  704. if (drv->pm) {
  705. if (drv->pm->thaw)
  706. ret = drv->pm->thaw(dev);
  707. } else {
  708. ret = platform_legacy_resume(dev);
  709. }
  710. return ret;
  711. }
  712. static int platform_pm_thaw_noirq(struct device *dev)
  713. {
  714. struct device_driver *drv = dev->driver;
  715. int ret = 0;
  716. if (!drv)
  717. return 0;
  718. if (drv->pm) {
  719. if (drv->pm->thaw_noirq)
  720. ret = drv->pm->thaw_noirq(dev);
  721. } else {
  722. ret = platform_legacy_resume_early(dev);
  723. }
  724. return ret;
  725. }
  726. static int platform_pm_poweroff(struct device *dev)
  727. {
  728. struct device_driver *drv = dev->driver;
  729. int ret = 0;
  730. if (!drv)
  731. return 0;
  732. if (drv->pm) {
  733. if (drv->pm->poweroff)
  734. ret = drv->pm->poweroff(dev);
  735. } else {
  736. ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
  737. }
  738. return ret;
  739. }
  740. static int platform_pm_poweroff_noirq(struct device *dev)
  741. {
  742. struct device_driver *drv = dev->driver;
  743. int ret = 0;
  744. if (!drv)
  745. return 0;
  746. if (drv->pm) {
  747. if (drv->pm->poweroff_noirq)
  748. ret = drv->pm->poweroff_noirq(dev);
  749. } else {
  750. ret = platform_legacy_suspend_late(dev, PMSG_HIBERNATE);
  751. }
  752. return ret;
  753. }
  754. static int platform_pm_restore(struct device *dev)
  755. {
  756. struct device_driver *drv = dev->driver;
  757. int ret = 0;
  758. if (!drv)
  759. return 0;
  760. if (drv->pm) {
  761. if (drv->pm->restore)
  762. ret = drv->pm->restore(dev);
  763. } else {
  764. ret = platform_legacy_resume(dev);
  765. }
  766. return ret;
  767. }
  768. static int platform_pm_restore_noirq(struct device *dev)
  769. {
  770. struct device_driver *drv = dev->driver;
  771. int ret = 0;
  772. if (!drv)
  773. return 0;
  774. if (drv->pm) {
  775. if (drv->pm->restore_noirq)
  776. ret = drv->pm->restore_noirq(dev);
  777. } else {
  778. ret = platform_legacy_resume_early(dev);
  779. }
  780. return ret;
  781. }
  782. #else /* !CONFIG_HIBERNATION */
  783. #define platform_pm_freeze NULL
  784. #define platform_pm_thaw NULL
  785. #define platform_pm_poweroff NULL
  786. #define platform_pm_restore NULL
  787. #define platform_pm_freeze_noirq NULL
  788. #define platform_pm_thaw_noirq NULL
  789. #define platform_pm_poweroff_noirq NULL
  790. #define platform_pm_restore_noirq NULL
  791. #endif /* !CONFIG_HIBERNATION */
  792. static struct dev_pm_ops platform_dev_pm_ops = {
  793. .prepare = platform_pm_prepare,
  794. .complete = platform_pm_complete,
  795. .suspend = platform_pm_suspend,
  796. .resume = platform_pm_resume,
  797. .freeze = platform_pm_freeze,
  798. .thaw = platform_pm_thaw,
  799. .poweroff = platform_pm_poweroff,
  800. .restore = platform_pm_restore,
  801. .suspend_noirq = platform_pm_suspend_noirq,
  802. .resume_noirq = platform_pm_resume_noirq,
  803. .freeze_noirq = platform_pm_freeze_noirq,
  804. .thaw_noirq = platform_pm_thaw_noirq,
  805. .poweroff_noirq = platform_pm_poweroff_noirq,
  806. .restore_noirq = platform_pm_restore_noirq,
  807. };
  808. #define PLATFORM_PM_OPS_PTR (&platform_dev_pm_ops)
  809. #else /* !CONFIG_PM_SLEEP */
  810. #define PLATFORM_PM_OPS_PTR NULL
  811. #endif /* !CONFIG_PM_SLEEP */
  812. struct bus_type platform_bus_type = {
  813. .name = "platform",
  814. .dev_attrs = platform_dev_attrs,
  815. .match = platform_match,
  816. .uevent = platform_uevent,
  817. .pm = PLATFORM_PM_OPS_PTR,
  818. };
  819. EXPORT_SYMBOL_GPL(platform_bus_type);
  820. int __init platform_bus_init(void)
  821. {
  822. int error;
  823. error = device_register(&platform_bus);
  824. if (error)
  825. return error;
  826. error = bus_register(&platform_bus_type);
  827. if (error)
  828. device_unregister(&platform_bus);
  829. return error;
  830. }
  831. #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
  832. u64 dma_get_required_mask(struct device *dev)
  833. {
  834. u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
  835. u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
  836. u64 mask;
  837. if (!high_totalram) {
  838. /* convert to mask just covering totalram */
  839. low_totalram = (1 << (fls(low_totalram) - 1));
  840. low_totalram += low_totalram - 1;
  841. mask = low_totalram;
  842. } else {
  843. high_totalram = (1 << (fls(high_totalram) - 1));
  844. high_totalram += high_totalram - 1;
  845. mask = (((u64)high_totalram) << 32) + 0xffffffff;
  846. }
  847. return mask;
  848. }
  849. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  850. #endif