platform.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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. ACPI_COMPANION_SET(&pdev->dev, pdevinfo->acpi_node.companion);
  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. *
  513. * Use this instead of platform_driver_register() when you know the device
  514. * is not hotpluggable and has already been registered, and you want to
  515. * remove its run-once probe() infrastructure from memory after the driver
  516. * has bound to the device.
  517. *
  518. * One typical use for this would be with drivers for controllers integrated
  519. * into system-on-chip processors, where the controller devices have been
  520. * configured as part of board setup.
  521. *
  522. * Note that this is incompatible with deferred probing.
  523. *
  524. * Returns zero if the driver registered and bound to a device, else returns
  525. * a negative error code and with the driver not registered.
  526. */
  527. int __init_or_module platform_driver_probe(struct platform_driver *drv,
  528. int (*probe)(struct platform_device *))
  529. {
  530. int retval, code;
  531. /*
  532. * Prevent driver from requesting probe deferral to avoid further
  533. * futile probe attempts.
  534. */
  535. drv->prevent_deferred_probe = true;
  536. /* make sure driver won't have bind/unbind attributes */
  537. drv->driver.suppress_bind_attrs = true;
  538. /* temporary section violation during probe() */
  539. drv->probe = probe;
  540. retval = code = platform_driver_register(drv);
  541. /*
  542. * Fixup that section violation, being paranoid about code scanning
  543. * the list of drivers in order to probe new devices. Check to see
  544. * if the probe was successful, and make sure any forced probes of
  545. * new devices fail.
  546. */
  547. spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
  548. drv->probe = NULL;
  549. if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
  550. retval = -ENODEV;
  551. drv->driver.probe = platform_drv_probe_fail;
  552. spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
  553. if (code != retval)
  554. platform_driver_unregister(drv);
  555. return retval;
  556. }
  557. EXPORT_SYMBOL_GPL(platform_driver_probe);
  558. /**
  559. * platform_create_bundle - register driver and create corresponding device
  560. * @driver: platform driver structure
  561. * @probe: the driver probe routine, probably from an __init section
  562. * @res: set of resources that needs to be allocated for the device
  563. * @n_res: number of resources
  564. * @data: platform specific data for this platform device
  565. * @size: size of platform specific data
  566. *
  567. * Use this in legacy-style modules that probe hardware directly and
  568. * register a single platform device and corresponding platform driver.
  569. *
  570. * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
  571. */
  572. struct platform_device * __init_or_module platform_create_bundle(
  573. struct platform_driver *driver,
  574. int (*probe)(struct platform_device *),
  575. struct resource *res, unsigned int n_res,
  576. const void *data, size_t size)
  577. {
  578. struct platform_device *pdev;
  579. int error;
  580. pdev = platform_device_alloc(driver->driver.name, -1);
  581. if (!pdev) {
  582. error = -ENOMEM;
  583. goto err_out;
  584. }
  585. error = platform_device_add_resources(pdev, res, n_res);
  586. if (error)
  587. goto err_pdev_put;
  588. error = platform_device_add_data(pdev, data, size);
  589. if (error)
  590. goto err_pdev_put;
  591. error = platform_device_add(pdev);
  592. if (error)
  593. goto err_pdev_put;
  594. error = platform_driver_probe(driver, probe);
  595. if (error)
  596. goto err_pdev_del;
  597. return pdev;
  598. err_pdev_del:
  599. platform_device_del(pdev);
  600. err_pdev_put:
  601. platform_device_put(pdev);
  602. err_out:
  603. return ERR_PTR(error);
  604. }
  605. EXPORT_SYMBOL_GPL(platform_create_bundle);
  606. /* modalias support enables more hands-off userspace setup:
  607. * (a) environment variable lets new-style hotplug events work once system is
  608. * fully running: "modprobe $MODALIAS"
  609. * (b) sysfs attribute lets new-style coldplug recover from hotplug events
  610. * mishandled before system is fully running: "modprobe $(cat modalias)"
  611. */
  612. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  613. char *buf)
  614. {
  615. struct platform_device *pdev = to_platform_device(dev);
  616. int len;
  617. len = of_device_get_modalias(dev, buf, PAGE_SIZE -1);
  618. if (len != -ENODEV)
  619. return len;
  620. len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
  621. if (len != -ENODEV)
  622. return len;
  623. len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
  624. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  625. }
  626. static DEVICE_ATTR_RO(modalias);
  627. static ssize_t driver_override_store(struct device *dev,
  628. struct device_attribute *attr,
  629. const char *buf, size_t count)
  630. {
  631. struct platform_device *pdev = to_platform_device(dev);
  632. char *driver_override, *old, *cp;
  633. if (count > PATH_MAX)
  634. return -EINVAL;
  635. driver_override = kstrndup(buf, count, GFP_KERNEL);
  636. if (!driver_override)
  637. return -ENOMEM;
  638. cp = strchr(driver_override, '\n');
  639. if (cp)
  640. *cp = '\0';
  641. device_lock(dev);
  642. old = pdev->driver_override;
  643. if (strlen(driver_override)) {
  644. pdev->driver_override = driver_override;
  645. } else {
  646. kfree(driver_override);
  647. pdev->driver_override = NULL;
  648. }
  649. device_unlock(dev);
  650. kfree(old);
  651. return count;
  652. }
  653. static ssize_t driver_override_show(struct device *dev,
  654. struct device_attribute *attr, char *buf)
  655. {
  656. struct platform_device *pdev = to_platform_device(dev);
  657. ssize_t len;
  658. device_lock(dev);
  659. len = sprintf(buf, "%s\n", pdev->driver_override);
  660. device_unlock(dev);
  661. return len;
  662. }
  663. static DEVICE_ATTR_RW(driver_override);
  664. static struct attribute *platform_dev_attrs[] = {
  665. &dev_attr_modalias.attr,
  666. &dev_attr_driver_override.attr,
  667. NULL,
  668. };
  669. ATTRIBUTE_GROUPS(platform_dev);
  670. static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
  671. {
  672. struct platform_device *pdev = to_platform_device(dev);
  673. int rc;
  674. /* Some devices have extra OF data and an OF-style MODALIAS */
  675. rc = of_device_uevent_modalias(dev, env);
  676. if (rc != -ENODEV)
  677. return rc;
  678. rc = acpi_device_uevent_modalias(dev, env);
  679. if (rc != -ENODEV)
  680. return rc;
  681. add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
  682. pdev->name);
  683. return 0;
  684. }
  685. static const struct platform_device_id *platform_match_id(
  686. const struct platform_device_id *id,
  687. struct platform_device *pdev)
  688. {
  689. while (id->name[0]) {
  690. if (strcmp(pdev->name, id->name) == 0) {
  691. pdev->id_entry = id;
  692. return id;
  693. }
  694. id++;
  695. }
  696. return NULL;
  697. }
  698. /**
  699. * platform_match - bind platform device to platform driver.
  700. * @dev: device.
  701. * @drv: driver.
  702. *
  703. * Platform device IDs are assumed to be encoded like this:
  704. * "<name><instance>", where <name> is a short description of the type of
  705. * device, like "pci" or "floppy", and <instance> is the enumerated
  706. * instance of the device, like '0' or '42'. Driver IDs are simply
  707. * "<name>". So, extract the <name> from the platform_device structure,
  708. * and compare it against the name of the driver. Return whether they match
  709. * or not.
  710. */
  711. static int platform_match(struct device *dev, struct device_driver *drv)
  712. {
  713. struct platform_device *pdev = to_platform_device(dev);
  714. struct platform_driver *pdrv = to_platform_driver(drv);
  715. /* When driver_override is set, only bind to the matching driver */
  716. if (pdev->driver_override)
  717. return !strcmp(pdev->driver_override, drv->name);
  718. /* Attempt an OF style match first */
  719. if (of_driver_match_device(dev, drv))
  720. return 1;
  721. /* Then try ACPI style match */
  722. if (acpi_driver_match_device(dev, drv))
  723. return 1;
  724. /* Then try to match against the id table */
  725. if (pdrv->id_table)
  726. return platform_match_id(pdrv->id_table, pdev) != NULL;
  727. /* fall-back to driver name match */
  728. return (strcmp(pdev->name, drv->name) == 0);
  729. }
  730. #ifdef CONFIG_PM_SLEEP
  731. static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
  732. {
  733. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  734. struct platform_device *pdev = to_platform_device(dev);
  735. int ret = 0;
  736. if (dev->driver && pdrv->suspend)
  737. ret = pdrv->suspend(pdev, mesg);
  738. return ret;
  739. }
  740. static int platform_legacy_resume(struct device *dev)
  741. {
  742. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  743. struct platform_device *pdev = to_platform_device(dev);
  744. int ret = 0;
  745. if (dev->driver && pdrv->resume)
  746. ret = pdrv->resume(pdev);
  747. return ret;
  748. }
  749. #endif /* CONFIG_PM_SLEEP */
  750. #ifdef CONFIG_SUSPEND
  751. int platform_pm_suspend(struct device *dev)
  752. {
  753. struct device_driver *drv = dev->driver;
  754. int ret = 0;
  755. if (!drv)
  756. return 0;
  757. if (drv->pm) {
  758. if (drv->pm->suspend)
  759. ret = drv->pm->suspend(dev);
  760. } else {
  761. ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
  762. }
  763. return ret;
  764. }
  765. int platform_pm_resume(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->resume)
  773. ret = drv->pm->resume(dev);
  774. } else {
  775. ret = platform_legacy_resume(dev);
  776. }
  777. return ret;
  778. }
  779. #endif /* CONFIG_SUSPEND */
  780. #ifdef CONFIG_HIBERNATE_CALLBACKS
  781. int platform_pm_freeze(struct device *dev)
  782. {
  783. struct device_driver *drv = dev->driver;
  784. int ret = 0;
  785. if (!drv)
  786. return 0;
  787. if (drv->pm) {
  788. if (drv->pm->freeze)
  789. ret = drv->pm->freeze(dev);
  790. } else {
  791. ret = platform_legacy_suspend(dev, PMSG_FREEZE);
  792. }
  793. return ret;
  794. }
  795. int platform_pm_thaw(struct device *dev)
  796. {
  797. struct device_driver *drv = dev->driver;
  798. int ret = 0;
  799. if (!drv)
  800. return 0;
  801. if (drv->pm) {
  802. if (drv->pm->thaw)
  803. ret = drv->pm->thaw(dev);
  804. } else {
  805. ret = platform_legacy_resume(dev);
  806. }
  807. return ret;
  808. }
  809. int platform_pm_poweroff(struct device *dev)
  810. {
  811. struct device_driver *drv = dev->driver;
  812. int ret = 0;
  813. if (!drv)
  814. return 0;
  815. if (drv->pm) {
  816. if (drv->pm->poweroff)
  817. ret = drv->pm->poweroff(dev);
  818. } else {
  819. ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
  820. }
  821. return ret;
  822. }
  823. int platform_pm_restore(struct device *dev)
  824. {
  825. struct device_driver *drv = dev->driver;
  826. int ret = 0;
  827. if (!drv)
  828. return 0;
  829. if (drv->pm) {
  830. if (drv->pm->restore)
  831. ret = drv->pm->restore(dev);
  832. } else {
  833. ret = platform_legacy_resume(dev);
  834. }
  835. return ret;
  836. }
  837. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  838. static const struct dev_pm_ops platform_dev_pm_ops = {
  839. .runtime_suspend = pm_generic_runtime_suspend,
  840. .runtime_resume = pm_generic_runtime_resume,
  841. USE_PLATFORM_PM_SLEEP_OPS
  842. };
  843. struct bus_type platform_bus_type = {
  844. .name = "platform",
  845. .dev_groups = platform_dev_groups,
  846. .match = platform_match,
  847. .uevent = platform_uevent,
  848. .pm = &platform_dev_pm_ops,
  849. };
  850. EXPORT_SYMBOL_GPL(platform_bus_type);
  851. int __init platform_bus_init(void)
  852. {
  853. int error;
  854. early_platform_cleanup();
  855. error = device_register(&platform_bus);
  856. if (error)
  857. return error;
  858. error = bus_register(&platform_bus_type);
  859. if (error)
  860. device_unregister(&platform_bus);
  861. return error;
  862. }
  863. #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
  864. u64 dma_get_required_mask(struct device *dev)
  865. {
  866. u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
  867. u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
  868. u64 mask;
  869. if (!high_totalram) {
  870. /* convert to mask just covering totalram */
  871. low_totalram = (1 << (fls(low_totalram) - 1));
  872. low_totalram += low_totalram - 1;
  873. mask = low_totalram;
  874. } else {
  875. high_totalram = (1 << (fls(high_totalram) - 1));
  876. high_totalram += high_totalram - 1;
  877. mask = (((u64)high_totalram) << 32) + 0xffffffff;
  878. }
  879. return mask;
  880. }
  881. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  882. #endif
  883. static __initdata LIST_HEAD(early_platform_driver_list);
  884. static __initdata LIST_HEAD(early_platform_device_list);
  885. /**
  886. * early_platform_driver_register - register early platform driver
  887. * @epdrv: early_platform driver structure
  888. * @buf: string passed from early_param()
  889. *
  890. * Helper function for early_platform_init() / early_platform_init_buffer()
  891. */
  892. int __init early_platform_driver_register(struct early_platform_driver *epdrv,
  893. char *buf)
  894. {
  895. char *tmp;
  896. int n;
  897. /* Simply add the driver to the end of the global list.
  898. * Drivers will by default be put on the list in compiled-in order.
  899. */
  900. if (!epdrv->list.next) {
  901. INIT_LIST_HEAD(&epdrv->list);
  902. list_add_tail(&epdrv->list, &early_platform_driver_list);
  903. }
  904. /* If the user has specified device then make sure the driver
  905. * gets prioritized. The driver of the last device specified on
  906. * command line will be put first on the list.
  907. */
  908. n = strlen(epdrv->pdrv->driver.name);
  909. if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
  910. list_move(&epdrv->list, &early_platform_driver_list);
  911. /* Allow passing parameters after device name */
  912. if (buf[n] == '\0' || buf[n] == ',')
  913. epdrv->requested_id = -1;
  914. else {
  915. epdrv->requested_id = simple_strtoul(&buf[n + 1],
  916. &tmp, 10);
  917. if (buf[n] != '.' || (tmp == &buf[n + 1])) {
  918. epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
  919. n = 0;
  920. } else
  921. n += strcspn(&buf[n + 1], ",") + 1;
  922. }
  923. if (buf[n] == ',')
  924. n++;
  925. if (epdrv->bufsize) {
  926. memcpy(epdrv->buffer, &buf[n],
  927. min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
  928. epdrv->buffer[epdrv->bufsize - 1] = '\0';
  929. }
  930. }
  931. return 0;
  932. }
  933. /**
  934. * early_platform_add_devices - adds a number of early platform devices
  935. * @devs: array of early platform devices to add
  936. * @num: number of early platform devices in array
  937. *
  938. * Used by early architecture code to register early platform devices and
  939. * their platform data.
  940. */
  941. void __init early_platform_add_devices(struct platform_device **devs, int num)
  942. {
  943. struct device *dev;
  944. int i;
  945. /* simply add the devices to list */
  946. for (i = 0; i < num; i++) {
  947. dev = &devs[i]->dev;
  948. if (!dev->devres_head.next) {
  949. pm_runtime_early_init(dev);
  950. INIT_LIST_HEAD(&dev->devres_head);
  951. list_add_tail(&dev->devres_head,
  952. &early_platform_device_list);
  953. }
  954. }
  955. }
  956. /**
  957. * early_platform_driver_register_all - register early platform drivers
  958. * @class_str: string to identify early platform driver class
  959. *
  960. * Used by architecture code to register all early platform drivers
  961. * for a certain class. If omitted then only early platform drivers
  962. * with matching kernel command line class parameters will be registered.
  963. */
  964. void __init early_platform_driver_register_all(char *class_str)
  965. {
  966. /* The "class_str" parameter may or may not be present on the kernel
  967. * command line. If it is present then there may be more than one
  968. * matching parameter.
  969. *
  970. * Since we register our early platform drivers using early_param()
  971. * we need to make sure that they also get registered in the case
  972. * when the parameter is missing from the kernel command line.
  973. *
  974. * We use parse_early_options() to make sure the early_param() gets
  975. * called at least once. The early_param() may be called more than
  976. * once since the name of the preferred device may be specified on
  977. * the kernel command line. early_platform_driver_register() handles
  978. * this case for us.
  979. */
  980. parse_early_options(class_str);
  981. }
  982. /**
  983. * early_platform_match - find early platform device matching driver
  984. * @epdrv: early platform driver structure
  985. * @id: id to match against
  986. */
  987. static struct platform_device * __init
  988. early_platform_match(struct early_platform_driver *epdrv, 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 pd;
  995. return NULL;
  996. }
  997. /**
  998. * early_platform_left - check if early platform driver has matching devices
  999. * @epdrv: early platform driver structure
  1000. * @id: return true if id or above exists
  1001. */
  1002. static int __init early_platform_left(struct early_platform_driver *epdrv,
  1003. int id)
  1004. {
  1005. struct platform_device *pd;
  1006. list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
  1007. if (platform_match(&pd->dev, &epdrv->pdrv->driver))
  1008. if (pd->id >= id)
  1009. return 1;
  1010. return 0;
  1011. }
  1012. /**
  1013. * early_platform_driver_probe_id - probe drivers matching class_str and id
  1014. * @class_str: string to identify early platform driver class
  1015. * @id: id to match against
  1016. * @nr_probe: number of platform devices to successfully probe before exiting
  1017. */
  1018. static int __init early_platform_driver_probe_id(char *class_str,
  1019. int id,
  1020. int nr_probe)
  1021. {
  1022. struct early_platform_driver *epdrv;
  1023. struct platform_device *match;
  1024. int match_id;
  1025. int n = 0;
  1026. int left = 0;
  1027. list_for_each_entry(epdrv, &early_platform_driver_list, list) {
  1028. /* only use drivers matching our class_str */
  1029. if (strcmp(class_str, epdrv->class_str))
  1030. continue;
  1031. if (id == -2) {
  1032. match_id = epdrv->requested_id;
  1033. left = 1;
  1034. } else {
  1035. match_id = id;
  1036. left += early_platform_left(epdrv, id);
  1037. /* skip requested id */
  1038. switch (epdrv->requested_id) {
  1039. case EARLY_PLATFORM_ID_ERROR:
  1040. case EARLY_PLATFORM_ID_UNSET:
  1041. break;
  1042. default:
  1043. if (epdrv->requested_id == id)
  1044. match_id = EARLY_PLATFORM_ID_UNSET;
  1045. }
  1046. }
  1047. switch (match_id) {
  1048. case EARLY_PLATFORM_ID_ERROR:
  1049. pr_warn("%s: unable to parse %s parameter\n",
  1050. class_str, epdrv->pdrv->driver.name);
  1051. /* fall-through */
  1052. case EARLY_PLATFORM_ID_UNSET:
  1053. match = NULL;
  1054. break;
  1055. default:
  1056. match = early_platform_match(epdrv, match_id);
  1057. }
  1058. if (match) {
  1059. /*
  1060. * Set up a sensible init_name to enable
  1061. * dev_name() and others to be used before the
  1062. * rest of the driver core is initialized.
  1063. */
  1064. if (!match->dev.init_name && slab_is_available()) {
  1065. if (match->id != -1)
  1066. match->dev.init_name =
  1067. kasprintf(GFP_KERNEL, "%s.%d",
  1068. match->name,
  1069. match->id);
  1070. else
  1071. match->dev.init_name =
  1072. kasprintf(GFP_KERNEL, "%s",
  1073. match->name);
  1074. if (!match->dev.init_name)
  1075. return -ENOMEM;
  1076. }
  1077. if (epdrv->pdrv->probe(match))
  1078. pr_warn("%s: unable to probe %s early.\n",
  1079. class_str, match->name);
  1080. else
  1081. n++;
  1082. }
  1083. if (n >= nr_probe)
  1084. break;
  1085. }
  1086. if (left)
  1087. return n;
  1088. else
  1089. return -ENODEV;
  1090. }
  1091. /**
  1092. * early_platform_driver_probe - probe a class of registered drivers
  1093. * @class_str: string to identify early platform driver class
  1094. * @nr_probe: number of platform devices to successfully probe before exiting
  1095. * @user_only: only probe user specified early platform devices
  1096. *
  1097. * Used by architecture code to probe registered early platform drivers
  1098. * within a certain class. For probe to happen a registered early platform
  1099. * device matching a registered early platform driver is needed.
  1100. */
  1101. int __init early_platform_driver_probe(char *class_str,
  1102. int nr_probe,
  1103. int user_only)
  1104. {
  1105. int k, n, i;
  1106. n = 0;
  1107. for (i = -2; n < nr_probe; i++) {
  1108. k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
  1109. if (k < 0)
  1110. break;
  1111. n += k;
  1112. if (user_only)
  1113. break;
  1114. }
  1115. return n;
  1116. }
  1117. /**
  1118. * early_platform_cleanup - clean up early platform code
  1119. */
  1120. void __init early_platform_cleanup(void)
  1121. {
  1122. struct platform_device *pd, *pd2;
  1123. /* clean up the devres list used to chain devices */
  1124. list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
  1125. dev.devres_head) {
  1126. list_del(&pd->dev.devres_head);
  1127. memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
  1128. }
  1129. }