platform.c 31 KB

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