2 tm6000-i2c.c - driver for TM5600/TM6000 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_scan = 0;
40 module_param(i2c_scan, int, 0444);
41 MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
43 static unsigned int i2c_debug = 0;
44 module_param(i2c_debug, int, 0644);
45 MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
47 #define i2c_dprintk(lvl,fmt, args...) if (i2c_debug>=lvl) do{ \
48 printk(KERN_DEBUG "%s at %s: " fmt, \
49 dev->name, __FUNCTION__ , ##args); } while (0)
52 /* Returns 0 if address is found */
53 static int tm6000_i2c_scan(struct i2c_adapter *i2c_adap, int addr)
55 struct tm6000_core *dev = i2c_adap->algo_data;
58 /* HACK: i2c scan is not working yet */
60 (dev->caps.has_tuner && (addr==dev->tuner_addr)) ||
61 (dev->caps.has_tda9874 && (addr==0xb0)) ||
62 (dev->caps.has_eeprom && (addr==0xa0))
64 printk("Hack: enabling device at addr 0x%02x\n",addr);
73 /* This sends addr + 1 byte with 0 */
74 rc = tm6000_read_write_usb (dev,
75 USB_DIR_IN | USB_TYPE_VENDOR,
76 REQ_16_SET_GET_I2CSEQ,
83 printk("no device at addr 0x%02x\n",addr);
86 printk("Hack: check on addr 0x%02x returned %d\n",addr,rc);
92 static int tm6000_i2c_xfer(struct i2c_adapter *i2c_adap,
93 struct i2c_msg msgs[], int num)
95 struct tm6000_core *dev = i2c_adap->algo_data;
96 int addr, rc, i, byte;
101 for (i = 0; i < num; i++) {
102 addr = (msgs[i].addr << 1) &0xff;
103 i2c_dprintk(2,"%s %s addr=0x%x len=%d:",
104 (msgs[i].flags & I2C_M_RD) ? "read" : "write",
105 i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
108 rc=tm6000_i2c_scan(i2c_adap, addr);
109 } else if (msgs[i].flags & I2C_M_RD) {
111 /* I2C is assumed to have always a subaddr at the first byte of the
112 message bus. Also, the first i2c value of the answer is returned
115 /* SMBus Read Byte command */
116 if(msgs[i].len == 1) {
117 // we use the previously used register to read from
118 rc = tm6000_read_write_usb (dev,
119 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
120 REQ_16_SET_GET_I2CSEQ,
121 addr | prev_reg<<8, 0,
122 msgs[i].buf, msgs[i].len);
125 rc = tm6000_read_write_usb (dev,
126 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
127 REQ_16_SET_GET_I2CSEQ,
128 addr|(*msgs[i].buf)<<8, 0,
129 msgs[i].buf, msgs[i].len);
132 for (byte = 0; byte < msgs[i].len; byte++) {
133 printk(" %02x", msgs[i].buf[byte]);
139 for (byte = 0; byte < msgs[i].len; byte++)
140 printk(" %02x", msgs[i].buf[byte]);
143 /* SMBus Write Byte command followed by a read command */
144 if(msgs[i].len == 1 && i+1 < num && msgs[i+1].flags & I2C_M_RD
145 && msgs[i+1].addr == msgs[i].addr) {
146 prev_reg = msgs[i].buf[0];
150 rc = tm6000_read_write_usb (dev,
151 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
152 REQ_16_SET_GET_I2CSEQ,
153 addr|(*msgs[i].buf)<<8, 0,
154 msgs[i].buf+1, msgs[i].len-1);
156 if(msgs[i].len >= 1) {
157 prev_reg = msgs[i].buf[0];
171 i2c_dprintk(2," ERROR: %i\n", rc);
176 static int tm6000_i2c_eeprom( struct tm6000_core *dev,
177 unsigned char *eedata, int len )
180 unsigned char *p = eedata;
181 unsigned char bytes[17];
183 dev->i2c_client.addr = 0xa0 >> 1;
185 //006779: OUT: 000006 ms 089867 ms c0 0e a0 00 00 00 01 00 <<< 00
186 //006780: OUT: 000005 ms 089873 ms c0 10 a0 00 00 00 01 00 <<< 00
187 //006781: OUT: 000108 ms 089878 ms 40 0e a0 00 00 00 01 00 >>> 99
188 //006782: OUT: 000015 ms 089986 ms c0 0e a0 00 01 00 01 00 <<< 99
189 //006783: OUT: 000004 ms 090001 ms c0 0e a0 00 10 00 01 00 <<< 99
190 //006784: OUT: 000005 ms 090005 ms 40 10 a0 00 00 00 01 00 >>> 00
191 //006785: OUT: 000308 ms 090010 ms 40 0e a0 00 00 00 01 00 >>> 00
194 for (i = 0; i < len; i++) {
197 rc = i2c_master_recv(&dev->i2c_client, p, 1);
200 printk (KERN_WARNING "%s doesn't have eeprom",
204 "%s: i2c eeprom read error (err=%d)\n",
211 printk(KERN_INFO "%s: i2c eeprom %02x:", dev->name, i);
212 printk(" %02x", eedata[i]);
213 if ((eedata[i]>=' ')&&(eedata[i]<='z')) {
214 bytes[i%16]=eedata[i];
218 if (15 == (i % 16)) {
220 printk(" %s\n", bytes);
225 printk(" %s\n", bytes);
230 /* ----------------------------------------------------------- */
235 static int algo_control(struct i2c_adapter *adapter,
236 unsigned int cmd, unsigned long arg)
244 static u32 functionality(struct i2c_adapter *adap)
246 return I2C_FUNC_SMBUS_EMUL;
250 static void inc_use(struct i2c_adapter *adap)
255 static void dec_use(struct i2c_adapter *adap)
261 #define mass_write(addr, reg, data...) \
262 { const static u8 _val[] = data; \
263 rc=tm6000_read_write_usb(dev,USB_DIR_OUT | USB_TYPE_VENDOR, \
264 REQ_16_SET_GET_I2CSEQ,(reg<<8)+addr, 0x00, (u8 *) _val, \
267 printk(KERN_ERR "Error on line %d: %d\n",__LINE__,rc); \
273 /* Tuner callback to provide the proper gpio changes needed for xc2028 */
275 static int tm6000_tuner_callback(void *ptr, int command, int arg)
278 struct tm6000_core *dev = ptr;
280 if (dev->tuner_type!=TUNER_XC2028)
284 case XC2028_RESET_CLK:
285 tm6000_set_reg (dev, REQ_04_EN_DISABLE_MCU_INT,
288 rc=tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
293 rc=tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
296 case XC2028_TUNER_RESET:
297 /* Reset codes during load firmware */
300 tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
301 dev->tuner_reset_gpio, 0x00);
303 tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
304 dev->tuner_reset_gpio, 0x01);
307 tm6000_set_reg (dev, REQ_04_EN_DISABLE_MCU_INT,
313 rc=tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
318 rc=tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
326 static int attach_inform(struct i2c_client *client)
328 struct tm6000_core *dev = client->adapter->algo_data;
329 struct tuner_setup tun_setup;
330 unsigned char eedata[11];
332 i2c_dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n",
333 client->driver->driver.name, client->addr, client->name);
335 switch (client->addr<<1) {
337 request_module("tvaudio");
341 /* If tuner, initialize the tuner part */
342 if ( dev->tuner_addr != client->addr<<1 ) {
346 memset (&tun_setup, 0, sizeof(tun_setup));
348 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
349 tun_setup.type = dev->tuner_type;
350 tun_setup.addr = dev->tuner_addr>>1;
351 tun_setup.tuner_callback = tm6000_tuner_callback;
353 client->driver->command (client,TUNER_SET_TYPE_ADDR, &tun_setup);
358 static struct i2c_algorithm tm6000_algo = {
359 .master_xfer = tm6000_i2c_xfer,
360 .algo_control = algo_control,
361 .functionality = functionality,
364 static struct i2c_adapter tm6000_adap_template = {
366 .owner = THIS_MODULE,
371 .class = I2C_CLASS_TV_ANALOG,
373 .id = I2C_HW_B_TM6000,
374 .algo = &tm6000_algo,
375 .client_register = attach_inform,
378 static struct i2c_client tm6000_client_template = {
379 .name = "tm6000 internal",
382 /* ----------------------------------------------------------- */
386 * incomplete list of known devices
388 static char *i2c_devs[128] = {
389 [0xc2 >> 1] = "tuner (analog)",
394 * check i2c address range for devices
396 static void do_i2c_scan(char *name, struct i2c_client *c)
401 for (i = 0; i < 128; i++) {
403 rc = i2c_master_recv(c, &buf, 0);
406 printk(KERN_INFO "%s: found i2c device @ 0x%x [%s]\n", name,
407 i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
412 * tm6000_i2c_call_clients()
413 * send commands to all attached i2c devices
415 void tm6000_i2c_call_clients(struct tm6000_core *dev, unsigned int cmd, void *arg)
417 BUG_ON(NULL == dev->i2c_adap.algo_data);
418 i2c_clients_command(&dev->i2c_adap, cmd, arg);
422 * tm6000_i2c_register()
425 int tm6000_i2c_register(struct tm6000_core *dev)
427 dev->i2c_adap = tm6000_adap_template;
428 dev->i2c_adap.dev.parent = &dev->udev->dev;
429 strcpy(dev->i2c_adap.name, dev->name);
430 dev->i2c_adap.algo_data = dev;
431 i2c_add_adapter(&dev->i2c_adap);
433 dev->i2c_client = tm6000_client_template;
434 dev->i2c_client.adapter = &dev->i2c_adap;
437 do_i2c_scan(dev->name, &dev->i2c_client);
443 * tm6000_i2c_unregister()
446 int tm6000_i2c_unregister(struct tm6000_core *dev)
448 i2c_del_adapter(&dev->i2c_adap);