/kern_oII/drivers/of/platform.c

http://omnia2droid.googlecode.com/ · C · 130 lines · 93 code · 22 blank · 15 comment · 15 complexity · d69a6c70213fb837d19846c4f532f8d6 MD5 · raw file

  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. extern struct device_attribute of_platform_device_attrs[];
  20. static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
  21. {
  22. struct of_device *of_dev = to_of_device(dev);
  23. struct of_platform_driver *of_drv = to_of_platform_driver(drv);
  24. const struct of_device_id *matches = of_drv->match_table;
  25. if (!matches)
  26. return 0;
  27. return of_match_device(matches, of_dev) != NULL;
  28. }
  29. static int of_platform_device_probe(struct device *dev)
  30. {
  31. int error = -ENODEV;
  32. struct of_platform_driver *drv;
  33. struct of_device *of_dev;
  34. const struct of_device_id *match;
  35. drv = to_of_platform_driver(dev->driver);
  36. of_dev = to_of_device(dev);
  37. if (!drv->probe)
  38. return error;
  39. of_dev_get(of_dev);
  40. match = of_match_device(drv->match_table, of_dev);
  41. if (match)
  42. error = drv->probe(of_dev, match);
  43. if (error)
  44. of_dev_put(of_dev);
  45. return error;
  46. }
  47. static int of_platform_device_remove(struct device *dev)
  48. {
  49. struct of_device *of_dev = to_of_device(dev);
  50. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  51. if (dev->driver && drv->remove)
  52. drv->remove(of_dev);
  53. return 0;
  54. }
  55. static int of_platform_device_suspend(struct device *dev, pm_message_t state)
  56. {
  57. struct of_device *of_dev = to_of_device(dev);
  58. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  59. int error = 0;
  60. if (dev->driver && drv->suspend)
  61. error = drv->suspend(of_dev, state);
  62. return error;
  63. }
  64. static int of_platform_device_resume(struct device * dev)
  65. {
  66. struct of_device *of_dev = to_of_device(dev);
  67. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  68. int error = 0;
  69. if (dev->driver && drv->resume)
  70. error = drv->resume(of_dev);
  71. return error;
  72. }
  73. static void of_platform_device_shutdown(struct device *dev)
  74. {
  75. struct of_device *of_dev = to_of_device(dev);
  76. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  77. if (dev->driver && drv->shutdown)
  78. drv->shutdown(of_dev);
  79. }
  80. int of_bus_type_init(struct bus_type *bus, const char *name)
  81. {
  82. bus->name = name;
  83. bus->match = of_platform_bus_match;
  84. bus->probe = of_platform_device_probe;
  85. bus->remove = of_platform_device_remove;
  86. bus->suspend = of_platform_device_suspend;
  87. bus->resume = of_platform_device_resume;
  88. bus->shutdown = of_platform_device_shutdown;
  89. bus->dev_attrs = of_platform_device_attrs;
  90. return bus_register(bus);
  91. }
  92. int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
  93. {
  94. /* initialize common driver fields */
  95. if (!drv->driver.name)
  96. drv->driver.name = drv->name;
  97. if (!drv->driver.owner)
  98. drv->driver.owner = drv->owner;
  99. drv->driver.bus = bus;
  100. /* register with core */
  101. return driver_register(&drv->driver);
  102. }
  103. EXPORT_SYMBOL(of_register_driver);
  104. void of_unregister_driver(struct of_platform_driver *drv)
  105. {
  106. driver_unregister(&drv->driver);
  107. }
  108. EXPORT_SYMBOL(of_unregister_driver);