bus.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-11-26 GuEe-GUI first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #define DBG_TAG "scmi.bus"
  13. #define DBG_LVL DBG_INFO
  14. #include <rtdbg.h>
  15. static struct rt_bus scmi_bus;
  16. rt_err_t rt_scmi_driver_register(struct rt_scmi_driver *driver)
  17. {
  18. RT_ASSERT(driver != RT_NULL);
  19. driver->parent.bus = &scmi_bus;
  20. return rt_driver_register(&driver->parent);
  21. }
  22. rt_err_t rt_scmi_device_register(struct rt_scmi_device *device)
  23. {
  24. RT_ASSERT(device != RT_NULL);
  25. return rt_bus_add_device(&scmi_bus, &device->parent);
  26. }
  27. static rt_bool_t scmi_match(rt_driver_t drv, rt_device_t dev)
  28. {
  29. const struct rt_scmi_device_id *id;
  30. struct rt_scmi_driver *driver = rt_container_of(drv, struct rt_scmi_driver, parent);
  31. struct rt_scmi_device *device = rt_container_of(dev, struct rt_scmi_device, parent);
  32. for (id = driver->ids; id->protocol_id; ++id)
  33. {
  34. if (id->protocol_id == device->protocol_id)
  35. {
  36. if (!id->name || !device->name || !rt_strcmp(id->name, device->name))
  37. {
  38. return RT_TRUE;
  39. }
  40. }
  41. }
  42. return RT_FALSE;
  43. }
  44. static rt_err_t scmi_probe(rt_device_t dev)
  45. {
  46. rt_err_t err;
  47. struct rt_scmi_driver *driver = rt_container_of(dev->drv, struct rt_scmi_driver, parent);
  48. struct rt_scmi_device *device = rt_container_of(dev, struct rt_scmi_device, parent);
  49. if (!device->agent)
  50. {
  51. return -RT_EINVAL;
  52. }
  53. err = driver->probe(device);
  54. return err;
  55. }
  56. static struct rt_bus scmi_bus =
  57. {
  58. .name = "scmi",
  59. .match = scmi_match,
  60. .probe = scmi_probe,
  61. };
  62. static int scmi_bus_init(void)
  63. {
  64. rt_bus_register(&scmi_bus);
  65. return 0;
  66. }
  67. INIT_CORE_EXPORT(scmi_bus_init);