platform.c 31 KB

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