platform.c 30 KB

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