flexcop-i2c.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. /* #define DUMP_I2C_MESSAGES */
  11. static int flexcop_i2c_operation(struct flexcop_device *fc, flexcop_ibi_value *r100)
  12. {
  13. int i;
  14. flexcop_ibi_value r;
  15. r100->tw_sm_c_100.working_start = 1;
  16. deb_i2c("r100 before: %08x\n",r100->raw);
  17. fc->write_ibi_reg(fc, tw_sm_c_100, ibi_zero);
  18. fc->write_ibi_reg(fc, tw_sm_c_100, *r100); /* initiating i2c operation */
  19. for (i = 0; i < FC_MAX_I2C_RETRIES; i++) {
  20. r = fc->read_ibi_reg(fc, tw_sm_c_100);
  21. if (!r.tw_sm_c_100.no_base_addr_ack_error) {
  22. if (r.tw_sm_c_100.st_done) { /* && !r.tw_sm_c_100.working_start */
  23. *r100 = r;
  24. deb_i2c("i2c success\n");
  25. return 0;
  26. }
  27. } else {
  28. deb_i2c("suffering from an i2c ack_error\n");
  29. return -EREMOTEIO;
  30. }
  31. }
  32. deb_i2c("tried %d times i2c operation, never finished or too many ack errors.\n",i);
  33. return -EREMOTEIO;
  34. }
  35. static int flexcop_i2c_read4(struct flexcop_i2c_adapter *i2c,
  36. flexcop_ibi_value r100, u8 *buf)
  37. {
  38. flexcop_ibi_value r104;
  39. int len = r100.tw_sm_c_100.total_bytes, /* remember total_bytes is buflen-1 */
  40. ret;
  41. ret = flexcop_i2c_operation(i2c->fc, &r100);
  42. if (ret != 0) {
  43. deb_i2c("Retrying operation\n");
  44. r100.tw_sm_c_100.no_base_addr_ack_error = i2c->no_base_addr;
  45. ret = flexcop_i2c_operation(i2c->fc, &r100);
  46. }
  47. if (ret != 0) {
  48. deb_i2c("read failed. %d\n", ret);
  49. return ret;
  50. }
  51. buf[0] = r100.tw_sm_c_100.data1_reg;
  52. if (len > 0) {
  53. r104 = i2c->fc->read_ibi_reg(i2c->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_i2c_adapter *i2c,
  78. flexcop_access_op_t op, u8 chipaddr, u8 addr, u8 *buf, u16 len)
  79. {
  80. int ret;
  81. #ifdef DUMP_I2C_MESSAGES
  82. int i;
  83. #endif
  84. u16 bytes_to_transfer;
  85. flexcop_ibi_value r100;
  86. deb_i2c("op = %d\n",op);
  87. r100.raw = 0;
  88. r100.tw_sm_c_100.chipaddr = chipaddr;
  89. r100.tw_sm_c_100.twoWS_rw = op;
  90. r100.tw_sm_c_100.twoWS_port_reg = i2c->port;
  91. #ifdef DUMP_I2C_MESSAGES
  92. printk(KERN_DEBUG "%d ", i2c->port);
  93. if (op == FC_READ)
  94. printk("rd(");
  95. else
  96. printk("wr(");
  97. printk("%02x): %02x ", chipaddr, addr);
  98. #endif
  99. /* in that case addr is the only value ->
  100. * we write it twice as baseaddr and val0
  101. * BBTI is doing it like that for ISL6421 at least */
  102. if (i2c->no_base_addr && len == 0 && op == FC_WRITE) {
  103. buf = &addr;
  104. len = 1;
  105. }
  106. while (len != 0) {
  107. bytes_to_transfer = len > 4 ? 4 : len;
  108. r100.tw_sm_c_100.total_bytes = bytes_to_transfer - 1;
  109. r100.tw_sm_c_100.baseaddr = addr;
  110. if (op == FC_READ)
  111. ret = flexcop_i2c_read4(i2c, r100, buf);
  112. else
  113. ret = flexcop_i2c_write4(i2c->fc, r100, buf);
  114. #ifdef DUMP_I2C_MESSAGES
  115. for (i = 0; i < bytes_to_transfer; i++)
  116. printk("%02x ", buf[i]);
  117. #endif
  118. if (ret < 0)
  119. return ret;
  120. buf += bytes_to_transfer;
  121. addr += bytes_to_transfer;
  122. len -= bytes_to_transfer;
  123. }
  124. #ifdef DUMP_I2C_MESSAGES
  125. printk("\n");
  126. #endif
  127. return 0;
  128. }
  129. /* exported for PCI i2c */
  130. EXPORT_SYMBOL(flexcop_i2c_request);
  131. /* master xfer callback for demodulator */
  132. static int flexcop_master_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
  133. {
  134. struct flexcop_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
  135. int i, ret = 0;
  136. /* Some drivers use 1 byte or 0 byte reads as probes, which this
  137. * driver doesn't support. These probes will always fail, so this
  138. * hack makes them always succeed. If one knew how, it would of
  139. * course be better to actually do the read. */
  140. if (num == 1 && msgs[0].flags == I2C_M_RD && msgs[0].len <= 1)
  141. return 1;
  142. if (mutex_lock_interruptible(&i2c->fc->i2c_mutex))
  143. return -ERESTARTSYS;
  144. for (i = 0; i < num; i++) {
  145. /* reading */
  146. if (i+1 < num && (msgs[i+1].flags == I2C_M_RD)) {
  147. ret = i2c->fc->i2c_request(i2c, FC_READ, msgs[i].addr,
  148. msgs[i].buf[0], msgs[i+1].buf, msgs[i+1].len);
  149. i++; /* skip the following message */
  150. } else /* writing */
  151. ret = i2c->fc->i2c_request(i2c, FC_WRITE, msgs[i].addr,
  152. msgs[i].buf[0], &msgs[i].buf[1],
  153. msgs[i].len - 1);
  154. if (ret < 0) {
  155. err("i2c master_xfer failed");
  156. break;
  157. }
  158. }
  159. mutex_unlock(&i2c->fc->i2c_mutex);
  160. if (ret == 0)
  161. ret = num;
  162. return ret;
  163. }
  164. static u32 flexcop_i2c_func(struct i2c_adapter *adapter)
  165. {
  166. return I2C_FUNC_I2C;
  167. }
  168. static struct i2c_algorithm flexcop_algo = {
  169. .master_xfer = flexcop_master_xfer,
  170. .functionality = flexcop_i2c_func,
  171. };
  172. int flexcop_i2c_init(struct flexcop_device *fc)
  173. {
  174. int ret;
  175. mutex_init(&fc->i2c_mutex);
  176. fc->fc_i2c_adap[0].fc = fc;
  177. fc->fc_i2c_adap[1].fc = fc;
  178. fc->fc_i2c_adap[2].fc = fc;
  179. fc->fc_i2c_adap[0].port = FC_I2C_PORT_DEMOD;
  180. fc->fc_i2c_adap[1].port = FC_I2C_PORT_EEPROM;
  181. fc->fc_i2c_adap[2].port = FC_I2C_PORT_TUNER;
  182. strlcpy(fc->fc_i2c_adap[0].i2c_adap.name, "B2C2 FlexCop I2C to demod",
  183. sizeof(fc->fc_i2c_adap[0].i2c_adap.name));
  184. strlcpy(fc->fc_i2c_adap[1].i2c_adap.name, "B2C2 FlexCop I2C to eeprom",
  185. sizeof(fc->fc_i2c_adap[1].i2c_adap.name));
  186. strlcpy(fc->fc_i2c_adap[2].i2c_adap.name, "B2C2 FlexCop I2C to tuner",
  187. sizeof(fc->fc_i2c_adap[2].i2c_adap.name));
  188. i2c_set_adapdata(&fc->fc_i2c_adap[0].i2c_adap, &fc->fc_i2c_adap[0]);
  189. i2c_set_adapdata(&fc->fc_i2c_adap[1].i2c_adap, &fc->fc_i2c_adap[1]);
  190. i2c_set_adapdata(&fc->fc_i2c_adap[2].i2c_adap, &fc->fc_i2c_adap[2]);
  191. fc->fc_i2c_adap[0].i2c_adap.class =
  192. fc->fc_i2c_adap[1].i2c_adap.class =
  193. fc->fc_i2c_adap[2].i2c_adap.class = I2C_CLASS_TV_DIGITAL;
  194. fc->fc_i2c_adap[0].i2c_adap.algo =
  195. fc->fc_i2c_adap[1].i2c_adap.algo =
  196. fc->fc_i2c_adap[2].i2c_adap.algo = &flexcop_algo;
  197. fc->fc_i2c_adap[0].i2c_adap.algo_data =
  198. fc->fc_i2c_adap[1].i2c_adap.algo_data =
  199. fc->fc_i2c_adap[2].i2c_adap.algo_data = NULL;
  200. fc->fc_i2c_adap[0].i2c_adap.dev.parent =
  201. fc->fc_i2c_adap[1].i2c_adap.dev.parent =
  202. fc->fc_i2c_adap[2].i2c_adap.dev.parent = fc->dev;
  203. ret = i2c_add_adapter(&fc->fc_i2c_adap[0].i2c_adap);
  204. if (ret < 0)
  205. return ret;
  206. ret = i2c_add_adapter(&fc->fc_i2c_adap[1].i2c_adap);
  207. if (ret < 0)
  208. goto adap_1_failed;
  209. ret = i2c_add_adapter(&fc->fc_i2c_adap[2].i2c_adap);
  210. if (ret < 0)
  211. goto adap_2_failed;
  212. fc->init_state |= FC_STATE_I2C_INIT;
  213. return 0;
  214. adap_2_failed:
  215. i2c_del_adapter(&fc->fc_i2c_adap[1].i2c_adap);
  216. adap_1_failed:
  217. i2c_del_adapter(&fc->fc_i2c_adap[0].i2c_adap);
  218. return ret;
  219. }
  220. void flexcop_i2c_exit(struct flexcop_device *fc)
  221. {
  222. if (fc->init_state & FC_STATE_I2C_INIT) {
  223. i2c_del_adapter(&fc->fc_i2c_adap[2].i2c_adap);
  224. i2c_del_adapter(&fc->fc_i2c_adap[1].i2c_adap);
  225. i2c_del_adapter(&fc->fc_i2c_adap[0].i2c_adap);
  226. }
  227. fc->init_state &= ~FC_STATE_I2C_INIT;
  228. }