platform.c 29 KB

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