platform.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-04-12 ErikChan the first version
  9. * 2023-10-13 zmshahaha distinguish ofw and none-ofw situation
  10. */
  11. #include <rtthread.h>
  12. #include <drivers/platform.h>
  13. #include <drivers/core/bus.h>
  14. #include <drivers/core/dm.h>
  15. static struct rt_bus platform_bus;
  16. /**
  17. * @brief This function create a platform device.
  18. *
  19. * @param name is name of the platform device.
  20. *
  21. * @return a new platform device.
  22. */
  23. struct rt_platform_device *rt_platform_device_alloc(const char *name)
  24. {
  25. struct rt_platform_device *pdev = rt_calloc(1, sizeof(*pdev));
  26. pdev->parent.bus = &platform_bus;
  27. pdev->name = name;
  28. return pdev;
  29. }
  30. /**
  31. * @brief This function register a rt_driver to platform bus.
  32. *
  33. * @return the error code, RT_EOK on successfully.
  34. */
  35. rt_err_t rt_platform_driver_register(struct rt_platform_driver *pdrv)
  36. {
  37. RT_ASSERT(pdrv != RT_NULL);
  38. pdrv->parent.bus = &platform_bus;
  39. return rt_driver_register(&pdrv->parent);
  40. }
  41. /**
  42. * @brief This function register a rt_device to platform bus.
  43. *
  44. * @return the error code, RT_EOK on successfully.
  45. */
  46. rt_err_t rt_platform_device_register(struct rt_platform_device *pdev)
  47. {
  48. RT_ASSERT(pdev != RT_NULL);
  49. return rt_bus_add_device(&platform_bus, &pdev->parent);
  50. }
  51. static rt_bool_t platform_match(rt_driver_t drv, rt_device_t dev)
  52. {
  53. struct rt_platform_driver *pdrv = rt_container_of(drv, struct rt_platform_driver, parent);
  54. struct rt_platform_device *pdev = rt_container_of(dev, struct rt_platform_device, parent);
  55. #ifdef RT_USING_OFW
  56. struct rt_ofw_node *np = dev->ofw_node;
  57. #endif
  58. #ifdef RT_USING_OFW
  59. if (np)
  60. {
  61. /* 1、match with ofw node */
  62. pdev->id = rt_ofw_node_match(np, pdrv->ids);
  63. return !!pdev->id;
  64. }
  65. #endif
  66. if (pdev->name && pdrv->name)
  67. {
  68. /* 2、match with name */
  69. if (pdev->name == pdrv->name)
  70. {
  71. return RT_TRUE;
  72. }
  73. else
  74. {
  75. return !rt_strcmp(pdrv->name, pdev->name);
  76. }
  77. }
  78. return RT_FALSE;
  79. }
  80. static rt_err_t platform_probe(rt_device_t dev)
  81. {
  82. rt_err_t err;
  83. struct rt_platform_driver *pdrv = rt_container_of(dev->drv, struct rt_platform_driver, parent);
  84. struct rt_platform_device *pdev = rt_container_of(dev, struct rt_platform_device, parent);
  85. #ifdef RT_USING_OFW
  86. struct rt_ofw_node *np = dev->ofw_node;
  87. #endif
  88. err = pdrv->probe(pdev);
  89. #ifdef RT_USING_OFW
  90. if (!err)
  91. {
  92. if (np)
  93. {
  94. rt_ofw_node_set_flag(np, RT_OFW_F_READLY);
  95. }
  96. }
  97. else
  98. {
  99. if (np)
  100. {
  101. rt_ofw_data(np) = &pdev->parent;
  102. }
  103. }
  104. #endif
  105. return err;
  106. }
  107. static struct rt_bus platform_bus =
  108. {
  109. .name = "platform",
  110. .match = platform_match,
  111. .probe = platform_probe,
  112. };
  113. static int platform_bus_init(void)
  114. {
  115. rt_bus_register(&platform_bus);
  116. return 0;
  117. }
  118. INIT_CORE_EXPORT(platform_bus_init);