315ba6fa013403d747a701e553ba05b5cba8261f
[safe/jmp/linux-2.6] / drivers / media / dvb / ttpci / av7110_v4l.c
1 /*
2  * av7110_v4l.c: av7110 video4linux interface for DVB and Siemens DVB-C analog module
3  *
4  * Copyright (C) 1999-2002 Ralph  Metzler
5  *                       & Marcus Metzler for convergence integrated media GmbH
6  *
7  * originally based on code by:
8  * Copyright (C) 1998,1999 Christian Theiss <mistert@rz.fh-augsburg.de>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
24  *
25  * the project's page is at http://www.linuxtv.org/dvb/
26  */
27
28 #include <linux/kernel.h>
29 #include <linux/types.h>
30 #include <linux/delay.h>
31 #include <linux/fs.h>
32 #include <linux/timer.h>
33 #include <linux/poll.h>
34
35 #include "av7110.h"
36 #include "av7110_hw.h"
37 #include "av7110_av.h"
38
39 int msp_writereg(struct av7110 *av7110, u8 dev, u16 reg, u16 val)
40 {
41         u8 msg[5] = { dev, reg >> 8, reg & 0xff, val >> 8 , val & 0xff };
42         struct i2c_msg msgs = { .flags = 0, .len = 5, .buf = msg };
43
44         switch (av7110->adac_type) {
45         case DVB_ADAC_MSP34x0:
46                 msgs.addr = 0x40;
47                 break;
48         case DVB_ADAC_MSP34x5:
49                 msgs.addr = 0x42;
50                 break;
51         default:
52                 return 0;
53         }
54
55         if (i2c_transfer(&av7110->i2c_adap, &msgs, 1) != 1) {
56                 dprintk(1, "dvb-ttpci: failed @ card %d, %u = %u\n",
57                        av7110->dvb_adapter.num, reg, val);
58                 return -EIO;
59         }
60         return 0;
61 }
62
63 static int msp_readreg(struct av7110 *av7110, u8 dev, u16 reg, u16 *val)
64 {
65         u8 msg1[3] = { dev, reg >> 8, reg & 0xff };
66         u8 msg2[2];
67         struct i2c_msg msgs[2] = {
68                 { .flags = 0       , .len = 3, .buf = msg1 },
69                 { .flags = I2C_M_RD, .len = 2, .buf = msg2 }
70         };
71
72         switch (av7110->adac_type) {
73         case DVB_ADAC_MSP34x0:
74                 msgs[0].addr = 0x40;
75                 msgs[1].addr = 0x40;
76                 break;
77         case DVB_ADAC_MSP34x5:
78                 msgs[0].addr = 0x42;
79                 msgs[1].addr = 0x42;
80                 break;
81         default:
82                 return 0;
83         }
84
85         if (i2c_transfer(&av7110->i2c_adap, &msgs[0], 2) != 2) {
86                 dprintk(1, "dvb-ttpci: failed @ card %d, %u\n",
87                        av7110->dvb_adapter.num, reg);
88                 return -EIO;
89         }
90         *val = (msg2[0] << 8) | msg2[1];
91         return 0;
92 }
93
94 static struct v4l2_input inputs[4] = {
95         {
96                 .index          = 0,
97                 .name           = "DVB",
98                 .type           = V4L2_INPUT_TYPE_CAMERA,
99                 .audioset       = 1,
100                 .tuner          = 0, /* ignored */
101                 .std            = V4L2_STD_PAL_BG|V4L2_STD_NTSC_M,
102                 .status         = 0,
103         }, {
104                 .index          = 1,
105                 .name           = "Television",
106                 .type           = V4L2_INPUT_TYPE_TUNER,
107                 .audioset       = 2,
108                 .tuner          = 0,
109                 .std            = V4L2_STD_PAL_BG|V4L2_STD_NTSC_M,
110                 .status         = 0,
111         }, {
112                 .index          = 2,
113                 .name           = "Video",
114                 .type           = V4L2_INPUT_TYPE_CAMERA,
115                 .audioset       = 0,
116                 .tuner          = 0,
117                 .std            = V4L2_STD_PAL_BG|V4L2_STD_NTSC_M,
118                 .status         = 0,
119         }, {
120                 .index          = 3,
121                 .name           = "Y/C",
122                 .type           = V4L2_INPUT_TYPE_CAMERA,
123                 .audioset       = 0,
124                 .tuner          = 0,
125                 .std            = V4L2_STD_PAL_BG|V4L2_STD_NTSC_M,
126                 .status         = 0,
127         }
128 };
129
130 static int ves1820_writereg(struct saa7146_dev *dev, u8 addr, u8 reg, u8 data)
131 {
132         struct av7110 *av7110 = dev->ext_priv;
133         u8 buf[] = { 0x00, reg, data };
134         struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = buf, .len = 3 };
135
136         dprintk(4, "dev: %p\n", dev);
137
138         if (1 != i2c_transfer(&av7110->i2c_adap, &msg, 1))
139                 return -1;
140         return 0;
141 }
142
143 static int tuner_write(struct saa7146_dev *dev, u8 addr, u8 data [4])
144 {
145         struct av7110 *av7110 = dev->ext_priv;
146         struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = data, .len = 4 };
147
148         dprintk(4, "dev: %p\n", dev);
149
150         if (1 != i2c_transfer(&av7110->i2c_adap, &msg, 1))
151                 return -1;
152         return 0;
153 }
154
155 static int ves1820_set_tv_freq(struct saa7146_dev *dev, u32 freq)
156 {
157         u32 div;
158         u8 config;
159         u8 buf[4];
160
161         dprintk(4, "freq: 0x%08x\n", freq);
162
163         /* magic number: 614. tuning with the frequency given by v4l2
164            is always off by 614*62.5 = 38375 kHz...*/
165         div = freq + 614;
166
167         buf[0] = (div >> 8) & 0x7f;
168         buf[1] = div & 0xff;
169         buf[2] = 0x8e;
170
171         if (freq < (u32) (16 * 168.25))
172                 config = 0xa0;
173         else if (freq < (u32) (16 * 447.25))
174                 config = 0x90;
175         else
176                 config = 0x30;
177         config &= ~0x02;
178
179         buf[3] = config;
180
181         return tuner_write(dev, 0x61, buf);
182 }
183
184 static int stv0297_set_tv_freq(struct saa7146_dev *dev, u32 freq)
185 {
186         struct av7110 *av7110 = (struct av7110*)dev->ext_priv;
187         u32 div;
188         u8 data[4];
189
190         div = (freq + 38900000 + 31250) / 62500;
191
192         data[0] = (div >> 8) & 0x7f;
193         data[1] = div & 0xff;
194         data[2] = 0xce;
195
196         if (freq < 45000000)
197                 return -EINVAL;
198         else if (freq < 137000000)
199                 data[3] = 0x01;
200         else if (freq < 403000000)
201                 data[3] = 0x02;
202         else if (freq < 860000000)
203                 data[3] = 0x04;
204         else
205                 return -EINVAL;
206
207         if (av7110->fe->ops.i2c_gate_ctrl)
208                 av7110->fe->ops.i2c_gate_ctrl(av7110->fe, 1);
209         return tuner_write(dev, 0x63, data);
210 }
211
212
213
214 static struct saa7146_standard analog_standard[];
215 static struct saa7146_standard dvb_standard[];
216 static struct saa7146_standard standard[];
217
218 static struct v4l2_audio msp3400_v4l2_audio = {
219         .index = 0,
220         .name = "Television",
221         .capability = V4L2_AUDCAP_STEREO
222 };
223
224 static int av7110_dvb_c_switch(struct saa7146_fh *fh)
225 {
226         struct saa7146_dev *dev = fh->dev;
227         struct saa7146_vv *vv = dev->vv_data;
228         struct av7110 *av7110 = (struct av7110*)dev->ext_priv;
229         u16 adswitch;
230         int source, sync, err;
231
232         dprintk(4, "%p\n", av7110);
233
234         if ((vv->video_status & STATUS_OVERLAY) != 0) {
235                 vv->ov_suspend = vv->video_fh;
236                 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
237                 if (err != 0) {
238                         dprintk(2, "suspending video failed\n");
239                         vv->ov_suspend = NULL;
240                 }
241         }
242
243         if (0 != av7110->current_input) {
244                 dprintk(1, "switching to analog TV:\n");
245                 adswitch = 1;
246                 source = SAA7146_HPS_SOURCE_PORT_B;
247                 sync = SAA7146_HPS_SYNC_PORT_B;
248                 memcpy(standard, analog_standard, sizeof(struct saa7146_standard) * 2);
249
250                 switch (av7110->current_input) {
251                 case 1:
252                         dprintk(1, "switching SAA7113 to Analog Tuner Input.\n");
253                         msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0000); // loudspeaker source
254                         msp_writereg(av7110, MSP_WR_DSP, 0x0009, 0x0000); // headphone source
255                         msp_writereg(av7110, MSP_WR_DSP, 0x000a, 0x0000); // SCART 1 source
256                         msp_writereg(av7110, MSP_WR_DSP, 0x000e, 0x3000); // FM matrix, mono
257                         msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x4f00); // loudspeaker + headphone
258                         msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x4f00); // SCART 1 volume
259
260                         if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
261                                 if (ves1820_writereg(dev, 0x09, 0x0f, 0x60))
262                                         dprintk(1, "setting band in demodulator failed.\n");
263                         } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
264                                 saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTHI); // TDA9819 pin9(STD)
265                                 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTHI); // TDA9819 pin30(VIF)
266                         }
267                         if (i2c_writereg(av7110, 0x48, 0x02, 0xd0) != 1)
268                                 dprintk(1, "saa7113 write failed @ card %d", av7110->dvb_adapter.num);
269                         break;
270                 case 2:
271                         dprintk(1, "switching SAA7113 to Video AV CVBS Input.\n");
272                         if (i2c_writereg(av7110, 0x48, 0x02, 0xd2) != 1)
273                                 dprintk(1, "saa7113 write failed @ card %d", av7110->dvb_adapter.num);
274                         break;
275                 case 3:
276                         dprintk(1, "switching SAA7113 to Video AV Y/C Input.\n");
277                         if (i2c_writereg(av7110, 0x48, 0x02, 0xd9) != 1)
278                                 dprintk(1, "saa7113 write failed @ card %d", av7110->dvb_adapter.num);
279                         break;
280                 default:
281                         dprintk(1, "switching SAA7113 to Input: AV7110: SAA7113: invalid input.\n");
282                 }
283         } else {
284                 adswitch = 0;
285                 source = SAA7146_HPS_SOURCE_PORT_A;
286                 sync = SAA7146_HPS_SYNC_PORT_A;
287                 memcpy(standard, dvb_standard, sizeof(struct saa7146_standard) * 2);
288                 dprintk(1, "switching DVB mode\n");
289                 msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0220); // loudspeaker source
290                 msp_writereg(av7110, MSP_WR_DSP, 0x0009, 0x0220); // headphone source
291                 msp_writereg(av7110, MSP_WR_DSP, 0x000a, 0x0220); // SCART 1 source
292                 msp_writereg(av7110, MSP_WR_DSP, 0x000e, 0x3000); // FM matrix, mono
293                 msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x7f00); // loudspeaker + headphone
294                 msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x7f00); // SCART 1 volume
295
296                 if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
297                         if (ves1820_writereg(dev, 0x09, 0x0f, 0x20))
298                                 dprintk(1, "setting band in demodulator failed.\n");
299                 } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
300                         saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTLO); // TDA9819 pin9(STD)
301                         saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO); // TDA9819 pin30(VIF)
302                 }
303         }
304
305         /* hmm, this does not do anything!? */
306         if (av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, ADSwitch, 1, adswitch))
307                 dprintk(1, "ADSwitch error\n");
308
309         saa7146_set_hps_source_and_sync(dev, source, sync);
310
311         if (vv->ov_suspend != NULL) {
312                 saa7146_start_preview(vv->ov_suspend);
313                 vv->ov_suspend = NULL;
314         }
315
316         return 0;
317 }
318
319 static int av7110_ioctl(struct saa7146_fh *fh, unsigned int cmd, void *arg)
320 {
321         struct saa7146_dev *dev = fh->dev;
322         struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
323         dprintk(4, "saa7146_dev: %p\n", dev);
324
325         switch (cmd) {
326         case VIDIOC_G_TUNER:
327         {
328                 struct v4l2_tuner *t = arg;
329                 u16 stereo_det;
330                 s8 stereo;
331
332                 dprintk(2, "VIDIOC_G_TUNER: %d\n", t->index);
333
334                 if (!av7110->analog_tuner_flags || t->index != 0)
335                         return -EINVAL;
336
337                 memset(t, 0, sizeof(*t));
338                 strcpy((char *)t->name, "Television");
339
340                 t->type = V4L2_TUNER_ANALOG_TV;
341                 t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
342                         V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
343                 t->rangelow = 772;      /* 48.25 MHZ / 62.5 kHz = 772, see fi1216mk2-specs, page 2 */
344                 t->rangehigh = 13684;   /* 855.25 MHz / 62.5 kHz = 13684 */
345                 /* FIXME: add the real signal strength here */
346                 t->signal = 0xffff;
347                 t->afc = 0;
348
349                 // FIXME: standard / stereo detection is still broken
350                 msp_readreg(av7110, MSP_RD_DEM, 0x007e, &stereo_det);
351                 dprintk(1, "VIDIOC_G_TUNER: msp3400 TV standard detection: 0x%04x\n", stereo_det);
352                 msp_readreg(av7110, MSP_RD_DSP, 0x0018, &stereo_det);
353                 dprintk(1, "VIDIOC_G_TUNER: msp3400 stereo detection: 0x%04x\n", stereo_det);
354                 stereo = (s8)(stereo_det >> 8);
355                 if (stereo > 0x10) {
356                         /* stereo */
357                         t->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
358                         t->audmode = V4L2_TUNER_MODE_STEREO;
359                 }
360                 else if (stereo < -0x10) {
361                         /* bilingual */
362                         t->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
363                         t->audmode = V4L2_TUNER_MODE_LANG1;
364                 }
365                 else /* mono */
366                         t->rxsubchans = V4L2_TUNER_SUB_MONO;
367
368                 return 0;
369         }
370         case VIDIOC_S_TUNER:
371         {
372                 struct v4l2_tuner *t = arg;
373                 u16 fm_matrix, src;
374                 dprintk(2, "VIDIOC_S_TUNER: %d\n", t->index);
375
376                 if (!av7110->analog_tuner_flags || av7110->current_input != 1)
377                         return -EINVAL;
378
379                 switch (t->audmode) {
380                 case V4L2_TUNER_MODE_STEREO:
381                         dprintk(2, "VIDIOC_S_TUNER: V4L2_TUNER_MODE_STEREO\n");
382                         fm_matrix = 0x3001; // stereo
383                         src = 0x0020;
384                         break;
385                 case V4L2_TUNER_MODE_LANG1_LANG2:
386                         dprintk(2, "VIDIOC_S_TUNER: V4L2_TUNER_MODE_LANG1_LANG2\n");
387                         fm_matrix = 0x3000; // bilingual
388                         src = 0x0020;
389                         break;
390                 case V4L2_TUNER_MODE_LANG1:
391                         dprintk(2, "VIDIOC_S_TUNER: V4L2_TUNER_MODE_LANG1\n");
392                         fm_matrix = 0x3000; // mono
393                         src = 0x0000;
394                         break;
395                 case V4L2_TUNER_MODE_LANG2:
396                         dprintk(2, "VIDIOC_S_TUNER: V4L2_TUNER_MODE_LANG2\n");
397                         fm_matrix = 0x3000; // mono
398                         src = 0x0010;
399                         break;
400                 default: /* case V4L2_TUNER_MODE_MONO: */
401                         dprintk(2, "VIDIOC_S_TUNER: TDA9840_SET_MONO\n");
402                         fm_matrix = 0x3000; // mono
403                         src = 0x0030;
404                         break;
405                 }
406                 msp_writereg(av7110, MSP_WR_DSP, 0x000e, fm_matrix);
407                 msp_writereg(av7110, MSP_WR_DSP, 0x0008, src);
408                 msp_writereg(av7110, MSP_WR_DSP, 0x0009, src);
409                 msp_writereg(av7110, MSP_WR_DSP, 0x000a, src);
410                 return 0;
411         }
412         case VIDIOC_G_FREQUENCY:
413         {
414                 struct v4l2_frequency *f = arg;
415
416                 dprintk(2, "VIDIOC_G_FREQ: freq:0x%08x.\n", f->frequency);
417
418                 if (!av7110->analog_tuner_flags || av7110->current_input != 1)
419                         return -EINVAL;
420
421                 memset(f, 0, sizeof(*f));
422                 f->type = V4L2_TUNER_ANALOG_TV;
423                 f->frequency =  av7110->current_freq;
424                 return 0;
425         }
426         case VIDIOC_S_FREQUENCY:
427         {
428                 struct v4l2_frequency *f = arg;
429
430                 dprintk(2, "VIDIOC_S_FREQUENCY: freq:0x%08x.\n", f->frequency);
431
432                 if (!av7110->analog_tuner_flags || av7110->current_input != 1)
433                         return -EINVAL;
434
435                 if (V4L2_TUNER_ANALOG_TV != f->type)
436                         return -EINVAL;
437
438                 msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0xffe0); // fast mute
439                 msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0xffe0);
440
441                 /* tune in desired frequency */
442                 if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
443                         ves1820_set_tv_freq(dev, f->frequency);
444                 } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
445                         stv0297_set_tv_freq(dev, f->frequency);
446                 }
447                 av7110->current_freq = f->frequency;
448
449                 msp_writereg(av7110, MSP_WR_DSP, 0x0015, 0x003f); // start stereo detection
450                 msp_writereg(av7110, MSP_WR_DSP, 0x0015, 0x0000);
451                 msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x4f00); // loudspeaker + headphone
452                 msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x4f00); // SCART 1 volume
453                 return 0;
454         }
455         case VIDIOC_ENUMINPUT:
456         {
457                 struct v4l2_input *i = arg;
458
459                 dprintk(2, "VIDIOC_ENUMINPUT: %d\n", i->index);
460
461                 if (av7110->analog_tuner_flags) {
462                         if (i->index < 0 || i->index >= 4)
463                                 return -EINVAL;
464                 } else {
465                         if (i->index != 0)
466                                 return -EINVAL;
467                 }
468
469                 memcpy(i, &inputs[i->index], sizeof(struct v4l2_input));
470
471                 return 0;
472         }
473         case VIDIOC_G_INPUT:
474         {
475                 int *input = (int *)arg;
476                 *input = av7110->current_input;
477                 dprintk(2, "VIDIOC_G_INPUT: %d\n", *input);
478                 return 0;
479         }
480         case VIDIOC_S_INPUT:
481         {
482                 int input = *(int *)arg;
483
484                 dprintk(2, "VIDIOC_S_INPUT: %d\n", input);
485
486                 if (!av7110->analog_tuner_flags)
487                         return 0;
488
489                 if (input < 0 || input >= 4)
490                         return -EINVAL;
491
492                 av7110->current_input = input;
493                 return av7110_dvb_c_switch(fh);
494         }
495         case VIDIOC_G_AUDIO:
496         {
497                 struct v4l2_audio *a = arg;
498
499                 dprintk(2, "VIDIOC_G_AUDIO: %d\n", a->index);
500                 if (a->index != 0)
501                         return -EINVAL;
502                 memcpy(a, &msp3400_v4l2_audio, sizeof(struct v4l2_audio));
503                 break;
504         }
505         case VIDIOC_S_AUDIO:
506         {
507                 struct v4l2_audio *a = arg;
508                 dprintk(2, "VIDIOC_S_AUDIO: %d\n", a->index);
509                 break;
510         }
511         case VIDIOC_G_SLICED_VBI_CAP:
512         {
513                 struct v4l2_sliced_vbi_cap *cap = arg;
514                 dprintk(2, "VIDIOC_G_SLICED_VBI_CAP\n");
515                 memset(cap, 0, sizeof *cap);
516                 if (FW_VERSION(av7110->arm_app) >= 0x2623) {
517                         cap->service_set = V4L2_SLICED_WSS_625;
518                         cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
519                 }
520                 break;
521         }
522         case VIDIOC_G_FMT:
523         {
524                 struct v4l2_format *f = arg;
525                 dprintk(2, "VIDIOC_G_FMT:\n");
526                 if (f->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT ||
527                     FW_VERSION(av7110->arm_app) < 0x2623)
528                         return -EAGAIN; /* handled by core driver */
529                 memset(&f->fmt.sliced, 0, sizeof f->fmt.sliced);
530                 if (av7110->wssMode) {
531                         f->fmt.sliced.service_set = V4L2_SLICED_WSS_625;
532                         f->fmt.sliced.service_lines[0][23] = V4L2_SLICED_WSS_625;
533                         f->fmt.sliced.io_size = sizeof (struct v4l2_sliced_vbi_data);
534                 }
535                 break;
536         }
537         case VIDIOC_S_FMT:
538         {
539                 struct v4l2_format *f = arg;
540                 dprintk(2, "VIDIOC_S_FMT\n");
541                 if (f->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT ||
542                     FW_VERSION(av7110->arm_app) < 0x2623)
543                         return -EAGAIN; /* handled by core driver */
544                 if (f->fmt.sliced.service_set != V4L2_SLICED_WSS_625 &&
545                     f->fmt.sliced.service_lines[0][23] != V4L2_SLICED_WSS_625) {
546                         memset(&f->fmt.sliced, 0, sizeof f->fmt.sliced);
547                         /* WSS controlled by firmware */
548                         av7110->wssMode = 0;
549                         av7110->wssData = 0;
550                         return av7110_fw_cmd(av7110, COMTYPE_ENCODER,
551                                              SetWSSConfig, 1, 0);
552                 } else {
553                         memset(&f->fmt.sliced, 0, sizeof f->fmt.sliced);
554                         f->fmt.sliced.service_set = V4L2_SLICED_WSS_625;
555                         f->fmt.sliced.service_lines[0][23] = V4L2_SLICED_WSS_625;
556                         f->fmt.sliced.io_size = sizeof (struct v4l2_sliced_vbi_data);
557                         /* WSS controlled by userspace */
558                         av7110->wssMode = 1;
559                         av7110->wssData = 0;
560                 }
561                 break;
562         }
563         default:
564                 printk("no such ioctl\n");
565                 return -ENOIOCTLCMD;
566         }
567         return 0;
568 }
569
570 static int av7110_vbi_reset(struct file *file)
571 {
572         struct saa7146_fh *fh = file->private_data;
573         struct saa7146_dev *dev = fh->dev;
574         struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
575
576         dprintk(2, "%s\n", __func__);
577         av7110->wssMode = 0;
578         av7110->wssData = 0;
579         if (FW_VERSION(av7110->arm_app) < 0x2623)
580                 return 0;
581         else
582                 return av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 1, 0);
583 }
584
585 static ssize_t av7110_vbi_write(struct file *file, const char __user *data, size_t count, loff_t *ppos)
586 {
587         struct saa7146_fh *fh = file->private_data;
588         struct saa7146_dev *dev = fh->dev;
589         struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
590         struct v4l2_sliced_vbi_data d;
591         int rc;
592
593         dprintk(2, "%s\n", __func__);
594         if (FW_VERSION(av7110->arm_app) < 0x2623 || !av7110->wssMode || count != sizeof d)
595                 return -EINVAL;
596         if (copy_from_user(&d, data, count))
597                 return -EFAULT;
598         if ((d.id != 0 && d.id != V4L2_SLICED_WSS_625) || d.field != 0 || d.line != 23)
599                 return -EINVAL;
600         if (d.id)
601                 av7110->wssData = ((d.data[1] << 8) & 0x3f00) | d.data[0];
602         else
603                 av7110->wssData = 0x8000;
604         rc = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 2, 1, av7110->wssData);
605         return (rc < 0) ? rc : count;
606 }
607
608 /****************************************************************************
609  * INITIALIZATION
610  ****************************************************************************/
611
612 static struct saa7146_extension_ioctls ioctls[] = {
613         { VIDIOC_ENUMINPUT,     SAA7146_EXCLUSIVE },
614         { VIDIOC_G_INPUT,       SAA7146_EXCLUSIVE },
615         { VIDIOC_S_INPUT,       SAA7146_EXCLUSIVE },
616         { VIDIOC_G_FREQUENCY,   SAA7146_EXCLUSIVE },
617         { VIDIOC_S_FREQUENCY,   SAA7146_EXCLUSIVE },
618         { VIDIOC_G_TUNER,       SAA7146_EXCLUSIVE },
619         { VIDIOC_S_TUNER,       SAA7146_EXCLUSIVE },
620         { VIDIOC_G_AUDIO,       SAA7146_EXCLUSIVE },
621         { VIDIOC_S_AUDIO,       SAA7146_EXCLUSIVE },
622         { VIDIOC_G_SLICED_VBI_CAP, SAA7146_EXCLUSIVE },
623         { VIDIOC_G_FMT,         SAA7146_BEFORE },
624         { VIDIOC_S_FMT,         SAA7146_BEFORE },
625         { 0, 0 }
626 };
627
628 static u8 saa7113_init_regs[] = {
629         0x02, 0xd0,
630         0x03, 0x23,
631         0x04, 0x00,
632         0x05, 0x00,
633         0x06, 0xe9,
634         0x07, 0x0d,
635         0x08, 0x98,
636         0x09, 0x02,
637         0x0a, 0x80,
638         0x0b, 0x40,
639         0x0c, 0x40,
640         0x0d, 0x00,
641         0x0e, 0x01,
642         0x0f, 0x7c,
643         0x10, 0x48,
644         0x11, 0x0c,
645         0x12, 0x8b,
646         0x13, 0x1a,
647         0x14, 0x00,
648         0x15, 0x00,
649         0x16, 0x00,
650         0x17, 0x00,
651         0x18, 0x00,
652         0x19, 0x00,
653         0x1a, 0x00,
654         0x1b, 0x00,
655         0x1c, 0x00,
656         0x1d, 0x00,
657         0x1e, 0x00,
658
659         0x41, 0x77,
660         0x42, 0x77,
661         0x43, 0x77,
662         0x44, 0x77,
663         0x45, 0x77,
664         0x46, 0x77,
665         0x47, 0x77,
666         0x48, 0x77,
667         0x49, 0x77,
668         0x4a, 0x77,
669         0x4b, 0x77,
670         0x4c, 0x77,
671         0x4d, 0x77,
672         0x4e, 0x77,
673         0x4f, 0x77,
674         0x50, 0x77,
675         0x51, 0x77,
676         0x52, 0x77,
677         0x53, 0x77,
678         0x54, 0x77,
679         0x55, 0x77,
680         0x56, 0x77,
681         0x57, 0xff,
682
683         0xff
684 };
685
686
687 static struct saa7146_ext_vv av7110_vv_data_st;
688 static struct saa7146_ext_vv av7110_vv_data_c;
689
690 int av7110_init_analog_module(struct av7110 *av7110)
691 {
692         u16 version1, version2;
693
694         if (i2c_writereg(av7110, 0x80, 0x0, 0x80) == 1 &&
695             i2c_writereg(av7110, 0x80, 0x0, 0) == 1) {
696                 printk("dvb-ttpci: DVB-C analog module @ card %d detected, initializing MSP3400\n",
697                         av7110->dvb_adapter.num);
698                 av7110->adac_type = DVB_ADAC_MSP34x0;
699         } else if (i2c_writereg(av7110, 0x84, 0x0, 0x80) == 1 &&
700                    i2c_writereg(av7110, 0x84, 0x0, 0) == 1) {
701                 printk("dvb-ttpci: DVB-C analog module @ card %d detected, initializing MSP3415\n",
702                         av7110->dvb_adapter.num);
703                 av7110->adac_type = DVB_ADAC_MSP34x5;
704         } else
705                 return -ENODEV;
706
707         msleep(100); // the probing above resets the msp...
708         msp_readreg(av7110, MSP_RD_DSP, 0x001e, &version1);
709         msp_readreg(av7110, MSP_RD_DSP, 0x001f, &version2);
710         dprintk(1, "dvb-ttpci: @ card %d MSP34xx version 0x%04x 0x%04x\n",
711                 av7110->dvb_adapter.num, version1, version2);
712         msp_writereg(av7110, MSP_WR_DSP, 0x0013, 0x0c00);
713         msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x7f00); // loudspeaker + headphone
714         msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0220); // loudspeaker source
715         msp_writereg(av7110, MSP_WR_DSP, 0x0009, 0x0220); // headphone source
716         msp_writereg(av7110, MSP_WR_DSP, 0x0004, 0x7f00); // loudspeaker volume
717         msp_writereg(av7110, MSP_WR_DSP, 0x000a, 0x0220); // SCART 1 source
718         msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x7f00); // SCART 1 volume
719         msp_writereg(av7110, MSP_WR_DSP, 0x000d, 0x1900); // prescale SCART
720
721         if (i2c_writereg(av7110, 0x48, 0x01, 0x00)!=1) {
722                 INFO(("saa7113 not accessible.\n"));
723         } else {
724                 u8 *i = saa7113_init_regs;
725
726                 if ((av7110->dev->pci->subsystem_vendor == 0x110a) && (av7110->dev->pci->subsystem_device == 0x0000)) {
727                         /* Fujitsu/Siemens DVB-Cable */
728                         av7110->analog_tuner_flags |= ANALOG_TUNER_VES1820;
729                 } else if ((av7110->dev->pci->subsystem_vendor == 0x13c2) && (av7110->dev->pci->subsystem_device == 0x0002)) {
730                         /* Hauppauge/TT DVB-C premium */
731                         av7110->analog_tuner_flags |= ANALOG_TUNER_VES1820;
732                 } else if ((av7110->dev->pci->subsystem_vendor == 0x13c2) && (av7110->dev->pci->subsystem_device == 0x000A)) {
733                         /* Hauppauge/TT DVB-C premium */
734                         av7110->analog_tuner_flags |= ANALOG_TUNER_STV0297;
735                 }
736
737                 /* setup for DVB by default */
738                 if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
739                         if (ves1820_writereg(av7110->dev, 0x09, 0x0f, 0x20))
740                                 dprintk(1, "setting band in demodulator failed.\n");
741                 } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
742                         saa7146_setgpio(av7110->dev, 1, SAA7146_GPIO_OUTLO); // TDA9819 pin9(STD)
743                         saa7146_setgpio(av7110->dev, 3, SAA7146_GPIO_OUTLO); // TDA9819 pin30(VIF)
744                 }
745
746                 /* init the saa7113 */
747                 while (*i != 0xff) {
748                         if (i2c_writereg(av7110, 0x48, i[0], i[1]) != 1) {
749                                 dprintk(1, "saa7113 initialization failed @ card %d", av7110->dvb_adapter.num);
750                                 break;
751                         }
752                         i += 2;
753                 }
754                 /* setup msp for analog sound: B/G Dual-FM */
755                 msp_writereg(av7110, MSP_WR_DEM, 0x00bb, 0x02d0); // AD_CV
756                 msp_writereg(av7110, MSP_WR_DEM, 0x0001,  3); // FIR1
757                 msp_writereg(av7110, MSP_WR_DEM, 0x0001, 18); // FIR1
758                 msp_writereg(av7110, MSP_WR_DEM, 0x0001, 27); // FIR1
759                 msp_writereg(av7110, MSP_WR_DEM, 0x0001, 48); // FIR1
760                 msp_writereg(av7110, MSP_WR_DEM, 0x0001, 66); // FIR1
761                 msp_writereg(av7110, MSP_WR_DEM, 0x0001, 72); // FIR1
762                 msp_writereg(av7110, MSP_WR_DEM, 0x0005,  4); // FIR2
763                 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 64); // FIR2
764                 msp_writereg(av7110, MSP_WR_DEM, 0x0005,  0); // FIR2
765                 msp_writereg(av7110, MSP_WR_DEM, 0x0005,  3); // FIR2
766                 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 18); // FIR2
767                 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 27); // FIR2
768                 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 48); // FIR2
769                 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 66); // FIR2
770                 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 72); // FIR2
771                 msp_writereg(av7110, MSP_WR_DEM, 0x0083, 0xa000); // MODE_REG
772                 msp_writereg(av7110, MSP_WR_DEM, 0x0093, 0x00aa); // DCO1_LO 5.74MHz
773                 msp_writereg(av7110, MSP_WR_DEM, 0x009b, 0x04fc); // DCO1_HI
774                 msp_writereg(av7110, MSP_WR_DEM, 0x00a3, 0x038e); // DCO2_LO 5.5MHz
775                 msp_writereg(av7110, MSP_WR_DEM, 0x00ab, 0x04c6); // DCO2_HI
776                 msp_writereg(av7110, MSP_WR_DEM, 0x0056, 0); // LOAD_REG 1/2
777         }
778
779         memcpy(standard, dvb_standard, sizeof(struct saa7146_standard) * 2);
780         /* set dd1 stream a & b */
781         saa7146_write(av7110->dev, DD1_STREAM_B, 0x00000000);
782         saa7146_write(av7110->dev, DD1_INIT, 0x03000700);
783         saa7146_write(av7110->dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
784
785         return 0;
786 }
787
788 int av7110_init_v4l(struct av7110 *av7110)
789 {
790         struct saa7146_dev* dev = av7110->dev;
791         int ret;
792
793         /* special case DVB-C: these cards have an analog tuner
794            plus need some special handling, so we have separate
795            saa7146_ext_vv data for these... */
796         if (av7110->analog_tuner_flags)
797                 ret = saa7146_vv_init(dev, &av7110_vv_data_c);
798         else
799                 ret = saa7146_vv_init(dev, &av7110_vv_data_st);
800
801         if (ret) {
802                 ERR(("cannot init capture device. skipping.\n"));
803                 return -ENODEV;
804         }
805
806         if (saa7146_register_device(&av7110->v4l_dev, dev, "av7110", VFL_TYPE_GRABBER)) {
807                 ERR(("cannot register capture device. skipping.\n"));
808                 saa7146_vv_release(dev);
809                 return -ENODEV;
810         }
811         if (saa7146_register_device(&av7110->vbi_dev, dev, "av7110", VFL_TYPE_VBI))
812                 ERR(("cannot register vbi v4l2 device. skipping.\n"));
813         return 0;
814 }
815
816 int av7110_exit_v4l(struct av7110 *av7110)
817 {
818         struct saa7146_dev* dev = av7110->dev;
819
820         saa7146_unregister_device(&av7110->v4l_dev, av7110->dev);
821         saa7146_unregister_device(&av7110->vbi_dev, av7110->dev);
822
823         saa7146_vv_release(dev);
824
825         return 0;
826 }
827
828
829
830 /* FIXME: these values are experimental values that look better than the
831    values from the latest "official" driver -- at least for me... (MiHu) */
832 static struct saa7146_standard standard[] = {
833         {
834                 .name   = "PAL",        .id             = V4L2_STD_PAL_BG,
835                 .v_offset       = 0x15, .v_field        = 288,
836                 .h_offset       = 0x48, .h_pixels       = 708,
837                 .v_max_out      = 576,  .h_max_out      = 768,
838         }, {
839                 .name   = "NTSC",       .id             = V4L2_STD_NTSC,
840                 .v_offset       = 0x10, .v_field        = 244,
841                 .h_offset       = 0x40, .h_pixels       = 708,
842                 .v_max_out      = 480,  .h_max_out      = 640,
843         }
844 };
845
846 static struct saa7146_standard analog_standard[] = {
847         {
848                 .name   = "PAL",        .id             = V4L2_STD_PAL_BG,
849                 .v_offset       = 0x1b, .v_field        = 288,
850                 .h_offset       = 0x08, .h_pixels       = 708,
851                 .v_max_out      = 576,  .h_max_out      = 768,
852         }, {
853                 .name   = "NTSC",       .id             = V4L2_STD_NTSC,
854                 .v_offset       = 0x10, .v_field        = 244,
855                 .h_offset       = 0x40, .h_pixels       = 708,
856                 .v_max_out      = 480,  .h_max_out      = 640,
857         }
858 };
859
860 static struct saa7146_standard dvb_standard[] = {
861         {
862                 .name   = "PAL",        .id             = V4L2_STD_PAL_BG,
863                 .v_offset       = 0x14, .v_field        = 288,
864                 .h_offset       = 0x48, .h_pixels       = 708,
865                 .v_max_out      = 576,  .h_max_out      = 768,
866         }, {
867                 .name   = "NTSC",       .id             = V4L2_STD_NTSC,
868                 .v_offset       = 0x10, .v_field        = 244,
869                 .h_offset       = 0x40, .h_pixels       = 708,
870                 .v_max_out      = 480,  .h_max_out      = 640,
871         }
872 };
873
874 static int std_callback(struct saa7146_dev* dev, struct saa7146_standard *std)
875 {
876         struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
877
878         if (std->id & V4L2_STD_PAL) {
879                 av7110->vidmode = AV7110_VIDEO_MODE_PAL;
880                 av7110_set_vidmode(av7110, av7110->vidmode);
881         }
882         else if (std->id & V4L2_STD_NTSC) {
883                 av7110->vidmode = AV7110_VIDEO_MODE_NTSC;
884                 av7110_set_vidmode(av7110, av7110->vidmode);
885         }
886         else
887                 return -1;
888
889         return 0;
890 }
891
892
893 static struct saa7146_ext_vv av7110_vv_data_st = {
894         .inputs         = 1,
895         .audios         = 1,
896         .capabilities   = V4L2_CAP_SLICED_VBI_OUTPUT,
897         .flags          = 0,
898
899         .stds           = &standard[0],
900         .num_stds       = ARRAY_SIZE(standard),
901         .std_callback   = &std_callback,
902
903         .ioctls         = &ioctls[0],
904         .ioctl          = av7110_ioctl,
905
906         .vbi_fops.open  = av7110_vbi_reset,
907         .vbi_fops.release = av7110_vbi_reset,
908         .vbi_fops.write = av7110_vbi_write,
909 };
910
911 static struct saa7146_ext_vv av7110_vv_data_c = {
912         .inputs         = 1,
913         .audios         = 1,
914         .capabilities   = V4L2_CAP_TUNER | V4L2_CAP_SLICED_VBI_OUTPUT,
915         .flags          = SAA7146_USE_PORT_B_FOR_VBI,
916
917         .stds           = &standard[0],
918         .num_stds       = ARRAY_SIZE(standard),
919         .std_callback   = &std_callback,
920
921         .ioctls         = &ioctls[0],
922         .ioctl          = av7110_ioctl,
923
924         .vbi_fops.open  = av7110_vbi_reset,
925         .vbi_fops.release = av7110_vbi_reset,
926         .vbi_fops.write = av7110_vbi_write,
927 };
928