platform.c 30 KB

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