platform.c 30 KB

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