drv_ssi.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #include <rtdevice.h>
  2. #include "gtypes.h"
  3. #include "gd_spi.h"
  4. #include "drv_ssi.h"
  5. #include "platform.h"
  6. /*---------------------------------------------------------------------------*/
  7. /* local defines */
  8. /*---------------------------------------------------------------------------*/
  9. /*---------------------------------------------------------------------------*/
  10. /* local data types */
  11. /*---------------------------------------------------------------------------*/
  12. /*---------------------------------------------------------------------------*/
  13. /* local data */
  14. /*---------------------------------------------------------------------------*/
  15. /*---------------------------------------------------------------------------*/
  16. /* local functions */
  17. /*---------------------------------------------------------------------------*/
  18. static void *gk_get_spi_dev_pri_data(struct rt_spi_device *device)
  19. {
  20. return device->parent.user_data;
  21. }
  22. static rt_err_t gk_spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration)
  23. {
  24. struct gk_spi_slave_info *spi_slave;
  25. struct gk_spi_controller *spi_control;
  26. GD_SPI_OPEN_PARAMS_S openParamsP;
  27. rt_uint32_t spi_hz;
  28. rt_int32_t ret;
  29. rt_uint8_t data_width = 8,polarity;
  30. if(!device || !configuration)
  31. {
  32. rt_kprintf("%s,%d,param wrong!\n",__FUNCTION__,__LINE__);
  33. return RT_ERROR;
  34. }
  35. spi_slave = (struct gk_spi_slave_info *)gk_get_spi_dev_pri_data(device);
  36. spi_control = spi_slave->control;
  37. rt_sem_take(&spi_control->xfer_lock, RT_WAITING_FOREVER);
  38. if(spi_slave->spihandle > 0)
  39. {
  40. GD_SPI_Close(&spi_slave->spihandle);
  41. spi_slave->spihandle = -1;
  42. }
  43. openParamsP.spi = spi_control->id;
  44. openParamsP.slave = spi_slave->id;
  45. openParamsP.csgpio = spi_slave->cs_pin;
  46. openParamsP.used_irq = 1;
  47. if (configuration->max_hz > GD_SPI_FREQ_81M)
  48. spi_hz = GD_SPI_FREQ_81M;
  49. else
  50. spi_hz = configuration->max_hz;
  51. openParamsP.baudrate = spi_hz;
  52. /* CPOL */
  53. if (configuration->mode & RT_SPI_CPOL && configuration->mode & RT_SPI_CPHA)
  54. {
  55. polarity = SPI_POLARITY_MODE3;
  56. }
  57. else if(configuration->mode & RT_SPI_CPOL && !configuration->mode & RT_SPI_CPHA)
  58. {
  59. polarity = SPI_POLARITY_MODE2;
  60. }
  61. else if(!configuration->mode & RT_SPI_CPOL && configuration->mode & RT_SPI_CPHA)
  62. {
  63. polarity = SPI_POLARITY_MODE1;
  64. }
  65. else
  66. polarity = SPI_POLARITY_MODE0;
  67. openParamsP.polarity = polarity;
  68. ret = GD_SPI_Open(&openParamsP,&(spi_slave->spihandle));
  69. if(ret != GD_OK)
  70. {
  71. rt_kprintf("GD_SPI_Open failed!\n");
  72. rt_sem_release(&spi_control->xfer_lock);
  73. return RT_ERROR;
  74. }
  75. /* data_width */
  76. if (configuration->data_width <= 8)
  77. {
  78. data_width = 8;
  79. }
  80. else if (configuration->data_width <= 16)
  81. {
  82. data_width = 16;
  83. }
  84. ret = GD_SPI_SetDatFormat(spi_slave->spihandle, data_width);;
  85. if(ret != GD_OK)
  86. {
  87. rt_kprintf("GD_SPI_SetDatFormat failed!\n");
  88. rt_sem_release(&spi_control->xfer_lock);
  89. return RT_ERROR;
  90. }
  91. rt_sem_release(&spi_control->xfer_lock);
  92. return RT_EOK;
  93. }
  94. static rt_uint32_t gk_spi_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
  95. {
  96. struct gk_spi_slave_info *spi_slave;
  97. struct gk_spi_controller *spi_control;
  98. GERR ret = GD_OK;
  99. if(!device || !message)
  100. {
  101. rt_kprintf("%s,%d,param wrong!\n",__FUNCTION__,__LINE__);
  102. return 0;
  103. }
  104. spi_slave = (struct gk_spi_slave_info *)gk_get_spi_dev_pri_data(device);
  105. if(!spi_slave)
  106. {
  107. rt_kprintf("%s,%d,param wrong!\n",__FUNCTION__,__LINE__);
  108. return 0;
  109. }
  110. spi_control = spi_slave->control;
  111. if(spi_slave->spihandle < 0)
  112. {
  113. rt_kprintf("%s,%d,spi channel not configured!\n",__FUNCTION__,__LINE__);
  114. return 0;
  115. }
  116. if(!message)
  117. {
  118. rt_kprintf("%s,%d,params wrong!\n",__FUNCTION__,__LINE__);
  119. return 0;
  120. }
  121. rt_sem_take(&spi_control->xfer_lock, RT_WAITING_FOREVER);
  122. if(message->cs_take)
  123. GD_SPI_GetDevice(spi_slave->spihandle);
  124. if(message->recv_buf && message->send_buf)
  125. ret = GD_SPI_WriteThenReadBytes(spi_slave->spihandle,(rt_uint8_t *)message->send_buf,message->length,
  126. (rt_uint8_t *)message->recv_buf,message->length);
  127. else if(message->send_buf && !message->recv_buf)
  128. ret = GD_SPI_WriteBytes(spi_slave->spihandle,(rt_uint8_t *)message->send_buf,message->length);
  129. else if(!message->send_buf && message->recv_buf)
  130. ret = GD_SPI_WriteThenReadBytes(spi_slave->spihandle,NULL,0,(rt_uint8_t *)message->recv_buf,message->length);
  131. if(message->cs_release)
  132. GD_SPI_ReleaseDevice(spi_slave->spihandle);
  133. rt_sem_release(&spi_control->xfer_lock);
  134. if(ret != GD_OK)
  135. {
  136. rt_kprintf("%s,%d,transfer error!\n",__FUNCTION__,__LINE__);
  137. return 0;
  138. }
  139. else
  140. {
  141. return message->length;
  142. }
  143. }
  144. static struct rt_spi_ops gk_spi_ops = {
  145. .configure = gk_spi_configure, .xfer = gk_spi_xfer,
  146. };
  147. int gk_spi_probe(void *priv_data)
  148. {
  149. int ret,i;
  150. char spi_dev_name[20] = {0};
  151. char spi_bus_name[20] = {0};
  152. char spi_lock_name[20] = {0};
  153. struct gk_spi_controller *spi_control;
  154. struct gk_spi_controller_data *spi_controller_data = (struct gk_spi_controller_data *)priv_data;
  155. struct gk_spi_slave_info **control_slave;
  156. struct gk_spi_slave_info *spi_slave,*next_slave;
  157. if (!spi_controller_data)
  158. {
  159. rt_kprintf("%s,%d input param wrong!\n",__FUNCTION__,__LINE__);
  160. return -1;
  161. }
  162. spi_control = (struct gk_spi_controller *)rt_malloc(sizeof(struct gk_spi_controller));
  163. if (!spi_control)
  164. {
  165. rt_kprintf("%s,%d malloc failed!\n",__FUNCTION__,__LINE__);
  166. return -1;
  167. }
  168. rt_memset(spi_control, 0, sizeof(struct gk_spi_controller));
  169. spi_control->id = spi_controller_data->id;
  170. rt_sprintf(spi_lock_name, "%s%d", "spi_lock", spi_control->id);
  171. rt_sprintf(spi_bus_name, "%s%d", "spi_bus", spi_control->id);
  172. rt_sem_init(&spi_control->xfer_lock, spi_lock_name, 1, RT_IPC_FLAG_FIFO);
  173. ret = rt_spi_bus_register(&spi_control->spi_bus, spi_bus_name, &gk_spi_ops);
  174. control_slave = &spi_control->spi_slave;
  175. for (i = 0; i < spi_controller_data->total_slave; i++)
  176. {
  177. spi_slave = (struct gk_spi_slave_info *)rt_malloc(sizeof(struct gk_spi_slave_info));
  178. if (!spi_slave)
  179. {
  180. rt_kprintf("%s,%d malloc failed!\n",__FUNCTION__,__LINE__);
  181. goto exit;
  182. }
  183. rt_memset(spi_slave, 0, sizeof(struct gk_spi_slave_info));
  184. spi_slave->id = i;
  185. spi_slave->control = spi_control;
  186. spi_slave->cs_pin = spi_controller_data->slave_cs_pin[i];
  187. spi_slave->spihandle = -1;
  188. rt_sprintf(spi_dev_name, "%s%d%s%d", "ssi", spi_control->id, "_", spi_slave->id);
  189. *control_slave = spi_slave;
  190. control_slave = &spi_slave->next;
  191. ret = rt_spi_bus_attach_device(&spi_slave->spi_device, spi_dev_name,spi_bus_name, spi_slave);
  192. if (ret != RT_EOK)
  193. {
  194. rt_kprintf("register dev to bus failed...\n");
  195. goto exit;
  196. }
  197. }
  198. spi_controller_data->control = spi_control;
  199. GD_SPI_Init();
  200. return RT_EOK;
  201. exit:
  202. spi_slave = spi_control->spi_slave;
  203. while (spi_slave != RT_NULL)
  204. {
  205. next_slave = spi_slave->next;
  206. rt_free(spi_slave);
  207. spi_slave = next_slave;
  208. }
  209. rt_sem_detach(&spi_control->xfer_lock);
  210. rt_free(spi_control);
  211. return RT_ERROR;
  212. }
  213. int gk_spi_exit(void *priv_data)
  214. {
  215. struct gk_spi_controller *spi_control;
  216. struct gk_spi_controller_data *plat_data;
  217. struct gk_spi_slave_info *spi_slave;
  218. struct gk_spi_slave_info *next_slave;
  219. plat_data = (struct gk_spi_controller_data *)priv_data;
  220. spi_control = plat_data->control;
  221. spi_slave = spi_control->spi_slave;
  222. while (spi_slave != RT_NULL)
  223. {
  224. next_slave = spi_slave->next;
  225. rt_free(spi_slave);
  226. spi_slave = next_slave;
  227. }
  228. rt_sem_detach(&spi_control->xfer_lock);
  229. rt_free(spi_control);
  230. return RT_EOK;
  231. }
  232. struct gk_platform_driver gk_spi_driver_ops = {
  233. .name = "spi", .probe = gk_spi_probe, .remove = gk_spi_exit,
  234. };
  235. void rt_hw_spi_init(void)
  236. {
  237. gk_platform_driver_init(&gk_spi_driver_ops);
  238. }