mt_i2c.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #ifndef __MT_I2C_H__
  2. #define __MT_I2C_H__
  3. #include <linux/i2c.h>
  4. #include <linux/types.h>
  5. #if !defined(CONFIG_MTK_CLKMGR)
  6. #include <linux/clk.h>
  7. #endif /* !defined(CONFIG_MTK_CLKMGR) */
  8. #define I2CTAG "[I2C]"
  9. /* #define pr_fmt(fmt) "[I2C]"fmt */
  10. #define I2CLOG(fmt, arg...) pr_err(fmt, ##arg)
  11. #define I2CMSG(fmt, arg...) pr_debug(fmt, ##arg)
  12. #define I2CERR(fmt, arg...) pr_err("ERROR,%d: "fmt, __LINE__, ##arg)
  13. #define I2CFUC(fmt, arg...) pr_debug("%s\n", __func__)
  14. #define I2C_DRV_NAME "mt-i2c"
  15. #define I2C_NR 4
  16. #define I2C_DRIVER_IN_KERNEL
  17. #ifdef I2C_DRIVER_IN_KERNEL
  18. #define I2C_MB() mb() /* foo */
  19. /* #define I2C_DEBUG */
  20. #ifdef I2C_DEBUG
  21. #define I2C_BUG_ON(a) BUG_ON(a)
  22. #else
  23. #define I2C_BUG_ON(a)
  24. #endif
  25. #ifdef CONFIG_MTK_FPGA
  26. #define CONFIG_MT_I2C_FPGA_ENABLE
  27. #endif
  28. #if (defined(CONFIG_MT_I2C_FPGA_ENABLE))
  29. #define FPGA_CLOCK 12000 /* FPGA crystal frequency (KHz) */
  30. #define I2C_CLK_DIV 5 /* frequency divider */
  31. #define I2C_CLK_RATE (FPGA_CLOCK / I2C_CLK_DIV) /* kHz for FPGA I2C work frequency */
  32. #else
  33. #define I2C_CLK_RATE 13600
  34. #endif
  35. #else
  36. #define I2C_MB()
  37. #define I2C_BUG_ON(a)
  38. #define I2C_M_RD 0x0001
  39. #endif
  40. #define I2C_OK 0x0000
  41. #define EAGAIN_I2C 11 /* Try again */
  42. #define EINVAL_I2C 22 /* Invalid argument */
  43. #define EOPNOTSUPP_I2C 95 /* Operation not supported on transport endpoint */
  44. #define ETIMEDOUT_I2C 110 /* Connection timed out */
  45. #define EREMOTEIO_I2C 121 /* Remote I/O error */
  46. #define ENOTSUPP_I2C 524 /* Remote I/O error */
  47. #define I2C_WRITE_FAIL_HS_NACKERR 0xA013
  48. #define I2C_WRITE_FAIL_ACKERR 0xA014
  49. #define I2C_WRITE_FAIL_TIMEOUT 0xA015
  50. /* enum for different I2C pins */
  51. enum {
  52. I2C0 = 0,
  53. I2C1,
  54. I2C2,
  55. I2C3,
  56. I2C4,
  57. };
  58. /******************************************register operation***********************************/
  59. enum I2C_REGS_OFFSET {
  60. OFFSET_DATA_PORT = 0x0,
  61. OFFSET_SLAVE_ADDR = 0x04,
  62. OFFSET_INTR_MASK = 0x08,
  63. OFFSET_INTR_STAT = 0x0C,
  64. OFFSET_CONTROL = 0x10,
  65. OFFSET_TRANSFER_LEN = 0x14,
  66. OFFSET_TRANSAC_LEN = 0x18,
  67. OFFSET_DELAY_LEN = 0x1C,
  68. OFFSET_TIMING = 0x20,
  69. OFFSET_START = 0x24,
  70. OFFSET_EXT_CONF = 0x28,
  71. OFFSET_FIFO_STAT = 0x30,
  72. OFFSET_FIFO_THRESH = 0x34,
  73. OFFSET_FIFO_ADDR_CLR = 0x38,
  74. OFFSET_IO_CONFIG = 0x40,
  75. OFFSET_RSV_DEBUG = 0x44,
  76. OFFSET_HS = 0x48,
  77. OFFSET_SOFTRESET = 0x50,
  78. OFFSET_DCM_EN = 0x54,
  79. OFFSET_DEBUGSTAT = 0x64,
  80. OFFSET_DEBUGCTRL = 0x68,
  81. OFFSET_TRANSFER_LEN_AUX = 0x6C,
  82. };
  83. #define I2C_HS_NACKERR (1 << 2)
  84. #define I2C_ACKERR (1 << 1)
  85. #define I2C_TRANSAC_COMP (1 << 0)
  86. #define I2C_FIFO_SIZE 8
  87. #define MAX_ST_MODE_SPEED 100 /* khz */
  88. #define MAX_FS_MODE_SPEED 400 /* khz */
  89. #define MAX_HS_MODE_SPEED 3400 /* khz */
  90. #define MAX_DMA_TRANS_SIZE 65532 /* Max(65535) aligned to 4 bytes = 65532 */
  91. #define MAX_DMA_TRANS_NUM 256
  92. #define MAX_SAMPLE_CNT_DIV 8
  93. #define MAX_STEP_CNT_DIV 64
  94. #define MAX_HS_STEP_CNT_DIV 8
  95. #define DMA_ADDRESS_HIGH (0xC0000000)
  96. /* refer to AP_DMA register address */
  97. #define DMA_I2C_BASE_CH(id) (AP_DMA_BASE + 0x180 + (0x80 * (id)))
  98. #define DMA_I2C_BASE(id, base) ((base) + 0x180 + (0x80 * (id)))
  99. enum DMA_REGS_OFFSET {
  100. OFFSET_INT_FLAG = 0x0,
  101. OFFSET_INT_EN = 0x04,
  102. OFFSET_EN = 0x08,
  103. OFFSET_RST = 0x0C,
  104. OFFSET_STOP = 0x10,
  105. OFFSET_FLUSH = 0x14,
  106. OFFSET_CON = 0x18,
  107. OFFSET_TX_MEM_ADDR = 0x1C,
  108. OFFSET_RX_MEM_ADDR = 0x20,
  109. OFFSET_TX_LEN = 0x24,
  110. OFFSET_RX_LEN = 0x28,
  111. OFFSET_INT_BUF_SIZE = 0x38,
  112. OFFSET_DEBUG_STA = 0x50,
  113. };
  114. struct i2c_dma_info {
  115. unsigned long base;
  116. unsigned int int_flag;
  117. unsigned int int_en;
  118. unsigned int en;
  119. unsigned int rst;
  120. unsigned int stop;
  121. unsigned int flush;
  122. unsigned int con;
  123. unsigned int tx_mem_addr;
  124. unsigned int rx_mem_addr;
  125. unsigned int tx_len;
  126. unsigned int rx_len;
  127. unsigned int int_buf_size;
  128. unsigned int debug_sta;
  129. };
  130. enum i2c_trans_st_rs {
  131. I2C_TRANS_STOP = 0,
  132. I2C_TRANS_REPEATED_START,
  133. };
  134. enum {
  135. ST_MODE,
  136. FS_MODE,
  137. HS_MODE,
  138. };
  139. enum mt_trans_op {
  140. I2C_MASTER_NONE = 0,
  141. I2C_MASTER_WR = 1,
  142. I2C_MASTER_RD,
  143. I2C_MASTER_WRRD,
  144. };
  145. /* CONTROL */
  146. #define I2C_CONTROL_RS (0x1 << 1)
  147. #define I2C_CONTROL_DMA_EN (0x1 << 2)
  148. #define I2C_CONTROL_CLK_EXT_EN (0x1 << 3)
  149. #define I2C_CONTROL_DIR_CHANGE (0x1 << 4)
  150. #define I2C_CONTROL_ACKERR_DET_EN (0x1 << 5)
  151. #define I2C_CONTROL_TRANSFER_LEN_CHANGE (0x1 << 6)
  152. /***********************************end of register operation****************************************/
  153. /***********************************I2C Param********************************************************/
  154. struct mt_trans_data {
  155. u16 trans_num;
  156. u16 data_size;
  157. u16 trans_len;
  158. u16 trans_auxlen;
  159. };
  160. struct i2c_dma_buf {
  161. u8 *vaddr;
  162. dma_addr_t paddr;
  163. };
  164. struct mt_i2c_t {
  165. #ifdef I2C_DRIVER_IN_KERNEL
  166. /* ==========only used in kernel================ */
  167. struct i2c_adapter adap; /* i2c host adapter */
  168. struct device *dev; /* the device object of i2c host adapter */
  169. atomic_t trans_err; /* i2c transfer error */
  170. atomic_t trans_comp; /* i2c transfer completion */
  171. atomic_t trans_stop; /* i2c transfer stop */
  172. spinlock_t lock; /* for struct mt_i2c_t struct protection */
  173. wait_queue_head_t wait; /* i2c transfer wait queue */
  174. #endif
  175. /* ==========set in i2c probe============ */
  176. void __iomem *base; /* i2c base addr */
  177. u16 id;
  178. u32 irqnr; /* i2c interrupt number */
  179. u16 irq_stat; /* i2c interrupt status */
  180. u32 clk; /* host clock speed in khz */
  181. u32 pdn; /*clock number */
  182. /* ==========common data define============ */
  183. enum i2c_trans_st_rs st_rs;
  184. enum mt_trans_op op;
  185. void __iomem *pdmabase;
  186. u32 speed; /* The speed (khz) */
  187. u16 delay_len; /* number of half pulse between transfers in a trasaction */
  188. u32 msg_len; /* number of bytes for transaction */
  189. u8 *msg_buf; /* pointer to msg data */
  190. u8 addr; /* The address of the slave device, 7bit,the value include read/write bit. */
  191. u8 master_code; /* master code in HS mode */
  192. u8 mode; /* ST/FS/HS mode */
  193. /* ==========reserved function============ */
  194. u8 is_push_pull_enable; /* IO push-pull or open-drain */
  195. u8 is_clk_ext_disable; /* clk entend default enable */
  196. u8 is_dma_enabled; /* Transaction via DMA instead of 8-byte FIFO */
  197. u8 read_flag; /* read,write,read_write */
  198. bool dma_en;
  199. bool poll_en;
  200. bool pushpull; /* open drain */
  201. bool filter_msg; /* filter msg error log */
  202. bool i2c_3dcamera_flag; /* flag for 3dcamera */
  203. /* ==========define reg============ */
  204. u16 timing_reg;
  205. u16 high_speed_reg;
  206. u16 control_reg;
  207. u32 last_speed;
  208. u8 last_mode;
  209. u32 defaul_speed;
  210. struct mt_trans_data trans_data;
  211. struct i2c_dma_buf dma_buf;
  212. #if !defined(CONFIG_MTK_CLKMGR)
  213. struct clk *clk_main; /* main clock for i2c bus */
  214. struct clk *clk_dma; /* DMA clock for i2c via DMA */
  215. #endif /* !defined(CONFIG_MTK_CLKMGR) */
  216. };
  217. struct mt_i2c_msg {
  218. u16 addr; /* slave address */
  219. u16 flags;
  220. #define I2C_M_TEN 0x0010 /* this is a ten bit chip address */
  221. #define I2C_M_RD 0x0001 /* read data, from slave to master */
  222. #define I2C_M_STOP 0x8000 /* if I2C_FUNC_PROTOCOL_MANGLING */
  223. #define I2C_M_NOSTART 0x4000 /* if I2C_FUNC_NOSTART */
  224. #define I2C_M_REV_DIR_ADDR 0x2000 /* if I2C_FUNC_PROTOCOL_MANGLING */
  225. #define I2C_M_IGNORE_NAK 0x1000 /* if I2C_FUNC_PROTOCOL_MANGLING */
  226. #define I2C_M_NO_RD_ACK 0x0800 /* if I2C_FUNC_PROTOCOL_MANGLING */
  227. #define I2C_M_RECV_LEN 0x0400 /* length will be first received byte */
  228. u16 len; /* msg length */
  229. u8 *buf; /* pointer to msg data */
  230. u32 timing;
  231. u32 ext_flag;
  232. };
  233. #define I2C_A_FILTER_MSG 0x8000 /* filer out error messages */
  234. #define I2C_A_CHANGE_TIMING 0x4000 /* change timing parameters */
  235. #define I2C_MASK_FLAG (0x00ff)
  236. #define I2C_DMA_FLAG (0xdead2000)
  237. #define I2C_WR_FLAG (0x1000)
  238. #define I2C_RS_FLAG (0x0800)
  239. #define I2C_HS_FLAG (0x0400)
  240. #define I2C_ENEXT_FLAG (0x0200)
  241. #define I2C_DISEXT_FLAG (0x0000)
  242. #define I2C_POLL_FLAG (0x4000)
  243. #define I2C_CH2_FLAG (0x8000)
  244. #define I2C_POLLING_FLAG (0x00000001)
  245. #define I2C_PUSHPULL_FLAG (0x00000002)
  246. #define I2C_3DCAMERA_FLAG (0x00000004)
  247. #define I2C_DIRECTION_FLAG (0x00000008)
  248. /* external API */
  249. extern void __iomem *spm_get_i2c_base(void);
  250. extern char *mt_i2c_bus_to_virt(unsigned long address);
  251. void _i2c_dump_info(struct mt_i2c_t *i2c);
  252. void i2c_writel(struct mt_i2c_t *i2c, u8 offset, u16 value);
  253. u32 i2c_readl(struct mt_i2c_t *i2c, u8 offset);
  254. #endif /* __MT_I2C_H__ */