platform.c 30 KB

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