platform.c 4.8 KB

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