platform.c 37 KB

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