platform.c 39 KB

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