dibx000_common.c 12 KB

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