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 * To obtain the license, point your browser to 23 * http://www.gnu.org/copyleft/gpl.html 24 */ 25 26 /* FIXME - some of these can probably be removed */ 27 #include <linux/module.h> 28 #include <linux/init.h> 29 #include <linux/delay.h> 30 #include <linux/slab.h> 31 #include <linux/poll.h> 32 #include <linux/io.h> 33 #include <asm/div64.h> 34 #include <linux/pci.h> 35 #include <linux/pci_ids.h> 36 #include <linux/timer.h> 37 #include <linux/byteorder/generic.h> 38 #include <linux/firmware.h> 39 #include <linux/vmalloc.h> 40 41 #include "ngene.h" 42 43 /* Firmware command for i2c operations */ 44 static int ngene_command_i2c_read(struct ngene *dev, u8 adr, 45 u8 *out, u8 outlen, u8 *in, u8 inlen, int flag) 46 { 47 struct ngene_command com; 48 49 com.cmd.hdr.Opcode = CMD_I2C_READ; 50 com.cmd.hdr.Length = outlen + 3; 51 com.cmd.I2CRead.Device = adr << 1; 52 memcpy(com.cmd.I2CRead.Data, out, outlen); 53 com.cmd.I2CRead.Data[outlen] = inlen; 54 com.cmd.I2CRead.Data[outlen + 1] = 0; 55 com.in_len = outlen + 3; 56 com.out_len = inlen + 1; 57 58 if (ngene_command(dev, &com) < 0) 59 return -EIO; 60 61 if ((com.cmd.raw8[0] >> 1) != adr) 62 return -EIO; 63 64 if (flag) 65 memcpy(in, com.cmd.raw8, inlen + 1); 66 else 67 memcpy(in, com.cmd.raw8 + 1, inlen); 68 return 0; 69 } 70 71 static int ngene_command_i2c_write(struct ngene *dev, u8 adr, 72 u8 *out, u8 outlen) 73 { 74 struct ngene_command com; 75 76 77 com.cmd.hdr.Opcode = CMD_I2C_WRITE; 78 com.cmd.hdr.Length = outlen + 1; 79 com.cmd.I2CRead.Device = adr << 1; 80 memcpy(com.cmd.I2CRead.Data, out, outlen); 81 com.in_len = outlen + 1; 82 com.out_len = 1; 83 84 if (ngene_command(dev, &com) < 0) 85 return -EIO; 86 87 if (com.cmd.raw8[0] == 1) 88 return -EIO; 89 90 return 0; 91 } 92 93 static void ngene_i2c_set_bus(struct ngene *dev, int bus) 94 { 95 if (!(dev->card_info->i2c_access & 2)) 96 return; 97 if (dev->i2c_current_bus == bus) 98 return; 99 100 switch (bus) { 101 case 0: 102 ngene_command_gpio_set(dev, 3, 0); 103 ngene_command_gpio_set(dev, 2, 1); 104 break; 105 106 case 1: 107 ngene_command_gpio_set(dev, 2, 0); 108 ngene_command_gpio_set(dev, 3, 1); 109 break; 110 } 111 dev->i2c_current_bus = bus; 112 } 113 114 static int ngene_i2c_master_xfer(struct i2c_adapter *adapter, 115 struct i2c_msg msg[], int num) 116 { 117 struct ngene_channel *chan = 118 (struct ngene_channel *)i2c_get_adapdata(adapter); 119 struct ngene *dev = chan->dev; 120 121 mutex_lock(&dev->i2c_switch_mutex); 122 ngene_i2c_set_bus(dev, chan->number); 123 124 if (num == 2 && msg[1].flags & I2C_M_RD && !(msg[0].flags & I2C_M_RD)) 125 if (!ngene_command_i2c_read(dev, msg[0].addr, 126 msg[0].buf, msg[0].len, 127 msg[1].buf, msg[1].len, 0)) 128 goto done; 129 130 if (num == 1 && !(msg[0].flags & I2C_M_RD)) 131 if (!ngene_command_i2c_write(dev, msg[0].addr, 132 msg[0].buf, msg[0].len)) 133 goto done; 134 if (num == 1 && (msg[0].flags & I2C_M_RD)) 135 if (!ngene_command_i2c_read(dev, msg[0].addr, NULL, 0, 136 msg[0].buf, msg[0].len, 0)) 137 goto done; 138 139 mutex_unlock(&dev->i2c_switch_mutex); 140 return -EIO; 141 142 done: 143 mutex_unlock(&dev->i2c_switch_mutex); 144 return num; 145 } 146 147 148 static u32 ngene_i2c_functionality(struct i2c_adapter *adap) 149 { 150 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 151 } 152 153 static const struct i2c_algorithm ngene_i2c_algo = { 154 .master_xfer = ngene_i2c_master_xfer, 155 .functionality = ngene_i2c_functionality, 156 }; 157 158 int ngene_i2c_init(struct ngene *dev, int dev_nr) 159 { 160 struct i2c_adapter *adap = &(dev->channel[dev_nr].i2c_adapter); 161 162 i2c_set_adapdata(adap, &(dev->channel[dev_nr])); 163 164 strcpy(adap->name, "nGene"); 165 166 adap->algo = &ngene_i2c_algo; 167 adap->algo_data = (void *)&(dev->channel[dev_nr]); 168 adap->dev.parent = &dev->pci_dev->dev; 169 170 return i2c_add_adapter(adap); 171 } 172 173