platform.c 38 KB

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