go7007-i2c.c 6.2 KB

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