flexcop-i2c.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. r100.tw_sm_c_100.no_base_addr_ack_error = i2c->no_base_addr;
  42. ret = flexcop_i2c_operation(i2c->fc, &r100);
  43. if (ret != 0) {
  44. deb_i2c("read failed. %d\n", ret);
  45. return ret;
  46. }
  47. buf[0] = r100.tw_sm_c_100.data1_reg;
  48. if (len > 0) {
  49. r104 = i2c->fc->read_ibi_reg(i2c->fc, tw_sm_c_104);
  50. deb_i2c("read: r100: %08x, r104: %08x\n", r100.raw, r104.raw);
  51. /* there is at least one more byte, otherwise we wouldn't be here */
  52. buf[1] = r104.tw_sm_c_104.data2_reg;
  53. if (len > 1) buf[2] = r104.tw_sm_c_104.data3_reg;
  54. if (len > 2) buf[3] = r104.tw_sm_c_104.data4_reg;
  55. }
  56. return 0;
  57. }
  58. static int flexcop_i2c_write4(struct flexcop_device *fc, flexcop_ibi_value r100, u8 *buf)
  59. {
  60. flexcop_ibi_value r104;
  61. int len = r100.tw_sm_c_100.total_bytes; /* remember total_bytes is buflen-1 */
  62. r104.raw = 0;
  63. /* there is at least one byte, otherwise we wouldn't be here */
  64. r100.tw_sm_c_100.data1_reg = buf[0];
  65. r104.tw_sm_c_104.data2_reg = len > 0 ? buf[1] : 0;
  66. r104.tw_sm_c_104.data3_reg = len > 1 ? buf[2] : 0;
  67. r104.tw_sm_c_104.data4_reg = len > 2 ? buf[3] : 0;
  68. deb_i2c("write: r100: %08x, r104: %08x\n", r100.raw, r104.raw);
  69. /* write the additional i2c data before doing the actual i2c operation */
  70. fc->write_ibi_reg(fc, tw_sm_c_104, r104);
  71. return flexcop_i2c_operation(fc, &r100);
  72. }
  73. int flexcop_i2c_request(struct flexcop_i2c_adapter *i2c,
  74. flexcop_access_op_t op, u8 chipaddr, u8 addr, u8 *buf, u16 len)
  75. {
  76. int ret;
  77. #ifdef DUMP_I2C_MESSAGES
  78. int i;
  79. #endif
  80. u16 bytes_to_transfer;
  81. flexcop_ibi_value r100;
  82. deb_i2c("op = %d\n",op);
  83. r100.raw = 0;
  84. r100.tw_sm_c_100.chipaddr = chipaddr;
  85. r100.tw_sm_c_100.twoWS_rw = op;
  86. r100.tw_sm_c_100.twoWS_port_reg = i2c->port;
  87. #ifdef DUMP_I2C_MESSAGES
  88. printk(KERN_DEBUG "%d ", i2c->port);
  89. if (op == FC_READ)
  90. printk("rd(");
  91. else
  92. printk("wr(");
  93. printk("%02x): %02x ", chipaddr, addr);
  94. #endif
  95. /* in that case addr is the only value ->
  96. * we write it twice as baseaddr and val0
  97. * BBTI is doing it like that for ISL6421 at least */
  98. if (i2c->no_base_addr && len == 0 && op == FC_WRITE) {
  99. buf = &addr;
  100. len = 1;
  101. }
  102. while (len != 0) {
  103. bytes_to_transfer = len > 4 ? 4 : len;
  104. r100.tw_sm_c_100.total_bytes = bytes_to_transfer - 1;
  105. r100.tw_sm_c_100.baseaddr = addr;
  106. if (op == FC_READ)
  107. ret = flexcop_i2c_read4(i2c, r100, buf);
  108. else
  109. ret = flexcop_i2c_write4(i2c->fc, r100, buf);
  110. #ifdef DUMP_I2C_MESSAGES
  111. for (i = 0; i < bytes_to_transfer; i++)
  112. printk("%02x ", buf[i]);
  113. #endif
  114. if (ret < 0)
  115. return ret;
  116. buf += bytes_to_transfer;
  117. addr += bytes_to_transfer;
  118. len -= bytes_to_transfer;
  119. }
  120. #ifdef DUMP_I2C_MESSAGES
  121. printk("\n");
  122. #endif
  123. return 0;
  124. }
  125. /* exported for PCI i2c */
  126. EXPORT_SYMBOL(flexcop_i2c_request);
  127. /* master xfer callback for demodulator */
  128. static int flexcop_master_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
  129. {
  130. struct flexcop_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
  131. int i, ret = 0;
  132. /* Some drivers use 1 byte or 0 byte reads as probes, which this
  133. * driver doesn't support. These probes will always fail, so this
  134. * hack makes them always succeed. If one knew how, it would of
  135. * course be better to actually do the read. */
  136. if (num == 1 && msgs[0].flags == I2C_M_RD && msgs[0].len <= 1)
  137. return 1;
  138. if (mutex_lock_interruptible(&i2c->fc->i2c_mutex))
  139. return -ERESTARTSYS;
  140. for (i = 0; i < num; i++) {
  141. /* reading */
  142. if (i+1 < num && (msgs[i+1].flags == I2C_M_RD)) {
  143. ret = i2c->fc->i2c_request(i2c, FC_READ, msgs[i].addr,
  144. msgs[i].buf[0], msgs[i+1].buf, msgs[i+1].len);
  145. i++; /* skip the following message */
  146. } else /* writing */
  147. ret = i2c->fc->i2c_request(i2c, FC_WRITE, msgs[i].addr,
  148. msgs[i].buf[0], &msgs[i].buf[1],
  149. msgs[i].len - 1);
  150. if (ret < 0) {
  151. err("i2c master_xfer failed");
  152. break;
  153. }
  154. }
  155. mutex_unlock(&i2c->fc->i2c_mutex);
  156. if (ret == 0)
  157. ret = num;
  158. return ret;
  159. }
  160. static u32 flexcop_i2c_func(struct i2c_adapter *adapter)
  161. {
  162. return I2C_FUNC_I2C;
  163. }
  164. static struct i2c_algorithm flexcop_algo = {
  165. .master_xfer = flexcop_master_xfer,
  166. .functionality = flexcop_i2c_func,
  167. };
  168. int flexcop_i2c_init(struct flexcop_device *fc)
  169. {
  170. int ret;
  171. mutex_init(&fc->i2c_mutex);
  172. fc->fc_i2c_adap[0].fc = fc;
  173. fc->fc_i2c_adap[1].fc = fc;
  174. fc->fc_i2c_adap[2].fc = fc;
  175. fc->fc_i2c_adap[0].port = FC_I2C_PORT_DEMOD;
  176. fc->fc_i2c_adap[1].port = FC_I2C_PORT_EEPROM;
  177. fc->fc_i2c_adap[2].port = FC_I2C_PORT_TUNER;
  178. strlcpy(fc->fc_i2c_adap[0].i2c_adap.name, "B2C2 FlexCop I2C to demod",
  179. sizeof(fc->fc_i2c_adap[0].i2c_adap.name));
  180. strlcpy(fc->fc_i2c_adap[1].i2c_adap.name, "B2C2 FlexCop I2C to eeprom",
  181. sizeof(fc->fc_i2c_adap[1].i2c_adap.name));
  182. strlcpy(fc->fc_i2c_adap[2].i2c_adap.name, "B2C2 FlexCop I2C to tuner",
  183. sizeof(fc->fc_i2c_adap[2].i2c_adap.name));
  184. i2c_set_adapdata(&fc->fc_i2c_adap[0].i2c_adap, &fc->fc_i2c_adap[0]);
  185. i2c_set_adapdata(&fc->fc_i2c_adap[1].i2c_adap, &fc->fc_i2c_adap[1]);
  186. i2c_set_adapdata(&fc->fc_i2c_adap[2].i2c_adap, &fc->fc_i2c_adap[2]);
  187. fc->fc_i2c_adap[0].i2c_adap.class =
  188. fc->fc_i2c_adap[1].i2c_adap.class =
  189. fc->fc_i2c_adap[2].i2c_adap.class = I2C_CLASS_TV_DIGITAL;
  190. fc->fc_i2c_adap[0].i2c_adap.algo =
  191. fc->fc_i2c_adap[1].i2c_adap.algo =
  192. fc->fc_i2c_adap[2].i2c_adap.algo = &flexcop_algo;
  193. fc->fc_i2c_adap[0].i2c_adap.algo_data =
  194. fc->fc_i2c_adap[1].i2c_adap.algo_data =
  195. fc->fc_i2c_adap[2].i2c_adap.algo_data = NULL;
  196. fc->fc_i2c_adap[0].i2c_adap.dev.parent =
  197. fc->fc_i2c_adap[1].i2c_adap.dev.parent =
  198. fc->fc_i2c_adap[2].i2c_adap.dev.parent = fc->dev;
  199. ret = i2c_add_adapter(&fc->fc_i2c_adap[0].i2c_adap);
  200. if (ret < 0)
  201. return ret;
  202. ret = i2c_add_adapter(&fc->fc_i2c_adap[1].i2c_adap);
  203. if (ret < 0)
  204. goto adap_1_failed;
  205. ret = i2c_add_adapter(&fc->fc_i2c_adap[2].i2c_adap);
  206. if (ret < 0)
  207. goto adap_2_failed;
  208. fc->init_state |= FC_STATE_I2C_INIT;
  209. return 0;
  210. adap_2_failed:
  211. i2c_del_adapter(&fc->fc_i2c_adap[1].i2c_adap);
  212. adap_1_failed:
  213. i2c_del_adapter(&fc->fc_i2c_adap[0].i2c_adap);
  214. return ret;
  215. }
  216. void flexcop_i2c_exit(struct flexcop_device *fc)
  217. {
  218. if (fc->init_state & FC_STATE_I2C_INIT) {
  219. i2c_del_adapter(&fc->fc_i2c_adap[2].i2c_adap);
  220. i2c_del_adapter(&fc->fc_i2c_adap[1].i2c_adap);
  221. i2c_del_adapter(&fc->fc_i2c_adap[0].i2c_adap);
  222. }
  223. fc->init_state &= ~FC_STATE_I2C_INIT;
  224. }