PageRenderTime 88ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/broadcast/fc8050/src/fc8050_spi.c

https://bitbucket.org/thenameisnigel/android_kernel_lge_ls840
C | 385 lines | 268 code | 103 blank | 14 comment | 15 complexity | 4f87b5bf4309a5692827de7b411462a9 MD5 | raw file
  1. /*****************************************************************************
  2. Copyright(c) 2009 FCI Inc. All Rights Reserved
  3. File name : fc8050_spi.c
  4. Description : fc8050 host interface
  5. History :
  6. ----------------------------------------------------------------------
  7. 2009/08/29 jason initial
  8. *******************************************************************************/
  9. #include <linux/input.h>
  10. #include <linux/spi/spi.h>
  11. #include "../inc/broadcast_fc8050.h"
  12. #include "../inc/fci_types.h"
  13. #include "../inc/fc8050_regs.h"
  14. #include "../inc/fci_oal.h"
  15. //#include <plat/regs-gpio.h>
  16. //#include <plat/gpio-cfg.h>
  17. #define DRIVER_NAME "fc8050_spi"
  18. #define HPIC_READ 0x01 // read command
  19. #define HPIC_WRITE 0x02 // write command
  20. #define HPIC_AINC 0x04 // address increment
  21. #define HPIC_BMODE 0x00 // byte mode
  22. #define HPIC_WMODE 0x10 // word mode
  23. #define HPIC_LMODE 0x20 // long mode
  24. #define HPIC_ENDIAN 0x00 // little endian
  25. #define HPIC_CLEAR 0x80 // currently not used
  26. #define CHIPID 0
  27. #if (CHIPID == 0)
  28. #define SPI_CMD_WRITE 0x0
  29. #define SPI_CMD_READ 0x1
  30. #define SPI_CMD_BURST_WRITE 0x2
  31. #define SPI_CMD_BURST_READ 0x3
  32. #else
  33. #define SPI_CMD_WRITE 0x4
  34. #define SPI_CMD_READ 0x5
  35. #define SPI_CMD_BURST_WRITE 0x6
  36. #define SPI_CMD_BURST_READ 0x7
  37. #endif
  38. struct spi_device *fc8050_spi = NULL;
  39. static fci_u8 tx_data[10];
  40. static fci_u8 tdata_buf[40] = {0};
  41. static fci_u8 rdata_buf[8196] = {0};
  42. static DEFINE_MUTEX(lock);
  43. extern struct spi_device *tdmb_fc8050_get_spi_device(void);
  44. int fc8050_spi_write_then_read(struct spi_device *spi, fci_u8 *txbuf, fci_u16 tx_length, fci_u8 *rxbuf, fci_u16 rx_length)
  45. {
  46. fci_s32 res;
  47. struct spi_message message;
  48. struct spi_transfer x;
  49. spi_message_init(&message);
  50. memset(&x, 0, sizeof x);
  51. spi_message_add_tail(&x, &message);
  52. memcpy(tdata_buf, txbuf, tx_length);
  53. x.tx_buf=tdata_buf;
  54. x.rx_buf=rdata_buf;
  55. x.len = tx_length + rx_length;
  56. res = spi_sync(spi, &message);
  57. memcpy(rxbuf, x.rx_buf + tx_length, rx_length);
  58. return res;
  59. }
  60. int fc8050_spi_write_then_read_burst(struct spi_device *spi, fci_u8 *txbuf, fci_u16 tx_length, fci_u8 *rxbuf, fci_u16 rx_length)
  61. {
  62. fci_s32 res;
  63. struct spi_message message;
  64. struct spi_transfer x;
  65. spi_message_init(&message);
  66. memset(&x, 0, sizeof x);
  67. spi_message_add_tail(&x, &message);
  68. x.tx_buf=txbuf;
  69. x.rx_buf=rxbuf;
  70. x.len = tx_length + rx_length;
  71. res = spi_sync(spi, &message);
  72. return res;
  73. }
  74. static int spi_bulkread(HANDLE hDevice, fci_u8 addr, fci_u8 *data, fci_u16 length)
  75. {
  76. fci_s32 ret;
  77. tx_data[0] = SPI_CMD_BURST_READ;
  78. tx_data[1] = addr;
  79. ret = fc8050_spi_write_then_read(fc8050_spi, &tx_data[0], 2, &data[0], length);
  80. if(!ret)
  81. {
  82. PRINTF(0, "fc8050_spi_bulkread fail : %d\n", ret);
  83. return BBM_NOK;
  84. }
  85. return BBM_OK;
  86. }
  87. static int spi_bulkwrite(HANDLE hDevice, fci_u8 addr, fci_u8* data, fci_u16 length)
  88. {
  89. fci_s32 ret;
  90. fci_s32 i;
  91. tx_data[0] = SPI_CMD_BURST_WRITE;
  92. tx_data[1] = addr;
  93. for(i=0;i<length;i++)
  94. {
  95. tx_data[2+i] = data[i];
  96. }
  97. ret =fc8050_spi_write_then_read(fc8050_spi, &tx_data[0], length+2, NULL, 0);
  98. if(!ret)
  99. {
  100. PRINTF(0, "fc8050_spi_bulkwrite fail : %d\n", ret);
  101. return BBM_NOK;
  102. }
  103. return BBM_OK;
  104. }
  105. static int spi_dataread(HANDLE hDevice, fci_u8 addr, fci_u8* data, fci_u16 length)
  106. {
  107. fci_s32 ret=0;
  108. tx_data[0] = SPI_CMD_BURST_READ;
  109. tx_data[1] = addr;
  110. if(length>384)
  111. ret = fc8050_spi_write_then_read_burst(fc8050_spi, &tx_data[0], 2, &data[0], length);
  112. else
  113. ret = fc8050_spi_write_then_read(fc8050_spi, &tx_data[0], 2, &data[0], length);
  114. //printk("spi_dataread (0x%x,0x%x,0x%x,0x%x)\n", data[0], data[1], data[2], data[3]);
  115. if(!ret)
  116. {
  117. PRINTF(0, "fc8050_spi_dataread fail : %d\n", ret);
  118. return BBM_NOK;
  119. }
  120. return BBM_OK;
  121. }
  122. #if 0
  123. static int __devinit fc8050_spi_probe(struct spi_device *spi)
  124. {
  125. fci_s32 ret;
  126. PRINTF(0, "fc8050_spi_probe\n");
  127. spi->max_speed_hz = 4000000;
  128. ret = spi_setup(spi);
  129. if (ret < 0)
  130. return ret;
  131. fc8050_spi = kzalloc(sizeof(struct spi_device), GFP_KERNEL);
  132. if (!fc8050_spi)
  133. return -ENOMEM;
  134. fc8050_spi = spi;
  135. return ret;
  136. }
  137. static int fc8050_spi_remove(struct spi_device *spi)
  138. {
  139. kfree(fc8050_spi);
  140. return 0;
  141. }
  142. static struct spi_driver fc8050_spi_driver = {
  143. .driver = {
  144. .name = DRIVER_NAME,
  145. .owner = THIS_MODULE,
  146. },
  147. .probe = fc8050_spi_probe,
  148. .remove = __devexit_p(fc8050_spi_remove),
  149. };
  150. #endif
  151. int fc8050_spi_init(HANDLE hDevice, fci_u16 param1, fci_u16 param2)
  152. {
  153. #if 0
  154. int res;
  155. res = spi_register_driver(&fc8050_spi_driver);
  156. if(res)
  157. {
  158. PRINTF(0, "fc8050_spi register fail : %d\n", res);
  159. return BBM_NOK;
  160. }
  161. #endif
  162. fc8050_spi = tdmb_fc8050_get_spi_device();
  163. if(fc8050_spi == NULL)
  164. {
  165. printk("spi device is not ready \n");
  166. return BBM_NOK;
  167. }
  168. return BBM_OK;
  169. }
  170. int fc8050_spi_byteread(HANDLE hDevice, fci_u16 addr, fci_u8 *data)
  171. {
  172. int res;
  173. fci_u8 command = HPIC_READ | HPIC_BMODE | HPIC_ENDIAN;
  174. mutex_lock(&lock);
  175. res = spi_bulkwrite(hDevice, BBM_COMMAND_REG, &command, 1);
  176. res |= spi_bulkwrite(hDevice, BBM_ADDRESS_REG, (fci_u8*)&addr, 2);
  177. res |= spi_bulkread(hDevice, BBM_DATA_REG, data, 1);
  178. mutex_unlock(&lock);
  179. return res;
  180. }
  181. int fc8050_spi_wordread(HANDLE hDevice, fci_u16 addr, fci_u16 *data)
  182. {
  183. int res;
  184. fci_u8 command = HPIC_READ | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
  185. if(BBM_SCI_DATA <= addr && BBM_SCI_SYNCRX >= addr)
  186. command = HPIC_READ | HPIC_WMODE | HPIC_ENDIAN;
  187. mutex_lock(&lock);
  188. res = spi_bulkwrite(hDevice, BBM_COMMAND_REG, &command, 1);
  189. res |= spi_bulkwrite(hDevice, BBM_ADDRESS_REG, (fci_u8*)&addr, 2);
  190. res |= spi_bulkread(hDevice, BBM_DATA_REG, (fci_u8*)data, 2);
  191. mutex_unlock(&lock);
  192. return res;
  193. }
  194. int fc8050_spi_longread(HANDLE hDevice, fci_u16 addr, fci_u32 *data)
  195. {
  196. int res;
  197. fci_u8 command = HPIC_READ | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
  198. mutex_lock(&lock);
  199. res = spi_bulkwrite(hDevice, BBM_COMMAND_REG, &command, 1);
  200. res |= spi_bulkwrite(hDevice, BBM_ADDRESS_REG, (fci_u8*)&addr, 2);
  201. res |= spi_bulkread(hDevice, BBM_DATA_REG, (fci_u8*)data, 4);
  202. mutex_unlock(&lock);
  203. return res;
  204. }
  205. int fc8050_spi_bulkread(HANDLE hDevice, fci_u16 addr, fci_u8 *data, fci_u16 length)
  206. {
  207. int res;
  208. fci_u8 command = HPIC_READ | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
  209. mutex_lock(&lock);
  210. res = spi_bulkwrite(hDevice, BBM_COMMAND_REG, &command, 1);
  211. res |= spi_bulkwrite(hDevice, BBM_ADDRESS_REG, (fci_u8*)&addr, 2);
  212. res |= spi_bulkread(hDevice, BBM_DATA_REG, data, length);
  213. mutex_unlock(&lock);
  214. return res;
  215. }
  216. int fc8050_spi_bytewrite(HANDLE hDevice, fci_u16 addr, fci_u8 data)
  217. {
  218. int res;
  219. fci_u8 command = HPIC_WRITE | HPIC_BMODE | HPIC_ENDIAN;
  220. mutex_lock(&lock);
  221. res = spi_bulkwrite(hDevice, BBM_COMMAND_REG, &command, 1);
  222. res |= spi_bulkwrite(hDevice, BBM_ADDRESS_REG, (fci_u8*)&addr, 2);
  223. res |= spi_bulkwrite(hDevice, BBM_DATA_REG, (fci_u8*)&data, 1);
  224. mutex_unlock(&lock);
  225. return res;
  226. }
  227. int fc8050_spi_wordwrite(HANDLE hDevice, fci_u16 addr, fci_u16 data)
  228. {
  229. int res;
  230. fci_u8 command = HPIC_WRITE | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
  231. if(BBM_SCI_DATA <= addr && BBM_SCI_SYNCRX >= addr)
  232. command = HPIC_WRITE | HPIC_WMODE | HPIC_ENDIAN;
  233. mutex_lock(&lock);
  234. res = spi_bulkwrite(hDevice, BBM_COMMAND_REG, &command, 1);
  235. res |= spi_bulkwrite(hDevice, BBM_ADDRESS_REG, (fci_u8*)&addr, 2);
  236. res |= spi_bulkwrite(hDevice, BBM_DATA_REG, (fci_u8*)&data, 2);
  237. mutex_unlock(&lock);
  238. return res;
  239. }
  240. int fc8050_spi_longwrite(HANDLE hDevice, fci_u16 addr, fci_u32 data)
  241. {
  242. int res;
  243. fci_u8 command = HPIC_WRITE | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
  244. mutex_lock(&lock);
  245. res = spi_bulkwrite(hDevice, BBM_COMMAND_REG, &command, 1);
  246. res |= spi_bulkwrite(hDevice, BBM_ADDRESS_REG, (fci_u8*)&addr, 2);
  247. res |= spi_bulkwrite(hDevice, BBM_DATA_REG, (fci_u8*)&data, 4);
  248. mutex_unlock(&lock);
  249. return res;
  250. }
  251. int fc8050_spi_bulkwrite(HANDLE hDevice, fci_u16 addr, fci_u8* data, fci_u16 length)
  252. {
  253. int res;
  254. fci_u8 command = HPIC_WRITE | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
  255. mutex_lock(&lock);
  256. res = spi_bulkwrite(hDevice, BBM_COMMAND_REG, &command, 1);
  257. res |= spi_bulkwrite(hDevice, BBM_ADDRESS_REG, (fci_u8*)&addr, 2);
  258. res |= spi_bulkwrite(hDevice, BBM_DATA_REG, data, length);
  259. mutex_unlock(&lock);
  260. return res;
  261. }
  262. int fc8050_spi_dataread(HANDLE hDevice, fci_u16 addr, fci_u8* data, fci_u16 length)
  263. {
  264. int res;
  265. fci_u8 command = HPIC_READ | HPIC_BMODE | HPIC_ENDIAN;
  266. mutex_lock(&lock);
  267. res = spi_bulkwrite(hDevice, BBM_COMMAND_REG, &command, 1);
  268. res |= spi_bulkwrite(hDevice, BBM_ADDRESS_REG, (fci_u8*)&addr, 2);
  269. res |= spi_dataread(hDevice, BBM_DATA_REG, data, length);
  270. mutex_unlock(&lock);
  271. return res;
  272. }
  273. int fc8050_spi_deinit(HANDLE hDevice)
  274. {
  275. #if 0
  276. spi_unregister_driver(&fc8050_spi_driver);
  277. #endif
  278. return BBM_OK;
  279. }