fc8150_i2c.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*****************************************************************************
  2. Copyright(c) 2012 FCI Inc. All Rights Reserved
  3. File name : fc8150_i2c.c
  4. Description : fc8150 host interface
  5. *******************************************************************************/
  6. #include "fci_types.h"
  7. #include "fc8150_regs.h"
  8. #include "fci_oal.h"
  9. #include "fci_hal.h"
  10. #define HPIC_READ 0x01 /* read command */
  11. #define HPIC_WRITE 0x02 /* write command */
  12. #define HPIC_AINC 0x04 /* address increment */
  13. #define HPIC_BMODE 0x00 /* byte mode */
  14. #define HPIC_WMODE 0x10 /* word mode */
  15. #define HPIC_LMODE 0x20 /* long mode */
  16. #define HPIC_ENDIAN 0x00 /* little endian */
  17. #define HPIC_CLEAR 0x80 /* currently not used */
  18. #define CHIP_ADDR 0x58
  19. static int i2c_bulkread(HANDLE hDevice, u8 chip, u8 addr, u8 *data, u16 length)
  20. {
  21. /* Write your own i2c driver code here for read operation. */
  22. return BBM_OK;
  23. }
  24. static int i2c_bulkwrite(HANDLE hDevice, u8 chip, u8 addr, u8 *data, u16 length)
  25. {
  26. /* Write your own i2c driver code here for Write operation. */
  27. return BBM_OK;
  28. }
  29. static int i2c_dataread(HANDLE hDevice, u8 chip, u8 addr, u8 *data, u32 length)
  30. {
  31. return i2c_bulkread(hDevice, chip, addr, data, length);
  32. }
  33. int fc8150_bypass_read(HANDLE hDevice, u8 chip, u8 addr, u8 *data, u16 length)
  34. {
  35. int res;
  36. u8 bypass_addr = 0x03;
  37. u8 bypass_data = 1;
  38. u8 bypass_len = 1;
  39. OAL_OBTAIN_SEMAPHORE();
  40. res = i2c_bulkwrite(hDevice, CHIP_ADDR, bypass_addr
  41. , &bypass_data, bypass_len);
  42. res |= i2c_bulkread(hDevice, chip, addr, data, length);
  43. OAL_RELEASE_SEMAPHORE();
  44. return res;
  45. }
  46. int fc8150_bypass_write(HANDLE hDevice, u8 chip, u8 addr, u8 *data, u16 length)
  47. {
  48. int res;
  49. u8 bypass_addr = 0x03;
  50. u8 bypass_data = 1;
  51. u8 bypass_len = 1;
  52. OAL_OBTAIN_SEMAPHORE();
  53. res = i2c_bulkwrite(hDevice, CHIP_ADDR, bypass_addr
  54. , &bypass_data, bypass_len);
  55. res |= i2c_bulkwrite(hDevice, chip, addr, data, length);
  56. OAL_RELEASE_SEMAPHORE();
  57. return res;
  58. }
  59. int fc8150_i2c_init(HANDLE hDevice, u16 param1, u16 param2)
  60. {
  61. OAL_CREATE_SEMAPHORE();
  62. /* for TSIF, you can call here your own TSIF initialization function. */
  63. /* tsif_initialize(); */
  64. bbm_write(hDevice, BBM_TS_CLK_DIV, 0x04);
  65. bbm_write(hDevice, BBM_TS_PAUSE, 0x80);
  66. bbm_write(hDevice, BBM_TS_CTRL, 0x02);
  67. bbm_write(hDevice, BBM_TS_SEL, 0x84);
  68. return BBM_OK;
  69. }
  70. int fc8150_i2c_byteread(HANDLE hDevice, u16 addr, u8 *data)
  71. {
  72. int res;
  73. u8 command = HPIC_READ | HPIC_BMODE | HPIC_ENDIAN;
  74. OAL_OBTAIN_SEMAPHORE();
  75. res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG
  76. , (u8 *)&addr, 2);
  77. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
  78. res |= i2c_bulkread(hDevice, CHIP_ADDR, BBM_DATA_REG, data, 1);
  79. OAL_RELEASE_SEMAPHORE();
  80. return res;
  81. }
  82. int fc8150_i2c_wordread(HANDLE hDevice, u16 addr, u16 *data)
  83. {
  84. int res;
  85. u8 command = HPIC_READ | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
  86. OAL_OBTAIN_SEMAPHORE();
  87. res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG
  88. , (u8 *)&addr, 2);
  89. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
  90. res |= i2c_bulkread(hDevice, CHIP_ADDR, BBM_DATA_REG, (u8 *)data, 2);
  91. OAL_RELEASE_SEMAPHORE();
  92. return res;
  93. }
  94. int fc8150_i2c_longread(HANDLE hDevice, u16 addr, u32 *data)
  95. {
  96. int res;
  97. u8 command = HPIC_READ | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
  98. OAL_OBTAIN_SEMAPHORE();
  99. res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG
  100. , (u8 *)&addr, 2);
  101. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
  102. res |= i2c_bulkread(hDevice, CHIP_ADDR, BBM_DATA_REG, (u8 *)data, 4);
  103. OAL_RELEASE_SEMAPHORE();
  104. return res;
  105. }
  106. int fc8150_i2c_bulkread(HANDLE hDevice, u16 addr, u8 *data, u16 length)
  107. {
  108. int res;
  109. u8 command = HPIC_READ | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
  110. OAL_OBTAIN_SEMAPHORE();
  111. res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG
  112. , (u8 *)&addr, 2);
  113. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
  114. res |= i2c_bulkread(hDevice, CHIP_ADDR, BBM_DATA_REG, data, length);
  115. OAL_RELEASE_SEMAPHORE();
  116. return res;
  117. }
  118. int fc8150_i2c_bytewrite(HANDLE hDevice, u16 addr, u8 data)
  119. {
  120. int res;
  121. u8 command = HPIC_WRITE | HPIC_BMODE | HPIC_ENDIAN;
  122. OAL_OBTAIN_SEMAPHORE();
  123. res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG
  124. , (u8 *)&addr, 2);
  125. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
  126. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_DATA_REG, (u8 *)&data, 1);
  127. OAL_RELEASE_SEMAPHORE();
  128. return res;
  129. }
  130. int fc8150_i2c_wordwrite(HANDLE hDevice, u16 addr, u16 data)
  131. {
  132. int res;
  133. u8 command = HPIC_WRITE | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
  134. OAL_OBTAIN_SEMAPHORE();
  135. res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG
  136. , (u8 *)&addr, 2);
  137. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
  138. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_DATA_REG, (u8 *)&data, 2);
  139. OAL_RELEASE_SEMAPHORE();
  140. return res;
  141. }
  142. int fc8150_i2c_longwrite(HANDLE hDevice, u16 addr, u32 data)
  143. {
  144. int res;
  145. u8 command = HPIC_WRITE | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
  146. OAL_OBTAIN_SEMAPHORE();
  147. res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG
  148. , (u8 *)&addr, 2);
  149. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
  150. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_DATA_REG, (u8 *)&data, 4);
  151. OAL_RELEASE_SEMAPHORE();
  152. return res;
  153. }
  154. int fc8150_i2c_bulkwrite(HANDLE hDevice, u16 addr, u8 *data, u16 length)
  155. {
  156. int res;
  157. u8 command = HPIC_WRITE | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
  158. OAL_OBTAIN_SEMAPHORE();
  159. res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG
  160. , (u8 *)&addr, 2);
  161. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
  162. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_DATA_REG, data, length);
  163. OAL_RELEASE_SEMAPHORE();
  164. return res;
  165. }
  166. int fc8150_i2c_dataread(HANDLE hDevice, u16 addr, u8 *data, u32 length)
  167. {
  168. int res;
  169. u8 command = HPIC_READ | HPIC_BMODE | HPIC_ENDIAN;
  170. OAL_OBTAIN_SEMAPHORE();
  171. res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG
  172. , (u8 *)&addr, 2);
  173. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
  174. res |= i2c_dataread(hDevice, CHIP_ADDR, BBM_DATA_REG, data, length);
  175. OAL_RELEASE_SEMAPHORE();
  176. return res;
  177. }
  178. int fc8150_i2c_deinit(HANDLE hDevice)
  179. {
  180. bbm_write(hDevice, BBM_TS_SEL, 0x00);
  181. /* tsif_disable(); */
  182. OAL_DELETE_SEMAPHORE();
  183. return BBM_OK;
  184. }