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