platform.c 37 KB

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