linux/drivers/media/video/cx18/cx18-av-core.c
<<
>>
Prefs
   1/*
   2 *  cx18 ADEC audio functions
   3 *
   4 *  Derived from cx25840-core.c
   5 *
   6 *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
   7 *
   8 *  This program is free software; you can redistribute it and/or
   9 *  modify it under the terms of the GNU General Public License
  10 *  as published by the Free Software Foundation; either version 2
  11 *  of the License, or (at your option) any later version.
  12 *
  13 *  This program is distributed in the hope that it will be useful,
  14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 *  GNU General Public License for more details.
  17 *
  18 *  You should have received a copy of the GNU General Public License
  19 *  along with this program; if not, write to the Free Software
  20 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21 *  02110-1301, USA.
  22 */
  23
  24#include "cx18-driver.h"
  25#include "cx18-io.h"
  26
  27int cx18_av_write(struct cx18 *cx, u16 addr, u8 value)
  28{
  29        u32 reg = 0xc40000 + (addr & ~3);
  30        u32 mask = 0xff;
  31        int shift = (addr & 3) * 8;
  32        u32 x = cx18_read_reg(cx, reg);
  33
  34        x = (x & ~(mask << shift)) | ((u32)value << shift);
  35        cx18_write_reg(cx, x, reg);
  36        return 0;
  37}
  38
  39int cx18_av_write4(struct cx18 *cx, u16 addr, u32 value)
  40{
  41        cx18_write_reg(cx, value, 0xc40000 + addr);
  42        return 0;
  43}
  44
  45int cx18_av_write4_noretry(struct cx18 *cx, u16 addr, u32 value)
  46{
  47        cx18_write_reg_noretry(cx, value, 0xc40000 + addr);
  48        return 0;
  49}
  50
  51u8 cx18_av_read(struct cx18 *cx, u16 addr)
  52{
  53        u32 x = cx18_read_reg(cx, 0xc40000 + (addr & ~3));
  54        int shift = (addr & 3) * 8;
  55
  56        return (x >> shift) & 0xff;
  57}
  58
  59u32 cx18_av_read4(struct cx18 *cx, u16 addr)
  60{
  61        return cx18_read_reg(cx, 0xc40000 + addr);
  62}
  63
  64u32 cx18_av_read4_noretry(struct cx18 *cx, u16 addr)
  65{
  66        return cx18_read_reg_noretry(cx, 0xc40000 + addr);
  67}
  68
  69int cx18_av_and_or(struct cx18 *cx, u16 addr, unsigned and_mask,
  70                   u8 or_value)
  71{
  72        return cx18_av_write(cx, addr,
  73                             (cx18_av_read(cx, addr) & and_mask) |
  74                             or_value);
  75}
  76
  77int cx18_av_and_or4(struct cx18 *cx, u16 addr, u32 and_mask,
  78                   u32 or_value)
  79{
  80        return cx18_av_write4(cx, addr,
  81                             (cx18_av_read4(cx, addr) & and_mask) |
  82                             or_value);
  83}
  84
  85/* ----------------------------------------------------------------------- */
  86
  87static int set_input(struct cx18 *cx, enum cx18_av_video_input vid_input,
  88                                        enum cx18_av_audio_input aud_input);
  89static void log_audio_status(struct cx18 *cx);
  90static void log_video_status(struct cx18 *cx);
  91
  92/* ----------------------------------------------------------------------- */
  93
  94static void cx18_av_initialize(struct cx18 *cx)
  95{
  96        struct cx18_av_state *state = &cx->av_state;
  97        u32 v;
  98
  99        cx18_av_loadfw(cx);
 100        /* Stop 8051 code execution */
 101        cx18_av_write4(cx, CXADEC_DL_CTL, 0x03000000);
 102
 103        /* initallize the PLL by toggling sleep bit */
 104        v = cx18_av_read4(cx, CXADEC_HOST_REG1);
 105        /* enable sleep mode */
 106        cx18_av_write4(cx, CXADEC_HOST_REG1, v | 1);
 107        /* disable sleep mode */
 108        cx18_av_write4(cx, CXADEC_HOST_REG1, v & 0xfffe);
 109
 110        /* initialize DLLs */
 111        v = cx18_av_read4(cx, CXADEC_DLL1_DIAG_CTRL) & 0xE1FFFEFF;
 112        /* disable FLD */
 113        cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v);
 114        /* enable FLD */
 115        cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v | 0x10000100);
 116
 117        v = cx18_av_read4(cx, CXADEC_DLL2_DIAG_CTRL) & 0xE1FFFEFF;
 118        /* disable FLD */
 119        cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v);
 120        /* enable FLD */
 121        cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v | 0x06000100);
 122
 123        /* set analog bias currents. Set Vreg to 1.20V. */
 124        cx18_av_write4(cx, CXADEC_AFE_DIAG_CTRL1, 0x000A1802);
 125
 126        v = cx18_av_read4(cx, CXADEC_AFE_DIAG_CTRL3) | 1;
 127        /* enable TUNE_FIL_RST */
 128        cx18_av_write4(cx, CXADEC_AFE_DIAG_CTRL3, v);
 129        /* disable TUNE_FIL_RST */
 130        cx18_av_write4(cx, CXADEC_AFE_DIAG_CTRL3, v & 0xFFFFFFFE);
 131
 132        /* enable 656 output */
 133        cx18_av_and_or4(cx, CXADEC_PIN_CTRL1, ~0, 0x040C00);
 134
 135        /* video output drive strength */
 136        cx18_av_and_or4(cx, CXADEC_PIN_CTRL2, ~0, 0x2);
 137
 138        /* reset video */
 139        cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0x8000);
 140        cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0);
 141
 142        /* set video to auto-detect */
 143        /* Clear bits 11-12 to enable slow locking mode.  Set autodetect mode */
 144        /* set the comb notch = 1 */
 145        cx18_av_and_or4(cx, CXADEC_MODE_CTRL, 0xFFF7E7F0, 0x02040800);
 146
 147        /* Enable wtw_en in CRUSH_CTRL (Set bit 22) */
 148        /* Enable maj_sel in CRUSH_CTRL (Set bit 20) */
 149        cx18_av_and_or4(cx, CXADEC_CRUSH_CTRL, ~0, 0x00500000);
 150
 151        /* Set VGA_TRACK_RANGE to 0x20 */
 152        cx18_av_and_or4(cx, CXADEC_DFE_CTRL2, 0xFFFF00FF, 0x00002000);
 153
 154        /* Enable VBI capture */
 155        cx18_av_write4(cx, CXADEC_OUT_CTRL1, 0x4010253F);
 156        /* cx18_av_write4(cx, CXADEC_OUT_CTRL1, 0x4010253E); */
 157
 158        /* Set the video input.
 159           The setting in MODE_CTRL gets lost when we do the above setup */
 160        /* EncSetSignalStd(dwDevNum, pEnc->dwSigStd); */
 161        /* EncSetVideoInput(dwDevNum, pEnc->VidIndSelection); */
 162
 163        v = cx18_av_read4(cx, CXADEC_AFE_CTRL);
 164        v &= 0xFFFBFFFF;            /* turn OFF bit 18 for droop_comp_ch1 */
 165        v &= 0xFFFF7FFF;            /* turn OFF bit 9 for clamp_sel_ch1 */
 166        v &= 0xFFFFFFFE;            /* turn OFF bit 0 for 12db_ch1 */
 167        /* v |= 0x00000001;*/            /* turn ON bit 0 for 12db_ch1 */
 168        cx18_av_write4(cx, CXADEC_AFE_CTRL, v);
 169
 170/*      if(dwEnable && dw3DCombAvailable) { */
 171/*              CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x7728021F); */
 172/*    } else { */
 173/*              CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x6628021F); */
 174/*    } */
 175        cx18_av_write4(cx, CXADEC_SRC_COMB_CFG, 0x6628021F);
 176        state->default_volume = 228 - cx18_av_read(cx, 0x8d4);
 177        state->default_volume = ((state->default_volume / 2) + 23) << 9;
 178}
 179
 180/* ----------------------------------------------------------------------- */
 181
 182void cx18_av_std_setup(struct cx18 *cx)
 183{
 184        struct cx18_av_state *state = &cx->av_state;
 185        v4l2_std_id std = state->std;
 186        int hblank, hactive, burst, vblank, vactive, sc;
 187        int vblank656, src_decimation;
 188        int luma_lpf, uv_lpf, comb;
 189        u32 pll_int, pll_frac, pll_post;
 190
 191        /* datasheet startup, step 8d */
 192        if (std & ~V4L2_STD_NTSC)
 193                cx18_av_write(cx, 0x49f, 0x11);
 194        else
 195                cx18_av_write(cx, 0x49f, 0x14);
 196
 197        if (std & V4L2_STD_625_50) {
 198                hblank = 132;
 199                hactive = 720;
 200                burst = 93;
 201                vblank = 36;
 202                vactive = 580;
 203                vblank656 = 40;
 204                src_decimation = 0x21f;
 205
 206                luma_lpf = 2;
 207                if (std & V4L2_STD_PAL) {
 208                        uv_lpf = 1;
 209                        comb = 0x20;
 210                        sc = 688739;
 211                } else if (std == V4L2_STD_PAL_Nc) {
 212                        uv_lpf = 1;
 213                        comb = 0x20;
 214                        sc = 556453;
 215                } else { /* SECAM */
 216                        uv_lpf = 0;
 217                        comb = 0;
 218                        sc = 672351;
 219                }
 220        } else {
 221                hactive = 720;
 222                hblank = 122;
 223                vactive = 487;
 224                luma_lpf = 1;
 225                uv_lpf = 1;
 226                vblank = 26;
 227                vblank656 = 26;
 228
 229                src_decimation = 0x21f;
 230                if (std == V4L2_STD_PAL_60) {
 231                        burst = 0x5b;
 232                        luma_lpf = 2;
 233                        comb = 0x20;
 234                        sc = 688739;
 235                } else if (std == V4L2_STD_PAL_M) {
 236                        burst = 0x61;
 237                        comb = 0x20;
 238                        sc = 555452;
 239                } else {
 240                        burst = 0x5b;
 241                        comb = 0x66;
 242                        sc = 556063;
 243                }
 244        }
 245
 246        /* DEBUG: Displays configured PLL frequency */
 247        pll_int = cx18_av_read(cx, 0x108);
 248        pll_frac = cx18_av_read4(cx, 0x10c) & 0x1ffffff;
 249        pll_post = cx18_av_read(cx, 0x109);
 250        CX18_DEBUG_INFO("PLL regs = int: %u, frac: %u, post: %u\n",
 251                        pll_int, pll_frac, pll_post);
 252
 253        if (pll_post) {
 254                int fin, fsc;
 255                int pll = 28636363L * ((((u64)pll_int) << 25) + pll_frac);
 256
 257                pll >>= 25;
 258                pll /= pll_post;
 259                CX18_DEBUG_INFO("PLL = %d.%06d MHz\n",
 260                                        pll / 1000000, pll % 1000000);
 261                CX18_DEBUG_INFO("PLL/8 = %d.%06d MHz\n",
 262                                        pll / 8000000, (pll / 8) % 1000000);
 263
 264                fin = ((u64)src_decimation * pll) >> 12;
 265                CX18_DEBUG_INFO("ADC Sampling freq = %d.%06d MHz\n",
 266                                        fin / 1000000, fin % 1000000);
 267
 268                fsc = (((u64)sc) * pll) >> 24L;
 269                CX18_DEBUG_INFO("Chroma sub-carrier freq = %d.%06d MHz\n",
 270                                        fsc / 1000000, fsc % 1000000);
 271
 272                CX18_DEBUG_INFO("hblank %i, hactive %i, "
 273                        "vblank %i , vactive %i, vblank656 %i, src_dec %i,"
 274                        "burst 0x%02x, luma_lpf %i, uv_lpf %i, comb 0x%02x,"
 275                        " sc 0x%06x\n",
 276                        hblank, hactive, vblank, vactive, vblank656,
 277                        src_decimation, burst, luma_lpf, uv_lpf, comb, sc);
 278        }
 279
 280        /* Sets horizontal blanking delay and active lines */
 281        cx18_av_write(cx, 0x470, hblank);
 282        cx18_av_write(cx, 0x471, 0xff & (((hblank >> 8) & 0x3) |
 283                                                (hactive << 4)));
 284        cx18_av_write(cx, 0x472, hactive >> 4);
 285
 286        /* Sets burst gate delay */
 287        cx18_av_write(cx, 0x473, burst);
 288
 289        /* Sets vertical blanking delay and active duration */
 290        cx18_av_write(cx, 0x474, vblank);
 291        cx18_av_write(cx, 0x475, 0xff & (((vblank >> 8) & 0x3) |
 292                                                (vactive << 4)));
 293        cx18_av_write(cx, 0x476, vactive >> 4);
 294        cx18_av_write(cx, 0x477, vblank656);
 295
 296        /* Sets src decimation rate */
 297        cx18_av_write(cx, 0x478, 0xff & src_decimation);
 298        cx18_av_write(cx, 0x479, 0xff & (src_decimation >> 8));
 299
 300        /* Sets Luma and UV Low pass filters */
 301        cx18_av_write(cx, 0x47a, luma_lpf << 6 | ((uv_lpf << 4) & 0x30));
 302
 303        /* Enables comb filters */
 304        cx18_av_write(cx, 0x47b, comb);
 305
 306        /* Sets SC Step*/
 307        cx18_av_write(cx, 0x47c, sc);
 308        cx18_av_write(cx, 0x47d, 0xff & sc >> 8);
 309        cx18_av_write(cx, 0x47e, 0xff & sc >> 16);
 310
 311        /* Sets VBI parameters */
 312        if (std & V4L2_STD_625_50) {
 313                cx18_av_write(cx, 0x47f, 0x01);
 314                state->vbi_line_offset = 5;
 315        } else {
 316                cx18_av_write(cx, 0x47f, 0x00);
 317                state->vbi_line_offset = 8;
 318        }
 319}
 320
 321/* ----------------------------------------------------------------------- */
 322
 323static void input_change(struct cx18 *cx)
 324{
 325        struct cx18_av_state *state = &cx->av_state;
 326        v4l2_std_id std = state->std;
 327
 328        /* Follow step 8c and 8d of section 3.16 in the cx18_av datasheet */
 329        cx18_av_write(cx, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
 330        cx18_av_and_or(cx, 0x401, ~0x60, 0);
 331        cx18_av_and_or(cx, 0x401, ~0x60, 0x60);
 332
 333        if (std & V4L2_STD_525_60) {
 334                if (std == V4L2_STD_NTSC_M_JP) {
 335                        /* Japan uses EIAJ audio standard */
 336                        cx18_av_write(cx, 0x808, 0xf7);
 337                        cx18_av_write(cx, 0x80b, 0x02);
 338                } else if (std == V4L2_STD_NTSC_M_KR) {
 339                        /* South Korea uses A2 audio standard */
 340                        cx18_av_write(cx, 0x808, 0xf8);
 341                        cx18_av_write(cx, 0x80b, 0x03);
 342                } else {
 343                        /* Others use the BTSC audio standard */
 344                        cx18_av_write(cx, 0x808, 0xf6);
 345                        cx18_av_write(cx, 0x80b, 0x01);
 346                }
 347        } else if (std & V4L2_STD_PAL) {
 348                /* Follow tuner change procedure for PAL */
 349                cx18_av_write(cx, 0x808, 0xff);
 350                cx18_av_write(cx, 0x80b, 0x03);
 351        } else if (std & V4L2_STD_SECAM) {
 352                /* Select autodetect for SECAM */
 353                cx18_av_write(cx, 0x808, 0xff);
 354                cx18_av_write(cx, 0x80b, 0x03);
 355        }
 356
 357        if (cx18_av_read(cx, 0x803) & 0x10) {
 358                /* restart audio decoder microcontroller */
 359                cx18_av_and_or(cx, 0x803, ~0x10, 0x00);
 360                cx18_av_and_or(cx, 0x803, ~0x10, 0x10);
 361        }
 362}
 363
 364static int set_input(struct cx18 *cx, enum cx18_av_video_input vid_input,
 365                                        enum cx18_av_audio_input aud_input)
 366{
 367        struct cx18_av_state *state = &cx->av_state;
 368        u8 is_composite = (vid_input >= CX18_AV_COMPOSITE1 &&
 369                           vid_input <= CX18_AV_COMPOSITE8);
 370        u8 reg;
 371
 372        CX18_DEBUG_INFO("decoder set video input %d, audio input %d\n",
 373                        vid_input, aud_input);
 374
 375        if (is_composite) {
 376                reg = 0xf0 + (vid_input - CX18_AV_COMPOSITE1);
 377        } else {
 378                int luma = vid_input & 0xf0;
 379                int chroma = vid_input & 0xf00;
 380
 381                if ((vid_input & ~0xff0) ||
 382                    luma < CX18_AV_SVIDEO_LUMA1 ||
 383                    luma > CX18_AV_SVIDEO_LUMA8 ||
 384                    chroma < CX18_AV_SVIDEO_CHROMA4 ||
 385                    chroma > CX18_AV_SVIDEO_CHROMA8) {
 386                        CX18_ERR("0x%04x is not a valid video input!\n",
 387                                        vid_input);
 388                        return -EINVAL;
 389                }
 390                reg = 0xf0 + ((luma - CX18_AV_SVIDEO_LUMA1) >> 4);
 391                if (chroma >= CX18_AV_SVIDEO_CHROMA7) {
 392                        reg &= 0x3f;
 393                        reg |= (chroma - CX18_AV_SVIDEO_CHROMA7) >> 2;
 394                } else {
 395                        reg &= 0xcf;
 396                        reg |= (chroma - CX18_AV_SVIDEO_CHROMA4) >> 4;
 397                }
 398        }
 399
 400        switch (aud_input) {
 401        case CX18_AV_AUDIO_SERIAL1:
 402        case CX18_AV_AUDIO_SERIAL2:
 403                /* do nothing, use serial audio input */
 404                break;
 405        case CX18_AV_AUDIO4: reg &= ~0x30; break;
 406        case CX18_AV_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
 407        case CX18_AV_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
 408        case CX18_AV_AUDIO7: reg &= ~0xc0; break;
 409        case CX18_AV_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
 410
 411        default:
 412                CX18_ERR("0x%04x is not a valid audio input!\n", aud_input);
 413                return -EINVAL;
 414        }
 415
 416        cx18_av_write(cx, 0x103, reg);
 417        /* Set INPUT_MODE to Composite (0) or S-Video (1) */
 418        cx18_av_and_or(cx, 0x401, ~0x6, is_composite ? 0 : 0x02);
 419        /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
 420        cx18_av_and_or(cx, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
 421        /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */
 422        if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
 423                cx18_av_and_or(cx, 0x102, ~0x4, 4);
 424        else
 425                cx18_av_and_or(cx, 0x102, ~0x4, 0);
 426        /*cx18_av_and_or4(cx, 0x104, ~0x001b4180, 0x00004180);*/
 427
 428        state->vid_input = vid_input;
 429        state->aud_input = aud_input;
 430        cx18_av_audio_set_path(cx);
 431        input_change(cx);
 432        return 0;
 433}
 434
 435/* ----------------------------------------------------------------------- */
 436
 437static int set_v4lstd(struct cx18 *cx)
 438{
 439        struct cx18_av_state *state = &cx->av_state;
 440        u8 fmt = 0;     /* zero is autodetect */
 441        u8 pal_m = 0;
 442
 443        /* First tests should be against specific std */
 444        if (state->std == V4L2_STD_NTSC_M_JP) {
 445                fmt = 0x2;
 446        } else if (state->std == V4L2_STD_NTSC_443) {
 447                fmt = 0x3;
 448        } else if (state->std == V4L2_STD_PAL_M) {
 449                pal_m = 1;
 450                fmt = 0x5;
 451        } else if (state->std == V4L2_STD_PAL_N) {
 452                fmt = 0x6;
 453        } else if (state->std == V4L2_STD_PAL_Nc) {
 454                fmt = 0x7;
 455        } else if (state->std == V4L2_STD_PAL_60) {
 456                fmt = 0x8;
 457        } else {
 458                /* Then, test against generic ones */
 459                if (state->std & V4L2_STD_NTSC)
 460                        fmt = 0x1;
 461                else if (state->std & V4L2_STD_PAL)
 462                        fmt = 0x4;
 463                else if (state->std & V4L2_STD_SECAM)
 464                        fmt = 0xc;
 465        }
 466
 467        CX18_DEBUG_INFO("changing video std to fmt %i\n", fmt);
 468
 469        /* Follow step 9 of section 3.16 in the cx18_av datasheet.
 470           Without this PAL may display a vertical ghosting effect.
 471           This happens for example with the Yuan MPC622. */
 472        if (fmt >= 4 && fmt < 8) {
 473                /* Set format to NTSC-M */
 474                cx18_av_and_or(cx, 0x400, ~0xf, 1);
 475                /* Turn off LCOMB */
 476                cx18_av_and_or(cx, 0x47b, ~6, 0);
 477        }
 478        cx18_av_and_or(cx, 0x400, ~0x2f, fmt | 0x20);
 479        cx18_av_and_or(cx, 0x403, ~0x3, pal_m);
 480        cx18_av_std_setup(cx);
 481        input_change(cx);
 482        return 0;
 483}
 484
 485/* ----------------------------------------------------------------------- */
 486
 487static int set_v4lctrl(struct cx18 *cx, struct v4l2_control *ctrl)
 488{
 489        switch (ctrl->id) {
 490        case V4L2_CID_BRIGHTNESS:
 491                if (ctrl->value < 0 || ctrl->value > 255) {
 492                        CX18_ERR("invalid brightness setting %d\n",
 493                                    ctrl->value);
 494                        return -ERANGE;
 495                }
 496
 497                cx18_av_write(cx, 0x414, ctrl->value - 128);
 498                break;
 499
 500        case V4L2_CID_CONTRAST:
 501                if (ctrl->value < 0 || ctrl->value > 127) {
 502                        CX18_ERR("invalid contrast setting %d\n",
 503                                    ctrl->value);
 504                        return -ERANGE;
 505                }
 506
 507                cx18_av_write(cx, 0x415, ctrl->value << 1);
 508                break;
 509
 510        case V4L2_CID_SATURATION:
 511                if (ctrl->value < 0 || ctrl->value > 127) {
 512                        CX18_ERR("invalid saturation setting %d\n",
 513                                    ctrl->value);
 514                        return -ERANGE;
 515                }
 516
 517                cx18_av_write(cx, 0x420, ctrl->value << 1);
 518                cx18_av_write(cx, 0x421, ctrl->value << 1);
 519                break;
 520
 521        case V4L2_CID_HUE:
 522                if (ctrl->value < -127 || ctrl->value > 127) {
 523                        CX18_ERR("invalid hue setting %d\n", ctrl->value);
 524                        return -ERANGE;
 525                }
 526
 527                cx18_av_write(cx, 0x422, ctrl->value);
 528                break;
 529
 530        case V4L2_CID_AUDIO_VOLUME:
 531        case V4L2_CID_AUDIO_BASS:
 532        case V4L2_CID_AUDIO_TREBLE:
 533        case V4L2_CID_AUDIO_BALANCE:
 534        case V4L2_CID_AUDIO_MUTE:
 535                return cx18_av_audio(cx, VIDIOC_S_CTRL, ctrl);
 536
 537        default:
 538                return -EINVAL;
 539        }
 540
 541        return 0;
 542}
 543
 544static int get_v4lctrl(struct cx18 *cx, struct v4l2_control *ctrl)
 545{
 546        switch (ctrl->id) {
 547        case V4L2_CID_BRIGHTNESS:
 548                ctrl->value = (s8)cx18_av_read(cx, 0x414) + 128;
 549                break;
 550        case V4L2_CID_CONTRAST:
 551                ctrl->value = cx18_av_read(cx, 0x415) >> 1;
 552                break;
 553        case V4L2_CID_SATURATION:
 554                ctrl->value = cx18_av_read(cx, 0x420) >> 1;
 555                break;
 556        case V4L2_CID_HUE:
 557                ctrl->value = (s8)cx18_av_read(cx, 0x422);
 558                break;
 559        case V4L2_CID_AUDIO_VOLUME:
 560        case V4L2_CID_AUDIO_BASS:
 561        case V4L2_CID_AUDIO_TREBLE:
 562        case V4L2_CID_AUDIO_BALANCE:
 563        case V4L2_CID_AUDIO_MUTE:
 564                return cx18_av_audio(cx, VIDIOC_G_CTRL, ctrl);
 565        default:
 566                return -EINVAL;
 567        }
 568
 569        return 0;
 570}
 571
 572/* ----------------------------------------------------------------------- */
 573
 574static int get_v4lfmt(struct cx18 *cx, struct v4l2_format *fmt)
 575{
 576        switch (fmt->type) {
 577        case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
 578                return cx18_av_vbi(cx, VIDIOC_G_FMT, fmt);
 579        default:
 580                return -EINVAL;
 581        }
 582
 583        return 0;
 584}
 585
 586static int set_v4lfmt(struct cx18 *cx, struct v4l2_format *fmt)
 587{
 588        struct cx18_av_state *state = &cx->av_state;
 589        struct v4l2_pix_format *pix;
 590        int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
 591        int is_50Hz = !(state->std & V4L2_STD_525_60);
 592
 593        switch (fmt->type) {
 594        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 595                pix = &(fmt->fmt.pix);
 596
 597                Vsrc = (cx18_av_read(cx, 0x476) & 0x3f) << 4;
 598                Vsrc |= (cx18_av_read(cx, 0x475) & 0xf0) >> 4;
 599
 600                Hsrc = (cx18_av_read(cx, 0x472) & 0x3f) << 4;
 601                Hsrc |= (cx18_av_read(cx, 0x471) & 0xf0) >> 4;
 602
 603                Vlines = pix->height + (is_50Hz ? 4 : 7);
 604
 605                if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
 606                    (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
 607                        CX18_ERR("%dx%d is not a valid size!\n",
 608                                    pix->width, pix->height);
 609                        return -ERANGE;
 610                }
 611
 612                HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
 613                VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
 614                VSC &= 0x1fff;
 615
 616                if (pix->width >= 385)
 617                        filter = 0;
 618                else if (pix->width > 192)
 619                        filter = 1;
 620                else if (pix->width > 96)
 621                        filter = 2;
 622                else
 623                        filter = 3;
 624
 625                CX18_DEBUG_INFO("decoder set size %dx%d -> scale  %ux%u\n",
 626                            pix->width, pix->height, HSC, VSC);
 627
 628                /* HSCALE=HSC */
 629                cx18_av_write(cx, 0x418, HSC & 0xff);
 630                cx18_av_write(cx, 0x419, (HSC >> 8) & 0xff);
 631                cx18_av_write(cx, 0x41a, HSC >> 16);
 632                /* VSCALE=VSC */
 633                cx18_av_write(cx, 0x41c, VSC & 0xff);
 634                cx18_av_write(cx, 0x41d, VSC >> 8);
 635                /* VS_INTRLACE=1 VFILT=filter */
 636                cx18_av_write(cx, 0x41e, 0x8 | filter);
 637                break;
 638
 639        case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
 640                return cx18_av_vbi(cx, VIDIOC_S_FMT, fmt);
 641
 642        case V4L2_BUF_TYPE_VBI_CAPTURE:
 643                return cx18_av_vbi(cx, VIDIOC_S_FMT, fmt);
 644
 645        default:
 646                return -EINVAL;
 647        }
 648
 649        return 0;
 650}
 651
 652/* ----------------------------------------------------------------------- */
 653
 654int cx18_av_cmd(struct cx18 *cx, unsigned int cmd, void *arg)
 655{
 656        struct cx18_av_state *state = &cx->av_state;
 657        struct v4l2_tuner *vt = arg;
 658        struct v4l2_routing *route = arg;
 659
 660        /* ignore these commands */
 661        switch (cmd) {
 662        case TUNER_SET_TYPE_ADDR:
 663                return 0;
 664        }
 665
 666        if (!state->is_initialized) {
 667                CX18_DEBUG_INFO("cmd %08x triggered fw load\n", cmd);
 668                /* initialize on first use */
 669                state->is_initialized = 1;
 670                cx18_av_initialize(cx);
 671        }
 672
 673        switch (cmd) {
 674        case VIDIOC_INT_DECODE_VBI_LINE:
 675                return cx18_av_vbi(cx, cmd, arg);
 676
 677        case VIDIOC_INT_AUDIO_CLOCK_FREQ:
 678                return cx18_av_audio(cx, cmd, arg);
 679
 680        case VIDIOC_STREAMON:
 681                CX18_DEBUG_INFO("enable output\n");
 682                cx18_av_write(cx, 0x115, 0x8c);
 683                cx18_av_write(cx, 0x116, 0x07);
 684                break;
 685
 686        case VIDIOC_STREAMOFF:
 687                CX18_DEBUG_INFO("disable output\n");
 688                cx18_av_write(cx, 0x115, 0x00);
 689                cx18_av_write(cx, 0x116, 0x00);
 690                break;
 691
 692        case VIDIOC_LOG_STATUS:
 693                log_video_status(cx);
 694                log_audio_status(cx);
 695                break;
 696
 697        case VIDIOC_G_CTRL:
 698                return get_v4lctrl(cx, (struct v4l2_control *)arg);
 699
 700        case VIDIOC_S_CTRL:
 701                return set_v4lctrl(cx, (struct v4l2_control *)arg);
 702
 703        case VIDIOC_QUERYCTRL:
 704        {
 705                struct v4l2_queryctrl *qc = arg;
 706
 707                switch (qc->id) {
 708                case V4L2_CID_BRIGHTNESS:
 709                case V4L2_CID_CONTRAST:
 710                case V4L2_CID_SATURATION:
 711                case V4L2_CID_HUE:
 712                        return v4l2_ctrl_query_fill_std(qc);
 713                default:
 714                        break;
 715                }
 716
 717                switch (qc->id) {
 718                case V4L2_CID_AUDIO_VOLUME:
 719                        return v4l2_ctrl_query_fill(qc, 0, 65535,
 720                                65535 / 100, state->default_volume);
 721                case V4L2_CID_AUDIO_MUTE:
 722                case V4L2_CID_AUDIO_BALANCE:
 723                case V4L2_CID_AUDIO_BASS:
 724                case V4L2_CID_AUDIO_TREBLE:
 725                        return v4l2_ctrl_query_fill_std(qc);
 726                default:
 727                        return -EINVAL;
 728                }
 729                return -EINVAL;
 730        }
 731
 732        case VIDIOC_G_STD:
 733                *(v4l2_std_id *)arg = state->std;
 734                break;
 735
 736        case VIDIOC_S_STD:
 737                if (state->radio == 0 && state->std == *(v4l2_std_id *)arg)
 738                        return 0;
 739                state->radio = 0;
 740                state->std = *(v4l2_std_id *)arg;
 741                return set_v4lstd(cx);
 742
 743        case AUDC_SET_RADIO:
 744                state->radio = 1;
 745                break;
 746
 747        case VIDIOC_INT_G_VIDEO_ROUTING:
 748                route->input = state->vid_input;
 749                route->output = 0;
 750                break;
 751
 752        case VIDIOC_INT_S_VIDEO_ROUTING:
 753                return set_input(cx, route->input, state->aud_input);
 754
 755        case VIDIOC_INT_G_AUDIO_ROUTING:
 756                route->input = state->aud_input;
 757                route->output = 0;
 758                break;
 759
 760        case VIDIOC_INT_S_AUDIO_ROUTING:
 761                return set_input(cx, state->vid_input, route->input);
 762
 763        case VIDIOC_S_FREQUENCY:
 764                input_change(cx);
 765                break;
 766
 767        case VIDIOC_G_TUNER:
 768        {
 769                u8 vpres = cx18_av_read(cx, 0x40e) & 0x20;
 770                u8 mode;
 771                int val = 0;
 772
 773                if (state->radio)
 774                        break;
 775
 776                vt->signal = vpres ? 0xffff : 0x0;
 777
 778                vt->capability |=
 779                    V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
 780                    V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
 781
 782                mode = cx18_av_read(cx, 0x804);
 783
 784                /* get rxsubchans and audmode */
 785                if ((mode & 0xf) == 1)
 786                        val |= V4L2_TUNER_SUB_STEREO;
 787                else
 788                        val |= V4L2_TUNER_SUB_MONO;
 789
 790                if (mode == 2 || mode == 4)
 791                        val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
 792
 793                if (mode & 0x10)
 794                        val |= V4L2_TUNER_SUB_SAP;
 795
 796                vt->rxsubchans = val;
 797                vt->audmode = state->audmode;
 798                break;
 799        }
 800
 801        case VIDIOC_S_TUNER:
 802                if (state->radio)
 803                        break;
 804
 805                switch (vt->audmode) {
 806                case V4L2_TUNER_MODE_MONO:
 807                        /* mono      -> mono
 808                           stereo    -> mono
 809                           bilingual -> lang1 */
 810                        cx18_av_and_or(cx, 0x809, ~0xf, 0x00);
 811                        break;
 812                case V4L2_TUNER_MODE_STEREO:
 813                case V4L2_TUNER_MODE_LANG1:
 814                        /* mono      -> mono
 815                           stereo    -> stereo
 816                           bilingual -> lang1 */
 817                        cx18_av_and_or(cx, 0x809, ~0xf, 0x04);
 818                        break;
 819                case V4L2_TUNER_MODE_LANG1_LANG2:
 820                        /* mono      -> mono
 821                           stereo    -> stereo
 822                           bilingual -> lang1/lang2 */
 823                        cx18_av_and_or(cx, 0x809, ~0xf, 0x07);
 824                        break;
 825                case V4L2_TUNER_MODE_LANG2:
 826                        /* mono      -> mono
 827                           stereo    -> stereo
 828                           bilingual -> lang2 */
 829                        cx18_av_and_or(cx, 0x809, ~0xf, 0x01);
 830                        break;
 831                default:
 832                        return -EINVAL;
 833                }
 834                state->audmode = vt->audmode;
 835                break;
 836
 837        case VIDIOC_G_FMT:
 838                return get_v4lfmt(cx, (struct v4l2_format *)arg);
 839
 840        case VIDIOC_S_FMT:
 841                return set_v4lfmt(cx, (struct v4l2_format *)arg);
 842
 843        case VIDIOC_INT_RESET:
 844                cx18_av_initialize(cx);
 845                break;
 846
 847        default:
 848                return -EINVAL;
 849        }
 850
 851        return 0;
 852}
 853
 854/* ----------------------------------------------------------------------- */
 855
 856/* ----------------------------------------------------------------------- */
 857
 858static void log_video_status(struct cx18 *cx)
 859{
 860        static const char *const fmt_strs[] = {
 861                "0x0",
 862                "NTSC-M", "NTSC-J", "NTSC-4.43",
 863                "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
 864                "0x9", "0xA", "0xB",
 865                "SECAM",
 866                "0xD", "0xE", "0xF"
 867        };
 868
 869        struct cx18_av_state *state = &cx->av_state;
 870        u8 vidfmt_sel = cx18_av_read(cx, 0x400) & 0xf;
 871        u8 gen_stat1 = cx18_av_read(cx, 0x40d);
 872        u8 gen_stat2 = cx18_av_read(cx, 0x40e);
 873        int vid_input = state->vid_input;
 874
 875        CX18_INFO("Video signal:              %spresent\n",
 876                    (gen_stat2 & 0x20) ? "" : "not ");
 877        CX18_INFO("Detected format:           %s\n",
 878                    fmt_strs[gen_stat1 & 0xf]);
 879
 880        CX18_INFO("Specified standard:        %s\n",
 881                    vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
 882
 883        if (vid_input >= CX18_AV_COMPOSITE1 &&
 884            vid_input <= CX18_AV_COMPOSITE8) {
 885                CX18_INFO("Specified video input:     Composite %d\n",
 886                        vid_input - CX18_AV_COMPOSITE1 + 1);
 887        } else {
 888                CX18_INFO("Specified video input:     S-Video (Luma In%d, Chroma In%d)\n",
 889                        (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
 890        }
 891
 892        CX18_INFO("Specified audioclock freq: %d Hz\n", state->audclk_freq);
 893}
 894
 895/* ----------------------------------------------------------------------- */
 896
 897static void log_audio_status(struct cx18 *cx)
 898{
 899        struct cx18_av_state *state = &cx->av_state;
 900        u8 download_ctl = cx18_av_read(cx, 0x803);
 901        u8 mod_det_stat0 = cx18_av_read(cx, 0x804);
 902        u8 mod_det_stat1 = cx18_av_read(cx, 0x805);
 903        u8 audio_config = cx18_av_read(cx, 0x808);
 904        u8 pref_mode = cx18_av_read(cx, 0x809);
 905        u8 afc0 = cx18_av_read(cx, 0x80b);
 906        u8 mute_ctl = cx18_av_read(cx, 0x8d3);
 907        int aud_input = state->aud_input;
 908        char *p;
 909
 910        switch (mod_det_stat0) {
 911        case 0x00: p = "mono"; break;
 912        case 0x01: p = "stereo"; break;
 913        case 0x02: p = "dual"; break;
 914        case 0x04: p = "tri"; break;
 915        case 0x10: p = "mono with SAP"; break;
 916        case 0x11: p = "stereo with SAP"; break;
 917        case 0x12: p = "dual with SAP"; break;
 918        case 0x14: p = "tri with SAP"; break;
 919        case 0xfe: p = "forced mode"; break;
 920        default: p = "not defined"; break;
 921        }
 922        CX18_INFO("Detected audio mode:       %s\n", p);
 923
 924        switch (mod_det_stat1) {
 925        case 0x00: p = "not defined"; break;
 926        case 0x01: p = "EIAJ"; break;
 927        case 0x02: p = "A2-M"; break;
 928        case 0x03: p = "A2-BG"; break;
 929        case 0x04: p = "A2-DK1"; break;
 930        case 0x05: p = "A2-DK2"; break;
 931        case 0x06: p = "A2-DK3"; break;
 932        case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
 933        case 0x08: p = "AM-L"; break;
 934        case 0x09: p = "NICAM-BG"; break;
 935        case 0x0a: p = "NICAM-DK"; break;
 936        case 0x0b: p = "NICAM-I"; break;
 937        case 0x0c: p = "NICAM-L"; break;
 938        case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
 939        case 0x0e: p = "IF FM Radio"; break;
 940        case 0x0f: p = "BTSC"; break;
 941        case 0x10: p = "detected chrominance"; break;
 942        case 0xfd: p = "unknown audio standard"; break;
 943        case 0xfe: p = "forced audio standard"; break;
 944        case 0xff: p = "no detected audio standard"; break;
 945        default: p = "not defined"; break;
 946        }
 947        CX18_INFO("Detected audio standard:   %s\n", p);
 948        CX18_INFO("Audio muted:               %s\n",
 949                    (mute_ctl & 0x2) ? "yes" : "no");
 950        CX18_INFO("Audio microcontroller:     %s\n",
 951                    (download_ctl & 0x10) ? "running" : "stopped");
 952
 953        switch (audio_config >> 4) {
 954        case 0x00: p = "undefined"; break;
 955        case 0x01: p = "BTSC"; break;
 956        case 0x02: p = "EIAJ"; break;
 957        case 0x03: p = "A2-M"; break;
 958        case 0x04: p = "A2-BG"; break;
 959        case 0x05: p = "A2-DK1"; break;
 960        case 0x06: p = "A2-DK2"; break;
 961        case 0x07: p = "A2-DK3"; break;
 962        case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
 963        case 0x09: p = "AM-L"; break;
 964        case 0x0a: p = "NICAM-BG"; break;
 965        case 0x0b: p = "NICAM-DK"; break;
 966        case 0x0c: p = "NICAM-I"; break;
 967        case 0x0d: p = "NICAM-L"; break;
 968        case 0x0e: p = "FM radio"; break;
 969        case 0x0f: p = "automatic detection"; break;
 970        default: p = "undefined"; break;
 971        }
 972        CX18_INFO("Configured audio standard: %s\n", p);
 973
 974        if ((audio_config >> 4) < 0xF) {
 975                switch (audio_config & 0xF) {
 976                case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
 977                case 0x01: p = "MONO2 (LANGUAGE B)"; break;
 978                case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
 979                case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
 980                case 0x04: p = "STEREO"; break;
 981                case 0x05: p = "DUAL1 (AC)"; break;
 982                case 0x06: p = "DUAL2 (BC)"; break;
 983                case 0x07: p = "DUAL3 (AB)"; break;
 984                default: p = "undefined";
 985                }
 986                CX18_INFO("Configured audio mode:     %s\n", p);
 987        } else {
 988                switch (audio_config & 0xF) {
 989                case 0x00: p = "BG"; break;
 990                case 0x01: p = "DK1"; break;
 991                case 0x02: p = "DK2"; break;
 992                case 0x03: p = "DK3"; break;
 993                case 0x04: p = "I"; break;
 994                case 0x05: p = "L"; break;
 995                case 0x06: p = "BTSC"; break;
 996                case 0x07: p = "EIAJ"; break;
 997                case 0x08: p = "A2-M"; break;
 998                case 0x09: p = "FM Radio (4.5 MHz)"; break;
 999                case 0x0a: p = "FM Radio (5.5 MHz)"; break;
1000                case 0x0b: p = "S-Video"; break;
1001                case 0x0f: p = "automatic standard and mode detection"; break;
1002                default: p = "undefined"; break;
1003                }
1004                CX18_INFO("Configured audio system:   %s\n", p);
1005        }
1006
1007        if (aud_input)
1008                CX18_INFO("Specified audio input:     Tuner (In%d)\n",
1009                                aud_input);
1010        else
1011                CX18_INFO("Specified audio input:     External\n");
1012
1013        switch (pref_mode & 0xf) {
1014        case 0: p = "mono/language A"; break;
1015        case 1: p = "language B"; break;
1016        case 2: p = "language C"; break;
1017        case 3: p = "analog fallback"; break;
1018        case 4: p = "stereo"; break;
1019        case 5: p = "language AC"; break;
1020        case 6: p = "language BC"; break;
1021        case 7: p = "language AB"; break;
1022        default: p = "undefined"; break;
1023        }
1024        CX18_INFO("Preferred audio mode:      %s\n", p);
1025
1026        if ((audio_config & 0xf) == 0xf) {
1027                switch ((afc0 >> 3) & 0x1) {
1028                case 0: p = "system DK"; break;
1029                case 1: p = "system L"; break;
1030                }
1031                CX18_INFO("Selected 65 MHz format:    %s\n", p);
1032
1033                switch (afc0 & 0x7) {
1034                case 0: p = "Chroma"; break;
1035                case 1: p = "BTSC"; break;
1036                case 2: p = "EIAJ"; break;
1037                case 3: p = "A2-M"; break;
1038                case 4: p = "autodetect"; break;
1039                default: p = "undefined"; break;
1040                }
1041                CX18_INFO("Selected 45 MHz format:    %s\n", p);
1042        }
1043}
1044
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.