platform.c 33 KB

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