platform.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  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. if (drv->suspend || drv->resume)
  418. pr_warning("Platform driver '%s' needs updating - please use "
  419. "dev_pm_ops\n", drv->driver.name);
  420. return driver_register(&drv->driver);
  421. }
  422. EXPORT_SYMBOL_GPL(platform_driver_register);
  423. /**
  424. * platform_driver_unregister
  425. * @drv: platform driver structure
  426. */
  427. void platform_driver_unregister(struct platform_driver *drv)
  428. {
  429. driver_unregister(&drv->driver);
  430. }
  431. EXPORT_SYMBOL_GPL(platform_driver_unregister);
  432. /**
  433. * platform_driver_probe - register driver for non-hotpluggable device
  434. * @drv: platform driver structure
  435. * @probe: the driver probe routine, probably from an __init section
  436. *
  437. * Use this instead of platform_driver_register() when you know the device
  438. * is not hotpluggable and has already been registered, and you want to
  439. * remove its run-once probe() infrastructure from memory after the driver
  440. * has bound to the device.
  441. *
  442. * One typical use for this would be with drivers for controllers integrated
  443. * into system-on-chip processors, where the controller devices have been
  444. * configured as part of board setup.
  445. *
  446. * Returns zero if the driver registered and bound to a device, else returns
  447. * a negative error code and with the driver not registered.
  448. */
  449. int __init_or_module platform_driver_probe(struct platform_driver *drv,
  450. int (*probe)(struct platform_device *))
  451. {
  452. int retval, code;
  453. /* temporary section violation during probe() */
  454. drv->probe = probe;
  455. retval = code = platform_driver_register(drv);
  456. /* Fixup that section violation, being paranoid about code scanning
  457. * the list of drivers in order to probe new devices. Check to see
  458. * if the probe was successful, and make sure any forced probes of
  459. * new devices fail.
  460. */
  461. spin_lock(&platform_bus_type.p->klist_drivers.k_lock);
  462. drv->probe = NULL;
  463. if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
  464. retval = -ENODEV;
  465. drv->driver.probe = platform_drv_probe_fail;
  466. spin_unlock(&platform_bus_type.p->klist_drivers.k_lock);
  467. if (code != retval)
  468. platform_driver_unregister(drv);
  469. return retval;
  470. }
  471. EXPORT_SYMBOL_GPL(platform_driver_probe);
  472. /* modalias support enables more hands-off userspace setup:
  473. * (a) environment variable lets new-style hotplug events work once system is
  474. * fully running: "modprobe $MODALIAS"
  475. * (b) sysfs attribute lets new-style coldplug recover from hotplug events
  476. * mishandled before system is fully running: "modprobe $(cat modalias)"
  477. */
  478. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  479. char *buf)
  480. {
  481. struct platform_device *pdev = to_platform_device(dev);
  482. int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
  483. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  484. }
  485. static struct device_attribute platform_dev_attrs[] = {
  486. __ATTR_RO(modalias),
  487. __ATTR_NULL,
  488. };
  489. static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
  490. {
  491. struct platform_device *pdev = to_platform_device(dev);
  492. add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
  493. (pdev->id_entry) ? pdev->id_entry->name : pdev->name);
  494. return 0;
  495. }
  496. static const struct platform_device_id *platform_match_id(
  497. struct platform_device_id *id,
  498. struct platform_device *pdev)
  499. {
  500. while (id->name[0]) {
  501. if (strcmp(pdev->name, id->name) == 0) {
  502. pdev->id_entry = id;
  503. return id;
  504. }
  505. id++;
  506. }
  507. return NULL;
  508. }
  509. /**
  510. * platform_match - bind platform device to platform driver.
  511. * @dev: device.
  512. * @drv: driver.
  513. *
  514. * Platform device IDs are assumed to be encoded like this:
  515. * "<name><instance>", where <name> is a short description of the type of
  516. * device, like "pci" or "floppy", and <instance> is the enumerated
  517. * instance of the device, like '0' or '42'. Driver IDs are simply
  518. * "<name>". So, extract the <name> from the platform_device structure,
  519. * and compare it against the name of the driver. Return whether they match
  520. * or not.
  521. */
  522. static int platform_match(struct device *dev, struct device_driver *drv)
  523. {
  524. struct platform_device *pdev = to_platform_device(dev);
  525. struct platform_driver *pdrv = to_platform_driver(drv);
  526. /* match against the id table first */
  527. if (pdrv->id_table)
  528. return platform_match_id(pdrv->id_table, pdev) != NULL;
  529. /* fall-back to driver name match */
  530. return (strcmp(pdev->name, drv->name) == 0);
  531. }
  532. #ifdef CONFIG_PM_SLEEP
  533. static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
  534. {
  535. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  536. struct platform_device *pdev = to_platform_device(dev);
  537. int ret = 0;
  538. if (dev->driver && pdrv->suspend)
  539. ret = pdrv->suspend(pdev, mesg);
  540. return ret;
  541. }
  542. static int platform_legacy_suspend_late(struct device *dev, pm_message_t mesg)
  543. {
  544. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  545. struct platform_device *pdev = to_platform_device(dev);
  546. int ret = 0;
  547. if (dev->driver && pdrv->suspend_late)
  548. ret = pdrv->suspend_late(pdev, mesg);
  549. return ret;
  550. }
  551. static int platform_legacy_resume_early(struct device *dev)
  552. {
  553. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  554. struct platform_device *pdev = to_platform_device(dev);
  555. int ret = 0;
  556. if (dev->driver && pdrv->resume_early)
  557. ret = pdrv->resume_early(pdev);
  558. return ret;
  559. }
  560. static int platform_legacy_resume(struct device *dev)
  561. {
  562. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  563. struct platform_device *pdev = to_platform_device(dev);
  564. int ret = 0;
  565. if (dev->driver && pdrv->resume)
  566. ret = pdrv->resume(pdev);
  567. return ret;
  568. }
  569. static int platform_pm_prepare(struct device *dev)
  570. {
  571. struct device_driver *drv = dev->driver;
  572. int ret = 0;
  573. if (drv && drv->pm && drv->pm->prepare)
  574. ret = drv->pm->prepare(dev);
  575. return ret;
  576. }
  577. static void platform_pm_complete(struct device *dev)
  578. {
  579. struct device_driver *drv = dev->driver;
  580. if (drv && drv->pm && drv->pm->complete)
  581. drv->pm->complete(dev);
  582. }
  583. #ifdef CONFIG_SUSPEND
  584. static int platform_pm_suspend(struct device *dev)
  585. {
  586. struct device_driver *drv = dev->driver;
  587. int ret = 0;
  588. if (!drv)
  589. return 0;
  590. if (drv->pm) {
  591. if (drv->pm->suspend)
  592. ret = drv->pm->suspend(dev);
  593. } else {
  594. ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
  595. }
  596. return ret;
  597. }
  598. static int platform_pm_suspend_noirq(struct device *dev)
  599. {
  600. struct device_driver *drv = dev->driver;
  601. int ret = 0;
  602. if (!drv)
  603. return 0;
  604. if (drv->pm) {
  605. if (drv->pm->suspend_noirq)
  606. ret = drv->pm->suspend_noirq(dev);
  607. } else {
  608. ret = platform_legacy_suspend_late(dev, PMSG_SUSPEND);
  609. }
  610. return ret;
  611. }
  612. static int platform_pm_resume(struct device *dev)
  613. {
  614. struct device_driver *drv = dev->driver;
  615. int ret = 0;
  616. if (!drv)
  617. return 0;
  618. if (drv->pm) {
  619. if (drv->pm->resume)
  620. ret = drv->pm->resume(dev);
  621. } else {
  622. ret = platform_legacy_resume(dev);
  623. }
  624. return ret;
  625. }
  626. static int platform_pm_resume_noirq(struct device *dev)
  627. {
  628. struct device_driver *drv = dev->driver;
  629. int ret = 0;
  630. if (!drv)
  631. return 0;
  632. if (drv->pm) {
  633. if (drv->pm->resume_noirq)
  634. ret = drv->pm->resume_noirq(dev);
  635. } else {
  636. ret = platform_legacy_resume_early(dev);
  637. }
  638. return ret;
  639. }
  640. #else /* !CONFIG_SUSPEND */
  641. #define platform_pm_suspend NULL
  642. #define platform_pm_resume NULL
  643. #define platform_pm_suspend_noirq NULL
  644. #define platform_pm_resume_noirq NULL
  645. #endif /* !CONFIG_SUSPEND */
  646. #ifdef CONFIG_HIBERNATION
  647. static int platform_pm_freeze(struct device *dev)
  648. {
  649. struct device_driver *drv = dev->driver;
  650. int ret = 0;
  651. if (!drv)
  652. return 0;
  653. if (drv->pm) {
  654. if (drv->pm->freeze)
  655. ret = drv->pm->freeze(dev);
  656. } else {
  657. ret = platform_legacy_suspend(dev, PMSG_FREEZE);
  658. }
  659. return ret;
  660. }
  661. static int platform_pm_freeze_noirq(struct device *dev)
  662. {
  663. struct device_driver *drv = dev->driver;
  664. int ret = 0;
  665. if (!drv)
  666. return 0;
  667. if (drv->pm) {
  668. if (drv->pm->freeze_noirq)
  669. ret = drv->pm->freeze_noirq(dev);
  670. } else {
  671. ret = platform_legacy_suspend_late(dev, PMSG_FREEZE);
  672. }
  673. return ret;
  674. }
  675. static int platform_pm_thaw(struct device *dev)
  676. {
  677. struct device_driver *drv = dev->driver;
  678. int ret = 0;
  679. if (!drv)
  680. return 0;
  681. if (drv->pm) {
  682. if (drv->pm->thaw)
  683. ret = drv->pm->thaw(dev);
  684. } else {
  685. ret = platform_legacy_resume(dev);
  686. }
  687. return ret;
  688. }
  689. static int platform_pm_thaw_noirq(struct device *dev)
  690. {
  691. struct device_driver *drv = dev->driver;
  692. int ret = 0;
  693. if (!drv)
  694. return 0;
  695. if (drv->pm) {
  696. if (drv->pm->thaw_noirq)
  697. ret = drv->pm->thaw_noirq(dev);
  698. } else {
  699. ret = platform_legacy_resume_early(dev);
  700. }
  701. return ret;
  702. }
  703. static int platform_pm_poweroff(struct device *dev)
  704. {
  705. struct device_driver *drv = dev->driver;
  706. int ret = 0;
  707. if (!drv)
  708. return 0;
  709. if (drv->pm) {
  710. if (drv->pm->poweroff)
  711. ret = drv->pm->poweroff(dev);
  712. } else {
  713. ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
  714. }
  715. return ret;
  716. }
  717. static int platform_pm_poweroff_noirq(struct device *dev)
  718. {
  719. struct device_driver *drv = dev->driver;
  720. int ret = 0;
  721. if (!drv)
  722. return 0;
  723. if (drv->pm) {
  724. if (drv->pm->poweroff_noirq)
  725. ret = drv->pm->poweroff_noirq(dev);
  726. } else {
  727. ret = platform_legacy_suspend_late(dev, PMSG_HIBERNATE);
  728. }
  729. return ret;
  730. }
  731. static int platform_pm_restore(struct device *dev)
  732. {
  733. struct device_driver *drv = dev->driver;
  734. int ret = 0;
  735. if (!drv)
  736. return 0;
  737. if (drv->pm) {
  738. if (drv->pm->restore)
  739. ret = drv->pm->restore(dev);
  740. } else {
  741. ret = platform_legacy_resume(dev);
  742. }
  743. return ret;
  744. }
  745. static int platform_pm_restore_noirq(struct device *dev)
  746. {
  747. struct device_driver *drv = dev->driver;
  748. int ret = 0;
  749. if (!drv)
  750. return 0;
  751. if (drv->pm) {
  752. if (drv->pm->restore_noirq)
  753. ret = drv->pm->restore_noirq(dev);
  754. } else {
  755. ret = platform_legacy_resume_early(dev);
  756. }
  757. return ret;
  758. }
  759. #else /* !CONFIG_HIBERNATION */
  760. #define platform_pm_freeze NULL
  761. #define platform_pm_thaw NULL
  762. #define platform_pm_poweroff NULL
  763. #define platform_pm_restore NULL
  764. #define platform_pm_freeze_noirq NULL
  765. #define platform_pm_thaw_noirq NULL
  766. #define platform_pm_poweroff_noirq NULL
  767. #define platform_pm_restore_noirq NULL
  768. #endif /* !CONFIG_HIBERNATION */
  769. static struct dev_pm_ops platform_dev_pm_ops = {
  770. .prepare = platform_pm_prepare,
  771. .complete = platform_pm_complete,
  772. .suspend = platform_pm_suspend,
  773. .resume = platform_pm_resume,
  774. .freeze = platform_pm_freeze,
  775. .thaw = platform_pm_thaw,
  776. .poweroff = platform_pm_poweroff,
  777. .restore = platform_pm_restore,
  778. .suspend_noirq = platform_pm_suspend_noirq,
  779. .resume_noirq = platform_pm_resume_noirq,
  780. .freeze_noirq = platform_pm_freeze_noirq,
  781. .thaw_noirq = platform_pm_thaw_noirq,
  782. .poweroff_noirq = platform_pm_poweroff_noirq,
  783. .restore_noirq = platform_pm_restore_noirq,
  784. };
  785. #define PLATFORM_PM_OPS_PTR (&platform_dev_pm_ops)
  786. #else /* !CONFIG_PM_SLEEP */
  787. #define PLATFORM_PM_OPS_PTR NULL
  788. #endif /* !CONFIG_PM_SLEEP */
  789. struct bus_type platform_bus_type = {
  790. .name = "platform",
  791. .dev_attrs = platform_dev_attrs,
  792. .match = platform_match,
  793. .uevent = platform_uevent,
  794. .pm = PLATFORM_PM_OPS_PTR,
  795. };
  796. EXPORT_SYMBOL_GPL(platform_bus_type);
  797. int __init platform_bus_init(void)
  798. {
  799. int error;
  800. early_platform_cleanup();
  801. error = device_register(&platform_bus);
  802. if (error)
  803. return error;
  804. error = bus_register(&platform_bus_type);
  805. if (error)
  806. device_unregister(&platform_bus);
  807. return error;
  808. }
  809. #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
  810. u64 dma_get_required_mask(struct device *dev)
  811. {
  812. u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
  813. u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
  814. u64 mask;
  815. if (!high_totalram) {
  816. /* convert to mask just covering totalram */
  817. low_totalram = (1 << (fls(low_totalram) - 1));
  818. low_totalram += low_totalram - 1;
  819. mask = low_totalram;
  820. } else {
  821. high_totalram = (1 << (fls(high_totalram) - 1));
  822. high_totalram += high_totalram - 1;
  823. mask = (((u64)high_totalram) << 32) + 0xffffffff;
  824. }
  825. return mask;
  826. }
  827. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  828. #endif
  829. static __initdata LIST_HEAD(early_platform_driver_list);
  830. static __initdata LIST_HEAD(early_platform_device_list);
  831. /**
  832. * early_platform_driver_register
  833. * @epdrv: early_platform driver structure
  834. * @buf: string passed from early_param()
  835. */
  836. int __init early_platform_driver_register(struct early_platform_driver *epdrv,
  837. char *buf)
  838. {
  839. unsigned long index;
  840. int n;
  841. /* Simply add the driver to the end of the global list.
  842. * Drivers will by default be put on the list in compiled-in order.
  843. */
  844. if (!epdrv->list.next) {
  845. INIT_LIST_HEAD(&epdrv->list);
  846. list_add_tail(&epdrv->list, &early_platform_driver_list);
  847. }
  848. /* If the user has specified device then make sure the driver
  849. * gets prioritized. The driver of the last device specified on
  850. * command line will be put first on the list.
  851. */
  852. n = strlen(epdrv->pdrv->driver.name);
  853. if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
  854. list_move(&epdrv->list, &early_platform_driver_list);
  855. if (!strcmp(buf, epdrv->pdrv->driver.name))
  856. epdrv->requested_id = -1;
  857. else if (buf[n] == '.' && strict_strtoul(&buf[n + 1], 10,
  858. &index) == 0)
  859. epdrv->requested_id = index;
  860. else
  861. epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
  862. }
  863. return 0;
  864. }
  865. /**
  866. * early_platform_add_devices - add a numbers of early platform devices
  867. * @devs: array of early platform devices to add
  868. * @num: number of early platform devices in array
  869. */
  870. void __init early_platform_add_devices(struct platform_device **devs, int num)
  871. {
  872. struct device *dev;
  873. int i;
  874. /* simply add the devices to list */
  875. for (i = 0; i < num; i++) {
  876. dev = &devs[i]->dev;
  877. if (!dev->devres_head.next) {
  878. INIT_LIST_HEAD(&dev->devres_head);
  879. list_add_tail(&dev->devres_head,
  880. &early_platform_device_list);
  881. }
  882. }
  883. }
  884. /**
  885. * early_platform_driver_register_all
  886. * @class_str: string to identify early platform driver class
  887. */
  888. void __init early_platform_driver_register_all(char *class_str)
  889. {
  890. /* The "class_str" parameter may or may not be present on the kernel
  891. * command line. If it is present then there may be more than one
  892. * matching parameter.
  893. *
  894. * Since we register our early platform drivers using early_param()
  895. * we need to make sure that they also get registered in the case
  896. * when the parameter is missing from the kernel command line.
  897. *
  898. * We use parse_early_options() to make sure the early_param() gets
  899. * called at least once. The early_param() may be called more than
  900. * once since the name of the preferred device may be specified on
  901. * the kernel command line. early_platform_driver_register() handles
  902. * this case for us.
  903. */
  904. parse_early_options(class_str);
  905. }
  906. /**
  907. * early_platform_match
  908. * @epdrv: early platform driver structure
  909. * @id: id to match against
  910. */
  911. static __init struct platform_device *
  912. early_platform_match(struct early_platform_driver *epdrv, int id)
  913. {
  914. struct platform_device *pd;
  915. list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
  916. if (platform_match(&pd->dev, &epdrv->pdrv->driver))
  917. if (pd->id == id)
  918. return pd;
  919. return NULL;
  920. }
  921. /**
  922. * early_platform_left
  923. * @epdrv: early platform driver structure
  924. * @id: return true if id or above exists
  925. */
  926. static __init int early_platform_left(struct early_platform_driver *epdrv,
  927. int id)
  928. {
  929. struct platform_device *pd;
  930. list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
  931. if (platform_match(&pd->dev, &epdrv->pdrv->driver))
  932. if (pd->id >= id)
  933. return 1;
  934. return 0;
  935. }
  936. /**
  937. * early_platform_driver_probe_id
  938. * @class_str: string to identify early platform driver class
  939. * @id: id to match against
  940. * @nr_probe: number of platform devices to successfully probe before exiting
  941. */
  942. static int __init early_platform_driver_probe_id(char *class_str,
  943. int id,
  944. int nr_probe)
  945. {
  946. struct early_platform_driver *epdrv;
  947. struct platform_device *match;
  948. int match_id;
  949. int n = 0;
  950. int left = 0;
  951. list_for_each_entry(epdrv, &early_platform_driver_list, list) {
  952. /* only use drivers matching our class_str */
  953. if (strcmp(class_str, epdrv->class_str))
  954. continue;
  955. if (id == -2) {
  956. match_id = epdrv->requested_id;
  957. left = 1;
  958. } else {
  959. match_id = id;
  960. left += early_platform_left(epdrv, id);
  961. /* skip requested id */
  962. switch (epdrv->requested_id) {
  963. case EARLY_PLATFORM_ID_ERROR:
  964. case EARLY_PLATFORM_ID_UNSET:
  965. break;
  966. default:
  967. if (epdrv->requested_id == id)
  968. match_id = EARLY_PLATFORM_ID_UNSET;
  969. }
  970. }
  971. switch (match_id) {
  972. case EARLY_PLATFORM_ID_ERROR:
  973. pr_warning("%s: unable to parse %s parameter\n",
  974. class_str, epdrv->pdrv->driver.name);
  975. /* fall-through */
  976. case EARLY_PLATFORM_ID_UNSET:
  977. match = NULL;
  978. break;
  979. default:
  980. match = early_platform_match(epdrv, match_id);
  981. }
  982. if (match) {
  983. if (epdrv->pdrv->probe(match))
  984. pr_warning("%s: unable to probe %s early.\n",
  985. class_str, match->name);
  986. else
  987. n++;
  988. }
  989. if (n >= nr_probe)
  990. break;
  991. }
  992. if (left)
  993. return n;
  994. else
  995. return -ENODEV;
  996. }
  997. /**
  998. * early_platform_driver_probe
  999. * @class_str: string to identify early platform driver class
  1000. * @nr_probe: number of platform devices to successfully probe before exiting
  1001. * @user_only: only probe user specified early platform devices
  1002. */
  1003. int __init early_platform_driver_probe(char *class_str,
  1004. int nr_probe,
  1005. int user_only)
  1006. {
  1007. int k, n, i;
  1008. n = 0;
  1009. for (i = -2; n < nr_probe; i++) {
  1010. k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
  1011. if (k < 0)
  1012. break;
  1013. n += k;
  1014. if (user_only)
  1015. break;
  1016. }
  1017. return n;
  1018. }
  1019. /**
  1020. * early_platform_cleanup - clean up early platform code
  1021. */
  1022. void __init early_platform_cleanup(void)
  1023. {
  1024. struct platform_device *pd, *pd2;
  1025. /* clean up the devres list used to chain devices */
  1026. list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
  1027. dev.devres_head) {
  1028. list_del(&pd->dev.devres_head);
  1029. memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
  1030. }
  1031. }