platform.c 30 KB

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