platform.c 37 KB

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