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