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