mantis_i2c.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. Mantis PCI bridge driver
  3. Copyright (C) 2005, 2006 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 <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/init.h>
  19. #include <linux/delay.h>
  20. #include <asm/io.h>
  21. #include <linux/ioport.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/page.h>
  24. #include "mantis_common.h"
  25. #define I2C_HW_B_MANTIS 0x1c
  26. static int mantis_ack_wait(struct mantis_pci *mantis)
  27. {
  28. int rc = 0;
  29. u32 timeout = 0;
  30. if (wait_event_interruptible_timeout(mantis->i2c_wq,
  31. mantis->mantis_int_stat & MANTIS_INT_I2CDONE,
  32. msecs_to_jiffies(50)) == -ERESTARTSYS) {
  33. dprintk(verbose, MANTIS_DEBUG, 1, "Master !I2CDONE");
  34. rc = -EREMOTEIO;
  35. }
  36. while (!(mantis->mantis_int_stat & MANTIS_INT_I2CRACK)) {
  37. dprintk(verbose, MANTIS_DEBUG, 1, "Waiting for Slave RACK");
  38. mantis->mantis_int_stat = mmread(MANTIS_INT_STAT);
  39. msleep(5);
  40. timeout++;
  41. if (timeout > 500) {
  42. dprintk(verbose, MANTIS_ERROR, 1, "Slave RACK Fail !");
  43. rc = -EREMOTEIO;
  44. break;
  45. }
  46. }
  47. udelay(350);
  48. return rc;
  49. }
  50. static int mantis_i2c_read(struct mantis_pci *mantis, const struct i2c_msg *msg)
  51. {
  52. u32 rxd, i;
  53. dprintk(verbose, MANTIS_INFO, 0, " %s: Address=[0x%02x] <R>[ ",
  54. __func__, msg->addr);
  55. for (i = 0; i < msg->len; i++) {
  56. rxd = (msg->addr << 25) | (1 << 24)
  57. | MANTIS_I2C_RATE_3
  58. | MANTIS_I2C_STOP
  59. | MANTIS_I2C_PGMODE;
  60. if (i == (msg->len - 1))
  61. rxd &= ~MANTIS_I2C_STOP;
  62. mmwrite(MANTIS_INT_I2CDONE, MANTIS_INT_STAT);
  63. mmwrite(rxd, MANTIS_I2CDATA_CTL);
  64. if (mantis_ack_wait(mantis) != 0) {
  65. dprintk(verbose, MANTIS_DEBUG, 1, "ACK failed<R>");
  66. return -EREMOTEIO;
  67. }
  68. rxd = mmread(MANTIS_I2CDATA_CTL);
  69. msg->buf[i] = (u8)((rxd >> 8) & 0xFF);
  70. dprintk(verbose, MANTIS_INFO, 0, "%02x ", msg->buf[i]);
  71. }
  72. dprintk(verbose, MANTIS_INFO, 0, "]\n");
  73. return 0;
  74. }
  75. static int mantis_i2c_write(struct mantis_pci *mantis, const struct i2c_msg *msg)
  76. {
  77. int i;
  78. u32 txd = 0;
  79. dprintk(verbose, MANTIS_INFO, 0, " %s: Address=[0x%02x] <W>[ ",
  80. __func__, msg->addr);
  81. for (i = 0; i < msg->len; i++) {
  82. dprintk(verbose, MANTIS_INFO, 0, "%02x ", msg->buf[i]);
  83. txd = (msg->addr << 25) | (msg->buf[i] << 8)
  84. | MANTIS_I2C_RATE_3
  85. | MANTIS_I2C_STOP
  86. | MANTIS_I2C_PGMODE;
  87. if (i == (msg->len - 1))
  88. txd &= ~MANTIS_I2C_STOP;
  89. mmwrite(MANTIS_INT_I2CDONE, MANTIS_INT_STAT);
  90. mmwrite(txd, MANTIS_I2CDATA_CTL);
  91. if (mantis_ack_wait(mantis) != 0) {
  92. dprintk(verbose, MANTIS_DEBUG, 1, "ACK failed<W>");
  93. return -EREMOTEIO;
  94. }
  95. }
  96. dprintk(verbose, MANTIS_INFO, 0, "]\n");
  97. return 0;
  98. }
  99. static int mantis_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
  100. {
  101. int ret = 0, i;
  102. struct mantis_pci *mantis;
  103. mantis = i2c_get_adapdata(adapter);
  104. mutex_lock(&mantis->i2c_lock);
  105. for (i = 0; i < num; i++) {
  106. if (msgs[i].flags & I2C_M_RD)
  107. ret = mantis_i2c_read(mantis, &msgs[i]);
  108. else
  109. ret = mantis_i2c_write(mantis, &msgs[i]);
  110. if (ret < 0)
  111. return ret;
  112. }
  113. mutex_unlock(&mantis->i2c_lock);
  114. return num;
  115. }
  116. static u32 mantis_i2c_func(struct i2c_adapter *adapter)
  117. {
  118. return I2C_FUNC_SMBUS_EMUL;
  119. }
  120. static struct i2c_algorithm mantis_algo = {
  121. .master_xfer = mantis_i2c_xfer,
  122. .functionality = mantis_i2c_func,
  123. };
  124. static struct i2c_adapter mantis_i2c_adapter = {
  125. .owner = THIS_MODULE,
  126. .name = "Mantis I2C",
  127. .id = I2C_HW_B_MANTIS,
  128. .class = I2C_CLASS_TV_DIGITAL,
  129. .algo = &mantis_algo,
  130. };
  131. int __devinit mantis_i2c_init(struct mantis_pci *mantis)
  132. {
  133. u32 intstat, intmask;
  134. struct i2c_adapter *i2c_adapter = &mantis->adapter;
  135. struct pci_dev *pdev = mantis->pdev;
  136. mutex_init(&mantis->i2c_lock);
  137. memcpy(i2c_adapter, &mantis_i2c_adapter, sizeof (mantis_i2c_adapter));
  138. i2c_set_adapdata(i2c_adapter, mantis);
  139. i2c_adapter->dev.parent = &pdev->dev;
  140. mantis->i2c_rc = i2c_add_adapter(i2c_adapter);
  141. if (mantis->i2c_rc < 0)
  142. return mantis->i2c_rc;
  143. dprintk(verbose, MANTIS_DEBUG, 1, "Initializing I2C ..");
  144. intstat = mmread(MANTIS_INT_STAT);
  145. intmask = mmread(MANTIS_INT_MASK);
  146. mmwrite(intstat, MANTIS_INT_STAT);
  147. mmwrite(intmask | MANTIS_INT_I2CDONE, MANTIS_INT_MASK);
  148. dprintk(verbose, MANTIS_DEBUG, 1, "[0x%08x/%08x]", intstat, intmask);
  149. return 0;
  150. }
  151. int __devexit mantis_i2c_exit(struct mantis_pci *mantis)
  152. {
  153. dprintk(verbose, MANTIS_DEBUG, 1, "Removing I2C adapter");
  154. return i2c_del_adapter(&mantis->adapter);
  155. }