2 tm6000-i2c.c - driver for TM5600/TM6000/TM6010 USB video capture devices
4 Copyright (C) 2006-2007 Mauro Carvalho Chehab <mchehab@infradead.org>
6 Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com>
7 - Fix SMBus Read Byte command
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation version 2
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/usb.h>
26 #include <linux/i2c.h>
29 #include "tm6000-regs.h"
30 #include <media/v4l2-common.h>
31 #include <media/tuner.h>
32 #include "tuner-xc2028.h"
35 /*FIXME: Hack to avoid needing to patch i2c-id.h */
36 #define I2C_HW_B_TM6000 I2C_HW_B_EM28XX
37 /* ----------------------------------------------------------- */
39 static unsigned int i2c_debug = 0;
40 module_param(i2c_debug, int, 0644);
41 MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
43 #define i2c_dprintk(lvl,fmt, args...) if (i2c_debug>=lvl) do{ \
44 printk(KERN_DEBUG "%s at %s: " fmt, \
45 dev->name, __FUNCTION__ , ##args); } while (0)
47 static int tm6000_i2c_send_regs(struct tm6000_core *dev, unsigned char addr,
48 __u8 reg, char *buf, int len)
50 return tm6000_read_write_usb(dev, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
51 REQ_16_SET_GET_I2C_WR1_RDN, addr | reg << 8, 0, buf, len);
54 /* Generic read - doesn't work fine with 16bit registers */
55 static int tm6000_i2c_recv_regs(struct tm6000_core *dev, unsigned char addr,
56 __u8 reg, char *buf, int len)
61 if ((dev->caps.has_zl10353) && (dev->demod_addr << 1 == addr) && (reg % 2 == 0)) {
63 * Workaround an I2C bug when reading from zl10353
68 rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
69 REQ_16_SET_GET_I2C_WR1_RDN, addr | reg << 8, 0, b, len);
73 rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
74 REQ_16_SET_GET_I2C_WR1_RDN, addr | reg << 8, 0, buf, len);
81 * read from a 16bit register
82 * for example xc2028, xc3028 or xc3028L
84 static int tm6000_i2c_recv_regs16(struct tm6000_core *dev, unsigned char addr,
85 __u16 reg, char *buf, int len)
87 return tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
88 REQ_14_SET_GET_I2C_WR2_RDN, addr, reg, buf, len);
91 static int tm6000_i2c_xfer(struct i2c_adapter *i2c_adap,
92 struct i2c_msg msgs[], int num)
94 struct tm6000_core *dev = i2c_adap->algo_data;
95 int addr, rc, i, byte;
99 for (i = 0; i < num; i++) {
100 addr = (msgs[i].addr << 1) & 0xff;
101 i2c_dprintk(2,"%s %s addr=0x%x len=%d:",
102 (msgs[i].flags & I2C_M_RD) ? "read" : "write",
103 i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
104 if (msgs[i].flags & I2C_M_RD) {
105 /* read request without preceding register selection */
107 * The TM6000 only supports a read transaction
108 * immediately after a 1 or 2 byte write to select
109 * a register. We cannot fulfil this request.
111 i2c_dprintk(2, " read without preceding write not"
115 } else if (i + 1 < num && msgs[i].len <= 2 &&
116 (msgs[i + 1].flags & I2C_M_RD) &&
117 msgs[i].addr == msgs[i + 1].addr) {
118 /* 1 or 2 byte write followed by a read */
120 for (byte = 0; byte < msgs[i].len; byte++)
121 printk(" %02x", msgs[i].buf[byte]);
122 i2c_dprintk(2, "; joined to read %s len=%d:",
123 i == num - 2 ? "stop" : "nonstop",
126 if (msgs[i].len == 2) {
127 rc = tm6000_i2c_recv_regs16(dev, addr,
128 msgs[i].buf[0] << 8 | msgs[i].buf[1],
129 msgs[i + 1].buf, msgs[i + 1].len);
131 rc = tm6000_i2c_recv_regs(dev, addr, msgs[i].buf[0],
132 msgs[i + 1].buf, msgs[i + 1].len);
137 if (addr == dev->tuner_addr << 1) {
138 tm6000_set_reg(dev, 0x32, 0,0);
139 tm6000_set_reg(dev, 0x33, 0,0);
142 for (byte = 0; byte < msgs[i].len; byte++)
143 printk(" %02x", msgs[i].buf[byte]);
147 for (byte = 0; byte < msgs[i].len; byte++)
148 printk(" %02x", msgs[i].buf[byte]);
149 rc = tm6000_i2c_send_regs(dev, addr, msgs[i].buf[0],
150 msgs[i].buf + 1, msgs[i].len - 1);
152 if (addr == dev->tuner_addr << 1) {
153 tm6000_set_reg(dev, 0x32, 0,0);
154 tm6000_set_reg(dev, 0x33, 0,0);
165 i2c_dprintk(2," ERROR: %i\n", rc);
169 static int tm6000_i2c_eeprom(struct tm6000_core *dev,
170 unsigned char *eedata, int len)
173 unsigned char *p = eedata;
174 unsigned char bytes[17];
176 dev->i2c_client.addr = 0xa0 >> 1;
179 for (i = 0; i < len; ) {
181 rc = tm6000_i2c_recv_regs(dev, 0xa0, i, p, 1);
187 "%s: i2c eeprom read error (err=%d)\n",
194 printk(KERN_INFO "%s: i2c eeprom %02x:", dev->name, i);
195 printk(" %02x", eedata[i]);
196 if ((eedata[i] >= ' ') && (eedata[i] <= 'z')) {
197 bytes[i%16] = eedata[i];
206 printk(" %s\n", bytes);
211 for (i %= 16; i < 16; i++)
214 printk(" %s\n", bytes);
219 printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
224 /* ----------------------------------------------------------- */
229 static u32 functionality(struct i2c_adapter *adap)
231 return I2C_FUNC_SMBUS_EMUL;
234 #define mass_write(addr, reg, data...) \
235 { const static u8 _val[] = data; \
236 rc=tm6000_read_write_usb(dev,USB_DIR_OUT | USB_TYPE_VENDOR, \
237 REQ_16_SET_GET_I2C_WR1_RDN,(reg<<8)+addr, 0x00, (u8 *) _val, \
240 printk(KERN_ERR "Error on line %d: %d\n",__LINE__,rc); \
246 static struct i2c_algorithm tm6000_algo = {
247 .master_xfer = tm6000_i2c_xfer,
248 .functionality = functionality,
251 static struct i2c_adapter tm6000_adap_template = {
252 .owner = THIS_MODULE,
253 .class = I2C_CLASS_TV_ANALOG | I2C_CLASS_TV_DIGITAL,
255 .id = I2C_HW_B_TM6000,
256 .algo = &tm6000_algo,
259 static struct i2c_client tm6000_client_template = {
260 .name = "tm6000 internal",
263 /* ----------------------------------------------------------- */
266 * tm6000_i2c_register()
269 int tm6000_i2c_register(struct tm6000_core *dev)
271 unsigned char eedata[256];
273 dev->i2c_adap = tm6000_adap_template;
274 dev->i2c_adap.dev.parent = &dev->udev->dev;
275 strcpy(dev->i2c_adap.name, dev->name);
276 dev->i2c_adap.algo_data = dev;
277 i2c_add_adapter(&dev->i2c_adap);
279 dev->i2c_client = tm6000_client_template;
280 dev->i2c_client.adapter = &dev->i2c_adap;
282 i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
284 tm6000_i2c_eeprom(dev, eedata, sizeof(eedata));
290 * tm6000_i2c_unregister()
293 int tm6000_i2c_unregister(struct tm6000_core *dev)
295 i2c_del_adapter(&dev->i2c_adap);