i2c-mtk.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2014 MediaTek Inc.
  3. * Author: Xudong.chen <xudong.chen@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #ifndef __I2C_MTK_H__
  15. #define __I2C_MTK_H__
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/i2c.h>
  20. #include <linux/init.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/sched.h>
  23. #include <linux/delay.h>
  24. #include <linux/errno.h>
  25. #include <linux/err.h>
  26. #include <linux/device.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/wait.h>
  29. #include <linux/mm.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/scatterlist.h>
  32. #include <linux/io.h>
  33. #include <linux/of_address.h>
  34. #include <linux/of_irq.h>
  35. #include <linux/clk.h>
  36. #define I2C_DEBUG_FS
  37. #define I2C_HS_NACKERR (1 << 2)
  38. #define I2C_ACKERR (1 << 1)
  39. #define I2C_TRANSAC_COMP (1 << 0)
  40. #define I2C_TRANSAC_START (1 << 0)
  41. #define I2C_TIMING_STEP_DIV_MASK (0x3f << 0)
  42. #define I2C_TIMING_SAMPLE_COUNT_MASK (0x7 << 0)
  43. #define I2C_TIMING_SAMPLE_DIV_MASK (0x7 << 8)
  44. #define I2C_TIMING_DATA_READ_MASK (0x7 << 12)
  45. #define I2C_DCM_DISABLE 0x0000
  46. #define I2C_IO_CONFIG_OPEN_DRAIN 0x0003
  47. #define I2C_IO_CONFIG_PUSH_PULL 0x0000
  48. #define I2C_SOFT_RST 0x0001
  49. #define I2C_FIFO_ADDR_CLR 0x0001
  50. #define I2C_DELAY_LEN 0x0002
  51. #define I2C_ST_START_CON 0x8001
  52. #define I2C_FS_START_CON 0x1800
  53. #define I2C_TIME_CLR_VALUE 0x0000
  54. #define I2C_TIME_DEFAULT_VALUE 0x0003
  55. #define I2C_DMA_CON_TX 0x0000
  56. #define I2C_DMA_CON_RX 0x0001
  57. #define I2C_DMA_START_EN 0x0001
  58. #define I2C_DMA_INT_FLAG_NONE 0x0000
  59. #define I2C_DEFAUT_SPEED 100000 /* hz */
  60. #define MAX_FS_MODE_SPEED 400000
  61. #define MAX_HS_MODE_SPEED 3400000
  62. #define MAX_DMA_TRANS_SIZE 4096 /* 255 */
  63. #define MAX_SAMPLE_CNT_DIV 8
  64. #define MAX_STEP_CNT_DIV 64
  65. #define MAX_HS_STEP_CNT_DIV 8
  66. #define I2C_CONTROL_RS (0x1 << 1)
  67. #define I2C_CONTROL_DMA_EN (0x1 << 2)
  68. #define I2C_CONTROL_CLK_EXT_EN (0x1 << 3)
  69. #define I2C_CONTROL_DIR_CHANGE (0x1 << 4)
  70. #define I2C_CONTROL_ACKERR_DET_EN (0x1 << 5)
  71. #define I2C_CONTROL_TRANSFER_LEN_CHANGE (0x1 << 6)
  72. #define I2C_CONTROL_WRAPPER (0x1 << 0)
  73. #define I2C_DRV_NAME "mt-i2c"
  74. #define I2CTAG "[I2C]"
  75. enum DMA_REGS_OFFSET {
  76. OFFSET_INT_FLAG = 0x0,
  77. OFFSET_INT_EN = 0x04,
  78. OFFSET_EN = 0x08,
  79. OFFSET_RST = 0x0C,
  80. OFFSET_STOP = 0x10,
  81. OFFSET_FLUSH = 0x14,
  82. OFFSET_CON = 0x18,
  83. OFFSET_TX_MEM_ADDR = 0x1C,
  84. OFFSET_RX_MEM_ADDR = 0x20,
  85. OFFSET_TX_LEN = 0x24,
  86. OFFSET_RX_LEN = 0x28,
  87. OFFSET_INT_BUF_SIZE = 0x38,
  88. };
  89. enum i2c_trans_st_rs {
  90. I2C_TRANS_STOP = 0,
  91. I2C_TRANS_REPEATED_START,
  92. };
  93. enum {
  94. FS_MODE,
  95. HS_MODE,
  96. };
  97. enum mt_trans_op {
  98. I2C_MASTER_WR = 1,
  99. I2C_MASTER_RD,
  100. I2C_MASTER_WRRD,
  101. };
  102. enum I2C_REGS_OFFSET {
  103. OFFSET_DATA_PORT = 0x0,
  104. OFFSET_SLAVE_ADDR = 0x04,
  105. OFFSET_INTR_MASK = 0x08,
  106. OFFSET_INTR_STAT = 0x0c,
  107. OFFSET_CONTROL = 0x10,
  108. OFFSET_TRANSFER_LEN = 0x14,
  109. OFFSET_TRANSAC_LEN = 0x18,
  110. OFFSET_DELAY_LEN = 0x1c,
  111. OFFSET_TIMING = 0x20,
  112. OFFSET_START = 0x24,
  113. OFFSET_EXT_CONF = 0x28,
  114. OFFSET_FIFO_STAT = 0x30,
  115. OFFSET_FIFO_THRESH = 0x34,
  116. OFFSET_FIFO_ADDR_CLR = 0x38,
  117. OFFSET_IO_CONFIG = 0x40,
  118. OFFSET_RSV_DEBUG = 0x44,
  119. OFFSET_HS = 0x48,
  120. OFFSET_SOFTRESET = 0x50,
  121. OFFSET_DCM_EN = 0x54,
  122. OFFSET_PATH_DIR = 0x60,
  123. OFFSET_DEBUGSTAT = 0x64,
  124. OFFSET_DEBUGCTRL = 0x68,
  125. OFFSET_TRANSFER_LEN_AUX = 0x6c,
  126. };
  127. enum PERICFG_OFFSET {
  128. OFFSET_PERI_I2C_MODE_ENABLE = 0x0410,
  129. };
  130. struct mt_i2c_data {
  131. unsigned int clk_frequency; /* bus speed in Hz */
  132. unsigned int flags;
  133. unsigned int clk_src_div;
  134. };
  135. struct i2c_dma_buf {
  136. u8 *vaddr;
  137. dma_addr_t paddr;
  138. };
  139. struct mt_i2c_ext {
  140. #define I2C_HWTRIG_FLAG 0x00000001
  141. bool isEnable;
  142. bool is_hw_trig;
  143. u32 timing;
  144. };
  145. struct mt_i2c {
  146. struct i2c_adapter adap; /* i2c host adapter */
  147. struct device *dev;
  148. wait_queue_head_t wait; /* i2c transfer wait queue */
  149. /* set in i2c probe */
  150. void __iomem *base; /* i2c base addr */
  151. void __iomem *pdmabase; /* dma base address*/
  152. int irqnr; /* i2c interrupt number */
  153. int id;
  154. struct i2c_dma_buf dma_buf; /* memory alloc for DMA mode */
  155. struct clk *clk_main; /* main clock for i2c bus */
  156. struct clk *clk_dma; /* DMA clock for i2c via DMA */
  157. struct clk *clk_pmic; /* PMIC clock for i2c from PMIC */
  158. struct clk *clk_arb; /* Arbitrator clock for i2c */
  159. bool have_pmic; /* can use i2c pins form PMIC */
  160. bool have_dcm; /* HW DCM function */
  161. bool use_push_pull; /* IO config push-pull mode */
  162. /* set when doing the transfer */
  163. u16 irq_stat; /* interrupt status */
  164. unsigned int speed_hz; /* The speed in transfer */
  165. unsigned int clk_src_div;
  166. bool trans_stop; /* i2c transfer stop */
  167. enum mt_trans_op op;
  168. u16 msg_len;
  169. u8 *msg_buf; /* pointer to msg data */
  170. u16 msg_aux_len; /* WRRD mode to set AUX_LEN register*/
  171. u16 addr; /* 7bit slave address, without read/write bit */
  172. u16 timing_reg;
  173. u16 high_speed_reg;
  174. struct mutex i2c_mutex;
  175. struct mt_i2c_ext ext_data;
  176. };
  177. extern void i2c_dump_info(struct mt_i2c *i2c);
  178. extern int mtk_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num,
  179. u32 ext_flag, u32 timing);
  180. #endif