1
0

dev_spi_bus.c 4.6 KB

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