flexcop-i2c.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Linux driver for digital TV devices equipped with B2C2 FlexcopII(b)/III
  3. * flexcop-i2c.c - flexcop internal 2Wire bus (I2C) and dvb i2c initialization
  4. * see flexcop.c for copyright information
  5. */
  6. #include "flexcop.h"
  7. #define FC_MAX_I2C_RETRIES 100000
  8. static int flexcop_i2c_operation(struct flexcop_device *fc,
  9. flexcop_ibi_value *r100)
  10. {
  11. int i;
  12. flexcop_ibi_value r;
  13. r100->tw_sm_c_100.working_start = 1;
  14. deb_i2c("r100 before: %08x\n",r100->raw);
  15. fc->write_ibi_reg(fc, tw_sm_c_100, ibi_zero);
  16. fc->write_ibi_reg(fc, tw_sm_c_100, *r100); /* initiating i2c operation */
  17. for (i = 0; i < FC_MAX_I2C_RETRIES; i++) {
  18. r = fc->read_ibi_reg(fc, tw_sm_c_100);
  19. if (!r.tw_sm_c_100.no_base_addr_ack_error) {
  20. if (r.tw_sm_c_100.st_done) {
  21. *r100 = r;
  22. deb_i2c("i2c success\n");
  23. return 0;
  24. }
  25. } else {
  26. deb_i2c("suffering from an i2c ack_error\n");
  27. return -EREMOTEIO;
  28. }
  29. }
  30. deb_i2c("tried %d times i2c operation, "
  31. "never finished or too many ack errors.\n", i);
  32. return -EREMOTEIO;
  33. }
  34. static int flexcop_i2c_read4(struct flexcop_i2c_adapter *i2c,
  35. flexcop_ibi_value r100, u8 *buf)
  36. {
  37. flexcop_ibi_value r104;
  38. int len = r100.tw_sm_c_100.total_bytes,
  39. /* remember total_bytes is buflen-1 */
  40. ret;
  41. /* work-around to have CableStar2 and SkyStar2 rev 2.7 work
  42. * correctly:
  43. *
  44. * the ITD1000 is behind an i2c-gate which closes automatically
  45. * after an i2c-transaction the STV0297 needs 2 consecutive reads
  46. * one with no_base_addr = 0 and one with 1
  47. *
  48. * those two work-arounds are conflictin: we check for the card
  49. * type, it is set when probing the ITD1000 */
  50. if (i2c->fc->dev_type == FC_SKY_REV27)
  51. r100.tw_sm_c_100.no_base_addr_ack_error = i2c->no_base_addr;
  52. ret = flexcop_i2c_operation(i2c->fc, &r100);
  53. if (ret != 0) {
  54. deb_i2c("Retrying operation\n");
  55. r100.tw_sm_c_100.no_base_addr_ack_error = i2c->no_base_addr;
  56. ret = flexcop_i2c_operation(i2c->fc, &r100);
  57. }
  58. if (ret != 0) {
  59. deb_i2c("read failed. %d\n", ret);
  60. return ret;
  61. }
  62. buf[0] = r100.tw_sm_c_100.data1_reg;
  63. if (len > 0) {
  64. r104 = i2c->fc->read_ibi_reg(i2c->fc, tw_sm_c_104);
  65. deb_i2c("read: r100: %08x, r104: %08x\n", r100.raw, r104.raw);
  66. /* there is at least one more byte, otherwise we wouldn't be here */
  67. buf[1] = r104.tw_sm_c_104.data2_reg;
  68. if (len > 1) buf[2] = r104.tw_sm_c_104.data3_reg;
  69. if (len > 2) buf[3] = r104.tw_sm_c_104.data4_reg;
  70. }
  71. return 0;
  72. }
  73. static int flexcop_i2c_write4(struct flexcop_device *fc,
  74. flexcop_ibi_value r100, u8 *buf)
  75. {
  76. flexcop_ibi_value r104;
  77. int len = r100.tw_sm_c_100.total_bytes; /* remember total_bytes is buflen-1 */
  78. r104.raw = 0;
  79. /* there is at least one byte, otherwise we wouldn't be here */
  80. r100.tw_sm_c_100.data1_reg = buf[0];
  81. r104.tw_sm_c_104.data2_reg = len > 0 ? buf[1] : 0;
  82. r104.tw_sm_c_104.data3_reg = len > 1 ? buf[2] : 0;
  83. r104.tw_sm_c_104.data4_reg = len > 2 ? buf[3] : 0;
  84. deb_i2c("write: r100: %08x, r104: %08x\n", r100.raw, r104.raw);
  85. /* write the additional i2c data before doing the actual i2c operation */
  86. fc->write_ibi_reg(fc, tw_sm_c_104, r104);
  87. return flexcop_i2c_operation(fc, &r100);
  88. }
  89. int flexcop_i2c_request(struct flexcop_i2c_adapter *i2c,
  90. flexcop_access_op_t op, u8 chipaddr, u8 addr, u8 *buf, u16 len)
  91. {
  92. int ret;
  93. #ifdef DUMP_I2C_MESSAGES
  94. int i;
  95. #endif
  96. u16 bytes_to_transfer;
  97. flexcop_ibi_value r100;
  98. deb_i2c("op = %d\n",op);
  99. r100.raw = 0;
  100. r100.tw_sm_c_100.chipaddr = chipaddr;
  101. r100.tw_sm_c_100.twoWS_rw = op;
  102. r100.tw_sm_c_100.twoWS_port_reg = i2c->port;
  103. #ifdef DUMP_I2C_MESSAGES
  104. printk(KERN_DEBUG "%d ", i2c->port);
  105. if (op == FC_READ)
  106. printk("rd(");
  107. else
  108. printk("wr(");
  109. printk("%02x): %02x ", chipaddr, addr);
  110. #endif
  111. /* in that case addr is the only value ->
  112. * we write it twice as baseaddr and val0
  113. * BBTI is doing it like that for ISL6421 at least */
  114. if (i2c->no_base_addr && len == 0 && op == FC_WRITE) {
  115. buf = &addr;
  116. len = 1;
  117. }
  118. while (len != 0) {
  119. bytes_to_transfer = len > 4 ? 4 : len;
  120. r100.tw_sm_c_100.total_bytes = bytes_to_transfer - 1;
  121. r100.tw_sm_c_100.baseaddr = addr;
  122. if (op == FC_READ)
  123. ret = flexcop_i2c_read4(i2c, r100, buf);
  124. else
  125. ret = flexcop_i2c_write4(i2c->fc, r100, buf);
  126. #ifdef DUMP_I2C_MESSAGES
  127. for (i = 0; i < bytes_to_transfer; i++)
  128. printk("%02x ", buf[i]);
  129. #endif
  130. if (ret < 0)
  131. return ret;
  132. buf += bytes_to_transfer;
  133. addr += bytes_to_transfer;
  134. len -= bytes_to_transfer;
  135. }
  136. #ifdef DUMP_I2C_MESSAGES
  137. printk("\n");
  138. #endif
  139. return 0;
  140. }
  141. /* exported for PCI i2c */
  142. EXPORT_SYMBOL(flexcop_i2c_request);
  143. /* master xfer callback for demodulator */
  144. static int flexcop_master_xfer(struct i2c_adapter *i2c_adap,
  145. struct i2c_msg msgs[], int num)
  146. {
  147. struct flexcop_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
  148. int i, ret = 0;
  149. /* Some drivers use 1 byte or 0 byte reads as probes, which this
  150. * driver doesn't support. These probes will always fail, so this
  151. * hack makes them always succeed. If one knew how, it would of
  152. * course be better to actually do the read. */
  153. if (num == 1 && msgs[0].flags == I2C_M_RD && msgs[0].len <= 1)
  154. return 1;
  155. if (mutex_lock_interruptible(&i2c->fc->i2c_mutex))
  156. return -ERESTARTSYS;
  157. for (i = 0; i < num; i++) {
  158. /* reading */
  159. if (i+1 < num && (msgs[i+1].flags == I2C_M_RD)) {
  160. ret = i2c->fc->i2c_request(i2c, FC_READ, msgs[i].addr,
  161. msgs[i].buf[0], msgs[i+1].buf,
  162. msgs[i+1].len);
  163. i++; /* skip the following message */
  164. } else /* writing */
  165. ret = i2c->fc->i2c_request(i2c, FC_WRITE, msgs[i].addr,
  166. msgs[i].buf[0], &msgs[i].buf[1],
  167. msgs[i].len - 1);
  168. if (ret < 0) {
  169. deb_i2c("i2c master_xfer failed");
  170. break;
  171. }
  172. }
  173. mutex_unlock(&i2c->fc->i2c_mutex);
  174. if (ret == 0)
  175. ret = num;
  176. return ret;
  177. }
  178. static u32 flexcop_i2c_func(struct i2c_adapter *adapter)
  179. {
  180. return I2C_FUNC_I2C;
  181. }
  182. static struct i2c_algorithm flexcop_algo = {
  183. .master_xfer = flexcop_master_xfer,
  184. .functionality = flexcop_i2c_func,
  185. };
  186. int flexcop_i2c_init(struct flexcop_device *fc)
  187. {
  188. int ret;
  189. mutex_init(&fc->i2c_mutex);
  190. fc->fc_i2c_adap[0].fc = fc;
  191. fc->fc_i2c_adap[1].fc = fc;
  192. fc->fc_i2c_adap[2].fc = fc;
  193. fc->fc_i2c_adap[0].port = FC_I2C_PORT_DEMOD;
  194. fc->fc_i2c_adap[1].port = FC_I2C_PORT_EEPROM;
  195. fc->fc_i2c_adap[2].port = FC_I2C_PORT_TUNER;
  196. strlcpy(fc->fc_i2c_adap[0].i2c_adap.name, "B2C2 FlexCop I2C to demod",
  197. sizeof(fc->fc_i2c_adap[0].i2c_adap.name));
  198. strlcpy(fc->fc_i2c_adap[1].i2c_adap.name, "B2C2 FlexCop I2C to eeprom",
  199. sizeof(fc->fc_i2c_adap[1].i2c_adap.name));
  200. strlcpy(fc->fc_i2c_adap[2].i2c_adap.name, "B2C2 FlexCop I2C to tuner",
  201. sizeof(fc->fc_i2c_adap[2].i2c_adap.name));
  202. i2c_set_adapdata(&fc->fc_i2c_adap[0].i2c_adap, &fc->fc_i2c_adap[0]);
  203. i2c_set_adapdata(&fc->fc_i2c_adap[1].i2c_adap, &fc->fc_i2c_adap[1]);
  204. i2c_set_adapdata(&fc->fc_i2c_adap[2].i2c_adap, &fc->fc_i2c_adap[2]);
  205. fc->fc_i2c_adap[0].i2c_adap.algo =
  206. fc->fc_i2c_adap[1].i2c_adap.algo =
  207. fc->fc_i2c_adap[2].i2c_adap.algo = &flexcop_algo;
  208. fc->fc_i2c_adap[0].i2c_adap.algo_data =
  209. fc->fc_i2c_adap[1].i2c_adap.algo_data =
  210. fc->fc_i2c_adap[2].i2c_adap.algo_data = NULL;
  211. fc->fc_i2c_adap[0].i2c_adap.dev.parent =
  212. fc->fc_i2c_adap[1].i2c_adap.dev.parent =
  213. fc->fc_i2c_adap[2].i2c_adap.dev.parent = fc->dev;
  214. ret = i2c_add_adapter(&fc->fc_i2c_adap[0].i2c_adap);
  215. if (ret < 0)
  216. return ret;
  217. ret = i2c_add_adapter(&fc->fc_i2c_adap[1].i2c_adap);
  218. if (ret < 0)
  219. goto adap_1_failed;
  220. ret = i2c_add_adapter(&fc->fc_i2c_adap[2].i2c_adap);
  221. if (ret < 0)
  222. goto adap_2_failed;
  223. fc->init_state |= FC_STATE_I2C_INIT;
  224. return 0;
  225. adap_2_failed:
  226. i2c_del_adapter(&fc->fc_i2c_adap[1].i2c_adap);
  227. adap_1_failed:
  228. i2c_del_adapter(&fc->fc_i2c_adap[0].i2c_adap);
  229. return ret;
  230. }
  231. void flexcop_i2c_exit(struct flexcop_device *fc)
  232. {
  233. if (fc->init_state & FC_STATE_I2C_INIT) {
  234. i2c_del_adapter(&fc->fc_i2c_adap[2].i2c_adap);
  235. i2c_del_adapter(&fc->fc_i2c_adap[1].i2c_adap);
  236. i2c_del_adapter(&fc->fc_i2c_adap[0].i2c_adap);
  237. }
  238. fc->init_state &= ~FC_STATE_I2C_INIT;
  239. }