platform.c 5.0 KB

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