platform.c 30 KB

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