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