platform.c 32 KB

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