platform.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  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. * @properties: null terminated array of properties to add
  285. *
  286. * The function will take deep copy of @properties and attach the copy to the
  287. * platform device. The memory associated with properties will be freed when the
  288. * platform device is released.
  289. */
  290. int platform_device_add_properties(struct platform_device *pdev,
  291. struct property_entry *properties)
  292. {
  293. return device_add_properties(&pdev->dev, properties);
  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_properties(&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->properties) {
  460. ret = platform_device_add_properties(pdev,
  461. pdevinfo->properties);
  462. if (ret)
  463. goto err;
  464. }
  465. ret = platform_device_add(pdev);
  466. if (ret) {
  467. err:
  468. ACPI_COMPANION_SET(&pdev->dev, NULL);
  469. kfree(pdev->dev.dma_mask);
  470. err_alloc:
  471. platform_device_put(pdev);
  472. return ERR_PTR(ret);
  473. }
  474. return pdev;
  475. }
  476. EXPORT_SYMBOL_GPL(platform_device_register_full);
  477. static int platform_drv_probe(struct device *_dev)
  478. {
  479. struct platform_driver *drv = to_platform_driver(_dev->driver);
  480. struct platform_device *dev = to_platform_device(_dev);
  481. int ret;
  482. ret = of_clk_set_defaults(_dev->of_node, false);
  483. if (ret < 0)
  484. return ret;
  485. ret = dev_pm_domain_attach(_dev, true);
  486. if (ret != -EPROBE_DEFER) {
  487. if (drv->probe) {
  488. ret = drv->probe(dev);
  489. if (ret)
  490. dev_pm_domain_detach(_dev, true);
  491. } else {
  492. /* don't fail if just dev_pm_domain_attach failed */
  493. ret = 0;
  494. }
  495. }
  496. if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
  497. dev_warn(_dev, "probe deferral not supported\n");
  498. ret = -ENXIO;
  499. }
  500. return ret;
  501. }
  502. static int platform_drv_probe_fail(struct device *_dev)
  503. {
  504. return -ENXIO;
  505. }
  506. static int platform_drv_remove(struct device *_dev)
  507. {
  508. struct platform_driver *drv = to_platform_driver(_dev->driver);
  509. struct platform_device *dev = to_platform_device(_dev);
  510. int ret = 0;
  511. if (drv->remove)
  512. ret = drv->remove(dev);
  513. dev_pm_domain_detach(_dev, true);
  514. return ret;
  515. }
  516. static void platform_drv_shutdown(struct device *_dev)
  517. {
  518. struct platform_driver *drv = to_platform_driver(_dev->driver);
  519. struct platform_device *dev = to_platform_device(_dev);
  520. if (drv->shutdown)
  521. drv->shutdown(dev);
  522. }
  523. /**
  524. * __platform_driver_register - register a driver for platform-level devices
  525. * @drv: platform driver structure
  526. * @owner: owning module/driver
  527. */
  528. int __platform_driver_register(struct platform_driver *drv,
  529. struct module *owner)
  530. {
  531. drv->driver.owner = owner;
  532. drv->driver.bus = &platform_bus_type;
  533. drv->driver.probe = platform_drv_probe;
  534. drv->driver.remove = platform_drv_remove;
  535. drv->driver.shutdown = platform_drv_shutdown;
  536. return driver_register(&drv->driver);
  537. }
  538. EXPORT_SYMBOL_GPL(__platform_driver_register);
  539. /**
  540. * platform_driver_unregister - unregister a driver for platform-level devices
  541. * @drv: platform driver structure
  542. */
  543. void platform_driver_unregister(struct platform_driver *drv)
  544. {
  545. driver_unregister(&drv->driver);
  546. }
  547. EXPORT_SYMBOL_GPL(platform_driver_unregister);
  548. /**
  549. * __platform_driver_probe - register driver for non-hotpluggable device
  550. * @drv: platform driver structure
  551. * @probe: the driver probe routine, probably from an __init section
  552. * @module: module which will be the owner of the driver
  553. *
  554. * Use this instead of platform_driver_register() when you know the device
  555. * is not hotpluggable and has already been registered, and you want to
  556. * remove its run-once probe() infrastructure from memory after the driver
  557. * has bound to the device.
  558. *
  559. * One typical use for this would be with drivers for controllers integrated
  560. * into system-on-chip processors, where the controller devices have been
  561. * configured as part of board setup.
  562. *
  563. * Note that this is incompatible with deferred probing.
  564. *
  565. * Returns zero if the driver registered and bound to a device, else returns
  566. * a negative error code and with the driver not registered.
  567. */
  568. int __init_or_module __platform_driver_probe(struct platform_driver *drv,
  569. int (*probe)(struct platform_device *), struct module *module)
  570. {
  571. int retval, code;
  572. if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
  573. pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
  574. drv->driver.name, __func__);
  575. return -EINVAL;
  576. }
  577. /*
  578. * We have to run our probes synchronously because we check if
  579. * we find any devices to bind to and exit with error if there
  580. * are any.
  581. */
  582. drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
  583. /*
  584. * Prevent driver from requesting probe deferral to avoid further
  585. * futile probe attempts.
  586. */
  587. drv->prevent_deferred_probe = true;
  588. /* make sure driver won't have bind/unbind attributes */
  589. drv->driver.suppress_bind_attrs = true;
  590. /* temporary section violation during probe() */
  591. drv->probe = probe;
  592. retval = code = __platform_driver_register(drv, module);
  593. /*
  594. * Fixup that section violation, being paranoid about code scanning
  595. * the list of drivers in order to probe new devices. Check to see
  596. * if the probe was successful, and make sure any forced probes of
  597. * new devices fail.
  598. */
  599. spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
  600. drv->probe = NULL;
  601. if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
  602. retval = -ENODEV;
  603. drv->driver.probe = platform_drv_probe_fail;
  604. spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
  605. if (code != retval)
  606. platform_driver_unregister(drv);
  607. return retval;
  608. }
  609. EXPORT_SYMBOL_GPL(__platform_driver_probe);
  610. /**
  611. * __platform_create_bundle - register driver and create corresponding device
  612. * @driver: platform driver structure
  613. * @probe: the driver probe routine, probably from an __init section
  614. * @res: set of resources that needs to be allocated for the device
  615. * @n_res: number of resources
  616. * @data: platform specific data for this platform device
  617. * @size: size of platform specific data
  618. * @module: module which will be the owner of the driver
  619. *
  620. * Use this in legacy-style modules that probe hardware directly and
  621. * register a single platform device and corresponding platform driver.
  622. *
  623. * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
  624. */
  625. struct platform_device * __init_or_module __platform_create_bundle(
  626. struct platform_driver *driver,
  627. int (*probe)(struct platform_device *),
  628. struct resource *res, unsigned int n_res,
  629. const void *data, size_t size, struct module *module)
  630. {
  631. struct platform_device *pdev;
  632. int error;
  633. pdev = platform_device_alloc(driver->driver.name, -1);
  634. if (!pdev) {
  635. error = -ENOMEM;
  636. goto err_out;
  637. }
  638. error = platform_device_add_resources(pdev, res, n_res);
  639. if (error)
  640. goto err_pdev_put;
  641. error = platform_device_add_data(pdev, data, size);
  642. if (error)
  643. goto err_pdev_put;
  644. error = platform_device_add(pdev);
  645. if (error)
  646. goto err_pdev_put;
  647. error = __platform_driver_probe(driver, probe, module);
  648. if (error)
  649. goto err_pdev_del;
  650. return pdev;
  651. err_pdev_del:
  652. platform_device_del(pdev);
  653. err_pdev_put:
  654. platform_device_put(pdev);
  655. err_out:
  656. return ERR_PTR(error);
  657. }
  658. EXPORT_SYMBOL_GPL(__platform_create_bundle);
  659. /**
  660. * __platform_register_drivers - register an array of platform drivers
  661. * @drivers: an array of drivers to register
  662. * @count: the number of drivers to register
  663. * @owner: module owning the drivers
  664. *
  665. * Registers platform drivers specified by an array. On failure to register a
  666. * driver, all previously registered drivers will be unregistered. Callers of
  667. * this API should use platform_unregister_drivers() to unregister drivers in
  668. * the reverse order.
  669. *
  670. * Returns: 0 on success or a negative error code on failure.
  671. */
  672. int __platform_register_drivers(struct platform_driver * const *drivers,
  673. unsigned int count, struct module *owner)
  674. {
  675. unsigned int i;
  676. int err;
  677. for (i = 0; i < count; i++) {
  678. pr_debug("registering platform driver %ps\n", drivers[i]);
  679. err = __platform_driver_register(drivers[i], owner);
  680. if (err < 0) {
  681. pr_err("failed to register platform driver %ps: %d\n",
  682. drivers[i], err);
  683. goto error;
  684. }
  685. }
  686. return 0;
  687. error:
  688. while (i--) {
  689. pr_debug("unregistering platform driver %ps\n", drivers[i]);
  690. platform_driver_unregister(drivers[i]);
  691. }
  692. return err;
  693. }
  694. EXPORT_SYMBOL_GPL(__platform_register_drivers);
  695. /**
  696. * platform_unregister_drivers - unregister an array of platform drivers
  697. * @drivers: an array of drivers to unregister
  698. * @count: the number of drivers to unregister
  699. *
  700. * Unegisters platform drivers specified by an array. This is typically used
  701. * to complement an earlier call to platform_register_drivers(). Drivers are
  702. * unregistered in the reverse order in which they were registered.
  703. */
  704. void platform_unregister_drivers(struct platform_driver * const *drivers,
  705. unsigned int count)
  706. {
  707. while (count--) {
  708. pr_debug("unregistering platform driver %ps\n", drivers[count]);
  709. platform_driver_unregister(drivers[count]);
  710. }
  711. }
  712. EXPORT_SYMBOL_GPL(platform_unregister_drivers);
  713. /* modalias support enables more hands-off userspace setup:
  714. * (a) environment variable lets new-style hotplug events work once system is
  715. * fully running: "modprobe $MODALIAS"
  716. * (b) sysfs attribute lets new-style coldplug recover from hotplug events
  717. * mishandled before system is fully running: "modprobe $(cat modalias)"
  718. */
  719. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  720. char *buf)
  721. {
  722. struct platform_device *pdev = to_platform_device(dev);
  723. int len;
  724. len = of_device_get_modalias(dev, buf, PAGE_SIZE -1);
  725. if (len != -ENODEV)
  726. return len;
  727. len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
  728. if (len != -ENODEV)
  729. return len;
  730. len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
  731. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  732. }
  733. static DEVICE_ATTR_RO(modalias);
  734. static ssize_t driver_override_store(struct device *dev,
  735. struct device_attribute *attr,
  736. const char *buf, size_t count)
  737. {
  738. struct platform_device *pdev = to_platform_device(dev);
  739. char *driver_override, *old = pdev->driver_override, *cp;
  740. if (count > PATH_MAX)
  741. return -EINVAL;
  742. driver_override = kstrndup(buf, count, GFP_KERNEL);
  743. if (!driver_override)
  744. return -ENOMEM;
  745. cp = strchr(driver_override, '\n');
  746. if (cp)
  747. *cp = '\0';
  748. if (strlen(driver_override)) {
  749. pdev->driver_override = driver_override;
  750. } else {
  751. kfree(driver_override);
  752. pdev->driver_override = NULL;
  753. }
  754. kfree(old);
  755. return count;
  756. }
  757. static ssize_t driver_override_show(struct device *dev,
  758. struct device_attribute *attr, char *buf)
  759. {
  760. struct platform_device *pdev = to_platform_device(dev);
  761. return sprintf(buf, "%s\n", pdev->driver_override);
  762. }
  763. static DEVICE_ATTR_RW(driver_override);
  764. static struct attribute *platform_dev_attrs[] = {
  765. &dev_attr_modalias.attr,
  766. &dev_attr_driver_override.attr,
  767. NULL,
  768. };
  769. ATTRIBUTE_GROUPS(platform_dev);
  770. static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
  771. {
  772. struct platform_device *pdev = to_platform_device(dev);
  773. int rc;
  774. /* Some devices have extra OF data and an OF-style MODALIAS */
  775. rc = of_device_uevent_modalias(dev, env);
  776. if (rc != -ENODEV)
  777. return rc;
  778. rc = acpi_device_uevent_modalias(dev, env);
  779. if (rc != -ENODEV)
  780. return rc;
  781. add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
  782. pdev->name);
  783. return 0;
  784. }
  785. static const struct platform_device_id *platform_match_id(
  786. const struct platform_device_id *id,
  787. struct platform_device *pdev)
  788. {
  789. while (id->name[0]) {
  790. if (strcmp(pdev->name, id->name) == 0) {
  791. pdev->id_entry = id;
  792. return id;
  793. }
  794. id++;
  795. }
  796. return NULL;
  797. }
  798. /**
  799. * platform_match - bind platform device to platform driver.
  800. * @dev: device.
  801. * @drv: driver.
  802. *
  803. * Platform device IDs are assumed to be encoded like this:
  804. * "<name><instance>", where <name> is a short description of the type of
  805. * device, like "pci" or "floppy", and <instance> is the enumerated
  806. * instance of the device, like '0' or '42'. Driver IDs are simply
  807. * "<name>". So, extract the <name> from the platform_device structure,
  808. * and compare it against the name of the driver. Return whether they match
  809. * or not.
  810. */
  811. static int platform_match(struct device *dev, struct device_driver *drv)
  812. {
  813. struct platform_device *pdev = to_platform_device(dev);
  814. struct platform_driver *pdrv = to_platform_driver(drv);
  815. /* When driver_override is set, only bind to the matching driver */
  816. if (pdev->driver_override)
  817. return !strcmp(pdev->driver_override, drv->name);
  818. /* Attempt an OF style match first */
  819. if (of_driver_match_device(dev, drv))
  820. return 1;
  821. /* Then try ACPI style match */
  822. if (acpi_driver_match_device(dev, drv))
  823. return 1;
  824. /* Then try to match against the id table */
  825. if (pdrv->id_table)
  826. return platform_match_id(pdrv->id_table, pdev) != NULL;
  827. /* fall-back to driver name match */
  828. return (strcmp(pdev->name, drv->name) == 0);
  829. }
  830. #ifdef CONFIG_PM_SLEEP
  831. static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
  832. {
  833. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  834. struct platform_device *pdev = to_platform_device(dev);
  835. int ret = 0;
  836. if (dev->driver && pdrv->suspend)
  837. ret = pdrv->suspend(pdev, mesg);
  838. return ret;
  839. }
  840. static int platform_legacy_resume(struct device *dev)
  841. {
  842. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  843. struct platform_device *pdev = to_platform_device(dev);
  844. int ret = 0;
  845. if (dev->driver && pdrv->resume)
  846. ret = pdrv->resume(pdev);
  847. return ret;
  848. }
  849. #endif /* CONFIG_PM_SLEEP */
  850. #ifdef CONFIG_SUSPEND
  851. int platform_pm_suspend(struct device *dev)
  852. {
  853. struct device_driver *drv = dev->driver;
  854. int ret = 0;
  855. if (!drv)
  856. return 0;
  857. if (drv->pm) {
  858. if (drv->pm->suspend)
  859. ret = drv->pm->suspend(dev);
  860. } else {
  861. ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
  862. }
  863. return ret;
  864. }
  865. int platform_pm_resume(struct device *dev)
  866. {
  867. struct device_driver *drv = dev->driver;
  868. int ret = 0;
  869. if (!drv)
  870. return 0;
  871. if (drv->pm) {
  872. if (drv->pm->resume)
  873. ret = drv->pm->resume(dev);
  874. } else {
  875. ret = platform_legacy_resume(dev);
  876. }
  877. return ret;
  878. }
  879. #endif /* CONFIG_SUSPEND */
  880. #ifdef CONFIG_HIBERNATE_CALLBACKS
  881. int platform_pm_freeze(struct device *dev)
  882. {
  883. struct device_driver *drv = dev->driver;
  884. int ret = 0;
  885. if (!drv)
  886. return 0;
  887. if (drv->pm) {
  888. if (drv->pm->freeze)
  889. ret = drv->pm->freeze(dev);
  890. } else {
  891. ret = platform_legacy_suspend(dev, PMSG_FREEZE);
  892. }
  893. return ret;
  894. }
  895. int platform_pm_thaw(struct device *dev)
  896. {
  897. struct device_driver *drv = dev->driver;
  898. int ret = 0;
  899. if (!drv)
  900. return 0;
  901. if (drv->pm) {
  902. if (drv->pm->thaw)
  903. ret = drv->pm->thaw(dev);
  904. } else {
  905. ret = platform_legacy_resume(dev);
  906. }
  907. return ret;
  908. }
  909. int platform_pm_poweroff(struct device *dev)
  910. {
  911. struct device_driver *drv = dev->driver;
  912. int ret = 0;
  913. if (!drv)
  914. return 0;
  915. if (drv->pm) {
  916. if (drv->pm->poweroff)
  917. ret = drv->pm->poweroff(dev);
  918. } else {
  919. ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
  920. }
  921. return ret;
  922. }
  923. int platform_pm_restore(struct device *dev)
  924. {
  925. struct device_driver *drv = dev->driver;
  926. int ret = 0;
  927. if (!drv)
  928. return 0;
  929. if (drv->pm) {
  930. if (drv->pm->restore)
  931. ret = drv->pm->restore(dev);
  932. } else {
  933. ret = platform_legacy_resume(dev);
  934. }
  935. return ret;
  936. }
  937. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  938. static const struct dev_pm_ops platform_dev_pm_ops = {
  939. .runtime_suspend = pm_generic_runtime_suspend,
  940. .runtime_resume = pm_generic_runtime_resume,
  941. USE_PLATFORM_PM_SLEEP_OPS
  942. };
  943. struct bus_type platform_bus_type = {
  944. .name = "platform",
  945. .dev_groups = platform_dev_groups,
  946. .match = platform_match,
  947. .uevent = platform_uevent,
  948. .pm = &platform_dev_pm_ops,
  949. };
  950. EXPORT_SYMBOL_GPL(platform_bus_type);
  951. int __init platform_bus_init(void)
  952. {
  953. int error;
  954. early_platform_cleanup();
  955. error = device_register(&platform_bus);
  956. if (error)
  957. return error;
  958. error = bus_register(&platform_bus_type);
  959. if (error)
  960. device_unregister(&platform_bus);
  961. of_platform_register_reconfig_notifier();
  962. return error;
  963. }
  964. #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
  965. u64 dma_get_required_mask(struct device *dev)
  966. {
  967. u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
  968. u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
  969. u64 mask;
  970. if (!high_totalram) {
  971. /* convert to mask just covering totalram */
  972. low_totalram = (1 << (fls(low_totalram) - 1));
  973. low_totalram += low_totalram - 1;
  974. mask = low_totalram;
  975. } else {
  976. high_totalram = (1 << (fls(high_totalram) - 1));
  977. high_totalram += high_totalram - 1;
  978. mask = (((u64)high_totalram) << 32) + 0xffffffff;
  979. }
  980. return mask;
  981. }
  982. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  983. #endif
  984. static __initdata LIST_HEAD(early_platform_driver_list);
  985. static __initdata LIST_HEAD(early_platform_device_list);
  986. /**
  987. * early_platform_driver_register - register early platform driver
  988. * @epdrv: early_platform driver structure
  989. * @buf: string passed from early_param()
  990. *
  991. * Helper function for early_platform_init() / early_platform_init_buffer()
  992. */
  993. int __init early_platform_driver_register(struct early_platform_driver *epdrv,
  994. char *buf)
  995. {
  996. char *tmp;
  997. int n;
  998. /* Simply add the driver to the end of the global list.
  999. * Drivers will by default be put on the list in compiled-in order.
  1000. */
  1001. if (!epdrv->list.next) {
  1002. INIT_LIST_HEAD(&epdrv->list);
  1003. list_add_tail(&epdrv->list, &early_platform_driver_list);
  1004. }
  1005. /* If the user has specified device then make sure the driver
  1006. * gets prioritized. The driver of the last device specified on
  1007. * command line will be put first on the list.
  1008. */
  1009. n = strlen(epdrv->pdrv->driver.name);
  1010. if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
  1011. list_move(&epdrv->list, &early_platform_driver_list);
  1012. /* Allow passing parameters after device name */
  1013. if (buf[n] == '\0' || buf[n] == ',')
  1014. epdrv->requested_id = -1;
  1015. else {
  1016. epdrv->requested_id = simple_strtoul(&buf[n + 1],
  1017. &tmp, 10);
  1018. if (buf[n] != '.' || (tmp == &buf[n + 1])) {
  1019. epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
  1020. n = 0;
  1021. } else
  1022. n += strcspn(&buf[n + 1], ",") + 1;
  1023. }
  1024. if (buf[n] == ',')
  1025. n++;
  1026. if (epdrv->bufsize) {
  1027. memcpy(epdrv->buffer, &buf[n],
  1028. min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
  1029. epdrv->buffer[epdrv->bufsize - 1] = '\0';
  1030. }
  1031. }
  1032. return 0;
  1033. }
  1034. /**
  1035. * early_platform_add_devices - adds a number of early platform devices
  1036. * @devs: array of early platform devices to add
  1037. * @num: number of early platform devices in array
  1038. *
  1039. * Used by early architecture code to register early platform devices and
  1040. * their platform data.
  1041. */
  1042. void __init early_platform_add_devices(struct platform_device **devs, int num)
  1043. {
  1044. struct device *dev;
  1045. int i;
  1046. /* simply add the devices to list */
  1047. for (i = 0; i < num; i++) {
  1048. dev = &devs[i]->dev;
  1049. if (!dev->devres_head.next) {
  1050. pm_runtime_early_init(dev);
  1051. INIT_LIST_HEAD(&dev->devres_head);
  1052. list_add_tail(&dev->devres_head,
  1053. &early_platform_device_list);
  1054. }
  1055. }
  1056. }
  1057. /**
  1058. * early_platform_driver_register_all - register early platform drivers
  1059. * @class_str: string to identify early platform driver class
  1060. *
  1061. * Used by architecture code to register all early platform drivers
  1062. * for a certain class. If omitted then only early platform drivers
  1063. * with matching kernel command line class parameters will be registered.
  1064. */
  1065. void __init early_platform_driver_register_all(char *class_str)
  1066. {
  1067. /* The "class_str" parameter may or may not be present on the kernel
  1068. * command line. If it is present then there may be more than one
  1069. * matching parameter.
  1070. *
  1071. * Since we register our early platform drivers using early_param()
  1072. * we need to make sure that they also get registered in the case
  1073. * when the parameter is missing from the kernel command line.
  1074. *
  1075. * We use parse_early_options() to make sure the early_param() gets
  1076. * called at least once. The early_param() may be called more than
  1077. * once since the name of the preferred device may be specified on
  1078. * the kernel command line. early_platform_driver_register() handles
  1079. * this case for us.
  1080. */
  1081. parse_early_options(class_str);
  1082. }
  1083. /**
  1084. * early_platform_match - find early platform device matching driver
  1085. * @epdrv: early platform driver structure
  1086. * @id: id to match against
  1087. */
  1088. static struct platform_device * __init
  1089. early_platform_match(struct early_platform_driver *epdrv, int id)
  1090. {
  1091. struct platform_device *pd;
  1092. list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
  1093. if (platform_match(&pd->dev, &epdrv->pdrv->driver))
  1094. if (pd->id == id)
  1095. return pd;
  1096. return NULL;
  1097. }
  1098. /**
  1099. * early_platform_left - check if early platform driver has matching devices
  1100. * @epdrv: early platform driver structure
  1101. * @id: return true if id or above exists
  1102. */
  1103. static int __init early_platform_left(struct early_platform_driver *epdrv,
  1104. int id)
  1105. {
  1106. struct platform_device *pd;
  1107. list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
  1108. if (platform_match(&pd->dev, &epdrv->pdrv->driver))
  1109. if (pd->id >= id)
  1110. return 1;
  1111. return 0;
  1112. }
  1113. /**
  1114. * early_platform_driver_probe_id - probe drivers matching class_str and id
  1115. * @class_str: string to identify early platform driver class
  1116. * @id: id to match against
  1117. * @nr_probe: number of platform devices to successfully probe before exiting
  1118. */
  1119. static int __init early_platform_driver_probe_id(char *class_str,
  1120. int id,
  1121. int nr_probe)
  1122. {
  1123. struct early_platform_driver *epdrv;
  1124. struct platform_device *match;
  1125. int match_id;
  1126. int n = 0;
  1127. int left = 0;
  1128. list_for_each_entry(epdrv, &early_platform_driver_list, list) {
  1129. /* only use drivers matching our class_str */
  1130. if (strcmp(class_str, epdrv->class_str))
  1131. continue;
  1132. if (id == -2) {
  1133. match_id = epdrv->requested_id;
  1134. left = 1;
  1135. } else {
  1136. match_id = id;
  1137. left += early_platform_left(epdrv, id);
  1138. /* skip requested id */
  1139. switch (epdrv->requested_id) {
  1140. case EARLY_PLATFORM_ID_ERROR:
  1141. case EARLY_PLATFORM_ID_UNSET:
  1142. break;
  1143. default:
  1144. if (epdrv->requested_id == id)
  1145. match_id = EARLY_PLATFORM_ID_UNSET;
  1146. }
  1147. }
  1148. switch (match_id) {
  1149. case EARLY_PLATFORM_ID_ERROR:
  1150. pr_warn("%s: unable to parse %s parameter\n",
  1151. class_str, epdrv->pdrv->driver.name);
  1152. /* fall-through */
  1153. case EARLY_PLATFORM_ID_UNSET:
  1154. match = NULL;
  1155. break;
  1156. default:
  1157. match = early_platform_match(epdrv, match_id);
  1158. }
  1159. if (match) {
  1160. /*
  1161. * Set up a sensible init_name to enable
  1162. * dev_name() and others to be used before the
  1163. * rest of the driver core is initialized.
  1164. */
  1165. if (!match->dev.init_name && slab_is_available()) {
  1166. if (match->id != -1)
  1167. match->dev.init_name =
  1168. kasprintf(GFP_KERNEL, "%s.%d",
  1169. match->name,
  1170. match->id);
  1171. else
  1172. match->dev.init_name =
  1173. kasprintf(GFP_KERNEL, "%s",
  1174. match->name);
  1175. if (!match->dev.init_name)
  1176. return -ENOMEM;
  1177. }
  1178. if (epdrv->pdrv->probe(match))
  1179. pr_warn("%s: unable to probe %s early.\n",
  1180. class_str, match->name);
  1181. else
  1182. n++;
  1183. }
  1184. if (n >= nr_probe)
  1185. break;
  1186. }
  1187. if (left)
  1188. return n;
  1189. else
  1190. return -ENODEV;
  1191. }
  1192. /**
  1193. * early_platform_driver_probe - probe a class of registered drivers
  1194. * @class_str: string to identify early platform driver class
  1195. * @nr_probe: number of platform devices to successfully probe before exiting
  1196. * @user_only: only probe user specified early platform devices
  1197. *
  1198. * Used by architecture code to probe registered early platform drivers
  1199. * within a certain class. For probe to happen a registered early platform
  1200. * device matching a registered early platform driver is needed.
  1201. */
  1202. int __init early_platform_driver_probe(char *class_str,
  1203. int nr_probe,
  1204. int user_only)
  1205. {
  1206. int k, n, i;
  1207. n = 0;
  1208. for (i = -2; n < nr_probe; i++) {
  1209. k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
  1210. if (k < 0)
  1211. break;
  1212. n += k;
  1213. if (user_only)
  1214. break;
  1215. }
  1216. return n;
  1217. }
  1218. /**
  1219. * early_platform_cleanup - clean up early platform code
  1220. */
  1221. void __init early_platform_cleanup(void)
  1222. {
  1223. struct platform_device *pd, *pd2;
  1224. /* clean up the devres list used to chain devices */
  1225. list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
  1226. dev.devres_head) {
  1227. list_del(&pd->dev.devres_head);
  1228. memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
  1229. }
  1230. }