dibx000_common.c 13 KB

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