platform.c 37 KB

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