linux/drivers/media/i2c/cx25840/cx25840-core.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/* cx25840 - Conexant CX25840 audio/video decoder driver
   3 *
   4 * Copyright (C) 2004 Ulf Eklund
   5 *
   6 * Based on the saa7115 driver and on the first version of Chris Kennedy's
   7 * cx25840 driver.
   8 *
   9 * Changes by Tyler Trafford <tatrafford@comcast.net>
  10 *    - cleanup/rewrite for V4L2 API (2005)
  11 *
  12 * VBI support by Hans Verkuil <hverkuil@xs4all.nl>.
  13 *
  14 * NTSC sliced VBI support by Christopher Neufeld <television@cneufeld.ca>
  15 * with additional fixes by Hans Verkuil <hverkuil@xs4all.nl>.
  16 *
  17 * CX23885 support by Steven Toth <stoth@linuxtv.org>.
  18 *
  19 * CX2388[578] IRQ handling, IO Pin mux configuration and other small fixes are
  20 * Copyright (C) 2010 Andy Walls <awalls@md.metrocast.net>
  21 *
  22 * CX23888 DIF support for the HVR1850
  23 * Copyright (C) 2011 Steven Toth <stoth@kernellabs.com>
  24 *
  25 * CX2584x pin to pad mapping and output format configuration support are
  26 * Copyright (C) 2011 Maciej S. Szmigiero <mail@maciej.szmigiero.name>
  27 */
  28
  29#include <linux/kernel.h>
  30#include <linux/module.h>
  31#include <linux/slab.h>
  32#include <linux/videodev2.h>
  33#include <linux/i2c.h>
  34#include <linux/delay.h>
  35#include <linux/math64.h>
  36#include <media/v4l2-common.h>
  37#include <media/drv-intf/cx25840.h>
  38
  39#include "cx25840-core.h"
  40
  41MODULE_DESCRIPTION("Conexant CX25840 audio/video decoder driver");
  42MODULE_AUTHOR("Ulf Eklund, Chris Kennedy, Hans Verkuil, Tyler Trafford");
  43MODULE_LICENSE("GPL");
  44
  45#define CX25840_VID_INT_STAT_REG 0x410
  46#define CX25840_VID_INT_STAT_BITS 0x0000ffff
  47#define CX25840_VID_INT_MASK_BITS 0xffff0000
  48#define CX25840_VID_INT_MASK_SHFT 16
  49#define CX25840_VID_INT_MASK_REG 0x412
  50
  51#define CX23885_AUD_MC_INT_MASK_REG 0x80c
  52#define CX23885_AUD_MC_INT_STAT_BITS 0xffff0000
  53#define CX23885_AUD_MC_INT_CTRL_BITS 0x0000ffff
  54#define CX23885_AUD_MC_INT_STAT_SHFT 16
  55
  56#define CX25840_AUD_INT_CTRL_REG 0x812
  57#define CX25840_AUD_INT_STAT_REG 0x813
  58
  59#define CX23885_PIN_CTRL_IRQ_REG 0x123
  60#define CX23885_PIN_CTRL_IRQ_IR_STAT  0x40
  61#define CX23885_PIN_CTRL_IRQ_AUD_STAT 0x20
  62#define CX23885_PIN_CTRL_IRQ_VID_STAT 0x10
  63
  64#define CX25840_IR_STATS_REG    0x210
  65#define CX25840_IR_IRQEN_REG    0x214
  66
  67static int cx25840_debug;
  68
  69module_param_named(debug, cx25840_debug, int, 0644);
  70
  71MODULE_PARM_DESC(debug, "Debugging messages [0=Off (default) 1=On]");
  72
  73/* ----------------------------------------------------------------------- */
  74static void cx23888_std_setup(struct i2c_client *client);
  75
  76int cx25840_write(struct i2c_client *client, u16 addr, u8 value)
  77{
  78        u8 buffer[3];
  79
  80        buffer[0] = addr >> 8;
  81        buffer[1] = addr & 0xff;
  82        buffer[2] = value;
  83        return i2c_master_send(client, buffer, 3);
  84}
  85
  86int cx25840_write4(struct i2c_client *client, u16 addr, u32 value)
  87{
  88        u8 buffer[6];
  89
  90        buffer[0] = addr >> 8;
  91        buffer[1] = addr & 0xff;
  92        buffer[2] = value & 0xff;
  93        buffer[3] = (value >> 8) & 0xff;
  94        buffer[4] = (value >> 16) & 0xff;
  95        buffer[5] = value >> 24;
  96        return i2c_master_send(client, buffer, 6);
  97}
  98
  99u8 cx25840_read(struct i2c_client *client, u16 addr)
 100{
 101        struct i2c_msg msgs[2];
 102        u8 tx_buf[2], rx_buf[1];
 103
 104        /* Write register address */
 105        tx_buf[0] = addr >> 8;
 106        tx_buf[1] = addr & 0xff;
 107        msgs[0].addr = client->addr;
 108        msgs[0].flags = 0;
 109        msgs[0].len = 2;
 110        msgs[0].buf = (char *)tx_buf;
 111
 112        /* Read data from register */
 113        msgs[1].addr = client->addr;
 114        msgs[1].flags = I2C_M_RD;
 115        msgs[1].len = 1;
 116        msgs[1].buf = (char *)rx_buf;
 117
 118        if (i2c_transfer(client->adapter, msgs, 2) < 2)
 119                return 0;
 120
 121        return rx_buf[0];
 122}
 123
 124u32 cx25840_read4(struct i2c_client *client, u16 addr)
 125{
 126        struct i2c_msg msgs[2];
 127        u8 tx_buf[2], rx_buf[4];
 128
 129        /* Write register address */
 130        tx_buf[0] = addr >> 8;
 131        tx_buf[1] = addr & 0xff;
 132        msgs[0].addr = client->addr;
 133        msgs[0].flags = 0;
 134        msgs[0].len = 2;
 135        msgs[0].buf = (char *)tx_buf;
 136
 137        /* Read data from registers */
 138        msgs[1].addr = client->addr;
 139        msgs[1].flags = I2C_M_RD;
 140        msgs[1].len = 4;
 141        msgs[1].buf = (char *)rx_buf;
 142
 143        if (i2c_transfer(client->adapter, msgs, 2) < 2)
 144                return 0;
 145
 146        return (rx_buf[3] << 24) | (rx_buf[2] << 16) | (rx_buf[1] << 8) |
 147                rx_buf[0];
 148}
 149
 150int cx25840_and_or(struct i2c_client *client, u16 addr, unsigned int and_mask,
 151                   u8 or_value)
 152{
 153        return cx25840_write(client, addr,
 154                             (cx25840_read(client, addr) & and_mask) |
 155                             or_value);
 156}
 157
 158int cx25840_and_or4(struct i2c_client *client, u16 addr, u32 and_mask,
 159                    u32 or_value)
 160{
 161        return cx25840_write4(client, addr,
 162                              (cx25840_read4(client, addr) & and_mask) |
 163                              or_value);
 164}
 165
 166/* ----------------------------------------------------------------------- */
 167
 168static int set_input(struct i2c_client *client,
 169                     enum cx25840_video_input vid_input,
 170                     enum cx25840_audio_input aud_input);
 171
 172/* ----------------------------------------------------------------------- */
 173
 174static int cx23885_s_io_pin_config(struct v4l2_subdev *sd, size_t n,
 175                                   struct v4l2_subdev_io_pin_config *p)
 176{
 177        struct i2c_client *client = v4l2_get_subdevdata(sd);
 178        int i;
 179        u32 pin_ctrl;
 180        u8 gpio_oe, gpio_data, strength;
 181
 182        pin_ctrl = cx25840_read4(client, 0x120);
 183        gpio_oe = cx25840_read(client, 0x160);
 184        gpio_data = cx25840_read(client, 0x164);
 185
 186        for (i = 0; i < n; i++) {
 187                strength = p[i].strength;
 188                if (strength > CX25840_PIN_DRIVE_FAST)
 189                        strength = CX25840_PIN_DRIVE_FAST;
 190
 191                switch (p[i].pin) {
 192                case CX23885_PIN_IRQ_N_GPIO16:
 193                        if (p[i].function != CX23885_PAD_IRQ_N) {
 194                                /* GPIO16 */
 195                                pin_ctrl &= ~(0x1 << 25);
 196                        } else {
 197                                /* IRQ_N */
 198                                if (p[i].flags &
 199                                        (BIT(V4L2_SUBDEV_IO_PIN_DISABLE) |
 200                                         BIT(V4L2_SUBDEV_IO_PIN_INPUT))) {
 201                                        pin_ctrl &= ~(0x1 << 25);
 202                                } else {
 203                                        pin_ctrl |= (0x1 << 25);
 204                                }
 205                                if (p[i].flags &
 206                                        BIT(V4L2_SUBDEV_IO_PIN_ACTIVE_LOW)) {
 207                                        pin_ctrl &= ~(0x1 << 24);
 208                                } else {
 209                                        pin_ctrl |= (0x1 << 24);
 210                                }
 211                        }
 212                        break;
 213                case CX23885_PIN_IR_RX_GPIO19:
 214                        if (p[i].function != CX23885_PAD_GPIO19) {
 215                                /* IR_RX */
 216                                gpio_oe |= (0x1 << 0);
 217                                pin_ctrl &= ~(0x3 << 18);
 218                                pin_ctrl |= (strength << 18);
 219                        } else {
 220                                /* GPIO19 */
 221                                gpio_oe &= ~(0x1 << 0);
 222                                if (p[i].flags & BIT(V4L2_SUBDEV_IO_PIN_SET_VALUE)) {
 223                                        gpio_data &= ~(0x1 << 0);
 224                                        gpio_data |= ((p[i].value & 0x1) << 0);
 225                                }
 226                                pin_ctrl &= ~(0x3 << 12);
 227                                pin_ctrl |= (strength << 12);
 228                        }
 229                        break;
 230                case CX23885_PIN_IR_TX_GPIO20:
 231                        if (p[i].function != CX23885_PAD_GPIO20) {
 232                                /* IR_TX */
 233                                gpio_oe |= (0x1 << 1);
 234                                if (p[i].flags & BIT(V4L2_SUBDEV_IO_PIN_DISABLE))
 235                                        pin_ctrl &= ~(0x1 << 10);
 236                                else
 237                                        pin_ctrl |= (0x1 << 10);
 238                                pin_ctrl &= ~(0x3 << 18);
 239                                pin_ctrl |= (strength << 18);
 240                        } else {
 241                                /* GPIO20 */
 242                                gpio_oe &= ~(0x1 << 1);
 243                                if (p[i].flags & BIT(V4L2_SUBDEV_IO_PIN_SET_VALUE)) {
 244                                        gpio_data &= ~(0x1 << 1);
 245                                        gpio_data |= ((p[i].value & 0x1) << 1);
 246                                }
 247                                pin_ctrl &= ~(0x3 << 12);
 248                                pin_ctrl |= (strength << 12);
 249                        }
 250                        break;
 251                case CX23885_PIN_I2S_SDAT_GPIO21:
 252                        if (p[i].function != CX23885_PAD_GPIO21) {
 253                                /* I2S_SDAT */
 254                                /* TODO: Input or Output config */
 255                                gpio_oe |= (0x1 << 2);
 256                                pin_ctrl &= ~(0x3 << 22);
 257                                pin_ctrl |= (strength << 22);
 258                        } else {
 259                                /* GPIO21 */
 260                                gpio_oe &= ~(0x1 << 2);
 261                                if (p[i].flags & BIT(V4L2_SUBDEV_IO_PIN_SET_VALUE)) {
 262                                        gpio_data &= ~(0x1 << 2);
 263                                        gpio_data |= ((p[i].value & 0x1) << 2);
 264                                }
 265                                pin_ctrl &= ~(0x3 << 12);
 266                                pin_ctrl |= (strength << 12);
 267                        }
 268                        break;
 269                case CX23885_PIN_I2S_WCLK_GPIO22:
 270                        if (p[i].function != CX23885_PAD_GPIO22) {
 271                                /* I2S_WCLK */
 272                                /* TODO: Input or Output config */
 273                                gpio_oe |= (0x1 << 3);
 274                                pin_ctrl &= ~(0x3 << 22);
 275                                pin_ctrl |= (strength << 22);
 276                        } else {
 277                                /* GPIO22 */
 278                                gpio_oe &= ~(0x1 << 3);
 279                                if (p[i].flags & BIT(V4L2_SUBDEV_IO_PIN_SET_VALUE)) {
 280                                        gpio_data &= ~(0x1 << 3);
 281                                        gpio_data |= ((p[i].value & 0x1) << 3);
 282                                }
 283                                pin_ctrl &= ~(0x3 << 12);
 284                                pin_ctrl |= (strength << 12);
 285                        }
 286                        break;
 287                case CX23885_PIN_I2S_BCLK_GPIO23:
 288                        if (p[i].function != CX23885_PAD_GPIO23) {
 289                                /* I2S_BCLK */
 290                                /* TODO: Input or Output config */
 291                                gpio_oe |= (0x1 << 4);
 292                                pin_ctrl &= ~(0x3 << 22);
 293                                pin_ctrl |= (strength << 22);
 294                        } else {
 295                                /* GPIO23 */
 296                                gpio_oe &= ~(0x1 << 4);
 297                                if (p[i].flags & BIT(V4L2_SUBDEV_IO_PIN_SET_VALUE)) {
 298                                        gpio_data &= ~(0x1 << 4);
 299                                        gpio_data |= ((p[i].value & 0x1) << 4);
 300                                }
 301                                pin_ctrl &= ~(0x3 << 12);
 302                                pin_ctrl |= (strength << 12);
 303                        }
 304                        break;
 305                }
 306        }
 307
 308        cx25840_write(client, 0x164, gpio_data);
 309        cx25840_write(client, 0x160, gpio_oe);
 310        cx25840_write4(client, 0x120, pin_ctrl);
 311        return 0;
 312}
 313
 314static u8 cx25840_function_to_pad(struct i2c_client *client, u8 function)
 315{
 316        if (function > CX25840_PAD_VRESET) {
 317                v4l_err(client, "invalid function %u, assuming default\n",
 318                        (unsigned int)function);
 319                return 0;
 320        }
 321
 322        return function;
 323}
 324
 325static void cx25840_set_invert(u8 *pinctrl3, u8 *voutctrl4, u8 function,
 326                               u8 pin, bool invert)
 327{
 328        switch (function) {
 329        case CX25840_PAD_IRQ_N:
 330                if (invert)
 331                        *pinctrl3 &= ~2;
 332                else
 333                        *pinctrl3 |= 2;
 334                break;
 335
 336        case CX25840_PAD_ACTIVE:
 337                if (invert)
 338                        *voutctrl4 |= BIT(2);
 339                else
 340                        *voutctrl4 &= ~BIT(2);
 341                break;
 342
 343        case CX25840_PAD_VACTIVE:
 344                if (invert)
 345                        *voutctrl4 |= BIT(5);
 346                else
 347                        *voutctrl4 &= ~BIT(5);
 348                break;
 349
 350        case CX25840_PAD_CBFLAG:
 351                if (invert)
 352                        *voutctrl4 |= BIT(4);
 353                else
 354                        *voutctrl4 &= ~BIT(4);
 355                break;
 356
 357        case CX25840_PAD_VRESET:
 358                if (invert)
 359                        *voutctrl4 |= BIT(0);
 360                else
 361                        *voutctrl4 &= ~BIT(0);
 362                break;
 363        }
 364
 365        if (function != CX25840_PAD_DEFAULT)
 366                return;
 367
 368        switch (pin) {
 369        case CX25840_PIN_DVALID_PRGM0:
 370                if (invert)
 371                        *voutctrl4 |= BIT(6);
 372                else
 373                        *voutctrl4 &= ~BIT(6);
 374                break;
 375
 376        case CX25840_PIN_HRESET_PRGM2:
 377                if (invert)
 378                        *voutctrl4 |= BIT(1);
 379                else
 380                        *voutctrl4 &= ~BIT(1);
 381                break;
 382        }
 383}
 384
 385static int cx25840_s_io_pin_config(struct v4l2_subdev *sd, size_t n,
 386                                   struct v4l2_subdev_io_pin_config *p)
 387{
 388        struct i2c_client *client = v4l2_get_subdevdata(sd);
 389        unsigned int i;
 390        u8 pinctrl[6], pinconf[10], voutctrl4;
 391
 392        for (i = 0; i < 6; i++)
 393                pinctrl[i] = cx25840_read(client, 0x114 + i);
 394
 395        for (i = 0; i < 10; i++)
 396                pinconf[i] = cx25840_read(client, 0x11c + i);
 397
 398        voutctrl4 = cx25840_read(client, 0x407);
 399
 400        for (i = 0; i < n; i++) {
 401                u8 strength = p[i].strength;
 402
 403                if (strength != CX25840_PIN_DRIVE_SLOW &&
 404                    strength != CX25840_PIN_DRIVE_MEDIUM &&
 405                    strength != CX25840_PIN_DRIVE_FAST) {
 406                        v4l_err(client,
 407                                "invalid drive speed for pin %u (%u), assuming fast\n",
 408                                (unsigned int)p[i].pin,
 409                                (unsigned int)strength);
 410
 411                        strength = CX25840_PIN_DRIVE_FAST;
 412                }
 413
 414                switch (p[i].pin) {
 415                case CX25840_PIN_DVALID_PRGM0:
 416                        if (p[i].flags & BIT(V4L2_SUBDEV_IO_PIN_DISABLE))
 417                                pinctrl[0] &= ~BIT(6);
 418                        else
 419                                pinctrl[0] |= BIT(6);
 420
 421                        pinconf[3] &= 0xf0;
 422                        pinconf[3] |= cx25840_function_to_pad(client,
 423                                                              p[i].function);
 424
 425                        cx25840_set_invert(&pinctrl[3], &voutctrl4,
 426                                           p[i].function,
 427                                           CX25840_PIN_DVALID_PRGM0,
 428                                           p[i].flags &
 429                                           BIT(V4L2_SUBDEV_IO_PIN_ACTIVE_LOW));
 430
 431                        pinctrl[4] &= ~(3 << 2); /* CX25840_PIN_DRIVE_MEDIUM */
 432                        switch (strength) {
 433                        case CX25840_PIN_DRIVE_SLOW:
 434                                pinctrl[4] |= 1 << 2;
 435                                break;
 436
 437                        case CX25840_PIN_DRIVE_FAST:
 438                                pinctrl[4] |= 2 << 2;
 439                                break;
 440                        }
 441
 442                        break;
 443
 444                case CX25840_PIN_HRESET_PRGM2:
 445                        if (p[i].flags & BIT(V4L2_SUBDEV_IO_PIN_DISABLE))
 446                                pinctrl[1] &= ~BIT(0);
 447                        else
 448                                pinctrl[1] |= BIT(0);
 449
 450                        pinconf[4] &= 0xf0;
 451                        pinconf[4] |= cx25840_function_to_pad(client,
 452                                                              p[i].function);
 453
 454                        cx25840_set_invert(&pinctrl[3], &voutctrl4,
 455                                           p[i].function,
 456                                           CX25840_PIN_HRESET_PRGM2,
 457                                           p[i].flags &
 458                                           BIT(V4L2_SUBDEV_IO_PIN_ACTIVE_LOW));
 459
 460                        pinctrl[4] &= ~(3 << 2); /* CX25840_PIN_DRIVE_MEDIUM */
 461                        switch (strength) {
 462                        case CX25840_PIN_DRIVE_SLOW:
 463                                pinctrl[4] |= 1 << 2;
 464                                break;
 465
 466                        case CX25840_PIN_DRIVE_FAST:
 467                                pinctrl[4] |= 2 << 2;
 468                                break;
 469                        }
 470
 471                        break;
 472
 473                case CX25840_PIN_PLL_CLK_PRGM7:
 474                        if (p[i].flags & BIT(V4L2_SUBDEV_IO_PIN_DISABLE))
 475                                pinctrl[2] &= ~BIT(2);
 476                        else
 477                                pinctrl[2] |= BIT(2);
 478
 479                        switch (p[i].function) {
 480                        case CX25840_PAD_XTI_X5_DLL:
 481                                pinconf[6] = 0;
 482                                break;
 483
 484                        case CX25840_PAD_AUX_PLL:
 485                                pinconf[6] = 1;
 486                                break;
 487
 488                        case CX25840_PAD_VID_PLL:
 489                                pinconf[6] = 5;
 490                                break;
 491
 492                        case CX25840_PAD_XTI:
 493                                pinconf[6] = 2;
 494                                break;
 495
 496                        default:
 497                                pinconf[6] = 3;
 498                                pinconf[6] |=
 499                                        cx25840_function_to_pad(client,
 500                                                                p[i].function)
 501                                        << 4;
 502                        }
 503
 504                        break;
 505
 506                default:
 507                        v4l_err(client, "invalid or unsupported pin %u\n",
 508                                (unsigned int)p[i].pin);
 509                        break;
 510                }
 511        }
 512
 513        cx25840_write(client, 0x407, voutctrl4);
 514
 515        for (i = 0; i < 6; i++)
 516                cx25840_write(client, 0x114 + i, pinctrl[i]);
 517
 518        for (i = 0; i < 10; i++)
 519                cx25840_write(client, 0x11c + i, pinconf[i]);
 520
 521        return 0;
 522}
 523
 524static int common_s_io_pin_config(struct v4l2_subdev *sd, size_t n,
 525                                  struct v4l2_subdev_io_pin_config *pincfg)
 526{
 527        struct cx25840_state *state = to_state(sd);
 528
 529        if (is_cx2388x(state))
 530                return cx23885_s_io_pin_config(sd, n, pincfg);
 531        else if (is_cx2584x(state))
 532                return cx25840_s_io_pin_config(sd, n, pincfg);
 533        return 0;
 534}
 535
 536/* ----------------------------------------------------------------------- */
 537
 538static void init_dll1(struct i2c_client *client)
 539{
 540        /*
 541         * This is the Hauppauge sequence used to
 542         * initialize the Delay Lock Loop 1 (ADC DLL).
 543         */
 544        cx25840_write(client, 0x159, 0x23);
 545        cx25840_write(client, 0x15a, 0x87);
 546        cx25840_write(client, 0x15b, 0x06);
 547        udelay(10);
 548        cx25840_write(client, 0x159, 0xe1);
 549        udelay(10);
 550        cx25840_write(client, 0x15a, 0x86);
 551        cx25840_write(client, 0x159, 0xe0);
 552        cx25840_write(client, 0x159, 0xe1);
 553        cx25840_write(client, 0x15b, 0x10);
 554}
 555
 556static void init_dll2(struct i2c_client *client)
 557{
 558        /*
 559         * This is the Hauppauge sequence used to
 560         * initialize the Delay Lock Loop 2 (ADC DLL).
 561         */
 562        cx25840_write(client, 0x15d, 0xe3);
 563        cx25840_write(client, 0x15e, 0x86);
 564        cx25840_write(client, 0x15f, 0x06);
 565        udelay(10);
 566        cx25840_write(client, 0x15d, 0xe1);
 567        cx25840_write(client, 0x15d, 0xe0);
 568        cx25840_write(client, 0x15d, 0xe1);
 569}
 570
 571static void cx25836_initialize(struct i2c_client *client)
 572{
 573        /*
 574         *reset configuration is described on page 3-77
 575         * of the CX25836 datasheet
 576         */
 577
 578        /* 2. */
 579        cx25840_and_or(client, 0x000, ~0x01, 0x01);
 580        cx25840_and_or(client, 0x000, ~0x01, 0x00);
 581        /* 3a. */
 582        cx25840_and_or(client, 0x15a, ~0x70, 0x00);
 583        /* 3b. */
 584        cx25840_and_or(client, 0x15b, ~0x1e, 0x06);
 585        /* 3c. */
 586        cx25840_and_or(client, 0x159, ~0x02, 0x02);
 587        /* 3d. */
 588        udelay(10);
 589        /* 3e. */
 590        cx25840_and_or(client, 0x159, ~0x02, 0x00);
 591        /* 3f. */
 592        cx25840_and_or(client, 0x159, ~0xc0, 0xc0);
 593        /* 3g. */
 594        cx25840_and_or(client, 0x159, ~0x01, 0x00);
 595        cx25840_and_or(client, 0x159, ~0x01, 0x01);
 596        /* 3h. */
 597        cx25840_and_or(client, 0x15b, ~0x1e, 0x10);
 598}
 599
 600static void cx25840_work_handler(struct work_struct *work)
 601{
 602        struct cx25840_state *state = container_of(work, struct cx25840_state, fw_work);
 603
 604        cx25840_loadfw(state->c);
 605        wake_up(&state->fw_wait);
 606}
 607
 608#define CX25840_VCONFIG_SET_BIT(state, opt_msk, voc, idx, bit, oneval)  \
 609        do {                                                            \
 610                if ((state)->vid_config & (opt_msk)) {                  \
 611                        if (((state)->vid_config & (opt_msk)) ==        \
 612                            (oneval))                                   \
 613                                (voc)[idx] |= BIT(bit);         \
 614                        else                                            \
 615                                (voc)[idx] &= ~BIT(bit);                \
 616                }                                                       \
 617        } while (0)
 618
 619/* apply current vconfig to hardware regs */
 620static void cx25840_vconfig_apply(struct i2c_client *client)
 621{
 622        struct cx25840_state *state = to_state(i2c_get_clientdata(client));
 623        u8 voutctrl[3];
 624        unsigned int i;
 625
 626        for (i = 0; i < 3; i++)
 627                voutctrl[i] = cx25840_read(client, 0x404 + i);
 628
 629        if (state->vid_config & CX25840_VCONFIG_FMT_MASK)
 630                voutctrl[0] &= ~3;
 631        switch (state->vid_config & CX25840_VCONFIG_FMT_MASK) {
 632        case CX25840_VCONFIG_FMT_BT656:
 633                voutctrl[0] |= 1;
 634                break;
 635
 636        case CX25840_VCONFIG_FMT_VIP11:
 637                voutctrl[0] |= 2;
 638                break;
 639
 640        case CX25840_VCONFIG_FMT_VIP2:
 641                voutctrl[0] |= 3;
 642                break;
 643
 644        case CX25840_VCONFIG_FMT_BT601:
 645                /* zero */
 646        default:
 647                break;
 648        }
 649
 650        CX25840_VCONFIG_SET_BIT(state, CX25840_VCONFIG_RES_MASK, voutctrl,
 651                                0, 2, CX25840_VCONFIG_RES_10BIT);
 652        CX25840_VCONFIG_SET_BIT(state, CX25840_VCONFIG_VBIRAW_MASK, voutctrl,
 653                                0, 3, CX25840_VCONFIG_VBIRAW_ENABLED);
 654        CX25840_VCONFIG_SET_BIT(state, CX25840_VCONFIG_ANCDATA_MASK, voutctrl,
 655                                0, 4, CX25840_VCONFIG_ANCDATA_ENABLED);
 656        CX25840_VCONFIG_SET_BIT(state, CX25840_VCONFIG_TASKBIT_MASK, voutctrl,
 657                                0, 5, CX25840_VCONFIG_TASKBIT_ONE);
 658        CX25840_VCONFIG_SET_BIT(state, CX25840_VCONFIG_ACTIVE_MASK, voutctrl,
 659                                1, 2, CX25840_VCONFIG_ACTIVE_HORIZONTAL);
 660        CX25840_VCONFIG_SET_BIT(state, CX25840_VCONFIG_VALID_MASK, voutctrl,
 661                                1, 3, CX25840_VCONFIG_VALID_ANDACTIVE);
 662        CX25840_VCONFIG_SET_BIT(state, CX25840_VCONFIG_HRESETW_MASK, voutctrl,
 663                                1, 4, CX25840_VCONFIG_HRESETW_PIXCLK);
 664
 665        if (state->vid_config & CX25840_VCONFIG_CLKGATE_MASK)
 666                voutctrl[1] &= ~(3 << 6);
 667        switch (state->vid_config & CX25840_VCONFIG_CLKGATE_MASK) {
 668        case CX25840_VCONFIG_CLKGATE_VALID:
 669                voutctrl[1] |= 2;
 670                break;
 671
 672        case CX25840_VCONFIG_CLKGATE_VALIDACTIVE:
 673                voutctrl[1] |= 3;
 674                break;
 675
 676        case CX25840_VCONFIG_CLKGATE_NONE:
 677                /* zero */
 678        default:
 679                break;
 680        }
 681
 682        CX25840_VCONFIG_SET_BIT(state, CX25840_VCONFIG_DCMODE_MASK, voutctrl,
 683                                2, 0, CX25840_VCONFIG_DCMODE_BYTES);
 684        CX25840_VCONFIG_SET_BIT(state, CX25840_VCONFIG_IDID0S_MASK, voutctrl,
 685                                2, 1, CX25840_VCONFIG_IDID0S_LINECNT);
 686        CX25840_VCONFIG_SET_BIT(state, CX25840_VCONFIG_VIPCLAMP_MASK, voutctrl,
 687                                2, 4, CX25840_VCONFIG_VIPCLAMP_ENABLED);
 688
 689        for (i = 0; i < 3; i++)
 690                cx25840_write(client, 0x404 + i, voutctrl[i]);
 691}
 692
 693static void cx25840_initialize(struct i2c_client *client)
 694{
 695        DEFINE_WAIT(wait);
 696        struct cx25840_state *state = to_state(i2c_get_clientdata(client));
 697        struct workqueue_struct *q;
 698
 699        /* datasheet startup in numbered steps, refer to page 3-77 */
 700        /* 2. */
 701        cx25840_and_or(client, 0x803, ~0x10, 0x00);
 702        /*
 703         * The default of this register should be 4, but I get 0 instead.
 704         * Set this register to 4 manually.
 705         */
 706        cx25840_write(client, 0x000, 0x04);
 707        /* 3. */
 708        init_dll1(client);
 709        init_dll2(client);
 710        cx25840_write(client, 0x136, 0x0a);
 711        /* 4. */
 712        cx25840_write(client, 0x13c, 0x01);
 713        cx25840_write(client, 0x13c, 0x00);
 714        /* 5. */
 715        /*
 716         * Do the firmware load in a work handler to prevent.
 717         * Otherwise the kernel is blocked waiting for the
 718         * bit-banging i2c interface to finish uploading the
 719         * firmware.
 720         */
 721        INIT_WORK(&state->fw_work, cx25840_work_handler);
 722        init_waitqueue_head(&state->fw_wait);
 723        q = create_singlethread_workqueue("cx25840_fw");
 724        if (q) {
 725                prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);
 726                queue_work(q, &state->fw_work);
 727                schedule();
 728                finish_wait(&state->fw_wait, &wait);
 729                destroy_workqueue(q);
 730        }
 731
 732        /* 6. */
 733        cx25840_write(client, 0x115, 0x8c);
 734        cx25840_write(client, 0x116, 0x07);
 735        cx25840_write(client, 0x118, 0x02);
 736        /* 7. */
 737        cx25840_write(client, 0x4a5, 0x80);
 738        cx25840_write(client, 0x4a5, 0x00);
 739        cx25840_write(client, 0x402, 0x00);
 740        /* 8. */
 741        cx25840_and_or(client, 0x401, ~0x18, 0);
 742        cx25840_and_or(client, 0x4a2, ~0x10, 0x10);
 743        /* steps 8c and 8d are done in change_input() */
 744        /* 10. */
 745        cx25840_write(client, 0x8d3, 0x1f);
 746        cx25840_write(client, 0x8e3, 0x03);
 747
 748        cx25840_std_setup(client);
 749
 750        /* trial and error says these are needed to get audio */
 751        cx25840_write(client, 0x914, 0xa0);
 752        cx25840_write(client, 0x918, 0xa0);
 753        cx25840_write(client, 0x919, 0x01);
 754
 755        /* stereo preferred */
 756        cx25840_write(client, 0x809, 0x04);
 757        /* AC97 shift */
 758        cx25840_write(client, 0x8cf, 0x0f);
 759
 760        /* (re)set input */
 761        set_input(client, state->vid_input, state->aud_input);
 762
 763        if (state->generic_mode)
 764                cx25840_vconfig_apply(client);
 765
 766        /* start microcontroller */
 767        cx25840_and_or(client, 0x803, ~0x10, 0x10);
 768}
 769
 770static void cx23885_initialize(struct i2c_client *client)
 771{
 772        DEFINE_WAIT(wait);
 773        struct cx25840_state *state = to_state(i2c_get_clientdata(client));
 774        u32 clk_freq = 0;
 775        struct workqueue_struct *q;
 776
 777        /* cx23885 sets hostdata to clk_freq pointer */
 778        if (v4l2_get_subdev_hostdata(&state->sd))
 779                clk_freq = *((u32 *)v4l2_get_subdev_hostdata(&state->sd));
 780
 781        /*
 782         * Come out of digital power down
 783         * The CX23888, at least, needs this, otherwise registers aside from
 784         * 0x0-0x2 can't be read or written.
 785         */
 786        cx25840_write(client, 0x000, 0);
 787
 788        /* Internal Reset */
 789        cx25840_and_or(client, 0x102, ~0x01, 0x01);
 790        cx25840_and_or(client, 0x102, ~0x01, 0x00);
 791
 792        /* Stop microcontroller */
 793        cx25840_and_or(client, 0x803, ~0x10, 0x00);
 794
 795        /* DIF in reset? */
 796        cx25840_write(client, 0x398, 0);
 797
 798        /*
 799         * Trust the default xtal, no division
 800         * '885: 28.636363... MHz
 801         * '887: 25.000000 MHz
 802         * '888: 50.000000 MHz
 803         */
 804        cx25840_write(client, 0x2, 0x76);
 805
 806        /* Power up all the PLL's and DLL */
 807        cx25840_write(client, 0x1, 0x40);
 808
 809        /* Sys PLL */
 810        switch (state->id) {
 811        case CX23888_AV:
 812                /*
 813                 * 50.0 MHz * (0xb + 0xe8ba26/0x2000000)/4 = 5 * 28.636363 MHz
 814                 * 572.73 MHz before post divide
 815                 */
 816                if (clk_freq == 25000000) {
 817                        /* 888/ImpactVCBe or 25Mhz xtal */
 818                        ; /* nothing to do */
 819                } else {
 820                        /* HVR1850 or 50MHz xtal */
 821                        cx25840_write(client, 0x2, 0x71);
 822                }
 823                cx25840_write4(client, 0x11c, 0x01d1744c);
 824                cx25840_write4(client, 0x118, 0x00000416);
 825                cx25840_write4(client, 0x404, 0x0010253e);
 826                cx25840_write4(client, 0x42c, 0x42600000);
 827                cx25840_write4(client, 0x44c, 0x161f1000);
 828                break;
 829        case CX23887_AV:
 830                /*
 831                 * 25.0 MHz * (0x16 + 0x1d1744c/0x2000000)/4 = 5 * 28.636363 MHz
 832                 * 572.73 MHz before post divide
 833                 */
 834                cx25840_write4(client, 0x11c, 0x01d1744c);
 835                cx25840_write4(client, 0x118, 0x00000416);
 836                break;
 837        case CX23885_AV:
 838        default:
 839                /*
 840                 * 28.636363 MHz * (0x14 + 0x0/0x2000000)/4 = 5 * 28.636363 MHz
 841                 * 572.73 MHz before post divide
 842                 */
 843                cx25840_write4(client, 0x11c, 0x00000000);
 844                cx25840_write4(client, 0x118, 0x00000414);
 845                break;
 846        }
 847
 848        /* Disable DIF bypass */
 849        cx25840_write4(client, 0x33c, 0x00000001);
 850
 851        /* DIF Src phase inc */
 852        cx25840_write4(client, 0x340, 0x0df7df83);
 853
 854        /*
 855         * Vid PLL
 856         * Setup for a BT.656 pixel clock of 13.5 Mpixels/second
 857         *
 858         * 28.636363 MHz * (0xf + 0x02be2c9/0x2000000)/4 = 8 * 13.5 MHz
 859         * 432.0 MHz before post divide
 860         */
 861
 862        /* HVR1850 */
 863        switch (state->id) {
 864        case CX23888_AV:
 865                if (clk_freq == 25000000) {
 866                        /* 888/ImpactVCBe or 25MHz xtal */
 867                        cx25840_write4(client, 0x10c, 0x01b6db7b);
 868                        cx25840_write4(client, 0x108, 0x00000512);
 869                } else {
 870                        /* 888/HVR1250 or 50MHz xtal */
 871                        cx25840_write4(client, 0x10c, 0x13333333);
 872                        cx25840_write4(client, 0x108, 0x00000515);
 873                }
 874                break;
 875        default:
 876                cx25840_write4(client, 0x10c, 0x002be2c9);
 877                cx25840_write4(client, 0x108, 0x0000040f);
 878        }
 879
 880        /* Luma */
 881        cx25840_write4(client, 0x414, 0x00107d12);
 882
 883        /* Chroma */
 884        if (is_cx23888(state))
 885                cx25840_write4(client, 0x418, 0x1d008282);
 886        else
 887                cx25840_write4(client, 0x420, 0x3d008282);
 888
 889        /*
 890         * Aux PLL
 891         * Initial setup for audio sample clock:
 892         * 48 ksps, 16 bits/sample, x160 multiplier = 122.88 MHz
 893         * Initial I2S output/master clock(?):
 894         * 48 ksps, 16 bits/sample, x16 multiplier = 12.288 MHz
 895         */
 896        switch (state->id) {
 897        case CX23888_AV:
 898                /*
 899                 * 50.0 MHz * (0x7 + 0x0bedfa4/0x2000000)/3 = 122.88 MHz
 900                 * 368.64 MHz before post divide
 901                 * 122.88 MHz / 0xa = 12.288 MHz
 902                 */
 903                /* HVR1850 or 50MHz xtal or 25MHz xtal */
 904                cx25840_write4(client, 0x114, 0x017dbf48);
 905                cx25840_write4(client, 0x110, 0x000a030e);
 906                break;
 907        case CX23887_AV:
 908                /*
 909                 * 25.0 MHz * (0xe + 0x17dbf48/0x2000000)/3 = 122.88 MHz
 910                 * 368.64 MHz before post divide
 911                 * 122.88 MHz / 0xa = 12.288 MHz
 912                 */
 913                cx25840_write4(client, 0x114, 0x017dbf48);
 914                cx25840_write4(client, 0x110, 0x000a030e);
 915                break;
 916        case CX23885_AV:
 917        default:
 918                /*
 919                 * 28.636363 MHz * (0xc + 0x1bf0c9e/0x2000000)/3 = 122.88 MHz
 920                 * 368.64 MHz before post divide
 921                 * 122.88 MHz / 0xa = 12.288 MHz
 922                 */
 923                cx25840_write4(client, 0x114, 0x01bf0c9e);
 924                cx25840_write4(client, 0x110, 0x000a030c);
 925                break;
 926        }
 927
 928        /* ADC2 input select */
 929        cx25840_write(client, 0x102, 0x10);
 930
 931        /* VIN1 & VIN5 */
 932        cx25840_write(client, 0x103, 0x11);
 933
 934        /* Enable format auto detect */
 935        cx25840_write(client, 0x400, 0);
 936        /* Fast subchroma lock */
 937        /* White crush, Chroma AGC & Chroma Killer enabled */
 938        cx25840_write(client, 0x401, 0xe8);
 939
 940        /* Select AFE clock pad output source */
 941        cx25840_write(client, 0x144, 0x05);
 942
 943        /* Drive GPIO2 direction and values for HVR1700
 944         * where an onboard mux selects the output of demodulator
 945         * vs the 417. Failure to set this results in no DTV.
 946         * It's safe to set this across all Hauppauge boards
 947         * currently, regardless of the board type.
 948         */
 949        cx25840_write(client, 0x160, 0x1d);
 950        cx25840_write(client, 0x164, 0x00);
 951
 952        /*
 953         * Do the firmware load in a work handler to prevent.
 954         * Otherwise the kernel is blocked waiting for the
 955         * bit-banging i2c interface to finish uploading the
 956         * firmware.
 957         */
 958        INIT_WORK(&state->fw_work, cx25840_work_handler);
 959        init_waitqueue_head(&state->fw_wait);
 960        q = create_singlethread_workqueue("cx25840_fw");
 961        if (q) {
 962                prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);
 963                queue_work(q, &state->fw_work);
 964                schedule();
 965                finish_wait(&state->fw_wait, &wait);
 966                destroy_workqueue(q);
 967        }
 968
 969        /*
 970         * Call the cx23888 specific std setup func, we no longer rely on
 971         * the generic cx24840 func.
 972         */
 973        if (is_cx23888(state))
 974                cx23888_std_setup(client);
 975        else
 976                cx25840_std_setup(client);
 977
 978        /* (re)set input */
 979        set_input(client, state->vid_input, state->aud_input);
 980
 981        /* start microcontroller */
 982        cx25840_and_or(client, 0x803, ~0x10, 0x10);
 983
 984        /* Disable and clear video interrupts - we don't use them */
 985        cx25840_write4(client, CX25840_VID_INT_STAT_REG, 0xffffffff);
 986
 987        /* Disable and clear audio interrupts - we don't use them */
 988        cx25840_write(client, CX25840_AUD_INT_CTRL_REG, 0xff);
 989        cx25840_write(client, CX25840_AUD_INT_STAT_REG, 0xff);
 990
 991        /* CC raw enable */
 992
 993        /*
 994         *  - VIP 1.1 control codes - 10bit, blue field enable.
 995         *  - enable raw data during vertical blanking.
 996         *  - enable ancillary Data insertion for 656 or VIP.
 997         */
 998        cx25840_write4(client, 0x404, 0x0010253e);
 999
1000        /* CC on  - VBI_LINE_CTRL3, FLD_VBI_MD_LINE12 */
1001        cx25840_write(client, state->vbi_regs_offset + 0x42f, 0x66);
1002
1003        /* HVR-1250 / HVR1850 DIF related */
1004        /* Power everything up */
1005        cx25840_write4(client, 0x130, 0x0);
1006
1007        /* SRC_COMB_CFG */
1008        if (is_cx23888(state))
1009                cx25840_write4(client, 0x454, 0x6628021F);
1010        else
1011                cx25840_write4(client, 0x478, 0x6628021F);
1012
1013        /* AFE_CLK_OUT_CTRL - Select the clock output source as output */
1014        cx25840_write4(client, 0x144, 0x5);
1015
1016        /* I2C_OUT_CTL - I2S output configuration as
1017         * Master, Sony, Left justified, left sample on WS=1
1018         */
1019        cx25840_write4(client, 0x918, 0x1a0);
1020
1021        /* AFE_DIAG_CTRL1 */
1022        cx25840_write4(client, 0x134, 0x000a1800);
1023
1024        /* AFE_DIAG_CTRL3 - Inverted Polarity for Audio and Video */
1025        cx25840_write4(client, 0x13c, 0x00310000);
1026}
1027
1028/* ----------------------------------------------------------------------- */
1029
1030static void cx231xx_initialize(struct i2c_client *client)
1031{
1032        DEFINE_WAIT(wait);
1033        struct cx25840_state *state = to_state(i2c_get_clientdata(client));
1034        struct workqueue_struct *q;
1035
1036        /* Internal Reset */
1037        cx25840_and_or(client, 0x102, ~0x01, 0x01);
1038        cx25840_and_or(client, 0x102, ~0x01, 0x00);
1039
1040        /* Stop microcontroller */
1041        cx25840_and_or(client, 0x803, ~0x10, 0x00);
1042
1043        /* DIF in reset? */
1044        cx25840_write(client, 0x398, 0);
1045
1046        /* Trust the default xtal, no division */
1047        /* This changes for the cx23888 products */
1048        cx25840_write(client, 0x2, 0x76);
1049
1050        /* Bring down the regulator for AUX clk */
1051        cx25840_write(client, 0x1, 0x40);
1052
1053        /* Disable DIF bypass */
1054        cx25840_write4(client, 0x33c, 0x00000001);
1055
1056        /* DIF Src phase inc */
1057        cx25840_write4(client, 0x340, 0x0df7df83);
1058
1059        /* Luma */
1060        cx25840_write4(client, 0x414, 0x00107d12);
1061
1062        /* Chroma */
1063        cx25840_write4(client, 0x420, 0x3d008282);
1064
1065        /* ADC2 input select */
1066        cx25840_write(client, 0x102, 0x10);
1067
1068        /* VIN1 & VIN5 */
1069        cx25840_write(client, 0x103, 0x11);
1070
1071        /* Enable format auto detect */
1072        cx25840_write(client, 0x400, 0);
1073        /* Fast subchroma lock */
1074        /* White crush, Chroma AGC & Chroma Killer enabled */
1075        cx25840_write(client, 0x401, 0xe8);
1076
1077        /*
1078         * Do the firmware load in a work handler to prevent.
1079         * Otherwise the kernel is blocked waiting for the
1080         * bit-banging i2c interface to finish uploading the
1081         * firmware.
1082         */
1083        INIT_WORK(&state->fw_work, cx25840_work_handler);
1084        init_waitqueue_head(&state->fw_wait);
1085        q = create_singlethread_workqueue("cx25840_fw");
1086        if (q) {
1087                prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);
1088                queue_work(q, &state->fw_work);
1089                schedule();
1090                finish_wait(&state->fw_wait, &wait);
1091                destroy_workqueue(q);
1092        }
1093
1094        cx25840_std_setup(client);
1095
1096        /* (re)set input */
1097        set_input(client, state->vid_input, state->aud_input);
1098
1099        /* start microcontroller */
1100        cx25840_and_or(client, 0x803, ~0x10, 0x10);
1101
1102        /* CC raw enable */
1103        cx25840_write(client, 0x404, 0x0b);
1104
1105        /* CC on */
1106        cx25840_write(client, 0x42f, 0x66);
1107        cx25840_write4(client, 0x474, 0x1e1e601a);
1108}
1109
1110/* ----------------------------------------------------------------------- */
1111
1112void cx25840_std_setup(struct i2c_client *client)
1113{
1114        struct cx25840_state *state = to_state(i2c_get_clientdata(client));
1115        v4l2_std_id std = state->std;
1116        int hblank, hactive, burst, vblank, vactive, sc;
1117        int vblank656, src_decimation;
1118        int luma_lpf, uv_lpf, comb;
1119        u32 pll_int, pll_frac, pll_post;
1120
1121        /* datasheet startup, step 8d */
1122        if (std & ~V4L2_STD_NTSC)
1123                cx25840_write(client, 0x49f, 0x11);
1124        else
1125                cx25840_write(client, 0x49f, 0x14);
1126
1127        /* generic mode uses the values that the chip autoconfig would set */
1128        if (std & V4L2_STD_625_50) {
1129                hblank = 132;
1130                hactive = 720;
1131                burst = 93;
1132                if (state->generic_mode) {
1133                        vblank = 34;
1134                        vactive = 576;
1135                        vblank656 = 38;
1136                } else {
1137                        vblank = 36;
1138                        vactive = 580;
1139                        vblank656 = 40;
1140                }
1141                src_decimation = 0x21f;
1142                luma_lpf = 2;
1143
1144                if (std & V4L2_STD_SECAM) {
1145                        uv_lpf = 0;
1146                        comb = 0;
1147                        sc = 0x0a425f;
1148                } else if (std == V4L2_STD_PAL_Nc) {
1149                        if (state->generic_mode) {
1150                                burst = 95;
1151                                luma_lpf = 1;
1152                        }
1153                        uv_lpf = 1;
1154                        comb = 0x20;
1155                        sc = 556453;
1156                } else {
1157                        uv_lpf = 1;
1158                        comb = 0x20;
1159                        sc = 688739;
1160                }
1161        } else {
1162                hactive = 720;
1163                hblank = 122;
1164                vactive = 487;
1165                luma_lpf = 1;
1166                uv_lpf = 1;
1167                if (state->generic_mode) {
1168                        vblank = 20;
1169                        vblank656 = 24;
1170                }
1171
1172                src_decimation = 0x21f;
1173                if (std == V4L2_STD_PAL_60) {
1174                        if (!state->generic_mode) {
1175                                vblank = 26;
1176                                vblank656 = 26;
1177                                burst = 0x5b;
1178                        } else {
1179                                burst = 0x59;
1180                        }
1181                        luma_lpf = 2;
1182                        comb = 0x20;
1183                        sc = 688739;
1184                } else if (std == V4L2_STD_PAL_M) {
1185                        vblank = 20;
1186                        vblank656 = 24;
1187                        burst = 0x61;
1188                        comb = 0x20;
1189                        sc = 555452;
1190                } else {
1191                        if (!state->generic_mode) {
1192                                vblank = 26;
1193                                vblank656 = 26;
1194                        }
1195                        burst = 0x5b;
1196                        comb = 0x66;
1197                        sc = 556063;
1198                }
1199        }
1200
1201        /* DEBUG: Displays configured PLL frequency */
1202        if (!is_cx231xx(state)) {
1203                pll_int = cx25840_read(client, 0x108);
1204                pll_frac = cx25840_read4(client, 0x10c) & 0x1ffffff;
1205                pll_post = cx25840_read(client, 0x109);
1206                v4l_dbg(1, cx25840_debug, client,
1207                        "PLL regs = int: %u, frac: %u, post: %u\n",
1208                        pll_int, pll_frac, pll_post);
1209
1210                if (pll_post) {
1211                        int fin, fsc;
1212                        int pll = (28636363L * ((((u64)pll_int) << 25L) + pll_frac)) >> 25L;
1213
1214                        pll /= pll_post;
1215                        v4l_dbg(1, cx25840_debug, client,
1216                                "PLL = %d.%06d MHz\n",
1217                                pll / 1000000, pll % 1000000);
1218                        v4l_dbg(1, cx25840_debug, client,
1219                                "PLL/8 = %d.%06d MHz\n",
1220                                pll / 8000000, (pll / 8) % 1000000);
1221
1222                        fin = ((u64)src_decimation * pll) >> 12;
1223                        v4l_dbg(1, cx25840_debug, client,
1224                                "ADC Sampling freq = %d.%06d MHz\n",
1225                                fin / 1000000, fin % 1000000);
1226
1227                        fsc = (((u64)sc) * pll) >> 24L;
1228                        v4l_dbg(1, cx25840_debug, client,
1229                                "Chroma sub-carrier freq = %d.%06d MHz\n",
1230                                fsc / 1000000, fsc % 1000000);
1231
1232                        v4l_dbg(1, cx25840_debug, client,
1233                                "hblank %i, hactive %i, vblank %i, vactive %i, vblank656 %i, src_dec %i, burst 0x%02x, luma_lpf %i, uv_lpf %i, comb 0x%02x, sc 0x%06x\n",
1234                                hblank, hactive, vblank, vactive, vblank656,
1235                                src_decimation, burst, luma_lpf, uv_lpf,
1236                                comb, sc);
1237                }
1238        }
1239
1240        /* Sets horizontal blanking delay and active lines */
1241        cx25840_write(client, 0x470, hblank);
1242        cx25840_write(client, 0x471,
1243                      (((hblank >> 8) & 0x3) | (hactive << 4)) & 0xff);
1244        cx25840_write(client, 0x472, hactive >> 4);
1245
1246        /* Sets burst gate delay */
1247        cx25840_write(client, 0x473, burst);
1248
1249        /* Sets vertical blanking delay and active duration */
1250        cx25840_write(client, 0x474, vblank);
1251        cx25840_write(client, 0x475,
1252                      (((vblank >> 8) & 0x3) | (vactive << 4)) & 0xff);
1253        cx25840_write(client, 0x476, vactive >> 4);
1254        cx25840_write(client, 0x477, vblank656);
1255
1256        /* Sets src decimation rate */
1257        cx25840_write(client, 0x478, src_decimation & 0xff);
1258        cx25840_write(client, 0x479, (src_decimation >> 8) & 0xff);
1259
1260        /* Sets Luma and UV Low pass filters */
1261        cx25840_write(client, 0x47a, luma_lpf << 6 | ((uv_lpf << 4) & 0x30));
1262
1263        /* Enables comb filters */
1264        cx25840_write(client, 0x47b, comb);
1265
1266        /* Sets SC Step*/
1267        cx25840_write(client, 0x47c, sc);
1268        cx25840_write(client, 0x47d, (sc >> 8) & 0xff);
1269        cx25840_write(client, 0x47e, (sc >> 16) & 0xff);
1270
1271        /* Sets VBI parameters */
1272        if (std & V4L2_STD_625_50) {
1273                cx25840_write(client, 0x47f, 0x01);
1274                state->vbi_line_offset = 5;
1275        } else {
1276                cx25840_write(client, 0x47f, 0x00);
1277                state->vbi_line_offset = 8;
1278        }
1279}
1280
1281/* ----------------------------------------------------------------------- */
1282
1283static void input_change(struct i2c_client *client)
1284{
1285        struct cx25840_state *state = to_state(i2c_get_clientdata(client));
1286        v4l2_std_id std = state->std;
1287
1288        /* Follow step 8c and 8d of section 3.16 in the cx25840 datasheet */
1289        if (std & V4L2_STD_SECAM) {
1290                cx25840_write(client, 0x402, 0);
1291        } else {
1292                cx25840_write(client, 0x402, 0x04);
1293                cx25840_write(client, 0x49f,
1294                              (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
1295        }
1296        cx25840_and_or(client, 0x401, ~0x60, 0);
1297        cx25840_and_or(client, 0x401, ~0x60, 0x60);
1298
1299        /* Don't write into audio registers on cx2583x chips */
1300        if (is_cx2583x(state))
1301                return;
1302
1303        cx25840_and_or(client, 0x810, ~0x01, 1);
1304
1305        if (state->radio) {
1306                cx25840_write(client, 0x808, 0xf9);
1307                cx25840_write(client, 0x80b, 0x00);
1308        } else if (std & V4L2_STD_525_60) {
1309                /*
1310                 * Certain Hauppauge PVR150 models have a hardware bug
1311                 * that causes audio to drop out. For these models the
1312                 * audio standard must be set explicitly.
1313                 * To be precise: it affects cards with tuner models
1314                 * 85, 99 and 112 (model numbers from tveeprom).
1315                 */
1316                int hw_fix = state->pvr150_workaround;
1317
1318                if (std == V4L2_STD_NTSC_M_JP) {
1319                        /* Japan uses EIAJ audio standard */
1320                        cx25840_write(client, 0x808, hw_fix ? 0x2f : 0xf7);
1321                } else if (std == V4L2_STD_NTSC_M_KR) {
1322                        /* South Korea uses A2 audio standard */
1323                        cx25840_write(client, 0x808, hw_fix ? 0x3f : 0xf8);
1324                } else {
1325                        /* Others use the BTSC audio standard */
1326                        cx25840_write(client, 0x808, hw_fix ? 0x1f : 0xf6);
1327                }
1328                cx25840_write(client, 0x80b, 0x00);
1329        } else if (std & V4L2_STD_PAL) {
1330                /* Autodetect audio standard and audio system */
1331                cx25840_write(client, 0x808, 0xff);
1332                /*
1333                 * Since system PAL-L is pretty much non-existent and
1334                 * not used by any public broadcast network, force
1335                 * 6.5 MHz carrier to be interpreted as System DK,
1336                 * this avoids DK audio detection instability
1337                 */
1338                cx25840_write(client, 0x80b, 0x00);
1339        } else if (std & V4L2_STD_SECAM) {
1340                /* Autodetect audio standard and audio system */
1341                cx25840_write(client, 0x808, 0xff);
1342                /*
1343                 * If only one of SECAM-DK / SECAM-L is required, then force
1344                 * 6.5MHz carrier, else autodetect it
1345                 */
1346                if ((std & V4L2_STD_SECAM_DK) &&
1347                    !(std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC))) {
1348                        /* 6.5 MHz carrier to be interpreted as System DK */
1349                        cx25840_write(client, 0x80b, 0x00);
1350                } else if (!(std & V4L2_STD_SECAM_DK) &&
1351                           (std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC))) {
1352                        /* 6.5 MHz carrier to be interpreted as System L */
1353                        cx25840_write(client, 0x80b, 0x08);
1354                } else {
1355                        /* 6.5 MHz carrier to be autodetected */
1356                        cx25840_write(client, 0x80b, 0x10);
1357                }
1358        }
1359
1360        cx25840_and_or(client, 0x810, ~0x01, 0);
1361}
1362
1363static int set_input(struct i2c_client *client,
1364                     enum cx25840_video_input vid_input,
1365                     enum cx25840_audio_input aud_input)
1366{
1367        struct cx25840_state *state = to_state(i2c_get_clientdata(client));
1368        u8 is_composite = (vid_input >= CX25840_COMPOSITE1 &&
1369                           vid_input <= CX25840_COMPOSITE8);
1370        u8 is_component = (vid_input & CX25840_COMPONENT_ON) ==
1371                        CX25840_COMPONENT_ON;
1372        u8 is_dif = (vid_input & CX25840_DIF_ON) ==
1373                        CX25840_DIF_ON;
1374        u8 is_svideo = (vid_input & CX25840_SVIDEO_ON) ==
1375                        CX25840_SVIDEO_ON;
1376        int luma = vid_input & 0xf0;
1377        int chroma = vid_input & 0xf00;
1378        u8 reg;
1379        u32 val;
1380
1381        v4l_dbg(1, cx25840_debug, client,
1382                "decoder set video input %d, audio input %d\n",
1383                vid_input, aud_input);
1384
1385        if (vid_input >= CX25840_VIN1_CH1) {
1386                v4l_dbg(1, cx25840_debug, client, "vid_input 0x%x\n",
1387                        vid_input);
1388                reg = vid_input & 0xff;
1389                is_composite = !is_component &&
1390                               ((vid_input & CX25840_SVIDEO_ON) != CX25840_SVIDEO_ON);
1391
1392                v4l_dbg(1, cx25840_debug, client, "mux cfg 0x%x comp=%d\n",
1393                        reg, is_composite);
1394        } else if (is_composite) {
1395                reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);
1396        } else {
1397                if ((vid_input & ~0xff0) ||
1398                    luma < CX25840_SVIDEO_LUMA1 ||
1399                    luma > CX25840_SVIDEO_LUMA8 ||
1400                    chroma < CX25840_SVIDEO_CHROMA4 ||
1401                    chroma > CX25840_SVIDEO_CHROMA8) {
1402                        v4l_err(client, "0x%04x is not a valid video input!\n",
1403                                vid_input);
1404                        return -EINVAL;
1405                }
1406                reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4);
1407                if (chroma >= CX25840_SVIDEO_CHROMA7) {
1408                        reg &= 0x3f;
1409                        reg |= (chroma - CX25840_SVIDEO_CHROMA7) >> 2;
1410                } else {
1411                        reg &= 0xcf;
1412                        reg |= (chroma - CX25840_SVIDEO_CHROMA4) >> 4;
1413                }
1414        }
1415
1416        /* The caller has previously prepared the correct routing
1417         * configuration in reg (for the cx23885) so we have no
1418         * need to attempt to flip bits for earlier av decoders.
1419         */
1420        if (!is_cx2388x(state) && !is_cx231xx(state)) {
1421                switch (aud_input) {
1422                case CX25840_AUDIO_SERIAL:
1423                        /* do nothing, use serial audio input */
1424                        break;
1425                case CX25840_AUDIO4:
1426                        reg &= ~0x30;
1427                        break;
1428                case CX25840_AUDIO5:
1429                        reg &= ~0x30;
1430                        reg |= 0x10;
1431                        break;
1432                case CX25840_AUDIO6:
1433                        reg &= ~0x30;
1434                        reg |= 0x20;
1435                        break;
1436                case CX25840_AUDIO7:
1437                        reg &= ~0xc0;
1438                        break;
1439                case CX25840_AUDIO8:
1440                        reg &= ~0xc0;
1441                        reg |= 0x40;
1442                        break;
1443                default:
1444                        v4l_err(client, "0x%04x is not a valid audio input!\n",
1445                                aud_input);
1446                        return -EINVAL;
1447                }
1448        }
1449
1450        cx25840_write(client, 0x103, reg);
1451
1452        /* Set INPUT_MODE to Composite, S-Video or Component */
1453        if (is_component)
1454                cx25840_and_or(client, 0x401, ~0x6, 0x6);
1455        else
1456                cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);
1457
1458        if (is_cx2388x(state)) {
1459                /* Enable or disable the DIF for tuner use */
1460                if (is_dif) {
1461                        cx25840_and_or(client, 0x102, ~0x80, 0x80);
1462
1463                        /* Set of defaults for NTSC and PAL */
1464                        cx25840_write4(client, 0x31c, 0xc2262600);
1465                        cx25840_write4(client, 0x320, 0xc2262600);
1466
1467                        /* 18271 IF - Nobody else yet uses a different
1468                         * tuner with the DIF, so these are reasonable
1469                         * assumptions (HVR1250 and HVR1850 specific).
1470                         */
1471                        cx25840_write4(client, 0x318, 0xda262600);
1472                        cx25840_write4(client, 0x33c, 0x2a24c800);
1473                        cx25840_write4(client, 0x104, 0x0704dd00);
1474                } else {
1475                        cx25840_write4(client, 0x300, 0x015c28f5);
1476
1477                        cx25840_and_or(client, 0x102, ~0x80, 0);
1478                        cx25840_write4(client, 0x340, 0xdf7df83);
1479                        cx25840_write4(client, 0x104, 0x0704dd80);
1480                        cx25840_write4(client, 0x314, 0x22400600);
1481                        cx25840_write4(client, 0x318, 0x40002600);
1482                        cx25840_write4(client, 0x324, 0x40002600);
1483                        cx25840_write4(client, 0x32c, 0x0250e620);
1484                        cx25840_write4(client, 0x39c, 0x01FF0B00);
1485
1486                        cx25840_write4(client, 0x410, 0xffff0dbf);
1487                        cx25840_write4(client, 0x414, 0x00137d03);
1488
1489                        if (is_cx23888(state)) {
1490                                /* 888 MISC_TIM_CTRL */
1491                                cx25840_write4(client, 0x42c, 0x42600000);
1492                                /* 888 FIELD_COUNT */
1493                                cx25840_write4(client, 0x430, 0x0000039b);
1494                                /* 888 VSCALE_CTRL */
1495                                cx25840_write4(client, 0x438, 0x00000000);
1496                                /* 888 DFE_CTRL1 */
1497                                cx25840_write4(client, 0x440, 0xF8E3E824);
1498                                /* 888 DFE_CTRL2 */
1499                                cx25840_write4(client, 0x444, 0x401040dc);
1500                                /* 888 DFE_CTRL3 */
1501                                cx25840_write4(client, 0x448, 0xcd3f02a0);
1502                                /* 888 PLL_CTRL */
1503                                cx25840_write4(client, 0x44c, 0x161f1000);
1504                                /* 888 HTL_CTRL */
1505                                cx25840_write4(client, 0x450, 0x00000802);
1506                        }
1507                        cx25840_write4(client, 0x91c, 0x01000000);
1508                        cx25840_write4(client, 0x8e0, 0x03063870);
1509                        cx25840_write4(client, 0x8d4, 0x7FFF0024);
1510                        cx25840_write4(client, 0x8d0, 0x00063073);
1511
1512                        cx25840_write4(client, 0x8c8, 0x00010000);
1513                        cx25840_write4(client, 0x8cc, 0x00080023);
1514
1515                        /* DIF BYPASS */
1516                        cx25840_write4(client, 0x33c, 0x2a04c800);
1517                }
1518
1519                /* Reset the DIF */
1520                cx25840_write4(client, 0x398, 0);
1521        }
1522
1523        if (!is_cx2388x(state) && !is_cx231xx(state)) {
1524                /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
1525                cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
1526                /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2&CH3 */
1527                if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
1528                        cx25840_and_or(client, 0x102, ~0x4, 4);
1529                else
1530                        cx25840_and_or(client, 0x102, ~0x4, 0);
1531        } else {
1532                /* Set DUAL_MODE_ADC2 to 1 if component*/
1533                cx25840_and_or(client, 0x102, ~0x4, is_component ? 0x4 : 0x0);
1534                if (is_composite) {
1535                        /* ADC2 input select channel 2 */
1536                        cx25840_and_or(client, 0x102, ~0x2, 0);
1537                } else if (!is_component) {
1538                        /* S-Video */
1539                        if (chroma >= CX25840_SVIDEO_CHROMA7) {
1540                                /* ADC2 input select channel 3 */
1541                                cx25840_and_or(client, 0x102, ~0x2, 2);
1542                        } else {
1543                                /* ADC2 input select channel 2 */
1544                                cx25840_and_or(client, 0x102, ~0x2, 0);
1545                        }
1546                }
1547
1548                /* cx23885 / SVIDEO */
1549                if (is_cx2388x(state) && is_svideo) {
1550#define AFE_CTRL  (0x104)
1551#define MODE_CTRL (0x400)
1552                        cx25840_and_or(client, 0x102, ~0x2, 0x2);
1553
1554                        val = cx25840_read4(client, MODE_CTRL);
1555                        val &= 0xFFFFF9FF;
1556
1557                        /* YC */
1558                        val |= 0x00000200;
1559                        val &= ~0x2000;
1560                        cx25840_write4(client, MODE_CTRL, val);
1561
1562                        val = cx25840_read4(client, AFE_CTRL);
1563
1564                        /* Chroma in select */
1565                        val |= 0x00001000;
1566                        val &= 0xfffffe7f;
1567                        /* Clear VGA_SEL_CH2 and VGA_SEL_CH3 (bits 7 and 8).
1568                         * This sets them to use video rather than audio.
1569                         * Only one of the two will be in use.
1570                         */
1571                        cx25840_write4(client, AFE_CTRL, val);
1572                } else {
1573                        cx25840_and_or(client, 0x102, ~0x2, 0);
1574                }
1575        }
1576
1577        state->vid_input = vid_input;
1578        state->aud_input = aud_input;
1579        cx25840_audio_set_path(client);
1580        input_change(client);
1581
1582        if (is_cx2388x(state)) {
1583                /* Audio channel 1 src : Parallel 1 */
1584                cx25840_write(client, 0x124, 0x03);
1585
1586                /* Select AFE clock pad output source */
1587                cx25840_write(client, 0x144, 0x05);
1588
1589                /* I2S_IN_CTL: I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1 */
1590                cx25840_write(client, 0x914, 0xa0);
1591
1592                /* I2S_OUT_CTL:
1593                 * I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1
1594                 * I2S_OUT_MASTER_MODE = Master
1595                 */
1596                cx25840_write(client, 0x918, 0xa0);
1597                cx25840_write(client, 0x919, 0x01);
1598        } else if (is_cx231xx(state)) {
1599                /* Audio channel 1 src : Parallel 1 */
1600                cx25840_write(client, 0x124, 0x03);
1601
1602                /* I2S_IN_CTL: I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1 */
1603                cx25840_write(client, 0x914, 0xa0);
1604
1605                /* I2S_OUT_CTL:
1606                 * I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1
1607                 * I2S_OUT_MASTER_MODE = Master
1608                 */
1609                cx25840_write(client, 0x918, 0xa0);
1610                cx25840_write(client, 0x919, 0x01);
1611        }
1612
1613        if (is_cx2388x(state) &&
1614            ((aud_input == CX25840_AUDIO7) || (aud_input == CX25840_AUDIO6))) {
1615                /* Configure audio from LR1 or LR2 input */
1616                cx25840_write4(client, 0x910, 0);
1617                cx25840_write4(client, 0x8d0, 0x63073);
1618        } else if (is_cx2388x(state) && (aud_input == CX25840_AUDIO8)) {
1619                /* Configure audio from tuner/sif input */
1620                cx25840_write4(client, 0x910, 0x12b000c9);
1621                cx25840_write4(client, 0x8d0, 0x1f063870);
1622        }
1623
1624        if (is_cx23888(state)) {
1625                /*
1626                 * HVR1850
1627                 *
1628                 * AUD_IO_CTRL - I2S Input, Parallel1
1629                 *  - Channel 1 src - Parallel1 (Merlin out)
1630                 *  - Channel 2 src - Parallel2 (Merlin out)
1631                 *  - Channel 3 src - Parallel3 (Merlin AC97 out)
1632                 *  - I2S source and dir - Merlin, output
1633                 */
1634                cx25840_write4(client, 0x124, 0x100);
1635
1636                if (!is_dif) {
1637                        /*
1638                         * Stop microcontroller if we don't need it
1639                         * to avoid audio popping on svideo/composite use.
1640                         */
1641                        cx25840_and_or(client, 0x803, ~0x10, 0x00);
1642                }
1643        }
1644
1645        return 0;
1646}
1647
1648/* ----------------------------------------------------------------------- */
1649
1650static int set_v4lstd(struct i2c_client *client)
1651{
1652        struct cx25840_state *state = to_state(i2c_get_clientdata(client));
1653        u8 fmt = 0;     /* zero is autodetect */
1654        u8 pal_m = 0;
1655
1656        /* First tests should be against specific std */
1657        if (state->std == V4L2_STD_NTSC_M_JP) {
1658                fmt = 0x2;
1659        } else if (state->std == V4L2_STD_NTSC_443) {
1660                fmt = 0x3;
1661        } else if (state->std == V4L2_STD_PAL_M) {
1662                pal_m = 1;
1663                fmt = 0x5;
1664        } else if (state->std == V4L2_STD_PAL_N) {
1665                fmt = 0x6;
1666        } else if (state->std == V4L2_STD_PAL_Nc) {
1667                fmt = 0x7;
1668        } else if (state->std == V4L2_STD_PAL_60) {
1669                fmt = 0x8;
1670        } else {
1671                /* Then, test against generic ones */
1672                if (state->std & V4L2_STD_NTSC)
1673                        fmt = 0x1;
1674                else if (state->std & V4L2_STD_PAL)
1675                        fmt = 0x4;
1676                else if (state->std & V4L2_STD_SECAM)
1677                        fmt = 0xc;
1678        }
1679
1680        v4l_dbg(1, cx25840_debug, client,
1681                "changing video std to fmt %i\n", fmt);
1682
1683        /*
1684         * Follow step 9 of section 3.16 in the cx25840 datasheet.
1685         * Without this PAL may display a vertical ghosting effect.
1686         * This happens for example with the Yuan MPC622.
1687         */
1688        if (fmt >= 4 && fmt < 8) {
1689                /* Set format to NTSC-M */
1690                cx25840_and_or(client, 0x400, ~0xf, 1);
1691                /* Turn off LCOMB */
1692                cx25840_and_or(client, 0x47b, ~6, 0);
1693        }
1694        cx25840_and_or(client, 0x400, ~0xf, fmt);
1695        cx25840_and_or(client, 0x403, ~0x3, pal_m);
1696        if (is_cx23888(state))
1697                cx23888_std_setup(client);
1698        else
1699                cx25840_std_setup(client);
1700        if (!is_cx2583x(state))
1701                input_change(client);
1702        return 0;
1703}
1704
1705/* ----------------------------------------------------------------------- */
1706
1707static int cx25840_s_ctrl(struct v4l2_ctrl *ctrl)
1708{
1709        struct v4l2_subdev *sd = to_sd(ctrl);
1710        struct cx25840_state *state = to_state(sd);
1711        struct i2c_client *client = v4l2_get_subdevdata(sd);
1712
1713        switch (ctrl->id) {
1714        case V4L2_CID_BRIGHTNESS:
1715                cx25840_write(client, 0x414, ctrl->val - 128);
1716                break;
1717
1718        case V4L2_CID_CONTRAST:
1719                cx25840_write(client, 0x415, ctrl->val << 1);
1720                break;
1721
1722        case V4L2_CID_SATURATION:
1723                if (is_cx23888(state)) {
1724                        cx25840_write(client, 0x418, ctrl->val << 1);
1725                        cx25840_write(client, 0x419, ctrl->val << 1);
1726                } else {
1727                        cx25840_write(client, 0x420, ctrl->val << 1);
1728                        cx25840_write(client, 0x421, ctrl->val << 1);
1729                }
1730                break;
1731
1732        case V4L2_CID_HUE:
1733                if (is_cx23888(state))
1734                        cx25840_write(client, 0x41a, ctrl->val);
1735                else
1736                        cx25840_write(client, 0x422, ctrl->val);
1737                break;
1738
1739        default:
1740                return -EINVAL;
1741        }
1742
1743        return 0;
1744}
1745
1746/* ----------------------------------------------------------------------- */
1747
1748static int cx25840_set_fmt(struct v4l2_subdev *sd,
1749                           struct v4l2_subdev_state *sd_state,
1750                           struct v4l2_subdev_format *format)
1751{
1752        struct v4l2_mbus_framefmt *fmt = &format->format;
1753        struct cx25840_state *state = to_state(sd);
1754        struct i2c_client *client = v4l2_get_subdevdata(sd);
1755        u32 hsc, vsc, v_src, h_src, v_add;
1756        int filter;
1757        int is_50hz = !(state->std & V4L2_STD_525_60);
1758
1759        if (format->pad || fmt->code != MEDIA_BUS_FMT_FIXED)
1760                return -EINVAL;
1761
1762        fmt->field = V4L2_FIELD_INTERLACED;
1763        fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
1764
1765        if (is_cx23888(state)) {
1766                v_src = (cx25840_read(client, 0x42a) & 0x3f) << 4;
1767                v_src |= (cx25840_read(client, 0x429) & 0xf0) >> 4;
1768        } else {
1769                v_src = (cx25840_read(client, 0x476) & 0x3f) << 4;
1770                v_src |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
1771        }
1772
1773        if (is_cx23888(state)) {
1774                h_src = (cx25840_read(client, 0x426) & 0x3f) << 4;
1775                h_src |= (cx25840_read(client, 0x425) & 0xf0) >> 4;
1776        } else {
1777                h_src = (cx25840_read(client, 0x472) & 0x3f) << 4;
1778                h_src |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
1779        }
1780
1781        if (!state->generic_mode) {
1782                v_add = is_50hz ? 4 : 7;
1783
1784                /*
1785                 * cx23888 in 525-line mode is programmed for 486 active lines
1786                 * while other chips use 487 active lines.
1787                 *
1788                 * See reg 0x428 bits [21:12] in cx23888_std_setup() vs
1789                 * vactive in cx25840_std_setup().
1790                 */
1791                if (is_cx23888(state) && !is_50hz)
1792                        v_add--;
1793        } else {
1794                v_add = 0;
1795        }
1796
1797        if (h_src == 0 ||
1798            v_src <= v_add) {
1799                v4l_err(client,
1800                        "chip reported picture size (%u x %u) is far too small\n",
1801                        (unsigned int)h_src, (unsigned int)v_src);
1802                /*
1803                 * that's the best we can do since the output picture
1804                 * size is completely unknown in this case
1805                 */
1806                return -EINVAL;
1807        }
1808
1809        fmt->width = clamp(fmt->width, (h_src + 15) / 16, h_src);
1810
1811        if (v_add * 8 >= v_src)
1812                fmt->height = clamp(fmt->height, (u32)1, v_src - v_add);
1813        else
1814                fmt->height = clamp(fmt->height, (v_src - v_add * 8 + 7) / 8,
1815                                    v_src - v_add);
1816
1817        if (format->which == V4L2_SUBDEV_FORMAT_TRY)
1818                return 0;
1819
1820        hsc = (h_src * (1 << 20)) / fmt->width - (1 << 20);
1821        vsc = (1 << 16) - (v_src * (1 << 9) / (fmt->height + v_add) - (1 << 9));
1822        vsc &= 0x1fff;
1823
1824        if (fmt->width >= 385)
1825                filter = 0;
1826        else if (fmt->width > 192)
1827                filter = 1;
1828        else if (fmt->width > 96)
1829                filter = 2;
1830        else
1831                filter = 3;
1832
1833        v4l_dbg(1, cx25840_debug, client,
1834                "decoder set size %u x %u with scale %x x %x\n",
1835                (unsigned int)fmt->width, (unsigned int)fmt->height,
1836                (unsigned int)hsc, (unsigned int)vsc);
1837
1838        /* HSCALE=hsc */
1839        if (is_cx23888(state)) {
1840                cx25840_write4(client, 0x434, hsc | (1 << 24));
1841                /* VSCALE=vsc VS_INTRLACE=1 VFILT=filter */
1842                cx25840_write4(client, 0x438, vsc | (1 << 19) | (filter << 16));
1843        } else {
1844                cx25840_write(client, 0x418, hsc & 0xff);
1845                cx25840_write(client, 0x419, (hsc >> 8) & 0xff);
1846                cx25840_write(client, 0x41a, hsc >> 16);
1847                /* VSCALE=vsc */
1848                cx25840_write(client, 0x41c, vsc & 0xff);
1849                cx25840_write(client, 0x41d, vsc >> 8);
1850                /* VS_INTRLACE=1 VFILT=filter */
1851                cx25840_write(client, 0x41e, 0x8 | filter);
1852        }
1853        return 0;
1854}
1855
1856/* ----------------------------------------------------------------------- */
1857
1858static void log_video_status(struct i2c_client *client)
1859{
1860        static const char *const fmt_strs[] = {
1861                "0x0",
1862                "NTSC-M", "NTSC-J", "NTSC-4.43",
1863                "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
1864                "0x9", "0xA", "0xB",
1865                "SECAM",
1866                "0xD", "0xE", "0xF"
1867        };
1868
1869        struct cx25840_state *state = to_state(i2c_get_clientdata(client));
1870        u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
1871        u8 gen_stat1 = cx25840_read(client, 0x40d);
1872        u8 gen_stat2 = cx25840_read(client, 0x40e);
1873        int vid_input = state->vid_input;
1874
1875        v4l_info(client, "Video signal:              %spresent\n",
1876                 (gen_stat2 & 0x20) ? "" : "not ");
1877        v4l_info(client, "Detected format:           %s\n",
1878                 fmt_strs[gen_stat1 & 0xf]);
1879
1880        v4l_info(client, "Specified standard:        %s\n",
1881                 vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
1882
1883        if (vid_input >= CX25840_COMPOSITE1 &&
1884            vid_input <= CX25840_COMPOSITE8) {
1885                v4l_info(client, "Specified video input:     Composite %d\n",
1886                         vid_input - CX25840_COMPOSITE1 + 1);
1887        } else {
1888                v4l_info(client,
1889                         "Specified video input:     S-Video (Luma In%d, Chroma In%d)\n",
1890                         (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
1891        }
1892
1893        v4l_info(client, "Specified audioclock freq: %d Hz\n",
1894                 state->audclk_freq);
1895}
1896
1897/* ----------------------------------------------------------------------- */
1898
1899static void log_audio_status(struct i2c_client *client)
1900{
1901        struct cx25840_state *state = to_state(i2c_get_clientdata(client));
1902        u8 download_ctl = cx25840_read(client, 0x803);
1903        u8 mod_det_stat0 = cx25840_read(client, 0x804);
1904        u8 mod_det_stat1 = cx25840_read(client, 0x805);
1905        u8 audio_config = cx25840_read(client, 0x808);
1906        u8 pref_mode = cx25840_read(client, 0x809);
1907        u8 afc0 = cx25840_read(client, 0x80b);
1908        u8 mute_ctl = cx25840_read(client, 0x8d3);
1909        int aud_input = state->aud_input;
1910        char *p;
1911
1912        switch (mod_det_stat0) {
1913        case 0x00:
1914                p = "mono";
1915                break;
1916        case 0x01:
1917                p = "stereo";
1918                break;
1919        case 0x02:
1920                p = "dual";
1921                break;
1922        case 0x04:
1923                p = "tri";
1924                break;
1925        case 0x10:
1926                p = "mono with SAP";
1927                break;
1928        case 0x11:
1929                p = "stereo with SAP";
1930                break;
1931        case 0x12:
1932                p = "dual with SAP";
1933                break;
1934        case 0x14:
1935                p = "tri with SAP";
1936                break;
1937        case 0xfe:
1938                p = "forced mode";
1939                break;
1940        default:
1941                p = "not defined";
1942        }
1943        v4l_info(client, "Detected audio mode:       %s\n", p);
1944
1945        switch (mod_det_stat1) {
1946        case 0x00:
1947                p = "not defined";
1948                break;
1949        case 0x01:
1950                p = "EIAJ";
1951                break;
1952        case 0x02:
1953                p = "A2-M";
1954                break;
1955        case 0x03:
1956                p = "A2-BG";
1957                break;
1958        case 0x04:
1959                p = "A2-DK1";
1960                break;
1961        case 0x05:
1962                p = "A2-DK2";
1963                break;
1964        case 0x06:
1965                p = "A2-DK3";
1966                break;
1967        case 0x07:
1968                p = "A1 (6.0 MHz FM Mono)";
1969                break;
1970        case 0x08:
1971                p = "AM-L";
1972                break;
1973        case 0x09:
1974                p = "NICAM-BG";
1975                break;
1976        case 0x0a:
1977                p = "NICAM-DK";
1978                break;
1979        case 0x0b:
1980                p = "NICAM-I";
1981                break;
1982        case 0x0c:
1983                p = "NICAM-L";
1984                break;
1985        case 0x0d:
1986                p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)";
1987                break;
1988        case 0x0e:
1989                p = "IF FM Radio";
1990                break;
1991        case 0x0f:
1992                p = "BTSC";
1993                break;
1994        case 0x10:
1995                p = "high-deviation FM";
1996                break;
1997        case 0x11:
1998                p = "very high-deviation FM";
1999                break;
2000        case 0xfd:
2001                p = "unknown audio standard";
2002                break;
2003        case 0xfe:
2004                p = "forced audio standard";
2005                break;
2006        case 0xff:
2007                p = "no detected audio standard";
2008                break;
2009        default:
2010                p = "not defined";
2011        }
2012        v4l_info(client, "Detected audio standard:   %s\n", p);
2013        v4l_info(client, "Audio microcontroller:     %s\n",
2014                 (download_ctl & 0x10) ?
2015                 ((mute_ctl & 0x2) ? "detecting" : "running") : "stopped");
2016
2017        switch (audio_config >> 4) {
2018        case 0x00:
2019                p = "undefined";
2020                break;
2021        case 0x01:
2022                p = "BTSC";
2023                break;
2024        case 0x02:
2025                p = "EIAJ";
2026                break;
2027        case 0x03:
2028                p = "A2-M";
2029                break;
2030        case 0x04:
2031                p = "A2-BG";
2032                break;
2033        case 0x05:
2034                p = "A2-DK1";
2035                break;
2036        case 0x06:
2037                p = "A2-DK2";
2038                break;
2039        case 0x07:
2040                p = "A2-DK3";
2041                break;
2042        case 0x08:
2043                p = "A1 (6.0 MHz FM Mono)";
2044                break;
2045        case 0x09:
2046                p = "AM-L";
2047                break;
2048        case 0x0a:
2049                p = "NICAM-BG";
2050                break;
2051        case 0x0b:
2052                p = "NICAM-DK";
2053                break;
2054        case 0x0c:
2055                p = "NICAM-I";
2056                break;
2057        case 0x0d:
2058                p = "NICAM-L";
2059                break;
2060        case 0x0e:
2061                p = "FM radio";
2062                break;
2063        case 0x0f:
2064                p = "automatic detection";
2065                break;
2066        default:
2067                p = "undefined";
2068        }
2069        v4l_info(client, "Configured audio standard: %s\n", p);
2070
2071        if ((audio_config >> 4) < 0xF) {
2072                switch (audio_config & 0xF) {
2073                case 0x00:
2074                        p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)";
2075                        break;
2076                case 0x01:
2077                        p = "MONO2 (LANGUAGE B)";
2078                        break;
2079                case 0x02:
2080                        p = "MONO3 (STEREO forced MONO)";
2081                        break;
2082                case 0x03:
2083                        p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)";
2084                        break;
2085                case 0x04:
2086                        p = "STEREO";
2087                        break;
2088                case 0x05:
2089                        p = "DUAL1 (AB)";
2090                        break;
2091                case 0x06:
2092                        p = "DUAL2 (AC) (FM)";
2093                        break;
2094                case 0x07:
2095                        p = "DUAL3 (BC) (FM)";
2096                        break;
2097                case 0x08:
2098                        p = "DUAL4 (AC) (AM)";
2099                        break;
2100                case 0x09:
2101                        p = "DUAL5 (BC) (AM)";
2102                        break;
2103                case 0x0a:
2104                        p = "SAP";
2105                        break;
2106                default:
2107                        p = "undefined";
2108                }
2109                v4l_info(client, "Configured audio mode:     %s\n", p);
2110        } else {
2111                switch (audio_config & 0xF) {
2112                case 0x00:
2113                        p = "BG";
2114                        break;
2115                case 0x01:
2116                        p = "DK1";
2117                        break;
2118                case 0x02:
2119                        p = "DK2";
2120                        break;
2121                case 0x03:
2122                        p = "DK3";
2123                        break;
2124                case 0x04:
2125                        p = "I";
2126                        break;
2127                case 0x05:
2128                        p = "L";
2129                        break;
2130                case 0x06:
2131                        p = "BTSC";
2132                        break;
2133                case 0x07:
2134                        p = "EIAJ";
2135                        break;
2136                case 0x08:
2137                        p = "A2-M";
2138                        break;
2139                case 0x09:
2140                        p = "FM Radio";
2141                        break;
2142                case 0x0f:
2143                        p = "automatic standard and mode detection";
2144                        break;
2145                default:
2146                        p = "undefined";
2147                }
2148                v4l_info(client, "Configured audio system:   %s\n", p);
2149        }
2150
2151        if (aud_input) {
2152                v4l_info(client, "Specified audio input:     Tuner (In%d)\n",
2153                         aud_input);
2154        } else {
2155                v4l_info(client, "Specified audio input:     External\n");
2156        }
2157
2158        switch (pref_mode & 0xf) {
2159        case 0:
2160                p = "mono/language A";
2161                break;
2162        case 1:
2163                p = "language B";
2164                break;
2165        case 2:
2166                p = "language C";
2167                break;
2168        case 3:
2169                p = "analog fallback";
2170                break;
2171        case 4:
2172                p = "stereo";
2173                break;
2174        case 5:
2175                p = "language AC";
2176                break;
2177        case 6:
2178                p = "language BC";
2179                break;
2180        case 7:
2181                p = "language AB";
2182                break;
2183        default:
2184                p = "undefined";
2185        }
2186        v4l_info(client, "Preferred audio mode:      %s\n", p);
2187
2188        if ((audio_config & 0xf) == 0xf) {
2189                switch ((afc0 >> 3) & 0x3) {
2190                case 0:
2191                        p = "system DK";
2192                        break;
2193                case 1:
2194                        p = "system L";
2195                        break;
2196                case 2:
2197                        p = "autodetect";
2198                        break;
2199                default:
2200                        p = "undefined";
2201                }
2202                v4l_info(client, "Selected 65 MHz format:    %s\n", p);
2203
2204                switch (afc0 & 0x7) {
2205                case 0:
2206                        p = "chroma";
2207                        break;
2208                case 1:
2209                        p = "BTSC";
2210                        break;
2211                case 2:
2212                        p = "EIAJ";
2213                        break;
2214                case 3:
2215                        p = "A2-M";
2216                        break;
2217                case 4:
2218                        p = "autodetect";
2219                        break;
2220                default:
2221                        p = "undefined";
2222                }
2223                v4l_info(client, "Selected 45 MHz format:    %s\n", p);
2224        }
2225}
2226
2227#define CX25840_VCONFIG_OPTION(state, cfg_in, opt_msk)                  \
2228        do {                                                            \
2229                if ((cfg_in) & (opt_msk)) {                             \
2230                        (state)->vid_config &= ~(opt_msk);              \
2231                        (state)->vid_config |= (cfg_in) & (opt_msk);    \
2232                }                                                       \
2233        } while (0)
2234
2235/* apply incoming options to the current vconfig */
2236static void cx25840_vconfig_add(struct cx25840_state *state, u32 cfg_in)
2237{
2238        CX25840_VCONFIG_OPTION(state, cfg_in, CX25840_VCONFIG_FMT_MASK);
2239        CX25840_VCONFIG_OPTION(state, cfg_in, CX25840_VCONFIG_RES_MASK);
2240        CX25840_VCONFIG_OPTION(state, cfg_in, CX25840_VCONFIG_VBIRAW_MASK);
2241        CX25840_VCONFIG_OPTION(state, cfg_in, CX25840_VCONFIG_ANCDATA_MASK);
2242        CX25840_VCONFIG_OPTION(state, cfg_in, CX25840_VCONFIG_TASKBIT_MASK);
2243        CX25840_VCONFIG_OPTION(state, cfg_in, CX25840_VCONFIG_ACTIVE_MASK);
2244        CX25840_VCONFIG_OPTION(state, cfg_in, CX25840_VCONFIG_VALID_MASK);
2245        CX25840_VCONFIG_OPTION(state, cfg_in, CX25840_VCONFIG_HRESETW_MASK);
2246        CX25840_VCONFIG_OPTION(state, cfg_in, CX25840_VCONFIG_CLKGATE_MASK);
2247        CX25840_VCONFIG_OPTION(state, cfg_in, CX25840_VCONFIG_DCMODE_MASK);
2248        CX25840_VCONFIG_OPTION(state, cfg_in, CX25840_VCONFIG_IDID0S_MASK);
2249        CX25840_VCONFIG_OPTION(state, cfg_in, CX25840_VCONFIG_VIPCLAMP_MASK);
2250}
2251
2252/* ----------------------------------------------------------------------- */
2253
2254/*
2255 * Initializes the device in the generic mode.
2256 * For cx2584x chips also adds additional video output settings provided
2257 * in @val parameter (CX25840_VCONFIG_*).
2258 *
2259 * The generic mode disables some of the ivtv-related hacks in this driver.
2260 * For cx2584x chips it also enables setting video output configuration while
2261 * setting it according to datasheet defaults by default.
2262 */
2263static int cx25840_init(struct v4l2_subdev *sd, u32 val)
2264{
2265        struct cx25840_state *state = to_state(sd);
2266
2267        state->generic_mode = true;
2268
2269        if (is_cx2584x(state)) {
2270                /* set datasheet video output defaults */
2271                state->vid_config = CX25840_VCONFIG_FMT_BT656 |
2272                                    CX25840_VCONFIG_RES_8BIT |
2273                                    CX25840_VCONFIG_VBIRAW_DISABLED |
2274                                    CX25840_VCONFIG_ANCDATA_ENABLED |
2275                                    CX25840_VCONFIG_TASKBIT_ONE |
2276                                    CX25840_VCONFIG_ACTIVE_HORIZONTAL |
2277                                    CX25840_VCONFIG_VALID_NORMAL |
2278                                    CX25840_VCONFIG_HRESETW_NORMAL |
2279                                    CX25840_VCONFIG_CLKGATE_NONE |
2280                                    CX25840_VCONFIG_DCMODE_DWORDS |
2281                                    CX25840_VCONFIG_IDID0S_NORMAL |
2282                                    CX25840_VCONFIG_VIPCLAMP_DISABLED;
2283
2284                /* add additional settings */
2285                cx25840_vconfig_add(state, val);
2286        } else {
2287                /* TODO: generic mode needs to be developed for other chips */
2288                WARN_ON(1);
2289        }
2290
2291        return 0;
2292}
2293
2294static int cx25840_reset(struct v4l2_subdev *sd, u32 val)
2295{
2296        struct cx25840_state *state = to_state(sd);
2297        struct i2c_client *client = v4l2_get_subdevdata(sd);
2298
2299        if (is_cx2583x(state))
2300                cx25836_initialize(client);
2301        else if (is_cx2388x(state))
2302                cx23885_initialize(client);
2303        else if (is_cx231xx(state))
2304                cx231xx_initialize(client);
2305        else
2306                cx25840_initialize(client);
2307
2308        state->is_initialized = 1;
2309
2310        return 0;
2311}
2312
2313/*
2314 * This load_fw operation must be called to load the driver's firmware.
2315 * This will load the firmware on the first invocation (further ones are NOP).
2316 * Without this the audio standard detection will fail and you will
2317 * only get mono.
2318 * Alternatively, you can call the reset operation instead of this one.
2319 *
2320 * Since loading the firmware is often problematic when the driver is
2321 * compiled into the kernel I recommend postponing calling this function
2322 * until the first open of the video device. Another reason for
2323 * postponing it is that loading this firmware takes a long time (seconds)
2324 * due to the slow i2c bus speed. So it will speed up the boot process if
2325 * you can avoid loading the fw as long as the video device isn't used.
2326 */
2327static int cx25840_load_fw(struct v4l2_subdev *sd)
2328{
2329        struct cx25840_state *state = to_state(sd);
2330
2331        if (!state->is_initialized) {
2332                /* initialize and load firmware */
2333                cx25840_reset(sd, 0);
2334        }
2335        return 0;
2336}
2337
2338#ifdef CONFIG_VIDEO_ADV_DEBUG
2339static int cx25840_g_register(struct v4l2_subdev *sd,
2340                              struct v4l2_dbg_register *reg)
2341{
2342        struct i2c_client *client = v4l2_get_subdevdata(sd);
2343
2344        reg->size = 1;
2345        reg->val = cx25840_read(client, reg->reg & 0x0fff);
2346        return 0;
2347}
2348
2349static int cx25840_s_register(struct v4l2_subdev *sd,
2350                              const struct v4l2_dbg_register *reg)
2351{
2352        struct i2c_client *client = v4l2_get_subdevdata(sd);
2353
2354        cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
2355        return 0;
2356}
2357#endif
2358
2359static int cx25840_s_audio_stream(struct v4l2_subdev *sd, int enable)
2360{
2361        struct cx25840_state *state = to_state(sd);
2362        struct i2c_client *client = v4l2_get_subdevdata(sd);
2363        u8 v;
2364
2365        if (is_cx2583x(state) || is_cx2388x(state) || is_cx231xx(state))
2366                return 0;
2367
2368        v4l_dbg(1, cx25840_debug, client, "%s audio output\n",
2369                enable ? "enable" : "disable");
2370
2371        if (enable) {
2372                v = cx25840_read(client, 0x115) | 0x80;
2373                cx25840_write(client, 0x115, v);
2374                v = cx25840_read(client, 0x116) | 0x03;
2375                cx25840_write(client, 0x116, v);
2376        } else {
2377                v = cx25840_read(client, 0x115) & ~(0x80);
2378                cx25840_write(client, 0x115, v);
2379                v = cx25840_read(client, 0x116) & ~(0x03);
2380                cx25840_write(client, 0x116, v);
2381        }
2382        return 0;
2383}
2384
2385static int cx25840_s_stream(struct v4l2_subdev *sd, int enable)
2386{
2387        struct cx25840_state *state = to_state(sd);
2388        struct i2c_client *client = v4l2_get_subdevdata(sd);
2389        u8 v;
2390
2391        v4l_dbg(1, cx25840_debug, client, "%s video output\n",
2392                enable ? "enable" : "disable");
2393
2394        /*
2395         * It's not clear what should be done for these devices.
2396         * The original code used the same addresses as for the cx25840, but
2397         * those addresses do something else entirely on the cx2388x and
2398         * cx231xx. Since it never did anything in the first place, just do
2399         * nothing.
2400         */
2401        if (is_cx2388x(state) || is_cx231xx(state))
2402                return 0;
2403
2404        if (enable) {
2405                v = cx25840_read(client, 0x115) | 0x0c;
2406                cx25840_write(client, 0x115, v);
2407                v = cx25840_read(client, 0x116) | 0x04;
2408                cx25840_write(client, 0x116, v);
2409        } else {
2410                v = cx25840_read(client, 0x115) & ~(0x0c);
2411                cx25840_write(client, 0x115, v);
2412                v = cx25840_read(client, 0x116) & ~(0x04);
2413                cx25840_write(client, 0x116, v);
2414        }
2415        return 0;
2416}
2417
2418/* Query the current detected video format */
2419static int cx25840_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
2420{
2421        struct i2c_client *client = v4l2_get_subdevdata(sd);
2422
2423        static const v4l2_std_id stds[] = {
2424                /* 0000 */ V4L2_STD_UNKNOWN,
2425
2426                /* 0001 */ V4L2_STD_NTSC_M,
2427                /* 0010 */ V4L2_STD_NTSC_M_JP,
2428                /* 0011 */ V4L2_STD_NTSC_443,
2429                /* 0100 */ V4L2_STD_PAL,
2430                /* 0101 */ V4L2_STD_PAL_M,
2431                /* 0110 */ V4L2_STD_PAL_N,
2432                /* 0111 */ V4L2_STD_PAL_Nc,
2433                /* 1000 */ V4L2_STD_PAL_60,
2434
2435                /* 1001 */ V4L2_STD_UNKNOWN,
2436                /* 1010 */ V4L2_STD_UNKNOWN,
2437                /* 1011 */ V4L2_STD_UNKNOWN,
2438                /* 1100 */ V4L2_STD_SECAM,
2439                /* 1101 */ V4L2_STD_UNKNOWN,
2440                /* 1110 */ V4L2_STD_UNKNOWN,
2441                /* 1111 */ V4L2_STD_UNKNOWN
2442        };
2443
2444        u32 fmt = (cx25840_read4(client, 0x40c) >> 8) & 0xf;
2445        *std = stds[fmt];
2446
2447        v4l_dbg(1, cx25840_debug, client,
2448                "querystd fmt = %x, v4l2_std_id = 0x%x\n",
2449                fmt, (unsigned int)stds[fmt]);
2450
2451        return 0;
2452}
2453
2454static int cx25840_g_input_status(struct v4l2_subdev *sd, u32 *status)
2455{
2456        struct i2c_client *client = v4l2_get_subdevdata(sd);
2457
2458        /*
2459         * A limited function that checks for signal status and returns
2460         * the state.
2461         */
2462
2463        /* Check for status of Horizontal lock (SRC lock isn't reliable) */
2464        if ((cx25840_read4(client, 0x40c) & 0x00010000) == 0)
2465                *status |= V4L2_IN_ST_NO_SIGNAL;
2466
2467        return 0;
2468}
2469
2470static int cx25840_g_std(struct v4l2_subdev *sd, v4l2_std_id *std)
2471{
2472        struct cx25840_state *state = to_state(sd);
2473
2474        *std = state->std;
2475
2476        return 0;
2477}
2478
2479static int cx25840_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
2480{
2481        struct cx25840_state *state = to_state(sd);
2482        struct i2c_client *client = v4l2_get_subdevdata(sd);
2483
2484        if (state->radio == 0 && state->std == std)
2485                return 0;
2486        state->radio = 0;
2487        state->std = std;
2488        return set_v4lstd(client);
2489}
2490
2491static int cx25840_s_radio(struct v4l2_subdev *sd)
2492{
2493        struct cx25840_state *state = to_state(sd);
2494
2495        state->radio = 1;
2496        return 0;
2497}
2498
2499static int cx25840_s_video_routing(struct v4l2_subdev *sd,
2500                                   u32 input, u32 output, u32 config)
2501{
2502        struct cx25840_state *state = to_state(sd);
2503        struct i2c_client *client = v4l2_get_subdevdata(sd);
2504
2505        if (is_cx23888(state))
2506                cx23888_std_setup(client);
2507
2508        if (is_cx2584x(state) && state->generic_mode && config) {
2509                cx25840_vconfig_add(state, config);
2510                cx25840_vconfig_apply(client);
2511        }
2512
2513        return set_input(client, input, state->aud_input);
2514}
2515
2516static int cx25840_s_audio_routing(struct v4l2_subdev *sd,
2517                                   u32 input, u32 output, u32 config)
2518{
2519        struct cx25840_state *state = to_state(sd);
2520        struct i2c_client *client = v4l2_get_subdevdata(sd);
2521
2522        if (is_cx23888(state))
2523                cx23888_std_setup(client);
2524        return set_input(client, state->vid_input, input);
2525}
2526
2527static int cx25840_s_frequency(struct v4l2_subdev *sd,
2528                               const struct v4l2_frequency *freq)
2529{
2530        struct i2c_client *client = v4l2_get_subdevdata(sd);
2531
2532        input_change(client);
2533        return 0;
2534}
2535
2536static int cx25840_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
2537{
2538        struct cx25840_state *state = to_state(sd);
2539        struct i2c_client *client = v4l2_get_subdevdata(sd);
2540        u8 vpres = cx25840_read(client, 0x40e) & 0x20;
2541        u8 mode;
2542        int val = 0;
2543
2544        if (state->radio)
2545                return 0;
2546
2547        vt->signal = vpres ? 0xffff : 0x0;
2548        if (is_cx2583x(state))
2549                return 0;
2550
2551        vt->capability |= V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
2552                          V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
2553
2554        mode = cx25840_read(client, 0x804);
2555
2556        /* get rxsubchans and audmode */
2557        if ((mode & 0xf) == 1)
2558                val |= V4L2_TUNER_SUB_STEREO;
2559        else
2560                val |= V4L2_TUNER_SUB_MONO;
2561
2562        if (mode == 2 || mode == 4)
2563                val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
2564
2565        if (mode & 0x10)
2566                val |= V4L2_TUNER_SUB_SAP;
2567
2568        vt->rxsubchans = val;
2569        vt->audmode = state->audmode;
2570        return 0;
2571}
2572
2573static int cx25840_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
2574{
2575        struct cx25840_state *state = to_state(sd);
2576        struct i2c_client *client = v4l2_get_subdevdata(sd);
2577
2578        if (state->radio || is_cx2583x(state))
2579                return 0;
2580
2581        switch (vt->audmode) {
2582        case V4L2_TUNER_MODE_MONO:
2583                /*
2584                 * mono      -> mono
2585                 * stereo    -> mono
2586                 * bilingual -> lang1
2587                 */
2588                cx25840_and_or(client, 0x809, ~0xf, 0x00);
2589                break;
2590        case V4L2_TUNER_MODE_STEREO:
2591        case V4L2_TUNER_MODE_LANG1:
2592                /*
2593                 * mono      -> mono
2594                 * stereo    -> stereo
2595                 * bilingual -> lang1
2596                 */
2597                cx25840_and_or(client, 0x809, ~0xf, 0x04);
2598                break;
2599        case V4L2_TUNER_MODE_LANG1_LANG2:
2600                /*
2601                 * mono      -> mono
2602                 * stereo    -> stereo
2603                 * bilingual -> lang1/lang2
2604                 */
2605                cx25840_and_or(client, 0x809, ~0xf, 0x07);
2606                break;
2607        case V4L2_TUNER_MODE_LANG2:
2608                /*
2609                 * mono      -> mono
2610                 * stereo    -> stereo
2611                 * bilingual -> lang2
2612                 */
2613                cx25840_and_or(client, 0x809, ~0xf, 0x01);
2614                break;
2615        default:
2616                return -EINVAL;
2617        }
2618        state->audmode = vt->audmode;
2619        return 0;
2620}
2621
2622static int cx25840_log_status(struct v4l2_subdev *sd)
2623{
2624        struct cx25840_state *state = to_state(sd);
2625        struct i2c_client *client = v4l2_get_subdevdata(sd);
2626
2627        log_video_status(client);
2628        if (!is_cx2583x(state))
2629                log_audio_status(client);
2630        cx25840_ir_log_status(sd);
2631        v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
2632        return 0;
2633}
2634
2635static int cx23885_irq_handler(struct v4l2_subdev *sd, u32 status,
2636                               bool *handled)
2637{
2638        struct cx25840_state *state = to_state(sd);
2639        struct i2c_client *c = v4l2_get_subdevdata(sd);
2640        u8 irq_stat, aud_stat, aud_en, ir_stat, ir_en;
2641        u32 vid_stat, aud_mc_stat;
2642        bool block_handled;
2643        int ret = 0;
2644
2645        irq_stat = cx25840_read(c, CX23885_PIN_CTRL_IRQ_REG);
2646        v4l_dbg(2, cx25840_debug, c, "AV Core IRQ status (entry): %s %s %s\n",
2647                irq_stat & CX23885_PIN_CTRL_IRQ_IR_STAT ? "ir" : "  ",
2648                irq_stat & CX23885_PIN_CTRL_IRQ_AUD_STAT ? "aud" : "   ",
2649                irq_stat & CX23885_PIN_CTRL_IRQ_VID_STAT ? "vid" : "   ");
2650
2651        if ((is_cx23885(state) || is_cx23887(state))) {
2652                ir_stat = cx25840_read(c, CX25840_IR_STATS_REG);
2653                ir_en = cx25840_read(c, CX25840_IR_IRQEN_REG);
2654                v4l_dbg(2, cx25840_debug, c,
2655                        "AV Core ir IRQ status: %#04x disables: %#04x\n",
2656                        ir_stat, ir_en);
2657                if (irq_stat & CX23885_PIN_CTRL_IRQ_IR_STAT) {
2658                        block_handled = false;
2659                        ret = cx25840_ir_irq_handler(sd,
2660                                                     status, &block_handled);
2661                        if (block_handled)
2662                                *handled = true;
2663                }
2664        }
2665
2666        aud_stat = cx25840_read(c, CX25840_AUD_INT_STAT_REG);
2667        aud_en = cx25840_read(c, CX25840_AUD_INT_CTRL_REG);
2668        v4l_dbg(2, cx25840_debug, c,
2669                "AV Core audio IRQ status: %#04x disables: %#04x\n",
2670                aud_stat, aud_en);
2671        aud_mc_stat = cx25840_read4(c, CX23885_AUD_MC_INT_MASK_REG);
2672        v4l_dbg(2, cx25840_debug, c,
2673                "AV Core audio MC IRQ status: %#06x enables: %#06x\n",
2674                aud_mc_stat >> CX23885_AUD_MC_INT_STAT_SHFT,
2675                aud_mc_stat & CX23885_AUD_MC_INT_CTRL_BITS);
2676        if (irq_stat & CX23885_PIN_CTRL_IRQ_AUD_STAT) {
2677                if (aud_stat) {
2678                        cx25840_write(c, CX25840_AUD_INT_STAT_REG, aud_stat);
2679                        *handled = true;
2680                }
2681        }
2682
2683        vid_stat = cx25840_read4(c, CX25840_VID_INT_STAT_REG);
2684        v4l_dbg(2, cx25840_debug, c,
2685                "AV Core video IRQ status: %#06x disables: %#06x\n",
2686                vid_stat & CX25840_VID_INT_STAT_BITS,
2687                vid_stat >> CX25840_VID_INT_MASK_SHFT);
2688        if (irq_stat & CX23885_PIN_CTRL_IRQ_VID_STAT) {
2689                if (vid_stat & CX25840_VID_INT_STAT_BITS) {
2690                        cx25840_write4(c, CX25840_VID_INT_STAT_REG, vid_stat);
2691                        *handled = true;
2692                }
2693        }
2694
2695        irq_stat = cx25840_read(c, CX23885_PIN_CTRL_IRQ_REG);
2696        v4l_dbg(2, cx25840_debug, c, "AV Core IRQ status (exit): %s %s %s\n",
2697                irq_stat & CX23885_PIN_CTRL_IRQ_IR_STAT ? "ir" : "  ",
2698                irq_stat & CX23885_PIN_CTRL_IRQ_AUD_STAT ? "aud" : "   ",
2699                irq_stat & CX23885_PIN_CTRL_IRQ_VID_STAT ? "vid" : "   ");
2700
2701        return ret;
2702}
2703
2704static int cx25840_irq_handler(struct v4l2_subdev *sd, u32 status,
2705                               bool *handled)
2706{
2707        struct cx25840_state *state = to_state(sd);
2708
2709        *handled = false;
2710
2711        /* Only support the CX2388[578] AV Core for now */
2712        if (is_cx2388x(state))
2713                return cx23885_irq_handler(sd, status, handled);
2714
2715        return -ENODEV;
2716}
2717
2718/* ----------------------------------------------------------------------- */
2719
2720#define DIF_PLL_FREQ_WORD       (0x300)
2721#define DIF_BPF_COEFF01         (0x348)
2722#define DIF_BPF_COEFF23         (0x34c)
2723#define DIF_BPF_COEFF45         (0x350)
2724#define DIF_BPF_COEFF67         (0x354)
2725#define DIF_BPF_COEFF89         (0x358)
2726#define DIF_BPF_COEFF1011       (0x35c)
2727#define DIF_BPF_COEFF1213       (0x360)
2728#define DIF_BPF_COEFF1415       (0x364)
2729#define DIF_BPF_COEFF1617       (0x368)
2730#define DIF_BPF_COEFF1819       (0x36c)
2731#define DIF_BPF_COEFF2021       (0x370)
2732#define DIF_BPF_COEFF2223       (0x374)
2733#define DIF_BPF_COEFF2425       (0x378)
2734#define DIF_BPF_COEFF2627       (0x37c)
2735#define DIF_BPF_COEFF2829       (0x380)
2736#define DIF_BPF_COEFF3031       (0x384)
2737#define DIF_BPF_COEFF3233       (0x388)
2738#define DIF_BPF_COEFF3435       (0x38c)
2739#define DIF_BPF_COEFF36         (0x390)
2740
2741static void cx23885_dif_setup(struct i2c_client *client, u32 ifHz)
2742{
2743        u64 pll_freq;
2744        u32 pll_freq_word;
2745
2746        v4l_dbg(1, cx25840_debug, client, "%s(%d)\n", __func__, ifHz);
2747
2748        /* Assuming TV */
2749        /* Calculate the PLL frequency word based on the adjusted ifHz */
2750        pll_freq = div_u64((u64)ifHz * 268435456, 50000000);
2751        pll_freq_word = (u32)pll_freq;
2752
2753        cx25840_write4(client, DIF_PLL_FREQ_WORD,  pll_freq_word);
2754
2755        /* Round down to the nearest 100KHz */
2756        ifHz = (ifHz / 100000) * 100000;
2757
2758        if (ifHz < 3000000)
2759                ifHz = 3000000;
2760
2761        if (ifHz > 16000000)
2762                ifHz = 16000000;
2763
2764        v4l_dbg(1, cx25840_debug, client, "%s(%d) again\n", __func__, ifHz);
2765
2766        switch (ifHz) {
2767        case 3000000:
2768                cx25840_write4(client, DIF_BPF_COEFF01, 0x00000002);
2769                cx25840_write4(client, DIF_BPF_COEFF23, 0x00080012);
2770                cx25840_write4(client, DIF_BPF_COEFF45, 0x001e0024);
2771                cx25840_write4(client, DIF_BPF_COEFF67, 0x001bfff8);
2772                cx25840_write4(client, DIF_BPF_COEFF89, 0xffb4ff50);
2773                cx25840_write4(client, DIF_BPF_COEFF1011, 0xfed8fe68);
2774                cx25840_write4(client, DIF_BPF_COEFF1213, 0xfe24fe34);
2775                cx25840_write4(client, DIF_BPF_COEFF1415, 0xfebaffc7);
2776                cx25840_write4(client, DIF_BPF_COEFF1617, 0x014d031f);
2777                cx25840_write4(client, DIF_BPF_COEFF1819, 0x04f0065d);
2778                cx25840_write4(client, DIF_BPF_COEFF2021, 0x07010688);
2779                cx25840_write4(client, DIF_BPF_COEFF2223, 0x04c901d6);
2780                cx25840_write4(client, DIF_BPF_COEFF2425, 0xfe00f9d3);
2781                cx25840_write4(client, DIF_BPF_COEFF2627, 0xf600f342);
2782                cx25840_write4(client, DIF_BPF_COEFF2829, 0xf235f337);
2783                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf64efb22);
2784                cx25840_write4(client, DIF_BPF_COEFF3233, 0x0105070f);
2785                cx25840_write4(client, DIF_BPF_COEFF3435, 0x0c460fce);
2786                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
2787                break;
2788
2789        case 3100000:
2790                cx25840_write4(client, DIF_BPF_COEFF01, 0x00000001);
2791                cx25840_write4(client, DIF_BPF_COEFF23, 0x00070012);
2792                cx25840_write4(client, DIF_BPF_COEFF45, 0x00220032);
2793                cx25840_write4(client, DIF_BPF_COEFF67, 0x00370026);
2794                cx25840_write4(client, DIF_BPF_COEFF89, 0xfff0ff91);
2795                cx25840_write4(client, DIF_BPF_COEFF1011, 0xff0efe7c);
2796                cx25840_write4(client, DIF_BPF_COEFF1213, 0xfe01fdcc);
2797                cx25840_write4(client, DIF_BPF_COEFF1415, 0xfe0afedb);
2798                cx25840_write4(client, DIF_BPF_COEFF1617, 0x00440224);
2799                cx25840_write4(client, DIF_BPF_COEFF1819, 0x0434060c);
2800                cx25840_write4(client, DIF_BPF_COEFF2021, 0x0738074e);
2801                cx25840_write4(client, DIF_BPF_COEFF2223, 0x06090361);
2802                cx25840_write4(client, DIF_BPF_COEFF2425, 0xff99fb39);
2803                cx25840_write4(client, DIF_BPF_COEFF2627, 0xf6fef3b6);
2804                cx25840_write4(client, DIF_BPF_COEFF2829, 0xf21af2a5);
2805                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf573fa33);
2806                cx25840_write4(client, DIF_BPF_COEFF3233, 0x0034067d);
2807                cx25840_write4(client, DIF_BPF_COEFF3435, 0x0bfb0fb9);
2808                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
2809                break;
2810
2811        case 3200000:
2812                cx25840_write4(client, DIF_BPF_COEFF01, 0x00000000);
2813                cx25840_write4(client, DIF_BPF_COEFF23, 0x0004000e);
2814                cx25840_write4(client, DIF_BPF_COEFF45, 0x00200038);
2815                cx25840_write4(client, DIF_BPF_COEFF67, 0x004c004f);
2816                cx25840_write4(client, DIF_BPF_COEFF89, 0x002fffdf);
2817                cx25840_write4(client, DIF_BPF_COEFF1011, 0xff5cfeb6);
2818                cx25840_write4(client, DIF_BPF_COEFF1213, 0xfe0dfd92);
2819                cx25840_write4(client, DIF_BPF_COEFF1415, 0xfd7ffe03);
2820                cx25840_write4(client, DIF_BPF_COEFF1617, 0xff36010a);
2821                cx25840_write4(client, DIF_BPF_COEFF1819, 0x03410575);
2822                cx25840_write4(client, DIF_BPF_COEFF2021, 0x072607d2);
2823                cx25840_write4(client, DIF_BPF_COEFF2223, 0x071804d5);
2824                cx25840_write4(client, DIF_BPF_COEFF2425, 0x0134fcb7);
2825                cx25840_write4(client, DIF_BPF_COEFF2627, 0xf81ff451);
2826                cx25840_write4(client, DIF_BPF_COEFF2829, 0xf223f22e);
2827                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf4a7f94b);
2828                cx25840_write4(client, DIF_BPF_COEFF3233, 0xff6405e8);
2829                cx25840_write4(client, DIF_BPF_COEFF3435, 0x0bae0fa4);
2830                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
2831                break;
2832
2833        case 3300000:
2834                cx25840_write4(client, DIF_BPF_COEFF01, 0x0000ffff);
2835                cx25840_write4(client, DIF_BPF_COEFF23, 0x00000008);
2836                cx25840_write4(client, DIF_BPF_COEFF45, 0x001a0036);
2837                cx25840_write4(client, DIF_BPF_COEFF67, 0x0056006d);
2838                cx25840_write4(client, DIF_BPF_COEFF89, 0x00670030);
2839                cx25840_write4(client, DIF_BPF_COEFF1011, 0xffbdff10);
2840                cx25840_write4(client, DIF_BPF_COEFF1213, 0xfe46fd8d);
2841                cx25840_write4(client, DIF_BPF_COEFF1415, 0xfd25fd4f);
2842                cx25840_write4(client, DIF_BPF_COEFF1617, 0xfe35ffe0);
2843                cx25840_write4(client, DIF_BPF_COEFF1819, 0x0224049f);
2844                cx25840_write4(client, DIF_BPF_COEFF2021, 0x06c9080e);
2845                cx25840_write4(client, DIF_BPF_COEFF2223, 0x07ef0627);
2846                cx25840_write4(client, DIF_BPF_COEFF2425, 0x02c9fe45);
2847                cx25840_write4(client, DIF_BPF_COEFF2627, 0xf961f513);
2848                cx25840_write4(client, DIF_BPF_COEFF2829, 0xf250f1d2);
2849                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf3ecf869);
2850                cx25840_write4(client, DIF_BPF_COEFF3233, 0xfe930552);
2851                cx25840_write4(client, DIF_BPF_COEFF3435, 0x0b5f0f8f);
2852                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
2853                break;
2854
2855        case 3400000:
2856                cx25840_write4(client, DIF_BPF_COEFF01, 0xfffffffe);
2857                cx25840_write4(client, DIF_BPF_COEFF23, 0xfffd0001);
2858                cx25840_write4(client, DIF_BPF_COEFF45, 0x000f002c);
2859                cx25840_write4(client, DIF_BPF_COEFF67, 0x0054007d);
2860                cx25840_write4(client, DIF_BPF_COEFF89, 0x0093007c);
2861                cx25840_write4(client, DIF_BPF_COEFF1011, 0x0024ff82);
2862                cx25840_write4(client, DIF_BPF_COEFF1213, 0xfea6fdbb);
2863                cx25840_write4(client, DIF_BPF_COEFF1415, 0xfd03fcca);
2864                cx25840_write4(client, DIF_BPF_COEFF1617, 0xfd51feb9);
2865                cx25840_write4(client, DIF_BPF_COEFF1819, 0x00eb0392);
2866                cx25840_write4(client, DIF_BPF_COEFF2021, 0x06270802);
2867                cx25840_write4(client, DIF_BPF_COEFF2223, 0x08880750);
2868                cx25840_write4(client, DIF_BPF_COEFF2425, 0x044dffdb);
2869                cx25840_write4(client, DIF_BPF_COEFF2627, 0xfabdf5f8);
2870                cx25840_write4(client, DIF_BPF_COEFF2829, 0xf2a0f193);
2871                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf342f78f);
2872                cx25840_write4(client, DIF_BPF_COEFF3233, 0xfdc404b9);
2873                cx25840_write4(client, DIF_BPF_COEFF3435, 0x0b0e0f78);
2874                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
2875                break;
2876
2877        case 3500000:
2878                cx25840_write4(client, DIF_BPF_COEFF01, 0xfffffffd);
2879                cx25840_write4(client, DIF_BPF_COEFF23, 0xfffafff9);
2880                cx25840_write4(client, DIF_BPF_COEFF45, 0x0002001b);
2881                cx25840_write4(client, DIF_BPF_COEFF67, 0x0046007d);
2882                cx25840_write4(client, DIF_BPF_COEFF89, 0x00ad00ba);
2883                cx25840_write4(client, DIF_BPF_COEFF1011, 0x00870000);
2884                cx25840_write4(client, DIF_BPF_COEFF1213, 0xff26fe1a);
2885                cx25840_write4(client, DIF_BPF_COEFF1415, 0xfd1bfc7e);
2886                cx25840_write4(client, DIF_BPF_COEFF1617, 0xfc99fda4);
2887                cx25840_write4(client, DIF_BPF_COEFF1819, 0xffa5025c);
2888                cx25840_write4(client, DIF_BPF_COEFF2021, 0x054507ad);
2889                cx25840_write4(client, DIF_BPF_COEFF2223, 0x08dd0847);
2890                cx25840_write4(client, DIF_BPF_COEFF2425, 0x05b80172);
2891                cx25840_write4(client, DIF_BPF_COEFF2627, 0xfc2ef6ff);
2892                cx25840_write4(client, DIF_BPF_COEFF2829, 0xf313f170);
2893                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf2abf6bd);
2894                cx25840_write4(client, DIF_BPF_COEFF3233, 0xfcf6041f);
2895                cx25840_write4(client, DIF_BPF_COEFF3435, 0x0abc0f61);
2896                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
2897                break;
2898
2899        case 3600000:
2900                cx25840_write4(client, DIF_BPF_COEFF01, 0xfffffffd);
2901                cx25840_write4(client, DIF_BPF_COEFF23, 0xfff8fff3);
2902                cx25840_write4(client, DIF_BPF_COEFF45, 0xfff50006);
2903                cx25840_write4(client, DIF_BPF_COEFF67, 0x002f006c);
2904                cx25840_write4(client, DIF_BPF_COEFF89, 0x00b200e3);
2905                cx25840_write4(client, DIF_BPF_COEFF1011, 0x00dc007e);
2906                cx25840_write4(client, DIF_BPF_COEFF1213, 0xffb9fea0);
2907                cx25840_write4(client, DIF_BPF_COEFF1415, 0xfd6bfc71);
2908                cx25840_write4(client, DIF_BPF_COEFF1617, 0xfc17fcb1);
2909                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfe65010b);
2910                cx25840_write4(client, DIF_BPF_COEFF2021, 0x042d0713);
2911                cx25840_write4(client, DIF_BPF_COEFF2223, 0x08ec0906);
2912                cx25840_write4(client, DIF_BPF_COEFF2425, 0x07020302);
2913                cx25840_write4(client, DIF_BPF_COEFF2627, 0xfdaff823);
2914                cx25840_write4(client, DIF_BPF_COEFF2829, 0xf3a7f16a);
2915                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf228f5f5);
2916                cx25840_write4(client, DIF_BPF_COEFF3233, 0xfc2a0384);
2917                cx25840_write4(client, DIF_BPF_COEFF3435, 0x0a670f4a);
2918                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
2919                break;
2920
2921        case 3700000:
2922                cx25840_write4(client, DIF_BPF_COEFF01, 0x0000fffd);
2923                cx25840_write4(client, DIF_BPF_COEFF23, 0xfff7ffef);
2924                cx25840_write4(client, DIF_BPF_COEFF45, 0xffe9fff1);
2925                cx25840_write4(client, DIF_BPF_COEFF67, 0x0010004d);
2926                cx25840_write4(client, DIF_BPF_COEFF89, 0x00a100f2);
2927                cx25840_write4(client, DIF_BPF_COEFF1011, 0x011a00f0);
2928                cx25840_write4(client, DIF_BPF_COEFF1213, 0x0053ff44);
2929                cx25840_write4(client, DIF_BPF_COEFF1415, 0xfdedfca2);
2930                cx25840_write4(client, DIF_BPF_COEFF1617, 0xfbd3fbef);
2931                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfd39ffae);
2932                cx25840_write4(client, DIF_BPF_COEFF2021, 0x02ea0638);
2933                cx25840_write4(client, DIF_BPF_COEFF2223, 0x08b50987);
2934                cx25840_write4(client, DIF_BPF_COEFF2425, 0x08230483);
2935                cx25840_write4(client, DIF_BPF_COEFF2627, 0xff39f960);
2936                cx25840_write4(client, DIF_BPF_COEFF2829, 0xf45bf180);
2937                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf1b8f537);
2938                cx25840_write4(client, DIF_BPF_COEFF3233, 0xfb6102e7);
2939                cx25840_write4(client, DIF_BPF_COEFF3435, 0x0a110f32);
2940                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
2941                break;
2942
2943        case 3800000:
2944                cx25840_write4(client, DIF_BPF_COEFF01, 0x0000fffe);
2945                cx25840_write4(client, DIF_BPF_COEFF23, 0xfff9ffee);
2946                cx25840_write4(client, DIF_BPF_COEFF45, 0xffe1ffdd);
2947                cx25840_write4(client, DIF_BPF_COEFF67, 0xfff00024);
2948                cx25840_write4(client, DIF_BPF_COEFF89, 0x007c00e5);
2949                cx25840_write4(client, DIF_BPF_COEFF1011, 0x013a014a);
2950                cx25840_write4(client, DIF_BPF_COEFF1213, 0x00e6fff8);
2951                cx25840_write4(client, DIF_BPF_COEFF1415, 0xfe98fd0f);
2952                cx25840_write4(client, DIF_BPF_COEFF1617, 0xfbd3fb67);
2953                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfc32fe54);
2954                cx25840_write4(client, DIF_BPF_COEFF2021, 0x01880525);
2955                cx25840_write4(client, DIF_BPF_COEFF2223, 0x083909c7);
2956                cx25840_write4(client, DIF_BPF_COEFF2425, 0x091505ee);
2957                cx25840_write4(client, DIF_BPF_COEFF2627, 0x00c7fab3);
2958                cx25840_write4(client, DIF_BPF_COEFF2829, 0xf52df1b4);
2959                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf15df484);
2960                cx25840_write4(client, DIF_BPF_COEFF3233, 0xfa9b0249);
2961                cx25840_write4(client, DIF_BPF_COEFF3435, 0x09ba0f19);
2962                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
2963                break;
2964
2965        case 3900000:
2966                cx25840_write4(client, DIF_BPF_COEFF01, 0x00000000);
2967                cx25840_write4(client, DIF_BPF_COEFF23, 0xfffbfff0);
2968                cx25840_write4(client, DIF_BPF_COEFF45, 0xffdeffcf);
2969                cx25840_write4(client, DIF_BPF_COEFF67, 0xffd1fff6);
2970                cx25840_write4(client, DIF_BPF_COEFF89, 0x004800be);
2971                cx25840_write4(client, DIF_BPF_COEFF1011, 0x01390184);
2972                cx25840_write4(client, DIF_BPF_COEFF1213, 0x016300ac);
2973                cx25840_write4(client, DIF_BPF_COEFF1415, 0xff5efdb1);
2974                cx25840_write4(client, DIF_BPF_COEFF1617, 0xfc17fb23);
2975                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfb5cfd0d);
2976                cx25840_write4(client, DIF_BPF_COEFF2021, 0x001703e4);
2977                cx25840_write4(client, DIF_BPF_COEFF2223, 0x077b09c4);
2978                cx25840_write4(client, DIF_BPF_COEFF2425, 0x09d2073c);
2979                cx25840_write4(client, DIF_BPF_COEFF2627, 0x0251fc18);
2980                cx25840_write4(client, DIF_BPF_COEFF2829, 0xf61cf203);
2981                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf118f3dc);
2982                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf9d801aa);
2983                cx25840_write4(client, DIF_BPF_COEFF3435, 0x09600eff);
2984                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
2985                break;
2986
2987        case 4000000:
2988                cx25840_write4(client, DIF_BPF_COEFF01, 0x00000001);
2989                cx25840_write4(client, DIF_BPF_COEFF23, 0xfffefff4);
2990                cx25840_write4(client, DIF_BPF_COEFF45, 0xffe1ffc8);
2991                cx25840_write4(client, DIF_BPF_COEFF67, 0xffbaffca);
2992                cx25840_write4(client, DIF_BPF_COEFF89, 0x000b0082);
2993                cx25840_write4(client, DIF_BPF_COEFF1011, 0x01170198);
2994                cx25840_write4(client, DIF_BPF_COEFF1213, 0x01c10152);
2995                cx25840_write4(client, DIF_BPF_COEFF1415, 0x0030fe7b);
2996                cx25840_write4(client, DIF_BPF_COEFF1617, 0xfc99fb24);
2997                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfac3fbe9);
2998                cx25840_write4(client, DIF_BPF_COEFF2021, 0xfea5027f);
2999                cx25840_write4(client, DIF_BPF_COEFF2223, 0x0683097f);
3000                cx25840_write4(client, DIF_BPF_COEFF2425, 0x0a560867);
3001                cx25840_write4(client, DIF_BPF_COEFF2627, 0x03d2fd89);
3002                cx25840_write4(client, DIF_BPF_COEFF2829, 0xf723f26f);
3003                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf0e8f341);
3004                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf919010a);
3005                cx25840_write4(client, DIF_BPF_COEFF3435, 0x09060ee5);
3006                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3007                break;
3008
3009        case 4100000:
3010                cx25840_write4(client, DIF_BPF_COEFF01, 0x00010002);
3011                cx25840_write4(client, DIF_BPF_COEFF23, 0x0002fffb);
3012                cx25840_write4(client, DIF_BPF_COEFF45, 0xffe8ffca);
3013                cx25840_write4(client, DIF_BPF_COEFF67, 0xffacffa4);
3014                cx25840_write4(client, DIF_BPF_COEFF89, 0xffcd0036);
3015                cx25840_write4(client, DIF_BPF_COEFF1011, 0x00d70184);
3016                cx25840_write4(client, DIF_BPF_COEFF1213, 0x01f601dc);
3017                cx25840_write4(client, DIF_BPF_COEFF1415, 0x00ffff60);
3018                cx25840_write4(client, DIF_BPF_COEFF1617, 0xfd51fb6d);
3019                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfa6efaf5);
3020                cx25840_write4(client, DIF_BPF_COEFF2021, 0xfd410103);
3021                cx25840_write4(client, DIF_BPF_COEFF2223, 0x055708f9);
3022                cx25840_write4(client, DIF_BPF_COEFF2425, 0x0a9e0969);
3023                cx25840_write4(client, DIF_BPF_COEFF2627, 0x0543ff02);
3024                cx25840_write4(client, DIF_BPF_COEFF2829, 0xf842f2f5);
3025                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf0cef2b2);
3026                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf85e006b);
3027                cx25840_write4(client, DIF_BPF_COEFF3435, 0x08aa0ecb);
3028                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3029                break;
3030
3031        case 4200000:
3032                cx25840_write4(client, DIF_BPF_COEFF01, 0x00010003);
3033                cx25840_write4(client, DIF_BPF_COEFF23, 0x00050003);
3034                cx25840_write4(client, DIF_BPF_COEFF45, 0xfff3ffd3);
3035                cx25840_write4(client, DIF_BPF_COEFF67, 0xffaaff8b);
3036                cx25840_write4(client, DIF_BPF_COEFF89, 0xff95ffe5);
3037                cx25840_write4(client, DIF_BPF_COEFF1011, 0x0080014a);
3038                cx25840_write4(client, DIF_BPF_COEFF1213, 0x01fe023f);
3039                cx25840_write4(client, DIF_BPF_COEFF1415, 0x01ba0050);
3040                cx25840_write4(client, DIF_BPF_COEFF1617, 0xfe35fbf8);
3041                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfa62fa3b);
3042                cx25840_write4(client, DIF_BPF_COEFF2021, 0xfbf9ff7e);
3043                cx25840_write4(client, DIF_BPF_COEFF2223, 0x04010836);
3044                cx25840_write4(client, DIF_BPF_COEFF2425, 0x0aa90a3d);
3045                cx25840_write4(client, DIF_BPF_COEFF2627, 0x069f007f);
3046                cx25840_write4(client, DIF_BPF_COEFF2829, 0xf975f395);
3047                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf0cbf231);
3048                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf7a9ffcb);
3049                cx25840_write4(client, DIF_BPF_COEFF3435, 0x084c0eaf);
3050                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3051                break;
3052
3053        case 4300000:
3054                cx25840_write4(client, DIF_BPF_COEFF01, 0x00010003);
3055                cx25840_write4(client, DIF_BPF_COEFF23, 0x0008000a);
3056                cx25840_write4(client, DIF_BPF_COEFF45, 0x0000ffe4);
3057                cx25840_write4(client, DIF_BPF_COEFF67, 0xffb4ff81);
3058                cx25840_write4(client, DIF_BPF_COEFF89, 0xff6aff96);
3059                cx25840_write4(client, DIF_BPF_COEFF1011, 0x001c00f0);
3060                cx25840_write4(client, DIF_BPF_COEFF1213, 0x01d70271);
3061                cx25840_write4(client, DIF_BPF_COEFF1415, 0x0254013b);
3062                cx25840_write4(client, DIF_BPF_COEFF1617, 0xff36fcbd);
3063                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfa9ff9c5);
3064                cx25840_write4(client, DIF_BPF_COEFF2021, 0xfadbfdfe);
3065                cx25840_write4(client, DIF_BPF_COEFF2223, 0x028c073b);
3066                cx25840_write4(client, DIF_BPF_COEFF2425, 0x0a750adf);
3067                cx25840_write4(client, DIF_BPF_COEFF2627, 0x07e101fa);
3068                cx25840_write4(client, DIF_BPF_COEFF2829, 0xfab8f44e);
3069                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf0ddf1be);
3070                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf6f9ff2b);
3071                cx25840_write4(client, DIF_BPF_COEFF3435, 0x07ed0e94);
3072                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3073                break;
3074
3075        case 4400000:
3076                cx25840_write4(client, DIF_BPF_COEFF01, 0x00000003);
3077                cx25840_write4(client, DIF_BPF_COEFF23, 0x0009000f);
3078                cx25840_write4(client, DIF_BPF_COEFF45, 0x000efff8);
3079                cx25840_write4(client, DIF_BPF_COEFF67, 0xffc9ff87);
3080                cx25840_write4(client, DIF_BPF_COEFF89, 0xff52ff54);
3081                cx25840_write4(client, DIF_BPF_COEFF1011, 0xffb5007e);
3082                cx25840_write4(client, DIF_BPF_COEFF1213, 0x01860270);
3083                cx25840_write4(client, DIF_BPF_COEFF1415, 0x02c00210);
3084                cx25840_write4(client, DIF_BPF_COEFF1617, 0x0044fdb2);
3085                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfb22f997);
3086                cx25840_write4(client, DIF_BPF_COEFF2021, 0xf9f2fc90);
3087                cx25840_write4(client, DIF_BPF_COEFF2223, 0x0102060f);
3088                cx25840_write4(client, DIF_BPF_COEFF2425, 0x0a050b4c);
3089                cx25840_write4(client, DIF_BPF_COEFF2627, 0x0902036e);
3090                cx25840_write4(client, DIF_BPF_COEFF2829, 0xfc0af51e);
3091                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf106f15a);
3092                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf64efe8b);
3093                cx25840_write4(client, DIF_BPF_COEFF3435, 0x078d0e77);
3094                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3095                break;
3096
3097        case 4500000:
3098                cx25840_write4(client, DIF_BPF_COEFF01, 0x00000002);
3099                cx25840_write4(client, DIF_BPF_COEFF23, 0x00080012);
3100                cx25840_write4(client, DIF_BPF_COEFF45, 0x0019000e);
3101                cx25840_write4(client, DIF_BPF_COEFF67, 0xffe5ff9e);
3102                cx25840_write4(client, DIF_BPF_COEFF89, 0xff4fff25);
3103                cx25840_write4(client, DIF_BPF_COEFF1011, 0xff560000);
3104                cx25840_write4(client, DIF_BPF_COEFF1213, 0x0112023b);
3105                cx25840_write4(client, DIF_BPF_COEFF1415, 0x02f702c0);
3106                cx25840_write4(client, DIF_BPF_COEFF1617, 0x014dfec8);
3107                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfbe5f9b3);
3108                cx25840_write4(client, DIF_BPF_COEFF2021, 0xf947fb41);
3109                cx25840_write4(client, DIF_BPF_COEFF2223, 0xff7004b9);
3110                cx25840_write4(client, DIF_BPF_COEFF2425, 0x095a0b81);
3111                cx25840_write4(client, DIF_BPF_COEFF2627, 0x0a0004d8);
3112                cx25840_write4(client, DIF_BPF_COEFF2829, 0xfd65f603);
3113                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf144f104);
3114                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf5aafdec);
3115                cx25840_write4(client, DIF_BPF_COEFF3435, 0x072b0e5a);
3116                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3117                break;
3118
3119        case 4600000:
3120                cx25840_write4(client, DIF_BPF_COEFF01, 0x00000001);
3121                cx25840_write4(client, DIF_BPF_COEFF23, 0x00060012);
3122                cx25840_write4(client, DIF_BPF_COEFF45, 0x00200022);
3123                cx25840_write4(client, DIF_BPF_COEFF67, 0x0005ffc1);
3124                cx25840_write4(client, DIF_BPF_COEFF89, 0xff61ff10);
3125                cx25840_write4(client, DIF_BPF_COEFF1011, 0xff09ff82);
3126                cx25840_write4(client, DIF_BPF_COEFF1213, 0x008601d7);
3127                cx25840_write4(client, DIF_BPF_COEFF1415, 0x02f50340);
3128                cx25840_write4(client, DIF_BPF_COEFF1617, 0x0241fff0);
3129                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfcddfa19);
3130                cx25840_write4(client, DIF_BPF_COEFF2021, 0xf8e2fa1e);
3131                cx25840_write4(client, DIF_BPF_COEFF2223, 0xfde30343);
3132                cx25840_write4(client, DIF_BPF_COEFF2425, 0x08790b7f);
3133                cx25840_write4(client, DIF_BPF_COEFF2627, 0x0ad50631);
3134                cx25840_write4(client, DIF_BPF_COEFF2829, 0xfec7f6fc);
3135                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf198f0bd);
3136                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf50dfd4e);
3137                cx25840_write4(client, DIF_BPF_COEFF3435, 0x06c90e3d);
3138                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3139                break;
3140
3141        case 4700000:
3142                cx25840_write4(client, DIF_BPF_COEFF01, 0x0000ffff);
3143                cx25840_write4(client, DIF_BPF_COEFF23, 0x0003000f);
3144                cx25840_write4(client, DIF_BPF_COEFF45, 0x00220030);
3145                cx25840_write4(client, DIF_BPF_COEFF67, 0x0025ffed);
3146                cx25840_write4(client, DIF_BPF_COEFF89, 0xff87ff15);
3147                cx25840_write4(client, DIF_BPF_COEFF1011, 0xfed6ff10);
3148                cx25840_write4(client, DIF_BPF_COEFF1213, 0xffed014c);
3149                cx25840_write4(client, DIF_BPF_COEFF1415, 0x02b90386);
3150                cx25840_write4(client, DIF_BPF_COEFF1617, 0x03110119);
3151                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfdfefac4);
3152                cx25840_write4(client, DIF_BPF_COEFF2021, 0xf8c6f92f);
3153                cx25840_write4(client, DIF_BPF_COEFF2223, 0xfc6701b7);
3154                cx25840_write4(client, DIF_BPF_COEFF2425, 0x07670b44);
3155                cx25840_write4(client, DIF_BPF_COEFF2627, 0x0b7e0776);
3156                cx25840_write4(client, DIF_BPF_COEFF2829, 0x002df807);
3157                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf200f086);
3158                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf477fcb1);
3159                cx25840_write4(client, DIF_BPF_COEFF3435, 0x06650e1e);
3160                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3161                break;
3162
3163        case 4800000:
3164                cx25840_write4(client, DIF_BPF_COEFF01, 0xfffffffe);
3165                cx25840_write4(client, DIF_BPF_COEFF23, 0xffff0009);
3166                cx25840_write4(client, DIF_BPF_COEFF45, 0x001e0038);
3167                cx25840_write4(client, DIF_BPF_COEFF67, 0x003f001b);
3168                cx25840_write4(client, DIF_BPF_COEFF89, 0xffbcff36);
3169                cx25840_write4(client, DIF_BPF_COEFF1011, 0xfec2feb6);
3170                cx25840_write4(client, DIF_BPF_COEFF1213, 0xff5600a5);
3171                cx25840_write4(client, DIF_BPF_COEFF1415, 0x0248038d);
3172                cx25840_write4(client, DIF_BPF_COEFF1617, 0x03b00232);
3173                cx25840_write4(client, DIF_BPF_COEFF1819, 0xff39fbab);
3174                cx25840_write4(client, DIF_BPF_COEFF2021, 0xf8f4f87f);
3175                cx25840_write4(client, DIF_BPF_COEFF2223, 0xfb060020);
3176                cx25840_write4(client, DIF_BPF_COEFF2425, 0x062a0ad2);
3177                cx25840_write4(client, DIF_BPF_COEFF2627, 0x0bf908a3);
3178                cx25840_write4(client, DIF_BPF_COEFF2829, 0x0192f922);
3179                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf27df05e);
3180                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf3e8fc14);
3181                cx25840_write4(client, DIF_BPF_COEFF3435, 0x06000e00);
3182                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3183                break;
3184
3185        case 4900000:
3186                cx25840_write4(client, DIF_BPF_COEFF01, 0xfffffffd);
3187                cx25840_write4(client, DIF_BPF_COEFF23, 0xfffc0002);
3188                cx25840_write4(client, DIF_BPF_COEFF45, 0x00160037);
3189                cx25840_write4(client, DIF_BPF_COEFF67, 0x00510046);
3190                cx25840_write4(client, DIF_BPF_COEFF89, 0xfff9ff6d);
3191                cx25840_write4(client, DIF_BPF_COEFF1011, 0xfed0fe7c);
3192                cx25840_write4(client, DIF_BPF_COEFF1213, 0xfecefff0);
3193                cx25840_write4(client, DIF_BPF_COEFF1415, 0x01aa0356);
3194                cx25840_write4(client, DIF_BPF_COEFF1617, 0x0413032b);
3195                cx25840_write4(client, DIF_BPF_COEFF1819, 0x007ffcc5);
3196                cx25840_write4(client, DIF_BPF_COEFF2021, 0xf96cf812);
3197                cx25840_write4(client, DIF_BPF_COEFF2223, 0xf9cefe87);
3198                cx25840_write4(client, DIF_BPF_COEFF2425, 0x04c90a2c);
3199                cx25840_write4(client, DIF_BPF_COEFF2627, 0x0c4309b4);
3200                cx25840_write4(client, DIF_BPF_COEFF2829, 0x02f3fa4a);
3201                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf30ef046);
3202                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf361fb7a);
3203                cx25840_write4(client, DIF_BPF_COEFF3435, 0x059b0de0);
3204                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3205                break;
3206
3207        case 5000000:
3208                cx25840_write4(client, DIF_BPF_COEFF01, 0xfffffffd);
3209                cx25840_write4(client, DIF_BPF_COEFF23, 0xfff9fffa);
3210                cx25840_write4(client, DIF_BPF_COEFF45, 0x000a002d);
3211                cx25840_write4(client, DIF_BPF_COEFF67, 0x00570067);
3212                cx25840_write4(client, DIF_BPF_COEFF89, 0x0037ffb5);
3213                cx25840_write4(client, DIF_BPF_COEFF1011, 0xfefffe68);
3214                cx25840_write4(client, DIF_BPF_COEFF1213, 0xfe62ff3d);
3215                cx25840_write4(client, DIF_BPF_COEFF1415, 0x00ec02e3);
3216                cx25840_write4(client, DIF_BPF_COEFF1617, 0x043503f6);
3217                cx25840_write4(client, DIF_BPF_COEFF1819, 0x01befe05);
3218                cx25840_write4(client, DIF_BPF_COEFF2021, 0xfa27f7ee);
3219                cx25840_write4(client, DIF_BPF_COEFF2223, 0xf8c6fcf8);
3220                cx25840_write4(client, DIF_BPF_COEFF2425, 0x034c0954);
3221                cx25840_write4(client, DIF_BPF_COEFF2627, 0x0c5c0aa4);
3222                cx25840_write4(client, DIF_BPF_COEFF2829, 0x044cfb7e);
3223                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf3b1f03f);
3224                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf2e2fae1);
3225                cx25840_write4(client, DIF_BPF_COEFF3435, 0x05340dc0);
3226                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3227                break;
3228
3229        case 5100000:
3230                cx25840_write4(client, DIF_BPF_COEFF01, 0x0000fffd);
3231                cx25840_write4(client, DIF_BPF_COEFF23, 0xfff8fff4);
3232                cx25840_write4(client, DIF_BPF_COEFF45, 0xfffd001e);
3233                cx25840_write4(client, DIF_BPF_COEFF67, 0x0051007b);
3234                cx25840_write4(client, DIF_BPF_COEFF89, 0x006e0006);
3235                cx25840_write4(client, DIF_BPF_COEFF1011, 0xff48fe7c);
3236                cx25840_write4(client, DIF_BPF_COEFF1213, 0xfe1bfe9a);
3237                cx25840_write4(client, DIF_BPF_COEFF1415, 0x001d023e);
3238                cx25840_write4(client, DIF_BPF_COEFF1617, 0x04130488);
3239                cx25840_write4(client, DIF_BPF_COEFF1819, 0x02e6ff5b);
3240                cx25840_write4(client, DIF_BPF_COEFF2021, 0xfb1ef812);
3241                cx25840_write4(client, DIF_BPF_COEFF2223, 0xf7f7fb7f);
3242                cx25840_write4(client, DIF_BPF_COEFF2425, 0x01bc084e);
3243                cx25840_write4(client, DIF_BPF_COEFF2627, 0x0c430b72);
3244                cx25840_write4(client, DIF_BPF_COEFF2829, 0x059afcba);
3245                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf467f046);
3246                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf26cfa4a);
3247                cx25840_write4(client, DIF_BPF_COEFF3435, 0x04cd0da0);
3248                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3249                break;
3250
3251        case 5200000:
3252                cx25840_write4(client, DIF_BPF_COEFF01, 0x0000fffe);
3253                cx25840_write4(client, DIF_BPF_COEFF23, 0xfff8ffef);
3254                cx25840_write4(client, DIF_BPF_COEFF45, 0xfff00009);
3255                cx25840_write4(client, DIF_BPF_COEFF67, 0x003f007f);
3256                cx25840_write4(client, DIF_BPF_COEFF89, 0x00980056);
3257                cx25840_write4(client, DIF_BPF_COEFF1011, 0xffa5feb6);
3258                cx25840_write4(client, DIF_BPF_COEFF1213, 0xfe00fe15);
3259                cx25840_write4(client, DIF_BPF_COEFF1415, 0xff4b0170);
3260                cx25840_write4(client, DIF_BPF_COEFF1617, 0x03b004d7);
3261                cx25840_write4(client, DIF_BPF_COEFF1819, 0x03e800b9);
3262                cx25840_write4(client, DIF_BPF_COEFF2021, 0xfc48f87f);
3263                cx25840_write4(client, DIF_BPF_COEFF2223, 0xf768fa23);
3264                cx25840_write4(client, DIF_BPF_COEFF2425, 0x0022071f);
3265                cx25840_write4(client, DIF_BPF_COEFF2627, 0x0bf90c1b);
3266                cx25840_write4(client, DIF_BPF_COEFF2829, 0x06dafdfd);
3267                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf52df05e);
3268                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf1fef9b5);
3269                cx25840_write4(client, DIF_BPF_COEFF3435, 0x04640d7f);
3270                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3271                break;
3272
3273        case 5300000:
3274                cx25840_write4(client, DIF_BPF_COEFF01, 0x0000ffff);
3275                cx25840_write4(client, DIF_BPF_COEFF23, 0xfff9ffee);
3276                cx25840_write4(client, DIF_BPF_COEFF45, 0xffe6fff3);
3277                cx25840_write4(client, DIF_BPF_COEFF67, 0x00250072);
3278                cx25840_write4(client, DIF_BPF_COEFF89, 0x00af009c);
3279                cx25840_write4(client, DIF_BPF_COEFF1011, 0x000cff10);
3280                cx25840_write4(client, DIF_BPF_COEFF1213, 0xfe13fdb8);
3281                cx25840_write4(client, DIF_BPF_COEFF1415, 0xfe870089);
3282                cx25840_write4(client, DIF_BPF_COEFF1617, 0x031104e1);
3283                cx25840_write4(client, DIF_BPF_COEFF1819, 0x04b8020f);
3284                cx25840_write4(client, DIF_BPF_COEFF2021, 0xfd98f92f);
3285                cx25840_write4(client, DIF_BPF_COEFF2223, 0xf71df8f0);
3286                cx25840_write4(client, DIF_BPF_COEFF2425, 0xfe8805ce);
3287                cx25840_write4(client, DIF_BPF_COEFF2627, 0x0b7e0c9c);
3288                cx25840_write4(client, DIF_BPF_COEFF2829, 0x0808ff44);
3289                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf603f086);
3290                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf19af922);
3291                cx25840_write4(client, DIF_BPF_COEFF3435, 0x03fb0d5e);
3292                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3293                break;
3294
3295        case 5400000:
3296                cx25840_write4(client, DIF_BPF_COEFF01, 0x00000001);
3297                cx25840_write4(client, DIF_BPF_COEFF23, 0xfffcffef);
3298                cx25840_write4(client, DIF_BPF_COEFF45, 0xffe0ffe0);
3299                cx25840_write4(client, DIF_BPF_COEFF67, 0x00050056);
3300                cx25840_write4(client, DIF_BPF_COEFF89, 0x00b000d1);
3301                cx25840_write4(client, DIF_BPF_COEFF1011, 0x0071ff82);
3302                cx25840_write4(client, DIF_BPF_COEFF1213, 0xfe53fd8c);
3303                cx25840_write4(client, DIF_BPF_COEFF1415, 0xfddfff99);
3304                cx25840_write4(client, DIF_BPF_COEFF1617, 0x024104a3);
3305                cx25840_write4(client, DIF_BPF_COEFF1819, 0x054a034d);
3306                cx25840_write4(client, DIF_BPF_COEFF2021, 0xff01fa1e);
3307                cx25840_write4(client, DIF_BPF_COEFF2223, 0xf717f7ed);
3308                cx25840_write4(client, DIF_BPF_COEFF2425, 0xfcf50461);
3309                cx25840_write4(client, DIF_BPF_COEFF2627, 0x0ad50cf4);
3310                cx25840_write4(client, DIF_BPF_COEFF2829, 0x0921008d);
3311                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf6e7f0bd);
3312                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf13ff891);
3313                cx25840_write4(client, DIF_BPF_COEFF3435, 0x03920d3b);
3314                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3315                break;
3316
3317        case 5500000:
3318                cx25840_write4(client, DIF_BPF_COEFF01, 0x00010002);
3319                cx25840_write4(client, DIF_BPF_COEFF23, 0xfffffff3);
3320                cx25840_write4(client, DIF_BPF_COEFF45, 0xffdeffd1);
3321                cx25840_write4(client, DIF_BPF_COEFF67, 0xffe5002f);
3322                cx25840_write4(client, DIF_BPF_COEFF89, 0x009c00ed);
3323                cx25840_write4(client, DIF_BPF_COEFF1011, 0x00cb0000);
3324                cx25840_write4(client, DIF_BPF_COEFF1213, 0xfebafd94);
3325                cx25840_write4(client, DIF_BPF_COEFF1415, 0xfd61feb0);
3326                cx25840_write4(client, DIF_BPF_COEFF1617, 0x014d0422);
3327                cx25840_write4(client, DIF_BPF_COEFF1819, 0x05970464);
3328                cx25840_write4(client, DIF_BPF_COEFF2021, 0x0074fb41);
3329                cx25840_write4(client, DIF_BPF_COEFF2223, 0xf759f721);
3330                cx25840_write4(client, DIF_BPF_COEFF2425, 0xfb7502de);
3331                cx25840_write4(client, DIF_BPF_COEFF2627, 0x0a000d21);
3332                cx25840_write4(client, DIF_BPF_COEFF2829, 0x0a2201d4);
3333                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf7d9f104);
3334                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf0edf804);
3335                cx25840_write4(client, DIF_BPF_COEFF3435, 0x03280d19);
3336                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3337                break;
3338
3339        case 5600000:
3340                cx25840_write4(client, DIF_BPF_COEFF01, 0x00010003);
3341                cx25840_write4(client, DIF_BPF_COEFF23, 0x0003fffa);
3342                cx25840_write4(client, DIF_BPF_COEFF45, 0xffe3ffc9);
3343                cx25840_write4(client, DIF_BPF_COEFF67, 0xffc90002);
3344                cx25840_write4(client, DIF_BPF_COEFF89, 0x007500ef);
3345                cx25840_write4(client, DIF_BPF_COEFF1011, 0x010e007e);
3346                cx25840_write4(client, DIF_BPF_COEFF1213, 0xff3dfdcf);
3347                cx25840_write4(client, DIF_BPF_COEFF1415, 0xfd16fddd);
3348                cx25840_write4(client, DIF_BPF_COEFF1617, 0x00440365);
3349                cx25840_write4(client, DIF_BPF_COEFF1819, 0x059b0548);
3350                cx25840_write4(client, DIF_BPF_COEFF2021, 0x01e3fc90);
3351                cx25840_write4(client, DIF_BPF_COEFF2223, 0xf7dff691);
3352                cx25840_write4(client, DIF_BPF_COEFF2425, 0xfa0f014d);
3353                cx25840_write4(client, DIF_BPF_COEFF2627, 0x09020d23);
3354                cx25840_write4(client, DIF_BPF_COEFF2829, 0x0b0a0318);
3355                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf8d7f15a);
3356                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf0a5f779);
3357                cx25840_write4(client, DIF_BPF_COEFF3435, 0x02bd0cf6);
3358                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3359                break;
3360
3361        case 5700000:
3362                cx25840_write4(client, DIF_BPF_COEFF01, 0x00010003);
3363                cx25840_write4(client, DIF_BPF_COEFF23, 0x00060001);
3364                cx25840_write4(client, DIF_BPF_COEFF45, 0xffecffc9);
3365                cx25840_write4(client, DIF_BPF_COEFF67, 0xffb4ffd4);
3366                cx25840_write4(client, DIF_BPF_COEFF89, 0x004000d5);
3367                cx25840_write4(client, DIF_BPF_COEFF1011, 0x013600f0);
3368                cx25840_write4(client, DIF_BPF_COEFF1213, 0xffd3fe39);
3369                cx25840_write4(client, DIF_BPF_COEFF1415, 0xfd04fd31);
3370                cx25840_write4(client, DIF_BPF_COEFF1617, 0xff360277);
3371                cx25840_write4(client, DIF_BPF_COEFF1819, 0x055605ef);
3372                cx25840_write4(client, DIF_BPF_COEFF2021, 0x033efdfe);
3373                cx25840_write4(client, DIF_BPF_COEFF2223, 0xf8a5f642);
3374                cx25840_write4(client, DIF_BPF_COEFF2425, 0xf8cbffb6);
3375                cx25840_write4(client, DIF_BPF_COEFF2627, 0x07e10cfb);
3376                cx25840_write4(client, DIF_BPF_COEFF2829, 0x0bd50456);
3377                cx25840_write4(client, DIF_BPF_COEFF3031, 0xf9dff1be);
3378                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf067f6f2);
3379                cx25840_write4(client, DIF_BPF_COEFF3435, 0x02520cd2);
3380                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3381                break;
3382
3383        case 5800000:
3384                cx25840_write4(client, DIF_BPF_COEFF01, 0x00000003);
3385                cx25840_write4(client, DIF_BPF_COEFF23, 0x00080009);
3386                cx25840_write4(client, DIF_BPF_COEFF45, 0xfff8ffd2);
3387                cx25840_write4(client, DIF_BPF_COEFF67, 0xffaaffac);
3388                cx25840_write4(client, DIF_BPF_COEFF89, 0x000200a3);
3389                cx25840_write4(client, DIF_BPF_COEFF1011, 0x013c014a);
3390                cx25840_write4(client, DIF_BPF_COEFF1213, 0x006dfec9);
3391                cx25840_write4(client, DIF_BPF_COEFF1415, 0xfd2bfcb7);
3392                cx25840_write4(client, DIF_BPF_COEFF1617, 0xfe350165);
3393                cx25840_write4(client, DIF_BPF_COEFF1819, 0x04cb0651);
3394                cx25840_write4(client, DIF_BPF_COEFF2021, 0x0477ff7e);
3395                cx25840_write4(client, DIF_BPF_COEFF2223, 0xf9a5f635);
3396                cx25840_write4(client, DIF_BPF_COEFF2425, 0xf7b1fe20);
3397                cx25840_write4(client, DIF_BPF_COEFF2627, 0x069f0ca8);
3398                cx25840_write4(client, DIF_BPF_COEFF2829, 0x0c81058b);
3399                cx25840_write4(client, DIF_BPF_COEFF3031, 0xfaf0f231);
3400                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf033f66d);
3401                cx25840_write4(client, DIF_BPF_COEFF3435, 0x01e60cae);
3402                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3403                break;
3404
3405        case 5900000:
3406                cx25840_write4(client, DIF_BPF_COEFF01, 0x00000002);
3407                cx25840_write4(client, DIF_BPF_COEFF23, 0x0009000e);
3408                cx25840_write4(client, DIF_BPF_COEFF45, 0x0005ffe1);
3409                cx25840_write4(client, DIF_BPF_COEFF67, 0xffacff90);
3410                cx25840_write4(client, DIF_BPF_COEFF89, 0xffc5005f);
3411                cx25840_write4(client, DIF_BPF_COEFF1011, 0x01210184);
3412                cx25840_write4(client, DIF_BPF_COEFF1213, 0x00fcff72);
3413                cx25840_write4(client, DIF_BPF_COEFF1415, 0xfd8afc77);
3414                cx25840_write4(client, DIF_BPF_COEFF1617, 0xfd51003f);
3415                cx25840_write4(client, DIF_BPF_COEFF1819, 0x04020669);
3416                cx25840_write4(client, DIF_BPF_COEFF2021, 0x05830103);
3417                cx25840_write4(client, DIF_BPF_COEFF2223, 0xfad7f66b);
3418                cx25840_write4(client, DIF_BPF_COEFF2425, 0xf6c8fc93);
3419                cx25840_write4(client, DIF_BPF_COEFF2627, 0x05430c2b);
3420                cx25840_write4(client, DIF_BPF_COEFF2829, 0x0d0d06b5);
3421                cx25840_write4(client, DIF_BPF_COEFF3031, 0xfc08f2b2);
3422                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf00af5ec);
3423                cx25840_write4(client, DIF_BPF_COEFF3435, 0x017b0c89);
3424                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3425                break;
3426
3427        case 6000000:
3428                cx25840_write4(client, DIF_BPF_COEFF01, 0x00000001);
3429                cx25840_write4(client, DIF_BPF_COEFF23, 0x00070012);
3430                cx25840_write4(client, DIF_BPF_COEFF45, 0x0012fff5);
3431                cx25840_write4(client, DIF_BPF_COEFF67, 0xffbaff82);
3432                cx25840_write4(client, DIF_BPF_COEFF89, 0xff8e000f);
3433                cx25840_write4(client, DIF_BPF_COEFF1011, 0x00e80198);
3434                cx25840_write4(client, DIF_BPF_COEFF1213, 0x01750028);
3435                cx25840_write4(client, DIF_BPF_COEFF1415, 0xfe18fc75);
3436                cx25840_write4(client, DIF_BPF_COEFF1617, 0xfc99ff15);
3437                cx25840_write4(client, DIF_BPF_COEFF1819, 0x03050636);
3438                cx25840_write4(client, DIF_BPF_COEFF2021, 0x0656027f);
3439                cx25840_write4(client, DIF_BPF_COEFF2223, 0xfc32f6e2);
3440                cx25840_write4(client, DIF_BPF_COEFF2425, 0xf614fb17);
3441                cx25840_write4(client, DIF_BPF_COEFF2627, 0x03d20b87);
3442                cx25840_write4(client, DIF_BPF_COEFF2829, 0x0d7707d2);
3443                cx25840_write4(client, DIF_BPF_COEFF3031, 0xfd26f341);
3444                cx25840_write4(client, DIF_BPF_COEFF3233, 0xefeaf56f);
3445                cx25840_write4(client, DIF_BPF_COEFF3435, 0x010f0c64);
3446                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3447                break;
3448
3449        case 6100000:
3450                cx25840_write4(client, DIF_BPF_COEFF01, 0xffff0000);
3451                cx25840_write4(client, DIF_BPF_COEFF23, 0x00050012);
3452                cx25840_write4(client, DIF_BPF_COEFF45, 0x001c000b);
3453                cx25840_write4(client, DIF_BPF_COEFF67, 0xffd1ff84);
3454                cx25840_write4(client, DIF_BPF_COEFF89, 0xff66ffbe);
3455                cx25840_write4(client, DIF_BPF_COEFF1011, 0x00960184);
3456                cx25840_write4(client, DIF_BPF_COEFF1213, 0x01cd00da);
3457                cx25840_write4(client, DIF_BPF_COEFF1415, 0xfeccfcb2);
3458                cx25840_write4(client, DIF_BPF_COEFF1617, 0xfc17fdf9);
3459                cx25840_write4(client, DIF_BPF_COEFF1819, 0x01e005bc);
3460                cx25840_write4(client, DIF_BPF_COEFF2021, 0x06e703e4);
3461                cx25840_write4(client, DIF_BPF_COEFF2223, 0xfdabf798);
3462                cx25840_write4(client, DIF_BPF_COEFF2425, 0xf599f9b3);
3463                cx25840_write4(client, DIF_BPF_COEFF2627, 0x02510abd);
3464                cx25840_write4(client, DIF_BPF_COEFF2829, 0x0dbf08df);
3465                cx25840_write4(client, DIF_BPF_COEFF3031, 0xfe48f3dc);
3466                cx25840_write4(client, DIF_BPF_COEFF3233, 0xefd5f4f6);
3467                cx25840_write4(client, DIF_BPF_COEFF3435, 0x00a20c3e);
3468                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3469                break;
3470
3471        case 6200000:
3472                cx25840_write4(client, DIF_BPF_COEFF01, 0xfffffffe);
3473                cx25840_write4(client, DIF_BPF_COEFF23, 0x0002000f);
3474                cx25840_write4(client, DIF_BPF_COEFF45, 0x0021001f);
3475                cx25840_write4(client, DIF_BPF_COEFF67, 0xfff0ff97);
3476                cx25840_write4(client, DIF_BPF_COEFF89, 0xff50ff74);
3477                cx25840_write4(client, DIF_BPF_COEFF1011, 0x0034014a);
3478                cx25840_write4(client, DIF_BPF_COEFF1213, 0x01fa0179);
3479                cx25840_write4(client, DIF_BPF_COEFF1415, 0xff97fd2a);
3480                cx25840_write4(client, DIF_BPF_COEFF1617, 0xfbd3fcfa);
3481                cx25840_write4(client, DIF_BPF_COEFF1819, 0x00a304fe);
3482                cx25840_write4(client, DIF_BPF_COEFF2021, 0x07310525);
3483                cx25840_write4(client, DIF_BPF_COEFF2223, 0xff37f886);
3484                cx25840_write4(client, DIF_BPF_COEFF2425, 0xf55cf86e);
3485                cx25840_write4(client, DIF_BPF_COEFF2627, 0x00c709d0);
3486                cx25840_write4(client, DIF_BPF_COEFF2829, 0x0de209db);
3487                cx25840_write4(client, DIF_BPF_COEFF3031, 0xff6df484);
3488                cx25840_write4(client, DIF_BPF_COEFF3233, 0xefcbf481);
3489                cx25840_write4(client, DIF_BPF_COEFF3435, 0x00360c18);
3490                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3491                break;
3492
3493        case 6300000:
3494                cx25840_write4(client, DIF_BPF_COEFF01, 0xfffffffd);
3495                cx25840_write4(client, DIF_BPF_COEFF23, 0xfffe000a);
3496                cx25840_write4(client, DIF_BPF_COEFF45, 0x0021002f);
3497                cx25840_write4(client, DIF_BPF_COEFF67, 0x0010ffb8);
3498                cx25840_write4(client, DIF_BPF_COEFF89, 0xff50ff3b);
3499                cx25840_write4(client, DIF_BPF_COEFF1011, 0xffcc00f0);
3500                cx25840_write4(client, DIF_BPF_COEFF1213, 0x01fa01fa);
3501                cx25840_write4(client, DIF_BPF_COEFF1415, 0x0069fdd4);
3502                cx25840_write4(client, DIF_BPF_COEFF1617, 0xfbd3fc26);
3503                cx25840_write4(client, DIF_BPF_COEFF1819, 0xff5d0407);
3504                cx25840_write4(client, DIF_BPF_COEFF2021, 0x07310638);
3505                cx25840_write4(client, DIF_BPF_COEFF2223, 0x00c9f9a8);
3506                cx25840_write4(client, DIF_BPF_COEFF2425, 0xf55cf74e);
3507                cx25840_write4(client, DIF_BPF_COEFF2627, 0xff3908c3);
3508                cx25840_write4(client, DIF_BPF_COEFF2829, 0x0de20ac3);
3509                cx25840_write4(client, DIF_BPF_COEFF3031, 0x0093f537);
3510                cx25840_write4(client, DIF_BPF_COEFF3233, 0xefcbf410);
3511                cx25840_write4(client, DIF_BPF_COEFF3435, 0xffca0bf2);
3512                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3513                break;
3514
3515        case 6400000:
3516                cx25840_write4(client, DIF_BPF_COEFF01, 0xfffffffd);
3517                cx25840_write4(client, DIF_BPF_COEFF23, 0xfffb0003);
3518                cx25840_write4(client, DIF_BPF_COEFF45, 0x001c0037);
3519                cx25840_write4(client, DIF_BPF_COEFF67, 0x002fffe2);
3520                cx25840_write4(client, DIF_BPF_COEFF89, 0xff66ff17);
3521                cx25840_write4(client, DIF_BPF_COEFF1011, 0xff6a007e);
3522                cx25840_write4(client, DIF_BPF_COEFF1213, 0x01cd0251);
3523                cx25840_write4(client, DIF_BPF_COEFF1415, 0x0134fea5);
3524                cx25840_write4(client, DIF_BPF_COEFF1617, 0xfc17fb8b);
3525                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfe2002e0);
3526                cx25840_write4(client, DIF_BPF_COEFF2021, 0x06e70713);
3527                cx25840_write4(client, DIF_BPF_COEFF2223, 0x0255faf5);
3528                cx25840_write4(client, DIF_BPF_COEFF2425, 0xf599f658);
3529                cx25840_write4(client, DIF_BPF_COEFF2627, 0xfdaf0799);
3530                cx25840_write4(client, DIF_BPF_COEFF2829, 0x0dbf0b96);
3531                cx25840_write4(client, DIF_BPF_COEFF3031, 0x01b8f5f5);
3532                cx25840_write4(client, DIF_BPF_COEFF3233, 0xefd5f3a3);
3533                cx25840_write4(client, DIF_BPF_COEFF3435, 0xff5e0bca);
3534                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3535                break;
3536
3537        case 6500000:
3538                cx25840_write4(client, DIF_BPF_COEFF01, 0x0000fffd);
3539                cx25840_write4(client, DIF_BPF_COEFF23, 0xfff9fffb);
3540                cx25840_write4(client, DIF_BPF_COEFF45, 0x00120037);
3541                cx25840_write4(client, DIF_BPF_COEFF67, 0x00460010);
3542                cx25840_write4(client, DIF_BPF_COEFF89, 0xff8eff0f);
3543                cx25840_write4(client, DIF_BPF_COEFF1011, 0xff180000);
3544                cx25840_write4(client, DIF_BPF_COEFF1213, 0x01750276);
3545                cx25840_write4(client, DIF_BPF_COEFF1415, 0x01e8ff8d);
3546                cx25840_write4(client, DIF_BPF_COEFF1617, 0xfc99fb31);
3547                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfcfb0198);
3548                cx25840_write4(client, DIF_BPF_COEFF2021, 0x065607ad);
3549                cx25840_write4(client, DIF_BPF_COEFF2223, 0x03cefc64);
3550                cx25840_write4(client, DIF_BPF_COEFF2425, 0xf614f592);
3551                cx25840_write4(client, DIF_BPF_COEFF2627, 0xfc2e0656);
3552                cx25840_write4(client, DIF_BPF_COEFF2829, 0x0d770c52);
3553                cx25840_write4(client, DIF_BPF_COEFF3031, 0x02daf6bd);
3554                cx25840_write4(client, DIF_BPF_COEFF3233, 0xefeaf33b);
3555                cx25840_write4(client, DIF_BPF_COEFF3435, 0xfef10ba3);
3556                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3557                break;
3558
3559        case 6600000:
3560                cx25840_write4(client, DIF_BPF_COEFF01, 0x0000fffe);
3561                cx25840_write4(client, DIF_BPF_COEFF23, 0xfff7fff5);
3562                cx25840_write4(client, DIF_BPF_COEFF45, 0x0005002f);
3563                cx25840_write4(client, DIF_BPF_COEFF67, 0x0054003c);
3564                cx25840_write4(client, DIF_BPF_COEFF89, 0xffc5ff22);
3565                cx25840_write4(client, DIF_BPF_COEFF1011, 0xfedfff82);
3566                cx25840_write4(client, DIF_BPF_COEFF1213, 0x00fc0267);
3567                cx25840_write4(client, DIF_BPF_COEFF1415, 0x0276007e);
3568                cx25840_write4(client, DIF_BPF_COEFF1617, 0xfd51fb1c);
3569                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfbfe003e);
3570                cx25840_write4(client, DIF_BPF_COEFF2021, 0x05830802);
3571                cx25840_write4(client, DIF_BPF_COEFF2223, 0x0529fdec);
3572                cx25840_write4(client, DIF_BPF_COEFF2425, 0xf6c8f4fe);
3573                cx25840_write4(client, DIF_BPF_COEFF2627, 0xfabd04ff);
3574                cx25840_write4(client, DIF_BPF_COEFF2829, 0x0d0d0cf6);
3575                cx25840_write4(client, DIF_BPF_COEFF3031, 0x03f8f78f);
3576                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf00af2d7);
3577                cx25840_write4(client, DIF_BPF_COEFF3435, 0xfe850b7b);
3578                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3579                break;
3580
3581        case 6700000:
3582                cx25840_write4(client, DIF_BPF_COEFF01, 0x0000ffff);
3583                cx25840_write4(client, DIF_BPF_COEFF23, 0xfff8fff0);
3584                cx25840_write4(client, DIF_BPF_COEFF45, 0xfff80020);
3585                cx25840_write4(client, DIF_BPF_COEFF67, 0x00560060);
3586                cx25840_write4(client, DIF_BPF_COEFF89, 0x0002ff4e);
3587                cx25840_write4(client, DIF_BPF_COEFF1011, 0xfec4ff10);
3588                cx25840_write4(client, DIF_BPF_COEFF1213, 0x006d0225);
3589                cx25840_write4(client, DIF_BPF_COEFF1415, 0x02d50166);
3590                cx25840_write4(client, DIF_BPF_COEFF1617, 0xfe35fb4e);
3591                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfb35fee1);
3592                cx25840_write4(client, DIF_BPF_COEFF2021, 0x0477080e);
3593                cx25840_write4(client, DIF_BPF_COEFF2223, 0x065bff82);
3594                cx25840_write4(client, DIF_BPF_COEFF2425, 0xf7b1f4a0);
3595                cx25840_write4(client, DIF_BPF_COEFF2627, 0xf9610397);
3596                cx25840_write4(client, DIF_BPF_COEFF2829, 0x0c810d80);
3597                cx25840_write4(client, DIF_BPF_COEFF3031, 0x0510f869);
3598                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf033f278);
3599                cx25840_write4(client, DIF_BPF_COEFF3435, 0xfe1a0b52);
3600                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3601                break;
3602
3603        case 6800000:
3604                cx25840_write4(client, DIF_BPF_COEFF01, 0x00010000);
3605                cx25840_write4(client, DIF_BPF_COEFF23, 0xfffaffee);
3606                cx25840_write4(client, DIF_BPF_COEFF45, 0xffec000c);
3607                cx25840_write4(client, DIF_BPF_COEFF67, 0x004c0078);
3608                cx25840_write4(client, DIF_BPF_COEFF89, 0x0040ff8e);
3609                cx25840_write4(client, DIF_BPF_COEFF1011, 0xfecafeb6);
3610                cx25840_write4(client, DIF_BPF_COEFF1213, 0xffd301b6);
3611                cx25840_write4(client, DIF_BPF_COEFF1415, 0x02fc0235);
3612                cx25840_write4(client, DIF_BPF_COEFF1617, 0xff36fbc5);
3613                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfaaafd90);
3614                cx25840_write4(client, DIF_BPF_COEFF2021, 0x033e07d2);
3615                cx25840_write4(client, DIF_BPF_COEFF2223, 0x075b011b);
3616                cx25840_write4(client, DIF_BPF_COEFF2425, 0xf8cbf47a);
3617                cx25840_write4(client, DIF_BPF_COEFF2627, 0xf81f0224);
3618                cx25840_write4(client, DIF_BPF_COEFF2829, 0x0bd50def);
3619                cx25840_write4(client, DIF_BPF_COEFF3031, 0x0621f94b);
3620                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf067f21e);
3621                cx25840_write4(client, DIF_BPF_COEFF3435, 0xfdae0b29);
3622                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3623                break;
3624
3625        case 6900000:
3626                cx25840_write4(client, DIF_BPF_COEFF01, 0x00010001);
3627                cx25840_write4(client, DIF_BPF_COEFF23, 0xfffdffef);
3628                cx25840_write4(client, DIF_BPF_COEFF45, 0xffe3fff6);
3629                cx25840_write4(client, DIF_BPF_COEFF67, 0x0037007f);
3630                cx25840_write4(client, DIF_BPF_COEFF89, 0x0075ffdc);
3631                cx25840_write4(client, DIF_BPF_COEFF1011, 0xfef2fe7c);
3632                cx25840_write4(client, DIF_BPF_COEFF1213, 0xff3d0122);
3633                cx25840_write4(client, DIF_BPF_COEFF1415, 0x02ea02dd);
3634                cx25840_write4(client, DIF_BPF_COEFF1617, 0x0044fc79);
3635                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfa65fc5d);
3636                cx25840_write4(client, DIF_BPF_COEFF2021, 0x01e3074e);
3637                cx25840_write4(client, DIF_BPF_COEFF2223, 0x082102ad);
3638                cx25840_write4(client, DIF_BPF_COEFF2425, 0xfa0ff48c);
3639                cx25840_write4(client, DIF_BPF_COEFF2627, 0xf6fe00a9);
3640                cx25840_write4(client, DIF_BPF_COEFF2829, 0x0b0a0e43);
3641                cx25840_write4(client, DIF_BPF_COEFF3031, 0x0729fa33);
3642                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf0a5f1c9);
3643                cx25840_write4(client, DIF_BPF_COEFF3435, 0xfd430b00);
3644                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3645                break;
3646
3647        case 7000000:
3648                cx25840_write4(client, DIF_BPF_COEFF01, 0x00010002);
3649                cx25840_write4(client, DIF_BPF_COEFF23, 0x0001fff3);
3650                cx25840_write4(client, DIF_BPF_COEFF45, 0xffdeffe2);
3651                cx25840_write4(client, DIF_BPF_COEFF67, 0x001b0076);
3652                cx25840_write4(client, DIF_BPF_COEFF89, 0x009c002d);
3653                cx25840_write4(client, DIF_BPF_COEFF1011, 0xff35fe68);
3654                cx25840_write4(client, DIF_BPF_COEFF1213, 0xfeba0076);
3655                cx25840_write4(client, DIF_BPF_COEFF1415, 0x029f0352);
3656                cx25840_write4(client, DIF_BPF_COEFF1617, 0x014dfd60);
3657                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfa69fb53);
3658                cx25840_write4(client, DIF_BPF_COEFF2021, 0x00740688);
3659                cx25840_write4(client, DIF_BPF_COEFF2223, 0x08a7042d);
3660                cx25840_write4(client, DIF_BPF_COEFF2425, 0xfb75f4d6);
3661                cx25840_write4(client, DIF_BPF_COEFF2627, 0xf600ff2d);
3662                cx25840_write4(client, DIF_BPF_COEFF2829, 0x0a220e7a);
3663                cx25840_write4(client, DIF_BPF_COEFF3031, 0x0827fb22);
3664                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf0edf17a);
3665                cx25840_write4(client, DIF_BPF_COEFF3435, 0xfcd80ad6);
3666                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3667                break;
3668
3669        case 7100000:
3670                cx25840_write4(client, DIF_BPF_COEFF01, 0x00000003);
3671                cx25840_write4(client, DIF_BPF_COEFF23, 0x0004fff9);
3672                cx25840_write4(client, DIF_BPF_COEFF45, 0xffe0ffd2);
3673                cx25840_write4(client, DIF_BPF_COEFF67, 0xfffb005e);
3674                cx25840_write4(client, DIF_BPF_COEFF89, 0x00b0007a);
3675                cx25840_write4(client, DIF_BPF_COEFF1011, 0xff8ffe7c);
3676                cx25840_write4(client, DIF_BPF_COEFF1213, 0xfe53ffc1);
3677                cx25840_write4(client, DIF_BPF_COEFF1415, 0x0221038c);
3678                cx25840_write4(client, DIF_BPF_COEFF1617, 0x0241fe6e);
3679                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfab6fa80);
3680                cx25840_write4(client, DIF_BPF_COEFF2021, 0xff010587);
3681                cx25840_write4(client, DIF_BPF_COEFF2223, 0x08e90590);
3682                cx25840_write4(client, DIF_BPF_COEFF2425, 0xfcf5f556);
3683                cx25840_write4(client, DIF_BPF_COEFF2627, 0xf52bfdb3);
3684                cx25840_write4(client, DIF_BPF_COEFF2829, 0x09210e95);
3685                cx25840_write4(client, DIF_BPF_COEFF3031, 0x0919fc15);
3686                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf13ff12f);
3687                cx25840_write4(client, DIF_BPF_COEFF3435, 0xfc6e0aab);
3688                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3689                break;
3690
3691        case 7200000:
3692                cx25840_write4(client, DIF_BPF_COEFF01, 0x00000003);
3693                cx25840_write4(client, DIF_BPF_COEFF23, 0x00070000);
3694                cx25840_write4(client, DIF_BPF_COEFF45, 0xffe6ffc9);
3695                cx25840_write4(client, DIF_BPF_COEFF67, 0xffdb0039);
3696                cx25840_write4(client, DIF_BPF_COEFF89, 0x00af00b8);
3697                cx25840_write4(client, DIF_BPF_COEFF1011, 0xfff4feb6);
3698                cx25840_write4(client, DIF_BPF_COEFF1213, 0xfe13ff10);
3699                cx25840_write4(client, DIF_BPF_COEFF1415, 0x01790388);
3700                cx25840_write4(client, DIF_BPF_COEFF1617, 0x0311ff92);
3701                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfb48f9ed);
3702                cx25840_write4(client, DIF_BPF_COEFF2021, 0xfd980453);
3703                cx25840_write4(client, DIF_BPF_COEFF2223, 0x08e306cd);
3704                cx25840_write4(client, DIF_BPF_COEFF2425, 0xfe88f60a);
3705                cx25840_write4(client, DIF_BPF_COEFF2627, 0xf482fc40);
3706                cx25840_write4(client, DIF_BPF_COEFF2829, 0x08080e93);
3707                cx25840_write4(client, DIF_BPF_COEFF3031, 0x09fdfd0c);
3708                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf19af0ea);
3709                cx25840_write4(client, DIF_BPF_COEFF3435, 0xfc050a81);
3710                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3711                break;
3712
3713        case 7300000:
3714                cx25840_write4(client, DIF_BPF_COEFF01, 0x00000002);
3715                cx25840_write4(client, DIF_BPF_COEFF23, 0x00080008);
3716                cx25840_write4(client, DIF_BPF_COEFF45, 0xfff0ffc9);
3717                cx25840_write4(client, DIF_BPF_COEFF67, 0xffc1000d);
3718                cx25840_write4(client, DIF_BPF_COEFF89, 0x009800e2);
3719                cx25840_write4(client, DIF_BPF_COEFF1011, 0x005bff10);
3720                cx25840_write4(client, DIF_BPF_COEFF1213, 0xfe00fe74);
3721                cx25840_write4(client, DIF_BPF_COEFF1415, 0x00b50345);
3722                cx25840_write4(client, DIF_BPF_COEFF1617, 0x03b000bc);
3723                cx25840_write4(client, DIF_BPF_COEFF1819, 0xfc18f9a1);
3724                cx25840_write4(client, DIF_BPF_COEFF2021, 0xfc4802f9);
3725                cx25840_write4(client, DIF_BPF_COEFF2223, 0x089807dc);
3726                cx25840_write4(client, DIF_BPF_COEFF2425, 0x0022f6f0);
3727                cx25840_write4(client, DIF_BPF_COEFF2627, 0xf407fada);
3728                cx25840_write4(client, DIF_BPF_COEFF2829, 0x06da0e74);
3729                cx25840_write4(client, DIF_BPF_COEFF3031, 0x0ad3fe06);
3730                cx25840_write4(client, DIF_BPF_COEFF3233, 0xf1fef0ab);
3731                cx25840_write4(client, DIF_BPF_COEFF3435, 0xfb9c0a55);
3732                cx25840_write4(client, DIF_BPF_COEFF36, 0x110d0000);
3733                break;
3734
3735        case 7400000:
3736                cx25840_write4(client, DIF_BPF_COEFF01, 0x00000001);
3737                cx25840_write4(client, DIF_BPF_COEFF23, 0x0008000e);
3738                cx25840_write4(client, DIF_BPF_COEFF45, 0xfffdffd0);