1 /* 2 * ngene-i2c.c: nGene PCIe bridge driver i2c functions 3 * 4 * Copyright (C) 2005-2007 Micronas 5 * 6 * Copyright (C) 2008-2009 Ralph Metzler <rjkm@metzlerbros.de> 7 * Modifications for new nGene firmware, 8 * support for EEPROM-copying, 9 * support for new dual DVB-S2 card prototype 10 * 11 * 12 * This program is free software; you can redistribute it and/or 13 * modify it under the terms of the GNU General Public License 14 * version 2 only, as published by the Free Software Foundation. 15 * 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 21 * 22 * 23 * You should have received a copy of the GNU General Public License 24 * along with this program; if not, write to the Free Software 25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 26 * 02110-1301, USA 27 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html 28 */ 29 30 /* FIXME - some of these can probably be removed */ 31 #include <linux/module.h> 32 #include <linux/init.h> 33 #include <linux/delay.h> 34 #include <linux/slab.h> 35 #include <linux/poll.h> 36 #include <linux/io.h> 37 #include <asm/div64.h> 38 #include <linux/pci.h> 39 #include <linux/pci_ids.h> 40 #include <linux/timer.h> 41 #include <linux/byteorder/generic.h> 42 #include <linux/firmware.h> 43 #include <linux/vmalloc.h> 44 45 #include "ngene.h" 46 47 /* Firmware command for i2c operations */ 48 static int ngene_command_i2c_read(struct ngene *dev, u8 adr, 49 u8 *out, u8 outlen, u8 *in, u8 inlen, int flag) 50 { 51 struct ngene_command com; 52 53 com.cmd.hdr.Opcode = CMD_I2C_READ; 54 com.cmd.hdr.Length = outlen + 3; 55 com.cmd.I2CRead.Device = adr << 1; 56 memcpy(com.cmd.I2CRead.Data, out, outlen); 57 com.cmd.I2CRead.Data[outlen] = inlen; 58 com.cmd.I2CRead.Data[outlen + 1] = 0; 59 com.in_len = outlen + 3; 60 com.out_len = inlen + 1; 61 62 if (ngene_command(dev, &com) < 0) 63 return -EIO; 64 65 if ((com.cmd.raw8[0] >> 1) != adr) 66 return -EIO; 67 68 if (flag) 69 memcpy(in, com.cmd.raw8, inlen + 1); 70 else 71 memcpy(in, com.cmd.raw8 + 1, inlen); 72 return 0; 73 } 74 75 static int ngene_command_i2c_write(struct ngene *dev, u8 adr, 76 u8 *out, u8 outlen) 77 { 78 struct ngene_command com; 79 80 81 com.cmd.hdr.Opcode = CMD_I2C_WRITE; 82 com.cmd.hdr.Length = outlen + 1; 83 com.cmd.I2CRead.Device = adr << 1; 84 memcpy(com.cmd.I2CRead.Data, out, outlen); 85 com.in_len = outlen + 1; 86 com.out_len = 1; 87 88 if (ngene_command(dev, &com) < 0) 89 return -EIO; 90 91 if (com.cmd.raw8[0] == 1) 92 return -EIO; 93 94 return 0; 95 } 96 97 static void ngene_i2c_set_bus(struct ngene *dev, int bus) 98 { 99 if (!(dev->card_info->i2c_access & 2)) 100 return; 101 if (dev->i2c_current_bus == bus) 102 return; 103 104 switch (bus) { 105 case 0: 106 ngene_command_gpio_set(dev, 3, 0); 107 ngene_command_gpio_set(dev, 2, 1); 108 break; 109 110 case 1: 111 ngene_command_gpio_set(dev, 2, 0); 112 ngene_command_gpio_set(dev, 3, 1); 113 break; 114 } 115 dev->i2c_current_bus = bus; 116 } 117 118 static int ngene_i2c_master_xfer(struct i2c_adapter *adapter, 119 struct i2c_msg msg[], int num) 120 { 121 struct ngene_channel *chan = 122 (struct ngene_channel *)i2c_get_adapdata(adapter); 123 struct ngene *dev = chan->dev; 124 125 down(&dev->i2c_switch_mutex); 126 ngene_i2c_set_bus(dev, chan->number); 127 128 if (num == 2 && msg[1].flags & I2C_M_RD && !(msg[0].flags & I2C_M_RD)) 129 if (!ngene_command_i2c_read(dev, msg[0].addr, 130 msg[0].buf, msg[0].len, 131 msg[1].buf, msg[1].len, 0)) 132 goto done; 133 134 if (num == 1 && !(msg[0].flags & I2C_M_RD)) 135 if (!ngene_command_i2c_write(dev, msg[0].addr, 136 msg[0].buf, msg[0].len)) 137 goto done; 138 if (num == 1 && (msg[0].flags & I2C_M_RD)) 139 if (!ngene_command_i2c_read(dev, msg[0].addr, NULL, 0, 140 msg[0].buf, msg[0].len, 0)) 141 goto done; 142 143 up(&dev->i2c_switch_mutex); 144 return -EIO; 145 146 done: 147 up(&dev->i2c_switch_mutex); 148 return num; 149 } 150 151 152 static u32 ngene_i2c_functionality(struct i2c_adapter *adap) 153 { 154 return I2C_FUNC_SMBUS_EMUL; 155 } 156 157 static struct i2c_algorithm ngene_i2c_algo = { 158 .master_xfer = ngene_i2c_master_xfer, 159 .functionality = ngene_i2c_functionality, 160 }; 161 162 int ngene_i2c_init(struct ngene *dev, int dev_nr) 163 { 164 struct i2c_adapter *adap = &(dev->channel[dev_nr].i2c_adapter); 165 166 i2c_set_adapdata(adap, &(dev->channel[dev_nr])); 167 168 strcpy(adap->name, "nGene"); 169 170 adap->algo = &ngene_i2c_algo; 171 adap->algo_data = (void *)&(dev->channel[dev_nr]); 172 adap->dev.parent = &dev->pci_dev->dev; 173 174 return i2c_add_adapter(adap); 175 } 176 177