mantis_i2c.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. Mantis PCI bridge driver
  4. Copyright (C) Manu Abraham ([email protected])
  5. */
  6. #include <asm/io.h>
  7. #include <linux/ioport.h>
  8. #include <linux/pci.h>
  9. #include <linux/i2c.h>
  10. #include <media/dmxdev.h>
  11. #include <media/dvbdev.h>
  12. #include <media/dvb_demux.h>
  13. #include <media/dvb_frontend.h>
  14. #include <media/dvb_net.h>
  15. #include "mantis_common.h"
  16. #include "mantis_reg.h"
  17. #include "mantis_i2c.h"
  18. #define TRIALS 10000
  19. static int mantis_i2c_read(struct mantis_pci *mantis, const struct i2c_msg *msg)
  20. {
  21. u32 rxd, i, stat, trials;
  22. dprintk(MANTIS_INFO, 0, " %s: Address=[0x%02x] <R>[ ",
  23. __func__, msg->addr);
  24. for (i = 0; i < msg->len; i++) {
  25. rxd = (msg->addr << 25) | (1 << 24)
  26. | MANTIS_I2C_RATE_3
  27. | MANTIS_I2C_STOP
  28. | MANTIS_I2C_PGMODE;
  29. if (i == (msg->len - 1))
  30. rxd &= ~MANTIS_I2C_STOP;
  31. mmwrite(MANTIS_INT_I2CDONE, MANTIS_INT_STAT);
  32. mmwrite(rxd, MANTIS_I2CDATA_CTL);
  33. /* wait for xfer completion */
  34. for (trials = 0; trials < TRIALS; trials++) {
  35. stat = mmread(MANTIS_INT_STAT);
  36. if (stat & MANTIS_INT_I2CDONE)
  37. break;
  38. }
  39. dprintk(MANTIS_TMG, 0, "I2CDONE: trials=%d\n", trials);
  40. /* wait for xfer completion */
  41. for (trials = 0; trials < TRIALS; trials++) {
  42. stat = mmread(MANTIS_INT_STAT);
  43. if (stat & MANTIS_INT_I2CRACK)
  44. break;
  45. }
  46. dprintk(MANTIS_TMG, 0, "I2CRACK: trials=%d\n", trials);
  47. rxd = mmread(MANTIS_I2CDATA_CTL);
  48. msg->buf[i] = (u8)((rxd >> 8) & 0xFF);
  49. dprintk(MANTIS_INFO, 0, "%02x ", msg->buf[i]);
  50. }
  51. dprintk(MANTIS_INFO, 0, "]\n");
  52. return 0;
  53. }
  54. static int mantis_i2c_write(struct mantis_pci *mantis, const struct i2c_msg *msg)
  55. {
  56. int i;
  57. u32 txd = 0, stat, trials;
  58. dprintk(MANTIS_INFO, 0, " %s: Address=[0x%02x] <W>[ ",
  59. __func__, msg->addr);
  60. for (i = 0; i < msg->len; i++) {
  61. dprintk(MANTIS_INFO, 0, "%02x ", msg->buf[i]);
  62. txd = (msg->addr << 25) | (msg->buf[i] << 8)
  63. | MANTIS_I2C_RATE_3
  64. | MANTIS_I2C_STOP
  65. | MANTIS_I2C_PGMODE;
  66. if (i == (msg->len - 1))
  67. txd &= ~MANTIS_I2C_STOP;
  68. mmwrite(MANTIS_INT_I2CDONE, MANTIS_INT_STAT);
  69. mmwrite(txd, MANTIS_I2CDATA_CTL);
  70. /* wait for xfer completion */
  71. for (trials = 0; trials < TRIALS; trials++) {
  72. stat = mmread(MANTIS_INT_STAT);
  73. if (stat & MANTIS_INT_I2CDONE)
  74. break;
  75. }
  76. dprintk(MANTIS_TMG, 0, "I2CDONE: trials=%d\n", trials);
  77. /* wait for xfer completion */
  78. for (trials = 0; trials < TRIALS; trials++) {
  79. stat = mmread(MANTIS_INT_STAT);
  80. if (stat & MANTIS_INT_I2CRACK)
  81. break;
  82. }
  83. dprintk(MANTIS_TMG, 0, "I2CRACK: trials=%d\n", trials);
  84. }
  85. dprintk(MANTIS_INFO, 0, "]\n");
  86. return 0;
  87. }
  88. static int mantis_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
  89. {
  90. int ret = 0, i = 0, trials;
  91. u32 stat, data, txd;
  92. struct mantis_pci *mantis;
  93. struct mantis_hwconfig *config;
  94. mantis = i2c_get_adapdata(adapter);
  95. BUG_ON(!mantis);
  96. config = mantis->hwconfig;
  97. BUG_ON(!config);
  98. dprintk(MANTIS_DEBUG, 1, "Messages:%d", num);
  99. mutex_lock(&mantis->i2c_lock);
  100. while (i < num) {
  101. /* Byte MODE */
  102. if ((config->i2c_mode & MANTIS_BYTE_MODE) &&
  103. ((i + 1) < num) &&
  104. (msgs[i].len < 2) &&
  105. (msgs[i + 1].len < 2) &&
  106. (msgs[i + 1].flags & I2C_M_RD)) {
  107. dprintk(MANTIS_DEBUG, 0, " Byte MODE:\n");
  108. /* Read operation */
  109. txd = msgs[i].addr << 25 | (0x1 << 24)
  110. | (msgs[i].buf[0] << 16)
  111. | MANTIS_I2C_RATE_3;
  112. mmwrite(txd, MANTIS_I2CDATA_CTL);
  113. /* wait for xfer completion */
  114. for (trials = 0; trials < TRIALS; trials++) {
  115. stat = mmread(MANTIS_INT_STAT);
  116. if (stat & MANTIS_INT_I2CDONE)
  117. break;
  118. }
  119. /* check for xfer completion */
  120. if (stat & MANTIS_INT_I2CDONE) {
  121. /* check xfer was acknowledged */
  122. if (stat & MANTIS_INT_I2CRACK) {
  123. data = mmread(MANTIS_I2CDATA_CTL);
  124. msgs[i + 1].buf[0] = (data >> 8) & 0xff;
  125. dprintk(MANTIS_DEBUG, 0, " Byte <%d> RXD=0x%02x [%02x]\n", 0x0, data, msgs[i + 1].buf[0]);
  126. } else {
  127. /* I/O error */
  128. dprintk(MANTIS_ERROR, 1, " I/O error, LINE:%d", __LINE__);
  129. ret = -EIO;
  130. break;
  131. }
  132. } else {
  133. /* I/O error */
  134. dprintk(MANTIS_ERROR, 1, " I/O error, LINE:%d", __LINE__);
  135. ret = -EIO;
  136. break;
  137. }
  138. i += 2; /* Write/Read operation in one go */
  139. }
  140. if (i < num) {
  141. if (msgs[i].flags & I2C_M_RD)
  142. ret = mantis_i2c_read(mantis, &msgs[i]);
  143. else
  144. ret = mantis_i2c_write(mantis, &msgs[i]);
  145. i++;
  146. if (ret < 0)
  147. goto bail_out;
  148. }
  149. }
  150. mutex_unlock(&mantis->i2c_lock);
  151. return num;
  152. bail_out:
  153. mutex_unlock(&mantis->i2c_lock);
  154. return ret;
  155. }
  156. static u32 mantis_i2c_func(struct i2c_adapter *adapter)
  157. {
  158. return I2C_FUNC_SMBUS_EMUL;
  159. }
  160. static const struct i2c_algorithm mantis_algo = {
  161. .master_xfer = mantis_i2c_xfer,
  162. .functionality = mantis_i2c_func,
  163. };
  164. int mantis_i2c_init(struct mantis_pci *mantis)
  165. {
  166. u32 intstat;
  167. struct i2c_adapter *i2c_adapter = &mantis->adapter;
  168. struct pci_dev *pdev = mantis->pdev;
  169. init_waitqueue_head(&mantis->i2c_wq);
  170. mutex_init(&mantis->i2c_lock);
  171. strscpy(i2c_adapter->name, "Mantis I2C", sizeof(i2c_adapter->name));
  172. i2c_set_adapdata(i2c_adapter, mantis);
  173. i2c_adapter->owner = THIS_MODULE;
  174. i2c_adapter->algo = &mantis_algo;
  175. i2c_adapter->algo_data = NULL;
  176. i2c_adapter->timeout = 500;
  177. i2c_adapter->retries = 3;
  178. i2c_adapter->dev.parent = &pdev->dev;
  179. mantis->i2c_rc = i2c_add_adapter(i2c_adapter);
  180. if (mantis->i2c_rc < 0)
  181. return mantis->i2c_rc;
  182. dprintk(MANTIS_DEBUG, 1, "Initializing I2C ..");
  183. intstat = mmread(MANTIS_INT_STAT);
  184. mmread(MANTIS_INT_MASK);
  185. mmwrite(intstat, MANTIS_INT_STAT);
  186. dprintk(MANTIS_DEBUG, 1, "Disabling I2C interrupt");
  187. mantis_mask_ints(mantis, MANTIS_INT_I2CDONE);
  188. return 0;
  189. }
  190. EXPORT_SYMBOL_GPL(mantis_i2c_init);
  191. int mantis_i2c_exit(struct mantis_pci *mantis)
  192. {
  193. dprintk(MANTIS_DEBUG, 1, "Disabling I2C interrupt");
  194. mantis_mask_ints(mantis, MANTIS_INT_I2CDONE);
  195. dprintk(MANTIS_DEBUG, 1, "Removing I2C adapter");
  196. i2c_del_adapter(&mantis->adapter);
  197. return 0;
  198. }
  199. EXPORT_SYMBOL_GPL(mantis_i2c_exit);