platform.c 31 KB

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