platform.c 33 KB

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