platform.c 33 KB

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