platform.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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. * @pdev: 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. if (r->parent)
  272. release_resource(r);
  273. }
  274. return ret;
  275. }
  276. EXPORT_SYMBOL_GPL(platform_device_add);
  277. /**
  278. * platform_device_del - remove a platform-level device
  279. * @pdev: platform device we're removing
  280. *
  281. * Note that this function will also release all memory- and port-based
  282. * resources owned by the device (@dev->resource). This function must
  283. * _only_ be externally called in error cases. All other usage is a bug.
  284. */
  285. void platform_device_del(struct platform_device *pdev)
  286. {
  287. int i;
  288. if (pdev) {
  289. device_del(&pdev->dev);
  290. for (i = 0; i < pdev->num_resources; i++) {
  291. struct resource *r = &pdev->resource[i];
  292. if (r->parent)
  293. release_resource(r);
  294. }
  295. }
  296. }
  297. EXPORT_SYMBOL_GPL(platform_device_del);
  298. /**
  299. * platform_device_register - add a platform-level device
  300. * @pdev: platform device we're adding
  301. */
  302. int platform_device_register(struct platform_device *pdev)
  303. {
  304. device_initialize(&pdev->dev);
  305. arch_setup_pdev_archdata(pdev);
  306. return platform_device_add(pdev);
  307. }
  308. EXPORT_SYMBOL_GPL(platform_device_register);
  309. /**
  310. * platform_device_unregister - unregister a platform-level device
  311. * @pdev: platform device we're unregistering
  312. *
  313. * Unregistration is done in 2 steps. First we release all resources
  314. * and remove it from the subsystem, then we drop reference count by
  315. * calling platform_device_put().
  316. */
  317. void platform_device_unregister(struct platform_device *pdev)
  318. {
  319. platform_device_del(pdev);
  320. platform_device_put(pdev);
  321. }
  322. EXPORT_SYMBOL_GPL(platform_device_unregister);
  323. /**
  324. * platform_device_register_full - add a platform-level device with
  325. * resources and platform-specific data
  326. *
  327. * @pdevinfo: data used to create device
  328. *
  329. * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
  330. */
  331. struct platform_device *platform_device_register_full(
  332. const struct platform_device_info *pdevinfo)
  333. {
  334. int ret = -ENOMEM;
  335. struct platform_device *pdev;
  336. pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
  337. if (!pdev)
  338. goto err_alloc;
  339. pdev->dev.parent = pdevinfo->parent;
  340. if (pdevinfo->dma_mask) {
  341. /*
  342. * This memory isn't freed when the device is put,
  343. * I don't have a nice idea for that though. Conceptually
  344. * dma_mask in struct device should not be a pointer.
  345. * See http://thread.gmane.org/gmane.linux.kernel.pci/9081
  346. */
  347. pdev->dev.dma_mask =
  348. kmalloc(sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
  349. if (!pdev->dev.dma_mask)
  350. goto err;
  351. *pdev->dev.dma_mask = pdevinfo->dma_mask;
  352. pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
  353. }
  354. ret = platform_device_add_resources(pdev,
  355. pdevinfo->res, pdevinfo->num_res);
  356. if (ret)
  357. goto err;
  358. ret = platform_device_add_data(pdev,
  359. pdevinfo->data, pdevinfo->size_data);
  360. if (ret)
  361. goto err;
  362. ret = platform_device_add(pdev);
  363. if (ret) {
  364. err:
  365. kfree(pdev->dev.dma_mask);
  366. err_alloc:
  367. platform_device_put(pdev);
  368. return ERR_PTR(ret);
  369. }
  370. return pdev;
  371. }
  372. EXPORT_SYMBOL_GPL(platform_device_register_full);
  373. static int platform_drv_probe(struct device *_dev)
  374. {
  375. struct platform_driver *drv = to_platform_driver(_dev->driver);
  376. struct platform_device *dev = to_platform_device(_dev);
  377. return drv->probe(dev);
  378. }
  379. static int platform_drv_probe_fail(struct device *_dev)
  380. {
  381. return -ENXIO;
  382. }
  383. static int platform_drv_remove(struct device *_dev)
  384. {
  385. struct platform_driver *drv = to_platform_driver(_dev->driver);
  386. struct platform_device *dev = to_platform_device(_dev);
  387. return drv->remove(dev);
  388. }
  389. static void platform_drv_shutdown(struct device *_dev)
  390. {
  391. struct platform_driver *drv = to_platform_driver(_dev->driver);
  392. struct platform_device *dev = to_platform_device(_dev);
  393. drv->shutdown(dev);
  394. }
  395. /**
  396. * platform_driver_register - register a driver for platform-level devices
  397. * @drv: platform driver structure
  398. */
  399. int platform_driver_register(struct platform_driver *drv)
  400. {
  401. drv->driver.bus = &platform_bus_type;
  402. if (drv->probe)
  403. drv->driver.probe = platform_drv_probe;
  404. if (drv->remove)
  405. drv->driver.remove = platform_drv_remove;
  406. if (drv->shutdown)
  407. drv->driver.shutdown = platform_drv_shutdown;
  408. return driver_register(&drv->driver);
  409. }
  410. EXPORT_SYMBOL_GPL(platform_driver_register);
  411. /**
  412. * platform_driver_unregister - unregister a driver for platform-level devices
  413. * @drv: platform driver structure
  414. */
  415. void platform_driver_unregister(struct platform_driver *drv)
  416. {
  417. driver_unregister(&drv->driver);
  418. }
  419. EXPORT_SYMBOL_GPL(platform_driver_unregister);
  420. /**
  421. * platform_driver_probe - register driver for non-hotpluggable device
  422. * @drv: platform driver structure
  423. * @probe: the driver probe routine, probably from an __init section
  424. *
  425. * Use this instead of platform_driver_register() when you know the device
  426. * is not hotpluggable and has already been registered, and you want to
  427. * remove its run-once probe() infrastructure from memory after the driver
  428. * has bound to the device.
  429. *
  430. * One typical use for this would be with drivers for controllers integrated
  431. * into system-on-chip processors, where the controller devices have been
  432. * configured as part of board setup.
  433. *
  434. * Returns zero if the driver registered and bound to a device, else returns
  435. * a negative error code and with the driver not registered.
  436. */
  437. int __init_or_module platform_driver_probe(struct platform_driver *drv,
  438. int (*probe)(struct platform_device *))
  439. {
  440. int retval, code;
  441. /* make sure driver won't have bind/unbind attributes */
  442. drv->driver.suppress_bind_attrs = true;
  443. /* temporary section violation during probe() */
  444. drv->probe = probe;
  445. retval = code = platform_driver_register(drv);
  446. /*
  447. * Fixup that section violation, being paranoid about code scanning
  448. * the list of drivers in order to probe new devices. Check to see
  449. * if the probe was successful, and make sure any forced probes of
  450. * new devices fail.
  451. */
  452. spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
  453. drv->probe = NULL;
  454. if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
  455. retval = -ENODEV;
  456. drv->driver.probe = platform_drv_probe_fail;
  457. spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
  458. if (code != retval)
  459. platform_driver_unregister(drv);
  460. return retval;
  461. }
  462. EXPORT_SYMBOL_GPL(platform_driver_probe);
  463. /**
  464. * platform_create_bundle - register driver and create corresponding device
  465. * @driver: platform driver structure
  466. * @probe: the driver probe routine, probably from an __init section
  467. * @res: set of resources that needs to be allocated for the device
  468. * @n_res: number of resources
  469. * @data: platform specific data for this platform device
  470. * @size: size of platform specific data
  471. *
  472. * Use this in legacy-style modules that probe hardware directly and
  473. * register a single platform device and corresponding platform driver.
  474. *
  475. * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
  476. */
  477. struct platform_device * __init_or_module platform_create_bundle(
  478. struct platform_driver *driver,
  479. int (*probe)(struct platform_device *),
  480. struct resource *res, unsigned int n_res,
  481. const void *data, size_t size)
  482. {
  483. struct platform_device *pdev;
  484. int error;
  485. pdev = platform_device_alloc(driver->driver.name, -1);
  486. if (!pdev) {
  487. error = -ENOMEM;
  488. goto err_out;
  489. }
  490. error = platform_device_add_resources(pdev, res, n_res);
  491. if (error)
  492. goto err_pdev_put;
  493. error = platform_device_add_data(pdev, data, size);
  494. if (error)
  495. goto err_pdev_put;
  496. error = platform_device_add(pdev);
  497. if (error)
  498. goto err_pdev_put;
  499. error = platform_driver_probe(driver, probe);
  500. if (error)
  501. goto err_pdev_del;
  502. return pdev;
  503. err_pdev_del:
  504. platform_device_del(pdev);
  505. err_pdev_put:
  506. platform_device_put(pdev);
  507. err_out:
  508. return ERR_PTR(error);
  509. }
  510. EXPORT_SYMBOL_GPL(platform_create_bundle);
  511. /* modalias support enables more hands-off userspace setup:
  512. * (a) environment variable lets new-style hotplug events work once system is
  513. * fully running: "modprobe $MODALIAS"
  514. * (b) sysfs attribute lets new-style coldplug recover from hotplug events
  515. * mishandled before system is fully running: "modprobe $(cat modalias)"
  516. */
  517. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  518. char *buf)
  519. {
  520. struct platform_device *pdev = to_platform_device(dev);
  521. int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
  522. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  523. }
  524. static struct device_attribute platform_dev_attrs[] = {
  525. __ATTR_RO(modalias),
  526. __ATTR_NULL,
  527. };
  528. static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
  529. {
  530. struct platform_device *pdev = to_platform_device(dev);
  531. int rc;
  532. /* Some devices have extra OF data and an OF-style MODALIAS */
  533. rc = of_device_uevent_modalias(dev,env);
  534. if (rc != -ENODEV)
  535. return rc;
  536. add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
  537. pdev->name);
  538. return 0;
  539. }
  540. static const struct platform_device_id *platform_match_id(
  541. const struct platform_device_id *id,
  542. struct platform_device *pdev)
  543. {
  544. while (id->name[0]) {
  545. if (strcmp(pdev->name, id->name) == 0) {
  546. pdev->id_entry = id;
  547. return id;
  548. }
  549. id++;
  550. }
  551. return NULL;
  552. }
  553. /**
  554. * platform_match - bind platform device to platform driver.
  555. * @dev: device.
  556. * @drv: driver.
  557. *
  558. * Platform device IDs are assumed to be encoded like this:
  559. * "<name><instance>", where <name> is a short description of the type of
  560. * device, like "pci" or "floppy", and <instance> is the enumerated
  561. * instance of the device, like '0' or '42'. Driver IDs are simply
  562. * "<name>". So, extract the <name> from the platform_device structure,
  563. * and compare it against the name of the driver. Return whether they match
  564. * or not.
  565. */
  566. static int platform_match(struct device *dev, struct device_driver *drv)
  567. {
  568. struct platform_device *pdev = to_platform_device(dev);
  569. struct platform_driver *pdrv = to_platform_driver(drv);
  570. /* Attempt an OF style match first */
  571. if (of_driver_match_device(dev, drv))
  572. return 1;
  573. /* Then try to match against the id table */
  574. if (pdrv->id_table)
  575. return platform_match_id(pdrv->id_table, pdev) != NULL;
  576. /* fall-back to driver name match */
  577. return (strcmp(pdev->name, drv->name) == 0);
  578. }
  579. #ifdef CONFIG_PM_SLEEP
  580. static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
  581. {
  582. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  583. struct platform_device *pdev = to_platform_device(dev);
  584. int ret = 0;
  585. if (dev->driver && pdrv->suspend)
  586. ret = pdrv->suspend(pdev, mesg);
  587. return ret;
  588. }
  589. static int platform_legacy_resume(struct device *dev)
  590. {
  591. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  592. struct platform_device *pdev = to_platform_device(dev);
  593. int ret = 0;
  594. if (dev->driver && pdrv->resume)
  595. ret = pdrv->resume(pdev);
  596. return ret;
  597. }
  598. #endif /* CONFIG_PM_SLEEP */
  599. #ifdef CONFIG_SUSPEND
  600. int platform_pm_suspend(struct device *dev)
  601. {
  602. struct device_driver *drv = dev->driver;
  603. int ret = 0;
  604. if (!drv)
  605. return 0;
  606. if (drv->pm) {
  607. if (drv->pm->suspend)
  608. ret = drv->pm->suspend(dev);
  609. } else {
  610. ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
  611. }
  612. return ret;
  613. }
  614. int platform_pm_resume(struct device *dev)
  615. {
  616. struct device_driver *drv = dev->driver;
  617. int ret = 0;
  618. if (!drv)
  619. return 0;
  620. if (drv->pm) {
  621. if (drv->pm->resume)
  622. ret = drv->pm->resume(dev);
  623. } else {
  624. ret = platform_legacy_resume(dev);
  625. }
  626. return ret;
  627. }
  628. #endif /* CONFIG_SUSPEND */
  629. #ifdef CONFIG_HIBERNATE_CALLBACKS
  630. int platform_pm_freeze(struct device *dev)
  631. {
  632. struct device_driver *drv = dev->driver;
  633. int ret = 0;
  634. if (!drv)
  635. return 0;
  636. if (drv->pm) {
  637. if (drv->pm->freeze)
  638. ret = drv->pm->freeze(dev);
  639. } else {
  640. ret = platform_legacy_suspend(dev, PMSG_FREEZE);
  641. }
  642. return ret;
  643. }
  644. int platform_pm_thaw(struct device *dev)
  645. {
  646. struct device_driver *drv = dev->driver;
  647. int ret = 0;
  648. if (!drv)
  649. return 0;
  650. if (drv->pm) {
  651. if (drv->pm->thaw)
  652. ret = drv->pm->thaw(dev);
  653. } else {
  654. ret = platform_legacy_resume(dev);
  655. }
  656. return ret;
  657. }
  658. int platform_pm_poweroff(struct device *dev)
  659. {
  660. struct device_driver *drv = dev->driver;
  661. int ret = 0;
  662. if (!drv)
  663. return 0;
  664. if (drv->pm) {
  665. if (drv->pm->poweroff)
  666. ret = drv->pm->poweroff(dev);
  667. } else {
  668. ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
  669. }
  670. return ret;
  671. }
  672. int platform_pm_restore(struct device *dev)
  673. {
  674. struct device_driver *drv = dev->driver;
  675. int ret = 0;
  676. if (!drv)
  677. return 0;
  678. if (drv->pm) {
  679. if (drv->pm->restore)
  680. ret = drv->pm->restore(dev);
  681. } else {
  682. ret = platform_legacy_resume(dev);
  683. }
  684. return ret;
  685. }
  686. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  687. static const struct dev_pm_ops platform_dev_pm_ops = {
  688. .runtime_suspend = pm_generic_runtime_suspend,
  689. .runtime_resume = pm_generic_runtime_resume,
  690. .runtime_idle = pm_generic_runtime_idle,
  691. USE_PLATFORM_PM_SLEEP_OPS
  692. };
  693. struct bus_type platform_bus_type = {
  694. .name = "platform",
  695. .dev_attrs = platform_dev_attrs,
  696. .match = platform_match,
  697. .uevent = platform_uevent,
  698. .pm = &platform_dev_pm_ops,
  699. };
  700. EXPORT_SYMBOL_GPL(platform_bus_type);
  701. int __init platform_bus_init(void)
  702. {
  703. int error;
  704. early_platform_cleanup();
  705. error = device_register(&platform_bus);
  706. if (error)
  707. return error;
  708. error = bus_register(&platform_bus_type);
  709. if (error)
  710. device_unregister(&platform_bus);
  711. return error;
  712. }
  713. #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
  714. u64 dma_get_required_mask(struct device *dev)
  715. {
  716. u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
  717. u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
  718. u64 mask;
  719. if (!high_totalram) {
  720. /* convert to mask just covering totalram */
  721. low_totalram = (1 << (fls(low_totalram) - 1));
  722. low_totalram += low_totalram - 1;
  723. mask = low_totalram;
  724. } else {
  725. high_totalram = (1 << (fls(high_totalram) - 1));
  726. high_totalram += high_totalram - 1;
  727. mask = (((u64)high_totalram) << 32) + 0xffffffff;
  728. }
  729. return mask;
  730. }
  731. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  732. #endif
  733. static __initdata LIST_HEAD(early_platform_driver_list);
  734. static __initdata LIST_HEAD(early_platform_device_list);
  735. /**
  736. * early_platform_driver_register - register early platform driver
  737. * @epdrv: early_platform driver structure
  738. * @buf: string passed from early_param()
  739. *
  740. * Helper function for early_platform_init() / early_platform_init_buffer()
  741. */
  742. int __init early_platform_driver_register(struct early_platform_driver *epdrv,
  743. char *buf)
  744. {
  745. char *tmp;
  746. int n;
  747. /* Simply add the driver to the end of the global list.
  748. * Drivers will by default be put on the list in compiled-in order.
  749. */
  750. if (!epdrv->list.next) {
  751. INIT_LIST_HEAD(&epdrv->list);
  752. list_add_tail(&epdrv->list, &early_platform_driver_list);
  753. }
  754. /* If the user has specified device then make sure the driver
  755. * gets prioritized. The driver of the last device specified on
  756. * command line will be put first on the list.
  757. */
  758. n = strlen(epdrv->pdrv->driver.name);
  759. if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
  760. list_move(&epdrv->list, &early_platform_driver_list);
  761. /* Allow passing parameters after device name */
  762. if (buf[n] == '\0' || buf[n] == ',')
  763. epdrv->requested_id = -1;
  764. else {
  765. epdrv->requested_id = simple_strtoul(&buf[n + 1],
  766. &tmp, 10);
  767. if (buf[n] != '.' || (tmp == &buf[n + 1])) {
  768. epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
  769. n = 0;
  770. } else
  771. n += strcspn(&buf[n + 1], ",") + 1;
  772. }
  773. if (buf[n] == ',')
  774. n++;
  775. if (epdrv->bufsize) {
  776. memcpy(epdrv->buffer, &buf[n],
  777. min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
  778. epdrv->buffer[epdrv->bufsize - 1] = '\0';
  779. }
  780. }
  781. return 0;
  782. }
  783. /**
  784. * early_platform_add_devices - adds a number of early platform devices
  785. * @devs: array of early platform devices to add
  786. * @num: number of early platform devices in array
  787. *
  788. * Used by early architecture code to register early platform devices and
  789. * their platform data.
  790. */
  791. void __init early_platform_add_devices(struct platform_device **devs, int num)
  792. {
  793. struct device *dev;
  794. int i;
  795. /* simply add the devices to list */
  796. for (i = 0; i < num; i++) {
  797. dev = &devs[i]->dev;
  798. if (!dev->devres_head.next) {
  799. INIT_LIST_HEAD(&dev->devres_head);
  800. list_add_tail(&dev->devres_head,
  801. &early_platform_device_list);
  802. }
  803. }
  804. }
  805. /**
  806. * early_platform_driver_register_all - register early platform drivers
  807. * @class_str: string to identify early platform driver class
  808. *
  809. * Used by architecture code to register all early platform drivers
  810. * for a certain class. If omitted then only early platform drivers
  811. * with matching kernel command line class parameters will be registered.
  812. */
  813. void __init early_platform_driver_register_all(char *class_str)
  814. {
  815. /* The "class_str" parameter may or may not be present on the kernel
  816. * command line. If it is present then there may be more than one
  817. * matching parameter.
  818. *
  819. * Since we register our early platform drivers using early_param()
  820. * we need to make sure that they also get registered in the case
  821. * when the parameter is missing from the kernel command line.
  822. *
  823. * We use parse_early_options() to make sure the early_param() gets
  824. * called at least once. The early_param() may be called more than
  825. * once since the name of the preferred device may be specified on
  826. * the kernel command line. early_platform_driver_register() handles
  827. * this case for us.
  828. */
  829. parse_early_options(class_str);
  830. }
  831. /**
  832. * early_platform_match - find early platform device matching driver
  833. * @epdrv: early platform driver structure
  834. * @id: id to match against
  835. */
  836. static __init struct platform_device *
  837. early_platform_match(struct early_platform_driver *epdrv, int id)
  838. {
  839. struct platform_device *pd;
  840. list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
  841. if (platform_match(&pd->dev, &epdrv->pdrv->driver))
  842. if (pd->id == id)
  843. return pd;
  844. return NULL;
  845. }
  846. /**
  847. * early_platform_left - check if early platform driver has matching devices
  848. * @epdrv: early platform driver structure
  849. * @id: return true if id or above exists
  850. */
  851. static __init int early_platform_left(struct early_platform_driver *epdrv,
  852. int id)
  853. {
  854. struct platform_device *pd;
  855. list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
  856. if (platform_match(&pd->dev, &epdrv->pdrv->driver))
  857. if (pd->id >= id)
  858. return 1;
  859. return 0;
  860. }
  861. /**
  862. * early_platform_driver_probe_id - probe drivers matching class_str and id
  863. * @class_str: string to identify early platform driver class
  864. * @id: id to match against
  865. * @nr_probe: number of platform devices to successfully probe before exiting
  866. */
  867. static int __init early_platform_driver_probe_id(char *class_str,
  868. int id,
  869. int nr_probe)
  870. {
  871. struct early_platform_driver *epdrv;
  872. struct platform_device *match;
  873. int match_id;
  874. int n = 0;
  875. int left = 0;
  876. list_for_each_entry(epdrv, &early_platform_driver_list, list) {
  877. /* only use drivers matching our class_str */
  878. if (strcmp(class_str, epdrv->class_str))
  879. continue;
  880. if (id == -2) {
  881. match_id = epdrv->requested_id;
  882. left = 1;
  883. } else {
  884. match_id = id;
  885. left += early_platform_left(epdrv, id);
  886. /* skip requested id */
  887. switch (epdrv->requested_id) {
  888. case EARLY_PLATFORM_ID_ERROR:
  889. case EARLY_PLATFORM_ID_UNSET:
  890. break;
  891. default:
  892. if (epdrv->requested_id == id)
  893. match_id = EARLY_PLATFORM_ID_UNSET;
  894. }
  895. }
  896. switch (match_id) {
  897. case EARLY_PLATFORM_ID_ERROR:
  898. pr_warning("%s: unable to parse %s parameter\n",
  899. class_str, epdrv->pdrv->driver.name);
  900. /* fall-through */
  901. case EARLY_PLATFORM_ID_UNSET:
  902. match = NULL;
  903. break;
  904. default:
  905. match = early_platform_match(epdrv, match_id);
  906. }
  907. if (match) {
  908. /*
  909. * Set up a sensible init_name to enable
  910. * dev_name() and others to be used before the
  911. * rest of the driver core is initialized.
  912. */
  913. if (!match->dev.init_name && slab_is_available()) {
  914. if (match->id != -1)
  915. match->dev.init_name =
  916. kasprintf(GFP_KERNEL, "%s.%d",
  917. match->name,
  918. match->id);
  919. else
  920. match->dev.init_name =
  921. kasprintf(GFP_KERNEL, "%s",
  922. match->name);
  923. if (!match->dev.init_name)
  924. return -ENOMEM;
  925. }
  926. if (epdrv->pdrv->probe(match))
  927. pr_warning("%s: unable to probe %s early.\n",
  928. class_str, match->name);
  929. else
  930. n++;
  931. }
  932. if (n >= nr_probe)
  933. break;
  934. }
  935. if (left)
  936. return n;
  937. else
  938. return -ENODEV;
  939. }
  940. /**
  941. * early_platform_driver_probe - probe a class of registered drivers
  942. * @class_str: string to identify early platform driver class
  943. * @nr_probe: number of platform devices to successfully probe before exiting
  944. * @user_only: only probe user specified early platform devices
  945. *
  946. * Used by architecture code to probe registered early platform drivers
  947. * within a certain class. For probe to happen a registered early platform
  948. * device matching a registered early platform driver is needed.
  949. */
  950. int __init early_platform_driver_probe(char *class_str,
  951. int nr_probe,
  952. int user_only)
  953. {
  954. int k, n, i;
  955. n = 0;
  956. for (i = -2; n < nr_probe; i++) {
  957. k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
  958. if (k < 0)
  959. break;
  960. n += k;
  961. if (user_only)
  962. break;
  963. }
  964. return n;
  965. }
  966. /**
  967. * early_platform_cleanup - clean up early platform code
  968. */
  969. void __init early_platform_cleanup(void)
  970. {
  971. struct platform_device *pd, *pd2;
  972. /* clean up the devres list used to chain devices */
  973. list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
  974. dev.devres_head) {
  975. list_del(&pd->dev.devres_head);
  976. memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
  977. }
  978. }