platform.c 32 KB

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