platform.c 32 KB

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