flexcop-i2c.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * This file is part of linux driver the digital TV devices equipped with B2C2 FlexcopII(b)/III
  3. *
  4. * flexcop-i2c.c - flexcop internal 2Wire bus (I2C) and dvb i2c initialization
  5. *
  6. * see flexcop.c for copyright information.
  7. */
  8. #include "flexcop.h"
  9. #define FC_MAX_I2C_RETRIES 100000
  10. static int flexcop_i2c_operation(struct flexcop_device *fc, flexcop_ibi_value *r100)
  11. {
  12. int i;
  13. flexcop_ibi_value r;
  14. r100->tw_sm_c_100.working_start = 1;
  15. deb_i2c("r100 before: %08x\n",r100->raw);
  16. fc->write_ibi_reg(fc, tw_sm_c_100, ibi_zero);
  17. fc->write_ibi_reg(fc, tw_sm_c_100, *r100); /* initiating i2c operation */
  18. for (i = 0; i < FC_MAX_I2C_RETRIES; i++) {
  19. r = fc->read_ibi_reg(fc, tw_sm_c_100);
  20. if (!r.tw_sm_c_100.no_base_addr_ack_error) {
  21. if (r.tw_sm_c_100.st_done) { /* && !r.tw_sm_c_100.working_start */
  22. *r100 = r;
  23. deb_i2c("i2c success\n");
  24. return 0;
  25. }
  26. } else {
  27. deb_i2c("suffering from an i2c ack_error\n");
  28. return -EREMOTEIO;
  29. }
  30. }
  31. deb_i2c("tried %d times i2c operation, never finished or too many ack errors.\n",i);
  32. return -EREMOTEIO;
  33. }
  34. static int flexcop_i2c_read4(struct flexcop_device *fc, flexcop_ibi_value r100, u8 *buf)
  35. {
  36. flexcop_ibi_value r104;
  37. int len = r100.tw_sm_c_100.total_bytes, /* remember total_bytes is buflen-1 */
  38. ret;
  39. if ((ret = flexcop_i2c_operation(fc,&r100)) != 0) {
  40. /* The Cablestar needs a different kind of i2c-transfer (does not
  41. * support "Repeat Start"):
  42. * wait for the ACK failure,
  43. * and do a subsequent read with the Bit 30 enabled
  44. */
  45. r100.tw_sm_c_100.no_base_addr_ack_error = 1;
  46. if ((ret = flexcop_i2c_operation(fc,&r100)) != 0) {
  47. deb_i2c("no_base_addr read failed. %d\n",ret);
  48. return ret;
  49. }
  50. }
  51. buf[0] = r100.tw_sm_c_100.data1_reg;
  52. if (len > 0) {
  53. r104 = fc->read_ibi_reg(fc,tw_sm_c_104);
  54. deb_i2c("read: r100: %08x, r104: %08x\n",r100.raw,r104.raw);
  55. /* there is at least one more byte, otherwise we wouldn't be here */
  56. buf[1] = r104.tw_sm_c_104.data2_reg;
  57. if (len > 1) buf[2] = r104.tw_sm_c_104.data3_reg;
  58. if (len > 2) buf[3] = r104.tw_sm_c_104.data4_reg;
  59. }
  60. return 0;
  61. }
  62. static int flexcop_i2c_write4(struct flexcop_device *fc, flexcop_ibi_value r100, u8 *buf)
  63. {
  64. flexcop_ibi_value r104;
  65. int len = r100.tw_sm_c_100.total_bytes; /* remember total_bytes is buflen-1 */
  66. r104.raw = 0;
  67. /* there is at least one byte, otherwise we wouldn't be here */
  68. r100.tw_sm_c_100.data1_reg = buf[0];
  69. r104.tw_sm_c_104.data2_reg = len > 0 ? buf[1] : 0;
  70. r104.tw_sm_c_104.data3_reg = len > 1 ? buf[2] : 0;
  71. r104.tw_sm_c_104.data4_reg = len > 2 ? buf[3] : 0;
  72. deb_i2c("write: r100: %08x, r104: %08x\n",r100.raw,r104.raw);
  73. /* write the additional i2c data before doing the actual i2c operation */
  74. fc->write_ibi_reg(fc,tw_sm_c_104,r104);
  75. return flexcop_i2c_operation(fc,&r100);
  76. }
  77. int flexcop_i2c_request(struct flexcop_device *fc, flexcop_access_op_t op,
  78. flexcop_i2c_port_t port, u8 chipaddr, u8 addr, u8 *buf, u16 len)
  79. {
  80. int ret;
  81. u16 bytes_to_transfer;
  82. flexcop_ibi_value r100;
  83. deb_i2c("op = %d\n",op);
  84. r100.raw = 0;
  85. r100.tw_sm_c_100.chipaddr = chipaddr;
  86. r100.tw_sm_c_100.twoWS_rw = op;
  87. r100.tw_sm_c_100.twoWS_port_reg = port;
  88. while (len != 0) {
  89. bytes_to_transfer = len > 4 ? 4 : len;
  90. r100.tw_sm_c_100.total_bytes = bytes_to_transfer - 1;
  91. r100.tw_sm_c_100.baseaddr = addr;
  92. if (op == FC_READ)
  93. ret = flexcop_i2c_read4(fc, r100, buf);
  94. else
  95. ret = flexcop_i2c_write4(fc,r100, buf);
  96. if (ret < 0)
  97. return ret;
  98. buf += bytes_to_transfer;
  99. addr += bytes_to_transfer;
  100. len -= bytes_to_transfer;
  101. };
  102. return 0;
  103. }
  104. /* exported for PCI i2c */
  105. EXPORT_SYMBOL(flexcop_i2c_request);
  106. /* master xfer callback for demodulator */
  107. static int flexcop_master_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
  108. {
  109. struct flexcop_device *fc = i2c_get_adapdata(i2c_adap);
  110. int i, ret = 0;
  111. if (mutex_lock_interruptible(&fc->i2c_mutex))
  112. return -ERESTARTSYS;
  113. /* reading */
  114. if (num == 2 &&
  115. msgs[0].flags == 0 &&
  116. msgs[1].flags == I2C_M_RD &&
  117. msgs[0].buf != NULL &&
  118. msgs[1].buf != NULL) {
  119. ret = fc->i2c_request(fc, FC_READ, FC_I2C_PORT_DEMOD, msgs[0].addr, msgs[0].buf[0], msgs[1].buf, msgs[1].len);
  120. } else for (i = 0; i < num; i++) { /* writing command */
  121. if (msgs[i].flags != 0 || msgs[i].buf == NULL || msgs[i].len < 2) {
  122. ret = -EINVAL;
  123. break;
  124. }
  125. ret = fc->i2c_request(fc, FC_WRITE, FC_I2C_PORT_DEMOD, msgs[i].addr, msgs[i].buf[0], &msgs[i].buf[1], msgs[i].len - 1);
  126. }
  127. if (ret < 0)
  128. err("i2c master_xfer failed");
  129. else
  130. ret = num;
  131. mutex_unlock(&fc->i2c_mutex);
  132. return ret;
  133. }
  134. static u32 flexcop_i2c_func(struct i2c_adapter *adapter)
  135. {
  136. return I2C_FUNC_I2C;
  137. }
  138. static struct i2c_algorithm flexcop_algo = {
  139. .master_xfer = flexcop_master_xfer,
  140. .functionality = flexcop_i2c_func,
  141. };
  142. int flexcop_i2c_init(struct flexcop_device *fc)
  143. {
  144. int ret;
  145. mutex_init(&fc->i2c_mutex);
  146. memset(&fc->i2c_adap, 0, sizeof(struct i2c_adapter));
  147. strncpy(fc->i2c_adap.name, "B2C2 FlexCop device",I2C_NAME_SIZE);
  148. i2c_set_adapdata(&fc->i2c_adap,fc);
  149. fc->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
  150. fc->i2c_adap.algo = &flexcop_algo;
  151. fc->i2c_adap.algo_data = NULL;
  152. if ((ret = i2c_add_adapter(&fc->i2c_adap)) < 0)
  153. return ret;
  154. fc->init_state |= FC_STATE_I2C_INIT;
  155. return 0;
  156. }
  157. void flexcop_i2c_exit(struct flexcop_device *fc)
  158. {
  159. if (fc->init_state & FC_STATE_I2C_INIT)
  160. i2c_del_adapter(&fc->i2c_adap);
  161. fc->init_state &= ~FC_STATE_I2C_INIT;
  162. }