platform.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
  3. * <benh@kernel.crashing.org>
  4. * and Arnd Bergmann, IBM Corp.
  5. * Merged from powerpc/kernel/of_platform.c and
  6. * sparc{,64}/kernel/of_device.c by Stephen Rothwell
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. */
  14. #include <linux/errno.h>
  15. #include <linux/module.h>
  16. #include <linux/device.h>
  17. #include <linux/of_device.h>
  18. #include <linux/of_platform.h>
  19. static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
  20. {
  21. struct of_device *of_dev = to_of_device(dev);
  22. struct of_platform_driver *of_drv = to_of_platform_driver(drv);
  23. const struct of_device_id *matches = of_drv->match_table;
  24. if (!matches)
  25. return 0;
  26. return of_match_device(matches, of_dev) != NULL;
  27. }
  28. static int of_platform_device_probe(struct device *dev)
  29. {
  30. int error = -ENODEV;
  31. struct of_platform_driver *drv;
  32. struct of_device *of_dev;
  33. const struct of_device_id *match;
  34. drv = to_of_platform_driver(dev->driver);
  35. of_dev = to_of_device(dev);
  36. if (!drv->probe)
  37. return error;
  38. of_dev_get(of_dev);
  39. match = of_match_device(drv->match_table, of_dev);
  40. if (match)
  41. error = drv->probe(of_dev, match);
  42. if (error)
  43. of_dev_put(of_dev);
  44. return error;
  45. }
  46. static int of_platform_device_remove(struct device *dev)
  47. {
  48. struct of_device *of_dev = to_of_device(dev);
  49. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  50. if (dev->driver && drv->remove)
  51. drv->remove(of_dev);
  52. return 0;
  53. }
  54. static int of_platform_device_suspend(struct device *dev, pm_message_t state)
  55. {
  56. struct of_device *of_dev = to_of_device(dev);
  57. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  58. int error = 0;
  59. if (dev->driver && drv->suspend)
  60. error = drv->suspend(of_dev, state);
  61. return error;
  62. }
  63. static int of_platform_device_resume(struct device * dev)
  64. {
  65. struct of_device *of_dev = to_of_device(dev);
  66. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  67. int error = 0;
  68. if (dev->driver && drv->resume)
  69. error = drv->resume(of_dev);
  70. return error;
  71. }
  72. int of_bus_type_init(struct bus_type *bus, const char *name)
  73. {
  74. bus->name = name;
  75. bus->match = of_platform_bus_match;
  76. bus->probe = of_platform_device_probe;
  77. bus->remove = of_platform_device_remove;
  78. bus->suspend = of_platform_device_suspend;
  79. bus->resume = of_platform_device_resume;
  80. return bus_register(bus);
  81. }
  82. int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
  83. {
  84. /* initialize common driver fields */
  85. if (!drv->driver.name)
  86. drv->driver.name = drv->name;
  87. if (!drv->driver.owner)
  88. drv->driver.owner = drv->owner;
  89. drv->driver.bus = bus;
  90. /* register with core */
  91. return driver_register(&drv->driver);
  92. }
  93. EXPORT_SYMBOL(of_register_driver);
  94. void of_unregister_driver(struct of_platform_driver *drv)
  95. {
  96. driver_unregister(&drv->driver);
  97. }
  98. EXPORT_SYMBOL(of_unregister_driver);