platform.c 29 KB

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