platform.c 30 KB

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