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.
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/usb.h>
22 #include <linux/i2c.h>
25 #include "tm6000-regs.h"
26 #include <media/v4l2-common.h>
27 #include <media/tuner.h>
28 #include "tuner-xc2028.h"
31 /* ----------------------------------------------------------- */
33 static unsigned int i2c_debug
;
34 module_param(i2c_debug
, int, 0644);
35 MODULE_PARM_DESC(i2c_debug
, "enable debug messages [i2c]");
37 #define i2c_dprintk(lvl, fmt, args...) if (i2c_debug >= lvl) do { \
38 printk(KERN_DEBUG "%s at %s: " fmt, \
39 dev->name, __func__, ##args); } while (0)
41 static int tm6000_i2c_send_regs(struct tm6000_core
*dev
, unsigned char addr
,
42 __u8 reg
, char *buf
, int len
)
45 unsigned int i2c_packet_limit
= 16;
47 if (dev
->dev_type
== TM6010
)
48 i2c_packet_limit
= 80;
53 if (len
< 1 || len
> i2c_packet_limit
) {
54 printk(KERN_ERR
"Incorrect length of i2c packet = %d, limit set to %d\n",
55 len
, i2c_packet_limit
);
60 rc
= tm6000_read_write_usb(dev
, USB_DIR_OUT
| USB_TYPE_VENDOR
|
61 USB_RECIP_DEVICE
, REQ_16_SET_GET_I2C_WR1_RDN
,
62 addr
| reg
<< 8, 0, buf
, len
);
73 /* Generic read - doesn't work fine with 16bit registers */
74 static int tm6000_i2c_recv_regs(struct tm6000_core
*dev
, unsigned char addr
,
75 __u8 reg
, char *buf
, int len
)
79 unsigned int i2c_packet_limit
= 16;
81 if (dev
->dev_type
== TM6010
)
82 i2c_packet_limit
= 64;
87 if (len
< 1 || len
> i2c_packet_limit
) {
88 printk(KERN_ERR
"Incorrect length of i2c packet = %d, limit set to %d\n",
89 len
, i2c_packet_limit
);
94 if ((dev
->caps
.has_zl10353
) && (dev
->demod_addr
<< 1 == addr
) && (reg
% 2 == 0)) {
96 * Workaround an I2C bug when reading from zl10353
101 rc
= tm6000_read_write_usb(dev
, USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
102 REQ_16_SET_GET_I2C_WR1_RDN
, addr
| reg
<< 8, 0, b
, len
);
106 rc
= tm6000_read_write_usb(dev
, USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
107 REQ_16_SET_GET_I2C_WR1_RDN
, addr
| reg
<< 8, 0, buf
, len
);
115 * read from a 16bit register
116 * for example xc2028, xc3028 or xc3028L
118 static int tm6000_i2c_recv_regs16(struct tm6000_core
*dev
, unsigned char addr
,
119 __u16 reg
, char *buf
, int len
)
124 if (!buf
|| len
!= 2)
128 if (dev
->dev_type
== TM6010
) {
130 rc
= tm6000_read_write_usb(dev
, USB_DIR_OUT
| USB_TYPE_VENDOR
|
131 USB_RECIP_DEVICE
, REQ_16_SET_GET_I2C_WR1_RDN
,
132 addr
| (reg
& 0xFF00), 0, &ureg
, 1);
139 rc
= tm6000_read_write_usb(dev
, USB_DIR_IN
| USB_TYPE_VENDOR
|
140 USB_RECIP_DEVICE
, REQ_35_AFTEK_TUNER_READ
,
143 rc
= tm6000_read_write_usb(dev
, USB_DIR_IN
| USB_TYPE_VENDOR
|
144 USB_RECIP_DEVICE
, REQ_14_SET_GET_I2C_WR2_RDN
,
145 addr
, reg
, buf
, len
);
152 static int tm6000_i2c_xfer(struct i2c_adapter
*i2c_adap
,
153 struct i2c_msg msgs
[], int num
)
155 struct tm6000_core
*dev
= i2c_adap
->algo_data
;
156 int addr
, rc
, i
, byte
;
160 for (i
= 0; i
< num
; i
++) {
161 addr
= (msgs
[i
].addr
<< 1) & 0xff;
162 i2c_dprintk(2, "%s %s addr=0x%x len=%d:",
163 (msgs
[i
].flags
& I2C_M_RD
) ? "read" : "write",
164 i
== num
- 1 ? "stop" : "nonstop", addr
, msgs
[i
].len
);
165 if (msgs
[i
].flags
& I2C_M_RD
) {
166 /* read request without preceding register selection */
168 * The TM6000 only supports a read transaction
169 * immediately after a 1 or 2 byte write to select
170 * a register. We cannot fulfil this request.
172 i2c_dprintk(2, " read without preceding write not supported");
175 } else if (i
+ 1 < num
&& msgs
[i
].len
<= 2 &&
176 (msgs
[i
+ 1].flags
& I2C_M_RD
) &&
177 msgs
[i
].addr
== msgs
[i
+ 1].addr
) {
178 /* 1 or 2 byte write followed by a read */
180 for (byte
= 0; byte
< msgs
[i
].len
; byte
++)
181 printk(KERN_CONT
" %02x", msgs
[i
].buf
[byte
]);
182 i2c_dprintk(2, "; joined to read %s len=%d:",
183 i
== num
- 2 ? "stop" : "nonstop",
186 if (msgs
[i
].len
== 2) {
187 rc
= tm6000_i2c_recv_regs16(dev
, addr
,
188 msgs
[i
].buf
[0] << 8 | msgs
[i
].buf
[1],
189 msgs
[i
+ 1].buf
, msgs
[i
+ 1].len
);
191 rc
= tm6000_i2c_recv_regs(dev
, addr
, msgs
[i
].buf
[0],
192 msgs
[i
+ 1].buf
, msgs
[i
+ 1].len
);
197 if (addr
== dev
->tuner_addr
<< 1) {
198 tm6000_set_reg(dev
, REQ_50_SET_START
, 0, 0);
199 tm6000_set_reg(dev
, REQ_51_SET_STOP
, 0, 0);
202 for (byte
= 0; byte
< msgs
[i
].len
; byte
++)
203 printk(KERN_CONT
" %02x", msgs
[i
].buf
[byte
]);
207 for (byte
= 0; byte
< msgs
[i
].len
; byte
++)
208 printk(KERN_CONT
" %02x", msgs
[i
].buf
[byte
]);
209 rc
= tm6000_i2c_send_regs(dev
, addr
, msgs
[i
].buf
[0],
210 msgs
[i
].buf
+ 1, msgs
[i
].len
- 1);
213 printk(KERN_CONT
"\n");
220 i2c_dprintk(2, " ERROR: %i\n", rc
);
224 static int tm6000_i2c_eeprom(struct tm6000_core
*dev
)
227 unsigned char *p
= dev
->eedata
;
228 unsigned char bytes
[17];
230 dev
->i2c_client
.addr
= 0xa0 >> 1;
231 dev
->eedata_size
= 0;
234 for (i
= 0; i
< sizeof(dev
->eedata
); ) {
236 rc
= tm6000_i2c_recv_regs(dev
, 0xa0, i
, p
, 1);
238 if (p
== dev
->eedata
)
242 "%s: i2c eeprom read error (err=%d)\n",
250 printk(KERN_INFO
"%s: i2c eeprom %02x:", dev
->name
, i
);
251 printk(KERN_CONT
" %02x", dev
->eedata
[i
]);
252 if ((dev
->eedata
[i
] >= ' ') && (dev
->eedata
[i
] <= 'z'))
253 bytes
[i
%16] = dev
->eedata
[i
];
261 printk(KERN_CONT
" %s\n", bytes
);
266 for (i
%= 16; i
< 16; i
++)
267 printk(KERN_CONT
" ");
268 printk(KERN_CONT
" %s\n", bytes
);
274 printk(KERN_INFO
"%s: Huh, no eeprom present (err=%d)?\n",
279 /* ----------------------------------------------------------- */
284 static u32
functionality(struct i2c_adapter
*adap
)
286 return I2C_FUNC_SMBUS_EMUL
;
289 static const struct i2c_algorithm tm6000_algo
= {
290 .master_xfer
= tm6000_i2c_xfer
,
291 .functionality
= functionality
,
294 /* ----------------------------------------------------------- */
297 * tm6000_i2c_register()
300 int tm6000_i2c_register(struct tm6000_core
*dev
)
304 dev
->i2c_adap
.owner
= THIS_MODULE
;
305 dev
->i2c_adap
.algo
= &tm6000_algo
;
306 dev
->i2c_adap
.dev
.parent
= &dev
->udev
->dev
;
307 strlcpy(dev
->i2c_adap
.name
, dev
->name
, sizeof(dev
->i2c_adap
.name
));
308 dev
->i2c_adap
.algo_data
= dev
;
309 i2c_set_adapdata(&dev
->i2c_adap
, &dev
->v4l2_dev
);
310 rc
= i2c_add_adapter(&dev
->i2c_adap
);
314 dev
->i2c_client
.adapter
= &dev
->i2c_adap
;
315 strlcpy(dev
->i2c_client
.name
, "tm6000 internal", I2C_NAME_SIZE
);
316 tm6000_i2c_eeprom(dev
);
322 * tm6000_i2c_unregister()
325 int tm6000_i2c_unregister(struct tm6000_core
*dev
)
327 i2c_del_adapter(&dev
->i2c_adap
);