platform.c 37 KB

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