platform.c 37 KB

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