platform.c 32 KB

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