]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/tm6000/tm6000-core.c
[media] V4L-DVB: tm6000: Move VBI init to a separate function
[mv-sheeva.git] / drivers / staging / tm6000 / tm6000-core.c
1 /*
2    tm6000-core.c - driver for TM5600/TM6000/TM6010 USB video capture devices
3
4    Copyright (C) 2006-2007 Mauro Carvalho Chehab <mchehab@infradead.org>
5
6    Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com>
7        - DVB-T support
8
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
12
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.
17
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.
21  */
22
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/usb.h>
27 #include <linux/i2c.h>
28 #include "tm6000.h"
29 #include "tm6000-regs.h"
30 #include <media/v4l2-common.h>
31 #include <media/tuner.h>
32
33 #define USB_TIMEOUT     5*HZ /* ms */
34
35 int tm6000_read_write_usb(struct tm6000_core *dev, u8 req_type, u8 req,
36                           u16 value, u16 index, u8 *buf, u16 len)
37 {
38         int          ret, i;
39         unsigned int pipe;
40         static int   ini = 0, last = 0, n = 0;
41         u8           *data = NULL;
42
43         if (len)
44                 data = kzalloc(len, GFP_KERNEL);
45
46
47         if (req_type & USB_DIR_IN)
48                 pipe = usb_rcvctrlpipe(dev->udev, 0);
49         else {
50                 pipe = usb_sndctrlpipe(dev->udev, 0);
51                 memcpy(data, buf, len);
52         }
53
54         if (tm6000_debug & V4L2_DEBUG_I2C) {
55                 if (!ini)
56                         last = ini = jiffies;
57
58                 printk("%06i (dev %p, pipe %08x): ", n, dev->udev, pipe);
59
60                 printk("%s: %06u ms %06u ms %02x %02x %02x %02x %02x %02x %02x %02x ",
61                         (req_type & USB_DIR_IN) ? " IN" : "OUT",
62                         jiffies_to_msecs(jiffies-last),
63                         jiffies_to_msecs(jiffies-ini),
64                         req_type, req, value&0xff, value>>8, index&0xff,
65                         index>>8, len&0xff, len>>8);
66                 last = jiffies;
67                 n++;
68
69                 if (!(req_type & USB_DIR_IN)) {
70                         printk(">>> ");
71                         for (i = 0; i < len; i++)
72                                 printk(" %02x", buf[i]);
73                 printk("\n");
74                 }
75         }
76
77         ret = usb_control_msg(dev->udev, pipe, req, req_type, value, index,
78                               data, len, USB_TIMEOUT);
79
80         if (req_type &  USB_DIR_IN)
81                 memcpy(buf, data, len);
82
83         if (tm6000_debug & V4L2_DEBUG_I2C) {
84                 if (ret < 0) {
85                         if (req_type &  USB_DIR_IN)
86                                 printk("<<< (len=%d)\n", len);
87
88                         printk("%s: Error #%d\n", __FUNCTION__, ret);
89                 } else if (req_type &  USB_DIR_IN) {
90                         printk("<<< ");
91                         for (i = 0; i < len; i++)
92                                 printk(" %02x", buf[i]);
93                         printk("\n");
94                 }
95         }
96
97         kfree(data);
98
99         msleep(5);
100
101         return ret;
102 }
103
104 int tm6000_set_reg(struct tm6000_core *dev, u8 req, u16 value, u16 index)
105 {
106         return
107                 tm6000_read_write_usb(dev, USB_DIR_OUT | USB_TYPE_VENDOR,
108                                       req, value, index, NULL, 0);
109 }
110 EXPORT_SYMBOL_GPL(tm6000_set_reg);
111
112 int tm6000_get_reg(struct tm6000_core *dev, u8 req, u16 value, u16 index)
113 {
114         int rc;
115         u8 buf[1];
116
117         rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR, req,
118                                         value, index, buf, 1);
119
120         if (rc < 0)
121                 return rc;
122
123         return *buf;
124 }
125 EXPORT_SYMBOL_GPL(tm6000_get_reg);
126
127 int tm6000_get_reg16(struct tm6000_core *dev, u8 req, u16 value, u16 index)
128 {
129         int rc;
130         u8 buf[2];
131
132         rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR, req,
133                                         value, index, buf, 2);
134
135         if (rc < 0)
136                 return rc;
137
138         return buf[1]|buf[0]<<8;
139 }
140
141 int tm6000_get_reg32(struct tm6000_core *dev, u8 req, u16 value, u16 index)
142 {
143         int rc;
144         u8 buf[4];
145
146         rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR, req,
147                                         value, index, buf, 4);
148
149         if (rc < 0)
150                 return rc;
151
152         return buf[3] | buf[2] << 8 | buf[1] << 16 | buf[0] << 24;
153 }
154
155 int tm6000_i2c_reset(struct tm6000_core *dev, u16 tsleep)
156 {
157         int rc;
158
159         rc = tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, TM6000_GPIO_CLK, 0);
160         if (rc < 0)
161                 return rc;
162
163         msleep(tsleep);
164
165         rc = tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, TM6000_GPIO_CLK, 1);
166         msleep(tsleep);
167
168         return rc;
169 }
170
171 void tm6000_set_fourcc_format(struct tm6000_core *dev)
172 {
173         if (dev->dev_type == TM6010) {
174                 int val;
175
176                 val = tm6000_get_reg(dev, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, 0) & 0xfc;
177                 if (dev->fourcc == V4L2_PIX_FMT_UYVY)
178                         tm6000_set_reg(dev, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, val);
179                 else
180                         tm6000_set_reg(dev, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, val | 1);
181         } else {
182                 if (dev->fourcc == V4L2_PIX_FMT_UYVY)
183                         tm6000_set_reg(dev, TM6010_REQ07_RC1_TRESHOLD, 0xd0);
184                 else
185                         tm6000_set_reg(dev, TM6010_REQ07_RC1_TRESHOLD, 0x90);
186         }
187 }
188
189 static void tm6000_set_vbi(struct tm6000_core *dev)
190 {
191         /*
192          * FIXME:
193          * VBI lines and start/end are different between 60Hz and 50Hz
194          * So, it is very likely that we need to change the config to
195          * something that takes it into account, doing something different
196          * if (dev->norm & V4L2_STD_525_60)
197          */
198
199         if (dev->dev_type == TM6010) {
200                 tm6000_set_reg(dev, TM6010_REQ07_R3F_RESET, 0x01);
201                 tm6000_set_reg(dev, TM6010_REQ07_R41_TELETEXT_VBI_CODE1, 0x27);
202                 tm6000_set_reg(dev, TM6010_REQ07_R42_VBI_DATA_HIGH_LEVEL, 0x55);
203                 tm6000_set_reg(dev, TM6010_REQ07_R43_VBI_DATA_TYPE_LINE7, 0x66);
204                 tm6000_set_reg(dev, TM6010_REQ07_R44_VBI_DATA_TYPE_LINE8, 0x66);
205                 tm6000_set_reg(dev, TM6010_REQ07_R45_VBI_DATA_TYPE_LINE9, 0x66);
206                 tm6000_set_reg(dev,
207                         TM6010_REQ07_R46_VBI_DATA_TYPE_LINE10, 0x66);
208                 tm6000_set_reg(dev,
209                         TM6010_REQ07_R47_VBI_DATA_TYPE_LINE11, 0x66);
210                 tm6000_set_reg(dev,
211                         TM6010_REQ07_R48_VBI_DATA_TYPE_LINE12, 0x66);
212                 tm6000_set_reg(dev,
213                         TM6010_REQ07_R49_VBI_DATA_TYPE_LINE13, 0x66);
214                 tm6000_set_reg(dev,
215                         TM6010_REQ07_R4A_VBI_DATA_TYPE_LINE14, 0x66);
216                 tm6000_set_reg(dev,
217                         TM6010_REQ07_R4B_VBI_DATA_TYPE_LINE15, 0x66);
218                 tm6000_set_reg(dev,
219                         TM6010_REQ07_R4C_VBI_DATA_TYPE_LINE16, 0x66);
220                 tm6000_set_reg(dev,
221                         TM6010_REQ07_R4D_VBI_DATA_TYPE_LINE17, 0x66);
222                 tm6000_set_reg(dev,
223                         TM6010_REQ07_R4E_VBI_DATA_TYPE_LINE18, 0x66);
224                 tm6000_set_reg(dev,
225                         TM6010_REQ07_R4F_VBI_DATA_TYPE_LINE19, 0x66);
226                 tm6000_set_reg(dev,
227                         TM6010_REQ07_R50_VBI_DATA_TYPE_LINE20, 0x66);
228                 tm6000_set_reg(dev,
229                         TM6010_REQ07_R51_VBI_DATA_TYPE_LINE21, 0x66);
230                 tm6000_set_reg(dev,
231                         TM6010_REQ07_R52_VBI_DATA_TYPE_LINE22, 0x66);
232                 tm6000_set_reg(dev,
233                         TM6010_REQ07_R53_VBI_DATA_TYPE_LINE23, 0x00);
234                 tm6000_set_reg(dev,
235                         TM6010_REQ07_R54_VBI_DATA_TYPE_RLINES, 0x00);
236                 tm6000_set_reg(dev,
237                         TM6010_REQ07_R55_VBI_LOOP_FILTER_GAIN, 0x01);
238                 tm6000_set_reg(dev,
239                         TM6010_REQ07_R56_VBI_LOOP_FILTER_I_GAIN, 0x00);
240                 tm6000_set_reg(dev,
241                         TM6010_REQ07_R57_VBI_LOOP_FILTER_P_GAIN, 0x02);
242                 tm6000_set_reg(dev, TM6010_REQ07_R58_VBI_CAPTION_DTO1, 0x35);
243                 tm6000_set_reg(dev, TM6010_REQ07_R59_VBI_CAPTION_DTO0, 0xa0);
244                 tm6000_set_reg(dev, TM6010_REQ07_R5A_VBI_TELETEXT_DTO1, 0x11);
245                 tm6000_set_reg(dev, TM6010_REQ07_R5B_VBI_TELETEXT_DTO0, 0x4c);
246                 tm6000_set_reg(dev, TM6010_REQ07_R40_TELETEXT_VBI_CODE0, 0x01);
247                 tm6000_set_reg(dev, TM6010_REQ07_R3F_RESET, 0x00);
248         }
249 }
250
251 int tm6000_init_analog_mode(struct tm6000_core *dev)
252 {
253         struct v4l2_frequency f;
254
255         if (dev->dev_type == TM6010) {
256                 int val;
257
258                 /* Enable video */
259                 val = tm6000_get_reg(dev, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, 0);
260                 val |= 0x60;
261                 tm6000_set_reg(dev, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, val);
262                 val = tm6000_get_reg(dev,
263                         TM6010_REQ07_RC0_ACTIVE_VIDEO_SOURCE, 0);
264                 val &= ~0x40;
265                 tm6000_set_reg(dev, TM6010_REQ07_RC0_ACTIVE_VIDEO_SOURCE, val);
266
267                 tm6000_set_reg(dev, TM6010_REQ08_RF1_AADC_POWER_DOWN, 0xfc);
268
269         } else {
270                 /* Enables soft reset */
271                 tm6000_set_reg(dev, TM6010_REQ07_R3F_RESET, 0x01);
272
273                 if (dev->scaler)
274                         tm6000_set_reg(dev, TM6010_REQ07_RC0_ACTIVE_VIDEO_SOURCE, 0x20);
275                 else    /* Enable Hfilter and disable TS Drop err */
276                         tm6000_set_reg(dev, TM6010_REQ07_RC0_ACTIVE_VIDEO_SOURCE, 0x80);
277
278                 tm6000_set_reg(dev, TM6010_REQ07_RC3_HSTART1, 0x88);
279                 tm6000_set_reg(dev, TM6010_REQ07_RD8_IR_WAKEUP_SEL, 0x23);
280                 tm6000_set_reg(dev, TM6010_REQ07_RD1_ADDR_FOR_REQ1, 0xc0);
281                 tm6000_set_reg(dev, TM6010_REQ07_RD2_ADDR_FOR_REQ2, 0xd8);
282                 tm6000_set_reg(dev, TM6010_REQ07_RD6_ENDP_REQ1_REQ2, 0x06);
283                 tm6000_set_reg(dev, TM6010_REQ07_RD8_IR_PULSE_CNT0, 0x1f);
284
285                 /* AP Software reset */
286                 tm6000_set_reg(dev, TM6010_REQ07_RFF_SOFT_RESET, 0x08);
287                 tm6000_set_reg(dev, TM6010_REQ07_RFF_SOFT_RESET, 0x00);
288
289                 tm6000_set_fourcc_format(dev);
290
291                 /* Disables soft reset */
292                 tm6000_set_reg(dev, TM6010_REQ07_R3F_RESET, 0x00);
293
294                 /* E3: Select input 0 - TV tuner */
295                 tm6000_set_reg(dev, TM6010_REQ07_RE3_OUT_SEL1, 0x00);
296                 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0xeb, 0x60);
297
298                 /* This controls input */
299                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, TM6000_GPIO_2, 0x0);
300                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, TM6000_GPIO_3, 0x01);
301         }
302         msleep(20);
303
304         /* Tuner firmware can now be loaded */
305
306         /*
307          * FIXME: This is a hack! xc3028 "sleeps" when no channel is detected
308          * for more than a few seconds. Not sure why, as this behavior does
309          * not happen on other devices with xc3028. So, I suspect that it
310          * is yet another bug at tm6000. After start sleeping, decoding 
311          * doesn't start automatically. Instead, it requires some
312          * I2C commands to wake it up. As we want to have image at the
313          * beginning, we needed to add this hack. The better would be to
314          * discover some way to make tm6000 to wake up without this hack.
315          */
316         mutex_lock(&dev->lock);
317         f.frequency = dev->freq;
318         v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
319         mutex_unlock(&dev->lock);
320
321         msleep(100);
322         tm6000_set_standard(dev, &dev->norm);
323         tm6000_set_vbi(dev);
324         tm6000_set_audio_bitrate(dev, 48000);
325
326         /* switch dvb led off */
327         if (dev->gpio.dvb_led) {
328                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
329                         dev->gpio.dvb_led, 0x01);
330         }
331
332         return 0;
333 }
334
335 int tm6000_init_digital_mode(struct tm6000_core *dev)
336 {
337         if (dev->dev_type == TM6010) {
338                 int val;
339                 u8 buf[2];
340
341                 /* digital init */
342                 val = tm6000_get_reg(dev, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, 0);
343                 val &= ~0x60;
344                 tm6000_set_reg(dev, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, val);
345                 val = tm6000_get_reg(dev, TM6010_REQ07_RC0_ACTIVE_VIDEO_SOURCE, 0);
346                 val |= 0x40;
347                 tm6000_set_reg(dev, TM6010_REQ07_RC0_ACTIVE_VIDEO_SOURCE, val);
348                 tm6000_set_reg(dev, TM6010_REQ07_RFE_POWER_DOWN, 0x28);
349                 tm6000_set_reg(dev, TM6010_REQ08_RE2_POWER_DOWN_CTRL1, 0xfc);
350                 tm6000_set_reg(dev, TM6010_REQ08_RE6_POWER_DOWN_CTRL2, 0xff);
351                 tm6000_read_write_usb(dev, 0xc0, 0x0e, 0x00c2, 0x0008, buf, 2);
352                 printk(KERN_INFO"buf %#x %#x\n", buf[0], buf[1]);
353         } else  {
354                 tm6000_set_reg(dev, TM6010_REQ07_RFF_SOFT_RESET, 0x08);
355                 tm6000_set_reg(dev, TM6010_REQ07_RFF_SOFT_RESET, 0x00);
356                 tm6000_set_reg(dev, TM6010_REQ07_R3F_RESET, 0x01);
357                 tm6000_set_reg(dev, TM6010_REQ07_RD8_IR_PULSE_CNT0, 0x08);
358                 tm6000_set_reg(dev, TM6010_REQ07_RE2_OUT_SEL2, 0x0c);
359                 tm6000_set_reg(dev, TM6010_REQ07_RE8_TYPESEL_MOS_I2S, 0xff);
360                 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0x00eb, 0xd8);
361                 tm6000_set_reg(dev, TM6010_REQ07_RC0_ACTIVE_VIDEO_SOURCE, 0x40);
362                 tm6000_set_reg(dev, TM6010_REQ07_RC1_TRESHOLD, 0xd0);
363                 tm6000_set_reg(dev, TM6010_REQ07_RC3_HSTART1, 0x09);
364                 tm6000_set_reg(dev, TM6010_REQ07_RD8_IR_WAKEUP_SEL, 0x37);
365                 tm6000_set_reg(dev, TM6010_REQ07_RD1_ADDR_FOR_REQ1, 0xd8);
366                 tm6000_set_reg(dev, TM6010_REQ07_RD2_ADDR_FOR_REQ2, 0xc0);
367                 tm6000_set_reg(dev, TM6010_REQ07_RD6_ENDP_REQ1_REQ2, 0x60);
368
369                 tm6000_set_reg(dev, TM6010_REQ07_RE2_OUT_SEL2, 0x0c);
370                 tm6000_set_reg(dev, TM6010_REQ07_RE8_TYPESEL_MOS_I2S, 0xff);
371                 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0x00eb, 0x08);
372                 msleep(50);
373
374                 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 0x0020, 0x00);
375                 msleep(50);
376                 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 0x0020, 0x01);
377                 msleep(50);
378                 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 0x0020, 0x00);
379                 msleep(100);
380         }
381
382         /* switch dvb led on */
383         if (dev->gpio.dvb_led) {
384                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
385                         dev->gpio.dvb_led, 0x00);
386         }
387
388         return 0;
389 }
390 EXPORT_SYMBOL(tm6000_init_digital_mode);
391
392 struct reg_init {
393         u8 req;
394         u8 reg;
395         u8 val;
396 };
397
398 /* The meaning of those initializations are unknown */
399 struct reg_init tm6000_init_tab[] = {
400         /* REG  VALUE */
401         { TM6010_REQ07_RD8_IR_PULSE_CNT0, 0x1f },
402         { TM6010_REQ07_RFF_SOFT_RESET, 0x08 },
403         { TM6010_REQ07_RFF_SOFT_RESET, 0x00 },
404         { TM6010_REQ07_RD5_POWERSAVE, 0x4f },
405         { TM6010_REQ07_RD8_IR_WAKEUP_SEL, 0x23 },
406         { TM6010_REQ07_RD8_IR_WAKEUP_ADD, 0x08 },
407         { TM6010_REQ07_RE2_OUT_SEL2, 0x00 },
408         { TM6010_REQ07_RE3_OUT_SEL1, 0x10 },
409         { TM6010_REQ07_RE5_REMOTE_WAKEUP, 0x00 },
410         { TM6010_REQ07_RE8_TYPESEL_MOS_I2S, 0x00 },
411         { REQ_07_SET_GET_AVREG,  0xeb, 0x64 },          /* 48000 bits/sample, external input */
412         { REQ_07_SET_GET_AVREG,  0xee, 0xc2 },
413         { TM6010_REQ07_R3F_RESET, 0x01 },               /* Start of soft reset */
414         { TM6010_REQ07_R00_VIDEO_CONTROL0, 0x00 },
415         { TM6010_REQ07_R01_VIDEO_CONTROL1, 0x07 },
416         { TM6010_REQ07_R02_VIDEO_CONTROL2, 0x5f },
417         { TM6010_REQ07_R03_YC_SEP_CONTROL, 0x00 },
418         { TM6010_REQ07_R05_NOISE_THRESHOLD, 0x64 },
419         { TM6010_REQ07_R07_OUTPUT_CONTROL, 0x01 },
420         { TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, 0x82 },
421         { TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, 0x36 },
422         { TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, 0x50 },
423         { TM6010_REQ07_R0C_CHROMA_AGC_CONTROL, 0x6a },
424         { TM6010_REQ07_R11_AGC_PEAK_CONTROL, 0xc9 },
425         { TM6010_REQ07_R12_AGC_GATE_STARTH, 0x07 },
426         { TM6010_REQ07_R13_AGC_GATE_STARTL, 0x3b },
427         { TM6010_REQ07_R14_AGC_GATE_WIDTH, 0x47 },
428         { TM6010_REQ07_R15_AGC_BP_DELAY, 0x6f },
429         { TM6010_REQ07_R17_HLOOP_MAXSTATE, 0xcd },
430         { TM6010_REQ07_R18_CHROMA_DTO_INCREMENT3, 0x1e },
431         { TM6010_REQ07_R19_CHROMA_DTO_INCREMENT2, 0x8b },
432         { TM6010_REQ07_R1A_CHROMA_DTO_INCREMENT1, 0xa2 },
433         { TM6010_REQ07_R1B_CHROMA_DTO_INCREMENT0, 0xe9 },
434         { TM6010_REQ07_R1C_HSYNC_DTO_INCREMENT3, 0x1c },
435         { TM6010_REQ07_R1D_HSYNC_DTO_INCREMENT2, 0xcc },
436         { TM6010_REQ07_R1E_HSYNC_DTO_INCREMENT1, 0xcc },
437         { TM6010_REQ07_R1F_HSYNC_DTO_INCREMENT0, 0xcd },
438         { TM6010_REQ07_R20_HSYNC_RISING_EDGE_TIME, 0x3c },
439         { TM6010_REQ07_R21_HSYNC_PHASE_OFFSET, 0x3c },
440         { TM6010_REQ07_R2D_CHROMA_BURST_END, 0x48 },
441         { TM6010_REQ07_R2E_ACTIVE_VIDEO_HSTART, 0x88 },
442         { TM6010_REQ07_R30_ACTIVE_VIDEO_VSTART, 0x22 },
443         { TM6010_REQ07_R31_ACTIVE_VIDEO_VHIGHT, 0x61 },
444         { TM6010_REQ07_R32_VSYNC_HLOCK_MIN, 0x74 },
445         { TM6010_REQ07_R33_VSYNC_HLOCK_MAX, 0x1c },
446         { TM6010_REQ07_R34_VSYNC_AGC_MIN, 0x74 },
447         { TM6010_REQ07_R35_VSYNC_AGC_MAX, 0x1c },
448         { TM6010_REQ07_R36_VSYNC_VBI_MIN, 0x7a },
449         { TM6010_REQ07_R37_VSYNC_VBI_MAX, 0x26 },
450         { TM6010_REQ07_R38_VSYNC_THRESHOLD, 0x40 },
451         { TM6010_REQ07_R39_VSYNC_TIME_CONSTANT, 0x0a },
452         { TM6010_REQ07_R42_VBI_DATA_HIGH_LEVEL, 0x55 },
453         { TM6010_REQ07_R51_VBI_DATA_TYPE_LINE21, 0x11 },
454         { TM6010_REQ07_R55_VBI_LOOP_FILTER_GAIN, 0x01 },
455         { TM6010_REQ07_R57_VBI_LOOP_FILTER_P_GAIN, 0x02 },
456         { TM6010_REQ07_R58_VBI_CAPTION_DTO1, 0x35 },
457         { TM6010_REQ07_R59_VBI_CAPTION_DTO0, 0xa0 },
458         { TM6010_REQ07_R80_COMB_FILTER_TRESHOLD, 0x15 },
459         { TM6010_REQ07_R82_COMB_FILTER_CONFIG, 0x42 },
460         { TM6010_REQ07_RC1_TRESHOLD, 0xd0 },
461         { TM6010_REQ07_RC3_HSTART1, 0x88 },
462         { TM6010_REQ07_R3F_RESET, 0x00 },               /* End of the soft reset */
463         { TM6010_REQ05_R18_IMASK7, 0x00 },
464 };
465
466 struct reg_init tm6010_init_tab[] = {
467         { TM6010_REQ07_RC0_ACTIVE_VIDEO_SOURCE, 0x00 },
468         { TM6010_REQ07_RC4_HSTART0, 0xa0 },
469         { TM6010_REQ07_RC6_HEND0, 0x40 },
470         { TM6010_REQ07_RCA_VEND0, 0x31 },
471         { TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, 0xe1 },
472         { TM6010_REQ07_RE0_DVIDEO_SOURCE, 0x03 },
473         { TM6010_REQ07_RFE_POWER_DOWN, 0x7f },
474
475         { TM6010_REQ08_RE2_POWER_DOWN_CTRL1, 0xf0 },
476         { TM6010_REQ08_RE3_ADC_IN1_SEL, 0xf4 },
477         { TM6010_REQ08_RE4_ADC_IN2_SEL, 0xf8 },
478         { TM6010_REQ08_RE6_POWER_DOWN_CTRL2, 0x00 },
479         { TM6010_REQ08_REA_BUFF_DRV_CTRL, 0xf2 },
480         { TM6010_REQ08_REB_SIF_GAIN_CTRL, 0xf0 },
481         { TM6010_REQ08_REC_REVERSE_YC_CTRL, 0xc2 },
482         { TM6010_REQ08_RF0_DAUDIO_INPUT_CONFIG, 0x60 },
483         { TM6010_REQ08_RF1_AADC_POWER_DOWN, 0xfc },
484
485         { TM6010_REQ07_R3F_RESET, 0x01 },
486         { TM6010_REQ07_R00_VIDEO_CONTROL0, 0x00 },
487         { TM6010_REQ07_R01_VIDEO_CONTROL1, 0x07 },
488         { TM6010_REQ07_R02_VIDEO_CONTROL2, 0x5f },
489         { TM6010_REQ07_R03_YC_SEP_CONTROL, 0x00 },
490         { TM6010_REQ07_R05_NOISE_THRESHOLD, 0x64 },
491         { TM6010_REQ07_R07_OUTPUT_CONTROL, 0x01 },
492         { TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, 0x82 },
493         { TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, 0x36 },
494         { TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, 0x50 },
495         { TM6010_REQ07_R0C_CHROMA_AGC_CONTROL, 0x6a },
496         { TM6010_REQ07_R11_AGC_PEAK_CONTROL, 0xc9 },
497         { TM6010_REQ07_R12_AGC_GATE_STARTH, 0x07 },
498         { TM6010_REQ07_R13_AGC_GATE_STARTL, 0x3b },
499         { TM6010_REQ07_R14_AGC_GATE_WIDTH, 0x47 },
500         { TM6010_REQ07_R15_AGC_BP_DELAY, 0x6f },
501         { TM6010_REQ07_R17_HLOOP_MAXSTATE, 0xcd },
502         { TM6010_REQ07_R18_CHROMA_DTO_INCREMENT3, 0x1e },
503         { TM6010_REQ07_R19_CHROMA_DTO_INCREMENT2, 0x8b },
504         { TM6010_REQ07_R1A_CHROMA_DTO_INCREMENT1, 0xa2 },
505         { TM6010_REQ07_R1B_CHROMA_DTO_INCREMENT0, 0xe9 },
506         { TM6010_REQ07_R1C_HSYNC_DTO_INCREMENT3, 0x1c },
507         { TM6010_REQ07_R1D_HSYNC_DTO_INCREMENT2, 0xcc },
508         { TM6010_REQ07_R1E_HSYNC_DTO_INCREMENT1, 0xcc },
509         { TM6010_REQ07_R1F_HSYNC_DTO_INCREMENT0, 0xcd },
510         { TM6010_REQ07_R20_HSYNC_RISING_EDGE_TIME, 0x3c },
511         { TM6010_REQ07_R21_HSYNC_PHASE_OFFSET, 0x3c },
512         { TM6010_REQ07_R2D_CHROMA_BURST_END, 0x48 },
513         { TM6010_REQ07_R2E_ACTIVE_VIDEO_HSTART, 0x88 },
514         { TM6010_REQ07_R30_ACTIVE_VIDEO_VSTART, 0x22 },
515         { TM6010_REQ07_R31_ACTIVE_VIDEO_VHIGHT, 0x61 },
516         { TM6010_REQ07_R32_VSYNC_HLOCK_MIN, 0x74 },
517         { TM6010_REQ07_R33_VSYNC_HLOCK_MAX, 0x1c },
518         { TM6010_REQ07_R34_VSYNC_AGC_MIN, 0x74 },
519         { TM6010_REQ07_R35_VSYNC_AGC_MAX, 0x1c },
520         { TM6010_REQ07_R36_VSYNC_VBI_MIN, 0x7a },
521         { TM6010_REQ07_R37_VSYNC_VBI_MAX, 0x26 },
522         { TM6010_REQ07_R38_VSYNC_THRESHOLD, 0x40 },
523         { TM6010_REQ07_R39_VSYNC_TIME_CONSTANT, 0x0a },
524         { TM6010_REQ07_R42_VBI_DATA_HIGH_LEVEL, 0x55 },
525         { TM6010_REQ07_R51_VBI_DATA_TYPE_LINE21, 0x11 },
526         { TM6010_REQ07_R55_VBI_LOOP_FILTER_GAIN, 0x01 },
527         { TM6010_REQ07_R57_VBI_LOOP_FILTER_P_GAIN, 0x02 },
528         { TM6010_REQ07_R58_VBI_CAPTION_DTO1, 0x35 },
529         { TM6010_REQ07_R59_VBI_CAPTION_DTO0, 0xa0 },
530         { TM6010_REQ07_R80_COMB_FILTER_TRESHOLD, 0x15 },
531         { TM6010_REQ07_R82_COMB_FILTER_CONFIG, 0x42 },
532         { TM6010_REQ07_RC1_TRESHOLD, 0xd0 },
533         { TM6010_REQ07_RC3_HSTART1, 0x88 },
534         { TM6010_REQ07_R3F_RESET, 0x00 },
535
536         { TM6010_REQ05_R18_IMASK7, 0x00 },
537
538         { TM6010_REQ07_RD8_IR_LEADER1, 0xaa },
539         { TM6010_REQ07_RD8_IR_LEADER0, 0x30 },
540         { TM6010_REQ07_RD8_IR_PULSE_CNT1, 0x20 },
541         { TM6010_REQ07_RD8_IR_PULSE_CNT0, 0xd0 },
542         { REQ_04_EN_DISABLE_MCU_INT, 0x02, 0x00 },
543         { TM6010_REQ07_RD8_IR, 0x2f },
544
545         /* set remote wakeup key:any key wakeup */
546         { TM6010_REQ07_RE5_REMOTE_WAKEUP,  0xfe },
547         { TM6010_REQ07_RD8_IR_WAKEUP_SEL,  0xff },
548 };
549
550 int tm6000_init(struct tm6000_core *dev)
551 {
552         int board, rc = 0, i, size;
553         struct reg_init *tab;
554
555         if (dev->dev_type == TM6010) {
556                 tab = tm6010_init_tab;
557                 size = ARRAY_SIZE(tm6010_init_tab);
558         } else {
559                 tab = tm6000_init_tab;
560                 size = ARRAY_SIZE(tm6000_init_tab);
561         }
562
563         /* Load board's initialization table */
564         for (i = 0; i < size; i++) {
565                 rc = tm6000_set_reg(dev, tab[i].req, tab[i].reg, tab[i].val);
566                 if (rc < 0) {
567                         printk(KERN_ERR "Error %i while setting req %d, "
568                                         "reg %d to value %d\n", rc,
569                                         tab[i].req, tab[i].reg, tab[i].val);
570                         return rc;
571                 }
572         }
573
574         msleep(5); /* Just to be conservative */
575
576         /* Check board version - maybe 10Moons specific */
577         board = tm6000_get_reg32(dev, REQ_40_GET_VERSION, 0, 0);
578         if (board >= 0)
579                 printk(KERN_INFO "Board version = 0x%08x\n", board);
580         else
581                 printk(KERN_ERR "Error %i while retrieving board version\n", board);
582
583         rc = tm6000_cards_setup(dev);
584
585         return rc;
586 }
587
588 int tm6000_set_audio_bitrate(struct tm6000_core *dev, int bitrate)
589 {
590         int val;
591
592         if (dev->dev_type == TM6010) {
593                 val = tm6000_get_reg(dev, TM6010_REQ08_R0A_A_I2S_MOD, 0);
594                 if (val < 0)
595                         return val;
596                 val = (val & 0xf0) | 0x1; /* 48 kHz, not muted */
597                 val = tm6000_set_reg(dev, TM6010_REQ08_R0A_A_I2S_MOD, val);
598                 if (val < 0)
599                         return val;
600         }
601
602         val = tm6000_get_reg(dev, REQ_07_SET_GET_AVREG, 0xeb, 0x0);
603         if (val < 0)
604                 return val;
605
606         val &= 0x0f;            /* Preserve the audio input control bits */
607         switch (bitrate) {
608         case 44100:
609                 val |= 0xd0;
610                 dev->audio_bitrate = bitrate;
611                 break;
612         case 48000:
613                 val |= 0x60;
614                 dev->audio_bitrate = bitrate;
615                 break;
616         }
617         val = tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0xeb, val);
618
619         return val;
620 }
621 EXPORT_SYMBOL_GPL(tm6000_set_audio_bitrate);
622
623 static LIST_HEAD(tm6000_devlist);
624 static DEFINE_MUTEX(tm6000_devlist_mutex);
625
626 /*
627  * tm6000_realease_resource()
628  */
629
630 void tm6000_remove_from_devlist(struct tm6000_core *dev)
631 {
632         mutex_lock(&tm6000_devlist_mutex);
633         list_del(&dev->devlist);
634         mutex_unlock(&tm6000_devlist_mutex);
635 };
636
637 void tm6000_add_into_devlist(struct tm6000_core *dev)
638 {
639         mutex_lock(&tm6000_devlist_mutex);
640         list_add_tail(&dev->devlist, &tm6000_devlist);
641         mutex_unlock(&tm6000_devlist_mutex);
642 };
643
644 /*
645  * Extension interface
646  */
647
648 static LIST_HEAD(tm6000_extension_devlist);
649 static DEFINE_MUTEX(tm6000_extension_devlist_lock);
650
651 int tm6000_call_fillbuf(struct tm6000_core *dev, enum tm6000_ops_type type,
652                         char *buf, int size)
653 {
654         struct tm6000_ops *ops = NULL;
655
656         /* FIXME: tm6000_extension_devlist_lock should be a spinlock */
657
658         if (!list_empty(&tm6000_extension_devlist)) {
659                 list_for_each_entry(ops, &tm6000_extension_devlist, next) {
660                         if (ops->fillbuf && ops->type == type)
661                                 ops->fillbuf(dev, buf, size);
662                 }
663         }
664
665         return 0;
666 }
667
668 int tm6000_register_extension(struct tm6000_ops *ops)
669 {
670         struct tm6000_core *dev = NULL;
671
672         mutex_lock(&tm6000_devlist_mutex);
673         mutex_lock(&tm6000_extension_devlist_lock);
674         list_add_tail(&ops->next, &tm6000_extension_devlist);
675         list_for_each_entry(dev, &tm6000_devlist, devlist) {
676                 ops->init(dev);
677                 printk(KERN_INFO "%s: Initialized (%s) extension\n",
678                        dev->name, ops->name);
679         }
680         mutex_unlock(&tm6000_extension_devlist_lock);
681         mutex_unlock(&tm6000_devlist_mutex);
682         return 0;
683 }
684 EXPORT_SYMBOL(tm6000_register_extension);
685
686 void tm6000_unregister_extension(struct tm6000_ops *ops)
687 {
688         struct tm6000_core *dev = NULL;
689
690         mutex_lock(&tm6000_devlist_mutex);
691         list_for_each_entry(dev, &tm6000_devlist, devlist) {
692                 if (dev)
693                         ops->fini(dev);
694         }
695
696         mutex_lock(&tm6000_extension_devlist_lock);
697         printk(KERN_INFO "tm6000: Remove (%s) extension\n", ops->name);
698         list_del(&ops->next);
699         mutex_unlock(&tm6000_extension_devlist_lock);
700         mutex_unlock(&tm6000_devlist_mutex);
701 }
702 EXPORT_SYMBOL(tm6000_unregister_extension);
703
704 void tm6000_init_extension(struct tm6000_core *dev)
705 {
706         struct tm6000_ops *ops = NULL;
707
708         mutex_lock(&tm6000_extension_devlist_lock);
709         if (!list_empty(&tm6000_extension_devlist)) {
710                 list_for_each_entry(ops, &tm6000_extension_devlist, next) {
711                         if (ops->init)
712                                 ops->init(dev);
713                 }
714         }
715         mutex_unlock(&tm6000_extension_devlist_lock);
716 }
717
718 void tm6000_close_extension(struct tm6000_core *dev)
719 {
720         struct tm6000_ops *ops = NULL;
721
722         mutex_lock(&tm6000_extension_devlist_lock);
723         if (!list_empty(&tm6000_extension_devlist)) {
724                 list_for_each_entry(ops, &tm6000_extension_devlist, next) {
725                         if (ops->fini)
726                                 ops->fini(dev);
727                 }
728         }
729         mutex_unlock(&tm6000_extension_devlist_lock);
730 }