mt_i2c.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 5 /* 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 12000 /* FPGA crystal frequency (KHz) */
  19. #define I2C_CLK_DIV 5 /* frequency divisor */
  20. #define I2C_CLK_RATE (FPGA_CLOCK / I2C_CLK_DIV) /* I2C base clock (KHz) */
  21. #else
  22. #define I2C_CLK_DIV 10 /* frequency divisor */
  23. #define I2C_CLK_RATE 13600 /* TODO: Calculate from bus clock */
  24. #endif
  25. #define I2C_M_RD 0x0001
  26. #ifndef I2C0_BASE
  27. #define I2C0_BASE (0x11007000)
  28. #endif
  29. #ifndef I2C1_BASE
  30. #define I2C1_BASE (0x11008000)
  31. #endif
  32. #ifndef I2C2_BASE
  33. #define I2C2_BASE (0x11009000)
  34. #endif
  35. #ifndef I2C3_BASE
  36. #define I2C3_BASE (0x1100F000)
  37. #endif
  38. #ifndef I2C4_BASE
  39. #define I2C4_BASE (0x11011000)
  40. #endif
  41. #define I2C_OK 0x0000
  42. #define EAGAIN_I2C 11 /* Try again */
  43. #define EINVAL_I2C 22 /* Invalid argument */
  44. #define EOPNOTSUPP_I2C 95 /* Operation not supported on transport endpoint */
  45. #define ETIMEDOUT_I2C 110 /* Connection timed out */
  46. #define EREMOTEIO_I2C 121 /* Remote I/O error */
  47. #define ENOTSUPP_I2C 524 /* Remote I/O error */
  48. #define I2C_WRITE_FAIL_HS_NACKERR 0xA013
  49. #define I2C_WRITE_FAIL_ACKERR 0xA014
  50. #define I2C_WRITE_FAIL_TIMEOUT 0xA015
  51. //#define I2C_CLK_WRAPPER_RATE 36000 /* kHz for wrapper I2C work frequency */
  52. /******************************************register operation***********************************/
  53. enum I2C_REGS_OFFSET {
  54. OFFSET_DATA_PORT = 0x0, //0x0
  55. OFFSET_SLAVE_ADDR = 0x04, //0x04
  56. OFFSET_INTR_MASK = 0x08, //0x08
  57. OFFSET_INTR_STAT = 0x0C, //0x0C
  58. OFFSET_CONTROL = 0x10, //0X10
  59. OFFSET_TRANSFER_LEN = 0x14, //0X14
  60. OFFSET_TRANSAC_LEN = 0x18, //0X18
  61. OFFSET_DELAY_LEN = 0x1C, //0X1C
  62. OFFSET_TIMING = 0x20, //0X20
  63. OFFSET_START = 0x24, //0X24
  64. OFFSET_EXT_CONF = 0x28,
  65. OFFSET_FIFO_STAT = 0x30, //0X30
  66. OFFSET_FIFO_THRESH = 0x34, //0X34
  67. OFFSET_FIFO_ADDR_CLR = 0x38, //0X38
  68. OFFSET_IO_CONFIG = 0x40, //0X40
  69. OFFSET_RSV_DEBUG = 0x44, //0X44
  70. OFFSET_HS = 0x48, //0X48
  71. OFFSET_SOFTRESET = 0x50, //0X50
  72. OFFSET_DCM_EN = 0x54, //0X54
  73. OFFSET_DEBUGSTAT = 0x64, //0X64
  74. OFFSET_DEBUGCTRL = 0x68, //0x68
  75. OFFSET_TRANSFER_LEN_AUX = 0x6C, //0x6C
  76. };
  77. #define I2C_HS_NACKERR (1 << 2)
  78. #define I2C_ACKERR (1 << 1)
  79. #define I2C_TRANSAC_COMP (1 << 0)
  80. #define I2C_FIFO_SIZE 8
  81. #define MAX_ST_MODE_SPEED 100 /* khz */
  82. #define MAX_FS_MODE_SPEED 400 /* khz */
  83. #define MAX_HS_MODE_SPEED 3400 /* khz */
  84. #define MAX_DMA_TRANS_SIZE 65532 /* Max(65535) aligned to 4 bytes = 65532 */
  85. #define MAX_DMA_TRANS_NUM 256
  86. #define MAX_SAMPLE_CNT_DIV 8
  87. #define MAX_STEP_CNT_DIV 64
  88. #define MAX_HS_STEP_CNT_DIV 8
  89. #define DMA_ADDRESS_HIGH (0xC0000000)
  90. enum DMA_REGS_OFFSET {
  91. OFFSET_INT_FLAG = 0x0,
  92. OFFSET_INT_EN = 0x04,
  93. OFFSET_EN = 0x08,
  94. OFFSET_RST = 0x0C,
  95. OFFSET_CON = 0x18,
  96. OFFSET_TX_MEM_ADDR = 0x1C,
  97. OFFSET_RX_MEM_ADDR = 0x20,
  98. OFFSET_TX_LEN = 0x24,
  99. OFFSET_RX_LEN = 0x28,
  100. };
  101. enum i2c_trans_st_rs {
  102. I2C_TRANS_STOP = 0,
  103. I2C_TRANS_REPEATED_START,
  104. };
  105. typedef enum {
  106. ST_MODE,
  107. FS_MODE,
  108. HS_MODE,
  109. } I2C_SPEED_MODE;
  110. enum mt_trans_op {
  111. I2C_MASTER_NONE = 0,
  112. I2C_MASTER_WR = 1,
  113. I2C_MASTER_RD,
  114. I2C_MASTER_WRRD,
  115. };
  116. //CONTROL
  117. #define I2C_CONTROL_RS (0x1 << 1)
  118. #define I2C_CONTROL_DMA_EN (0x1 << 2)
  119. #define I2C_CONTROL_CLK_EXT_EN (0x1 << 3)
  120. #define I2C_CONTROL_DIR_CHANGE (0x1 << 4)
  121. #define I2C_CONTROL_ACKERR_DET_EN (0x1 << 5)
  122. #define I2C_CONTROL_TRANSFER_LEN_CHANGE (0x1 << 6)
  123. #define I2C_CONTROL_WRAPPER (0x1 << 0)
  124. /***********************************end of register operation****************************************/
  125. /***********************************I2C Param********************************************************/
  126. struct mt_trans_data {
  127. U16 trans_num;
  128. U16 data_size;
  129. U16 trans_len;
  130. U16 trans_auxlen;
  131. };
  132. typedef struct mt_i2c_t {
  133. #ifdef I2C_DRIVER_IN_KERNEL
  134. //==========only used in kernel================//
  135. struct i2c_adapter adap; /* i2c host adapter */
  136. struct device *dev; /* the device object of i2c host adapter */
  137. atomic_t trans_err; /* i2c transfer error */
  138. atomic_t trans_comp; /* i2c transfer completion */
  139. atomic_t trans_stop; /* i2c transfer stop */
  140. spinlock_t lock; /* for mt_i2c struct protection */
  141. wait_queue_head_t wait; /* i2c transfer wait queue */
  142. #endif
  143. //==========set in i2c probe============//
  144. U32 base; /* i2c base addr */
  145. U16 id;
  146. U16 irqnr; /* i2c interrupt number */
  147. U16 irq_stat; /* i2c interrupt status */
  148. U32 clk; /* host clock speed in khz */
  149. U32 pdn; /*clock number*/
  150. //==========common data define============//
  151. enum i2c_trans_st_rs st_rs;
  152. enum mt_trans_op op;
  153. U32 pdmabase;
  154. U32 speed; //The speed (khz)
  155. U16 delay_len; //number of half pulse between transfers in a trasaction
  156. U32 msg_len; //number of half pulse between transfers in a trasaction
  157. U8 *msg_buf; /* pointer to msg data */
  158. U8 addr; //The address of the slave device, 7bit,the value include read/write bit.
  159. U8 master_code;/* master code in HS mode */
  160. U8 mode; /* ST/FS/HS mode */
  161. //==========reserved funtion============//
  162. U8 is_push_pull_enable; //IO push-pull or open-drain
  163. U8 is_clk_ext_disable; //clk entend default enable
  164. U8 is_dma_enabled; //Transaction via DMA instead of 8-byte FIFO
  165. U8 read_flag;//read,write,read_write
  166. BOOL dma_en;
  167. BOOL poll_en;
  168. BOOL pushpull;//open drain
  169. BOOL filter_msg;//filter msg error log
  170. BOOL i2c_3dcamera_flag;//flag for 3dcamera
  171. //==========define reg============//
  172. U16 timing_reg;
  173. U16 high_speed_reg;
  174. U16 control_reg;
  175. struct mt_trans_data trans_data;
  176. } mt_i2c;
  177. //===========================i2c old driver===================================================//
  178. enum {
  179. I2C0,
  180. I2C1,
  181. I2C2,
  182. I2C_APPM,
  183. I2C3,
  184. };
  185. //==============================================================================
  186. // I2C Exported Function
  187. //==============================================================================
  188. extern S32 i2c_read(mt_i2c *i2c,U8 *buffer, U32 len);
  189. extern S32 i2c_write(mt_i2c *i2c,U8 *buffer, U32 len);
  190. extern S32 i2c_write_read(mt_i2c *i2c,U8 *buffer, U32 write_len, U32 read_len);
  191. extern S32 i2c_set_speed(mt_i2c *i2c);
  192. extern int i2c_hw_init(void);
  193. //#define I2C_EARLY_PORTING
  194. #ifdef I2C_EARLY_PORTING
  195. int mt_i2c_test(void);
  196. #endif
  197. #endif /* __MT_I2C_H__ */