]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/tm6000/tm6000-core.c
[media] tm6000: don't use BKL at the driver
[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         f.frequency = dev->freq;
317         v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
318
319         msleep(100);
320         tm6000_set_standard(dev, &dev->norm);
321         tm6000_set_vbi(dev);
322         tm6000_set_audio_bitrate(dev, 48000);
323
324         /* switch dvb led off */
325         if (dev->gpio.dvb_led) {
326                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
327                         dev->gpio.dvb_led, 0x01);
328         }
329
330         return 0;
331 }
332
333 int tm6000_init_digital_mode(struct tm6000_core *dev)
334 {
335         if (dev->dev_type == TM6010) {
336                 int val;
337                 u8 buf[2];
338
339                 /* digital init */
340                 val = tm6000_get_reg(dev, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, 0);
341                 val &= ~0x60;
342                 tm6000_set_reg(dev, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, val);
343                 val = tm6000_get_reg(dev, TM6010_REQ07_RC0_ACTIVE_VIDEO_SOURCE, 0);
344                 val |= 0x40;
345                 tm6000_set_reg(dev, TM6010_REQ07_RC0_ACTIVE_VIDEO_SOURCE, val);
346                 tm6000_set_reg(dev, TM6010_REQ07_RFE_POWER_DOWN, 0x28);
347                 tm6000_set_reg(dev, TM6010_REQ08_RE2_POWER_DOWN_CTRL1, 0xfc);
348                 tm6000_set_reg(dev, TM6010_REQ08_RE6_POWER_DOWN_CTRL2, 0xff);
349                 tm6000_read_write_usb(dev, 0xc0, 0x0e, 0x00c2, 0x0008, buf, 2);
350                 printk(KERN_INFO"buf %#x %#x\n", buf[0], buf[1]);
351         } else  {
352                 tm6000_set_reg(dev, TM6010_REQ07_RFF_SOFT_RESET, 0x08);
353                 tm6000_set_reg(dev, TM6010_REQ07_RFF_SOFT_RESET, 0x00);
354                 tm6000_set_reg(dev, TM6010_REQ07_R3F_RESET, 0x01);
355                 tm6000_set_reg(dev, TM6010_REQ07_RD8_IR_PULSE_CNT0, 0x08);
356                 tm6000_set_reg(dev, TM6010_REQ07_RE2_OUT_SEL2, 0x0c);
357                 tm6000_set_reg(dev, TM6010_REQ07_RE8_TYPESEL_MOS_I2S, 0xff);
358                 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0x00eb, 0xd8);
359                 tm6000_set_reg(dev, TM6010_REQ07_RC0_ACTIVE_VIDEO_SOURCE, 0x40);
360                 tm6000_set_reg(dev, TM6010_REQ07_RC1_TRESHOLD, 0xd0);
361                 tm6000_set_reg(dev, TM6010_REQ07_RC3_HSTART1, 0x09);
362                 tm6000_set_reg(dev, TM6010_REQ07_RD8_IR_WAKEUP_SEL, 0x37);
363                 tm6000_set_reg(dev, TM6010_REQ07_RD1_ADDR_FOR_REQ1, 0xd8);
364                 tm6000_set_reg(dev, TM6010_REQ07_RD2_ADDR_FOR_REQ2, 0xc0);
365                 tm6000_set_reg(dev, TM6010_REQ07_RD6_ENDP_REQ1_REQ2, 0x60);
366
367                 tm6000_set_reg(dev, TM6010_REQ07_RE2_OUT_SEL2, 0x0c);
368                 tm6000_set_reg(dev, TM6010_REQ07_RE8_TYPESEL_MOS_I2S, 0xff);
369                 tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0x00eb, 0x08);
370                 msleep(50);
371
372                 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 0x0020, 0x00);
373                 msleep(50);
374                 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 0x0020, 0x01);
375                 msleep(50);
376                 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 0x0020, 0x00);
377                 msleep(100);
378         }
379
380         /* switch dvb led on */
381         if (dev->gpio.dvb_led) {
382                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
383                         dev->gpio.dvb_led, 0x00);
384         }
385
386         return 0;
387 }
388 EXPORT_SYMBOL(tm6000_init_digital_mode);
389
390 struct reg_init {
391         u8 req;
392         u8 reg;
393         u8 val;
394 };
395
396 /* The meaning of those initializations are unknown */
397 struct reg_init tm6000_init_tab[] = {
398         /* REG  VALUE */
399         { TM6010_REQ07_RD8_IR_PULSE_CNT0, 0x1f },
400         { TM6010_REQ07_RFF_SOFT_RESET, 0x08 },
401         { TM6010_REQ07_RFF_SOFT_RESET, 0x00 },
402         { TM6010_REQ07_RD5_POWERSAVE, 0x4f },
403         { TM6010_REQ07_RD8_IR_WAKEUP_SEL, 0x23 },
404         { TM6010_REQ07_RD8_IR_WAKEUP_ADD, 0x08 },
405         { TM6010_REQ07_RE2_OUT_SEL2, 0x00 },
406         { TM6010_REQ07_RE3_OUT_SEL1, 0x10 },
407         { TM6010_REQ07_RE5_REMOTE_WAKEUP, 0x00 },
408         { TM6010_REQ07_RE8_TYPESEL_MOS_I2S, 0x00 },
409         { REQ_07_SET_GET_AVREG,  0xeb, 0x64 },          /* 48000 bits/sample, external input */
410         { REQ_07_SET_GET_AVREG,  0xee, 0xc2 },
411         { TM6010_REQ07_R3F_RESET, 0x01 },               /* Start of soft reset */
412         { TM6010_REQ07_R00_VIDEO_CONTROL0, 0x00 },
413         { TM6010_REQ07_R01_VIDEO_CONTROL1, 0x07 },
414         { TM6010_REQ07_R02_VIDEO_CONTROL2, 0x5f },
415         { TM6010_REQ07_R03_YC_SEP_CONTROL, 0x00 },
416         { TM6010_REQ07_R05_NOISE_THRESHOLD, 0x64 },
417         { TM6010_REQ07_R07_OUTPUT_CONTROL, 0x01 },
418         { TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, 0x82 },
419         { TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, 0x36 },
420         { TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, 0x50 },
421         { TM6010_REQ07_R0C_CHROMA_AGC_CONTROL, 0x6a },
422         { TM6010_REQ07_R11_AGC_PEAK_CONTROL, 0xc9 },
423         { TM6010_REQ07_R12_AGC_GATE_STARTH, 0x07 },
424         { TM6010_REQ07_R13_AGC_GATE_STARTL, 0x3b },
425         { TM6010_REQ07_R14_AGC_GATE_WIDTH, 0x47 },
426         { TM6010_REQ07_R15_AGC_BP_DELAY, 0x6f },
427         { TM6010_REQ07_R17_HLOOP_MAXSTATE, 0xcd },
428         { TM6010_REQ07_R18_CHROMA_DTO_INCREMENT3, 0x1e },
429         { TM6010_REQ07_R19_CHROMA_DTO_INCREMENT2, 0x8b },
430         { TM6010_REQ07_R1A_CHROMA_DTO_INCREMENT1, 0xa2 },
431         { TM6010_REQ07_R1B_CHROMA_DTO_INCREMENT0, 0xe9 },
432         { TM6010_REQ07_R1C_HSYNC_DTO_INCREMENT3, 0x1c },
433         { TM6010_REQ07_R1D_HSYNC_DTO_INCREMENT2, 0xcc },
434         { TM6010_REQ07_R1E_HSYNC_DTO_INCREMENT1, 0xcc },
435         { TM6010_REQ07_R1F_HSYNC_DTO_INCREMENT0, 0xcd },
436         { TM6010_REQ07_R20_HSYNC_RISING_EDGE_TIME, 0x3c },
437         { TM6010_REQ07_R21_HSYNC_PHASE_OFFSET, 0x3c },
438         { TM6010_REQ07_R2D_CHROMA_BURST_END, 0x48 },
439         { TM6010_REQ07_R2E_ACTIVE_VIDEO_HSTART, 0x88 },
440         { TM6010_REQ07_R30_ACTIVE_VIDEO_VSTART, 0x22 },
441         { TM6010_REQ07_R31_ACTIVE_VIDEO_VHIGHT, 0x61 },
442         { TM6010_REQ07_R32_VSYNC_HLOCK_MIN, 0x74 },
443         { TM6010_REQ07_R33_VSYNC_HLOCK_MAX, 0x1c },
444         { TM6010_REQ07_R34_VSYNC_AGC_MIN, 0x74 },
445         { TM6010_REQ07_R35_VSYNC_AGC_MAX, 0x1c },
446         { TM6010_REQ07_R36_VSYNC_VBI_MIN, 0x7a },
447         { TM6010_REQ07_R37_VSYNC_VBI_MAX, 0x26 },
448         { TM6010_REQ07_R38_VSYNC_THRESHOLD, 0x40 },
449         { TM6010_REQ07_R39_VSYNC_TIME_CONSTANT, 0x0a },
450         { TM6010_REQ07_R42_VBI_DATA_HIGH_LEVEL, 0x55 },
451         { TM6010_REQ07_R51_VBI_DATA_TYPE_LINE21, 0x11 },
452         { TM6010_REQ07_R55_VBI_LOOP_FILTER_GAIN, 0x01 },
453         { TM6010_REQ07_R57_VBI_LOOP_FILTER_P_GAIN, 0x02 },
454         { TM6010_REQ07_R58_VBI_CAPTION_DTO1, 0x35 },
455         { TM6010_REQ07_R59_VBI_CAPTION_DTO0, 0xa0 },
456         { TM6010_REQ07_R80_COMB_FILTER_TRESHOLD, 0x15 },
457         { TM6010_REQ07_R82_COMB_FILTER_CONFIG, 0x42 },
458         { TM6010_REQ07_RC1_TRESHOLD, 0xd0 },
459         { TM6010_REQ07_RC3_HSTART1, 0x88 },
460         { TM6010_REQ07_R3F_RESET, 0x00 },               /* End of the soft reset */
461         { TM6010_REQ05_R18_IMASK7, 0x00 },
462 };
463
464 struct reg_init tm6010_init_tab[] = {
465         { TM6010_REQ07_RC0_ACTIVE_VIDEO_SOURCE, 0x00 },
466         { TM6010_REQ07_RC4_HSTART0, 0xa0 },
467         { TM6010_REQ07_RC6_HEND0, 0x40 },
468         { TM6010_REQ07_RCA_VEND0, 0x31 },
469         { TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, 0xe1 },
470         { TM6010_REQ07_RE0_DVIDEO_SOURCE, 0x03 },
471         { TM6010_REQ07_RFE_POWER_DOWN, 0x7f },
472
473         { TM6010_REQ08_RE2_POWER_DOWN_CTRL1, 0xf0 },
474         { TM6010_REQ08_RE3_ADC_IN1_SEL, 0xf4 },
475         { TM6010_REQ08_RE4_ADC_IN2_SEL, 0xf8 },
476         { TM6010_REQ08_RE6_POWER_DOWN_CTRL2, 0x00 },
477         { TM6010_REQ08_REA_BUFF_DRV_CTRL, 0xf2 },
478         { TM6010_REQ08_REB_SIF_GAIN_CTRL, 0xf0 },
479         { TM6010_REQ08_REC_REVERSE_YC_CTRL, 0xc2 },
480         { TM6010_REQ08_RF0_DAUDIO_INPUT_CONFIG, 0x60 },
481         { TM6010_REQ08_RF1_AADC_POWER_DOWN, 0xfc },
482
483         { TM6010_REQ07_R3F_RESET, 0x01 },
484         { TM6010_REQ07_R00_VIDEO_CONTROL0, 0x00 },
485         { TM6010_REQ07_R01_VIDEO_CONTROL1, 0x07 },
486         { TM6010_REQ07_R02_VIDEO_CONTROL2, 0x5f },
487         { TM6010_REQ07_R03_YC_SEP_CONTROL, 0x00 },
488         { TM6010_REQ07_R05_NOISE_THRESHOLD, 0x64 },
489         { TM6010_REQ07_R07_OUTPUT_CONTROL, 0x01 },
490         { TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, 0x82 },
491         { TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, 0x36 },
492         { TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, 0x50 },
493         { TM6010_REQ07_R0C_CHROMA_AGC_CONTROL, 0x6a },
494         { TM6010_REQ07_R11_AGC_PEAK_CONTROL, 0xc9 },
495         { TM6010_REQ07_R12_AGC_GATE_STARTH, 0x07 },
496         { TM6010_REQ07_R13_AGC_GATE_STARTL, 0x3b },
497         { TM6010_REQ07_R14_AGC_GATE_WIDTH, 0x47 },
498         { TM6010_REQ07_R15_AGC_BP_DELAY, 0x6f },
499         { TM6010_REQ07_R17_HLOOP_MAXSTATE, 0xcd },
500         { TM6010_REQ07_R18_CHROMA_DTO_INCREMENT3, 0x1e },
501         { TM6010_REQ07_R19_CHROMA_DTO_INCREMENT2, 0x8b },
502         { TM6010_REQ07_R1A_CHROMA_DTO_INCREMENT1, 0xa2 },
503         { TM6010_REQ07_R1B_CHROMA_DTO_INCREMENT0, 0xe9 },
504         { TM6010_REQ07_R1C_HSYNC_DTO_INCREMENT3, 0x1c },
505         { TM6010_REQ07_R1D_HSYNC_DTO_INCREMENT2, 0xcc },
506         { TM6010_REQ07_R1E_HSYNC_DTO_INCREMENT1, 0xcc },
507         { TM6010_REQ07_R1F_HSYNC_DTO_INCREMENT0, 0xcd },
508         { TM6010_REQ07_R20_HSYNC_RISING_EDGE_TIME, 0x3c },
509         { TM6010_REQ07_R21_HSYNC_PHASE_OFFSET, 0x3c },
510         { TM6010_REQ07_R2D_CHROMA_BURST_END, 0x48 },
511         { TM6010_REQ07_R2E_ACTIVE_VIDEO_HSTART, 0x88 },
512         { TM6010_REQ07_R30_ACTIVE_VIDEO_VSTART, 0x22 },
513         { TM6010_REQ07_R31_ACTIVE_VIDEO_VHIGHT, 0x61 },
514         { TM6010_REQ07_R32_VSYNC_HLOCK_MIN, 0x74 },
515         { TM6010_REQ07_R33_VSYNC_HLOCK_MAX, 0x1c },
516         { TM6010_REQ07_R34_VSYNC_AGC_MIN, 0x74 },
517         { TM6010_REQ07_R35_VSYNC_AGC_MAX, 0x1c },
518         { TM6010_REQ07_R36_VSYNC_VBI_MIN, 0x7a },
519         { TM6010_REQ07_R37_VSYNC_VBI_MAX, 0x26 },
520         { TM6010_REQ07_R38_VSYNC_THRESHOLD, 0x40 },
521         { TM6010_REQ07_R39_VSYNC_TIME_CONSTANT, 0x0a },
522         { TM6010_REQ07_R42_VBI_DATA_HIGH_LEVEL, 0x55 },
523         { TM6010_REQ07_R51_VBI_DATA_TYPE_LINE21, 0x11 },
524         { TM6010_REQ07_R55_VBI_LOOP_FILTER_GAIN, 0x01 },
525         { TM6010_REQ07_R57_VBI_LOOP_FILTER_P_GAIN, 0x02 },
526         { TM6010_REQ07_R58_VBI_CAPTION_DTO1, 0x35 },
527         { TM6010_REQ07_R59_VBI_CAPTION_DTO0, 0xa0 },
528         { TM6010_REQ07_R80_COMB_FILTER_TRESHOLD, 0x15 },
529         { TM6010_REQ07_R82_COMB_FILTER_CONFIG, 0x42 },
530         { TM6010_REQ07_RC1_TRESHOLD, 0xd0 },
531         { TM6010_REQ07_RC3_HSTART1, 0x88 },
532         { TM6010_REQ07_R3F_RESET, 0x00 },
533
534         { TM6010_REQ05_R18_IMASK7, 0x00 },
535
536         { TM6010_REQ07_RD8_IR_LEADER1, 0xaa },
537         { TM6010_REQ07_RD8_IR_LEADER0, 0x30 },
538         { TM6010_REQ07_RD8_IR_PULSE_CNT1, 0x20 },
539         { TM6010_REQ07_RD8_IR_PULSE_CNT0, 0xd0 },
540         { REQ_04_EN_DISABLE_MCU_INT, 0x02, 0x00 },
541         { TM6010_REQ07_RD8_IR, 0x2f },
542
543         /* set remote wakeup key:any key wakeup */
544         { TM6010_REQ07_RE5_REMOTE_WAKEUP,  0xfe },
545         { TM6010_REQ07_RD8_IR_WAKEUP_SEL,  0xff },
546 };
547
548 int tm6000_init(struct tm6000_core *dev)
549 {
550         int board, rc = 0, i, size;
551         struct reg_init *tab;
552
553         if (dev->dev_type == TM6010) {
554                 tab = tm6010_init_tab;
555                 size = ARRAY_SIZE(tm6010_init_tab);
556         } else {
557                 tab = tm6000_init_tab;
558                 size = ARRAY_SIZE(tm6000_init_tab);
559         }
560
561         /* Load board's initialization table */
562         for (i = 0; i < size; i++) {
563                 rc = tm6000_set_reg(dev, tab[i].req, tab[i].reg, tab[i].val);
564                 if (rc < 0) {
565                         printk(KERN_ERR "Error %i while setting req %d, "
566                                         "reg %d to value %d\n", rc,
567                                         tab[i].req, tab[i].reg, tab[i].val);
568                         return rc;
569                 }
570         }
571
572         msleep(5); /* Just to be conservative */
573
574         /* Check board version - maybe 10Moons specific */
575         board = tm6000_get_reg32(dev, REQ_40_GET_VERSION, 0, 0);
576         if (board >= 0)
577                 printk(KERN_INFO "Board version = 0x%08x\n", board);
578         else
579                 printk(KERN_ERR "Error %i while retrieving board version\n", board);
580
581         rc = tm6000_cards_setup(dev);
582
583         return rc;
584 }
585
586 int tm6000_set_audio_bitrate(struct tm6000_core *dev, int bitrate)
587 {
588         int val;
589
590         if (dev->dev_type == TM6010) {
591                 val = tm6000_get_reg(dev, TM6010_REQ08_R0A_A_I2S_MOD, 0);
592                 if (val < 0)
593                         return val;
594                 val = (val & 0xf0) | 0x1; /* 48 kHz, not muted */
595                 val = tm6000_set_reg(dev, TM6010_REQ08_R0A_A_I2S_MOD, val);
596                 if (val < 0)
597                         return val;
598         }
599
600         val = tm6000_get_reg(dev, REQ_07_SET_GET_AVREG, 0xeb, 0x0);
601         if (val < 0)
602                 return val;
603
604         val &= 0x0f;            /* Preserve the audio input control bits */
605         switch (bitrate) {
606         case 44100:
607                 val |= 0xd0;
608                 dev->audio_bitrate = bitrate;
609                 break;
610         case 48000:
611                 val |= 0x60;
612                 dev->audio_bitrate = bitrate;
613                 break;
614         }
615         val = tm6000_set_reg(dev, REQ_07_SET_GET_AVREG, 0xeb, val);
616
617         return val;
618 }
619 EXPORT_SYMBOL_GPL(tm6000_set_audio_bitrate);
620
621 static LIST_HEAD(tm6000_devlist);
622 static DEFINE_MUTEX(tm6000_devlist_mutex);
623
624 /*
625  * tm6000_realease_resource()
626  */
627
628 void tm6000_remove_from_devlist(struct tm6000_core *dev)
629 {
630         mutex_lock(&tm6000_devlist_mutex);
631         list_del(&dev->devlist);
632         mutex_unlock(&tm6000_devlist_mutex);
633 };
634
635 void tm6000_add_into_devlist(struct tm6000_core *dev)
636 {
637         mutex_lock(&tm6000_devlist_mutex);
638         list_add_tail(&dev->devlist, &tm6000_devlist);
639         mutex_unlock(&tm6000_devlist_mutex);
640 };
641
642 /*
643  * Extension interface
644  */
645
646 static LIST_HEAD(tm6000_extension_devlist);
647 static DEFINE_MUTEX(tm6000_extension_devlist_lock);
648
649 int tm6000_call_fillbuf(struct tm6000_core *dev, enum tm6000_ops_type type,
650                         char *buf, int size)
651 {
652         struct tm6000_ops *ops = NULL;
653
654         /* FIXME: tm6000_extension_devlist_lock should be a spinlock */
655
656         if (!list_empty(&tm6000_extension_devlist)) {
657                 list_for_each_entry(ops, &tm6000_extension_devlist, next) {
658                         if (ops->fillbuf && ops->type == type)
659                                 ops->fillbuf(dev, buf, size);
660                 }
661         }
662
663         return 0;
664 }
665
666 int tm6000_register_extension(struct tm6000_ops *ops)
667 {
668         struct tm6000_core *dev = NULL;
669
670         mutex_lock(&tm6000_devlist_mutex);
671         mutex_lock(&tm6000_extension_devlist_lock);
672         list_add_tail(&ops->next, &tm6000_extension_devlist);
673         list_for_each_entry(dev, &tm6000_devlist, devlist) {
674                 ops->init(dev);
675                 printk(KERN_INFO "%s: Initialized (%s) extension\n",
676                        dev->name, ops->name);
677         }
678         mutex_unlock(&tm6000_extension_devlist_lock);
679         mutex_unlock(&tm6000_devlist_mutex);
680         return 0;
681 }
682 EXPORT_SYMBOL(tm6000_register_extension);
683
684 void tm6000_unregister_extension(struct tm6000_ops *ops)
685 {
686         struct tm6000_core *dev = NULL;
687
688         mutex_lock(&tm6000_devlist_mutex);
689         list_for_each_entry(dev, &tm6000_devlist, devlist) {
690                 if (dev)
691                         ops->fini(dev);
692         }
693
694         mutex_lock(&tm6000_extension_devlist_lock);
695         printk(KERN_INFO "tm6000: Remove (%s) extension\n", ops->name);
696         list_del(&ops->next);
697         mutex_unlock(&tm6000_extension_devlist_lock);
698         mutex_unlock(&tm6000_devlist_mutex);
699 }
700 EXPORT_SYMBOL(tm6000_unregister_extension);
701
702 void tm6000_init_extension(struct tm6000_core *dev)
703 {
704         struct tm6000_ops *ops = NULL;
705
706         mutex_lock(&tm6000_extension_devlist_lock);
707         if (!list_empty(&tm6000_extension_devlist)) {
708                 list_for_each_entry(ops, &tm6000_extension_devlist, next) {
709                         if (ops->init)
710                                 ops->init(dev);
711                 }
712         }
713         mutex_unlock(&tm6000_extension_devlist_lock);
714 }
715
716 void tm6000_close_extension(struct tm6000_core *dev)
717 {
718         struct tm6000_ops *ops = NULL;
719
720         mutex_lock(&tm6000_extension_devlist_lock);
721         if (!list_empty(&tm6000_extension_devlist)) {
722                 list_for_each_entry(ops, &tm6000_extension_devlist, next) {
723                         if (ops->fini)
724                                 ops->fini(dev);
725                 }
726         }
727         mutex_unlock(&tm6000_extension_devlist_lock);
728 }