platform.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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,
  63. const char *name)
  64. {
  65. int i;
  66. for (i = 0; i < dev->num_resources; i++) {
  67. struct resource *r = &dev->resource[i];
  68. if (type == resource_type(r) && !strcmp(r->name, name))
  69. return r;
  70. }
  71. return NULL;
  72. }
  73. EXPORT_SYMBOL_GPL(platform_get_resource_byname);
  74. /**
  75. * platform_get_irq - get an IRQ for a device
  76. * @dev: platform device
  77. * @name: IRQ name
  78. */
  79. int platform_get_irq_byname(struct platform_device *dev, const char *name)
  80. {
  81. struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ,
  82. name);
  83. return r ? r->start : -ENXIO;
  84. }
  85. EXPORT_SYMBOL_GPL(platform_get_irq_byname);
  86. /**
  87. * platform_add_devices - add a numbers of platform devices
  88. * @devs: array of platform devices to add
  89. * @num: number of platform devices in array
  90. */
  91. int platform_add_devices(struct platform_device **devs, int num)
  92. {
  93. int i, ret = 0;
  94. for (i = 0; i < num; i++) {
  95. ret = platform_device_register(devs[i]);
  96. if (ret) {
  97. while (--i >= 0)
  98. platform_device_unregister(devs[i]);
  99. break;
  100. }
  101. }
  102. return ret;
  103. }
  104. EXPORT_SYMBOL_GPL(platform_add_devices);
  105. struct platform_object {
  106. struct platform_device pdev;
  107. char name[1];
  108. };
  109. /**
  110. * platform_device_put
  111. * @pdev: platform device to free
  112. *
  113. * Free all memory associated with a platform device. This function must
  114. * _only_ be externally called in error cases. All other usage is a bug.
  115. */
  116. void platform_device_put(struct platform_device *pdev)
  117. {
  118. if (pdev)
  119. put_device(&pdev->dev);
  120. }
  121. EXPORT_SYMBOL_GPL(platform_device_put);
  122. static void platform_device_release(struct device *dev)
  123. {
  124. struct platform_object *pa = container_of(dev, struct platform_object,
  125. pdev.dev);
  126. kfree(pa->pdev.dev.platform_data);
  127. kfree(pa->pdev.resource);
  128. kfree(pa);
  129. }
  130. /**
  131. * platform_device_alloc
  132. * @name: base name of the device we're adding
  133. * @id: instance id
  134. *
  135. * Create a platform device object which can have other objects attached
  136. * to it, and which will have attached objects freed when it is released.
  137. */
  138. struct platform_device *platform_device_alloc(const char *name, int id)
  139. {
  140. struct platform_object *pa;
  141. pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
  142. if (pa) {
  143. strcpy(pa->name, name);
  144. pa->pdev.name = pa->name;
  145. pa->pdev.id = id;
  146. device_initialize(&pa->pdev.dev);
  147. pa->pdev.dev.release = platform_device_release;
  148. }
  149. return pa ? &pa->pdev : NULL;
  150. }
  151. EXPORT_SYMBOL_GPL(platform_device_alloc);
  152. /**
  153. * platform_device_add_resources
  154. * @pdev: platform device allocated by platform_device_alloc to add resources to
  155. * @res: set of resources that needs to be allocated for the device
  156. * @num: number of resources
  157. *
  158. * Add a copy of the resources to the platform device. The memory
  159. * associated with the resources will be freed when the platform device is
  160. * released.
  161. */
  162. int platform_device_add_resources(struct platform_device *pdev,
  163. struct resource *res, unsigned int num)
  164. {
  165. struct resource *r;
  166. r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL);
  167. if (r) {
  168. memcpy(r, res, sizeof(struct resource) * num);
  169. pdev->resource = r;
  170. pdev->num_resources = num;
  171. }
  172. return r ? 0 : -ENOMEM;
  173. }
  174. EXPORT_SYMBOL_GPL(platform_device_add_resources);
  175. /**
  176. * platform_device_add_data
  177. * @pdev: platform device allocated by platform_device_alloc to add resources to
  178. * @data: platform specific data for this platform device
  179. * @size: size of platform specific data
  180. *
  181. * Add a copy of platform specific data to the platform device's
  182. * platform_data pointer. The memory associated with the platform data
  183. * will be freed when the platform device is released.
  184. */
  185. int platform_device_add_data(struct platform_device *pdev, const void *data,
  186. size_t size)
  187. {
  188. void *d;
  189. d = kmalloc(size, GFP_KERNEL);
  190. if (d) {
  191. memcpy(d, data, size);
  192. pdev->dev.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, "%s", pdev->name);
  216. for (i = 0; i < pdev->num_resources; i++) {
  217. struct resource *p, *r = &pdev->resource[i];
  218. if (r->name == NULL)
  219. r->name = dev_name(&pdev->dev);
  220. p = r->parent;
  221. if (!p) {
  222. if (resource_type(r) == IORESOURCE_MEM)
  223. p = &iomem_resource;
  224. else if (resource_type(r) == IORESOURCE_IO)
  225. p = &ioport_resource;
  226. }
  227. if (p && insert_resource(p, r)) {
  228. printk(KERN_ERR
  229. "%s: failed to claim resource %d\n",
  230. dev_name(&pdev->dev), i);
  231. ret = -EBUSY;
  232. goto failed;
  233. }
  234. }
  235. pr_debug("Registering platform device '%s'. Parent at %s\n",
  236. dev_name(&pdev->dev), dev_name(pdev->dev.parent));
  237. ret = device_add(&pdev->dev);
  238. if (ret == 0)
  239. return ret;
  240. failed:
  241. while (--i >= 0) {
  242. struct resource *r = &pdev->resource[i];
  243. unsigned long type = resource_type(r);
  244. if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
  245. release_resource(r);
  246. }
  247. return ret;
  248. }
  249. EXPORT_SYMBOL_GPL(platform_device_add);
  250. /**
  251. * platform_device_del - remove a platform-level device
  252. * @pdev: platform device we're removing
  253. *
  254. * Note that this function will also release all memory- and port-based
  255. * resources owned by the device (@dev->resource). This function must
  256. * _only_ be externally called in error cases. All other usage is a bug.
  257. */
  258. void platform_device_del(struct platform_device *pdev)
  259. {
  260. int i;
  261. if (pdev) {
  262. device_del(&pdev->dev);
  263. for (i = 0; i < pdev->num_resources; i++) {
  264. struct resource *r = &pdev->resource[i];
  265. unsigned long type = resource_type(r);
  266. if (type == IORESOURCE_MEM || type == 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. /**
  343. * platform_device_register_data
  344. * @parent: parent device for the device we're adding
  345. * @name: base name of the device we're adding
  346. * @id: instance id
  347. * @data: platform specific data for this platform device
  348. * @size: size of platform specific data
  349. *
  350. * This function creates a simple platform device that requires minimal
  351. * resource and memory management. Canned release function freeing memory
  352. * allocated for the device allows drivers using such devices to be
  353. * unloaded without waiting for the last reference to the device to be
  354. * dropped.
  355. */
  356. struct platform_device *platform_device_register_data(
  357. struct device *parent,
  358. const char *name, int id,
  359. const void *data, size_t size)
  360. {
  361. struct platform_device *pdev;
  362. int retval;
  363. pdev = platform_device_alloc(name, id);
  364. if (!pdev) {
  365. retval = -ENOMEM;
  366. goto error;
  367. }
  368. pdev->dev.parent = parent;
  369. if (size) {
  370. retval = platform_device_add_data(pdev, data, size);
  371. if (retval)
  372. goto error;
  373. }
  374. retval = platform_device_add(pdev);
  375. if (retval)
  376. goto error;
  377. return pdev;
  378. error:
  379. platform_device_put(pdev);
  380. return ERR_PTR(retval);
  381. }
  382. static int platform_drv_probe(struct device *_dev)
  383. {
  384. struct platform_driver *drv = to_platform_driver(_dev->driver);
  385. struct platform_device *dev = to_platform_device(_dev);
  386. return drv->probe(dev);
  387. }
  388. static int platform_drv_probe_fail(struct device *_dev)
  389. {
  390. return -ENXIO;
  391. }
  392. static int platform_drv_remove(struct device *_dev)
  393. {
  394. struct platform_driver *drv = to_platform_driver(_dev->driver);
  395. struct platform_device *dev = to_platform_device(_dev);
  396. return drv->remove(dev);
  397. }
  398. static void platform_drv_shutdown(struct device *_dev)
  399. {
  400. struct platform_driver *drv = to_platform_driver(_dev->driver);
  401. struct platform_device *dev = to_platform_device(_dev);
  402. drv->shutdown(dev);
  403. }
  404. /**
  405. * platform_driver_register
  406. * @drv: platform driver structure
  407. */
  408. int platform_driver_register(struct platform_driver *drv)
  409. {
  410. drv->driver.bus = &platform_bus_type;
  411. if (drv->probe)
  412. drv->driver.probe = platform_drv_probe;
  413. if (drv->remove)
  414. drv->driver.remove = platform_drv_remove;
  415. if (drv->shutdown)
  416. drv->driver.shutdown = platform_drv_shutdown;
  417. return driver_register(&drv->driver);
  418. }
  419. EXPORT_SYMBOL_GPL(platform_driver_register);
  420. /**
  421. * platform_driver_unregister
  422. * @drv: platform driver structure
  423. */
  424. void platform_driver_unregister(struct platform_driver *drv)
  425. {
  426. driver_unregister(&drv->driver);
  427. }
  428. EXPORT_SYMBOL_GPL(platform_driver_unregister);
  429. /**
  430. * platform_driver_probe - register driver for non-hotpluggable device
  431. * @drv: platform driver structure
  432. * @probe: the driver probe routine, probably from an __init section
  433. *
  434. * Use this instead of platform_driver_register() when you know the device
  435. * is not hotpluggable and has already been registered, and you want to
  436. * remove its run-once probe() infrastructure from memory after the driver
  437. * has bound to the device.
  438. *
  439. * One typical use for this would be with drivers for controllers integrated
  440. * into system-on-chip processors, where the controller devices have been
  441. * configured as part of board setup.
  442. *
  443. * Returns zero if the driver registered and bound to a device, else returns
  444. * a negative error code and with the driver not registered.
  445. */
  446. int __init_or_module platform_driver_probe(struct platform_driver *drv,
  447. int (*probe)(struct platform_device *))
  448. {
  449. int retval, code;
  450. /* temporary section violation during probe() */
  451. drv->probe = probe;
  452. retval = code = platform_driver_register(drv);
  453. /* Fixup that section violation, being paranoid about code scanning
  454. * the list of drivers in order to probe new devices. Check to see
  455. * if the probe was successful, and make sure any forced probes of
  456. * new devices fail.
  457. */
  458. spin_lock(&platform_bus_type.p->klist_drivers.k_lock);
  459. drv->probe = NULL;
  460. if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
  461. retval = -ENODEV;
  462. drv->driver.probe = platform_drv_probe_fail;
  463. spin_unlock(&platform_bus_type.p->klist_drivers.k_lock);
  464. if (code != retval)
  465. platform_driver_unregister(drv);
  466. return retval;
  467. }
  468. EXPORT_SYMBOL_GPL(platform_driver_probe);
  469. /* modalias support enables more hands-off userspace setup:
  470. * (a) environment variable lets new-style hotplug events work once system is
  471. * fully running: "modprobe $MODALIAS"
  472. * (b) sysfs attribute lets new-style coldplug recover from hotplug events
  473. * mishandled before system is fully running: "modprobe $(cat modalias)"
  474. */
  475. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  476. char *buf)
  477. {
  478. struct platform_device *pdev = to_platform_device(dev);
  479. int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
  480. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  481. }
  482. static struct device_attribute platform_dev_attrs[] = {
  483. __ATTR_RO(modalias),
  484. __ATTR_NULL,
  485. };
  486. static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
  487. {
  488. struct platform_device *pdev = to_platform_device(dev);
  489. add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
  490. (pdev->id_entry) ? pdev->id_entry->name : pdev->name);
  491. return 0;
  492. }
  493. static const struct platform_device_id *platform_match_id(
  494. struct platform_device_id *id,
  495. struct platform_device *pdev)
  496. {
  497. while (id->name[0]) {
  498. if (strcmp(pdev->name, id->name) == 0) {
  499. pdev->id_entry = id;
  500. return id;
  501. }
  502. id++;
  503. }
  504. return NULL;
  505. }
  506. /**
  507. * platform_match - bind platform device to platform driver.
  508. * @dev: device.
  509. * @drv: driver.
  510. *
  511. * Platform device IDs are assumed to be encoded like this:
  512. * "<name><instance>", where <name> is a short description of the type of
  513. * device, like "pci" or "floppy", and <instance> is the enumerated
  514. * instance of the device, like '0' or '42'. Driver IDs are simply
  515. * "<name>". So, extract the <name> from the platform_device structure,
  516. * and compare it against the name of the driver. Return whether they match
  517. * or not.
  518. */
  519. static int platform_match(struct device *dev, struct device_driver *drv)
  520. {
  521. struct platform_device *pdev = to_platform_device(dev);
  522. struct platform_driver *pdrv = to_platform_driver(drv);
  523. /* match against the id table first */
  524. if (pdrv->id_table)
  525. return platform_match_id(pdrv->id_table, pdev) != NULL;
  526. /* fall-back to driver name match */
  527. return (strcmp(pdev->name, drv->name) == 0);
  528. }
  529. #ifdef CONFIG_PM_SLEEP
  530. static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
  531. {
  532. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  533. struct platform_device *pdev = to_platform_device(dev);
  534. int ret = 0;
  535. if (dev->driver && pdrv->suspend)
  536. ret = pdrv->suspend(pdev, mesg);
  537. return ret;
  538. }
  539. static int platform_legacy_suspend_late(struct device *dev, pm_message_t mesg)
  540. {
  541. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  542. struct platform_device *pdev = to_platform_device(dev);
  543. int ret = 0;
  544. if (dev->driver && pdrv->suspend_late)
  545. ret = pdrv->suspend_late(pdev, mesg);
  546. return ret;
  547. }
  548. static int platform_legacy_resume_early(struct device *dev)
  549. {
  550. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  551. struct platform_device *pdev = to_platform_device(dev);
  552. int ret = 0;
  553. if (dev->driver && pdrv->resume_early)
  554. ret = pdrv->resume_early(pdev);
  555. return ret;
  556. }
  557. static int platform_legacy_resume(struct device *dev)
  558. {
  559. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  560. struct platform_device *pdev = to_platform_device(dev);
  561. int ret = 0;
  562. if (dev->driver && pdrv->resume)
  563. ret = pdrv->resume(pdev);
  564. return ret;
  565. }
  566. static int platform_pm_prepare(struct device *dev)
  567. {
  568. struct device_driver *drv = dev->driver;
  569. int ret = 0;
  570. if (drv && drv->pm && drv->pm->prepare)
  571. ret = drv->pm->prepare(dev);
  572. return ret;
  573. }
  574. static void platform_pm_complete(struct device *dev)
  575. {
  576. struct device_driver *drv = dev->driver;
  577. if (drv && drv->pm && drv->pm->complete)
  578. drv->pm->complete(dev);
  579. }
  580. #ifdef CONFIG_SUSPEND
  581. static int platform_pm_suspend(struct device *dev)
  582. {
  583. struct device_driver *drv = dev->driver;
  584. int ret = 0;
  585. if (!drv)
  586. return 0;
  587. if (drv->pm) {
  588. if (drv->pm->suspend)
  589. ret = drv->pm->suspend(dev);
  590. } else {
  591. ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
  592. }
  593. return ret;
  594. }
  595. static int platform_pm_suspend_noirq(struct device *dev)
  596. {
  597. struct device_driver *drv = dev->driver;
  598. int ret = 0;
  599. if (!drv)
  600. return 0;
  601. if (drv->pm) {
  602. if (drv->pm->suspend_noirq)
  603. ret = drv->pm->suspend_noirq(dev);
  604. } else {
  605. ret = platform_legacy_suspend_late(dev, PMSG_SUSPEND);
  606. }
  607. return ret;
  608. }
  609. static int platform_pm_resume(struct device *dev)
  610. {
  611. struct device_driver *drv = dev->driver;
  612. int ret = 0;
  613. if (!drv)
  614. return 0;
  615. if (drv->pm) {
  616. if (drv->pm->resume)
  617. ret = drv->pm->resume(dev);
  618. } else {
  619. ret = platform_legacy_resume(dev);
  620. }
  621. return ret;
  622. }
  623. static int platform_pm_resume_noirq(struct device *dev)
  624. {
  625. struct device_driver *drv = dev->driver;
  626. int ret = 0;
  627. if (!drv)
  628. return 0;
  629. if (drv->pm) {
  630. if (drv->pm->resume_noirq)
  631. ret = drv->pm->resume_noirq(dev);
  632. } else {
  633. ret = platform_legacy_resume_early(dev);
  634. }
  635. return ret;
  636. }
  637. #else /* !CONFIG_SUSPEND */
  638. #define platform_pm_suspend NULL
  639. #define platform_pm_resume NULL
  640. #define platform_pm_suspend_noirq NULL
  641. #define platform_pm_resume_noirq NULL
  642. #endif /* !CONFIG_SUSPEND */
  643. #ifdef CONFIG_HIBERNATION
  644. static int platform_pm_freeze(struct device *dev)
  645. {
  646. struct device_driver *drv = dev->driver;
  647. int ret = 0;
  648. if (!drv)
  649. return 0;
  650. if (drv->pm) {
  651. if (drv->pm->freeze)
  652. ret = drv->pm->freeze(dev);
  653. } else {
  654. ret = platform_legacy_suspend(dev, PMSG_FREEZE);
  655. }
  656. return ret;
  657. }
  658. static int platform_pm_freeze_noirq(struct device *dev)
  659. {
  660. struct device_driver *drv = dev->driver;
  661. int ret = 0;
  662. if (!drv)
  663. return 0;
  664. if (drv->pm) {
  665. if (drv->pm->freeze_noirq)
  666. ret = drv->pm->freeze_noirq(dev);
  667. } else {
  668. ret = platform_legacy_suspend_late(dev, PMSG_FREEZE);
  669. }
  670. return ret;
  671. }
  672. static int platform_pm_thaw(struct device *dev)
  673. {
  674. struct device_driver *drv = dev->driver;
  675. int ret = 0;
  676. if (!drv)
  677. return 0;
  678. if (drv->pm) {
  679. if (drv->pm->thaw)
  680. ret = drv->pm->thaw(dev);
  681. } else {
  682. ret = platform_legacy_resume(dev);
  683. }
  684. return ret;
  685. }
  686. static int platform_pm_thaw_noirq(struct device *dev)
  687. {
  688. struct device_driver *drv = dev->driver;
  689. int ret = 0;
  690. if (!drv)
  691. return 0;
  692. if (drv->pm) {
  693. if (drv->pm->thaw_noirq)
  694. ret = drv->pm->thaw_noirq(dev);
  695. } else {
  696. ret = platform_legacy_resume_early(dev);
  697. }
  698. return ret;
  699. }
  700. static int platform_pm_poweroff(struct device *dev)
  701. {
  702. struct device_driver *drv = dev->driver;
  703. int ret = 0;
  704. if (!drv)
  705. return 0;
  706. if (drv->pm) {
  707. if (drv->pm->poweroff)
  708. ret = drv->pm->poweroff(dev);
  709. } else {
  710. ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
  711. }
  712. return ret;
  713. }
  714. static int platform_pm_poweroff_noirq(struct device *dev)
  715. {
  716. struct device_driver *drv = dev->driver;
  717. int ret = 0;
  718. if (!drv)
  719. return 0;
  720. if (drv->pm) {
  721. if (drv->pm->poweroff_noirq)
  722. ret = drv->pm->poweroff_noirq(dev);
  723. } else {
  724. ret = platform_legacy_suspend_late(dev, PMSG_HIBERNATE);
  725. }
  726. return ret;
  727. }
  728. static int platform_pm_restore(struct device *dev)
  729. {
  730. struct device_driver *drv = dev->driver;
  731. int ret = 0;
  732. if (!drv)
  733. return 0;
  734. if (drv->pm) {
  735. if (drv->pm->restore)
  736. ret = drv->pm->restore(dev);
  737. } else {
  738. ret = platform_legacy_resume(dev);
  739. }
  740. return ret;
  741. }
  742. static int platform_pm_restore_noirq(struct device *dev)
  743. {
  744. struct device_driver *drv = dev->driver;
  745. int ret = 0;
  746. if (!drv)
  747. return 0;
  748. if (drv->pm) {
  749. if (drv->pm->restore_noirq)
  750. ret = drv->pm->restore_noirq(dev);
  751. } else {
  752. ret = platform_legacy_resume_early(dev);
  753. }
  754. return ret;
  755. }
  756. #else /* !CONFIG_HIBERNATION */
  757. #define platform_pm_freeze NULL
  758. #define platform_pm_thaw NULL
  759. #define platform_pm_poweroff NULL
  760. #define platform_pm_restore NULL
  761. #define platform_pm_freeze_noirq NULL
  762. #define platform_pm_thaw_noirq NULL
  763. #define platform_pm_poweroff_noirq NULL
  764. #define platform_pm_restore_noirq NULL
  765. #endif /* !CONFIG_HIBERNATION */
  766. static struct dev_pm_ops platform_dev_pm_ops = {
  767. .prepare = platform_pm_prepare,
  768. .complete = platform_pm_complete,
  769. .suspend = platform_pm_suspend,
  770. .resume = platform_pm_resume,
  771. .freeze = platform_pm_freeze,
  772. .thaw = platform_pm_thaw,
  773. .poweroff = platform_pm_poweroff,
  774. .restore = platform_pm_restore,
  775. .suspend_noirq = platform_pm_suspend_noirq,
  776. .resume_noirq = platform_pm_resume_noirq,
  777. .freeze_noirq = platform_pm_freeze_noirq,
  778. .thaw_noirq = platform_pm_thaw_noirq,
  779. .poweroff_noirq = platform_pm_poweroff_noirq,
  780. .restore_noirq = platform_pm_restore_noirq,
  781. };
  782. #define PLATFORM_PM_OPS_PTR (&platform_dev_pm_ops)
  783. #else /* !CONFIG_PM_SLEEP */
  784. #define PLATFORM_PM_OPS_PTR NULL
  785. #endif /* !CONFIG_PM_SLEEP */
  786. struct bus_type platform_bus_type = {
  787. .name = "platform",
  788. .dev_attrs = platform_dev_attrs,
  789. .match = platform_match,
  790. .uevent = platform_uevent,
  791. .pm = PLATFORM_PM_OPS_PTR,
  792. };
  793. EXPORT_SYMBOL_GPL(platform_bus_type);
  794. int __init platform_bus_init(void)
  795. {
  796. int error;
  797. early_platform_cleanup();
  798. error = device_register(&platform_bus);
  799. if (error)
  800. return error;
  801. error = bus_register(&platform_bus_type);
  802. if (error)
  803. device_unregister(&platform_bus);
  804. return error;
  805. }
  806. #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
  807. u64 dma_get_required_mask(struct device *dev)
  808. {
  809. u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
  810. u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
  811. u64 mask;
  812. if (!high_totalram) {
  813. /* convert to mask just covering totalram */
  814. low_totalram = (1 << (fls(low_totalram) - 1));
  815. low_totalram += low_totalram - 1;
  816. mask = low_totalram;
  817. } else {
  818. high_totalram = (1 << (fls(high_totalram) - 1));
  819. high_totalram += high_totalram - 1;
  820. mask = (((u64)high_totalram) << 32) + 0xffffffff;
  821. }
  822. return mask;
  823. }
  824. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  825. #endif
  826. static __initdata LIST_HEAD(early_platform_driver_list);
  827. static __initdata LIST_HEAD(early_platform_device_list);
  828. /**
  829. * early_platform_driver_register
  830. * @epdrv: early_platform driver structure
  831. * @buf: string passed from early_param()
  832. */
  833. int __init early_platform_driver_register(struct early_platform_driver *epdrv,
  834. char *buf)
  835. {
  836. unsigned long index;
  837. int n;
  838. /* Simply add the driver to the end of the global list.
  839. * Drivers will by default be put on the list in compiled-in order.
  840. */
  841. if (!epdrv->list.next) {
  842. INIT_LIST_HEAD(&epdrv->list);
  843. list_add_tail(&epdrv->list, &early_platform_driver_list);
  844. }
  845. /* If the user has specified device then make sure the driver
  846. * gets prioritized. The driver of the last device specified on
  847. * command line will be put first on the list.
  848. */
  849. n = strlen(epdrv->pdrv->driver.name);
  850. if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
  851. list_move(&epdrv->list, &early_platform_driver_list);
  852. if (!strcmp(buf, epdrv->pdrv->driver.name))
  853. epdrv->requested_id = -1;
  854. else if (buf[n] == '.' && strict_strtoul(&buf[n + 1], 10,
  855. &index) == 0)
  856. epdrv->requested_id = index;
  857. else
  858. epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
  859. }
  860. return 0;
  861. }
  862. /**
  863. * early_platform_add_devices - add a numbers of early platform devices
  864. * @devs: array of early platform devices to add
  865. * @num: number of early platform devices in array
  866. */
  867. void __init early_platform_add_devices(struct platform_device **devs, int num)
  868. {
  869. struct device *dev;
  870. int i;
  871. /* simply add the devices to list */
  872. for (i = 0; i < num; i++) {
  873. dev = &devs[i]->dev;
  874. if (!dev->devres_head.next) {
  875. INIT_LIST_HEAD(&dev->devres_head);
  876. list_add_tail(&dev->devres_head,
  877. &early_platform_device_list);
  878. }
  879. }
  880. }
  881. /**
  882. * early_platform_driver_register_all
  883. * @class_str: string to identify early platform driver class
  884. */
  885. void __init early_platform_driver_register_all(char *class_str)
  886. {
  887. /* The "class_str" parameter may or may not be present on the kernel
  888. * command line. If it is present then there may be more than one
  889. * matching parameter.
  890. *
  891. * Since we register our early platform drivers using early_param()
  892. * we need to make sure that they also get registered in the case
  893. * when the parameter is missing from the kernel command line.
  894. *
  895. * We use parse_early_options() to make sure the early_param() gets
  896. * called at least once. The early_param() may be called more than
  897. * once since the name of the preferred device may be specified on
  898. * the kernel command line. early_platform_driver_register() handles
  899. * this case for us.
  900. */
  901. parse_early_options(class_str);
  902. }
  903. /**
  904. * early_platform_match
  905. * @epdrv: early platform driver structure
  906. * @id: id to match against
  907. */
  908. static __init struct platform_device *
  909. early_platform_match(struct early_platform_driver *epdrv, int id)
  910. {
  911. struct platform_device *pd;
  912. list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
  913. if (platform_match(&pd->dev, &epdrv->pdrv->driver))
  914. if (pd->id == id)
  915. return pd;
  916. return NULL;
  917. }
  918. /**
  919. * early_platform_left
  920. * @epdrv: early platform driver structure
  921. * @id: return true if id or above exists
  922. */
  923. static __init int early_platform_left(struct early_platform_driver *epdrv,
  924. int id)
  925. {
  926. struct platform_device *pd;
  927. list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
  928. if (platform_match(&pd->dev, &epdrv->pdrv->driver))
  929. if (pd->id >= id)
  930. return 1;
  931. return 0;
  932. }
  933. /**
  934. * early_platform_driver_probe_id
  935. * @class_str: string to identify early platform driver class
  936. * @id: id to match against
  937. * @nr_probe: number of platform devices to successfully probe before exiting
  938. */
  939. static int __init early_platform_driver_probe_id(char *class_str,
  940. int id,
  941. int nr_probe)
  942. {
  943. struct early_platform_driver *epdrv;
  944. struct platform_device *match;
  945. int match_id;
  946. int n = 0;
  947. int left = 0;
  948. list_for_each_entry(epdrv, &early_platform_driver_list, list) {
  949. /* only use drivers matching our class_str */
  950. if (strcmp(class_str, epdrv->class_str))
  951. continue;
  952. if (id == -2) {
  953. match_id = epdrv->requested_id;
  954. left = 1;
  955. } else {
  956. match_id = id;
  957. left += early_platform_left(epdrv, id);
  958. /* skip requested id */
  959. switch (epdrv->requested_id) {
  960. case EARLY_PLATFORM_ID_ERROR:
  961. case EARLY_PLATFORM_ID_UNSET:
  962. break;
  963. default:
  964. if (epdrv->requested_id == id)
  965. match_id = EARLY_PLATFORM_ID_UNSET;
  966. }
  967. }
  968. switch (match_id) {
  969. case EARLY_PLATFORM_ID_ERROR:
  970. pr_warning("%s: unable to parse %s parameter\n",
  971. class_str, epdrv->pdrv->driver.name);
  972. /* fall-through */
  973. case EARLY_PLATFORM_ID_UNSET:
  974. match = NULL;
  975. break;
  976. default:
  977. match = early_platform_match(epdrv, match_id);
  978. }
  979. if (match) {
  980. if (epdrv->pdrv->probe(match))
  981. pr_warning("%s: unable to probe %s early.\n",
  982. class_str, match->name);
  983. else
  984. n++;
  985. }
  986. if (n >= nr_probe)
  987. break;
  988. }
  989. if (left)
  990. return n;
  991. else
  992. return -ENODEV;
  993. }
  994. /**
  995. * early_platform_driver_probe
  996. * @class_str: string to identify early platform driver class
  997. * @nr_probe: number of platform devices to successfully probe before exiting
  998. * @user_only: only probe user specified early platform devices
  999. */
  1000. int __init early_platform_driver_probe(char *class_str,
  1001. int nr_probe,
  1002. int user_only)
  1003. {
  1004. int k, n, i;
  1005. n = 0;
  1006. for (i = -2; n < nr_probe; i++) {
  1007. k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
  1008. if (k < 0)
  1009. break;
  1010. n += k;
  1011. if (user_only)
  1012. break;
  1013. }
  1014. return n;
  1015. }
  1016. /**
  1017. * early_platform_cleanup - clean up early platform code
  1018. */
  1019. void __init early_platform_cleanup(void)
  1020. {
  1021. struct platform_device *pd, *pd2;
  1022. /* clean up the devres list used to chain devices */
  1023. list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
  1024. dev.devres_head) {
  1025. list_del(&pd->dev.devres_head);
  1026. memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
  1027. }
  1028. }