platform.c 37 KB

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