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