platform.c 28 KB

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