aml_i2c.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*aml_i2c.h*/
  2. #ifndef AML_I2C
  3. #define AML_I2C
  4. #include <linux/i2c.h>
  5. #include <linux/i2c-aml.h>
  6. #define ADAPTER_NAME "aml_i2c_adap"
  7. #define NAME_LEN 8
  8. #define AML_I2C_MAX_TOKENS 8
  9. #define AML_I2C_CTRL_CLK_DELAY_MASK 0x3ff
  10. #define AML_I2C_SLAVE_ADDR_MASK 0xff
  11. #define AML_I2C_PRINT_DATA(name) do{ \
  12. if(i2c->i2c_debug) {\
  13. printk("[%s]:%s", name, p->flags & I2C_M_RD? "read" : "write");\
  14. for(i=0;i<p->len;i++) \
  15. printk("%x-",*(p->buf)++); \
  16. printk("\n");\
  17. }\
  18. }while(0)
  19. /*I2C_CONTROL_REG 0x2140*/
  20. struct aml_i2c_reg_ctrl {
  21. unsigned int start:1; /*[0] Set to 1 to start list processing*/
  22. /*Setting this bit to 0 while the list processor is operating causes the list
  23. processor to abort the current I2C operation and generate an I2C STOP
  24. command on the I2C bus. Normally this bit is set to 1 and left high
  25. until processing is complete. To re-start the list processor with a
  26. new list (after a previous list has been exhausted), simply set this
  27. bit to zero then to one.*/
  28. unsigned int ack_ignore:1; /*[1] Set to 1 to disable I2C ACK detection.*/
  29. /*The I2C bus uses an ACK signal after every byte transfer to detect
  30. problems during the transfer. Current Software implementations of the
  31. I2C bus ignore this ACK. This bit is for compatibility with the current
  32. Amlogic software. This bit should be set to 0 to allow NACK
  33. operations to abort I2C bus transactions. If a NACK occurs, the ERROR
  34. bit above will be set. */
  35. unsigned int status:1; /*[2] the status of the List processor*/
  36. #define IDLE 0
  37. #define RUNNING 1
  38. /* 0: IDLE
  39. 1: Running. The list processor will enter this state on the clock cycle
  40. after the START bit is set. The software can poll the status register to
  41. determine when processing is complete.
  42. */
  43. unsigned int error:1; /*[3] */
  44. /*This read only bit is set if the I2C device generates a NACK during writing.
  45. This bit is cleared at on the clock cycle after the START bit is set to 1
  46. indicating the start of list processing. Errors can be ignored by setting
  47. the ACK_IGNORE bit below. Errors will be generated on Writes to
  48. devices that return NACK instead of ACK. A NACK is returned by a
  49. device if it is unable to accept any more data (for example because it
  50. is processing some other real-time function). In the event of an
  51. ERROR, the I2C module will automatically generate a STOP condition
  52. on the bus.*/
  53. unsigned int cur_token:4; /*[7:4] the current token being processed*/
  54. unsigned int rd_data_cnt:4;/*[11:8] number of bytes READ over the I2C bus*/
  55. /*If this value is zero, then no data has been read. If this value is 1, then
  56. bits [7:0] in TOKEN_RDATA_REG0 contains valid data. The software can
  57. read this register after an I2C transaction to get the number of bytes to
  58. read from the I2C device*/
  59. unsigned int clk_delay:10; /*[21:12] Quarter clock delay*/
  60. /*This value corresponds to period of the SCL clock divided by 4
  61. Quarter Clock Delay = * System Clock Frequency
  62. For example, if the system clock is 133Mhz, and the I2C clock period
  63. is 10uS (100khz), then
  64. Quarter Clock Delay = * 133 Mhz = 332
  65. */
  66. unsigned int manual_en:1; /*[22] enable manual mode. */
  67. /*Manual I2C mode is controlled by bits 12,13,14 and 15 above.*/
  68. unsigned int wrscl:1; /*[23] Sets the level of the SCL line */
  69. /*if manual mode is enabled. If this bit is '0', then the SCL line is
  70. pulled low. If this bit is '1' then the SCL line is tri-stated.*/
  71. unsigned int wrsda:1; /*[24] Sets the level of the SDA line */
  72. /*if manual mode is enabled. If this bit is '0', then the SDA line is
  73. pulled low. If this bit is '1' then the SDA line is tri-stated.*/
  74. unsigned int rdscl:1; /*[25] Read back level of the SCL line*/
  75. unsigned int rdsda:1; /*[26] Read back level of the SDA line*/
  76. unsigned int unused:5; /*[31:27]*/
  77. };
  78. struct aml_i2c_reg_slave_addr {
  79. unsigned int slave_addr:8; /*[7:0] SLAVE ADDRESS.*/
  80. /*This is a 7-bit value for a 7-bit I2C device, or (0xF0 | {A9,A8}) for a
  81. 10 bit I2C device. By convention, the slave address is typically
  82. stored in by first left shifting it so that it's MSB is D7 (The I2C bus
  83. assumes the 7-bit address is left shifted one). Additionally, since
  84. the SLAVE address is always an 7-bit value, D0 is always 0.
  85. NOTE: The I2C always transfers 8-bits even for address. The I2C
  86. hardware will use D0 to dictate the direction of the bus. Therefore,
  87. D0 should always be '0' when this register is set.
  88. */
  89. unsigned int sda_filter:3; /*[10:8] SDA FILTER*/
  90. /*A filter was added in the SCL input path to allow for filtering of slow
  91. rise times. 0 = no filtering, 7 = max filtering*/
  92. unsigned int scl_filter:3; /*[13:11] SCL FILTER*/
  93. /*A filter was added in the SCL input path to allow for filtering of slow
  94. rise times. 0 = no filtering, 7 = max filtering*/
  95. unsigned int unused:18; /*[31:14]*/
  96. };
  97. /*Write data associated with the DATA token should be placed into the
  98. I2C_TOKEN_WDATA_REG0 or I2C_TOKEN_WDATA_REG1 registers.
  99. Read data associated with the DATA or DATA-LAST token can be read from
  100. the I2C_TOKEN_RDATA_REG0 or I2C_TOKEN_RDATA_REG1 registers*/
  101. enum aml_i2c_token {
  102. TOKEN_END,
  103. TOKEN_START,
  104. TOKEN_SLAVE_ADDR_WRITE,
  105. TOKEN_SLAVE_ADDR_READ,
  106. TOKEN_DATA,
  107. TOKEN_DATA_LAST,
  108. TOKEN_STOP
  109. };
  110. struct aml_i2c_reg_master {
  111. volatile unsigned int i2c_ctrl;
  112. volatile unsigned int i2c_slave_addr;
  113. volatile unsigned int i2c_token_list_0;
  114. volatile unsigned int i2c_token_list_1;
  115. volatile unsigned int i2c_token_wdata_0;
  116. volatile unsigned int i2c_token_wdata_1;
  117. volatile unsigned int i2c_token_rdata_0;
  118. volatile unsigned int i2c_token_rdata_1;
  119. };
  120. struct aml_i2c_reg_slave_ctrl {
  121. unsigned int samp_rate:7; /*[6:0] sampling rate*/
  122. /*Defined as MPEG system clock / (value + 1). The SDA and SCL inputs into
  123. the slave module are sampled as a way of filtering the inputs. A
  124. rising or falling edge is determined by when 3 successive samples are
  125. either high or low respectively*/
  126. unsigned int enable:1; /*[7] A '1' enables the I2C slave state machine*/
  127. unsigned int hold_time:8; /*[15:8]*/
  128. /*Data hold time after the falling edge of SCL.
  129. Hold time = (MPEG system clock period) * (value + 1).
  130. */
  131. unsigned int slave_addr:8; /*[23-16]*/
  132. /*Bits [7:1] are used to identify the device.
  133. Bit [0] is ignored since this corresponds to the R/W bit.*/
  134. unsigned int ack_always:1; /*[24]*/
  135. /*Typically the ACK of a slave I2C device is dependent upon the
  136. availability of data (if reading) and room to store data (when we are
  137. being written). Our I2C module has a status register that can be read
  138. continuously. This bit can be set if the I2C master wants to
  139. continually read the status register. */
  140. unsigned int irq_en:1; /*[25]*/
  141. /*If this bit is set, then an interrupt will be sent to the ARC whenever 4
  142. bytes have been read or 4 bytes have been written to the I2C slave
  143. module.*/
  144. unsigned int busy:1; /*[26] */
  145. /*Read only status bit. '1' indicates that the I2C slave module is sending
  146. or receiving data.*/
  147. unsigned int rx_rdy:1; /*[27] */
  148. /*This bit is set to '1' by the ARC to indicate to the slave machine that
  149. the I2C slave module is ready to receive data. This bit is cleared by
  150. the I2C module when it has received 4 bytes from the I2C master.
  151. This bit is also available in the status register that can be read by
  152. the I2C master. The I2C master can read the status register to see
  153. when the I2C slave module is ready to receive data.*/
  154. unsigned int tx_rdy:1; /*[28] */
  155. /*This bit is set to '1' by the ARC to indicate to the slave machine that
  156. the I2C slave module is ready to send data. This bit is cleared by
  157. the I2C module when it has sent 4 bytes to the I2C master. This bit
  158. is also available in the status register that can be read by the I2C
  159. master. The I2C master can read the status register to see when the
  160. I2C slave module has data to send.*/
  161. unsigned int reg_ptr:3; /*[31:29] */
  162. /*There are 5 internal registers inside the I2C slave module. The I2C
  163. Master sets this value using the byte that follows the address byte
  164. in the I2C data stream. Register 4 (numbered 0,1,¡­4) is the
  165. status register.*/
  166. };
  167. struct aml_i2c_reg_slave{
  168. unsigned int i2c_slave_ctrl;
  169. unsigned int i2c_slave_tx_data;
  170. unsigned int i2c_slave_rx_data;
  171. };
  172. struct aml_i2c {
  173. unsigned int i2c_debug;
  174. unsigned int cur_slave_addr;
  175. unsigned int wait_count;
  176. unsigned int wait_ack_interval;
  177. unsigned int wait_read_interval;
  178. unsigned int wait_xfer_interval;
  179. unsigned int master_no;/*master a:0 master b:1*/
  180. #define MASTER_A 0
  181. #define MASTER_B 1
  182. unsigned char token_tag[AML_I2C_MAX_TOKENS];
  183. unsigned int msg_flags;
  184. struct i2c_adapter adap;
  185. struct i2c_adapter adap2;/*the same adapter, different speed*/
  186. struct aml_i2c_ops* ops;
  187. struct aml_i2c_reg_master __iomem* master_regs;
  188. pinmux_set_t master_pinmux;
  189. unsigned int master_i2c_speed;
  190. unsigned int master_i2c_speed2;/*the same adapter, different speed*/
  191. struct mutex lock;
  192. struct class cls;
  193. unsigned int cur_token;
  194. /*reserved original member, used in bsp*/
  195. unsigned int use_pio;/*0: hardware i2c, 1: pio i2c*/
  196. pinmux_set_t master_a_pinmux;
  197. pinmux_set_t master_b_pinmux;
  198. };
  199. struct aml_i2c_ops {
  200. void (*xfer_prepare)(struct aml_i2c *i2c, unsigned int speed);
  201. int (*read)(struct aml_i2c *i2c, unsigned char *buf, unsigned int len);
  202. int (*write)(struct aml_i2c *i2c, unsigned char *buf, unsigned int len);
  203. int (*do_address)(struct aml_i2c *i2c, unsigned int addr);
  204. void (*stop)(struct aml_i2c *i2c);
  205. };
  206. #endif