platform.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #include <drivers/core/power_domain.h>
  19. static struct rt_bus platform_bus;
  20. /**
  21. * @brief This function create a platform device.
  22. *
  23. * @param name is name of the platform device.
  24. *
  25. * @return a new platform device.
  26. */
  27. struct rt_platform_device *rt_platform_device_alloc(const char *name)
  28. {
  29. struct rt_platform_device *pdev = rt_calloc(1, sizeof(*pdev));
  30. if (!pdev)
  31. {
  32. return RT_NULL;
  33. }
  34. pdev->parent.bus = &platform_bus;
  35. pdev->name = name;
  36. return pdev;
  37. }
  38. /**
  39. * @brief This function register a rt_driver to platform bus.
  40. *
  41. * @return the error code, RT_EOK on successfully.
  42. */
  43. rt_err_t rt_platform_driver_register(struct rt_platform_driver *pdrv)
  44. {
  45. RT_ASSERT(pdrv != RT_NULL);
  46. pdrv->parent.bus = &platform_bus;
  47. #if RT_NAME_MAX > 0
  48. rt_strcpy(pdrv->parent.parent.name, pdrv->name);
  49. #else
  50. pdrv->parent.parent.name = pdrv->name;
  51. #endif
  52. return rt_driver_register(&pdrv->parent);
  53. }
  54. /**
  55. * @brief This function register a rt_device to platform bus.
  56. *
  57. * @return the error code, RT_EOK on successfully.
  58. */
  59. rt_err_t rt_platform_device_register(struct rt_platform_device *pdev)
  60. {
  61. RT_ASSERT(pdev != RT_NULL);
  62. return rt_bus_add_device(&platform_bus, &pdev->parent);
  63. }
  64. static rt_bool_t platform_match(rt_driver_t drv, rt_device_t dev)
  65. {
  66. struct rt_platform_driver *pdrv = rt_container_of(drv, struct rt_platform_driver, parent);
  67. struct rt_platform_device *pdev = rt_container_of(dev, struct rt_platform_device, parent);
  68. #ifdef RT_USING_OFW
  69. struct rt_ofw_node *np = dev->ofw_node;
  70. /* 1、match with ofw node */
  71. if (np)
  72. {
  73. pdev->id = rt_ofw_node_match(np, pdrv->ids);
  74. if (pdev->id)
  75. {
  76. return RT_TRUE;
  77. }
  78. }
  79. #endif
  80. /* 2、match with name */
  81. if (pdev->name && pdrv->name)
  82. {
  83. if (pdev->name == pdrv->name)
  84. {
  85. return RT_TRUE;
  86. }
  87. else
  88. {
  89. return !rt_strcmp(pdrv->name, pdev->name);
  90. }
  91. }
  92. return RT_FALSE;
  93. }
  94. static rt_err_t platform_probe(rt_device_t dev)
  95. {
  96. rt_err_t err;
  97. struct rt_platform_driver *pdrv = rt_container_of(dev->drv, struct rt_platform_driver, parent);
  98. struct rt_platform_device *pdev = rt_container_of(dev, struct rt_platform_device, parent);
  99. #ifdef RT_USING_OFW
  100. struct rt_ofw_node *np = dev->ofw_node;
  101. #endif
  102. err = rt_dm_power_domain_attach(dev, RT_TRUE);
  103. if (err && err != -RT_EEMPTY)
  104. {
  105. LOG_E("Attach power domain error = %s in device %s", pdev->name, rt_strerror(err));
  106. return err;
  107. }
  108. err = pdrv->probe(pdev);
  109. if (!err)
  110. {
  111. #ifdef RT_USING_OFW
  112. if (np)
  113. {
  114. rt_ofw_node_set_flag(np, RT_OFW_F_READLY);
  115. }
  116. #endif
  117. }
  118. else
  119. {
  120. if (err == -RT_ENOMEM)
  121. {
  122. LOG_W("System not memory in driver %s", pdrv->name);
  123. }
  124. rt_dm_power_domain_detach(dev, RT_TRUE);
  125. }
  126. return err;
  127. }
  128. static rt_err_t platform_remove(rt_device_t dev)
  129. {
  130. struct rt_platform_driver *pdrv = rt_container_of(dev->drv, struct rt_platform_driver, parent);
  131. struct rt_platform_device *pdev = rt_container_of(dev, struct rt_platform_device, parent);
  132. if (pdrv && pdrv->remove)
  133. {
  134. pdrv->remove(pdev);
  135. }
  136. rt_dm_power_domain_detach(dev, RT_TRUE);
  137. rt_platform_ofw_free(pdev);
  138. return RT_EOK;
  139. }
  140. static rt_err_t platform_shutdown(rt_device_t dev)
  141. {
  142. struct rt_platform_driver *pdrv = rt_container_of(dev->drv, struct rt_platform_driver, parent);
  143. struct rt_platform_device *pdev = rt_container_of(dev, struct rt_platform_device, parent);
  144. if (pdrv && pdrv->shutdown)
  145. {
  146. pdrv->shutdown(pdev);
  147. }
  148. rt_dm_power_domain_detach(dev, RT_TRUE);
  149. rt_platform_ofw_free(pdev);
  150. return RT_EOK;
  151. }
  152. static struct rt_bus platform_bus =
  153. {
  154. .name = "platform",
  155. .match = platform_match,
  156. .probe = platform_probe,
  157. .remove = platform_remove,
  158. .shutdown = platform_shutdown,
  159. };
  160. static int platform_bus_init(void)
  161. {
  162. rt_bus_register(&platform_bus);
  163. return 0;
  164. }
  165. INIT_CORE_EXPORT(platform_bus_init);