platform.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. struct rt_ofw_node *np = dev->ofw_node;
  69. /* 1、match with ofw node */
  70. if (np)
  71. {
  72. #ifdef RT_USING_OFW
  73. pdev->id = rt_ofw_node_match(np, pdrv->ids);
  74. #else
  75. pdev->id = RT_NULL;
  76. #endif
  77. if (pdev->id)
  78. {
  79. return RT_TRUE;
  80. }
  81. }
  82. /* 2、match with name */
  83. if (pdev->name && pdrv->name)
  84. {
  85. if (pdev->name == pdrv->name)
  86. {
  87. return RT_TRUE;
  88. }
  89. else
  90. {
  91. return !rt_strcmp(pdrv->name, pdev->name);
  92. }
  93. }
  94. return RT_FALSE;
  95. }
  96. static rt_err_t platform_probe(rt_device_t dev)
  97. {
  98. rt_err_t err;
  99. struct rt_platform_driver *pdrv = rt_container_of(dev->drv, struct rt_platform_driver, parent);
  100. struct rt_platform_device *pdev = rt_container_of(dev, struct rt_platform_device, parent);
  101. #ifdef RT_USING_OFW
  102. struct rt_ofw_node *np = dev->ofw_node;
  103. #endif
  104. err = rt_dm_power_domain_attach(dev, RT_TRUE);
  105. if (err && err != -RT_EEMPTY)
  106. {
  107. LOG_E("Attach power domain error = %s in device %s", rt_strerror(err),
  108. #ifdef RT_USING_OFW
  109. (pdev->name && pdev->name[0]) ? pdev->name : rt_ofw_node_full_name(np)
  110. #else
  111. pdev->name
  112. #endif
  113. );
  114. return err;
  115. }
  116. err = pdrv->probe(pdev);
  117. if (!err)
  118. {
  119. #ifdef RT_USING_OFW
  120. if (np)
  121. {
  122. rt_ofw_node_set_flag(np, RT_OFW_F_READLY);
  123. }
  124. #endif
  125. }
  126. else
  127. {
  128. if (err == -RT_ENOMEM)
  129. {
  130. LOG_W("System not memory in driver %s", pdrv->name);
  131. }
  132. rt_dm_power_domain_detach(dev, RT_TRUE);
  133. }
  134. return err;
  135. }
  136. static rt_err_t platform_remove(rt_device_t dev)
  137. {
  138. struct rt_platform_driver *pdrv = rt_container_of(dev->drv, struct rt_platform_driver, parent);
  139. struct rt_platform_device *pdev = rt_container_of(dev, struct rt_platform_device, parent);
  140. if (pdrv && pdrv->remove)
  141. {
  142. pdrv->remove(pdev);
  143. }
  144. rt_dm_power_domain_detach(dev, RT_TRUE);
  145. rt_platform_ofw_free(pdev);
  146. return RT_EOK;
  147. }
  148. static rt_err_t platform_shutdown(rt_device_t dev)
  149. {
  150. struct rt_platform_driver *pdrv = rt_container_of(dev->drv, struct rt_platform_driver, parent);
  151. struct rt_platform_device *pdev = rt_container_of(dev, struct rt_platform_device, parent);
  152. if (pdrv && pdrv->shutdown)
  153. {
  154. pdrv->shutdown(pdev);
  155. }
  156. rt_dm_power_domain_detach(dev, RT_TRUE);
  157. rt_platform_ofw_free(pdev);
  158. return RT_EOK;
  159. }
  160. static struct rt_bus platform_bus =
  161. {
  162. .name = "platform",
  163. .match = platform_match,
  164. .probe = platform_probe,
  165. .remove = platform_remove,
  166. .shutdown = platform_shutdown,
  167. };
  168. static int platform_bus_init(void)
  169. {
  170. rt_bus_register(&platform_bus);
  171. return 0;
  172. }
  173. INIT_CORE_EXPORT(platform_bus_init);