platform.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #define DBG_TAG "rtdm.pltaform"
  13. #define DBG_LVL DBG_INFO
  14. #include <rtdbg.h>
  15. #include <drivers/platform.h>
  16. #include <drivers/core/bus.h>
  17. #include <drivers/core/dm.h>
  18. static struct rt_bus platform_bus;
  19. /**
  20. * @brief This function create a platform device.
  21. *
  22. * @param name is name of the platform device.
  23. *
  24. * @return a new platform device.
  25. */
  26. struct rt_platform_device *rt_platform_device_alloc(const char *name)
  27. {
  28. struct rt_platform_device *pdev = rt_calloc(1, sizeof(*pdev));
  29. if (!pdev)
  30. {
  31. return RT_NULL;
  32. }
  33. pdev->parent.bus = &platform_bus;
  34. pdev->name = name;
  35. return pdev;
  36. }
  37. /**
  38. * @brief This function register a rt_driver to platform bus.
  39. *
  40. * @return the error code, RT_EOK on successfully.
  41. */
  42. rt_err_t rt_platform_driver_register(struct rt_platform_driver *pdrv)
  43. {
  44. RT_ASSERT(pdrv != RT_NULL);
  45. pdrv->parent.bus = &platform_bus;
  46. #if RT_NAME_MAX > 0
  47. rt_strcpy(pdrv->parent.parent.name, pdrv->name);
  48. #else
  49. pdrv->parent.parent.name = pdrv->name;
  50. #endif
  51. return rt_driver_register(&pdrv->parent);
  52. }
  53. /**
  54. * @brief This function register a rt_device to platform bus.
  55. *
  56. * @return the error code, RT_EOK on successfully.
  57. */
  58. rt_err_t rt_platform_device_register(struct rt_platform_device *pdev)
  59. {
  60. RT_ASSERT(pdev != RT_NULL);
  61. return rt_bus_add_device(&platform_bus, &pdev->parent);
  62. }
  63. static rt_bool_t platform_match(rt_driver_t drv, rt_device_t dev)
  64. {
  65. struct rt_platform_driver *pdrv = rt_container_of(drv, struct rt_platform_driver, parent);
  66. struct rt_platform_device *pdev = rt_container_of(dev, struct rt_platform_device, parent);
  67. #ifdef RT_USING_OFW
  68. struct rt_ofw_node *np = dev->ofw_node;
  69. /* 1、match with ofw node */
  70. if (np)
  71. {
  72. pdev->id = rt_ofw_node_match(np, pdrv->ids);
  73. if (pdev->id)
  74. {
  75. return RT_TRUE;
  76. }
  77. }
  78. #endif
  79. /* 2、match with name */
  80. if (pdev->name && pdrv->name)
  81. {
  82. if (pdev->name == pdrv->name)
  83. {
  84. return RT_TRUE;
  85. }
  86. else
  87. {
  88. return !rt_strcmp(pdrv->name, pdev->name);
  89. }
  90. }
  91. return RT_FALSE;
  92. }
  93. static rt_err_t platform_probe(rt_device_t dev)
  94. {
  95. rt_err_t err;
  96. struct rt_platform_driver *pdrv = rt_container_of(dev->drv, struct rt_platform_driver, parent);
  97. struct rt_platform_device *pdev = rt_container_of(dev, struct rt_platform_device, parent);
  98. #ifdef RT_USING_OFW
  99. struct rt_ofw_node *np = dev->ofw_node;
  100. #endif
  101. err = pdrv->probe(pdev);
  102. if (!err)
  103. {
  104. #ifdef RT_USING_OFW
  105. if (np)
  106. {
  107. rt_ofw_node_set_flag(np, RT_OFW_F_READLY);
  108. }
  109. #endif
  110. }
  111. else
  112. {
  113. if (err == -RT_ENOMEM)
  114. {
  115. LOG_W("System not memory in driver %s", pdrv->name);
  116. }
  117. }
  118. return err;
  119. }
  120. static struct rt_bus platform_bus =
  121. {
  122. .name = "platform",
  123. .match = platform_match,
  124. .probe = platform_probe,
  125. };
  126. static int platform_bus_init(void)
  127. {
  128. rt_bus_register(&platform_bus);
  129. return 0;
  130. }
  131. INIT_CORE_EXPORT(platform_bus_init);