platform.c 35 KB

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