platform.c 36 KB

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