platform.c 33 KB

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