dibx000_common.c 12 KB

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