dibx000_common.c 13 KB

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