1
0

driver.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <rtthread.h>
  7. #ifdef RT_USING_DM
  8. #ifdef RT_USING_FDT
  9. #include <dtb_node.h>
  10. #endif
  11. /**
  12. * This function driver device match with id
  13. *
  14. * @param drv the pointer of driver structure
  15. * @param device_id the id of the device
  16. *
  17. * @return the error code, RT_EOK on successfully.
  18. */
  19. rt_err_t rt_driver_match_with_id(const rt_driver_t drv,int device_id)
  20. {
  21. rt_device_t device;
  22. int ret;
  23. if (!drv)
  24. {
  25. return -RT_EINVAL;
  26. }
  27. device = rt_device_create_since_driver(drv,device_id);
  28. if(!device)
  29. {
  30. return -RT_ERROR;
  31. }
  32. ret = rt_device_bind_driver(device,drv,RT_NULL);
  33. if(ret != 0)
  34. {
  35. return -RT_ERROR;
  36. }
  37. ret = rt_device_probe_and_init(device);
  38. if(ret != 0)
  39. {
  40. return -RT_ERROR;
  41. }
  42. return ret;
  43. }
  44. RTM_EXPORT(rt_driver_match_with_id);
  45. #ifdef RT_USING_FDT
  46. /**
  47. * This function driver device match with dtb_node
  48. *
  49. * @param drv the pointer of driver structure
  50. * @param from_node dtb node entry
  51. * @param max_dev_num the max device support
  52. *
  53. * @return the error code, RT_EOK on successfully.
  54. */
  55. rt_err_t rt_driver_match_with_dtb(const rt_driver_t drv,void *from_node,int max_dev_num)
  56. {
  57. struct dtb_node** node_list;
  58. rt_device_t device;
  59. int ret,i;
  60. int total_dev_num = 0;
  61. if ((!drv)||(!drv->dev_match)||(!drv->dev_match->compatible)||(!from_node)||(!drv->device_size))
  62. {
  63. return -RT_EINVAL;
  64. }
  65. node_list = rt_calloc(max_dev_num,sizeof(void *));
  66. if(!node_list)
  67. {
  68. return -RT_ERROR;
  69. }
  70. ret = dtb_node_find_all_compatible_node(from_node,drv->dev_match->compatible,node_list,max_dev_num,&total_dev_num);
  71. if((ret != 0) || (!total_dev_num))
  72. {
  73. return -RT_ERROR;
  74. }
  75. for(i = 0; i < total_dev_num; i ++)
  76. {
  77. if (!dtb_node_device_is_available(node_list[i]))
  78. {
  79. continue;
  80. }
  81. device = rt_device_create_since_driver(drv,i);
  82. if(!device)
  83. {
  84. continue;
  85. }
  86. ret = rt_device_bind_driver(device,drv,node_list[i]);
  87. if(ret != 0)
  88. {
  89. continue;
  90. }
  91. ret = rt_device_probe_and_init(device);
  92. if(ret != 0)
  93. {
  94. continue;
  95. }
  96. }
  97. rt_free(node_list);
  98. return ret;
  99. }
  100. RTM_EXPORT(rt_driver_match_with_dtb);
  101. #endif /* RT_USING_FDT */
  102. #endif /* RT_USING_DM */