platform.c 32 KB

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