platform.c 30 KB

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