platform.c 30 KB

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