2 * ngene-i2c.c: nGene PCIe bridge driver i2c functions
4 * Copyright (C) 2005-2007 Micronas
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
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.
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.
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
27 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
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>
37 #include <asm/div64.h>
38 #include <linux/pci.h>
39 #include <linux/pci_ids.h>
40 #include <linux/smp_lock.h>
41 #include <linux/timer.h>
42 #include <linux/version.h>
43 #include <linux/byteorder/generic.h>
44 #include <linux/firmware.h>
45 #include <linux/vmalloc.h>
49 /* Firmware command for i2c operations */
50 static int ngene_command_i2c_read(struct ngene *dev, u8 adr,
51 u8 *out, u8 outlen, u8 *in, u8 inlen, int flag)
53 struct ngene_command com;
55 com.cmd.hdr.Opcode = CMD_I2C_READ;
56 com.cmd.hdr.Length = outlen + 3;
57 com.cmd.I2CRead.Device = adr << 1;
58 memcpy(com.cmd.I2CRead.Data, out, outlen);
59 com.cmd.I2CRead.Data[outlen] = inlen;
60 com.cmd.I2CRead.Data[outlen + 1] = 0;
61 com.in_len = outlen + 3;
62 com.out_len = inlen + 1;
64 if (ngene_command(dev, &com) < 0)
67 if ((com.cmd.raw8[0] >> 1) != adr)
71 memcpy(in, com.cmd.raw8, inlen + 1);
73 memcpy(in, com.cmd.raw8 + 1, inlen);
77 static int ngene_command_i2c_write(struct ngene *dev, u8 adr,
80 struct ngene_command com;
83 com.cmd.hdr.Opcode = CMD_I2C_WRITE;
84 com.cmd.hdr.Length = outlen + 1;
85 com.cmd.I2CRead.Device = adr << 1;
86 memcpy(com.cmd.I2CRead.Data, out, outlen);
87 com.in_len = outlen + 1;
90 if (ngene_command(dev, &com) < 0)
93 if (com.cmd.raw8[0] == 1)
99 static void ngene_i2c_set_bus(struct ngene *dev, int bus)
101 if (!(dev->card_info->i2c_access & 2))
103 if (dev->i2c_current_bus == bus)
108 ngene_command_gpio_set(dev, 3, 0);
109 ngene_command_gpio_set(dev, 2, 1);
113 ngene_command_gpio_set(dev, 2, 0);
114 ngene_command_gpio_set(dev, 3, 1);
117 dev->i2c_current_bus = bus;
120 static int ngene_i2c_master_xfer(struct i2c_adapter *adapter,
121 struct i2c_msg msg[], int num)
123 struct ngene_channel *chan =
124 (struct ngene_channel *)i2c_get_adapdata(adapter);
125 struct ngene *dev = chan->dev;
127 down(&dev->i2c_switch_mutex);
128 ngene_i2c_set_bus(dev, chan->number);
130 if (num == 2 && msg[1].flags & I2C_M_RD && !(msg[0].flags & I2C_M_RD))
131 if (!ngene_command_i2c_read(dev, msg[0].addr,
132 msg[0].buf, msg[0].len,
133 msg[1].buf, msg[1].len, 0))
136 if (num == 1 && !(msg[0].flags & I2C_M_RD))
137 if (!ngene_command_i2c_write(dev, msg[0].addr,
138 msg[0].buf, msg[0].len))
140 if (num == 1 && (msg[0].flags & I2C_M_RD))
141 if (!ngene_command_i2c_read(dev, msg[0].addr, NULL, 0,
142 msg[0].buf, msg[0].len, 0))
145 up(&dev->i2c_switch_mutex);
149 up(&dev->i2c_switch_mutex);
154 static u32 ngene_i2c_functionality(struct i2c_adapter *adap)
156 return I2C_FUNC_SMBUS_EMUL;
159 static struct i2c_algorithm ngene_i2c_algo = {
160 .master_xfer = ngene_i2c_master_xfer,
161 .functionality = ngene_i2c_functionality,
164 int ngene_i2c_init(struct ngene *dev, int dev_nr)
166 struct i2c_adapter *adap = &(dev->channel[dev_nr].i2c_adapter);
168 i2c_set_adapdata(adap, &(dev->channel[dev_nr]));
169 adap->class = I2C_CLASS_TV_DIGITAL | I2C_CLASS_TV_ANALOG;
171 strcpy(adap->name, "nGene");
173 adap->algo = &ngene_i2c_algo;
174 adap->algo_data = (void *)&(dev->channel[dev_nr]);
175 adap->dev.parent = &dev->pci_dev->dev;
177 return i2c_add_adapter(adap);