dev_spi_bus.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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-12-06 GuEe-GUI first version
  9. */
  10. #include "dev_spi_dm.h"
  11. #define DBG_TAG "spi.bus"
  12. #define DBG_LVL DBG_INFO
  13. #include <rtdbg.h>
  14. extern rt_err_t rt_spidev_device_init(struct rt_spi_device *dev, const char *name);
  15. static struct rt_bus spi_bus;
  16. void spi_bus_scan_devices(struct rt_spi_bus *bus)
  17. {
  18. #ifdef RT_USING_OFW
  19. if (bus->parent.ofw_node)
  20. {
  21. struct rt_ofw_node *np = bus->parent.ofw_node, *spi_dev_np;
  22. rt_ofw_foreach_available_child_node(np, spi_dev_np)
  23. {
  24. struct rt_spi_device *spi_dev;
  25. if (!rt_ofw_prop_read_bool(spi_dev_np, "compatible"))
  26. {
  27. continue;
  28. }
  29. spi_dev = rt_calloc(1, sizeof(*spi_dev));
  30. if (!spi_dev)
  31. {
  32. rt_ofw_node_put(spi_dev_np);
  33. LOG_E("Not memory to create spi device: %s",
  34. rt_ofw_node_full_name(spi_dev_np));
  35. return;
  36. }
  37. spi_dev->parent.ofw_node = spi_dev_np;
  38. spi_dev->parent.type = RT_Device_Class_Unknown;
  39. spi_dev->name = rt_ofw_node_name(spi_dev_np);
  40. spi_dev->bus = bus;
  41. rt_dm_dev_set_name(&spi_dev->parent, rt_ofw_node_full_name(spi_dev_np));
  42. if (spi_device_ofw_parse(spi_dev))
  43. {
  44. continue;
  45. }
  46. rt_spi_device_register(spi_dev);
  47. }
  48. }
  49. #endif /* RT_USING_OFW */
  50. }
  51. rt_err_t rt_spi_driver_register(struct rt_spi_driver *driver)
  52. {
  53. RT_ASSERT(driver != RT_NULL);
  54. driver->parent.bus = &spi_bus;
  55. return rt_driver_register(&driver->parent);
  56. }
  57. rt_err_t rt_spi_device_register(struct rt_spi_device *device)
  58. {
  59. RT_ASSERT(device != RT_NULL);
  60. return rt_bus_add_device(&spi_bus, &device->parent);
  61. }
  62. static rt_bool_t spi_match(rt_driver_t drv, rt_device_t dev)
  63. {
  64. const struct rt_spi_device_id *id;
  65. struct rt_spi_driver *driver = rt_container_of(drv, struct rt_spi_driver, parent);
  66. struct rt_spi_device *device = rt_container_of(dev, struct rt_spi_device, parent);
  67. if ((id = driver->ids))
  68. {
  69. for (; id->name[0]; ++id)
  70. {
  71. if (!rt_strcmp(id->name, device->name))
  72. {
  73. device->id = id;
  74. device->ofw_id = RT_NULL;
  75. return RT_TRUE;
  76. }
  77. }
  78. }
  79. #ifdef RT_USING_OFW
  80. device->ofw_id = rt_ofw_node_match(device->parent.ofw_node, driver->ofw_ids);
  81. if (device->ofw_id)
  82. {
  83. device->id = RT_NULL;
  84. return RT_TRUE;
  85. }
  86. #endif
  87. return RT_FALSE;
  88. }
  89. static rt_err_t spi_probe(rt_device_t dev)
  90. {
  91. rt_err_t err;
  92. struct rt_spi_bus *bus;
  93. struct rt_spi_driver *driver = rt_container_of(dev->drv, struct rt_spi_driver, parent);
  94. struct rt_spi_device *device = rt_container_of(dev, struct rt_spi_device, parent);
  95. if (!device->bus)
  96. {
  97. return -RT_EINVAL;
  98. }
  99. err = driver->probe(device);
  100. if (err)
  101. {
  102. return err;
  103. }
  104. bus = device->bus;
  105. if (bus->cs_pins[0] >= 0)
  106. {
  107. device->cs_pin = bus->cs_pins[device->chip_select[0]];
  108. rt_pin_mode(device->cs_pin, PIN_MODE_OUTPUT);
  109. }
  110. else
  111. {
  112. device->cs_pin = PIN_NONE;
  113. }
  114. /* Driver not register SPI device to system */
  115. if (device->parent.type == RT_Device_Class_Unknown)
  116. {
  117. rt_spidev_device_init(device, rt_dm_dev_get_name(&device->parent));
  118. }
  119. return err;
  120. }
  121. static rt_err_t spi_remove(rt_device_t dev)
  122. {
  123. struct rt_spi_driver *driver = rt_container_of(dev->drv, struct rt_spi_driver, parent);
  124. struct rt_spi_device *device = rt_container_of(dev, struct rt_spi_device, parent);
  125. if (driver && driver->remove)
  126. {
  127. driver->remove(device);
  128. }
  129. rt_free(device);
  130. return RT_EOK;
  131. }
  132. static rt_err_t spi_shutdown(rt_device_t dev)
  133. {
  134. struct rt_spi_driver *driver = rt_container_of(dev->drv, struct rt_spi_driver, parent);
  135. struct rt_spi_device *device = rt_container_of(dev, struct rt_spi_device, parent);
  136. if (driver && driver->shutdown)
  137. {
  138. driver->shutdown(device);
  139. }
  140. rt_free(device);
  141. return RT_EOK;
  142. }
  143. static struct rt_bus spi_bus =
  144. {
  145. .name = "spi",
  146. .match = spi_match,
  147. .probe = spi_probe,
  148. .remove = spi_remove,
  149. .shutdown = spi_shutdown,
  150. };
  151. static int spi_bus_init(void)
  152. {
  153. rt_bus_register(&spi_bus);
  154. return 0;
  155. }
  156. INIT_CORE_EXPORT(spi_bus_init);