platform.c 31 KB

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