platform.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/device.h>
  16. #include <linux/of_device.h>
  17. #include <linux/of_platform.h>
  18. static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
  19. {
  20. struct of_device *of_dev = to_of_device(dev);
  21. struct of_platform_driver *of_drv = to_of_platform_driver(drv);
  22. const struct of_device_id *matches = of_drv->match_table;
  23. if (!matches)
  24. return 0;
  25. return of_match_device(matches, of_dev) != NULL;
  26. }
  27. static int of_platform_device_probe(struct device *dev)
  28. {
  29. int error = -ENODEV;
  30. struct of_platform_driver *drv;
  31. struct of_device *of_dev;
  32. const struct of_device_id *match;
  33. drv = to_of_platform_driver(dev->driver);
  34. of_dev = to_of_device(dev);
  35. if (!drv->probe)
  36. return error;
  37. of_dev_get(of_dev);
  38. match = of_match_device(drv->match_table, of_dev);
  39. if (match)
  40. error = drv->probe(of_dev, match);
  41. if (error)
  42. of_dev_put(of_dev);
  43. return error;
  44. }
  45. static int of_platform_device_remove(struct device *dev)
  46. {
  47. struct of_device *of_dev = to_of_device(dev);
  48. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  49. if (dev->driver && drv->remove)
  50. drv->remove(of_dev);
  51. return 0;
  52. }
  53. static int of_platform_device_suspend(struct device *dev, pm_message_t state)
  54. {
  55. struct of_device *of_dev = to_of_device(dev);
  56. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  57. int error = 0;
  58. if (dev->driver && drv->suspend)
  59. error = drv->suspend(of_dev, state);
  60. return error;
  61. }
  62. static int of_platform_device_resume(struct device * dev)
  63. {
  64. struct of_device *of_dev = to_of_device(dev);
  65. struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
  66. int error = 0;
  67. if (dev->driver && drv->resume)
  68. error = drv->resume(of_dev);
  69. return error;
  70. }
  71. int of_bus_type_init(struct bus_type *bus, const char *name)
  72. {
  73. bus->name = name;
  74. bus->match = of_platform_bus_match;
  75. bus->probe = of_platform_device_probe;
  76. bus->remove = of_platform_device_remove;
  77. bus->suspend = of_platform_device_suspend;
  78. bus->resume = of_platform_device_resume;
  79. return bus_register(bus);
  80. }