2  * av7110_v4l.c: av7110 video4linux interface for DVB and Siemens DVB-C analog module
 
   4  * Copyright (C) 1999-2002 Ralph  Metzler
 
   5  *                       & Marcus Metzler for convergence integrated media GmbH
 
   7  * originally based on code by:
 
   8  * Copyright (C) 1998,1999 Christian Theiss <mistert@rz.fh-augsburg.de>
 
  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.
 
  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.
 
  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
 
  25  * the project's page is at http://www.linuxtv.org/dvb/
 
  28 #include <linux/kernel.h>
 
  29 #include <linux/sched.h>
 
  30 #include <linux/types.h>
 
  31 #include <linux/delay.h>
 
  33 #include <linux/timer.h>
 
  34 #include <linux/poll.h>
 
  35 #include <linux/byteorder/swabb.h>
 
  36 #include <linux/smp_lock.h>
 
  39 #include "av7110_hw.h"
 
  40 #include "av7110_av.h"
 
  42 int msp_writereg(struct av7110 *av7110, u8 dev, u16 reg, u16 val)
 
  44         u8 msg[5] = { dev, reg >> 8, reg & 0xff, val >> 8 , val & 0xff };
 
  45         struct i2c_msg msgs = { .flags = 0, .addr = 0x40, .len = 5, .buf = msg };
 
  47         if (i2c_transfer(&av7110->i2c_adap, &msgs, 1) != 1) {
 
  48                 dprintk(1, "dvb-ttpci: failed @ card %d, %u = %u\n",
 
  49                        av7110->dvb_adapter.num, reg, val);
 
  55 static int msp_readreg(struct av7110 *av7110, u8 dev, u16 reg, u16 *val)
 
  57         u8 msg1[3] = { dev, reg >> 8, reg & 0xff };
 
  59         struct i2c_msg msgs[2] = {
 
  60                 { .flags = 0,        .addr = 0x40, .len = 3, .buf = msg1 },
 
  61                 { .flags = I2C_M_RD, .addr = 0x40, .len = 2, .buf = msg2 }
 
  64         if (i2c_transfer(&av7110->i2c_adap, &msgs[0], 2) != 2) {
 
  65                 dprintk(1, "dvb-ttpci: failed @ card %d, %u\n",
 
  66                        av7110->dvb_adapter.num, reg);
 
  69         *val = (msg2[0] << 8) | msg2[1];
 
  73 static struct v4l2_input inputs[4] = {
 
  77                 .type           = V4L2_INPUT_TYPE_CAMERA,
 
  79                 .tuner          = 0, /* ignored */
 
  80                 .std            = V4L2_STD_PAL_BG|V4L2_STD_NTSC_M,
 
  85                 .type           = V4L2_INPUT_TYPE_TUNER,
 
  88                 .std            = V4L2_STD_PAL_BG|V4L2_STD_NTSC_M,
 
  93                 .type           = V4L2_INPUT_TYPE_CAMERA,
 
  96                 .std            = V4L2_STD_PAL_BG|V4L2_STD_NTSC_M,
 
 101                 .type           = V4L2_INPUT_TYPE_CAMERA,
 
 104                 .std            = V4L2_STD_PAL_BG|V4L2_STD_NTSC_M,
 
 109 static int ves1820_writereg(struct saa7146_dev *dev, u8 addr, u8 reg, u8 data)
 
 111         u8 buf[] = { 0x00, reg, data };
 
 112         struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = buf, .len = 3 };
 
 114         dprintk(4, "dev: %p\n", dev);
 
 116         if (1 != saa7146_i2c_transfer(dev, &msg, 1, 1))
 
 121 static int stv0297_writereg(struct saa7146_dev *dev, u8 addr, u8 reg, u8 data)
 
 123         u8 buf [] = { reg, data };
 
 124         struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = buf, .len = 2 };
 
 126         if (1 != saa7146_i2c_transfer(dev, &msg, 1, 1))
 
 132 static int tuner_write(struct saa7146_dev *dev, u8 addr, u8 data [4])
 
 134         struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = data, .len = 4 };
 
 136         dprintk(4, "dev: %p\n", dev);
 
 138         if (1 != saa7146_i2c_transfer(dev, &msg, 1, 1))
 
 143 static int ves1820_set_tv_freq(struct saa7146_dev *dev, u32 freq)
 
 149         dprintk(4, "freq: 0x%08x\n", freq);
 
 151         /* magic number: 614. tuning with the frequency given by v4l2
 
 152            is always off by 614*62.5 = 38375 kHz...*/
 
 155         buf[0] = (div >> 8) & 0x7f;
 
 159         if (freq < (u32) (16 * 168.25))
 
 161         else if (freq < (u32) (16 * 447.25))
 
 169         return tuner_write(dev, 0x61, buf);
 
 172 static int stv0297_set_tv_freq(struct saa7146_dev *dev, u32 freq)
 
 177         div = (freq + 38900000 + 31250) / 62500;
 
 179         data[0] = (div >> 8) & 0x7f;
 
 180         data[1] = div & 0xff;
 
 185         else if (freq < 137000000)
 
 187         else if (freq < 403000000)
 
 189         else if (freq < 860000000)
 
 194         stv0297_writereg(dev, 0x1C, 0x87, 0x78);
 
 195         stv0297_writereg(dev, 0x1C, 0x86, 0xc8);
 
 196         return tuner_write(dev, 0x63, data);
 
 201 static struct saa7146_standard analog_standard[];
 
 202 static struct saa7146_standard dvb_standard[];
 
 203 static struct saa7146_standard standard[];
 
 205 static struct v4l2_audio msp3400_v4l2_audio = {
 
 207         .name = "Television",
 
 208         .capability = V4L2_AUDCAP_STEREO
 
 211 static int av7110_dvb_c_switch(struct saa7146_fh *fh)
 
 213         struct saa7146_dev *dev = fh->dev;
 
 214         struct saa7146_vv *vv = dev->vv_data;
 
 215         struct av7110 *av7110 = (struct av7110*)dev->ext_priv;
 
 217         int source, sync, err;
 
 219         dprintk(4, "%p\n", av7110);
 
 221         if ((vv->video_status & STATUS_OVERLAY) != 0) {
 
 222                 vv->ov_suspend = vv->video_fh;
 
 223                 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
 
 225                         dprintk(2, "suspending video failed\n");
 
 226                         vv->ov_suspend = NULL;
 
 230         if (0 != av7110->current_input) {
 
 231                 dprintk(1, "switching to analog TV:\n");
 
 233                 source = SAA7146_HPS_SOURCE_PORT_B;
 
 234                 sync = SAA7146_HPS_SYNC_PORT_B;
 
 235                 memcpy(standard, analog_standard, sizeof(struct saa7146_standard) * 2);
 
 237                 switch (av7110->current_input) {
 
 239                         dprintk(1, "switching SAA7113 to Analog Tuner Input.\n");
 
 240                         msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0000); // loudspeaker source
 
 241                         msp_writereg(av7110, MSP_WR_DSP, 0x0009, 0x0000); // headphone source
 
 242                         msp_writereg(av7110, MSP_WR_DSP, 0x000a, 0x0000); // SCART 1 source
 
 243                         msp_writereg(av7110, MSP_WR_DSP, 0x000e, 0x3000); // FM matrix, mono
 
 244                         msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x4f00); // loudspeaker + headphone
 
 245                         msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x4f00); // SCART 1 volume
 
 247                         if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
 
 248                                 if (ves1820_writereg(dev, 0x09, 0x0f, 0x60))
 
 249                                         dprintk(1, "setting band in demodulator failed.\n");
 
 250                         } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
 
 251                                 saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTHI); // TDA9198 pin9(STD)
 
 252                                 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTHI); // TDA9198 pin30(VIF)
 
 254                         if (i2c_writereg(av7110, 0x48, 0x02, 0xd0) != 1)
 
 255                                 dprintk(1, "saa7113 write failed @ card %d", av7110->dvb_adapter.num);
 
 258                         dprintk(1, "switching SAA7113 to Video AV CVBS Input.\n");
 
 259                         if (i2c_writereg(av7110, 0x48, 0x02, 0xd2) != 1)
 
 260                                 dprintk(1, "saa7113 write failed @ card %d", av7110->dvb_adapter.num);
 
 263                         dprintk(1, "switching SAA7113 to Video AV Y/C Input.\n");
 
 264                         if (i2c_writereg(av7110, 0x48, 0x02, 0xd9) != 1)
 
 265                                 dprintk(1, "saa7113 write failed @ card %d", av7110->dvb_adapter.num);
 
 268                         dprintk(1, "switching SAA7113 to Input: AV7110: SAA7113: invalid input.\n");
 
 272                 source = SAA7146_HPS_SOURCE_PORT_A;
 
 273                 sync = SAA7146_HPS_SYNC_PORT_A;
 
 274                 memcpy(standard, dvb_standard, sizeof(struct saa7146_standard) * 2);
 
 275                 dprintk(1, "switching DVB mode\n");
 
 276                 msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0220); // loudspeaker source
 
 277                 msp_writereg(av7110, MSP_WR_DSP, 0x0009, 0x0220); // headphone source
 
 278                 msp_writereg(av7110, MSP_WR_DSP, 0x000a, 0x0220); // SCART 1 source
 
 279                 msp_writereg(av7110, MSP_WR_DSP, 0x000e, 0x3000); // FM matrix, mono
 
 280                 msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x7f00); // loudspeaker + headphone
 
 281                 msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x7f00); // SCART 1 volume
 
 283                 if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
 
 284                         if (ves1820_writereg(dev, 0x09, 0x0f, 0x20))
 
 285                                 dprintk(1, "setting band in demodulator failed.\n");
 
 286                 } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
 
 287                         saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTLO); // TDA9198 pin9(STD)
 
 288                         saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO); // TDA9198 pin30(VIF)
 
 292         /* hmm, this does not do anything!? */
 
 293         if (av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, ADSwitch, 1, adswitch))
 
 294                 dprintk(1, "ADSwitch error\n");
 
 296         saa7146_set_hps_source_and_sync(dev, source, sync);
 
 298         if (vv->ov_suspend != NULL) {
 
 299                 saa7146_start_preview(vv->ov_suspend);
 
 300                 vv->ov_suspend = NULL;
 
 306 static int av7110_ioctl(struct saa7146_fh *fh, unsigned int cmd, void *arg)
 
 308         struct saa7146_dev *dev = fh->dev;
 
 309         struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
 
 310         dprintk(4, "saa7146_dev: %p\n", dev);
 
 315                 struct v4l2_tuner *t = arg;
 
 319                 dprintk(2, "VIDIOC_G_TUNER: %d\n", t->index);
 
 321                 if (!av7110->analog_tuner_flags || t->index != 0)
 
 324                 memset(t, 0, sizeof(*t));
 
 325                 strcpy(t->name, "Television");
 
 327                 t->type = V4L2_TUNER_ANALOG_TV;
 
 328                 t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
 
 329                         V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
 
 330                 t->rangelow = 772;      /* 48.25 MHZ / 62.5 kHz = 772, see fi1216mk2-specs, page 2 */
 
 331                 t->rangehigh = 13684;   /* 855.25 MHz / 62.5 kHz = 13684 */
 
 332                 /* FIXME: add the real signal strength here */
 
 336                 // FIXME: standard / stereo detection is still broken
 
 337                 msp_readreg(av7110, MSP_RD_DEM, 0x007e, &stereo_det);
 
 338                 dprintk(1, "VIDIOC_G_TUNER: msp3400 TV standard detection: 0x%04x\n", stereo_det);
 
 339                 msp_readreg(av7110, MSP_RD_DSP, 0x0018, &stereo_det);
 
 340                 dprintk(1, "VIDIOC_G_TUNER: msp3400 stereo detection: 0x%04x\n", stereo_det);
 
 341                 stereo = (s8)(stereo_det >> 8);
 
 344                         t->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
 
 345                         t->audmode = V4L2_TUNER_MODE_STEREO;
 
 347                 else if (stereo < -0x10) {
 
 349                         t->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
 
 350                         t->audmode = V4L2_TUNER_MODE_LANG1;
 
 353                         t->rxsubchans = V4L2_TUNER_SUB_MONO;
 
 359                 struct v4l2_tuner *t = arg;
 
 361                 dprintk(2, "VIDIOC_S_TUNER: %d\n", t->index);
 
 363                 if (!av7110->analog_tuner_flags || av7110->current_input != 1)
 
 366                 switch (t->audmode) {
 
 367                 case V4L2_TUNER_MODE_STEREO:
 
 368                         dprintk(2, "VIDIOC_S_TUNER: V4L2_TUNER_MODE_STEREO\n");
 
 369                         fm_matrix = 0x3001; // stereo
 
 372                 case V4L2_TUNER_MODE_LANG1_LANG2:
 
 373                         dprintk(2, "VIDIOC_S_TUNER: V4L2_TUNER_MODE_LANG1_LANG2\n");
 
 374                         fm_matrix = 0x3000; // bilingual
 
 377                 case V4L2_TUNER_MODE_LANG1:
 
 378                         dprintk(2, "VIDIOC_S_TUNER: V4L2_TUNER_MODE_LANG1\n");
 
 379                         fm_matrix = 0x3000; // mono
 
 382                 case V4L2_TUNER_MODE_LANG2:
 
 383                         dprintk(2, "VIDIOC_S_TUNER: V4L2_TUNER_MODE_LANG2\n");
 
 384                         fm_matrix = 0x3000; // mono
 
 387                 default: /* case V4L2_TUNER_MODE_MONO: */
 
 388                         dprintk(2, "VIDIOC_S_TUNER: TDA9840_SET_MONO\n");
 
 389                         fm_matrix = 0x3000; // mono
 
 393                 msp_writereg(av7110, MSP_WR_DSP, 0x000e, fm_matrix);
 
 394                 msp_writereg(av7110, MSP_WR_DSP, 0x0008, src);
 
 395                 msp_writereg(av7110, MSP_WR_DSP, 0x0009, src);
 
 396                 msp_writereg(av7110, MSP_WR_DSP, 0x000a, src);
 
 399         case VIDIOC_G_FREQUENCY:
 
 401                 struct v4l2_frequency *f = arg;
 
 403                 dprintk(2, "VIDIOC_G_FREQ: freq:0x%08x.\n", f->frequency);
 
 405                 if (!av7110->analog_tuner_flags || av7110->current_input != 1)
 
 408                 memset(f, 0, sizeof(*f));
 
 409                 f->type = V4L2_TUNER_ANALOG_TV;
 
 410                 f->frequency =  av7110->current_freq;
 
 413         case VIDIOC_S_FREQUENCY:
 
 415                 struct v4l2_frequency *f = arg;
 
 417                 dprintk(2, "VIDIOC_S_FREQUENCY: freq:0x%08x.\n", f->frequency);
 
 419                 if (!av7110->analog_tuner_flags || av7110->current_input != 1)
 
 422                 if (V4L2_TUNER_ANALOG_TV != f->type)
 
 425                 msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0xffe0); // fast mute
 
 426                 msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0xffe0);
 
 428                 /* tune in desired frequency */
 
 429                 if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
 
 430                         ves1820_set_tv_freq(dev, f->frequency);
 
 431                 } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
 
 432                         stv0297_set_tv_freq(dev, f->frequency);
 
 434                 av7110->current_freq = f->frequency;
 
 436                 msp_writereg(av7110, MSP_WR_DSP, 0x0015, 0x003f); // start stereo detection
 
 437                 msp_writereg(av7110, MSP_WR_DSP, 0x0015, 0x0000);
 
 438                 msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x4f00); // loudspeaker + headphone
 
 439                 msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x4f00); // SCART 1 volume
 
 442         case VIDIOC_ENUMINPUT:
 
 444                 struct v4l2_input *i = arg;
 
 446                 dprintk(2, "VIDIOC_ENUMINPUT: %d\n", i->index);
 
 448                 if (av7110->analog_tuner_flags) {
 
 449                         if (i->index < 0 || i->index >= 4)
 
 456                 memcpy(i, &inputs[i->index], sizeof(struct v4l2_input));
 
 462                 int *input = (int *)arg;
 
 463                 *input = av7110->current_input;
 
 464                 dprintk(2, "VIDIOC_G_INPUT: %d\n", *input);
 
 469                 int input = *(int *)arg;
 
 471                 dprintk(2, "VIDIOC_S_INPUT: %d\n", input);
 
 473                 if (!av7110->analog_tuner_flags)
 
 476                 if (input < 0 || input >= 4)
 
 479                 av7110->current_input = input;
 
 480                 return av7110_dvb_c_switch(fh);
 
 484                 struct v4l2_audio *a = arg;
 
 486                 dprintk(2, "VIDIOC_G_AUDIO: %d\n", a->index);
 
 489                 memcpy(a, &msp3400_v4l2_audio, sizeof(struct v4l2_audio));
 
 494                 struct v4l2_audio *a = arg;
 
 495                 dprintk(2, "VIDIOC_S_AUDIO: %d\n", a->index);
 
 498         case VIDIOC_G_SLICED_VBI_CAP:
 
 500                 struct v4l2_sliced_vbi_cap *cap = arg;
 
 501                 dprintk(2, "VIDIOC_G_SLICED_VBI_CAP\n");
 
 502                 memset(cap, 0, sizeof *cap);
 
 503                 if (FW_VERSION(av7110->arm_app) >= 0x2623) {
 
 504                         cap->service_set = V4L2_SLICED_WSS_625;
 
 505                         cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
 
 511                 struct v4l2_format *f = arg;
 
 512                 dprintk(2, "VIDIOC_G_FMT:\n");
 
 513                 if (f->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT ||
 
 514                     FW_VERSION(av7110->arm_app) < 0x2623)
 
 515                         return -EAGAIN; /* handled by core driver */
 
 516                 memset(&f->fmt.sliced, 0, sizeof f->fmt.sliced);
 
 517                 if (av7110->wssMode) {
 
 518                         f->fmt.sliced.service_set = V4L2_SLICED_WSS_625;
 
 519                         f->fmt.sliced.service_lines[0][23] = V4L2_SLICED_WSS_625;
 
 520                         f->fmt.sliced.io_size = sizeof (struct v4l2_sliced_vbi_data);
 
 526                 struct v4l2_format *f = arg;
 
 527                 dprintk(2, "VIDIOC_S_FMT\n");
 
 528                 if (f->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT ||
 
 529                     FW_VERSION(av7110->arm_app) < 0x2623)
 
 530                         return -EAGAIN; /* handled by core driver */
 
 531                 if (f->fmt.sliced.service_set != V4L2_SLICED_WSS_625 &&
 
 532                     f->fmt.sliced.service_lines[0][23] != V4L2_SLICED_WSS_625) {
 
 533                         memset(&f->fmt.sliced, 0, sizeof f->fmt.sliced);
 
 534                         /* WSS controlled by firmware */
 
 537                         return av7110_fw_cmd(av7110, COMTYPE_ENCODER,
 
 540                         memset(&f->fmt.sliced, 0, sizeof f->fmt.sliced);
 
 541                         f->fmt.sliced.service_set = V4L2_SLICED_WSS_625;
 
 542                         f->fmt.sliced.service_lines[0][23] = V4L2_SLICED_WSS_625;
 
 543                         f->fmt.sliced.io_size = sizeof (struct v4l2_sliced_vbi_data);
 
 544                         /* WSS controlled by userspace */
 
 551                 printk("no such ioctl\n");
 
 557 static int av7110_vbi_reset(struct inode *inode, struct file *file)
 
 559         struct saa7146_fh *fh = file->private_data;
 
 560         struct saa7146_dev *dev = fh->dev;
 
 561         struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
 
 563         dprintk(2, "%s\n", __FUNCTION__);
 
 566         if (FW_VERSION(av7110->arm_app) < 0x2623)
 
 569                 return av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 1, 0);
 
 572 static ssize_t av7110_vbi_write(struct file *file, const char __user *data, size_t count, loff_t *ppos)
 
 574         struct saa7146_fh *fh = file->private_data;
 
 575         struct saa7146_dev *dev = fh->dev;
 
 576         struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
 
 577         struct v4l2_sliced_vbi_data d;
 
 580         dprintk(2, "%s\n", __FUNCTION__);
 
 581         if (FW_VERSION(av7110->arm_app) < 0x2623 || !av7110->wssMode || count != sizeof d)
 
 583         if (copy_from_user(&d, data, count))
 
 585         if ((d.id != 0 && d.id != V4L2_SLICED_WSS_625) || d.field != 0 || d.line != 23)
 
 588                 av7110->wssData = ((d.data[1] << 8) & 0x3f00) | d.data[0];
 
 590                 av7110->wssData = 0x8000;
 
 591         rc = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 2, 1, av7110->wssData);
 
 592         return (rc < 0) ? rc : count;
 
 595 /****************************************************************************
 
 597  ****************************************************************************/
 
 599 static struct saa7146_extension_ioctls ioctls[] = {
 
 600         { VIDIOC_ENUMINPUT,     SAA7146_EXCLUSIVE },
 
 601         { VIDIOC_G_INPUT,       SAA7146_EXCLUSIVE },
 
 602         { VIDIOC_S_INPUT,       SAA7146_EXCLUSIVE },
 
 603         { VIDIOC_G_FREQUENCY,   SAA7146_EXCLUSIVE },
 
 604         { VIDIOC_S_FREQUENCY,   SAA7146_EXCLUSIVE },
 
 605         { VIDIOC_G_TUNER,       SAA7146_EXCLUSIVE },
 
 606         { VIDIOC_S_TUNER,       SAA7146_EXCLUSIVE },
 
 607         { VIDIOC_G_AUDIO,       SAA7146_EXCLUSIVE },
 
 608         { VIDIOC_S_AUDIO,       SAA7146_EXCLUSIVE },
 
 609         { VIDIOC_G_SLICED_VBI_CAP, SAA7146_EXCLUSIVE },
 
 610         { VIDIOC_G_FMT,         SAA7146_BEFORE },
 
 611         { VIDIOC_S_FMT,         SAA7146_BEFORE },
 
 615 static u8 saa7113_init_regs[] = {
 
 674 static struct saa7146_ext_vv av7110_vv_data_st;
 
 675 static struct saa7146_ext_vv av7110_vv_data_c;
 
 677 int av7110_init_analog_module(struct av7110 *av7110)
 
 679         u16 version1, version2;
 
 681         if (i2c_writereg(av7110, 0x80, 0x0, 0x80) != 1
 
 682             || i2c_writereg(av7110, 0x80, 0x0, 0) != 1)
 
 685         printk("dvb-ttpci: DVB-C analog module @ card %d detected, initializing MSP3400\n",
 
 686                 av7110->dvb_adapter.num);
 
 687         av7110->adac_type = DVB_ADAC_MSP34x0;
 
 688         msleep(100); // the probing above resets the msp...
 
 689         msp_readreg(av7110, MSP_RD_DSP, 0x001e, &version1);
 
 690         msp_readreg(av7110, MSP_RD_DSP, 0x001f, &version2);
 
 691         dprintk(1, "dvb-ttpci: @ card %d MSP3400 version 0x%04x 0x%04x\n",
 
 692                 av7110->dvb_adapter.num, version1, version2);
 
 693         msp_writereg(av7110, MSP_WR_DSP, 0x0013, 0x0c00);
 
 694         msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x7f00); // loudspeaker + headphone
 
 695         msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0220); // loudspeaker source
 
 696         msp_writereg(av7110, MSP_WR_DSP, 0x0009, 0x0220); // headphone source
 
 697         msp_writereg(av7110, MSP_WR_DSP, 0x0004, 0x7f00); // loudspeaker volume
 
 698         msp_writereg(av7110, MSP_WR_DSP, 0x000a, 0x0220); // SCART 1 source
 
 699         msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x7f00); // SCART 1 volume
 
 700         msp_writereg(av7110, MSP_WR_DSP, 0x000d, 0x4800); // prescale SCART
 
 702         if (i2c_writereg(av7110, 0x48, 0x01, 0x00)!=1) {
 
 703                 INFO(("saa7113 not accessible.\n"));
 
 705                 u8 *i = saa7113_init_regs;
 
 707                 if ((av7110->dev->pci->subsystem_vendor == 0x110a) && (av7110->dev->pci->subsystem_device == 0x0000)) {
 
 708                         /* Fujitsu/Siemens DVB-Cable */
 
 709                         av7110->analog_tuner_flags |= ANALOG_TUNER_VES1820;
 
 710                 } else if ((av7110->dev->pci->subsystem_vendor == 0x13c2) && (av7110->dev->pci->subsystem_device == 0x0002)) {
 
 711                         /* Hauppauge/TT DVB-C premium */
 
 712                         av7110->analog_tuner_flags |= ANALOG_TUNER_VES1820;
 
 713                 } else if ((av7110->dev->pci->subsystem_vendor == 0x13c2) && (av7110->dev->pci->subsystem_device == 0x000A)) {
 
 714                         /* Hauppauge/TT DVB-C premium */
 
 715                         av7110->analog_tuner_flags |= ANALOG_TUNER_STV0297;
 
 718                 /* setup for DVB by default */
 
 719                 if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
 
 720                         if (ves1820_writereg(av7110->dev, 0x09, 0x0f, 0x20))
 
 721                                 dprintk(1, "setting band in demodulator failed.\n");
 
 722                 } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
 
 723                         saa7146_setgpio(av7110->dev, 1, SAA7146_GPIO_OUTLO); // TDA9198 pin9(STD)
 
 724                         saa7146_setgpio(av7110->dev, 3, SAA7146_GPIO_OUTLO); // TDA9198 pin30(VIF)
 
 727                 /* init the saa7113 */
 
 729                         if (i2c_writereg(av7110, 0x48, i[0], i[1]) != 1) {
 
 730                                 dprintk(1, "saa7113 initialization failed @ card %d", av7110->dvb_adapter.num);
 
 735                 /* setup msp for analog sound: B/G Dual-FM */
 
 736                 msp_writereg(av7110, MSP_WR_DEM, 0x00bb, 0x02d0); // AD_CV
 
 737                 msp_writereg(av7110, MSP_WR_DEM, 0x0001,  3); // FIR1
 
 738                 msp_writereg(av7110, MSP_WR_DEM, 0x0001, 18); // FIR1
 
 739                 msp_writereg(av7110, MSP_WR_DEM, 0x0001, 27); // FIR1
 
 740                 msp_writereg(av7110, MSP_WR_DEM, 0x0001, 48); // FIR1
 
 741                 msp_writereg(av7110, MSP_WR_DEM, 0x0001, 66); // FIR1
 
 742                 msp_writereg(av7110, MSP_WR_DEM, 0x0001, 72); // FIR1
 
 743                 msp_writereg(av7110, MSP_WR_DEM, 0x0005,  4); // FIR2
 
 744                 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 64); // FIR2
 
 745                 msp_writereg(av7110, MSP_WR_DEM, 0x0005,  0); // FIR2
 
 746                 msp_writereg(av7110, MSP_WR_DEM, 0x0005,  3); // FIR2
 
 747                 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 18); // FIR2
 
 748                 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 27); // FIR2
 
 749                 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 48); // FIR2
 
 750                 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 66); // FIR2
 
 751                 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 72); // FIR2
 
 752                 msp_writereg(av7110, MSP_WR_DEM, 0x0083, 0xa000); // MODE_REG
 
 753                 msp_writereg(av7110, MSP_WR_DEM, 0x0093, 0x00aa); // DCO1_LO 5.74MHz
 
 754                 msp_writereg(av7110, MSP_WR_DEM, 0x009b, 0x04fc); // DCO1_HI
 
 755                 msp_writereg(av7110, MSP_WR_DEM, 0x00a3, 0x038e); // DCO2_LO 5.5MHz
 
 756                 msp_writereg(av7110, MSP_WR_DEM, 0x00ab, 0x04c6); // DCO2_HI
 
 757                 msp_writereg(av7110, MSP_WR_DEM, 0x0056, 0); // LOAD_REG 1/2
 
 760         memcpy(standard, dvb_standard, sizeof(struct saa7146_standard) * 2);
 
 761         /* set dd1 stream a & b */
 
 762         saa7146_write(av7110->dev, DD1_STREAM_B, 0x00000000);
 
 763         saa7146_write(av7110->dev, DD1_INIT, 0x03000700);
 
 764         saa7146_write(av7110->dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
 
 769 int av7110_init_v4l(struct av7110 *av7110)
 
 771         struct saa7146_dev* dev = av7110->dev;
 
 774         /* special case DVB-C: these cards have an analog tuner
 
 775            plus need some special handling, so we have separate
 
 776            saa7146_ext_vv data for these... */
 
 777         if (av7110->analog_tuner_flags)
 
 778                 ret = saa7146_vv_init(dev, &av7110_vv_data_c);
 
 780                 ret = saa7146_vv_init(dev, &av7110_vv_data_st);
 
 783                 ERR(("cannot init capture device. skipping.\n"));
 
 787         if (saa7146_register_device(&av7110->v4l_dev, dev, "av7110", VFL_TYPE_GRABBER)) {
 
 788                 ERR(("cannot register capture device. skipping.\n"));
 
 789                 saa7146_vv_release(dev);
 
 792         if (saa7146_register_device(&av7110->vbi_dev, dev, "av7110", VFL_TYPE_VBI)) {
 
 793                 ERR(("cannot register vbi v4l2 device. skipping.\n"));
 
 795                 if (av7110->analog_tuner_flags)
 
 796                         av7110->analog_tuner_flags |= ANALOG_TUNER_VBI;
 
 801 int av7110_exit_v4l(struct av7110 *av7110)
 
 803         saa7146_unregister_device(&av7110->v4l_dev, av7110->dev);
 
 804         if (av7110->analog_tuner_flags & ANALOG_TUNER_VBI)
 
 805                 saa7146_unregister_device(&av7110->vbi_dev, av7110->dev);
 
 811 /* FIXME: these values are experimental values that look better than the
 
 812    values from the latest "official" driver -- at least for me... (MiHu) */
 
 813 static struct saa7146_standard standard[] = {
 
 815                 .name   = "PAL",        .id             = V4L2_STD_PAL_BG,
 
 816                 .v_offset       = 0x15, .v_field        = 288,
 
 817                 .h_offset       = 0x48, .h_pixels       = 708,
 
 818                 .v_max_out      = 576,  .h_max_out      = 768,
 
 820                 .name   = "NTSC",       .id             = V4L2_STD_NTSC,
 
 821                 .v_offset       = 0x10, .v_field        = 244,
 
 822                 .h_offset       = 0x40, .h_pixels       = 708,
 
 823                 .v_max_out      = 480,  .h_max_out      = 640,
 
 827 static struct saa7146_standard analog_standard[] = {
 
 829                 .name   = "PAL",        .id             = V4L2_STD_PAL_BG,
 
 830                 .v_offset       = 0x1b, .v_field        = 288,
 
 831                 .h_offset       = 0x08, .h_pixels       = 708,
 
 832                 .v_max_out      = 576,  .h_max_out      = 768,
 
 834                 .name   = "NTSC",       .id             = V4L2_STD_NTSC,
 
 835                 .v_offset       = 0x10, .v_field        = 244,
 
 836                 .h_offset       = 0x40, .h_pixels       = 708,
 
 837                 .v_max_out      = 480,  .h_max_out      = 640,
 
 841 static struct saa7146_standard dvb_standard[] = {
 
 843                 .name   = "PAL",        .id             = V4L2_STD_PAL_BG,
 
 844                 .v_offset       = 0x14, .v_field        = 288,
 
 845                 .h_offset       = 0x48, .h_pixels       = 708,
 
 846                 .v_max_out      = 576,  .h_max_out      = 768,
 
 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,
 
 855 static int std_callback(struct saa7146_dev* dev, struct saa7146_standard *std)
 
 857         struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
 
 859         if (std->id & V4L2_STD_PAL) {
 
 860                 av7110->vidmode = VIDEO_MODE_PAL;
 
 861                 av7110_set_vidmode(av7110, av7110->vidmode);
 
 863         else if (std->id & V4L2_STD_NTSC) {
 
 864                 av7110->vidmode = VIDEO_MODE_NTSC;
 
 865                 av7110_set_vidmode(av7110, av7110->vidmode);
 
 874 static struct saa7146_ext_vv av7110_vv_data_st = {
 
 877         .capabilities   = V4L2_CAP_SLICED_VBI_OUTPUT,
 
 880         .stds           = &standard[0],
 
 881         .num_stds       = ARRAY_SIZE(standard),
 
 882         .std_callback   = &std_callback,
 
 884         .ioctls         = &ioctls[0],
 
 885         .ioctl          = av7110_ioctl,
 
 887         .vbi_fops.open  = av7110_vbi_reset,
 
 888         .vbi_fops.release = av7110_vbi_reset,
 
 889         .vbi_fops.write = av7110_vbi_write,
 
 892 static struct saa7146_ext_vv av7110_vv_data_c = {
 
 895         .capabilities   = V4L2_CAP_TUNER | V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_OUTPUT,
 
 896         .flags          = SAA7146_USE_PORT_B_FOR_VBI,
 
 898         .stds           = &standard[0],
 
 899         .num_stds       = ARRAY_SIZE(standard),
 
 900         .std_callback   = &std_callback,
 
 902         .ioctls         = &ioctls[0],
 
 903         .ioctl          = av7110_ioctl,
 
 905         .vbi_fops.open  = av7110_vbi_reset,
 
 906         .vbi_fops.release = av7110_vbi_reset,
 
 907         .vbi_fops.write = av7110_vbi_write,