flexcop-i2c.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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, int max_ack_errors)
  11. {
  12. int i,ack_errors = 0;
  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. if (++ack_errors >= max_ack_errors)
  29. break;
  30. fc->write_ibi_reg(fc, tw_sm_c_100, ibi_zero);
  31. fc->write_ibi_reg(fc, tw_sm_c_100, *r100);
  32. }
  33. }
  34. deb_i2c("tried %d times i2c operation, never finished or too many ack errors.\n",i);
  35. return -EREMOTEIO;
  36. }
  37. static int flexcop_i2c_read4(struct flexcop_device *fc, flexcop_ibi_value r100, u8 *buf)
  38. {
  39. flexcop_ibi_value r104;
  40. int len = r100.tw_sm_c_100.total_bytes, /* remember total_bytes is buflen-1 */
  41. ret;
  42. if ((ret = flexcop_i2c_operation(fc,&r100,30)) != 0)
  43. return ret;
  44. r104 = fc->read_ibi_reg(fc,tw_sm_c_104);
  45. deb_i2c("read: r100: %08x, r104: %08x\n",r100.raw,r104.raw);
  46. /* there is at least one byte, otherwise we wouldn't be here */
  47. buf[0] = r100.tw_sm_c_100.data1_reg;
  48. if (len > 0) buf[1] = r104.tw_sm_c_104.data2_reg;
  49. if (len > 1) buf[2] = r104.tw_sm_c_104.data3_reg;
  50. if (len > 2) buf[3] = r104.tw_sm_c_104.data4_reg;
  51. return 0;
  52. }
  53. static int flexcop_i2c_write4(struct flexcop_device *fc, flexcop_ibi_value r100, u8 *buf)
  54. {
  55. flexcop_ibi_value r104;
  56. int len = r100.tw_sm_c_100.total_bytes; /* remember total_bytes is buflen-1 */
  57. r104.raw = 0;
  58. /* there is at least one byte, otherwise we wouldn't be here */
  59. r100.tw_sm_c_100.data1_reg = buf[0];
  60. r104.tw_sm_c_104.data2_reg = len > 0 ? buf[1] : 0;
  61. r104.tw_sm_c_104.data3_reg = len > 1 ? buf[2] : 0;
  62. r104.tw_sm_c_104.data4_reg = len > 2 ? buf[3] : 0;
  63. deb_i2c("write: r100: %08x, r104: %08x\n",r100.raw,r104.raw);
  64. /* write the additional i2c data before doing the actual i2c operation */
  65. fc->write_ibi_reg(fc,tw_sm_c_104,r104);
  66. return flexcop_i2c_operation(fc,&r100,30);
  67. }
  68. /* master xfer callback for demodulator */
  69. static int flexcop_master_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
  70. {
  71. struct flexcop_device *fc = i2c_get_adapdata(i2c_adap);
  72. int i, ret = 0;
  73. if (down_interruptible(&fc->i2c_sem))
  74. return -ERESTARTSYS;
  75. /* reading */
  76. if (num == 2 &&
  77. msgs[0].flags == 0 &&
  78. msgs[1].flags == I2C_M_RD &&
  79. msgs[0].buf != NULL &&
  80. msgs[1].buf != NULL) {
  81. ret = fc->i2c_request(fc, FC_READ, FC_I2C_PORT_DEMOD, msgs[0].addr, msgs[0].buf[0], msgs[1].buf, msgs[1].len);
  82. } else for (i = 0; i < num; i++) { /* writing command */
  83. if (msgs[i].flags != 0 || msgs[i].buf == NULL || msgs[i].len < 2) {
  84. ret = -EINVAL;
  85. break;
  86. }
  87. 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);
  88. }
  89. if (ret < 0)
  90. err("i2c master_xfer failed");
  91. else
  92. ret = num;
  93. up(&fc->i2c_sem);
  94. return ret;
  95. }
  96. int flexcop_i2c_request(struct flexcop_device *fc, flexcop_access_op_t op,
  97. flexcop_i2c_port_t port, u8 chipaddr, u8 addr, u8 *buf, u16 len)
  98. {
  99. int ret;
  100. u16 bytes_to_transfer;
  101. flexcop_ibi_value r100;
  102. deb_i2c("op = %d\n",op);
  103. r100.raw = 0;
  104. r100.tw_sm_c_100.chipaddr = chipaddr;
  105. r100.tw_sm_c_100.twoWS_rw = op;
  106. r100.tw_sm_c_100.twoWS_port_reg = port;
  107. while (len != 0) {
  108. bytes_to_transfer = len > 4 ? 4 : len;
  109. r100.tw_sm_c_100.total_bytes = bytes_to_transfer - 1;
  110. r100.tw_sm_c_100.baseaddr = addr;
  111. if (op == FC_READ)
  112. ret = flexcop_i2c_read4(fc, r100, buf);
  113. else
  114. ret = flexcop_i2c_write4(fc,r100, buf);
  115. if (ret < 0)
  116. return ret;
  117. buf += bytes_to_transfer;
  118. addr += bytes_to_transfer;
  119. len -= bytes_to_transfer;
  120. };
  121. return 0;
  122. }
  123. /* exported for PCI i2c */
  124. EXPORT_SYMBOL(flexcop_i2c_request);
  125. static u32 flexcop_i2c_func(struct i2c_adapter *adapter)
  126. {
  127. return I2C_FUNC_I2C;
  128. }
  129. static struct i2c_algorithm flexcop_algo = {
  130. .name = "FlexCop I2C algorithm",
  131. .id = I2C_ALGO_BIT,
  132. .master_xfer = flexcop_master_xfer,
  133. .functionality = flexcop_i2c_func,
  134. };
  135. int flexcop_i2c_init(struct flexcop_device *fc)
  136. {
  137. int ret;
  138. sema_init(&fc->i2c_sem,1);
  139. memset(&fc->i2c_adap, 0, sizeof(struct i2c_adapter));
  140. strncpy(fc->i2c_adap.name, "B2C2 FlexCop device",I2C_NAME_SIZE);
  141. i2c_set_adapdata(&fc->i2c_adap,fc);
  142. fc->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
  143. fc->i2c_adap.algo = &flexcop_algo;
  144. fc->i2c_adap.algo_data = NULL;
  145. fc->i2c_adap.id = I2C_ALGO_BIT;
  146. if ((ret = i2c_add_adapter(&fc->i2c_adap)) < 0)
  147. return ret;
  148. fc->init_state |= FC_STATE_I2C_INIT;
  149. return 0;
  150. }
  151. void flexcop_i2c_exit(struct flexcop_device *fc)
  152. {
  153. if (fc->init_state & FC_STATE_I2C_INIT)
  154. i2c_del_adapter(&fc->i2c_adap);
  155. fc->init_state &= ~FC_STATE_I2C_INIT;
  156. }