platform.c 32 KB

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