platform.c 31 KB

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