dibx000_common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #include <linux/i2c.h>
  2. #include "dibx000_common.h"
  3. static int debug;
  4. module_param(debug, int, 0644);
  5. MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
  6. #define dprintk(args...) do { if (debug) { printk(KERN_DEBUG "DiBX000: "); printk(args); printk("\n"); } } while (0)
  7. static int dibx000_write_word(struct dibx000_i2c_master *mst, u16 reg, u16 val)
  8. {
  9. u8 b[4] = {
  10. (reg >> 8) & 0xff, reg & 0xff,
  11. (val >> 8) & 0xff, val & 0xff,
  12. };
  13. struct i2c_msg msg = {
  14. .addr = mst->i2c_addr,.flags = 0,.buf = b,.len = 4
  15. };
  16. return i2c_transfer(mst->i2c_adap, &msg, 1) != 1 ? -EREMOTEIO : 0;
  17. }
  18. static u16 dibx000_read_word(struct dibx000_i2c_master *mst, u16 reg)
  19. {
  20. u8 wb[2] = { reg >> 8, reg & 0xff };
  21. u8 rb[2];
  22. struct i2c_msg msg[2] = {
  23. {.addr = mst->i2c_addr, .flags = 0, .buf = wb, .len = 2},
  24. {.addr = mst->i2c_addr, .flags = I2C_M_RD, .buf = rb, .len = 2},
  25. };
  26. if (i2c_transfer(mst->i2c_adap, msg, 2) != 2)
  27. dprintk("i2c read error on %d", reg);
  28. return (rb[0] << 8) | rb[1];
  29. }
  30. static int dibx000_is_i2c_done(struct dibx000_i2c_master *mst)
  31. {
  32. int i = 100;
  33. u16 status;
  34. while (((status = dibx000_read_word(mst, mst->base_reg + 2)) & 0x0100) == 0 && --i > 0)
  35. ;
  36. /* i2c timed out */
  37. if (i == 0)
  38. return -EREMOTEIO;
  39. /* no acknowledge */
  40. if ((status & 0x0080) == 0)
  41. return -EREMOTEIO;
  42. return 0;
  43. }
  44. static int dibx000_master_i2c_write(struct dibx000_i2c_master *mst, struct i2c_msg *msg, u8 stop)
  45. {
  46. u16 data;
  47. u16 da;
  48. u16 i;
  49. u16 txlen = msg->len, len;
  50. const u8 *b = msg->buf;
  51. while (txlen) {
  52. dibx000_read_word(mst, mst->base_reg + 2);
  53. len = txlen > 8 ? 8 : txlen;
  54. for (i = 0; i < len; i += 2) {
  55. data = *b++ << 8;
  56. if (i+1 < len)
  57. data |= *b++;
  58. dibx000_write_word(mst, mst->base_reg, data);
  59. }
  60. da = (((u8) (msg->addr)) << 9) |
  61. (1 << 8) |
  62. (1 << 7) |
  63. (0 << 6) |
  64. (0 << 5) |
  65. ((len & 0x7) << 2) |
  66. (0 << 1) |
  67. (0 << 0);
  68. if (txlen == msg->len)
  69. da |= 1 << 5; /* start */
  70. if (txlen-len == 0 && stop)
  71. da |= 1 << 6; /* stop */
  72. dibx000_write_word(mst, mst->base_reg+1, da);
  73. if (dibx000_is_i2c_done(mst) != 0)
  74. return -EREMOTEIO;
  75. txlen -= len;
  76. }
  77. return 0;
  78. }
  79. static int dibx000_master_i2c_read(struct dibx000_i2c_master *mst, struct i2c_msg *msg)
  80. {
  81. u16 da;
  82. u8 *b = msg->buf;
  83. u16 rxlen = msg->len, len;
  84. while (rxlen) {
  85. len = rxlen > 8 ? 8 : rxlen;
  86. da = (((u8) (msg->addr)) << 9) |
  87. (1 << 8) |
  88. (1 << 7) |
  89. (0 << 6) |
  90. (0 << 5) |
  91. ((len & 0x7) << 2) |
  92. (1 << 1) |
  93. (0 << 0);
  94. if (rxlen == msg->len)
  95. da |= 1 << 5; /* start */
  96. if (rxlen-len == 0)
  97. da |= 1 << 6; /* stop */
  98. dibx000_write_word(mst, mst->base_reg+1, da);
  99. if (dibx000_is_i2c_done(mst) != 0)
  100. return -EREMOTEIO;
  101. rxlen -= len;
  102. while (len) {
  103. da = dibx000_read_word(mst, mst->base_reg);
  104. *b++ = (da >> 8) & 0xff;
  105. len--;
  106. if (len >= 1) {
  107. *b++ = da & 0xff;
  108. len--;
  109. }
  110. }
  111. }
  112. return 0;
  113. }
  114. int dibx000_i2c_set_speed(struct i2c_adapter *i2c_adap, u16 speed)
  115. {
  116. struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
  117. if (mst->device_rev < DIB7000MC && speed < 235)
  118. speed = 235;
  119. return dibx000_write_word(mst, mst->base_reg + 3, (u16)(60000 / speed));
  120. }
  121. EXPORT_SYMBOL(dibx000_i2c_set_speed);
  122. static u32 dibx000_i2c_func(struct i2c_adapter *adapter)
  123. {
  124. return I2C_FUNC_I2C;
  125. }
  126. static int dibx000_i2c_select_interface(struct dibx000_i2c_master *mst,
  127. enum dibx000_i2c_interface intf)
  128. {
  129. if (mst->device_rev > DIB3000MC && mst->selected_interface != intf) {
  130. dprintk("selecting interface: %d", intf);
  131. mst->selected_interface = intf;
  132. return dibx000_write_word(mst, mst->base_reg + 4, intf);
  133. }
  134. return 0;
  135. }
  136. static int dibx000_i2c_master_xfer_gpio12(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  137. {
  138. struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
  139. int msg_index;
  140. int ret = 0;
  141. dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_1_2);
  142. for (msg_index = 0; msg_index < num; msg_index++) {
  143. if (msg[msg_index].flags & I2C_M_RD) {
  144. ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
  145. if (ret != 0)
  146. return 0;
  147. } else {
  148. ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
  149. if (ret != 0)
  150. return 0;
  151. }
  152. }
  153. return num;
  154. }
  155. static int dibx000_i2c_master_xfer_gpio34(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  156. {
  157. struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
  158. int msg_index;
  159. int ret = 0;
  160. dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_3_4);
  161. for (msg_index = 0; msg_index < num; msg_index++) {
  162. if (msg[msg_index].flags & I2C_M_RD) {
  163. ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
  164. if (ret != 0)
  165. return 0;
  166. } else {
  167. ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
  168. if (ret != 0)
  169. return 0;
  170. }
  171. }
  172. return num;
  173. }
  174. static struct i2c_algorithm dibx000_i2c_master_gpio12_xfer_algo = {
  175. .master_xfer = dibx000_i2c_master_xfer_gpio12,
  176. .functionality = dibx000_i2c_func,
  177. };
  178. static struct i2c_algorithm dibx000_i2c_master_gpio34_xfer_algo = {
  179. .master_xfer = dibx000_i2c_master_xfer_gpio34,
  180. .functionality = dibx000_i2c_func,
  181. };
  182. static int dibx000_i2c_gate_ctrl(struct dibx000_i2c_master *mst, u8 tx[4],
  183. u8 addr, int onoff)
  184. {
  185. u16 val;
  186. if (onoff)
  187. val = addr << 8; // bit 7 = use master or not, if 0, the gate is open
  188. else
  189. val = 1 << 7;
  190. if (mst->device_rev > DIB7000)
  191. val <<= 1;
  192. tx[0] = (((mst->base_reg + 1) >> 8) & 0xff);
  193. tx[1] = ((mst->base_reg + 1) & 0xff);
  194. tx[2] = val >> 8;
  195. tx[3] = val & 0xff;
  196. return 0;
  197. }
  198. static int dibx000_i2c_gated_gpio67_xfer(struct i2c_adapter *i2c_adap,
  199. struct i2c_msg msg[], int num)
  200. {
  201. struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
  202. struct i2c_msg m[2 + num];
  203. u8 tx_open[4], tx_close[4];
  204. memset(m, 0, sizeof(struct i2c_msg) * (2 + num));
  205. dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_6_7);
  206. dibx000_i2c_gate_ctrl(mst, tx_open, msg[0].addr, 1);
  207. m[0].addr = mst->i2c_addr;
  208. m[0].buf = tx_open;
  209. m[0].len = 4;
  210. memcpy(&m[1], msg, sizeof(struct i2c_msg) * num);
  211. dibx000_i2c_gate_ctrl(mst, tx_close, 0, 0);
  212. m[num + 1].addr = mst->i2c_addr;
  213. m[num + 1].buf = tx_close;
  214. m[num + 1].len = 4;
  215. return i2c_transfer(mst->i2c_adap, m, 2 + num) == 2 + num ? num : -EIO;
  216. }
  217. static struct i2c_algorithm dibx000_i2c_gated_gpio67_algo = {
  218. .master_xfer = dibx000_i2c_gated_gpio67_xfer,
  219. .functionality = dibx000_i2c_func,
  220. };
  221. static int dibx000_i2c_gated_tuner_xfer(struct i2c_adapter *i2c_adap,
  222. struct i2c_msg msg[], int num)
  223. {
  224. struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
  225. struct i2c_msg m[2 + num];
  226. u8 tx_open[4], tx_close[4];
  227. memset(m, 0, sizeof(struct i2c_msg) * (2 + num));
  228. dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
  229. dibx000_i2c_gate_ctrl(mst, tx_open, msg[0].addr, 1);
  230. m[0].addr = mst->i2c_addr;
  231. m[0].buf = tx_open;
  232. m[0].len = 4;
  233. memcpy(&m[1], msg, sizeof(struct i2c_msg) * num);
  234. dibx000_i2c_gate_ctrl(mst, tx_close, 0, 0);
  235. m[num + 1].addr = mst->i2c_addr;
  236. m[num + 1].buf = tx_close;
  237. m[num + 1].len = 4;
  238. return i2c_transfer(mst->i2c_adap, m, 2 + num) == 2 + num ? num : -EIO;
  239. }
  240. static struct i2c_algorithm dibx000_i2c_gated_tuner_algo = {
  241. .master_xfer = dibx000_i2c_gated_tuner_xfer,
  242. .functionality = dibx000_i2c_func,
  243. };
  244. struct i2c_adapter *dibx000_get_i2c_adapter(struct dibx000_i2c_master *mst,
  245. enum dibx000_i2c_interface intf,
  246. int gating)
  247. {
  248. struct i2c_adapter *i2c = NULL;
  249. switch (intf) {
  250. case DIBX000_I2C_INTERFACE_TUNER:
  251. if (gating)
  252. i2c = &mst->gated_tuner_i2c_adap;
  253. break;
  254. case DIBX000_I2C_INTERFACE_GPIO_1_2:
  255. if (!gating)
  256. i2c = &mst->master_i2c_adap_gpio12;
  257. break;
  258. case DIBX000_I2C_INTERFACE_GPIO_3_4:
  259. if (!gating)
  260. i2c = &mst->master_i2c_adap_gpio34;
  261. break;
  262. case DIBX000_I2C_INTERFACE_GPIO_6_7:
  263. if (gating)
  264. i2c = &mst->master_i2c_adap_gpio67;
  265. break;
  266. default:
  267. printk(KERN_ERR "DiBX000: incorrect I2C interface selected\n");
  268. break;
  269. }
  270. return i2c;
  271. }
  272. EXPORT_SYMBOL(dibx000_get_i2c_adapter);
  273. void dibx000_reset_i2c_master(struct dibx000_i2c_master *mst)
  274. {
  275. /* initialize the i2c-master by closing the gate */
  276. u8 tx[4];
  277. struct i2c_msg m = {.addr = mst->i2c_addr,.buf = tx,.len = 4 };
  278. dibx000_i2c_gate_ctrl(mst, tx, 0, 0);
  279. i2c_transfer(mst->i2c_adap, &m, 1);
  280. mst->selected_interface = 0xff; // the first time force a select of the I2C
  281. dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
  282. }
  283. EXPORT_SYMBOL(dibx000_reset_i2c_master);
  284. static int i2c_adapter_init(struct i2c_adapter *i2c_adap,
  285. struct i2c_algorithm *algo, const char *name,
  286. struct dibx000_i2c_master *mst)
  287. {
  288. strncpy(i2c_adap->name, name, sizeof(i2c_adap->name));
  289. i2c_adap->algo = algo;
  290. i2c_adap->algo_data = NULL;
  291. i2c_set_adapdata(i2c_adap, mst);
  292. if (i2c_add_adapter(i2c_adap) < 0)
  293. return -ENODEV;
  294. return 0;
  295. }
  296. int dibx000_init_i2c_master(struct dibx000_i2c_master *mst, u16 device_rev,
  297. struct i2c_adapter *i2c_adap, u8 i2c_addr)
  298. {
  299. u8 tx[4];
  300. struct i2c_msg m = {.addr = i2c_addr >> 1,.buf = tx,.len = 4 };
  301. mst->device_rev = device_rev;
  302. mst->i2c_adap = i2c_adap;
  303. mst->i2c_addr = i2c_addr >> 1;
  304. if (device_rev == DIB7000P || device_rev == DIB8000)
  305. mst->base_reg = 1024;
  306. else
  307. mst->base_reg = 768;
  308. mst->gated_tuner_i2c_adap.dev.parent = mst->i2c_adap->dev.parent;
  309. if (i2c_adapter_init
  310. (&mst->gated_tuner_i2c_adap, &dibx000_i2c_gated_tuner_algo,
  311. "DiBX000 tuner I2C bus", mst) != 0)
  312. printk(KERN_ERR
  313. "DiBX000: could not initialize the tuner i2c_adapter\n");
  314. mst->master_i2c_adap_gpio12.dev.parent = mst->i2c_adap->dev.parent;
  315. if (i2c_adapter_init
  316. (&mst->master_i2c_adap_gpio12, &dibx000_i2c_master_gpio12_xfer_algo,
  317. "DiBX000 master GPIO12 I2C bus", mst) != 0)
  318. printk(KERN_ERR
  319. "DiBX000: could not initialize the master i2c_adapter\n");
  320. mst->master_i2c_adap_gpio34.dev.parent = mst->i2c_adap->dev.parent;
  321. if (i2c_adapter_init
  322. (&mst->master_i2c_adap_gpio34, &dibx000_i2c_master_gpio34_xfer_algo,
  323. "DiBX000 master GPIO34 I2C bus", mst) != 0)
  324. printk(KERN_ERR
  325. "DiBX000: could not initialize the master i2c_adapter\n");
  326. mst->master_i2c_adap_gpio67.dev.parent = mst->i2c_adap->dev.parent;
  327. if (i2c_adapter_init
  328. (&mst->master_i2c_adap_gpio67, &dibx000_i2c_gated_gpio67_algo,
  329. "DiBX000 master GPIO67 I2C bus", mst) != 0)
  330. printk(KERN_ERR
  331. "DiBX000: could not initialize the master i2c_adapter\n");
  332. /* initialize the i2c-master by closing the gate */
  333. dibx000_i2c_gate_ctrl(mst, tx, 0, 0);
  334. return i2c_transfer(i2c_adap, &m, 1) == 1;
  335. }
  336. EXPORT_SYMBOL(dibx000_init_i2c_master);
  337. void dibx000_exit_i2c_master(struct dibx000_i2c_master *mst)
  338. {
  339. i2c_del_adapter(&mst->gated_tuner_i2c_adap);
  340. i2c_del_adapter(&mst->master_i2c_adap_gpio12);
  341. i2c_del_adapter(&mst->master_i2c_adap_gpio34);
  342. i2c_del_adapter(&mst->master_i2c_adap_gpio67);
  343. }
  344. EXPORT_SYMBOL(dibx000_exit_i2c_master);
  345. u32 systime(void)
  346. {
  347. struct timespec t;
  348. t = current_kernel_time();
  349. return (t.tv_sec * 10000) + (t.tv_nsec / 100000);
  350. }
  351. EXPORT_SYMBOL(systime);
  352. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  353. MODULE_DESCRIPTION("Common function the DiBcom demodulator family");
  354. MODULE_LICENSE("GPL");