platform.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. */
  10. #include <rtthread.h>
  11. #include <drivers/platform.h>
  12. #include <drivers/core/bus.h>
  13. #include <drivers/core/dm.h>
  14. static struct rt_bus platform_bus;
  15. /**
  16. * @brief This function create a platform device.
  17. *
  18. * @param name is name of the platform device.
  19. *
  20. * @return a new platform device.
  21. */
  22. struct rt_platform_device *rt_platform_device_alloc(const char *name)
  23. {
  24. struct rt_platform_device *pdev = rt_calloc(1, sizeof(*pdev));
  25. pdev->parent.bus = &platform_bus;
  26. pdev->name = name;
  27. return pdev;
  28. }
  29. /**
  30. * @brief This function register a rt_driver to platform bus.
  31. *
  32. * @return the error code, RT_EOK on successfully.
  33. */
  34. rt_err_t rt_platform_driver_register(struct rt_platform_driver *pdrv)
  35. {
  36. RT_ASSERT(pdrv != RT_NULL);
  37. pdrv->parent.bus = &platform_bus;
  38. return rt_driver_register(&pdrv->parent);
  39. }
  40. /**
  41. * @brief This function register a rt_device to platform bus.
  42. *
  43. * @return the error code, RT_EOK on successfully.
  44. */
  45. rt_err_t rt_platform_device_register(struct rt_platform_device *pdev)
  46. {
  47. RT_ASSERT(pdev != RT_NULL);
  48. return rt_bus_add_device(&platform_bus, &pdev->parent);
  49. }
  50. static rt_bool_t platform_match(rt_driver_t drv, rt_device_t dev)
  51. {
  52. struct rt_ofw_node *np = dev->ofw_node;
  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. if (np)
  56. {
  57. /* 1、match with ofw node */
  58. pdev->id = rt_ofw_node_match(np, pdrv->ids);
  59. return !!pdev->id;
  60. }
  61. else if (pdev->name && pdrv->name)
  62. {
  63. /* 2、match with name */
  64. if (pdev->name == pdrv->name)
  65. {
  66. return RT_TRUE;
  67. }
  68. else
  69. {
  70. return !rt_strcmp(pdrv->name, pdev->name);
  71. }
  72. }
  73. return RT_FALSE;
  74. }
  75. static rt_err_t platform_probe(rt_device_t dev)
  76. {
  77. rt_err_t err;
  78. struct rt_ofw_node *np = dev->ofw_node;
  79. struct rt_platform_driver *pdrv = rt_container_of(dev->drv, struct rt_platform_driver, parent);
  80. struct rt_platform_device *pdev = rt_container_of(dev, struct rt_platform_device, parent);
  81. err = pdrv->probe(pdev);
  82. if (!err)
  83. {
  84. if (np)
  85. {
  86. rt_ofw_node_set_flag(np, RT_OFW_F_READLY);
  87. }
  88. }
  89. else
  90. {
  91. if (np)
  92. {
  93. rt_ofw_data(np) = &pdev->parent;
  94. }
  95. }
  96. return err;
  97. }
  98. static struct rt_bus platform_bus =
  99. {
  100. .name = "platform",
  101. .match = platform_match,
  102. .probe = platform_probe,
  103. };
  104. static int platform_bus_init(void)
  105. {
  106. rt_bus_register(&platform_bus);
  107. return 0;
  108. }
  109. INIT_CORE_EXPORT(platform_bus_init);