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