platform.c 36 KB

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