platform.c 29 KB

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