platform.c 29 KB

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