dibx000_common.c 13 KB

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