mantis_i2c.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. Mantis PCI bridge driver
  3. Copyright (C) Manu Abraham (abraham.manu@gmail.com)
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <asm/io.h>
  17. #include <linux/ioport.h>
  18. #include <linux/pci.h>
  19. #include <linux/i2c.h>
  20. #include "dmxdev.h"
  21. #include "dvbdev.h"
  22. #include "dvb_demux.h"
  23. #include "dvb_frontend.h"
  24. #include "dvb_net.h"
  25. #include "mantis_common.h"
  26. #include "mantis_reg.h"
  27. #include "mantis_i2c.h"
  28. #define TRIALS 10000
  29. static int mantis_i2c_read(struct mantis_pci *mantis, const struct i2c_msg *msg)
  30. {
  31. u32 rxd, i, stat, trials;
  32. dprintk(MANTIS_INFO, 0, " %s: Address=[0x%02x] <R>[ ",
  33. __func__, msg->addr);
  34. for (i = 0; i < msg->len; i++) {
  35. rxd = (msg->addr << 25) | (1 << 24)
  36. | MANTIS_I2C_RATE_3
  37. | MANTIS_I2C_STOP
  38. | MANTIS_I2C_PGMODE;
  39. if (i == (msg->len - 1))
  40. rxd &= ~MANTIS_I2C_STOP;
  41. mmwrite(MANTIS_INT_I2CDONE, MANTIS_INT_STAT);
  42. mmwrite(rxd, MANTIS_I2CDATA_CTL);
  43. /* wait for xfer completion */
  44. for (trials = 0; trials < TRIALS; trials++) {
  45. stat = mmread(MANTIS_INT_STAT);
  46. if (stat & MANTIS_INT_I2CDONE)
  47. break;
  48. }
  49. dprintk(MANTIS_TMG, 0, "I2CDONE: trials=%d\n", trials);
  50. /* wait for xfer completion */
  51. for (trials = 0; trials < TRIALS; trials++) {
  52. stat = mmread(MANTIS_INT_STAT);
  53. if (stat & MANTIS_INT_I2CRACK)
  54. break;
  55. }
  56. dprintk(MANTIS_TMG, 0, "I2CRACK: trials=%d\n", trials);
  57. rxd = mmread(MANTIS_I2CDATA_CTL);
  58. msg->buf[i] = (u8)((rxd >> 8) & 0xFF);
  59. dprintk(MANTIS_INFO, 0, "%02x ", msg->buf[i]);
  60. }
  61. dprintk(MANTIS_INFO, 0, "]\n");
  62. return 0;
  63. }
  64. static int mantis_i2c_write(struct mantis_pci *mantis, const struct i2c_msg *msg)
  65. {
  66. int i;
  67. u32 txd = 0, stat, trials;
  68. dprintk(MANTIS_INFO, 0, " %s: Address=[0x%02x] <W>[ ",
  69. __func__, msg->addr);
  70. for (i = 0; i < msg->len; i++) {
  71. dprintk(MANTIS_INFO, 0, "%02x ", msg->buf[i]);
  72. txd = (msg->addr << 25) | (msg->buf[i] << 8)
  73. | MANTIS_I2C_RATE_3
  74. | MANTIS_I2C_STOP
  75. | MANTIS_I2C_PGMODE;
  76. if (i == (msg->len - 1))
  77. txd &= ~MANTIS_I2C_STOP;
  78. mmwrite(MANTIS_INT_I2CDONE, MANTIS_INT_STAT);
  79. mmwrite(txd, MANTIS_I2CDATA_CTL);
  80. /* wait for xfer completion */
  81. for (trials = 0; trials < TRIALS; trials++) {
  82. stat = mmread(MANTIS_INT_STAT);
  83. if (stat & MANTIS_INT_I2CDONE)
  84. break;
  85. }
  86. dprintk(MANTIS_TMG, 0, "I2CDONE: trials=%d\n", trials);
  87. /* wait for xfer completion */
  88. for (trials = 0; trials < TRIALS; trials++) {
  89. stat = mmread(MANTIS_INT_STAT);
  90. if (stat & MANTIS_INT_I2CRACK)
  91. break;
  92. }
  93. dprintk(MANTIS_TMG, 0, "I2CRACK: trials=%d\n", trials);
  94. }
  95. dprintk(MANTIS_INFO, 0, "]\n");
  96. return 0;
  97. }
  98. static int mantis_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
  99. {
  100. int ret = 0, i = 0, trials;
  101. u32 stat, data, txd;
  102. struct mantis_pci *mantis;
  103. struct mantis_hwconfig *config;
  104. mantis = i2c_get_adapdata(adapter);
  105. BUG_ON(!mantis);
  106. config = mantis->hwconfig;
  107. BUG_ON(!config);
  108. dprintk(MANTIS_DEBUG, 1, "Messages:%d", num);
  109. mutex_lock(&mantis->i2c_lock);
  110. while (i < num) {
  111. /* Byte MODE */
  112. if ((config->i2c_mode & MANTIS_BYTE_MODE) &&
  113. ((i + 1) < num) &&
  114. (msgs[i].len < 2) &&
  115. (msgs[i + 1].len < 2) &&
  116. (msgs[i + 1].flags & I2C_M_RD)) {
  117. dprintk(MANTIS_DEBUG, 0, " Byte MODE:\n");
  118. /* Read operation */
  119. txd = msgs[i].addr << 25 | (0x1 << 24)
  120. | (msgs[i].buf[0] << 16)
  121. | MANTIS_I2C_RATE_3;
  122. mmwrite(txd, MANTIS_I2CDATA_CTL);
  123. /* wait for xfer completion */
  124. for (trials = 0; trials < TRIALS; trials++) {
  125. stat = mmread(MANTIS_INT_STAT);
  126. if (stat & MANTIS_INT_I2CDONE)
  127. break;
  128. }
  129. /* check for xfer completion */
  130. if (stat & MANTIS_INT_I2CDONE) {
  131. /* check xfer was acknowledged */
  132. if (stat & MANTIS_INT_I2CRACK) {
  133. data = mmread(MANTIS_I2CDATA_CTL);
  134. msgs[i + 1].buf[0] = (data >> 8) & 0xff;
  135. dprintk(MANTIS_DEBUG, 0, " Byte <%d> RXD=0x%02x [%02x]\n", 0x0, data, msgs[i + 1].buf[0]);
  136. } else {
  137. /* I/O error */
  138. dprintk(MANTIS_ERROR, 1, " I/O error, LINE:%d", __LINE__);
  139. ret = -EIO;
  140. break;
  141. }
  142. } else {
  143. /* I/O error */
  144. dprintk(MANTIS_ERROR, 1, " I/O error, LINE:%d", __LINE__);
  145. ret = -EIO;
  146. break;
  147. }
  148. i += 2; /* Write/Read operation in one go */
  149. }
  150. if (i < num) {
  151. if (msgs[i].flags & I2C_M_RD)
  152. ret = mantis_i2c_read(mantis, &msgs[i]);
  153. else
  154. ret = mantis_i2c_write(mantis, &msgs[i]);
  155. i++;
  156. if (ret < 0)
  157. goto bail_out;
  158. }
  159. }
  160. mutex_unlock(&mantis->i2c_lock);
  161. return num;
  162. bail_out:
  163. mutex_unlock(&mantis->i2c_lock);
  164. return ret;
  165. }
  166. static u32 mantis_i2c_func(struct i2c_adapter *adapter)
  167. {
  168. return I2C_FUNC_SMBUS_EMUL;
  169. }
  170. static struct i2c_algorithm mantis_algo = {
  171. .master_xfer = mantis_i2c_xfer,
  172. .functionality = mantis_i2c_func,
  173. };
  174. int mantis_i2c_init(struct mantis_pci *mantis)
  175. {
  176. u32 intstat;
  177. struct i2c_adapter *i2c_adapter = &mantis->adapter;
  178. struct pci_dev *pdev = mantis->pdev;
  179. init_waitqueue_head(&mantis->i2c_wq);
  180. mutex_init(&mantis->i2c_lock);
  181. strncpy(i2c_adapter->name, "Mantis I2C", sizeof(i2c_adapter->name));
  182. i2c_set_adapdata(i2c_adapter, mantis);
  183. i2c_adapter->owner = THIS_MODULE;
  184. i2c_adapter->algo = &mantis_algo;
  185. i2c_adapter->algo_data = NULL;
  186. i2c_adapter->timeout = 500;
  187. i2c_adapter->retries = 3;
  188. i2c_adapter->dev.parent = &pdev->dev;
  189. mantis->i2c_rc = i2c_add_adapter(i2c_adapter);
  190. if (mantis->i2c_rc < 0)
  191. return mantis->i2c_rc;
  192. dprintk(MANTIS_DEBUG, 1, "Initializing I2C ..");
  193. intstat = mmread(MANTIS_INT_STAT);
  194. mmread(MANTIS_INT_MASK);
  195. mmwrite(intstat, MANTIS_INT_STAT);
  196. dprintk(MANTIS_DEBUG, 1, "Disabling I2C interrupt");
  197. mantis_mask_ints(mantis, MANTIS_INT_I2CDONE);
  198. return 0;
  199. }
  200. EXPORT_SYMBOL_GPL(mantis_i2c_init);
  201. int mantis_i2c_exit(struct mantis_pci *mantis)
  202. {
  203. dprintk(MANTIS_DEBUG, 1, "Disabling I2C interrupt");
  204. mantis_mask_ints(mantis, MANTIS_INT_I2CDONE);
  205. dprintk(MANTIS_DEBUG, 1, "Removing I2C adapter");
  206. i2c_del_adapter(&mantis->adapter);
  207. return 0;
  208. }
  209. EXPORT_SYMBOL_GPL(mantis_i2c_exit);