2 tm6000-core.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>
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>
28 #include "tm6000-regs.h"
29 #include <media/v4l2-common.h>
30 #include <media/tuner.h>
32 #define USB_TIMEOUT 5*HZ /* ms */
34 int tm6000_read_write_usb (struct tm6000_core *dev, u8 req_type, u8 req,
35 u16 value, u16 index, u8 *buf, u16 len)
39 static int ini=0, last=0, n=0;
43 data = kzalloc(len, GFP_KERNEL);
46 if (req_type & USB_DIR_IN)
47 pipe=usb_rcvctrlpipe(dev->udev, 0);
49 pipe=usb_sndctrlpipe(dev->udev, 0);
50 memcpy(data, buf, len);
53 if (tm6000_debug & V4L2_DEBUG_I2C) {
57 printk("%06i (dev %p, pipe %08x): ", n, dev->udev, pipe);
59 printk( "%s: %06u ms %06u ms %02x %02x %02x %02x %02x %02x %02x %02x ",
60 (req_type & USB_DIR_IN)?" IN":"OUT",
61 jiffies_to_msecs(jiffies-last),
62 jiffies_to_msecs(jiffies-ini),
63 req_type, req,value&0xff,value>>8, index&0xff, index>>8,
68 if ( !(req_type & USB_DIR_IN) ) {
71 printk(" %02x",buf[i]);
77 ret = usb_control_msg(dev->udev, pipe, req, req_type, value, index, data,
80 if (req_type & USB_DIR_IN)
81 memcpy(buf, data, len);
83 if (tm6000_debug & V4L2_DEBUG_I2C) {
85 if (req_type & USB_DIR_IN)
86 printk("<<< (len=%d)\n",len);
88 printk("%s: Error #%d\n", __FUNCTION__, ret);
89 } else if (req_type & USB_DIR_IN) {
92 printk(" %02x",buf[i]);
105 int tm6000_set_reg (struct tm6000_core *dev, u8 req, u16 value, u16 index)
108 tm6000_read_write_usb (dev, USB_DIR_OUT | USB_TYPE_VENDOR,
109 req, value, index, NULL, 0);
112 int tm6000_get_reg (struct tm6000_core *dev, u8 req, u16 value, u16 index)
117 rc=tm6000_read_write_usb (dev, USB_DIR_IN | USB_TYPE_VENDOR, req,
118 value, index, buf, 1);
126 int tm6000_get_reg16 (struct tm6000_core *dev, u8 req, u16 value, u16 index)
131 rc=tm6000_read_write_usb (dev, USB_DIR_IN | USB_TYPE_VENDOR, req,
132 value, index, buf, 2);
137 return buf[1]|buf[0]<<8;
140 void tm6000_set_fourcc_format(struct tm6000_core *dev)
142 if (dev->dev_type == TM6010) {
143 if (dev->fourcc == V4L2_PIX_FMT_UYVY)
144 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0xc1, 0xfc);
146 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0xc1, 0xfd);
148 if (dev->fourcc == V4L2_PIX_FMT_UYVY)
149 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0xc1, 0xd0);
151 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0xc1, 0x90);
155 int tm6000_init_analog_mode (struct tm6000_core *dev)
157 if (dev->dev_type == TM6010) {
161 val = tm6000_get_reg(dev, REQ_07_SET_GET_AVREG, 0xcc, 0);
163 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0xcc, val);
164 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0xfe, 0xcf);
167 /* Enables soft reset */
168 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0x3f, 0x01);
171 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0xc0, 0x20);
173 /* Enable Hfilter and disable TS Drop err */
174 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0xc0, 0x80);
177 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0xc3, 0x88);
178 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0xda, 0x23);
179 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0xd1, 0xc0);
180 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0xd2, 0xd8);
181 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0xd6, 0x06);
182 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0xdf, 0x1f);
184 /* AP Software reset */
185 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0xff, 0x08);
186 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0xff, 0x00);
188 tm6000_set_fourcc_format(dev);
190 /* Disables soft reset */
191 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0x3f, 0x00);
193 /* E3: Select input 0 - TV tuner */
194 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0xe3, 0x00);
195 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0xeb, 0x60);
197 /* This controls input */
198 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, TM6000_GPIO_2, 0x0);
199 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, TM6000_GPIO_3, 0x01);
203 /* Tuner firmware can now be loaded */
206 struct v4l2_frequency f;
207 mutex_lock(&dev->lock);
208 f.frequency=dev->freq;
209 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
210 mutex_unlock(&dev->lock);
213 tm6000_set_standard (dev, &dev->norm);
214 tm6000_set_audio_bitrate (dev,48000);
219 int tm6000_init_digital_mode (struct tm6000_core *dev)
221 if (dev->dev_type == TM6010) {
226 val = tm6000_get_reg(dev, REQ_07_SET_GET_AVREG, 0xcc, 0);
228 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0xcc, val);
229 val = tm6000_get_reg(dev, REQ_07_SET_GET_AVREG, 0xc0, 0);
231 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0xc0, val);
232 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0xfe, 0x28);
233 tm6000_set_reg(dev, REQ_08_SET_GET_AVREG_BIT, 0xe2, 0xfc);
234 tm6000_set_reg(dev, REQ_08_SET_GET_AVREG_BIT, 0xe6, 0xff);
235 tm6000_set_reg(dev, REQ_08_SET_GET_AVREG_BIT, 0xf1, 0xfe);
236 tm6000_read_write_usb (dev, 0xc0, 0x0e, 0x00c2, 0x0008, buf, 2);
237 printk (KERN_INFO "buf %#x %#x \n", buf[0], buf[1]);
241 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x00ff, 0x08);
242 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x00ff, 0x00);
243 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x003f, 0x01);
244 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x00df, 0x08);
245 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x00e2, 0x0c);
246 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x00e8, 0xff);
247 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x00eb, 0xd8);
248 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x00c0, 0x40);
249 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x00c1, 0xd0);
250 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x00c3, 0x09);
251 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x00da, 0x37);
252 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x00d1, 0xd8);
253 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x00d2, 0xc0);
254 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x00d6, 0x60);
256 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x00e2, 0x0c);
257 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x00e8, 0xff);
258 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x00eb, 0x08);
261 tm6000_set_reg (dev, REQ_04_EN_DISABLE_MCU_INT, 0x0020, 0x00);
263 tm6000_set_reg (dev, REQ_04_EN_DISABLE_MCU_INT, 0x0020, 0x01);
265 tm6000_set_reg (dev, REQ_04_EN_DISABLE_MCU_INT, 0x0020, 0x00);
277 /* The meaning of those initializations are unknown */
278 struct reg_init tm6000_init_tab[] = {
280 { REQ_07_SET_GET_AVREG, 0xdf, 0x1f },
281 { REQ_07_SET_GET_AVREG, 0xff, 0x08 },
282 { REQ_07_SET_GET_AVREG, 0xff, 0x00 },
283 { REQ_07_SET_GET_AVREG, 0xd5, 0x4f },
284 { REQ_07_SET_GET_AVREG, 0xda, 0x23 },
285 { REQ_07_SET_GET_AVREG, 0xdb, 0x08 },
286 { REQ_07_SET_GET_AVREG, 0xe2, 0x00 },
287 { REQ_07_SET_GET_AVREG, 0xe3, 0x10 },
288 { REQ_07_SET_GET_AVREG, 0xe5, 0x00 },
289 { REQ_07_SET_GET_AVREG, 0xe8, 0x00 },
290 { REQ_07_SET_GET_AVREG, 0xeb, 0x64 }, /* 48000 bits/sample, external input */
291 { REQ_07_SET_GET_AVREG, 0xee, 0xc2 },
292 { REQ_07_SET_GET_AVREG, 0x3f, 0x01 }, /* Start of soft reset */
293 { REQ_07_SET_GET_AVREG, 0x00, 0x00 },
294 { REQ_07_SET_GET_AVREG, 0x01, 0x07 },
295 { REQ_07_SET_GET_AVREG, 0x02, 0x5f },
296 { REQ_07_SET_GET_AVREG, 0x03, 0x00 },
297 { REQ_07_SET_GET_AVREG, 0x05, 0x64 },
298 { REQ_07_SET_GET_AVREG, 0x07, 0x01 },
299 { REQ_07_SET_GET_AVREG, 0x08, 0x82 },
300 { REQ_07_SET_GET_AVREG, 0x09, 0x36 },
301 { REQ_07_SET_GET_AVREG, 0x0a, 0x50 },
302 { REQ_07_SET_GET_AVREG, 0x0c, 0x6a },
303 { REQ_07_SET_GET_AVREG, 0x11, 0xc9 },
304 { REQ_07_SET_GET_AVREG, 0x12, 0x07 },
305 { REQ_07_SET_GET_AVREG, 0x13, 0x3b },
306 { REQ_07_SET_GET_AVREG, 0x14, 0x47 },
307 { REQ_07_SET_GET_AVREG, 0x15, 0x6f },
308 { REQ_07_SET_GET_AVREG, 0x17, 0xcd },
309 { REQ_07_SET_GET_AVREG, 0x18, 0x1e },
310 { REQ_07_SET_GET_AVREG, 0x19, 0x8b },
311 { REQ_07_SET_GET_AVREG, 0x1a, 0xa2 },
312 { REQ_07_SET_GET_AVREG, 0x1b, 0xe9 },
313 { REQ_07_SET_GET_AVREG, 0x1c, 0x1c },
314 { REQ_07_SET_GET_AVREG, 0x1d, 0xcc },
315 { REQ_07_SET_GET_AVREG, 0x1e, 0xcc },
316 { REQ_07_SET_GET_AVREG, 0x1f, 0xcd },
317 { REQ_07_SET_GET_AVREG, 0x20, 0x3c },
318 { REQ_07_SET_GET_AVREG, 0x21, 0x3c },
319 { REQ_07_SET_GET_AVREG, 0x2d, 0x48 },
320 { REQ_07_SET_GET_AVREG, 0x2e, 0x88 },
321 { REQ_07_SET_GET_AVREG, 0x30, 0x22 },
322 { REQ_07_SET_GET_AVREG, 0x31, 0x61 },
323 { REQ_07_SET_GET_AVREG, 0x32, 0x74 },
324 { REQ_07_SET_GET_AVREG, 0x33, 0x1c },
325 { REQ_07_SET_GET_AVREG, 0x34, 0x74 },
326 { REQ_07_SET_GET_AVREG, 0x35, 0x1c },
327 { REQ_07_SET_GET_AVREG, 0x36, 0x7a },
328 { REQ_07_SET_GET_AVREG, 0x37, 0x26 },
329 { REQ_07_SET_GET_AVREG, 0x38, 0x40 },
330 { REQ_07_SET_GET_AVREG, 0x39, 0x0a },
331 { REQ_07_SET_GET_AVREG, 0x42, 0x55 },
332 { REQ_07_SET_GET_AVREG, 0x51, 0x11 },
333 { REQ_07_SET_GET_AVREG, 0x55, 0x01 },
334 { REQ_07_SET_GET_AVREG, 0x57, 0x02 },
335 { REQ_07_SET_GET_AVREG, 0x58, 0x35 },
336 { REQ_07_SET_GET_AVREG, 0x59, 0xa0 },
337 { REQ_07_SET_GET_AVREG, 0x80, 0x15 },
338 { REQ_07_SET_GET_AVREG, 0x82, 0x42 },
339 { REQ_07_SET_GET_AVREG, 0xc1, 0xd0 },
340 { REQ_07_SET_GET_AVREG, 0xc3, 0x88 },
341 { REQ_07_SET_GET_AVREG, 0x3f, 0x00 }, /* End of the soft reset */
342 { REQ_05_SET_GET_USBREG, 0x18, 0x00 },
345 struct reg_init tm6010_init_tab[] = {
346 { REQ_07_SET_GET_AVREG, 0xc0, 0x00 },
347 { REQ_07_SET_GET_AVREG, 0xc4, 0xa0 },
348 { REQ_07_SET_GET_AVREG, 0xc6, 0x40 },
349 { REQ_07_SET_GET_AVREG, 0xca, 0x31 },
350 { REQ_07_SET_GET_AVREG, 0xcc, 0xe1 },
351 { REQ_07_SET_GET_AVREG, 0xe0, 0x03 },
352 { REQ_07_SET_GET_AVREG, 0xfe, 0x7f },
354 { REQ_08_SET_GET_AVREG_BIT, 0xe2, 0xf0 },
355 { REQ_08_SET_GET_AVREG_BIT, 0xe3, 0xf4 },
356 { REQ_08_SET_GET_AVREG_BIT, 0xe4, 0xf8 },
357 { REQ_08_SET_GET_AVREG_BIT, 0xe6, 0x00 },
358 { REQ_08_SET_GET_AVREG_BIT, 0xea, 0xf2 },
359 { REQ_08_SET_GET_AVREG_BIT, 0xeb, 0xf0 },
360 { REQ_08_SET_GET_AVREG_BIT, 0xec, 0xc2 },
361 { REQ_08_SET_GET_AVREG_BIT, 0xf0, 0x60 },
362 { REQ_08_SET_GET_AVREG_BIT, 0xf1, 0xfc },
364 { REQ_07_SET_GET_AVREG, 0x3f, 0x01 },
365 { REQ_07_SET_GET_AVREG, 0x00, 0x00 },
366 { REQ_07_SET_GET_AVREG, 0x01, 0x07 },
367 { REQ_07_SET_GET_AVREG, 0x02, 0x5f },
368 { REQ_07_SET_GET_AVREG, 0x03, 0x00 },
369 { REQ_07_SET_GET_AVREG, 0x05, 0x64 },
370 { REQ_07_SET_GET_AVREG, 0x07, 0x01 },
371 { REQ_07_SET_GET_AVREG, 0x08, 0x82 },
372 { REQ_07_SET_GET_AVREG, 0x09, 0x36 },
373 { REQ_07_SET_GET_AVREG, 0x0a, 0x50 },
374 { REQ_07_SET_GET_AVREG, 0x0c, 0x6a },
375 { REQ_07_SET_GET_AVREG, 0x11, 0xc9 },
376 { REQ_07_SET_GET_AVREG, 0x12, 0x07 },
377 { REQ_07_SET_GET_AVREG, 0x13, 0x3b },
378 { REQ_07_SET_GET_AVREG, 0x14, 0x47 },
379 { REQ_07_SET_GET_AVREG, 0x15, 0x6f },
380 { REQ_07_SET_GET_AVREG, 0x17, 0xcd },
381 { REQ_07_SET_GET_AVREG, 0x18, 0x1e },
382 { REQ_07_SET_GET_AVREG, 0x19, 0x8b },
383 { REQ_07_SET_GET_AVREG, 0x1a, 0xa2 },
384 { REQ_07_SET_GET_AVREG, 0x1b, 0xe9 },
385 { REQ_07_SET_GET_AVREG, 0x1c, 0x1c },
386 { REQ_07_SET_GET_AVREG, 0x1d, 0xcc },
387 { REQ_07_SET_GET_AVREG, 0x1e, 0xcc },
388 { REQ_07_SET_GET_AVREG, 0x1f, 0xcd },
389 { REQ_07_SET_GET_AVREG, 0x20, 0x3c },
390 { REQ_07_SET_GET_AVREG, 0x21, 0x3c },
391 { REQ_07_SET_GET_AVREG, 0x2d, 0x48 },
392 { REQ_07_SET_GET_AVREG, 0x2e, 0x88 },
393 { REQ_07_SET_GET_AVREG, 0x30, 0x22 },
394 { REQ_07_SET_GET_AVREG, 0x31, 0x61 },
395 { REQ_07_SET_GET_AVREG, 0x32, 0x74 },
396 { REQ_07_SET_GET_AVREG, 0x33, 0x1c },
397 { REQ_07_SET_GET_AVREG, 0x34, 0x74 },
398 { REQ_07_SET_GET_AVREG, 0x35, 0x1c },
399 { REQ_07_SET_GET_AVREG, 0x36, 0x7a },
400 { REQ_07_SET_GET_AVREG, 0x37, 0x26 },
401 { REQ_07_SET_GET_AVREG, 0x38, 0x40 },
402 { REQ_07_SET_GET_AVREG, 0x39, 0x0a },
403 { REQ_07_SET_GET_AVREG, 0x42, 0x55 },
404 { REQ_07_SET_GET_AVREG, 0x51, 0x11 },
405 { REQ_07_SET_GET_AVREG, 0x55, 0x01 },
406 { REQ_07_SET_GET_AVREG, 0x57, 0x02 },
407 { REQ_07_SET_GET_AVREG, 0x58, 0x35 },
408 { REQ_07_SET_GET_AVREG, 0x59, 0xa0 },
409 { REQ_07_SET_GET_AVREG, 0x80, 0x15 },
410 { REQ_07_SET_GET_AVREG, 0x82, 0x42 },
411 { REQ_07_SET_GET_AVREG, 0xc1, 0xd0 },
412 { REQ_07_SET_GET_AVREG, 0xc3, 0x88 },
413 { REQ_07_SET_GET_AVREG, 0x3f, 0x00 },
415 { REQ_05_SET_GET_USBREG, 0x18, 0x00 },
417 /* set remote wakeup key:any key wakeup */
418 { REQ_07_SET_GET_AVREG, 0xe5, 0xfe },
419 { REQ_07_SET_GET_AVREG, 0xda, 0xff },
422 int tm6000_init (struct tm6000_core *dev)
424 int board, rc=0, i, size;
425 struct reg_init *tab;
427 if (dev->dev_type == TM6010) {
428 tab = tm6010_init_tab;
429 size = ARRAY_SIZE(tm6010_init_tab);
431 tab = tm6000_init_tab;
432 size = ARRAY_SIZE(tm6000_init_tab);
435 /* Load board's initialization table */
436 for (i=0; i< size; i++) {
437 rc= tm6000_set_reg (dev, tab[i].req, tab[i].reg, tab[i].val);
439 printk (KERN_ERR "Error %i while setting req %d, "
440 "reg %d to value %d\n", rc,
441 tab[i].req,tab[i].reg, tab[i].val);
446 msleep(5); /* Just to be conservative */
448 /* Check board version - maybe 10Moons specific */
449 board=tm6000_get_reg16 (dev, 0x40, 0, 0);
451 printk (KERN_INFO "Board version = 0x%04x\n",board);
453 printk (KERN_ERR "Error %i while retrieving board version\n",board);
456 rc = tm6000_cards_setup(dev);
461 int tm6000_set_audio_bitrate(struct tm6000_core *dev, int bitrate)
465 val=tm6000_get_reg (dev, REQ_07_SET_GET_AVREG, 0xeb, 0x0);
466 printk("Original value=%d\n",val);
470 val &= 0x0f; /* Preserve the audio input control bits */
474 dev->audio_bitrate=bitrate;
478 dev->audio_bitrate=bitrate;
481 val=tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0xeb, val);
485 EXPORT_SYMBOL_GPL(tm6000_set_audio_bitrate);