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 /* ----------------------------------------------------------- */
37 static unsigned int i2c_debug
;
38 module_param(i2c_debug
, int, 0644);
39 MODULE_PARM_DESC(i2c_debug
, "enable debug messages [i2c]");
41 #define i2c_dprintk(lvl, fmt, args...) if (i2c_debug >= lvl) do { \
42 printk(KERN_DEBUG "%s at %s: " fmt, \
43 dev->name, __func__, ##args); } while (0)
45 static int tm6000_i2c_send_regs(struct tm6000_core
*dev
, unsigned char addr
,
46 __u8 reg
, char *buf
, int len
)
49 unsigned int i2c_packet_limit
= 16;
51 if (dev
->dev_type
== TM6010
)
52 i2c_packet_limit
= 80;
57 if (len
< 1 || len
> i2c_packet_limit
) {
58 printk(KERN_ERR
"Incorrect length of i2c packet = %d, limit set to %d\n",
59 len
, i2c_packet_limit
);
64 rc
= tm6000_read_write_usb(dev
, USB_DIR_OUT
| USB_TYPE_VENDOR
|
65 USB_RECIP_DEVICE
, REQ_16_SET_GET_I2C_WR1_RDN
,
66 addr
| reg
<< 8, 0, buf
, len
);
77 /* Generic read - doesn't work fine with 16bit registers */
78 static int tm6000_i2c_recv_regs(struct tm6000_core
*dev
, unsigned char addr
,
79 __u8 reg
, char *buf
, int len
)
83 unsigned int i2c_packet_limit
= 16;
85 if (dev
->dev_type
== TM6010
)
86 i2c_packet_limit
= 64;
91 if (len
< 1 || len
> i2c_packet_limit
) {
92 printk(KERN_ERR
"Incorrect length of i2c packet = %d, limit set to %d\n",
93 len
, i2c_packet_limit
);
98 if ((dev
->caps
.has_zl10353
) && (dev
->demod_addr
<< 1 == addr
) && (reg
% 2 == 0)) {
100 * Workaround an I2C bug when reading from zl10353
105 rc
= tm6000_read_write_usb(dev
, USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
106 REQ_16_SET_GET_I2C_WR1_RDN
, addr
| reg
<< 8, 0, b
, len
);
110 rc
= tm6000_read_write_usb(dev
, USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
111 REQ_16_SET_GET_I2C_WR1_RDN
, addr
| reg
<< 8, 0, buf
, len
);
119 * read from a 16bit register
120 * for example xc2028, xc3028 or xc3028L
122 static int tm6000_i2c_recv_regs16(struct tm6000_core
*dev
, unsigned char addr
,
123 __u16 reg
, char *buf
, int len
)
128 if (!buf
|| len
!= 2)
132 if (dev
->dev_type
== TM6010
) {
134 rc
= tm6000_read_write_usb(dev
, USB_DIR_OUT
| USB_TYPE_VENDOR
|
135 USB_RECIP_DEVICE
, REQ_16_SET_GET_I2C_WR1_RDN
,
136 addr
| (reg
& 0xFF00), 0, &ureg
, 1);
143 rc
= tm6000_read_write_usb(dev
, USB_DIR_IN
| USB_TYPE_VENDOR
|
144 USB_RECIP_DEVICE
, REQ_35_AFTEK_TUNER_READ
,
147 rc
= tm6000_read_write_usb(dev
, USB_DIR_IN
| USB_TYPE_VENDOR
|
148 USB_RECIP_DEVICE
, REQ_14_SET_GET_I2C_WR2_RDN
,
149 addr
, reg
, buf
, len
);
156 static int tm6000_i2c_xfer(struct i2c_adapter
*i2c_adap
,
157 struct i2c_msg msgs
[], int num
)
159 struct tm6000_core
*dev
= i2c_adap
->algo_data
;
160 int addr
, rc
, i
, byte
;
164 for (i
= 0; i
< num
; i
++) {
165 addr
= (msgs
[i
].addr
<< 1) & 0xff;
166 i2c_dprintk(2, "%s %s addr=0x%x len=%d:",
167 (msgs
[i
].flags
& I2C_M_RD
) ? "read" : "write",
168 i
== num
- 1 ? "stop" : "nonstop", addr
, msgs
[i
].len
);
169 if (msgs
[i
].flags
& I2C_M_RD
) {
170 /* read request without preceding register selection */
172 * The TM6000 only supports a read transaction
173 * immediately after a 1 or 2 byte write to select
174 * a register. We cannot fulfil this request.
176 i2c_dprintk(2, " read without preceding write not"
180 } else if (i
+ 1 < num
&& msgs
[i
].len
<= 2 &&
181 (msgs
[i
+ 1].flags
& I2C_M_RD
) &&
182 msgs
[i
].addr
== msgs
[i
+ 1].addr
) {
183 /* 1 or 2 byte write followed by a read */
185 for (byte
= 0; byte
< msgs
[i
].len
; byte
++)
186 printk(KERN_CONT
" %02x", msgs
[i
].buf
[byte
]);
187 i2c_dprintk(2, "; joined to read %s len=%d:",
188 i
== num
- 2 ? "stop" : "nonstop",
191 if (msgs
[i
].len
== 2) {
192 rc
= tm6000_i2c_recv_regs16(dev
, addr
,
193 msgs
[i
].buf
[0] << 8 | msgs
[i
].buf
[1],
194 msgs
[i
+ 1].buf
, msgs
[i
+ 1].len
);
196 rc
= tm6000_i2c_recv_regs(dev
, addr
, msgs
[i
].buf
[0],
197 msgs
[i
+ 1].buf
, msgs
[i
+ 1].len
);
202 if (addr
== dev
->tuner_addr
<< 1) {
203 tm6000_set_reg(dev
, REQ_50_SET_START
, 0, 0);
204 tm6000_set_reg(dev
, REQ_51_SET_STOP
, 0, 0);
207 for (byte
= 0; byte
< msgs
[i
].len
; byte
++)
208 printk(KERN_CONT
" %02x", msgs
[i
].buf
[byte
]);
212 for (byte
= 0; byte
< msgs
[i
].len
; byte
++)
213 printk(KERN_CONT
" %02x", msgs
[i
].buf
[byte
]);
214 rc
= tm6000_i2c_send_regs(dev
, addr
, msgs
[i
].buf
[0],
215 msgs
[i
].buf
+ 1, msgs
[i
].len
- 1);
218 printk(KERN_CONT
"\n");
225 i2c_dprintk(2, " ERROR: %i\n", rc
);
229 static int tm6000_i2c_eeprom(struct tm6000_core
*dev
)
232 unsigned char *p
= dev
->eedata
;
233 unsigned char bytes
[17];
235 dev
->i2c_client
.addr
= 0xa0 >> 1;
236 dev
->eedata_size
= 0;
239 for (i
= 0; i
< sizeof(dev
->eedata
); ) {
241 rc
= tm6000_i2c_recv_regs(dev
, 0xa0, i
, p
, 1);
243 if (p
== dev
->eedata
)
247 "%s: i2c eeprom read error (err=%d)\n",
255 printk(KERN_INFO
"%s: i2c eeprom %02x:", dev
->name
, i
);
256 printk(KERN_CONT
" %02x", dev
->eedata
[i
]);
257 if ((dev
->eedata
[i
] >= ' ') && (dev
->eedata
[i
] <= 'z'))
258 bytes
[i
%16] = dev
->eedata
[i
];
266 printk(KERN_CONT
" %s\n", bytes
);
271 for (i
%= 16; i
< 16; i
++)
272 printk(KERN_CONT
" ");
273 printk(KERN_CONT
" %s\n", bytes
);
279 printk(KERN_INFO
"%s: Huh, no eeprom present (err=%d)?\n",
284 /* ----------------------------------------------------------- */
289 static u32
functionality(struct i2c_adapter
*adap
)
291 return I2C_FUNC_SMBUS_EMUL
;
294 static const struct i2c_algorithm tm6000_algo
= {
295 .master_xfer
= tm6000_i2c_xfer
,
296 .functionality
= functionality
,
299 /* ----------------------------------------------------------- */
302 * tm6000_i2c_register()
305 int tm6000_i2c_register(struct tm6000_core
*dev
)
309 dev
->i2c_adap
.owner
= THIS_MODULE
;
310 dev
->i2c_adap
.algo
= &tm6000_algo
;
311 dev
->i2c_adap
.dev
.parent
= &dev
->udev
->dev
;
312 strlcpy(dev
->i2c_adap
.name
, dev
->name
, sizeof(dev
->i2c_adap
.name
));
313 dev
->i2c_adap
.algo_data
= dev
;
314 i2c_set_adapdata(&dev
->i2c_adap
, &dev
->v4l2_dev
);
315 rc
= i2c_add_adapter(&dev
->i2c_adap
);
319 dev
->i2c_client
.adapter
= &dev
->i2c_adap
;
320 strlcpy(dev
->i2c_client
.name
, "tm6000 internal", I2C_NAME_SIZE
);
321 tm6000_i2c_eeprom(dev
);
327 * tm6000_i2c_unregister()
330 int tm6000_i2c_unregister(struct tm6000_core
*dev
)
332 i2c_del_adapter(&dev
->i2c_adap
);