1 // SPDX-License-Identifier: GPL-2.0
2 // tm6000-i2c.c - driver for TM5600/TM6000/TM6010 USB video capture devices
4 // Copyright (c) 2006-2007 Mauro Carvalho Chehab <mchehab@kernel.org>
6 // Copyright (c) 2007 Michel Ludwig <michel.ludwig@gmail.com>
7 // - Fix SMBus Read Byte command
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/usb.h>
12 #include <linux/i2c.h>
15 #include "tm6000-regs.h"
16 #include <media/v4l2-common.h>
17 #include <media/tuner.h>
18 #include "tuner-xc2028.h"
21 /* ----------------------------------------------------------- */
23 static unsigned int i2c_debug
;
24 module_param(i2c_debug
, int, 0644);
25 MODULE_PARM_DESC(i2c_debug
, "enable debug messages [i2c]");
27 #define i2c_dprintk(lvl, fmt, args...) if (i2c_debug >= lvl) do { \
28 printk(KERN_DEBUG "%s at %s: " fmt, \
29 dev->name, __func__, ##args); } while (0)
31 static int tm6000_i2c_send_regs(struct tm6000_core
*dev
, unsigned char addr
,
32 __u8 reg
, char *buf
, int len
)
35 unsigned int i2c_packet_limit
= 16;
37 if (dev
->dev_type
== TM6010
)
38 i2c_packet_limit
= 80;
43 if (len
< 1 || len
> i2c_packet_limit
) {
44 printk(KERN_ERR
"Incorrect length of i2c packet = %d, limit set to %d\n",
45 len
, i2c_packet_limit
);
50 rc
= tm6000_read_write_usb(dev
, USB_DIR_OUT
| USB_TYPE_VENDOR
|
51 USB_RECIP_DEVICE
, REQ_16_SET_GET_I2C_WR1_RDN
,
52 addr
| reg
<< 8, 0, buf
, len
);
63 /* Generic read - doesn't work fine with 16bit registers */
64 static int tm6000_i2c_recv_regs(struct tm6000_core
*dev
, unsigned char addr
,
65 __u8 reg
, char *buf
, int len
)
69 unsigned int i2c_packet_limit
= 16;
71 if (dev
->dev_type
== TM6010
)
72 i2c_packet_limit
= 64;
77 if (len
< 1 || len
> i2c_packet_limit
) {
78 printk(KERN_ERR
"Incorrect length of i2c packet = %d, limit set to %d\n",
79 len
, i2c_packet_limit
);
84 if ((dev
->caps
.has_zl10353
) && (dev
->demod_addr
<< 1 == addr
) && (reg
% 2 == 0)) {
86 * Workaround an I2C bug when reading from zl10353
91 rc
= tm6000_read_write_usb(dev
, USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
92 REQ_16_SET_GET_I2C_WR1_RDN
, addr
| reg
<< 8, 0, b
, len
);
96 rc
= tm6000_read_write_usb(dev
, USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
97 REQ_16_SET_GET_I2C_WR1_RDN
, addr
| reg
<< 8, 0, buf
, len
);
105 * read from a 16bit register
106 * for example xc2028, xc3028 or xc3028L
108 static int tm6000_i2c_recv_regs16(struct tm6000_core
*dev
, unsigned char addr
,
109 __u16 reg
, char *buf
, int len
)
114 if (!buf
|| len
!= 2)
118 if (dev
->dev_type
== TM6010
) {
120 rc
= tm6000_read_write_usb(dev
, USB_DIR_OUT
| USB_TYPE_VENDOR
|
121 USB_RECIP_DEVICE
, REQ_16_SET_GET_I2C_WR1_RDN
,
122 addr
| (reg
& 0xFF00), 0, &ureg
, 1);
129 rc
= tm6000_read_write_usb(dev
, USB_DIR_IN
| USB_TYPE_VENDOR
|
130 USB_RECIP_DEVICE
, REQ_35_AFTEK_TUNER_READ
,
133 rc
= tm6000_read_write_usb(dev
, USB_DIR_IN
| USB_TYPE_VENDOR
|
134 USB_RECIP_DEVICE
, REQ_14_SET_GET_I2C_WR2_RDN
,
135 addr
, reg
, buf
, len
);
142 static int tm6000_i2c_xfer(struct i2c_adapter
*i2c_adap
,
143 struct i2c_msg msgs
[], int num
)
145 struct tm6000_core
*dev
= i2c_adap
->algo_data
;
146 int addr
, rc
, i
, byte
;
148 for (i
= 0; i
< num
; i
++) {
149 addr
= (msgs
[i
].addr
<< 1) & 0xff;
150 i2c_dprintk(2, "%s %s addr=0x%x len=%d:",
151 (msgs
[i
].flags
& I2C_M_RD
) ? "read" : "write",
152 i
== num
- 1 ? "stop" : "nonstop", addr
, msgs
[i
].len
);
153 if (msgs
[i
].flags
& I2C_M_RD
) {
154 /* read request without preceding register selection */
156 * The TM6000 only supports a read transaction
157 * immediately after a 1 or 2 byte write to select
158 * a register. We cannot fulfill this request.
160 i2c_dprintk(2, " read without preceding write not supported");
163 } else if (i
+ 1 < num
&& msgs
[i
].len
<= 2 &&
164 (msgs
[i
+ 1].flags
& I2C_M_RD
) &&
165 msgs
[i
].addr
== msgs
[i
+ 1].addr
) {
166 /* 1 or 2 byte write followed by a read */
168 for (byte
= 0; byte
< msgs
[i
].len
; byte
++)
169 printk(KERN_CONT
" %02x", msgs
[i
].buf
[byte
]);
170 i2c_dprintk(2, "; joined to read %s len=%d:",
171 i
== num
- 2 ? "stop" : "nonstop",
174 if (msgs
[i
].len
== 2) {
175 rc
= tm6000_i2c_recv_regs16(dev
, addr
,
176 msgs
[i
].buf
[0] << 8 | msgs
[i
].buf
[1],
177 msgs
[i
+ 1].buf
, msgs
[i
+ 1].len
);
179 rc
= tm6000_i2c_recv_regs(dev
, addr
, msgs
[i
].buf
[0],
180 msgs
[i
+ 1].buf
, msgs
[i
+ 1].len
);
185 if (addr
== dev
->tuner_addr
<< 1) {
186 tm6000_set_reg(dev
, REQ_50_SET_START
, 0, 0);
187 tm6000_set_reg(dev
, REQ_51_SET_STOP
, 0, 0);
190 for (byte
= 0; byte
< msgs
[i
].len
; byte
++)
191 printk(KERN_CONT
" %02x", msgs
[i
].buf
[byte
]);
195 for (byte
= 0; byte
< msgs
[i
].len
; byte
++)
196 printk(KERN_CONT
" %02x", msgs
[i
].buf
[byte
]);
197 rc
= tm6000_i2c_send_regs(dev
, addr
, msgs
[i
].buf
[0],
198 msgs
[i
].buf
+ 1, msgs
[i
].len
- 1);
201 printk(KERN_CONT
"\n");
208 i2c_dprintk(2, " ERROR: %i\n", rc
);
212 static int tm6000_i2c_eeprom(struct tm6000_core
*dev
)
215 unsigned char *p
= dev
->eedata
;
216 unsigned char bytes
[17];
218 dev
->i2c_client
.addr
= 0xa0 >> 1;
219 dev
->eedata_size
= 0;
222 for (i
= 0; i
< sizeof(dev
->eedata
); ) {
224 rc
= tm6000_i2c_recv_regs(dev
, 0xa0, i
, p
, 1);
226 if (p
== dev
->eedata
)
230 "%s: i2c eeprom read error (err=%d)\n",
238 printk(KERN_INFO
"%s: i2c eeprom %02x:", dev
->name
, i
);
239 printk(KERN_CONT
" %02x", dev
->eedata
[i
]);
240 if ((dev
->eedata
[i
] >= ' ') && (dev
->eedata
[i
] <= 'z'))
241 bytes
[i
%16] = dev
->eedata
[i
];
249 printk(KERN_CONT
" %s\n", bytes
);
254 for (i
%= 16; i
< 16; i
++)
255 printk(KERN_CONT
" ");
256 printk(KERN_CONT
" %s\n", bytes
);
262 printk(KERN_INFO
"%s: Huh, no eeprom present (err=%d)?\n",
267 /* ----------------------------------------------------------- */
272 static u32
functionality(struct i2c_adapter
*adap
)
274 return I2C_FUNC_SMBUS_EMUL
;
277 static const struct i2c_algorithm tm6000_algo
= {
278 .master_xfer
= tm6000_i2c_xfer
,
279 .functionality
= functionality
,
282 /* ----------------------------------------------------------- */
285 * tm6000_i2c_register()
288 int tm6000_i2c_register(struct tm6000_core
*dev
)
292 dev
->i2c_adap
.owner
= THIS_MODULE
;
293 dev
->i2c_adap
.algo
= &tm6000_algo
;
294 dev
->i2c_adap
.dev
.parent
= &dev
->udev
->dev
;
295 strscpy(dev
->i2c_adap
.name
, dev
->name
, sizeof(dev
->i2c_adap
.name
));
296 dev
->i2c_adap
.algo_data
= dev
;
297 i2c_set_adapdata(&dev
->i2c_adap
, &dev
->v4l2_dev
);
298 rc
= i2c_add_adapter(&dev
->i2c_adap
);
302 dev
->i2c_client
.adapter
= &dev
->i2c_adap
;
303 strscpy(dev
->i2c_client
.name
, "tm6000 internal", I2C_NAME_SIZE
);
304 tm6000_i2c_eeprom(dev
);
310 * tm6000_i2c_unregister()
313 int tm6000_i2c_unregister(struct tm6000_core
*dev
)
315 i2c_del_adapter(&dev
->i2c_adap
);