platform.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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_resume(struct device *dev)
  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->resume)
  545. ret = pdrv->resume(pdev);
  546. return ret;
  547. }
  548. static int platform_pm_prepare(struct device *dev)
  549. {
  550. struct device_driver *drv = dev->driver;
  551. int ret = 0;
  552. if (drv && drv->pm && drv->pm->prepare)
  553. ret = drv->pm->prepare(dev);
  554. return ret;
  555. }
  556. static void platform_pm_complete(struct device *dev)
  557. {
  558. struct device_driver *drv = dev->driver;
  559. if (drv && drv->pm && drv->pm->complete)
  560. drv->pm->complete(dev);
  561. }
  562. #ifdef CONFIG_SUSPEND
  563. static int platform_pm_suspend(struct device *dev)
  564. {
  565. struct device_driver *drv = dev->driver;
  566. int ret = 0;
  567. if (!drv)
  568. return 0;
  569. if (drv->pm) {
  570. if (drv->pm->suspend)
  571. ret = drv->pm->suspend(dev);
  572. } else {
  573. ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
  574. }
  575. return ret;
  576. }
  577. static int platform_pm_suspend_noirq(struct device *dev)
  578. {
  579. struct device_driver *drv = dev->driver;
  580. int ret = 0;
  581. if (!drv)
  582. return 0;
  583. if (drv->pm) {
  584. if (drv->pm->suspend_noirq)
  585. ret = drv->pm->suspend_noirq(dev);
  586. }
  587. return ret;
  588. }
  589. static int platform_pm_resume(struct device *dev)
  590. {
  591. struct device_driver *drv = dev->driver;
  592. int ret = 0;
  593. if (!drv)
  594. return 0;
  595. if (drv->pm) {
  596. if (drv->pm->resume)
  597. ret = drv->pm->resume(dev);
  598. } else {
  599. ret = platform_legacy_resume(dev);
  600. }
  601. return ret;
  602. }
  603. static int platform_pm_resume_noirq(struct device *dev)
  604. {
  605. struct device_driver *drv = dev->driver;
  606. int ret = 0;
  607. if (!drv)
  608. return 0;
  609. if (drv->pm) {
  610. if (drv->pm->resume_noirq)
  611. ret = drv->pm->resume_noirq(dev);
  612. }
  613. return ret;
  614. }
  615. #else /* !CONFIG_SUSPEND */
  616. #define platform_pm_suspend NULL
  617. #define platform_pm_resume NULL
  618. #define platform_pm_suspend_noirq NULL
  619. #define platform_pm_resume_noirq NULL
  620. #endif /* !CONFIG_SUSPEND */
  621. #ifdef CONFIG_HIBERNATION
  622. static int platform_pm_freeze(struct device *dev)
  623. {
  624. struct device_driver *drv = dev->driver;
  625. int ret = 0;
  626. if (!drv)
  627. return 0;
  628. if (drv->pm) {
  629. if (drv->pm->freeze)
  630. ret = drv->pm->freeze(dev);
  631. } else {
  632. ret = platform_legacy_suspend(dev, PMSG_FREEZE);
  633. }
  634. return ret;
  635. }
  636. static int platform_pm_freeze_noirq(struct device *dev)
  637. {
  638. struct device_driver *drv = dev->driver;
  639. int ret = 0;
  640. if (!drv)
  641. return 0;
  642. if (drv->pm) {
  643. if (drv->pm->freeze_noirq)
  644. ret = drv->pm->freeze_noirq(dev);
  645. }
  646. return ret;
  647. }
  648. static int platform_pm_thaw(struct device *dev)
  649. {
  650. struct device_driver *drv = dev->driver;
  651. int ret = 0;
  652. if (!drv)
  653. return 0;
  654. if (drv->pm) {
  655. if (drv->pm->thaw)
  656. ret = drv->pm->thaw(dev);
  657. } else {
  658. ret = platform_legacy_resume(dev);
  659. }
  660. return ret;
  661. }
  662. static int platform_pm_thaw_noirq(struct device *dev)
  663. {
  664. struct device_driver *drv = dev->driver;
  665. int ret = 0;
  666. if (!drv)
  667. return 0;
  668. if (drv->pm) {
  669. if (drv->pm->thaw_noirq)
  670. ret = drv->pm->thaw_noirq(dev);
  671. }
  672. return ret;
  673. }
  674. static int platform_pm_poweroff(struct device *dev)
  675. {
  676. struct device_driver *drv = dev->driver;
  677. int ret = 0;
  678. if (!drv)
  679. return 0;
  680. if (drv->pm) {
  681. if (drv->pm->poweroff)
  682. ret = drv->pm->poweroff(dev);
  683. } else {
  684. ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
  685. }
  686. return ret;
  687. }
  688. static int platform_pm_poweroff_noirq(struct device *dev)
  689. {
  690. struct device_driver *drv = dev->driver;
  691. int ret = 0;
  692. if (!drv)
  693. return 0;
  694. if (drv->pm) {
  695. if (drv->pm->poweroff_noirq)
  696. ret = drv->pm->poweroff_noirq(dev);
  697. }
  698. return ret;
  699. }
  700. static int platform_pm_restore(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->restore)
  708. ret = drv->pm->restore(dev);
  709. } else {
  710. ret = platform_legacy_resume(dev);
  711. }
  712. return ret;
  713. }
  714. static int platform_pm_restore_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->restore_noirq)
  722. ret = drv->pm->restore_noirq(dev);
  723. }
  724. return ret;
  725. }
  726. #else /* !CONFIG_HIBERNATION */
  727. #define platform_pm_freeze NULL
  728. #define platform_pm_thaw NULL
  729. #define platform_pm_poweroff NULL
  730. #define platform_pm_restore NULL
  731. #define platform_pm_freeze_noirq NULL
  732. #define platform_pm_thaw_noirq NULL
  733. #define platform_pm_poweroff_noirq NULL
  734. #define platform_pm_restore_noirq NULL
  735. #endif /* !CONFIG_HIBERNATION */
  736. static const struct dev_pm_ops platform_dev_pm_ops = {
  737. .prepare = platform_pm_prepare,
  738. .complete = platform_pm_complete,
  739. .suspend = platform_pm_suspend,
  740. .resume = platform_pm_resume,
  741. .freeze = platform_pm_freeze,
  742. .thaw = platform_pm_thaw,
  743. .poweroff = platform_pm_poweroff,
  744. .restore = platform_pm_restore,
  745. .suspend_noirq = platform_pm_suspend_noirq,
  746. .resume_noirq = platform_pm_resume_noirq,
  747. .freeze_noirq = platform_pm_freeze_noirq,
  748. .thaw_noirq = platform_pm_thaw_noirq,
  749. .poweroff_noirq = platform_pm_poweroff_noirq,
  750. .restore_noirq = platform_pm_restore_noirq,
  751. };
  752. #define PLATFORM_PM_OPS_PTR (&platform_dev_pm_ops)
  753. #else /* !CONFIG_PM_SLEEP */
  754. #define PLATFORM_PM_OPS_PTR NULL
  755. #endif /* !CONFIG_PM_SLEEP */
  756. struct bus_type platform_bus_type = {
  757. .name = "platform",
  758. .dev_attrs = platform_dev_attrs,
  759. .match = platform_match,
  760. .uevent = platform_uevent,
  761. .pm = PLATFORM_PM_OPS_PTR,
  762. };
  763. EXPORT_SYMBOL_GPL(platform_bus_type);
  764. int __init platform_bus_init(void)
  765. {
  766. int error;
  767. early_platform_cleanup();
  768. error = device_register(&platform_bus);
  769. if (error)
  770. return error;
  771. error = bus_register(&platform_bus_type);
  772. if (error)
  773. device_unregister(&platform_bus);
  774. return error;
  775. }
  776. #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
  777. u64 dma_get_required_mask(struct device *dev)
  778. {
  779. u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
  780. u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
  781. u64 mask;
  782. if (!high_totalram) {
  783. /* convert to mask just covering totalram */
  784. low_totalram = (1 << (fls(low_totalram) - 1));
  785. low_totalram += low_totalram - 1;
  786. mask = low_totalram;
  787. } else {
  788. high_totalram = (1 << (fls(high_totalram) - 1));
  789. high_totalram += high_totalram - 1;
  790. mask = (((u64)high_totalram) << 32) + 0xffffffff;
  791. }
  792. return mask;
  793. }
  794. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  795. #endif
  796. static __initdata LIST_HEAD(early_platform_driver_list);
  797. static __initdata LIST_HEAD(early_platform_device_list);
  798. /**
  799. * early_platform_driver_register
  800. * @epdrv: early_platform driver structure
  801. * @buf: string passed from early_param()
  802. */
  803. int __init early_platform_driver_register(struct early_platform_driver *epdrv,
  804. char *buf)
  805. {
  806. unsigned long index;
  807. int n;
  808. /* Simply add the driver to the end of the global list.
  809. * Drivers will by default be put on the list in compiled-in order.
  810. */
  811. if (!epdrv->list.next) {
  812. INIT_LIST_HEAD(&epdrv->list);
  813. list_add_tail(&epdrv->list, &early_platform_driver_list);
  814. }
  815. /* If the user has specified device then make sure the driver
  816. * gets prioritized. The driver of the last device specified on
  817. * command line will be put first on the list.
  818. */
  819. n = strlen(epdrv->pdrv->driver.name);
  820. if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
  821. list_move(&epdrv->list, &early_platform_driver_list);
  822. if (!strcmp(buf, epdrv->pdrv->driver.name))
  823. epdrv->requested_id = -1;
  824. else if (buf[n] == '.' && strict_strtoul(&buf[n + 1], 10,
  825. &index) == 0)
  826. epdrv->requested_id = index;
  827. else
  828. epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
  829. }
  830. return 0;
  831. }
  832. /**
  833. * early_platform_add_devices - add a numbers of early platform devices
  834. * @devs: array of early platform devices to add
  835. * @num: number of early platform devices in array
  836. */
  837. void __init early_platform_add_devices(struct platform_device **devs, int num)
  838. {
  839. struct device *dev;
  840. int i;
  841. /* simply add the devices to list */
  842. for (i = 0; i < num; i++) {
  843. dev = &devs[i]->dev;
  844. if (!dev->devres_head.next) {
  845. INIT_LIST_HEAD(&dev->devres_head);
  846. list_add_tail(&dev->devres_head,
  847. &early_platform_device_list);
  848. }
  849. }
  850. }
  851. /**
  852. * early_platform_driver_register_all
  853. * @class_str: string to identify early platform driver class
  854. */
  855. void __init early_platform_driver_register_all(char *class_str)
  856. {
  857. /* The "class_str" parameter may or may not be present on the kernel
  858. * command line. If it is present then there may be more than one
  859. * matching parameter.
  860. *
  861. * Since we register our early platform drivers using early_param()
  862. * we need to make sure that they also get registered in the case
  863. * when the parameter is missing from the kernel command line.
  864. *
  865. * We use parse_early_options() to make sure the early_param() gets
  866. * called at least once. The early_param() may be called more than
  867. * once since the name of the preferred device may be specified on
  868. * the kernel command line. early_platform_driver_register() handles
  869. * this case for us.
  870. */
  871. parse_early_options(class_str);
  872. }
  873. /**
  874. * early_platform_match
  875. * @epdrv: early platform driver structure
  876. * @id: id to match against
  877. */
  878. static __init struct platform_device *
  879. early_platform_match(struct early_platform_driver *epdrv, int id)
  880. {
  881. struct platform_device *pd;
  882. list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
  883. if (platform_match(&pd->dev, &epdrv->pdrv->driver))
  884. if (pd->id == id)
  885. return pd;
  886. return NULL;
  887. }
  888. /**
  889. * early_platform_left
  890. * @epdrv: early platform driver structure
  891. * @id: return true if id or above exists
  892. */
  893. static __init int early_platform_left(struct early_platform_driver *epdrv,
  894. int id)
  895. {
  896. struct platform_device *pd;
  897. list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
  898. if (platform_match(&pd->dev, &epdrv->pdrv->driver))
  899. if (pd->id >= id)
  900. return 1;
  901. return 0;
  902. }
  903. /**
  904. * early_platform_driver_probe_id
  905. * @class_str: string to identify early platform driver class
  906. * @id: id to match against
  907. * @nr_probe: number of platform devices to successfully probe before exiting
  908. */
  909. static int __init early_platform_driver_probe_id(char *class_str,
  910. int id,
  911. int nr_probe)
  912. {
  913. struct early_platform_driver *epdrv;
  914. struct platform_device *match;
  915. int match_id;
  916. int n = 0;
  917. int left = 0;
  918. list_for_each_entry(epdrv, &early_platform_driver_list, list) {
  919. /* only use drivers matching our class_str */
  920. if (strcmp(class_str, epdrv->class_str))
  921. continue;
  922. if (id == -2) {
  923. match_id = epdrv->requested_id;
  924. left = 1;
  925. } else {
  926. match_id = id;
  927. left += early_platform_left(epdrv, id);
  928. /* skip requested id */
  929. switch (epdrv->requested_id) {
  930. case EARLY_PLATFORM_ID_ERROR:
  931. case EARLY_PLATFORM_ID_UNSET:
  932. break;
  933. default:
  934. if (epdrv->requested_id == id)
  935. match_id = EARLY_PLATFORM_ID_UNSET;
  936. }
  937. }
  938. switch (match_id) {
  939. case EARLY_PLATFORM_ID_ERROR:
  940. pr_warning("%s: unable to parse %s parameter\n",
  941. class_str, epdrv->pdrv->driver.name);
  942. /* fall-through */
  943. case EARLY_PLATFORM_ID_UNSET:
  944. match = NULL;
  945. break;
  946. default:
  947. match = early_platform_match(epdrv, match_id);
  948. }
  949. if (match) {
  950. if (epdrv->pdrv->probe(match))
  951. pr_warning("%s: unable to probe %s early.\n",
  952. class_str, match->name);
  953. else
  954. n++;
  955. }
  956. if (n >= nr_probe)
  957. break;
  958. }
  959. if (left)
  960. return n;
  961. else
  962. return -ENODEV;
  963. }
  964. /**
  965. * early_platform_driver_probe
  966. * @class_str: string to identify early platform driver class
  967. * @nr_probe: number of platform devices to successfully probe before exiting
  968. * @user_only: only probe user specified early platform devices
  969. */
  970. int __init early_platform_driver_probe(char *class_str,
  971. int nr_probe,
  972. int user_only)
  973. {
  974. int k, n, i;
  975. n = 0;
  976. for (i = -2; n < nr_probe; i++) {
  977. k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
  978. if (k < 0)
  979. break;
  980. n += k;
  981. if (user_only)
  982. break;
  983. }
  984. return n;
  985. }
  986. /**
  987. * early_platform_cleanup - clean up early platform code
  988. */
  989. void __init early_platform_cleanup(void)
  990. {
  991. struct platform_device *pd, *pd2;
  992. /* clean up the devres list used to chain devices */
  993. list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
  994. dev.devres_head) {
  995. list_del(&pd->dev.devres_head);
  996. memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
  997. }
  998. }