go7007-i2c.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2005-2006 Micronas USA Inc.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/delay.h>
  7. #include <linux/sched.h>
  8. #include <linux/list.h>
  9. #include <linux/unistd.h>
  10. #include <linux/time.h>
  11. #include <linux/device.h>
  12. #include <linux/i2c.h>
  13. #include <linux/mutex.h>
  14. #include <linux/uaccess.h>
  15. #include "go7007-priv.h"
  16. /********************* Driver for on-board I2C adapter *********************/
  17. /* #define GO7007_I2C_DEBUG */
  18. #define SPI_I2C_ADDR_BASE 0x1400
  19. #define STATUS_REG_ADDR (SPI_I2C_ADDR_BASE + 0x2)
  20. #define I2C_CTRL_REG_ADDR (SPI_I2C_ADDR_BASE + 0x6)
  21. #define I2C_DEV_UP_ADDR_REG_ADDR (SPI_I2C_ADDR_BASE + 0x7)
  22. #define I2C_LO_ADDR_REG_ADDR (SPI_I2C_ADDR_BASE + 0x8)
  23. #define I2C_DATA_REG_ADDR (SPI_I2C_ADDR_BASE + 0x9)
  24. #define I2C_CLKFREQ_REG_ADDR (SPI_I2C_ADDR_BASE + 0xa)
  25. #define I2C_STATE_MASK 0x0007
  26. #define I2C_READ_READY_MASK 0x0008
  27. /* There is only one I2C port on the TW2804 that feeds all four GO7007 VIPs
  28. * on the Adlink PCI-MPG24, so access is shared between all of them. */
  29. static DEFINE_MUTEX(adlink_mpg24_i2c_lock);
  30. static int go7007_i2c_xfer(struct go7007 *go, u16 addr, int read,
  31. u16 command, int flags, u8 *data)
  32. {
  33. int i, ret = -EIO;
  34. u16 val;
  35. if (go->status == STATUS_SHUTDOWN)
  36. return -ENODEV;
  37. #ifdef GO7007_I2C_DEBUG
  38. if (read)
  39. dev_dbg(go->dev, "go7007-i2c: reading 0x%02x on 0x%02x\n",
  40. command, addr);
  41. else
  42. dev_dbg(go->dev,
  43. "go7007-i2c: writing 0x%02x to 0x%02x on 0x%02x\n",
  44. *data, command, addr);
  45. #endif
  46. mutex_lock(&go->hw_lock);
  47. if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) {
  48. /* Bridge the I2C port on this GO7007 to the shared bus */
  49. mutex_lock(&adlink_mpg24_i2c_lock);
  50. go7007_write_addr(go, 0x3c82, 0x0020);
  51. }
  52. /* Wait for I2C adapter to be ready */
  53. for (i = 0; i < 10; ++i) {
  54. if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
  55. goto i2c_done;
  56. if (!(val & I2C_STATE_MASK))
  57. break;
  58. msleep(100);
  59. }
  60. if (i == 10) {
  61. dev_err(go->dev, "go7007-i2c: I2C adapter is hung\n");
  62. goto i2c_done;
  63. }
  64. /* Set target register (command) */
  65. go7007_write_addr(go, I2C_CTRL_REG_ADDR, flags);
  66. go7007_write_addr(go, I2C_LO_ADDR_REG_ADDR, command);
  67. /* If we're writing, send the data and target address and we're done */
  68. if (!read) {
  69. go7007_write_addr(go, I2C_DATA_REG_ADDR, *data);
  70. go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
  71. (addr << 9) | (command >> 8));
  72. ret = 0;
  73. goto i2c_done;
  74. }
  75. /* Otherwise, we're reading. First clear i2c_rx_data_rdy. */
  76. if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
  77. goto i2c_done;
  78. /* Send the target address plus read flag */
  79. go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
  80. (addr << 9) | 0x0100 | (command >> 8));
  81. /* Wait for i2c_rx_data_rdy */
  82. for (i = 0; i < 10; ++i) {
  83. if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
  84. goto i2c_done;
  85. if (val & I2C_READ_READY_MASK)
  86. break;
  87. msleep(100);
  88. }
  89. if (i == 10) {
  90. dev_err(go->dev, "go7007-i2c: I2C adapter is hung\n");
  91. goto i2c_done;
  92. }
  93. /* Retrieve the read byte */
  94. if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
  95. goto i2c_done;
  96. *data = val;
  97. ret = 0;
  98. i2c_done:
  99. if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) {
  100. /* Isolate the I2C port on this GO7007 from the shared bus */
  101. go7007_write_addr(go, 0x3c82, 0x0000);
  102. mutex_unlock(&adlink_mpg24_i2c_lock);
  103. }
  104. mutex_unlock(&go->hw_lock);
  105. return ret;
  106. }
  107. static int go7007_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
  108. unsigned short flags, char read_write,
  109. u8 command, int size, union i2c_smbus_data *data)
  110. {
  111. struct go7007 *go = i2c_get_adapdata(adapter);
  112. if (size != I2C_SMBUS_BYTE_DATA)
  113. return -EIO;
  114. return go7007_i2c_xfer(go, addr, read_write == I2C_SMBUS_READ, command,
  115. flags & I2C_CLIENT_SCCB ? 0x10 : 0x00, &data->byte);
  116. }
  117. /* VERY LIMITED I2C master xfer function -- only needed because the
  118. * SMBus functions only support 8-bit commands and the SAA7135 uses
  119. * 16-bit commands. The I2C interface on the GO7007, as limited as
  120. * it is, does support this mode. */
  121. static int go7007_i2c_master_xfer(struct i2c_adapter *adapter,
  122. struct i2c_msg msgs[], int num)
  123. {
  124. struct go7007 *go = i2c_get_adapdata(adapter);
  125. int i;
  126. for (i = 0; i < num; ++i) {
  127. /* We can only do two things here -- write three bytes, or
  128. * write two bytes and read one byte. */
  129. if (msgs[i].len == 2) {
  130. if (i + 1 == num || msgs[i].addr != msgs[i + 1].addr ||
  131. (msgs[i].flags & I2C_M_RD) ||
  132. !(msgs[i + 1].flags & I2C_M_RD) ||
  133. msgs[i + 1].len != 1)
  134. return -EIO;
  135. if (go7007_i2c_xfer(go, msgs[i].addr, 1,
  136. (msgs[i].buf[0] << 8) | msgs[i].buf[1],
  137. 0x01, &msgs[i + 1].buf[0]) < 0)
  138. return -EIO;
  139. ++i;
  140. } else if (msgs[i].len == 3) {
  141. if (msgs[i].flags & I2C_M_RD)
  142. return -EIO;
  143. if (go7007_i2c_xfer(go, msgs[i].addr, 0,
  144. (msgs[i].buf[0] << 8) | msgs[i].buf[1],
  145. 0x01, &msgs[i].buf[2]) < 0)
  146. return -EIO;
  147. } else
  148. return -EIO;
  149. }
  150. return num;
  151. }
  152. static u32 go7007_functionality(struct i2c_adapter *adapter)
  153. {
  154. return I2C_FUNC_SMBUS_BYTE_DATA;
  155. }
  156. static const struct i2c_algorithm go7007_algo = {
  157. .smbus_xfer = go7007_smbus_xfer,
  158. .master_xfer = go7007_i2c_master_xfer,
  159. .functionality = go7007_functionality,
  160. };
  161. static struct i2c_adapter go7007_adap_templ = {
  162. .owner = THIS_MODULE,
  163. .name = "WIS GO7007SB",
  164. .algo = &go7007_algo,
  165. };
  166. int go7007_i2c_init(struct go7007 *go)
  167. {
  168. memcpy(&go->i2c_adapter, &go7007_adap_templ,
  169. sizeof(go7007_adap_templ));
  170. go->i2c_adapter.dev.parent = go->dev;
  171. i2c_set_adapdata(&go->i2c_adapter, go);
  172. if (i2c_add_adapter(&go->i2c_adapter) < 0) {
  173. dev_err(go->dev,
  174. "go7007-i2c: error: i2c_add_adapter failed\n");
  175. return -1;
  176. }
  177. return 0;
  178. }