platform.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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
  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
  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
  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
  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
  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
  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. static int platform_drv_probe(struct device *_dev)
  384. {
  385. struct platform_driver *drv = to_platform_driver(_dev->driver);
  386. struct platform_device *dev = to_platform_device(_dev);
  387. return drv->probe(dev);
  388. }
  389. static int platform_drv_probe_fail(struct device *_dev)
  390. {
  391. return -ENXIO;
  392. }
  393. static int platform_drv_remove(struct device *_dev)
  394. {
  395. struct platform_driver *drv = to_platform_driver(_dev->driver);
  396. struct platform_device *dev = to_platform_device(_dev);
  397. return drv->remove(dev);
  398. }
  399. static void platform_drv_shutdown(struct device *_dev)
  400. {
  401. struct platform_driver *drv = to_platform_driver(_dev->driver);
  402. struct platform_device *dev = to_platform_device(_dev);
  403. drv->shutdown(dev);
  404. }
  405. /**
  406. * platform_driver_register
  407. * @drv: platform driver structure
  408. */
  409. int platform_driver_register(struct platform_driver *drv)
  410. {
  411. drv->driver.bus = &platform_bus_type;
  412. if (drv->probe)
  413. drv->driver.probe = platform_drv_probe;
  414. if (drv->remove)
  415. drv->driver.remove = platform_drv_remove;
  416. if (drv->shutdown)
  417. drv->driver.shutdown = platform_drv_shutdown;
  418. return driver_register(&drv->driver);
  419. }
  420. EXPORT_SYMBOL_GPL(platform_driver_register);
  421. /**
  422. * platform_driver_unregister
  423. * @drv: platform driver structure
  424. */
  425. void platform_driver_unregister(struct platform_driver *drv)
  426. {
  427. driver_unregister(&drv->driver);
  428. }
  429. EXPORT_SYMBOL_GPL(platform_driver_unregister);
  430. /**
  431. * platform_driver_probe - register driver for non-hotpluggable device
  432. * @drv: platform driver structure
  433. * @probe: the driver probe routine, probably from an __init section
  434. *
  435. * Use this instead of platform_driver_register() when you know the device
  436. * is not hotpluggable and has already been registered, and you want to
  437. * remove its run-once probe() infrastructure from memory after the driver
  438. * has bound to the device.
  439. *
  440. * One typical use for this would be with drivers for controllers integrated
  441. * into system-on-chip processors, where the controller devices have been
  442. * configured as part of board setup.
  443. *
  444. * Returns zero if the driver registered and bound to a device, else returns
  445. * a negative error code and with the driver not registered.
  446. */
  447. int __init_or_module platform_driver_probe(struct platform_driver *drv,
  448. int (*probe)(struct platform_device *))
  449. {
  450. int retval, code;
  451. /* make sure driver won't have bind/unbind attributes */
  452. drv->driver.suppress_bind_attrs = true;
  453. /* temporary section violation during probe() */
  454. drv->probe = probe;
  455. retval = code = platform_driver_register(drv);
  456. /*
  457. * Fixup that section violation, being paranoid about code scanning
  458. * the list of drivers in order to probe new devices. Check to see
  459. * if the probe was successful, and make sure any forced probes of
  460. * new devices fail.
  461. */
  462. spin_lock(&platform_bus_type.p->klist_drivers.k_lock);
  463. drv->probe = NULL;
  464. if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
  465. retval = -ENODEV;
  466. drv->driver.probe = platform_drv_probe_fail;
  467. spin_unlock(&platform_bus_type.p->klist_drivers.k_lock);
  468. if (code != retval)
  469. platform_driver_unregister(drv);
  470. return retval;
  471. }
  472. EXPORT_SYMBOL_GPL(platform_driver_probe);
  473. /* modalias support enables more hands-off userspace setup:
  474. * (a) environment variable lets new-style hotplug events work once system is
  475. * fully running: "modprobe $MODALIAS"
  476. * (b) sysfs attribute lets new-style coldplug recover from hotplug events
  477. * mishandled before system is fully running: "modprobe $(cat modalias)"
  478. */
  479. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  480. char *buf)
  481. {
  482. struct platform_device *pdev = to_platform_device(dev);
  483. int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
  484. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  485. }
  486. static struct device_attribute platform_dev_attrs[] = {
  487. __ATTR_RO(modalias),
  488. __ATTR_NULL,
  489. };
  490. static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
  491. {
  492. struct platform_device *pdev = to_platform_device(dev);
  493. add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
  494. (pdev->id_entry) ? pdev->id_entry->name : pdev->name);
  495. return 0;
  496. }
  497. static const struct platform_device_id *platform_match_id(
  498. struct platform_device_id *id,
  499. struct platform_device *pdev)
  500. {
  501. while (id->name[0]) {
  502. if (strcmp(pdev->name, id->name) == 0) {
  503. pdev->id_entry = id;
  504. return id;
  505. }
  506. id++;
  507. }
  508. return NULL;
  509. }
  510. /**
  511. * platform_match - bind platform device to platform driver.
  512. * @dev: device.
  513. * @drv: driver.
  514. *
  515. * Platform device IDs are assumed to be encoded like this:
  516. * "<name><instance>", where <name> is a short description of the type of
  517. * device, like "pci" or "floppy", and <instance> is the enumerated
  518. * instance of the device, like '0' or '42'. Driver IDs are simply
  519. * "<name>". So, extract the <name> from the platform_device structure,
  520. * and compare it against the name of the driver. Return whether they match
  521. * or not.
  522. */
  523. static int platform_match(struct device *dev, struct device_driver *drv)
  524. {
  525. struct platform_device *pdev = to_platform_device(dev);
  526. struct platform_driver *pdrv = to_platform_driver(drv);
  527. /* match against the id table first */
  528. if (pdrv->id_table)
  529. return platform_match_id(pdrv->id_table, pdev) != NULL;
  530. /* fall-back to driver name match */
  531. return (strcmp(pdev->name, drv->name) == 0);
  532. }
  533. #ifdef CONFIG_PM_SLEEP
  534. static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
  535. {
  536. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  537. struct platform_device *pdev = to_platform_device(dev);
  538. int ret = 0;
  539. if (dev->driver && pdrv->suspend)
  540. ret = pdrv->suspend(pdev, mesg);
  541. return ret;
  542. }
  543. static int platform_legacy_resume(struct device *dev)
  544. {
  545. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  546. struct platform_device *pdev = to_platform_device(dev);
  547. int ret = 0;
  548. if (dev->driver && pdrv->resume)
  549. ret = pdrv->resume(pdev);
  550. return ret;
  551. }
  552. static int platform_pm_prepare(struct device *dev)
  553. {
  554. struct device_driver *drv = dev->driver;
  555. int ret = 0;
  556. if (drv && drv->pm && drv->pm->prepare)
  557. ret = drv->pm->prepare(dev);
  558. return ret;
  559. }
  560. static void platform_pm_complete(struct device *dev)
  561. {
  562. struct device_driver *drv = dev->driver;
  563. if (drv && drv->pm && drv->pm->complete)
  564. drv->pm->complete(dev);
  565. }
  566. #else /* !CONFIG_PM_SLEEP */
  567. #define platform_pm_prepare NULL
  568. #define platform_pm_complete NULL
  569. #endif /* !CONFIG_PM_SLEEP */
  570. #ifdef CONFIG_SUSPEND
  571. static int platform_pm_suspend(struct device *dev)
  572. {
  573. struct device_driver *drv = dev->driver;
  574. int ret = 0;
  575. if (!drv)
  576. return 0;
  577. if (drv->pm) {
  578. if (drv->pm->suspend)
  579. ret = drv->pm->suspend(dev);
  580. } else {
  581. ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
  582. }
  583. return ret;
  584. }
  585. static int platform_pm_suspend_noirq(struct device *dev)
  586. {
  587. struct device_driver *drv = dev->driver;
  588. int ret = 0;
  589. if (!drv)
  590. return 0;
  591. if (drv->pm) {
  592. if (drv->pm->suspend_noirq)
  593. ret = drv->pm->suspend_noirq(dev);
  594. }
  595. return ret;
  596. }
  597. static int platform_pm_resume(struct device *dev)
  598. {
  599. struct device_driver *drv = dev->driver;
  600. int ret = 0;
  601. if (!drv)
  602. return 0;
  603. if (drv->pm) {
  604. if (drv->pm->resume)
  605. ret = drv->pm->resume(dev);
  606. } else {
  607. ret = platform_legacy_resume(dev);
  608. }
  609. return ret;
  610. }
  611. static int platform_pm_resume_noirq(struct device *dev)
  612. {
  613. struct device_driver *drv = dev->driver;
  614. int ret = 0;
  615. if (!drv)
  616. return 0;
  617. if (drv->pm) {
  618. if (drv->pm->resume_noirq)
  619. ret = drv->pm->resume_noirq(dev);
  620. }
  621. return ret;
  622. }
  623. #else /* !CONFIG_SUSPEND */
  624. #define platform_pm_suspend NULL
  625. #define platform_pm_resume NULL
  626. #define platform_pm_suspend_noirq NULL
  627. #define platform_pm_resume_noirq NULL
  628. #endif /* !CONFIG_SUSPEND */
  629. #ifdef CONFIG_HIBERNATION
  630. static int platform_pm_freeze(struct device *dev)
  631. {
  632. struct device_driver *drv = dev->driver;
  633. int ret = 0;
  634. if (!drv)
  635. return 0;
  636. if (drv->pm) {
  637. if (drv->pm->freeze)
  638. ret = drv->pm->freeze(dev);
  639. } else {
  640. ret = platform_legacy_suspend(dev, PMSG_FREEZE);
  641. }
  642. return ret;
  643. }
  644. static int platform_pm_freeze_noirq(struct device *dev)
  645. {
  646. struct device_driver *drv = dev->driver;
  647. int ret = 0;
  648. if (!drv)
  649. return 0;
  650. if (drv->pm) {
  651. if (drv->pm->freeze_noirq)
  652. ret = drv->pm->freeze_noirq(dev);
  653. }
  654. return ret;
  655. }
  656. static int platform_pm_thaw(struct device *dev)
  657. {
  658. struct device_driver *drv = dev->driver;
  659. int ret = 0;
  660. if (!drv)
  661. return 0;
  662. if (drv->pm) {
  663. if (drv->pm->thaw)
  664. ret = drv->pm->thaw(dev);
  665. } else {
  666. ret = platform_legacy_resume(dev);
  667. }
  668. return ret;
  669. }
  670. static int platform_pm_thaw_noirq(struct device *dev)
  671. {
  672. struct device_driver *drv = dev->driver;
  673. int ret = 0;
  674. if (!drv)
  675. return 0;
  676. if (drv->pm) {
  677. if (drv->pm->thaw_noirq)
  678. ret = drv->pm->thaw_noirq(dev);
  679. }
  680. return ret;
  681. }
  682. static int platform_pm_poweroff(struct device *dev)
  683. {
  684. struct device_driver *drv = dev->driver;
  685. int ret = 0;
  686. if (!drv)
  687. return 0;
  688. if (drv->pm) {
  689. if (drv->pm->poweroff)
  690. ret = drv->pm->poweroff(dev);
  691. } else {
  692. ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
  693. }
  694. return ret;
  695. }
  696. static int platform_pm_poweroff_noirq(struct device *dev)
  697. {
  698. struct device_driver *drv = dev->driver;
  699. int ret = 0;
  700. if (!drv)
  701. return 0;
  702. if (drv->pm) {
  703. if (drv->pm->poweroff_noirq)
  704. ret = drv->pm->poweroff_noirq(dev);
  705. }
  706. return ret;
  707. }
  708. static int platform_pm_restore(struct device *dev)
  709. {
  710. struct device_driver *drv = dev->driver;
  711. int ret = 0;
  712. if (!drv)
  713. return 0;
  714. if (drv->pm) {
  715. if (drv->pm->restore)
  716. ret = drv->pm->restore(dev);
  717. } else {
  718. ret = platform_legacy_resume(dev);
  719. }
  720. return ret;
  721. }
  722. static int platform_pm_restore_noirq(struct device *dev)
  723. {
  724. struct device_driver *drv = dev->driver;
  725. int ret = 0;
  726. if (!drv)
  727. return 0;
  728. if (drv->pm) {
  729. if (drv->pm->restore_noirq)
  730. ret = drv->pm->restore_noirq(dev);
  731. }
  732. return ret;
  733. }
  734. #else /* !CONFIG_HIBERNATION */
  735. #define platform_pm_freeze NULL
  736. #define platform_pm_thaw NULL
  737. #define platform_pm_poweroff NULL
  738. #define platform_pm_restore NULL
  739. #define platform_pm_freeze_noirq NULL
  740. #define platform_pm_thaw_noirq NULL
  741. #define platform_pm_poweroff_noirq NULL
  742. #define platform_pm_restore_noirq NULL
  743. #endif /* !CONFIG_HIBERNATION */
  744. #ifdef CONFIG_PM_RUNTIME
  745. int __weak platform_pm_runtime_suspend(struct device *dev)
  746. {
  747. return -ENOSYS;
  748. };
  749. int __weak platform_pm_runtime_resume(struct device *dev)
  750. {
  751. return -ENOSYS;
  752. };
  753. int __weak platform_pm_runtime_idle(struct device *dev)
  754. {
  755. return -ENOSYS;
  756. };
  757. #else /* !CONFIG_PM_RUNTIME */
  758. #define platform_pm_runtime_suspend NULL
  759. #define platform_pm_runtime_resume NULL
  760. #define platform_pm_runtime_idle NULL
  761. #endif /* !CONFIG_PM_RUNTIME */
  762. static const struct dev_pm_ops platform_dev_pm_ops = {
  763. .prepare = platform_pm_prepare,
  764. .complete = platform_pm_complete,
  765. .suspend = platform_pm_suspend,
  766. .resume = platform_pm_resume,
  767. .freeze = platform_pm_freeze,
  768. .thaw = platform_pm_thaw,
  769. .poweroff = platform_pm_poweroff,
  770. .restore = platform_pm_restore,
  771. .suspend_noirq = platform_pm_suspend_noirq,
  772. .resume_noirq = platform_pm_resume_noirq,
  773. .freeze_noirq = platform_pm_freeze_noirq,
  774. .thaw_noirq = platform_pm_thaw_noirq,
  775. .poweroff_noirq = platform_pm_poweroff_noirq,
  776. .restore_noirq = platform_pm_restore_noirq,
  777. .runtime_suspend = platform_pm_runtime_suspend,
  778. .runtime_resume = platform_pm_runtime_resume,
  779. .runtime_idle = platform_pm_runtime_idle,
  780. };
  781. struct bus_type platform_bus_type = {
  782. .name = "platform",
  783. .dev_attrs = platform_dev_attrs,
  784. .match = platform_match,
  785. .uevent = platform_uevent,
  786. .pm = &platform_dev_pm_ops,
  787. };
  788. EXPORT_SYMBOL_GPL(platform_bus_type);
  789. int __init platform_bus_init(void)
  790. {
  791. int error;
  792. early_platform_cleanup();
  793. error = device_register(&platform_bus);
  794. if (error)
  795. return error;
  796. error = bus_register(&platform_bus_type);
  797. if (error)
  798. device_unregister(&platform_bus);
  799. return error;
  800. }
  801. #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
  802. u64 dma_get_required_mask(struct device *dev)
  803. {
  804. u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
  805. u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
  806. u64 mask;
  807. if (!high_totalram) {
  808. /* convert to mask just covering totalram */
  809. low_totalram = (1 << (fls(low_totalram) - 1));
  810. low_totalram += low_totalram - 1;
  811. mask = low_totalram;
  812. } else {
  813. high_totalram = (1 << (fls(high_totalram) - 1));
  814. high_totalram += high_totalram - 1;
  815. mask = (((u64)high_totalram) << 32) + 0xffffffff;
  816. }
  817. return mask;
  818. }
  819. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  820. #endif
  821. static __initdata LIST_HEAD(early_platform_driver_list);
  822. static __initdata LIST_HEAD(early_platform_device_list);
  823. /**
  824. * early_platform_driver_register
  825. * @epdrv: early_platform driver structure
  826. * @buf: string passed from early_param()
  827. */
  828. int __init early_platform_driver_register(struct early_platform_driver *epdrv,
  829. char *buf)
  830. {
  831. char *tmp;
  832. int n;
  833. /* Simply add the driver to the end of the global list.
  834. * Drivers will by default be put on the list in compiled-in order.
  835. */
  836. if (!epdrv->list.next) {
  837. INIT_LIST_HEAD(&epdrv->list);
  838. list_add_tail(&epdrv->list, &early_platform_driver_list);
  839. }
  840. /* If the user has specified device then make sure the driver
  841. * gets prioritized. The driver of the last device specified on
  842. * command line will be put first on the list.
  843. */
  844. n = strlen(epdrv->pdrv->driver.name);
  845. if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
  846. list_move(&epdrv->list, &early_platform_driver_list);
  847. /* Allow passing parameters after device name */
  848. if (buf[n] == '\0' || buf[n] == ',')
  849. epdrv->requested_id = -1;
  850. else {
  851. epdrv->requested_id = simple_strtoul(&buf[n + 1],
  852. &tmp, 10);
  853. if (buf[n] != '.' || (tmp == &buf[n + 1])) {
  854. epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
  855. n = 0;
  856. } else
  857. n += strcspn(&buf[n + 1], ",") + 1;
  858. }
  859. if (buf[n] == ',')
  860. n++;
  861. if (epdrv->bufsize) {
  862. memcpy(epdrv->buffer, &buf[n],
  863. min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
  864. epdrv->buffer[epdrv->bufsize - 1] = '\0';
  865. }
  866. }
  867. return 0;
  868. }
  869. /**
  870. * early_platform_add_devices - add a numbers of early platform devices
  871. * @devs: array of early platform devices to add
  872. * @num: number of early platform devices in array
  873. */
  874. void __init early_platform_add_devices(struct platform_device **devs, int num)
  875. {
  876. struct device *dev;
  877. int i;
  878. /* simply add the devices to list */
  879. for (i = 0; i < num; i++) {
  880. dev = &devs[i]->dev;
  881. if (!dev->devres_head.next) {
  882. INIT_LIST_HEAD(&dev->devres_head);
  883. list_add_tail(&dev->devres_head,
  884. &early_platform_device_list);
  885. }
  886. }
  887. }
  888. /**
  889. * early_platform_driver_register_all
  890. * @class_str: string to identify early platform driver class
  891. */
  892. void __init early_platform_driver_register_all(char *class_str)
  893. {
  894. /* The "class_str" parameter may or may not be present on the kernel
  895. * command line. If it is present then there may be more than one
  896. * matching parameter.
  897. *
  898. * Since we register our early platform drivers using early_param()
  899. * we need to make sure that they also get registered in the case
  900. * when the parameter is missing from the kernel command line.
  901. *
  902. * We use parse_early_options() to make sure the early_param() gets
  903. * called at least once. The early_param() may be called more than
  904. * once since the name of the preferred device may be specified on
  905. * the kernel command line. early_platform_driver_register() handles
  906. * this case for us.
  907. */
  908. parse_early_options(class_str);
  909. }
  910. /**
  911. * early_platform_match
  912. * @epdrv: early platform driver structure
  913. * @id: id to match against
  914. */
  915. static __init struct platform_device *
  916. early_platform_match(struct early_platform_driver *epdrv, int id)
  917. {
  918. struct platform_device *pd;
  919. list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
  920. if (platform_match(&pd->dev, &epdrv->pdrv->driver))
  921. if (pd->id == id)
  922. return pd;
  923. return NULL;
  924. }
  925. /**
  926. * early_platform_left
  927. * @epdrv: early platform driver structure
  928. * @id: return true if id or above exists
  929. */
  930. static __init int early_platform_left(struct early_platform_driver *epdrv,
  931. int id)
  932. {
  933. struct platform_device *pd;
  934. list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
  935. if (platform_match(&pd->dev, &epdrv->pdrv->driver))
  936. if (pd->id >= id)
  937. return 1;
  938. return 0;
  939. }
  940. /**
  941. * early_platform_driver_probe_id
  942. * @class_str: string to identify early platform driver class
  943. * @id: id to match against
  944. * @nr_probe: number of platform devices to successfully probe before exiting
  945. */
  946. static int __init early_platform_driver_probe_id(char *class_str,
  947. int id,
  948. int nr_probe)
  949. {
  950. struct early_platform_driver *epdrv;
  951. struct platform_device *match;
  952. int match_id;
  953. int n = 0;
  954. int left = 0;
  955. list_for_each_entry(epdrv, &early_platform_driver_list, list) {
  956. /* only use drivers matching our class_str */
  957. if (strcmp(class_str, epdrv->class_str))
  958. continue;
  959. if (id == -2) {
  960. match_id = epdrv->requested_id;
  961. left = 1;
  962. } else {
  963. match_id = id;
  964. left += early_platform_left(epdrv, id);
  965. /* skip requested id */
  966. switch (epdrv->requested_id) {
  967. case EARLY_PLATFORM_ID_ERROR:
  968. case EARLY_PLATFORM_ID_UNSET:
  969. break;
  970. default:
  971. if (epdrv->requested_id == id)
  972. match_id = EARLY_PLATFORM_ID_UNSET;
  973. }
  974. }
  975. switch (match_id) {
  976. case EARLY_PLATFORM_ID_ERROR:
  977. pr_warning("%s: unable to parse %s parameter\n",
  978. class_str, epdrv->pdrv->driver.name);
  979. /* fall-through */
  980. case EARLY_PLATFORM_ID_UNSET:
  981. match = NULL;
  982. break;
  983. default:
  984. match = early_platform_match(epdrv, match_id);
  985. }
  986. if (match) {
  987. if (epdrv->pdrv->probe(match))
  988. pr_warning("%s: unable to probe %s early.\n",
  989. class_str, match->name);
  990. else
  991. n++;
  992. }
  993. if (n >= nr_probe)
  994. break;
  995. }
  996. if (left)
  997. return n;
  998. else
  999. return -ENODEV;
  1000. }
  1001. /**
  1002. * early_platform_driver_probe
  1003. * @class_str: string to identify early platform driver class
  1004. * @nr_probe: number of platform devices to successfully probe before exiting
  1005. * @user_only: only probe user specified early platform devices
  1006. */
  1007. int __init early_platform_driver_probe(char *class_str,
  1008. int nr_probe,
  1009. int user_only)
  1010. {
  1011. int k, n, i;
  1012. n = 0;
  1013. for (i = -2; n < nr_probe; i++) {
  1014. k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
  1015. if (k < 0)
  1016. break;
  1017. n += k;
  1018. if (user_only)
  1019. break;
  1020. }
  1021. return n;
  1022. }
  1023. /**
  1024. * early_platform_cleanup - clean up early platform code
  1025. */
  1026. void __init early_platform_cleanup(void)
  1027. {
  1028. struct platform_device *pd, *pd2;
  1029. /* clean up the devres list used to chain devices */
  1030. list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
  1031. dev.devres_head) {
  1032. list_del(&pd->dev.devres_head);
  1033. memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
  1034. }
  1035. }