2 * Driver for the Conexant CX23885 PCIe bridge
4 * Copyright (c) 2006 Steven Toth <stoth@hauppauge.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
30 #include <media/v4l2-common.h>
32 static unsigned int i2c_debug = 2;
33 module_param(i2c_debug, int, 0644);
34 MODULE_PARM_DESC(i2c_debug,"enable debug messages [i2c]");
36 static unsigned int i2c_scan = 0;
37 module_param(i2c_scan, int, 0444);
38 MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
40 #define dprintk(level,fmt, arg...) if (i2c_debug >= level) \
41 printk(KERN_DEBUG "%s: " fmt, dev->name , ## arg)
43 #define I2C_WAIT_DELAY 32
44 #define I2C_WAIT_RETRY 64
46 #define I2C_EXTEND (1 << 3)
47 #define I2C_NOSTOP (1 << 4)
49 static inline int i2c_slave_did_ack(struct i2c_adapter *i2c_adap)
51 struct cx23885_i2c *bus = i2c_adap->algo_data;
52 struct cx23885_dev *dev = bus->dev;
53 return cx_read(bus->reg_stat) & 0x01;
56 static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
58 struct cx23885_i2c *bus = i2c_adap->algo_data;
59 struct cx23885_dev *dev = bus->dev;
60 return cx_read(bus->reg_stat) & 0x02 ? 1 : 0;
63 static int i2c_wait_done(struct i2c_adapter *i2c_adap)
67 for (count = 0; count < I2C_WAIT_RETRY; count++) {
68 if (!i2c_is_busy(i2c_adap))
70 udelay(I2C_WAIT_DELAY);
73 if (I2C_WAIT_RETRY == count)
79 static int i2c_sendbytes(struct i2c_adapter *i2c_adap, const struct i2c_msg *msg, int last)
81 struct cx23885_i2c *bus = i2c_adap->algo_data;
82 struct cx23885_dev *dev = bus->dev;
83 u32 wdata, addr, ctrl;
86 dprintk(1, "%s()\n", __FUNCTION__);
87 /* Deal with i2c probe functions with zero payload */
89 cx_write(bus->reg_addr, msg->addr << 25);
90 cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2));
91 if (!i2c_wait_done(i2c_adap))
93 if (!i2c_slave_did_ack(i2c_adap))
96 dprintk(1, "%s() returns 0\n", __FUNCTION__);
101 /* dev, reg + first byte */
102 addr = (msg->addr << 25) | msg->buf[0];
104 ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
107 ctrl |= I2C_NOSTOP | I2C_EXTEND;
109 cx_write(bus->reg_addr, addr);
110 cx_write(bus->reg_wdata, wdata);
111 cx_write(bus->reg_ctrl, ctrl);
113 retval = i2c_wait_done(i2c_adap);
119 printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
120 if (!(ctrl & I2C_NOSTOP))
124 for (cnt = 1; cnt < msg->len; cnt++ ) {
125 /* following bytes */
126 wdata = msg->buf[cnt];
127 ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
129 if (cnt < msg->len-1 || !last)
130 ctrl |= I2C_NOSTOP | I2C_EXTEND;
132 //printk("addr = 0x%08x wdata = 0x%08x ctrl = 0x%08x\n", addr, wdata, ctrl);
133 cx_write(bus->reg_addr, addr);
134 cx_write(bus->reg_wdata, wdata);
135 cx_write(bus->reg_ctrl, ctrl);
137 retval = i2c_wait_done(i2c_adap);
143 printk(" %02x", msg->buf[cnt]);
144 if (!(ctrl & I2C_NOSTOP))
153 printk(" ERR: %d\n",retval);
157 static int i2c_readbytes(struct i2c_adapter *i2c_adap, const struct i2c_msg *msg, int last)
159 struct cx23885_i2c *bus = i2c_adap->algo_data;
160 struct cx23885_dev *dev = bus->dev;
164 dprintk(1, "%s()\n", __FUNCTION__);
166 /* Deal with i2c probe functions with zero payload */
168 cx_write(bus->reg_addr, msg->addr << 25);
169 cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2) | 1);
170 if (!i2c_wait_done(i2c_adap))
172 if (!i2c_slave_did_ack(i2c_adap))
176 dprintk(1, "%s() returns 0\n", __FUNCTION__);
180 for(cnt = 0; cnt < msg->len; cnt++) {
182 ctrl = bus->i2c_period | (1 << 12) | (1 << 2) | 1;
184 if (cnt < msg->len-1 || !last)
185 ctrl |= I2C_NOSTOP | I2C_EXTEND;
187 cx_write(bus->reg_addr, msg->addr << 25);
188 cx_write(bus->reg_ctrl, ctrl);
190 retval = i2c_wait_done(i2c_adap);
195 msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff;
197 if (!(ctrl & I2C_NOSTOP))
198 printk(" <R %02x", (msg->addr << 1) +1);
199 printk(" =%02x", msg->buf[cnt]);
200 if (!(ctrl & I2C_NOSTOP))
209 printk(" ERR: %d\n",retval);
213 static int i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
215 struct cx23885_i2c *bus = i2c_adap->algo_data;
216 struct cx23885_dev *dev = bus->dev;
219 dprintk(1, "%s(num = %d)\n", __FUNCTION__, num);
221 for (i = 0 ; i < num; i++) {
222 dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n"
223 , __FUNCTION__, num, msgs[i].addr, msgs[i].len);
224 if (msgs[i].flags & I2C_M_RD) {
226 retval = i2c_readbytes(i2c_adap, &msgs[i], i+1 == num);
231 retval = i2c_sendbytes(i2c_adap, &msgs[i], i+1 == num);
242 static int attach_inform(struct i2c_client *client)
244 struct cx23885_dev *dev = i2c_get_adapdata(client->adapter);
246 dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n",
247 client->driver->driver.name, client->addr, client->name);
249 if (!client->driver->command)
255 static int detach_inform(struct i2c_client *client)
257 struct cx23885_dev *dev = i2c_get_adapdata(client->adapter);
259 dprintk(1, "i2c detach [client=%s]\n", client->name);
264 void cx23885_call_i2c_clients(struct cx23885_i2c *bus, unsigned int cmd, void *arg)
266 // struct cx23885_dev *dev = bus->dev;
268 if (bus->i2c_rc != 0)
271 i2c_clients_command(&bus->i2c_adap, cmd, arg);
274 static int cx23885_algo_control(struct i2c_adapter *adap,
275 unsigned int cmd, unsigned long arg)
280 static u32 cx23885_functionality(struct i2c_adapter *adap)
282 return I2C_FUNC_SMBUS_EMUL;
285 static struct i2c_algorithm cx23885_i2c_algo_template = {
286 .master_xfer = i2c_xfer,
287 .algo_control = cx23885_algo_control,
288 .functionality = cx23885_functionality,
291 /* ----------------------------------------------------------------------- */
293 static struct i2c_adapter cx23885_i2c_adap_template = {
295 .owner = THIS_MODULE,
296 .id = I2C_HW_B_CX23885,
297 .algo = &cx23885_i2c_algo_template,
298 // .class = I2C_CLASS_TV_ANALOG,
299 .client_register = attach_inform,
300 .client_unregister = detach_inform,
303 static struct i2c_client cx23885_i2c_client_template = {
304 .name = "cx23885 internal",
307 static char *i2c_devs[128] = {
308 [ 0x32 >> 1 ] = "cx24227",
309 [ 0x88 >> 1 ] = "cx25837",
310 [ 0x84 >> 1 ] = "tda8295",
311 [ 0xa0 >> 1 ] = "eeprom",
312 [ 0xc0 >> 1 ] = "mt2131/tda8275",
313 [ 0xc2 >> 1 ] = "mt2131/tda8275",
316 static void do_i2c_scan(char *name, struct i2c_client *c)
321 for (i = 0; i < 128; i++) {
323 rc = i2c_master_recv(c,&buf,0);
326 printk("%s: i2c scan: found device @ 0x%x [%s]\n",
327 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
331 /* init + register i2c algo-bit adapter */
332 int cx23885_i2c_register(struct cx23885_i2c *bus)
334 struct cx23885_dev *dev = bus->dev;
336 dprintk(1, "%s(bus = %d)\n", __FUNCTION__, bus->nr);
338 memcpy(&bus->i2c_adap, &cx23885_i2c_adap_template, sizeof(bus->i2c_adap));
339 memcpy(&bus->i2c_algo, &cx23885_i2c_algo_template, sizeof(bus->i2c_algo));
340 memcpy(&bus->i2c_client, &cx23885_i2c_client_template, sizeof(bus->i2c_client));
342 bus->i2c_adap.dev.parent = &dev->pci->dev;
344 strlcpy(bus->i2c_adap.name, bus->dev->name, sizeof(bus->i2c_adap.name));
345 bus->i2c_algo.data = bus;
346 bus->i2c_adap.algo_data = bus;
347 i2c_add_adapter(&bus->i2c_adap);
349 bus->i2c_client.adapter = &bus->i2c_adap;
351 if (0 == bus->i2c_rc) {
352 printk("%s: i2c bus %d registered\n", dev->name, bus->nr);
354 do_i2c_scan(dev->name, &bus->i2c_client);
356 printk("%s: i2c bus %d register FAILED\n", dev->name, bus->nr);
361 int cx23885_i2c_unregister(struct cx23885_i2c *bus)
363 i2c_del_adapter(&bus->i2c_adap);
367 /* ----------------------------------------------------------------------- */
369 EXPORT_SYMBOL(cx23885_call_i2c_clients);