mt_i2c.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #ifndef __MT_I2C_H__
  2. #define __MT_I2C_H__
  3. #include <platform/mt_typedefs.h>
  4. #include <platform/mt_reg_base.h>
  5. #include <debug.h>
  6. #include <platform/sync_write.h>
  7. #include <sys/types.h>
  8. #define I2C_NR 12 /* Number of I2C controllers */
  9. #define I2CTAG "[I2C-LK] "
  10. #define I2CLOG(fmt, arg...) dprintf(INFO,I2CTAG fmt, ##arg)
  11. #define I2CMSG(fmt, arg...) dprintf(SPEW,I2CTAG fmt, ##arg)
  12. #define I2CERR(fmt, arg...) dprintf(ALWAYS,I2CTAG "%d: "fmt, __LINE__, ##arg)
  13. #define I2CFUC() dprintf(ALWAYS,I2CTAG "%s\n", __FUNCTION__)
  14. #ifdef MACH_FPGA
  15. #define CONFIG_MT_I2C_FPGA_ENABLE
  16. #endif
  17. #ifdef CONFIG_MT_I2C_FPGA_ENABLE
  18. #define FPGA_CLOCK 10000 /* FPGA crystal frequency (KHz) */
  19. #define I2C_CLK_DIV (5*2) /* frequency divisor */
  20. #define I2C_CLK_RATE (FPGA_CLOCK / I2C_CLK_DIV) /* I2C base clock (KHz) */
  21. #define SCP_I2C_CLK (FPGA_CLOCK / 16)
  22. #else
  23. #define I2C_CLK_RATE 124800 / I2C_CLK_DIV /* TODO: Calculate from bus clock */
  24. #define I2C_CLK_DIV (5) /* frequency divisor */
  25. #define SCP_I2C_CLK (26000 / 2) /* TODO: Check the correct frequency */
  26. #endif
  27. #define I2C_MB()
  28. #define I2C_BUG_ON(a)
  29. #define I2C_M_RD 0x0001
  30. #ifndef I2C0_BASE
  31. #define I2C0_BASE (0x11007000)
  32. #endif
  33. #ifndef I2C1_BASE
  34. #define I2C1_BASE (0x11011000)
  35. #endif
  36. #ifndef I2C2_BASE
  37. #define I2C2_BASE (0x11009100)
  38. #endif
  39. #ifndef I2C3_BASE
  40. #define I2C3_BASE (0x1100F000)
  41. #endif
  42. #ifndef I2C4_BASE
  43. #define I2C4_BASE (0x11008100)
  44. #endif
  45. #ifndef I2C5_BASE
  46. #define I2C5_BASE (0x11017000)
  47. #endif
  48. #ifndef I2C6_BASE
  49. #define I2C6_BASE (0x11005000)
  50. #endif
  51. #ifndef I2C7_BASE
  52. #define I2C7_BASE (0x1101A000)
  53. #endif
  54. #ifndef I2C8_BASE
  55. #define I2C8_BASE (0x1101B000)
  56. #endif
  57. #ifndef I2C9_BASE
  58. #define I2C9_BASE (0x11014000)
  59. #endif
  60. #ifndef I2C10_BASE
  61. #define I2C10_BASE (0x11015000)
  62. #endif
  63. #ifndef I2C11_BASE
  64. #define I2C11_BASE (0x11016000)
  65. #endif
  66. #define I2C0_APDMA_BASE (0x11000080)
  67. #define I2C1_APDMA_BASE (0x11000480)
  68. #define I2C2_APDMA_BASE (0x11000280)
  69. #define I2C3_APDMA_BASE (0x11000400)
  70. #define I2C4_APDMA_BASE (0x11000100)
  71. #define I2C5_APDMA_BASE (0x11000580)
  72. #define I2C6_APDMA_BASE (0x11000600)
  73. #define I2C7_APDMA_BASE (0x11000680)
  74. #define I2C8_APDMA_BASE (0x11000700)
  75. #define I2C9_APDMA_BASE (0x11000180)
  76. #define I2C10_APDMA_BASE (0x11000300)
  77. #define I2C11_APDMA_BASE (0x11000500)
  78. #define I2C_OK 0x0000
  79. #define EAGAIN_I2C 11 /* Try again */
  80. #define EINVAL_I2C 22 /* Invalid argument */
  81. #define EOPNOTSUPP_I2C 95 /* Operation not supported on transport endpoint */
  82. #define ETIMEDOUT_I2C 110 /* Connection timed out */
  83. #define EREMOTEIO_I2C 121 /* Remote I/O error */
  84. #define ENOTSUPP_I2C 524 /* Remote I/O error */
  85. #define I2C_WRITE_FAIL_HS_NACKERR 0xA013
  86. #define I2C_WRITE_FAIL_ACKERR 0xA014
  87. #define I2C_WRITE_FAIL_TIMEOUT 0xA015
  88. #define DUTY_CYCLE 45
  89. /* offset is different in mt6771 */
  90. /******************************************register operation***********************************/
  91. enum I2C_REGS_OFFSET {
  92. OFFSET_DATA_PORT = 0x0,
  93. OFFSET_SLAVE_ADDR = 0x04,
  94. OFFSET_INTR_MASK = 0x08,
  95. OFFSET_INTR_STAT = 0x0C,
  96. OFFSET_CONTROL = 0x10,
  97. OFFSET_TRANSFER_LEN = 0x14,
  98. OFFSET_TRANSAC_LEN = 0x18,
  99. OFFSET_DELAY_LEN = 0x1C,
  100. OFFSET_HTIMING = 0x20,
  101. OFFSET_START = 0x24,
  102. OFFSET_EXT_CONF = 0x28,
  103. OFFSET_LTIMING = 0x2C,
  104. OFFSET_HS = 0x30,
  105. OFFSET_IO_CONFIG = 0x34,
  106. OFFSET_FIFO_ADDR_CLR = 0x38,
  107. OFFSET_TRANSFER_LEN_AUX = 0x44,
  108. OFFSET_CLOCK_DIV = 0x48,
  109. OFFSET_SOFTRESET = 0x50,
  110. OFFSET_DEBUGSTAT = 0xe4,
  111. OFFSET_DEBUGCTRL = 0xe8,
  112. OFFSET_FIFO_STAT = 0xf4,
  113. OFFSET_FIFO_THRESH = 0xf8,
  114. OFFSET_DCM_EN = 0xf88,
  115. };
  116. #define I2C_HS_NACKERR (1 << 2)
  117. #define I2C_ACKERR (1 << 1)
  118. #define I2C_TRANSAC_COMP (1 << 0)
  119. #define I2C_FIFO_SIZE 8
  120. #define MAX_ST_MODE_SPEED 100 /* khz */
  121. #define MAX_FS_MODE_SPEED 400 /* khz */
  122. #define MAX_HS_MODE_SPEED 3400 /* khz */
  123. #define MAX_DMA_TRANS_SIZE 65532 /* Max(65535) aligned to 4 bytes = 65532 */
  124. #define MAX_DMA_TRANS_NUM 256
  125. #define MAX_SAMPLE_CNT_DIV 8
  126. #define MAX_STEP_CNT_DIV 64
  127. #define MAX_HS_STEP_CNT_DIV 8
  128. #define DMA_ADDRESS_HIGH (0xC0000000)
  129. enum DMA_REGS_OFFSET {
  130. OFFSET_INT_FLAG = 0x0,
  131. OFFSET_INT_EN = 0x04,
  132. OFFSET_EN = 0x08,
  133. OFFSET_RST = 0x0C,
  134. OFFSET_CON = 0x18,
  135. OFFSET_TX_MEM_ADDR = 0x1C,
  136. OFFSET_RX_MEM_ADDR = 0x20,
  137. OFFSET_TX_LEN = 0x24,
  138. OFFSET_RX_LEN = 0x28,
  139. };
  140. enum i2c_trans_st_rs {
  141. I2C_TRANS_STOP = 0,
  142. I2C_TRANS_REPEATED_START,
  143. };
  144. typedef enum {
  145. ST_MODE,
  146. FS_MODE,
  147. HS_MODE,
  148. } I2C_SPEED_MODE;
  149. enum mt_trans_op {
  150. I2C_MASTER_NONE = 0,
  151. I2C_MASTER_WR = 1,
  152. I2C_MASTER_RD,
  153. I2C_MASTER_WRRD,
  154. };
  155. //CONTROL
  156. #define I2C_CONTROL_RS (0x1 << 1)
  157. #define I2C_CONTROL_DMA_EN (0x1 << 2)
  158. #define I2C_CONTROL_CLK_EXT_EN (0x1 << 3)
  159. #define I2C_CONTROL_DIR_CHANGE (0x1 << 4)
  160. #define I2C_CONTROL_ACKERR_DET_EN (0x1 << 5)
  161. #define I2C_CONTROL_TRANSFER_LEN_CHANGE (0x1 << 6)
  162. #define I2C_CONTROL_WRAPPER (0x1 << 0)
  163. /***********************************end of register operation****************************************/
  164. /***********************************I2C Param********************************************************/
  165. struct mt_trans_data {
  166. U16 trans_num;
  167. U16 data_size;
  168. U16 trans_len;
  169. U16 trans_auxlen;
  170. };
  171. typedef struct mt_i2c_t {
  172. //==========set in i2c probe============//
  173. U32 base; /* i2c base addr */
  174. U16 id;
  175. U16 irqnr; /* i2c interrupt number */
  176. U16 irq_stat; /* i2c interrupt status */
  177. U32 clk; /* host clock speed in khz */
  178. U32 pdn; /*clock number*/
  179. //==========common data define============//
  180. enum i2c_trans_st_rs st_rs;
  181. enum mt_trans_op op;
  182. U32 pdmabase;
  183. U32 speed; //The speed (khz)
  184. U16 delay_len; //number of half pulse between transfers in a trasaction
  185. U32 msg_len; //number of half pulse between transfers in a trasaction
  186. U8 *msg_buf; /* pointer to msg data */
  187. U8 addr; //The address of the slave device, 7bit,the value include read/write bit.
  188. U8 master_code;/* master code in HS mode */
  189. U8 mode; /* ST/FS/HS mode */
  190. //==========reserved funtion============//
  191. U8 is_push_pull_enable; //IO push-pull or open-drain
  192. U8 is_clk_ext_disable; //clk entend default enable
  193. U8 is_dma_enabled; //Transaction via DMA instead of 8-byte FIFO
  194. U8 read_flag;//read,write,read_write
  195. BOOL dma_en;
  196. BOOL poll_en;
  197. BOOL pushpull;//open drain
  198. BOOL filter_msg;//filter msg error log
  199. BOOL i2c_3dcamera_flag;//flag for 3dcamera
  200. //==========define reg============//
  201. U16 htiming_reg;
  202. U16 ltiming_reg;
  203. U16 high_speed_reg;
  204. U16 control_reg;
  205. struct mt_trans_data trans_data;
  206. } mt_i2c;
  207. //===========================i2c old driver===================================================//
  208. enum {
  209. I2C0 = 0,
  210. I2C1 = 1,
  211. I2C2 = 2,
  212. I2C3 = 3,
  213. I2C4 = 4,
  214. I2C5 = 5,
  215. I2C6 = 6,
  216. I2C7 = 7,
  217. I2C8 = 8,
  218. I2C9 = 9,
  219. I2C10 = 10,
  220. I2C11 = 11,
  221. };
  222. //==============================================================================
  223. // I2C Exported Function
  224. //==============================================================================
  225. extern S32 i2c_read(mt_i2c *i2c,U8 *buffer, U32 len);
  226. extern S32 i2c_write(mt_i2c *i2c,U8 *buffer, U32 len);
  227. extern S32 i2c_write_read(mt_i2c *i2c,U8 *buffer, U32 write_len, U32 read_len);
  228. extern S32 i2c_set_speed(mt_i2c *i2c);
  229. extern int i2c_hw_init(void);
  230. //#define I2C_EARLY_PORTING
  231. #ifdef I2C_EARLY_PORTING
  232. int mt_i2c_test(void);
  233. #endif
  234. #endif /* __MT_I2C_H__ */