platform.c 31 KB

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