platform.c 30 KB

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