platform.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  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. * @module: module which will be the owner of the driver
  504. *
  505. * Use this instead of platform_driver_register() when you know the device
  506. * is not hotpluggable and has already been registered, and you want to
  507. * remove its run-once probe() infrastructure from memory after the driver
  508. * has bound to the device.
  509. *
  510. * One typical use for this would be with drivers for controllers integrated
  511. * into system-on-chip processors, where the controller devices have been
  512. * configured as part of board setup.
  513. *
  514. * Note that this is incompatible with deferred probing.
  515. *
  516. * Returns zero if the driver registered and bound to a device, else returns
  517. * a negative error code and with the driver not registered.
  518. */
  519. int __init_or_module __platform_driver_probe(struct platform_driver *drv,
  520. int (*probe)(struct platform_device *), struct module *module)
  521. {
  522. int retval, code;
  523. /*
  524. * Prevent driver from requesting probe deferral to avoid further
  525. * futile probe attempts.
  526. */
  527. drv->prevent_deferred_probe = true;
  528. /* make sure driver won't have bind/unbind attributes */
  529. drv->driver.suppress_bind_attrs = true;
  530. /* temporary section violation during probe() */
  531. drv->probe = probe;
  532. retval = code = __platform_driver_register(drv, module);
  533. /*
  534. * Fixup that section violation, being paranoid about code scanning
  535. * the list of drivers in order to probe new devices. Check to see
  536. * if the probe was successful, and make sure any forced probes of
  537. * new devices fail.
  538. */
  539. spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
  540. drv->probe = NULL;
  541. if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
  542. retval = -ENODEV;
  543. drv->driver.probe = platform_drv_probe_fail;
  544. spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
  545. if (code != retval)
  546. platform_driver_unregister(drv);
  547. return retval;
  548. }
  549. EXPORT_SYMBOL_GPL(__platform_driver_probe);
  550. /**
  551. * __platform_create_bundle - register driver and create corresponding device
  552. * @driver: platform driver structure
  553. * @probe: the driver probe routine, probably from an __init section
  554. * @res: set of resources that needs to be allocated for the device
  555. * @n_res: number of resources
  556. * @data: platform specific data for this platform device
  557. * @size: size of platform specific data
  558. * @module: module which will be the owner of the driver
  559. *
  560. * Use this in legacy-style modules that probe hardware directly and
  561. * register a single platform device and corresponding platform driver.
  562. *
  563. * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
  564. */
  565. struct platform_device * __init_or_module __platform_create_bundle(
  566. struct platform_driver *driver,
  567. int (*probe)(struct platform_device *),
  568. struct resource *res, unsigned int n_res,
  569. const void *data, size_t size, struct module *module)
  570. {
  571. struct platform_device *pdev;
  572. int error;
  573. pdev = platform_device_alloc(driver->driver.name, -1);
  574. if (!pdev) {
  575. error = -ENOMEM;
  576. goto err_out;
  577. }
  578. error = platform_device_add_resources(pdev, res, n_res);
  579. if (error)
  580. goto err_pdev_put;
  581. error = platform_device_add_data(pdev, data, size);
  582. if (error)
  583. goto err_pdev_put;
  584. error = platform_device_add(pdev);
  585. if (error)
  586. goto err_pdev_put;
  587. error = __platform_driver_probe(driver, probe, module);
  588. if (error)
  589. goto err_pdev_del;
  590. return pdev;
  591. err_pdev_del:
  592. platform_device_del(pdev);
  593. err_pdev_put:
  594. platform_device_put(pdev);
  595. err_out:
  596. return ERR_PTR(error);
  597. }
  598. EXPORT_SYMBOL_GPL(__platform_create_bundle);
  599. /* modalias support enables more hands-off userspace setup:
  600. * (a) environment variable lets new-style hotplug events work once system is
  601. * fully running: "modprobe $MODALIAS"
  602. * (b) sysfs attribute lets new-style coldplug recover from hotplug events
  603. * mishandled before system is fully running: "modprobe $(cat modalias)"
  604. */
  605. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  606. char *buf)
  607. {
  608. struct platform_device *pdev = to_platform_device(dev);
  609. int len;
  610. len = of_device_get_modalias(dev, buf, PAGE_SIZE -1);
  611. if (len != -ENODEV)
  612. return len;
  613. len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
  614. if (len != -ENODEV)
  615. return len;
  616. len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
  617. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  618. }
  619. static DEVICE_ATTR_RO(modalias);
  620. static ssize_t driver_override_store(struct device *dev,
  621. struct device_attribute *attr,
  622. const char *buf, size_t count)
  623. {
  624. struct platform_device *pdev = to_platform_device(dev);
  625. char *driver_override, *old = pdev->driver_override, *cp;
  626. if (count > PATH_MAX)
  627. return -EINVAL;
  628. driver_override = kstrndup(buf, count, GFP_KERNEL);
  629. if (!driver_override)
  630. return -ENOMEM;
  631. cp = strchr(driver_override, '\n');
  632. if (cp)
  633. *cp = '\0';
  634. if (strlen(driver_override)) {
  635. pdev->driver_override = driver_override;
  636. } else {
  637. kfree(driver_override);
  638. pdev->driver_override = NULL;
  639. }
  640. kfree(old);
  641. return count;
  642. }
  643. static ssize_t driver_override_show(struct device *dev,
  644. struct device_attribute *attr, char *buf)
  645. {
  646. struct platform_device *pdev = to_platform_device(dev);
  647. return sprintf(buf, "%s\n", pdev->driver_override);
  648. }
  649. static DEVICE_ATTR_RW(driver_override);
  650. static struct attribute *platform_dev_attrs[] = {
  651. &dev_attr_modalias.attr,
  652. &dev_attr_driver_override.attr,
  653. NULL,
  654. };
  655. ATTRIBUTE_GROUPS(platform_dev);
  656. static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
  657. {
  658. struct platform_device *pdev = to_platform_device(dev);
  659. int rc;
  660. /* Some devices have extra OF data and an OF-style MODALIAS */
  661. rc = of_device_uevent_modalias(dev, env);
  662. if (rc != -ENODEV)
  663. return rc;
  664. rc = acpi_device_uevent_modalias(dev, env);
  665. if (rc != -ENODEV)
  666. return rc;
  667. add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
  668. pdev->name);
  669. return 0;
  670. }
  671. static const struct platform_device_id *platform_match_id(
  672. const struct platform_device_id *id,
  673. struct platform_device *pdev)
  674. {
  675. while (id->name[0]) {
  676. if (strcmp(pdev->name, id->name) == 0) {
  677. pdev->id_entry = id;
  678. return id;
  679. }
  680. id++;
  681. }
  682. return NULL;
  683. }
  684. /**
  685. * platform_match - bind platform device to platform driver.
  686. * @dev: device.
  687. * @drv: driver.
  688. *
  689. * Platform device IDs are assumed to be encoded like this:
  690. * "<name><instance>", where <name> is a short description of the type of
  691. * device, like "pci" or "floppy", and <instance> is the enumerated
  692. * instance of the device, like '0' or '42'. Driver IDs are simply
  693. * "<name>". So, extract the <name> from the platform_device structure,
  694. * and compare it against the name of the driver. Return whether they match
  695. * or not.
  696. */
  697. static int platform_match(struct device *dev, struct device_driver *drv)
  698. {
  699. struct platform_device *pdev = to_platform_device(dev);
  700. struct platform_driver *pdrv = to_platform_driver(drv);
  701. /* When driver_override is set, only bind to the matching driver */
  702. if (pdev->driver_override)
  703. return !strcmp(pdev->driver_override, drv->name);
  704. /* Attempt an OF style match first */
  705. if (of_driver_match_device(dev, drv))
  706. return 1;
  707. /* Then try ACPI style match */
  708. if (acpi_driver_match_device(dev, drv))
  709. return 1;
  710. /* Then try to match against the id table */
  711. if (pdrv->id_table)
  712. return platform_match_id(pdrv->id_table, pdev) != NULL;
  713. /* fall-back to driver name match */
  714. return (strcmp(pdev->name, drv->name) == 0);
  715. }
  716. #ifdef CONFIG_PM_SLEEP
  717. static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
  718. {
  719. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  720. struct platform_device *pdev = to_platform_device(dev);
  721. int ret = 0;
  722. if (dev->driver && pdrv->suspend)
  723. ret = pdrv->suspend(pdev, mesg);
  724. return ret;
  725. }
  726. static int platform_legacy_resume(struct device *dev)
  727. {
  728. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  729. struct platform_device *pdev = to_platform_device(dev);
  730. int ret = 0;
  731. if (dev->driver && pdrv->resume)
  732. ret = pdrv->resume(pdev);
  733. return ret;
  734. }
  735. #endif /* CONFIG_PM_SLEEP */
  736. #ifdef CONFIG_SUSPEND
  737. int platform_pm_suspend(struct device *dev)
  738. {
  739. struct device_driver *drv = dev->driver;
  740. int ret = 0;
  741. if (!drv)
  742. return 0;
  743. if (drv->pm) {
  744. if (drv->pm->suspend)
  745. ret = drv->pm->suspend(dev);
  746. } else {
  747. ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
  748. }
  749. return ret;
  750. }
  751. int platform_pm_resume(struct device *dev)
  752. {
  753. struct device_driver *drv = dev->driver;
  754. int ret = 0;
  755. if (!drv)
  756. return 0;
  757. if (drv->pm) {
  758. if (drv->pm->resume)
  759. ret = drv->pm->resume(dev);
  760. } else {
  761. ret = platform_legacy_resume(dev);
  762. }
  763. return ret;
  764. }
  765. #endif /* CONFIG_SUSPEND */
  766. #ifdef CONFIG_HIBERNATE_CALLBACKS
  767. int platform_pm_freeze(struct device *dev)
  768. {
  769. struct device_driver *drv = dev->driver;
  770. int ret = 0;
  771. if (!drv)
  772. return 0;
  773. if (drv->pm) {
  774. if (drv->pm->freeze)
  775. ret = drv->pm->freeze(dev);
  776. } else {
  777. ret = platform_legacy_suspend(dev, PMSG_FREEZE);
  778. }
  779. return ret;
  780. }
  781. int platform_pm_thaw(struct device *dev)
  782. {
  783. struct device_driver *drv = dev->driver;
  784. int ret = 0;
  785. if (!drv)
  786. return 0;
  787. if (drv->pm) {
  788. if (drv->pm->thaw)
  789. ret = drv->pm->thaw(dev);
  790. } else {
  791. ret = platform_legacy_resume(dev);
  792. }
  793. return ret;
  794. }
  795. int platform_pm_poweroff(struct device *dev)
  796. {
  797. struct device_driver *drv = dev->driver;
  798. int ret = 0;
  799. if (!drv)
  800. return 0;
  801. if (drv->pm) {
  802. if (drv->pm->poweroff)
  803. ret = drv->pm->poweroff(dev);
  804. } else {
  805. ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
  806. }
  807. return ret;
  808. }
  809. int platform_pm_restore(struct device *dev)
  810. {
  811. struct device_driver *drv = dev->driver;
  812. int ret = 0;
  813. if (!drv)
  814. return 0;
  815. if (drv->pm) {
  816. if (drv->pm->restore)
  817. ret = drv->pm->restore(dev);
  818. } else {
  819. ret = platform_legacy_resume(dev);
  820. }
  821. return ret;
  822. }
  823. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  824. static const struct dev_pm_ops platform_dev_pm_ops = {
  825. .runtime_suspend = pm_generic_runtime_suspend,
  826. .runtime_resume = pm_generic_runtime_resume,
  827. USE_PLATFORM_PM_SLEEP_OPS
  828. };
  829. struct bus_type platform_bus_type = {
  830. .name = "platform",
  831. .dev_groups = platform_dev_groups,
  832. .match = platform_match,
  833. .uevent = platform_uevent,
  834. .pm = &platform_dev_pm_ops,
  835. };
  836. EXPORT_SYMBOL_GPL(platform_bus_type);
  837. int __init platform_bus_init(void)
  838. {
  839. int error;
  840. early_platform_cleanup();
  841. error = device_register(&platform_bus);
  842. if (error)
  843. return error;
  844. error = bus_register(&platform_bus_type);
  845. if (error)
  846. device_unregister(&platform_bus);
  847. of_platform_register_reconfig_notifier();
  848. return error;
  849. }
  850. #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
  851. u64 dma_get_required_mask(struct device *dev)
  852. {
  853. u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
  854. u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
  855. u64 mask;
  856. if (!high_totalram) {
  857. /* convert to mask just covering totalram */
  858. low_totalram = (1 << (fls(low_totalram) - 1));
  859. low_totalram += low_totalram - 1;
  860. mask = low_totalram;
  861. } else {
  862. high_totalram = (1 << (fls(high_totalram) - 1));
  863. high_totalram += high_totalram - 1;
  864. mask = (((u64)high_totalram) << 32) + 0xffffffff;
  865. }
  866. return mask;
  867. }
  868. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  869. #endif
  870. static __initdata LIST_HEAD(early_platform_driver_list);
  871. static __initdata LIST_HEAD(early_platform_device_list);
  872. /**
  873. * early_platform_driver_register - register early platform driver
  874. * @epdrv: early_platform driver structure
  875. * @buf: string passed from early_param()
  876. *
  877. * Helper function for early_platform_init() / early_platform_init_buffer()
  878. */
  879. int __init early_platform_driver_register(struct early_platform_driver *epdrv,
  880. char *buf)
  881. {
  882. char *tmp;
  883. int n;
  884. /* Simply add the driver to the end of the global list.
  885. * Drivers will by default be put on the list in compiled-in order.
  886. */
  887. if (!epdrv->list.next) {
  888. INIT_LIST_HEAD(&epdrv->list);
  889. list_add_tail(&epdrv->list, &early_platform_driver_list);
  890. }
  891. /* If the user has specified device then make sure the driver
  892. * gets prioritized. The driver of the last device specified on
  893. * command line will be put first on the list.
  894. */
  895. n = strlen(epdrv->pdrv->driver.name);
  896. if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
  897. list_move(&epdrv->list, &early_platform_driver_list);
  898. /* Allow passing parameters after device name */
  899. if (buf[n] == '\0' || buf[n] == ',')
  900. epdrv->requested_id = -1;
  901. else {
  902. epdrv->requested_id = simple_strtoul(&buf[n + 1],
  903. &tmp, 10);
  904. if (buf[n] != '.' || (tmp == &buf[n + 1])) {
  905. epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
  906. n = 0;
  907. } else
  908. n += strcspn(&buf[n + 1], ",") + 1;
  909. }
  910. if (buf[n] == ',')
  911. n++;
  912. if (epdrv->bufsize) {
  913. memcpy(epdrv->buffer, &buf[n],
  914. min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
  915. epdrv->buffer[epdrv->bufsize - 1] = '\0';
  916. }
  917. }
  918. return 0;
  919. }
  920. /**
  921. * early_platform_add_devices - adds a number of early platform devices
  922. * @devs: array of early platform devices to add
  923. * @num: number of early platform devices in array
  924. *
  925. * Used by early architecture code to register early platform devices and
  926. * their platform data.
  927. */
  928. void __init early_platform_add_devices(struct platform_device **devs, int num)
  929. {
  930. struct device *dev;
  931. int i;
  932. /* simply add the devices to list */
  933. for (i = 0; i < num; i++) {
  934. dev = &devs[i]->dev;
  935. if (!dev->devres_head.next) {
  936. pm_runtime_early_init(dev);
  937. INIT_LIST_HEAD(&dev->devres_head);
  938. list_add_tail(&dev->devres_head,
  939. &early_platform_device_list);
  940. }
  941. }
  942. }
  943. /**
  944. * early_platform_driver_register_all - register early platform drivers
  945. * @class_str: string to identify early platform driver class
  946. *
  947. * Used by architecture code to register all early platform drivers
  948. * for a certain class. If omitted then only early platform drivers
  949. * with matching kernel command line class parameters will be registered.
  950. */
  951. void __init early_platform_driver_register_all(char *class_str)
  952. {
  953. /* The "class_str" parameter may or may not be present on the kernel
  954. * command line. If it is present then there may be more than one
  955. * matching parameter.
  956. *
  957. * Since we register our early platform drivers using early_param()
  958. * we need to make sure that they also get registered in the case
  959. * when the parameter is missing from the kernel command line.
  960. *
  961. * We use parse_early_options() to make sure the early_param() gets
  962. * called at least once. The early_param() may be called more than
  963. * once since the name of the preferred device may be specified on
  964. * the kernel command line. early_platform_driver_register() handles
  965. * this case for us.
  966. */
  967. parse_early_options(class_str);
  968. }
  969. /**
  970. * early_platform_match - find early platform device matching driver
  971. * @epdrv: early platform driver structure
  972. * @id: id to match against
  973. */
  974. static struct platform_device * __init
  975. early_platform_match(struct early_platform_driver *epdrv, int id)
  976. {
  977. struct platform_device *pd;
  978. list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
  979. if (platform_match(&pd->dev, &epdrv->pdrv->driver))
  980. if (pd->id == id)
  981. return pd;
  982. return NULL;
  983. }
  984. /**
  985. * early_platform_left - check if early platform driver has matching devices
  986. * @epdrv: early platform driver structure
  987. * @id: return true if id or above exists
  988. */
  989. static int __init early_platform_left(struct early_platform_driver *epdrv,
  990. int id)
  991. {
  992. struct platform_device *pd;
  993. list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
  994. if (platform_match(&pd->dev, &epdrv->pdrv->driver))
  995. if (pd->id >= id)
  996. return 1;
  997. return 0;
  998. }
  999. /**
  1000. * early_platform_driver_probe_id - probe drivers matching class_str and id
  1001. * @class_str: string to identify early platform driver class
  1002. * @id: id to match against
  1003. * @nr_probe: number of platform devices to successfully probe before exiting
  1004. */
  1005. static int __init early_platform_driver_probe_id(char *class_str,
  1006. int id,
  1007. int nr_probe)
  1008. {
  1009. struct early_platform_driver *epdrv;
  1010. struct platform_device *match;
  1011. int match_id;
  1012. int n = 0;
  1013. int left = 0;
  1014. list_for_each_entry(epdrv, &early_platform_driver_list, list) {
  1015. /* only use drivers matching our class_str */
  1016. if (strcmp(class_str, epdrv->class_str))
  1017. continue;
  1018. if (id == -2) {
  1019. match_id = epdrv->requested_id;
  1020. left = 1;
  1021. } else {
  1022. match_id = id;
  1023. left += early_platform_left(epdrv, id);
  1024. /* skip requested id */
  1025. switch (epdrv->requested_id) {
  1026. case EARLY_PLATFORM_ID_ERROR:
  1027. case EARLY_PLATFORM_ID_UNSET:
  1028. break;
  1029. default:
  1030. if (epdrv->requested_id == id)
  1031. match_id = EARLY_PLATFORM_ID_UNSET;
  1032. }
  1033. }
  1034. switch (match_id) {
  1035. case EARLY_PLATFORM_ID_ERROR:
  1036. pr_warn("%s: unable to parse %s parameter\n",
  1037. class_str, epdrv->pdrv->driver.name);
  1038. /* fall-through */
  1039. case EARLY_PLATFORM_ID_UNSET:
  1040. match = NULL;
  1041. break;
  1042. default:
  1043. match = early_platform_match(epdrv, match_id);
  1044. }
  1045. if (match) {
  1046. /*
  1047. * Set up a sensible init_name to enable
  1048. * dev_name() and others to be used before the
  1049. * rest of the driver core is initialized.
  1050. */
  1051. if (!match->dev.init_name && slab_is_available()) {
  1052. if (match->id != -1)
  1053. match->dev.init_name =
  1054. kasprintf(GFP_KERNEL, "%s.%d",
  1055. match->name,
  1056. match->id);
  1057. else
  1058. match->dev.init_name =
  1059. kasprintf(GFP_KERNEL, "%s",
  1060. match->name);
  1061. if (!match->dev.init_name)
  1062. return -ENOMEM;
  1063. }
  1064. if (epdrv->pdrv->probe(match))
  1065. pr_warn("%s: unable to probe %s early.\n",
  1066. class_str, match->name);
  1067. else
  1068. n++;
  1069. }
  1070. if (n >= nr_probe)
  1071. break;
  1072. }
  1073. if (left)
  1074. return n;
  1075. else
  1076. return -ENODEV;
  1077. }
  1078. /**
  1079. * early_platform_driver_probe - probe a class of registered drivers
  1080. * @class_str: string to identify early platform driver class
  1081. * @nr_probe: number of platform devices to successfully probe before exiting
  1082. * @user_only: only probe user specified early platform devices
  1083. *
  1084. * Used by architecture code to probe registered early platform drivers
  1085. * within a certain class. For probe to happen a registered early platform
  1086. * device matching a registered early platform driver is needed.
  1087. */
  1088. int __init early_platform_driver_probe(char *class_str,
  1089. int nr_probe,
  1090. int user_only)
  1091. {
  1092. int k, n, i;
  1093. n = 0;
  1094. for (i = -2; n < nr_probe; i++) {
  1095. k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
  1096. if (k < 0)
  1097. break;
  1098. n += k;
  1099. if (user_only)
  1100. break;
  1101. }
  1102. return n;
  1103. }
  1104. /**
  1105. * early_platform_cleanup - clean up early platform code
  1106. */
  1107. void __init early_platform_cleanup(void)
  1108. {
  1109. struct platform_device *pd, *pd2;
  1110. /* clean up the devres list used to chain devices */
  1111. list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
  1112. dev.devres_head) {
  1113. list_del(&pd->dev.devres_head);
  1114. memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
  1115. }
  1116. }