linux/drivers/media/pci/cx23885/cx23885-dvb.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *  Driver for the Conexant CX23885 PCIe bridge
   4 *
   5 *  Copyright (c) 2006 Steven Toth <stoth@linuxtv.org>
   6 */
   7
   8#include "cx23885.h"
   9
  10#include <linux/module.h>
  11#include <linux/init.h>
  12#include <linux/device.h>
  13#include <linux/fs.h>
  14#include <linux/kthread.h>
  15#include <linux/file.h>
  16#include <linux/suspend.h>
  17
  18#include <media/v4l2-common.h>
  19
  20#include <media/dvb_ca_en50221.h>
  21#include "s5h1409.h"
  22#include "s5h1411.h"
  23#include "mt2131.h"
  24#include "tda8290.h"
  25#include "tda18271.h"
  26#include "lgdt330x.h"
  27#include "xc4000.h"
  28#include "xc5000.h"
  29#include "max2165.h"
  30#include "tda10048.h"
  31#include "tuner-xc2028.h"
  32#include "tuner-simple.h"
  33#include "dib7000p.h"
  34#include "dib0070.h"
  35#include "dibx000_common.h"
  36#include "zl10353.h"
  37#include "stv0900.h"
  38#include "stv0900_reg.h"
  39#include "stv6110.h"
  40#include "lnbh24.h"
  41#include "cx24116.h"
  42#include "cx24117.h"
  43#include "cimax2.h"
  44#include "lgs8gxx.h"
  45#include "netup-eeprom.h"
  46#include "netup-init.h"
  47#include "lgdt3305.h"
  48#include "atbm8830.h"
  49#include "ts2020.h"
  50#include "ds3000.h"
  51#include "cx23885-f300.h"
  52#include "altera-ci.h"
  53#include "stv0367.h"
  54#include "drxk.h"
  55#include "mt2063.h"
  56#include "stv090x.h"
  57#include "stb6100.h"
  58#include "stb6100_cfg.h"
  59#include "tda10071.h"
  60#include "a8293.h"
  61#include "mb86a20s.h"
  62#include "si2165.h"
  63#include "si2168.h"
  64#include "si2157.h"
  65#include "sp2.h"
  66#include "m88ds3103.h"
  67#include "m88rs6000t.h"
  68#include "lgdt3306a.h"
  69
  70static unsigned int debug;
  71
  72#define dprintk(level, fmt, arg...)\
  73        do { if (debug >= level)\
  74                printk(KERN_DEBUG pr_fmt("%s dvb: " fmt), \
  75                        __func__, ##arg); \
  76        } while (0)
  77
  78/* ------------------------------------------------------------------ */
  79
  80static unsigned int alt_tuner;
  81module_param(alt_tuner, int, 0644);
  82MODULE_PARM_DESC(alt_tuner, "Enable alternate tuner configuration");
  83
  84DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  85
  86/* ------------------------------------------------------------------ */
  87
  88static int queue_setup(struct vb2_queue *q,
  89                           unsigned int *num_buffers, unsigned int *num_planes,
  90                           unsigned int sizes[], struct device *alloc_devs[])
  91{
  92        struct cx23885_tsport *port = q->drv_priv;
  93
  94        port->ts_packet_size  = 188 * 4;
  95        port->ts_packet_count = 32;
  96        *num_planes = 1;
  97        sizes[0] = port->ts_packet_size * port->ts_packet_count;
  98        *num_buffers = 32;
  99        return 0;
 100}
 101
 102
 103static int buffer_prepare(struct vb2_buffer *vb)
 104{
 105        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 106        struct cx23885_tsport *port = vb->vb2_queue->drv_priv;
 107        struct cx23885_buffer *buf =
 108                container_of(vbuf, struct cx23885_buffer, vb);
 109
 110        return cx23885_buf_prepare(buf, port);
 111}
 112
 113static void buffer_finish(struct vb2_buffer *vb)
 114{
 115        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 116        struct cx23885_tsport *port = vb->vb2_queue->drv_priv;
 117        struct cx23885_dev *dev = port->dev;
 118        struct cx23885_buffer *buf = container_of(vbuf,
 119                struct cx23885_buffer, vb);
 120
 121        cx23885_free_buffer(dev, buf);
 122}
 123
 124static void buffer_queue(struct vb2_buffer *vb)
 125{
 126        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 127        struct cx23885_tsport *port = vb->vb2_queue->drv_priv;
 128        struct cx23885_buffer   *buf = container_of(vbuf,
 129                struct cx23885_buffer, vb);
 130
 131        cx23885_buf_queue(port, buf);
 132}
 133
 134static void cx23885_dvb_gate_ctrl(struct cx23885_tsport  *port, int open)
 135{
 136        struct vb2_dvb_frontends *f;
 137        struct vb2_dvb_frontend *fe;
 138
 139        f = &port->frontends;
 140
 141        if (f->gate <= 1) /* undefined or fe0 */
 142                fe = vb2_dvb_get_frontend(f, 1);
 143        else
 144                fe = vb2_dvb_get_frontend(f, f->gate);
 145
 146        if (fe && fe->dvb.frontend && fe->dvb.frontend->ops.i2c_gate_ctrl)
 147                fe->dvb.frontend->ops.i2c_gate_ctrl(fe->dvb.frontend, open);
 148}
 149
 150static int cx23885_start_streaming(struct vb2_queue *q, unsigned int count)
 151{
 152        struct cx23885_tsport *port = q->drv_priv;
 153        struct cx23885_dmaqueue *dmaq = &port->mpegq;
 154        struct cx23885_buffer *buf = list_entry(dmaq->active.next,
 155                        struct cx23885_buffer, queue);
 156
 157        cx23885_start_dma(port, dmaq, buf);
 158        return 0;
 159}
 160
 161static void cx23885_stop_streaming(struct vb2_queue *q)
 162{
 163        struct cx23885_tsport *port = q->drv_priv;
 164
 165        cx23885_cancel_buffers(port);
 166}
 167
 168static const struct vb2_ops dvb_qops = {
 169        .queue_setup    = queue_setup,
 170        .buf_prepare  = buffer_prepare,
 171        .buf_finish = buffer_finish,
 172        .buf_queue    = buffer_queue,
 173        .wait_prepare = vb2_ops_wait_prepare,
 174        .wait_finish = vb2_ops_wait_finish,
 175        .start_streaming = cx23885_start_streaming,
 176        .stop_streaming = cx23885_stop_streaming,
 177};
 178
 179static struct s5h1409_config hauppauge_generic_config = {
 180        .demod_address = 0x32 >> 1,
 181        .output_mode   = S5H1409_SERIAL_OUTPUT,
 182        .gpio          = S5H1409_GPIO_ON,
 183        .qam_if        = 44000,
 184        .inversion     = S5H1409_INVERSION_OFF,
 185        .status_mode   = S5H1409_DEMODLOCKING,
 186        .mpeg_timing   = S5H1409_MPEGTIMING_CONTINUOUS_NONINVERTING_CLOCK,
 187};
 188
 189static struct tda10048_config hauppauge_hvr1200_config = {
 190        .demod_address    = 0x10 >> 1,
 191        .output_mode      = TDA10048_SERIAL_OUTPUT,
 192        .fwbulkwritelen   = TDA10048_BULKWRITE_200,
 193        .inversion        = TDA10048_INVERSION_ON,
 194        .dtv6_if_freq_khz = TDA10048_IF_3300,
 195        .dtv7_if_freq_khz = TDA10048_IF_3800,
 196        .dtv8_if_freq_khz = TDA10048_IF_4300,
 197        .clk_freq_khz     = TDA10048_CLK_16000,
 198};
 199
 200static struct tda10048_config hauppauge_hvr1210_config = {
 201        .demod_address    = 0x10 >> 1,
 202        .output_mode      = TDA10048_SERIAL_OUTPUT,
 203        .fwbulkwritelen   = TDA10048_BULKWRITE_200,
 204        .inversion        = TDA10048_INVERSION_ON,
 205        .dtv6_if_freq_khz = TDA10048_IF_3300,
 206        .dtv7_if_freq_khz = TDA10048_IF_3500,
 207        .dtv8_if_freq_khz = TDA10048_IF_4000,
 208        .clk_freq_khz     = TDA10048_CLK_16000,
 209};
 210
 211static struct s5h1409_config hauppauge_ezqam_config = {
 212        .demod_address = 0x32 >> 1,
 213        .output_mode   = S5H1409_SERIAL_OUTPUT,
 214        .gpio          = S5H1409_GPIO_OFF,
 215        .qam_if        = 4000,
 216        .inversion     = S5H1409_INVERSION_ON,
 217        .status_mode   = S5H1409_DEMODLOCKING,
 218        .mpeg_timing   = S5H1409_MPEGTIMING_CONTINUOUS_NONINVERTING_CLOCK,
 219};
 220
 221static struct s5h1409_config hauppauge_hvr1800lp_config = {
 222        .demod_address = 0x32 >> 1,
 223        .output_mode   = S5H1409_SERIAL_OUTPUT,
 224        .gpio          = S5H1409_GPIO_OFF,
 225        .qam_if        = 44000,
 226        .inversion     = S5H1409_INVERSION_OFF,
 227        .status_mode   = S5H1409_DEMODLOCKING,
 228        .mpeg_timing   = S5H1409_MPEGTIMING_CONTINUOUS_NONINVERTING_CLOCK,
 229};
 230
 231static struct s5h1409_config hauppauge_hvr1500_config = {
 232        .demod_address = 0x32 >> 1,
 233        .output_mode   = S5H1409_SERIAL_OUTPUT,
 234        .gpio          = S5H1409_GPIO_OFF,
 235        .inversion     = S5H1409_INVERSION_OFF,
 236        .status_mode   = S5H1409_DEMODLOCKING,
 237        .mpeg_timing   = S5H1409_MPEGTIMING_CONTINUOUS_NONINVERTING_CLOCK,
 238};
 239
 240static struct mt2131_config hauppauge_generic_tunerconfig = {
 241        0x61
 242};
 243
 244static struct lgdt330x_config fusionhdtv_5_express = {
 245        .demod_chip = LGDT3303,
 246        .serial_mpeg = 0x40,
 247};
 248
 249static struct s5h1409_config hauppauge_hvr1500q_config = {
 250        .demod_address = 0x32 >> 1,
 251        .output_mode   = S5H1409_SERIAL_OUTPUT,
 252        .gpio          = S5H1409_GPIO_ON,
 253        .qam_if        = 44000,
 254        .inversion     = S5H1409_INVERSION_OFF,
 255        .status_mode   = S5H1409_DEMODLOCKING,
 256        .mpeg_timing   = S5H1409_MPEGTIMING_CONTINUOUS_NONINVERTING_CLOCK,
 257};
 258
 259static struct s5h1409_config dvico_s5h1409_config = {
 260        .demod_address = 0x32 >> 1,
 261        .output_mode   = S5H1409_SERIAL_OUTPUT,
 262        .gpio          = S5H1409_GPIO_ON,
 263        .qam_if        = 44000,
 264        .inversion     = S5H1409_INVERSION_OFF,
 265        .status_mode   = S5H1409_DEMODLOCKING,
 266        .mpeg_timing   = S5H1409_MPEGTIMING_CONTINUOUS_NONINVERTING_CLOCK,
 267};
 268
 269static struct s5h1411_config dvico_s5h1411_config = {
 270        .output_mode   = S5H1411_SERIAL_OUTPUT,
 271        .gpio          = S5H1411_GPIO_ON,
 272        .qam_if        = S5H1411_IF_44000,
 273        .vsb_if        = S5H1411_IF_44000,
 274        .inversion     = S5H1411_INVERSION_OFF,
 275        .status_mode   = S5H1411_DEMODLOCKING,
 276        .mpeg_timing   = S5H1411_MPEGTIMING_CONTINUOUS_NONINVERTING_CLOCK,
 277};
 278
 279static struct s5h1411_config hcw_s5h1411_config = {
 280        .output_mode   = S5H1411_SERIAL_OUTPUT,
 281        .gpio          = S5H1411_GPIO_OFF,
 282        .vsb_if        = S5H1411_IF_44000,
 283        .qam_if        = S5H1411_IF_4000,
 284        .inversion     = S5H1411_INVERSION_ON,
 285        .status_mode   = S5H1411_DEMODLOCKING,
 286        .mpeg_timing   = S5H1411_MPEGTIMING_CONTINUOUS_NONINVERTING_CLOCK,
 287};
 288
 289static struct xc5000_config hauppauge_hvr1500q_tunerconfig = {
 290        .i2c_address      = 0x61,
 291        .if_khz           = 5380,
 292};
 293
 294static struct xc5000_config dvico_xc5000_tunerconfig = {
 295        .i2c_address      = 0x64,
 296        .if_khz           = 5380,
 297};
 298
 299static struct tda829x_config tda829x_no_probe = {
 300        .probe_tuner = TDA829X_DONT_PROBE,
 301};
 302
 303static struct tda18271_std_map hauppauge_tda18271_std_map = {
 304        .atsc_6   = { .if_freq = 5380, .agc_mode = 3, .std = 3,
 305                      .if_lvl = 6, .rfagc_top = 0x37 },
 306        .qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 0,
 307                      .if_lvl = 6, .rfagc_top = 0x37 },
 308};
 309
 310static struct tda18271_std_map hauppauge_hvr1200_tda18271_std_map = {
 311        .dvbt_6   = { .if_freq = 3300, .agc_mode = 3, .std = 4,
 312                      .if_lvl = 1, .rfagc_top = 0x37, },
 313        .dvbt_7   = { .if_freq = 3800, .agc_mode = 3, .std = 5,
 314                      .if_lvl = 1, .rfagc_top = 0x37, },
 315        .dvbt_8   = { .if_freq = 4300, .agc_mode = 3, .std = 6,
 316                      .if_lvl = 1, .rfagc_top = 0x37, },
 317};
 318
 319static struct tda18271_config hauppauge_tda18271_config = {
 320        .std_map = &hauppauge_tda18271_std_map,
 321        .gate    = TDA18271_GATE_ANALOG,
 322        .output_opt = TDA18271_OUTPUT_LT_OFF,
 323};
 324
 325static struct tda18271_config hauppauge_hvr1200_tuner_config = {
 326        .std_map = &hauppauge_hvr1200_tda18271_std_map,
 327        .gate    = TDA18271_GATE_ANALOG,
 328        .output_opt = TDA18271_OUTPUT_LT_OFF,
 329};
 330
 331static struct tda18271_config hauppauge_hvr1210_tuner_config = {
 332        .gate    = TDA18271_GATE_DIGITAL,
 333        .output_opt = TDA18271_OUTPUT_LT_OFF,
 334};
 335
 336static struct tda18271_config hauppauge_hvr4400_tuner_config = {
 337        .gate    = TDA18271_GATE_DIGITAL,
 338        .output_opt = TDA18271_OUTPUT_LT_OFF,
 339};
 340
 341static struct tda18271_std_map hauppauge_hvr127x_std_map = {
 342        .atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 4,
 343                      .if_lvl = 1, .rfagc_top = 0x58 },
 344        .qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 5,
 345                      .if_lvl = 1, .rfagc_top = 0x58 },
 346};
 347
 348static struct tda18271_config hauppauge_hvr127x_config = {
 349        .std_map = &hauppauge_hvr127x_std_map,
 350        .output_opt = TDA18271_OUTPUT_LT_OFF,
 351};
 352
 353static struct lgdt3305_config hauppauge_lgdt3305_config = {
 354        .i2c_addr           = 0x0e,
 355        .mpeg_mode          = LGDT3305_MPEG_SERIAL,
 356        .tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
 357        .tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
 358        .deny_i2c_rptr      = 1,
 359        .spectral_inversion = 1,
 360        .qam_if_khz         = 4000,
 361        .vsb_if_khz         = 3250,
 362};
 363
 364static struct dibx000_agc_config xc3028_agc_config = {
 365        BAND_VHF | BAND_UHF,    /* band_caps */
 366
 367        /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=0,
 368         * P_agc_inv_pwm1=0, P_agc_inv_pwm2=0,
 369         * P_agc_inh_dc_rv_est=0, P_agc_time_est=3, P_agc_freeze=0,
 370         * P_agc_nb_est=2, P_agc_write=0
 371         */
 372        (0 << 15) | (0 << 14) | (0 << 11) | (0 << 10) | (0 << 9) | (0 << 8) |
 373                (3 << 5) | (0 << 4) | (2 << 1) | (0 << 0), /* setup */
 374
 375        712,    /* inv_gain */
 376        21,     /* time_stabiliz */
 377
 378        0,      /* alpha_level */
 379        118,    /* thlock */
 380
 381        0,      /* wbd_inv */
 382        2867,   /* wbd_ref */
 383        0,      /* wbd_sel */
 384        2,      /* wbd_alpha */
 385
 386        0,      /* agc1_max */
 387        0,      /* agc1_min */
 388        39718,  /* agc2_max */
 389        9930,   /* agc2_min */
 390        0,      /* agc1_pt1 */
 391        0,      /* agc1_pt2 */
 392        0,      /* agc1_pt3 */
 393        0,      /* agc1_slope1 */
 394        0,      /* agc1_slope2 */
 395        0,      /* agc2_pt1 */
 396        128,    /* agc2_pt2 */
 397        29,     /* agc2_slope1 */
 398        29,     /* agc2_slope2 */
 399
 400        17,     /* alpha_mant */
 401        27,     /* alpha_exp */
 402        23,     /* beta_mant */
 403        51,     /* beta_exp */
 404
 405        1,      /* perform_agc_softsplit */
 406};
 407
 408/* PLL Configuration for COFDM BW_MHz = 8.000000
 409 * With external clock = 30.000000 */
 410static struct dibx000_bandwidth_config xc3028_bw_config = {
 411        60000,  /* internal */
 412        30000,  /* sampling */
 413        1,      /* pll_cfg: prediv */
 414        8,      /* pll_cfg: ratio */
 415        3,      /* pll_cfg: range */
 416        1,      /* pll_cfg: reset */
 417        0,      /* pll_cfg: bypass */
 418        0,      /* misc: refdiv */
 419        0,      /* misc: bypclk_div */
 420        1,      /* misc: IO_CLK_en_core */
 421        1,      /* misc: ADClkSrc */
 422        0,      /* misc: modulo */
 423        (3 << 14) | (1 << 12) | (524 << 0), /* sad_cfg: refsel, sel, freq_15k */
 424        (1 << 25) | 5816102, /* ifreq = 5.200000 MHz */
 425        20452225, /* timf */
 426        30000000  /* xtal_hz */
 427};
 428
 429static struct dib7000p_config hauppauge_hvr1400_dib7000_config = {
 430        .output_mpeg2_in_188_bytes = 1,
 431        .hostbus_diversity = 1,
 432        .tuner_is_baseband = 0,
 433        .update_lna  = NULL,
 434
 435        .agc_config_count = 1,
 436        .agc = &xc3028_agc_config,
 437        .bw  = &xc3028_bw_config,
 438
 439        .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
 440        .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
 441        .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
 442
 443        .pwm_freq_div = 0,
 444        .agc_control  = NULL,
 445        .spur_protect = 0,
 446
 447        .output_mode = OUTMODE_MPEG2_SERIAL,
 448};
 449
 450static struct zl10353_config dvico_fusionhdtv_xc3028 = {
 451        .demod_address = 0x0f,
 452        .if2           = 45600,
 453        .no_tuner      = 1,
 454        .disable_i2c_gate_ctrl = 1,
 455};
 456
 457static struct stv0900_reg stv0900_ts_regs[] = {
 458        { R0900_TSGENERAL, 0x00 },
 459        { R0900_P1_TSSPEED, 0x40 },
 460        { R0900_P2_TSSPEED, 0x40 },
 461        { R0900_P1_TSCFGM, 0xc0 },
 462        { R0900_P2_TSCFGM, 0xc0 },
 463        { R0900_P1_TSCFGH, 0xe0 },
 464        { R0900_P2_TSCFGH, 0xe0 },
 465        { R0900_P1_TSCFGL, 0x20 },
 466        { R0900_P2_TSCFGL, 0x20 },
 467        { 0xffff, 0xff }, /* terminate */
 468};
 469
 470static struct stv0900_config netup_stv0900_config = {
 471        .demod_address = 0x68,
 472        .demod_mode = 1, /* dual */
 473        .xtal = 8000000,
 474        .clkmode = 3,/* 0-CLKI, 2-XTALI, else AUTO */
 475        .diseqc_mode = 2,/* 2/3 PWM */
 476        .ts_config_regs = stv0900_ts_regs,
 477        .tun1_maddress = 0,/* 0x60 */
 478        .tun2_maddress = 3,/* 0x63 */
 479        .tun1_adc = 1,/* 1 Vpp */
 480        .tun2_adc = 1,/* 1 Vpp */
 481};
 482
 483static struct stv6110_config netup_stv6110_tunerconfig_a = {
 484        .i2c_address = 0x60,
 485        .mclk = 16000000,
 486        .clk_div = 1,
 487        .gain = 8, /* +16 dB  - maximum gain */
 488};
 489
 490static struct stv6110_config netup_stv6110_tunerconfig_b = {
 491        .i2c_address = 0x63,
 492        .mclk = 16000000,
 493        .clk_div = 1,
 494        .gain = 8, /* +16 dB  - maximum gain */
 495};
 496
 497static struct cx24116_config tbs_cx24116_config = {
 498        .demod_address = 0x55,
 499};
 500
 501static struct cx24117_config tbs_cx24117_config = {
 502        .demod_address = 0x55,
 503};
 504
 505static struct ds3000_config tevii_ds3000_config = {
 506        .demod_address = 0x68,
 507};
 508
 509static struct ts2020_config tevii_ts2020_config  = {
 510        .tuner_address = 0x60,
 511        .clk_out_div = 1,
 512        .frequency_div = 1146000,
 513};
 514
 515static struct cx24116_config dvbworld_cx24116_config = {
 516        .demod_address = 0x05,
 517};
 518
 519static struct lgs8gxx_config mygica_x8506_lgs8gl5_config = {
 520        .prod = LGS8GXX_PROD_LGS8GL5,
 521        .demod_address = 0x19,
 522        .serial_ts = 0,
 523        .ts_clk_pol = 1,
 524        .ts_clk_gated = 1,
 525        .if_clk_freq = 30400, /* 30.4 MHz */
 526        .if_freq = 5380, /* 5.38 MHz */
 527        .if_neg_center = 1,
 528        .ext_adc = 0,
 529        .adc_signed = 0,
 530        .if_neg_edge = 0,
 531};
 532
 533static struct xc5000_config mygica_x8506_xc5000_config = {
 534        .i2c_address = 0x61,
 535        .if_khz = 5380,
 536};
 537
 538static struct mb86a20s_config mygica_x8507_mb86a20s_config = {
 539        .demod_address = 0x10,
 540};
 541
 542static struct xc5000_config mygica_x8507_xc5000_config = {
 543        .i2c_address = 0x61,
 544        .if_khz = 4000,
 545};
 546
 547static struct stv090x_config prof_8000_stv090x_config = {
 548        .device                 = STV0903,
 549        .demod_mode             = STV090x_SINGLE,
 550        .clk_mode               = STV090x_CLK_EXT,
 551        .xtal                   = 27000000,
 552        .address                = 0x6A,
 553        .ts1_mode               = STV090x_TSMODE_PARALLEL_PUNCTURED,
 554        .repeater_level         = STV090x_RPTLEVEL_64,
 555        .adc1_range             = STV090x_ADC_2Vpp,
 556        .diseqc_envelope_mode   = false,
 557
 558        .tuner_get_frequency    = stb6100_get_frequency,
 559        .tuner_set_frequency    = stb6100_set_frequency,
 560        .tuner_set_bandwidth    = stb6100_set_bandwidth,
 561        .tuner_get_bandwidth    = stb6100_get_bandwidth,
 562};
 563
 564static struct stb6100_config prof_8000_stb6100_config = {
 565        .tuner_address = 0x60,
 566        .refclock = 27000000,
 567};
 568
 569static struct lgdt3306a_config hauppauge_quadHD_ATSC_a_config = {
 570        .i2c_addr               = 0x59,
 571        .qam_if_khz             = 4000,
 572        .vsb_if_khz             = 3250,
 573        .deny_i2c_rptr          = 1, /* Disabled */
 574        .spectral_inversion     = 0, /* Disabled */
 575        .mpeg_mode              = LGDT3306A_MPEG_SERIAL,
 576        .tpclk_edge             = LGDT3306A_TPCLK_RISING_EDGE,
 577        .tpvalid_polarity       = LGDT3306A_TP_VALID_HIGH,
 578        .xtalMHz                = 25, /* 24 or 25 */
 579};
 580
 581static struct lgdt3306a_config hauppauge_quadHD_ATSC_b_config = {
 582        .i2c_addr               = 0x0e,
 583        .qam_if_khz             = 4000,
 584        .vsb_if_khz             = 3250,
 585        .deny_i2c_rptr          = 1, /* Disabled */
 586        .spectral_inversion     = 0, /* Disabled */
 587        .mpeg_mode              = LGDT3306A_MPEG_SERIAL,
 588        .tpclk_edge             = LGDT3306A_TPCLK_RISING_EDGE,
 589        .tpvalid_polarity       = LGDT3306A_TP_VALID_HIGH,
 590        .xtalMHz                = 25, /* 24 or 25 */
 591};
 592
 593static int p8000_set_voltage(struct dvb_frontend *fe,
 594                             enum fe_sec_voltage voltage)
 595{
 596        struct cx23885_tsport *port = fe->dvb->priv;
 597        struct cx23885_dev *dev = port->dev;
 598
 599        if (voltage == SEC_VOLTAGE_18)
 600                cx_write(MC417_RWD, 0x00001e00);
 601        else if (voltage == SEC_VOLTAGE_13)
 602                cx_write(MC417_RWD, 0x00001a00);
 603        else
 604                cx_write(MC417_RWD, 0x00001800);
 605        return 0;
 606}
 607
 608static int dvbsky_t9580_set_voltage(struct dvb_frontend *fe,
 609                                        enum fe_sec_voltage voltage)
 610{
 611        struct cx23885_tsport *port = fe->dvb->priv;
 612        struct cx23885_dev *dev = port->dev;
 613
 614        cx23885_gpio_enable(dev, GPIO_0 | GPIO_1, 1);
 615
 616        switch (voltage) {
 617        case SEC_VOLTAGE_13:
 618                cx23885_gpio_set(dev, GPIO_1);
 619                cx23885_gpio_clear(dev, GPIO_0);
 620                break;
 621        case SEC_VOLTAGE_18:
 622                cx23885_gpio_set(dev, GPIO_1);
 623                cx23885_gpio_set(dev, GPIO_0);
 624                break;
 625        case SEC_VOLTAGE_OFF:
 626                cx23885_gpio_clear(dev, GPIO_1);
 627                cx23885_gpio_clear(dev, GPIO_0);
 628                break;
 629        }
 630
 631        /* call the frontend set_voltage function */
 632        port->fe_set_voltage(fe, voltage);
 633
 634        return 0;
 635}
 636
 637static int dvbsky_s952_portc_set_voltage(struct dvb_frontend *fe,
 638                                        enum fe_sec_voltage voltage)
 639{
 640        struct cx23885_tsport *port = fe->dvb->priv;
 641        struct cx23885_dev *dev = port->dev;
 642
 643        cx23885_gpio_enable(dev, GPIO_12 | GPIO_13, 1);
 644
 645        switch (voltage) {
 646        case SEC_VOLTAGE_13:
 647                cx23885_gpio_set(dev, GPIO_13);
 648                cx23885_gpio_clear(dev, GPIO_12);
 649                break;
 650        case SEC_VOLTAGE_18:
 651                cx23885_gpio_set(dev, GPIO_13);
 652                cx23885_gpio_set(dev, GPIO_12);
 653                break;
 654        case SEC_VOLTAGE_OFF:
 655                cx23885_gpio_clear(dev, GPIO_13);
 656                cx23885_gpio_clear(dev, GPIO_12);
 657                break;
 658        }
 659        /* call the frontend set_voltage function */
 660        return port->fe_set_voltage(fe, voltage);
 661}
 662
 663static int cx23885_sp2_ci_ctrl(void *priv, u8 read, int addr,
 664                                u8 data, int *mem)
 665{
 666        /* MC417 */
 667        #define SP2_DATA              0x000000ff
 668        #define SP2_WR                0x00008000
 669        #define SP2_RD                0x00004000
 670        #define SP2_ACK               0x00001000
 671        #define SP2_ADHI              0x00000800
 672        #define SP2_ADLO              0x00000400
 673        #define SP2_CS1               0x00000200
 674        #define SP2_CS0               0x00000100
 675        #define SP2_EN_ALL            0x00001000
 676        #define SP2_CTRL_OFF          (SP2_CS1 | SP2_CS0 | SP2_WR | SP2_RD)
 677
 678        struct cx23885_tsport *port = priv;
 679        struct cx23885_dev *dev = port->dev;
 680        int ret;
 681        int tmp = 0;
 682        unsigned long timeout;
 683
 684        mutex_lock(&dev->gpio_lock);
 685
 686        /* write addr */
 687        cx_write(MC417_OEN, SP2_EN_ALL);
 688        cx_write(MC417_RWD, SP2_CTRL_OFF |
 689                                SP2_ADLO | (0xff & addr));
 690        cx_clear(MC417_RWD, SP2_ADLO);
 691        cx_write(MC417_RWD, SP2_CTRL_OFF |
 692                                SP2_ADHI | (0xff & (addr >> 8)));
 693        cx_clear(MC417_RWD, SP2_ADHI);
 694
 695        if (read)
 696                /* data in */
 697                cx_write(MC417_OEN, SP2_EN_ALL | SP2_DATA);
 698        else
 699                /* data out */
 700                cx_write(MC417_RWD, SP2_CTRL_OFF | data);
 701
 702        /* chip select 0 */
 703        cx_clear(MC417_RWD, SP2_CS0);
 704
 705        /* read/write */
 706        cx_clear(MC417_RWD, (read) ? SP2_RD : SP2_WR);
 707
 708        /* wait for a maximum of 1 msec */
 709        timeout = jiffies + msecs_to_jiffies(1);
 710        while (!time_after(jiffies, timeout)) {
 711                tmp = cx_read(MC417_RWD);
 712                if ((tmp & SP2_ACK) == 0)
 713                        break;
 714                usleep_range(50, 100);
 715        }
 716
 717        cx_set(MC417_RWD, SP2_CTRL_OFF);
 718        *mem = tmp & 0xff;
 719
 720        mutex_unlock(&dev->gpio_lock);
 721
 722        if (!read) {
 723                if (*mem < 0) {
 724                        ret = -EREMOTEIO;
 725                        goto err;
 726                }
 727        }
 728
 729        return 0;
 730err:
 731        return ret;
 732}
 733
 734static int cx23885_dvb_set_frontend(struct dvb_frontend *fe)
 735{
 736        struct dtv_frontend_properties *p = &fe->dtv_property_cache;
 737        struct cx23885_tsport *port = fe->dvb->priv;
 738        struct cx23885_dev *dev = port->dev;
 739
 740        switch (dev->board) {
 741        case CX23885_BOARD_HAUPPAUGE_HVR1275:
 742                switch (p->modulation) {
 743                case VSB_8:
 744                        cx23885_gpio_clear(dev, GPIO_5);
 745                        break;
 746                case QAM_64:
 747                case QAM_256:
 748                default:
 749                        cx23885_gpio_set(dev, GPIO_5);
 750                        break;
 751                }
 752                break;
 753        case CX23885_BOARD_MYGICA_X8506:
 754        case CX23885_BOARD_MYGICA_X8507:
 755        case CX23885_BOARD_MAGICPRO_PROHDTVE2:
 756                /* Select Digital TV */
 757                cx23885_gpio_set(dev, GPIO_0);
 758                break;
 759        }
 760
 761        /* Call the real set_frontend */
 762        if (port->set_frontend)
 763                return port->set_frontend(fe);
 764
 765        return 0;
 766}
 767
 768static void cx23885_set_frontend_hook(struct cx23885_tsport *port,
 769                                     struct dvb_frontend *fe)
 770{
 771        port->set_frontend = fe->ops.set_frontend;
 772        fe->ops.set_frontend = cx23885_dvb_set_frontend;
 773}
 774
 775static struct lgs8gxx_config magicpro_prohdtve2_lgs8g75_config = {
 776        .prod = LGS8GXX_PROD_LGS8G75,
 777        .demod_address = 0x19,
 778        .serial_ts = 0,
 779        .ts_clk_pol = 1,
 780        .ts_clk_gated = 1,
 781        .if_clk_freq = 30400, /* 30.4 MHz */
 782        .if_freq = 6500, /* 6.50 MHz */
 783        .if_neg_center = 1,
 784        .ext_adc = 0,
 785        .adc_signed = 1,
 786        .adc_vpp = 2, /* 1.6 Vpp */
 787        .if_neg_edge = 1,
 788};
 789
 790static struct xc5000_config magicpro_prohdtve2_xc5000_config = {
 791        .i2c_address = 0x61,
 792        .if_khz = 6500,
 793};
 794
 795static struct atbm8830_config mygica_x8558pro_atbm8830_cfg1 = {
 796        .prod = ATBM8830_PROD_8830,
 797        .demod_address = 0x44,
 798        .serial_ts = 0,
 799        .ts_sampling_edge = 1,
 800        .ts_clk_gated = 0,
 801        .osc_clk_freq = 30400, /* in kHz */
 802        .if_freq = 0, /* zero IF */
 803        .zif_swap_iq = 1,
 804        .agc_min = 0x2E,
 805        .agc_max = 0xFF,
 806        .agc_hold_loop = 0,
 807};
 808
 809static struct max2165_config mygic_x8558pro_max2165_cfg1 = {
 810        .i2c_address = 0x60,
 811        .osc_clk = 20
 812};
 813
 814static struct atbm8830_config mygica_x8558pro_atbm8830_cfg2 = {
 815        .prod = ATBM8830_PROD_8830,
 816        .demod_address = 0x44,
 817        .serial_ts = 1,
 818        .ts_sampling_edge = 1,
 819        .ts_clk_gated = 0,
 820        .osc_clk_freq = 30400, /* in kHz */
 821        .if_freq = 0, /* zero IF */
 822        .zif_swap_iq = 1,
 823        .agc_min = 0x2E,
 824        .agc_max = 0xFF,
 825        .agc_hold_loop = 0,
 826};
 827
 828static struct max2165_config mygic_x8558pro_max2165_cfg2 = {
 829        .i2c_address = 0x60,
 830        .osc_clk = 20
 831};
 832static struct stv0367_config netup_stv0367_config[] = {
 833        {
 834                .demod_address = 0x1c,
 835                .xtal = 27000000,
 836                .if_khz = 4500,
 837                .if_iq_mode = 0,
 838                .ts_mode = 1,
 839                .clk_pol = 0,
 840        }, {
 841                .demod_address = 0x1d,
 842                .xtal = 27000000,
 843                .if_khz = 4500,
 844                .if_iq_mode = 0,
 845                .ts_mode = 1,
 846                .clk_pol = 0,
 847        },
 848};
 849
 850static struct xc5000_config netup_xc5000_config[] = {
 851        {
 852                .i2c_address = 0x61,
 853                .if_khz = 4500,
 854        }, {
 855                .i2c_address = 0x64,
 856                .if_khz = 4500,
 857        },
 858};
 859
 860static struct drxk_config terratec_drxk_config[] = {
 861        {
 862                .adr = 0x29,
 863                .no_i2c_bridge = 1,
 864        }, {
 865                .adr = 0x2a,
 866                .no_i2c_bridge = 1,
 867        },
 868};
 869
 870static struct mt2063_config terratec_mt2063_config[] = {
 871        {
 872                .tuner_address = 0x60,
 873        }, {
 874                .tuner_address = 0x67,
 875        },
 876};
 877
 878static const struct tda10071_platform_data hauppauge_tda10071_pdata = {
 879        .clk = 40444000, /* 40.444 MHz */
 880        .i2c_wr_max = 64,
 881        .ts_mode = TDA10071_TS_SERIAL,
 882        .pll_multiplier = 20,
 883        .tuner_i2c_addr = 0x54,
 884};
 885
 886static const struct m88ds3103_config dvbsky_t9580_m88ds3103_config = {
 887        .i2c_addr = 0x68,
 888        .clock = 27000000,
 889        .i2c_wr_max = 33,
 890        .clock_out = 0,
 891        .ts_mode = M88DS3103_TS_PARALLEL,
 892        .ts_clk = 16000,
 893        .ts_clk_pol = 1,
 894        .lnb_en_pol = 1,
 895        .lnb_hv_pol = 0,
 896        .agc = 0x99,
 897};
 898
 899static const struct m88ds3103_config dvbsky_s950c_m88ds3103_config = {
 900        .i2c_addr = 0x68,
 901        .clock = 27000000,
 902        .i2c_wr_max = 33,
 903        .clock_out = 0,
 904        .ts_mode = M88DS3103_TS_CI,
 905        .ts_clk = 10000,
 906        .ts_clk_pol = 1,
 907        .lnb_en_pol = 1,
 908        .lnb_hv_pol = 0,
 909        .agc = 0x99,
 910};
 911
 912static const struct m88ds3103_config hauppauge_hvr5525_m88ds3103_config = {
 913        .i2c_addr = 0x69,
 914        .clock = 27000000,
 915        .i2c_wr_max = 33,
 916        .ts_mode = M88DS3103_TS_PARALLEL,
 917        .ts_clk = 16000,
 918        .ts_clk_pol = 1,
 919        .agc = 0x99,
 920};
 921
 922static struct lgdt3306a_config hauppauge_hvr1265k4_config = {
 923        .i2c_addr               = 0x59,
 924        .qam_if_khz             = 4000,
 925        .vsb_if_khz             = 3250,
 926        .deny_i2c_rptr          = 1, /* Disabled */
 927        .spectral_inversion     = 0, /* Disabled */
 928        .mpeg_mode              = LGDT3306A_MPEG_SERIAL,
 929        .tpclk_edge             = LGDT3306A_TPCLK_RISING_EDGE,
 930        .tpvalid_polarity       = LGDT3306A_TP_VALID_HIGH,
 931        .xtalMHz                = 25, /* 24 or 25 */
 932};
 933
 934static int netup_altera_fpga_rw(void *device, int flag, int data, int read)
 935{
 936        struct cx23885_dev *dev = (struct cx23885_dev *)device;
 937        unsigned long timeout = jiffies + msecs_to_jiffies(1);
 938        uint32_t mem = 0;
 939
 940        mem = cx_read(MC417_RWD);
 941        if (read)
 942                cx_set(MC417_OEN, ALT_DATA);
 943        else {
 944                cx_clear(MC417_OEN, ALT_DATA);/* D0-D7 out */
 945                mem &= ~ALT_DATA;
 946                mem |= (data & ALT_DATA);
 947        }
 948
 949        if (flag)
 950                mem |= ALT_AD_RG;
 951        else
 952                mem &= ~ALT_AD_RG;
 953
 954        mem &= ~ALT_CS;
 955        if (read)
 956                mem = (mem & ~ALT_RD) | ALT_WR;
 957        else
 958                mem = (mem & ~ALT_WR) | ALT_RD;
 959
 960        cx_write(MC417_RWD, mem);  /* start RW cycle */
 961
 962        for (;;) {
 963                mem = cx_read(MC417_RWD);
 964                if ((mem & ALT_RDY) == 0)
 965                        break;
 966                if (time_after(jiffies, timeout))
 967                        break;
 968                udelay(1);
 969        }
 970
 971        cx_set(MC417_RWD, ALT_RD | ALT_WR | ALT_CS);
 972        if (read)
 973                return mem & ALT_DATA;
 974
 975        return 0;
 976};
 977
 978static int dib7070_tuner_reset(struct dvb_frontend *fe, int onoff)
 979{
 980        struct dib7000p_ops *dib7000p_ops = fe->sec_priv;
 981
 982        return dib7000p_ops->set_gpio(fe, 8, 0, !onoff);
 983}
 984
 985static int dib7070_tuner_sleep(struct dvb_frontend *fe, int onoff)
 986{
 987        return 0;
 988}
 989
 990static struct dib0070_config dib7070p_dib0070_config = {
 991        .i2c_address = DEFAULT_DIB0070_I2C_ADDRESS,
 992        .reset = dib7070_tuner_reset,
 993        .sleep = dib7070_tuner_sleep,
 994        .clock_khz = 12000,
 995        .freq_offset_khz_vhf = 550,
 996        /* .flip_chip = 1, */
 997};
 998
 999/* DIB7070 generic */
1000static struct dibx000_agc_config dib7070_agc_config = {
1001        .band_caps = BAND_UHF | BAND_VHF | BAND_LBAND | BAND_SBAND,
1002
1003        /*
1004         * P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=5,
1005         * P_agc_inv_pwm1=0, P_agc_inv_pwm2=0, P_agc_inh_dc_rv_est=0,
1006         * P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=5, P_agc_write=0
1007         */
1008        .setup = (0 << 15) | (0 << 14) | (5 << 11) | (0 << 10) | (0 << 9) |
1009                 (0 << 8) | (3 << 5) | (0 << 4) | (5 << 1) | (0 << 0),
1010        .inv_gain = 600,
1011        .time_stabiliz = 10,
1012        .alpha_level = 0,
1013        .thlock = 118,
1014        .wbd_inv = 0,
1015        .wbd_ref = 3530,
1016        .wbd_sel = 1,
1017        .wbd_alpha = 5,
1018        .agc1_max = 65535,
1019        .agc1_min = 0,
1020        .agc2_max = 65535,
1021        .agc2_min = 0,
1022        .agc1_pt1 = 0,
1023        .agc1_pt2 = 40,
1024        .agc1_pt3 = 183,
1025        .agc1_slope1 = 206,
1026        .agc1_slope2 = 255,
1027        .agc2_pt1 = 72,
1028        .agc2_pt2 = 152,
1029        .agc2_slope1 = 88,
1030        .agc2_slope2 = 90,
1031        .alpha_mant = 17,
1032        .alpha_exp = 27,
1033        .beta_mant = 23,
1034        .beta_exp = 51,
1035        .perform_agc_softsplit = 0,
1036};
1037
1038static struct dibx000_bandwidth_config dib7070_bw_config_12_mhz = {
1039        .internal = 60000,
1040        .sampling = 15000,
1041        .pll_prediv = 1,
1042        .pll_ratio = 20,
1043        .pll_range = 3,
1044        .pll_reset = 1,
1045        .pll_bypass = 0,
1046        .enable_refdiv = 0,
1047        .bypclk_div = 0,
1048        .IO_CLK_en_core = 1,
1049        .ADClkSrc = 1,
1050        .modulo = 2,
1051        /* refsel, sel, freq_15k */
1052        .sad_cfg = (3 << 14) | (1 << 12) | (524 << 0),
1053        .ifreq = (0 << 25) | 0,
1054        .timf = 20452225,
1055        .xtal_hz = 12000000,
1056};
1057
1058static struct dib7000p_config dib7070p_dib7000p_config = {
1059        /* .output_mode = OUTMODE_MPEG2_FIFO, */
1060        .output_mode = OUTMODE_MPEG2_SERIAL,
1061        /* .output_mode = OUTMODE_MPEG2_PAR_GATED_CLK, */
1062        .output_mpeg2_in_188_bytes = 1,
1063
1064        .agc_config_count = 1,
1065        .agc = &dib7070_agc_config,
1066        .bw  = &dib7070_bw_config_12_mhz,
1067        .tuner_is_baseband = 1,
1068        .spur_protect = 1,
1069
1070        .gpio_dir = 0xfcef, /* DIB7000P_GPIO_DEFAULT_DIRECTIONS, */
1071        .gpio_val = 0x0110, /* DIB7000P_GPIO_DEFAULT_VALUES, */
1072        .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
1073
1074        .hostbus_diversity = 1,
1075};
1076
1077static int dvb_register_ci_mac(struct cx23885_tsport *port)
1078{
1079        struct cx23885_dev *dev = port->dev;
1080        struct i2c_client *client_ci = NULL;
1081        struct vb2_dvb_frontend *fe0;
1082
1083        fe0 = vb2_dvb_get_frontend(&port->frontends, 1);
1084        if (!fe0)
1085                return -EINVAL;
1086
1087        switch (dev->board) {
1088        case CX23885_BOARD_NETUP_DUAL_DVBS2_CI: {
1089                static struct netup_card_info cinfo;
1090
1091                netup_get_card_info(&dev->i2c_bus[0].i2c_adap, &cinfo);
1092                memcpy(port->frontends.adapter.proposed_mac,
1093                                cinfo.port[port->nr - 1].mac, 6);
1094                pr_info("NetUP Dual DVB-S2 CI card port%d MAC=%pM\n",
1095                        port->nr, port->frontends.adapter.proposed_mac);
1096
1097                netup_ci_init(port);
1098                return 0;
1099                }
1100        case CX23885_BOARD_NETUP_DUAL_DVB_T_C_CI_RF: {
1101                struct altera_ci_config netup_ci_cfg = {
1102                        .dev = dev,/* magic number to identify*/
1103                        .adapter = &port->frontends.adapter,/* for CI */
1104                        .demux = &fe0->dvb.demux,/* for hw pid filter */
1105                        .fpga_rw = netup_altera_fpga_rw,
1106                };
1107
1108                altera_ci_init(&netup_ci_cfg, port->nr);
1109                return 0;
1110                }
1111        case CX23885_BOARD_TEVII_S470: {
1112                u8 eeprom[256]; /* 24C02 i2c eeprom */
1113
1114                if (port->nr != 1)
1115                        return 0;
1116
1117                /* Read entire EEPROM */
1118                dev->i2c_bus[0].i2c_client.addr = 0xa0 >> 1;
1119                tveeprom_read(&dev->i2c_bus[0].i2c_client, eeprom, sizeof(eeprom));
1120                pr_info("TeVii S470 MAC= %pM\n", eeprom + 0xa0);
1121                memcpy(port->frontends.adapter.proposed_mac, eeprom + 0xa0, 6);
1122                return 0;
1123                }
1124        case CX23885_BOARD_DVBSKY_T9580:
1125        case CX23885_BOARD_DVBSKY_S950:
1126        case CX23885_BOARD_DVBSKY_S952:
1127        case CX23885_BOARD_DVBSKY_T982: {
1128                u8 eeprom[256]; /* 24C02 i2c eeprom */
1129
1130                if (port->nr > 2)
1131                        return 0;
1132
1133                /* Read entire EEPROM */
1134                dev->i2c_bus[0].i2c_client.addr = 0xa0 >> 1;
1135                tveeprom_read(&dev->i2c_bus[0].i2c_client, eeprom,
1136                                sizeof(eeprom));
1137                pr_info("%s port %d MAC address: %pM\n",
1138                        cx23885_boards[dev->board].name, port->nr,
1139                        eeprom + 0xc0 + (port->nr-1) * 8);
1140                memcpy(port->frontends.adapter.proposed_mac, eeprom + 0xc0 +
1141                        (port->nr-1) * 8, 6);
1142                return 0;
1143                }
1144        case CX23885_BOARD_DVBSKY_S950C:
1145        case CX23885_BOARD_DVBSKY_T980C:
1146        case CX23885_BOARD_TT_CT2_4500_CI: {
1147                u8 eeprom[256]; /* 24C02 i2c eeprom */
1148                struct sp2_config sp2_config;
1149                struct i2c_board_info info;
1150                struct cx23885_i2c *i2c_bus = &dev->i2c_bus[0];
1151
1152                /* attach CI */
1153                memset(&sp2_config, 0, sizeof(sp2_config));
1154                sp2_config.dvb_adap = &port->frontends.adapter;
1155                sp2_config.priv = port;
1156                sp2_config.ci_control = cx23885_sp2_ci_ctrl;
1157                memset(&info, 0, sizeof(struct i2c_board_info));
1158                strscpy(info.type, "sp2", I2C_NAME_SIZE);
1159                info.addr = 0x40;
1160                info.platform_data = &sp2_config;
1161                request_module(info.type);
1162                client_ci = i2c_new_device(&i2c_bus->i2c_adap, &info);
1163                if (client_ci == NULL || client_ci->dev.driver == NULL)
1164                        return -ENODEV;
1165                if (!try_module_get(client_ci->dev.driver->owner)) {
1166                        i2c_unregister_device(client_ci);
1167                        return -ENODEV;
1168                }
1169                port->i2c_client_ci = client_ci;
1170
1171                if (port->nr != 1)
1172                        return 0;
1173
1174                /* Read entire EEPROM */
1175                dev->i2c_bus[0].i2c_client.addr = 0xa0 >> 1;
1176                tveeprom_read(&dev->i2c_bus[0].i2c_client, eeprom,
1177                                sizeof(eeprom));
1178                pr_info("%s MAC address: %pM\n",
1179                        cx23885_boards[dev->board].name, eeprom + 0xc0);
1180                memcpy(port->frontends.adapter.proposed_mac, eeprom + 0xc0, 6);
1181                return 0;
1182                }
1183        }
1184        return 0;
1185}
1186
1187static int dvb_register(struct cx23885_tsport *port)
1188{
1189        struct dib7000p_ops dib7000p_ops;
1190        struct cx23885_dev *dev = port->dev;
1191        struct cx23885_i2c *i2c_bus = NULL, *i2c_bus2 = NULL;
1192        struct vb2_dvb_frontend *fe0, *fe1 = NULL;
1193        struct si2168_config si2168_config;
1194        struct si2165_platform_data si2165_pdata;
1195        struct si2157_config si2157_config;
1196        struct ts2020_config ts2020_config;
1197        struct m88ds3103_platform_data m88ds3103_pdata;
1198        struct m88rs6000t_config m88rs6000t_config = {};
1199        struct a8293_platform_data a8293_pdata = {};
1200        struct i2c_board_info info;
1201        struct i2c_adapter *adapter;
1202        struct i2c_client *client_demod = NULL, *client_tuner = NULL;
1203        struct i2c_client *client_sec = NULL;
1204        int (*p_set_voltage)(struct dvb_frontend *fe,
1205                             enum fe_sec_voltage voltage) = NULL;
1206        int mfe_shared = 0; /* bus not shared by default */
1207        int ret;
1208
1209        /* Get the first frontend */
1210        fe0 = vb2_dvb_get_frontend(&port->frontends, 1);
1211        if (!fe0)
1212                return -EINVAL;
1213
1214        /* init struct vb2_dvb */
1215        fe0->dvb.name = dev->name;
1216
1217        /* multi-frontend gate control is undefined or defaults to fe0 */
1218        port->frontends.gate = 0;
1219
1220        /* Sets the gate control callback to be used by i2c command calls */
1221        port->gate_ctrl = cx23885_dvb_gate_ctrl;
1222
1223        /* init frontend */
1224        switch (dev->board) {
1225        case CX23885_BOARD_HAUPPAUGE_HVR1250:
1226                i2c_bus = &dev->i2c_bus[0];
1227                fe0->dvb.frontend = dvb_attach(s5h1409_attach,
1228                                                &hauppauge_generic_config,
1229                                                &i2c_bus->i2c_adap);
1230                if (fe0->dvb.frontend == NULL)
1231                        break;
1232                dvb_attach(mt2131_attach, fe0->dvb.frontend,
1233                           &i2c_bus->i2c_adap,
1234                           &hauppauge_generic_tunerconfig, 0);
1235                break;
1236        case CX23885_BOARD_HAUPPAUGE_HVR1270:
1237        case CX23885_BOARD_HAUPPAUGE_HVR1275:
1238                i2c_bus = &dev->i2c_bus[0];
1239                fe0->dvb.frontend = dvb_attach(lgdt3305_attach,
1240                                               &hauppauge_lgdt3305_config,
1241                                               &i2c_bus->i2c_adap);
1242                if (fe0->dvb.frontend == NULL)
1243                        break;
1244                dvb_attach(tda18271_attach, fe0->dvb.frontend,
1245                           0x60, &dev->i2c_bus[1].i2c_adap,
1246                           &hauppauge_hvr127x_config);
1247                if (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1275)
1248                        cx23885_set_frontend_hook(port, fe0->dvb.frontend);
1249                break;
1250        case CX23885_BOARD_HAUPPAUGE_HVR1255:
1251        case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
1252                i2c_bus = &dev->i2c_bus[0];
1253                fe0->dvb.frontend = dvb_attach(s5h1411_attach,
1254                                               &hcw_s5h1411_config,
1255                                               &i2c_bus->i2c_adap);
1256                if (fe0->dvb.frontend == NULL)
1257                        break;
1258
1259                dvb_attach(tda18271_attach, fe0->dvb.frontend,
1260                           0x60, &dev->i2c_bus[1].i2c_adap,
1261                           &hauppauge_tda18271_config);
1262
1263                tda18271_attach(&dev->ts1.analog_fe,
1264                        0x60, &dev->i2c_bus[1].i2c_adap,
1265                        &hauppauge_tda18271_config);
1266
1267                break;
1268        case CX23885_BOARD_HAUPPAUGE_HVR1800:
1269                i2c_bus = &dev->i2c_bus[0];
1270                switch (alt_tuner) {
1271                case 1:
1272                        fe0->dvb.frontend =
1273                                dvb_attach(s5h1409_attach,
1274                                           &hauppauge_ezqam_config,
1275                                           &i2c_bus->i2c_adap);
1276                        if (fe0->dvb.frontend == NULL)
1277                                break;
1278
1279                        dvb_attach(tda829x_attach, fe0->dvb.frontend,
1280                                   &dev->i2c_bus[1].i2c_adap, 0x42,
1281                                   &tda829x_no_probe);
1282                        dvb_attach(tda18271_attach, fe0->dvb.frontend,
1283                                   0x60, &dev->i2c_bus[1].i2c_adap,
1284                                   &hauppauge_tda18271_config);
1285                        break;
1286                case 0:
1287                default:
1288                        fe0->dvb.frontend =
1289                                dvb_attach(s5h1409_attach,
1290                                           &hauppauge_generic_config,
1291                                           &i2c_bus->i2c_adap);
1292                        if (fe0->dvb.frontend == NULL)
1293                                break;
1294                        dvb_attach(mt2131_attach, fe0->dvb.frontend,
1295                                   &i2c_bus->i2c_adap,
1296                                   &hauppauge_generic_tunerconfig, 0);
1297                }
1298                break;
1299        case CX23885_BOARD_HAUPPAUGE_HVR1800lp:
1300                i2c_bus = &dev->i2c_bus[0];
1301                fe0->dvb.frontend = dvb_attach(s5h1409_attach,
1302                                                &hauppauge_hvr1800lp_config,
1303                                                &i2c_bus->i2c_adap);
1304                if (fe0->dvb.frontend == NULL)
1305                        break;
1306                dvb_attach(mt2131_attach, fe0->dvb.frontend,
1307                           &i2c_bus->i2c_adap,
1308                           &hauppauge_generic_tunerconfig, 0);
1309                break;
1310        case CX23885_BOARD_DVICO_FUSIONHDTV_5_EXP:
1311                i2c_bus = &dev->i2c_bus[0];
1312                fe0->dvb.frontend = dvb_attach(lgdt330x_attach,
1313                                               &fusionhdtv_5_express,
1314                                               0x0e,
1315                                               &i2c_bus->i2c_adap);
1316                if (fe0->dvb.frontend == NULL)
1317                        break;
1318                dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1319                           &i2c_bus->i2c_adap, 0x61,
1320                           TUNER_LG_TDVS_H06XF);
1321                break;
1322        case CX23885_BOARD_HAUPPAUGE_HVR1500Q:
1323                i2c_bus = &dev->i2c_bus[1];
1324                fe0->dvb.frontend = dvb_attach(s5h1409_attach,
1325                                                &hauppauge_hvr1500q_config,
1326                                                &dev->i2c_bus[0].i2c_adap);
1327                if (fe0->dvb.frontend == NULL)
1328                        break;
1329                dvb_attach(xc5000_attach, fe0->dvb.frontend,
1330                           &i2c_bus->i2c_adap,
1331                           &hauppauge_hvr1500q_tunerconfig);
1332                break;
1333        case CX23885_BOARD_HAUPPAUGE_HVR1500:
1334                i2c_bus = &dev->i2c_bus[1];
1335                fe0->dvb.frontend = dvb_attach(s5h1409_attach,
1336                                                &hauppauge_hvr1500_config,
1337                                                &dev->i2c_bus[0].i2c_adap);
1338                if (fe0->dvb.frontend != NULL) {
1339                        struct dvb_frontend *fe;
1340                        struct xc2028_config cfg = {
1341                                .i2c_adap  = &i2c_bus->i2c_adap,
1342                                .i2c_addr  = 0x61,
1343                        };
1344                        static struct xc2028_ctrl ctl = {
1345                                .fname       = XC2028_DEFAULT_FIRMWARE,
1346                                .max_len     = 64,
1347                                .demod       = XC3028_FE_OREN538,
1348                        };
1349
1350                        fe = dvb_attach(xc2028_attach,
1351                                        fe0->dvb.frontend, &cfg);
1352                        if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
1353                                fe->ops.tuner_ops.set_config(fe, &ctl);
1354                }
1355                break;
1356        case CX23885_BOARD_HAUPPAUGE_HVR1200:
1357        case CX23885_BOARD_HAUPPAUGE_HVR1700:
1358                i2c_bus = &dev->i2c_bus[0];
1359                fe0->dvb.frontend = dvb_attach(tda10048_attach,
1360                        &hauppauge_hvr1200_config,
1361                        &i2c_bus->i2c_adap);
1362                if (fe0->dvb.frontend == NULL)
1363                        break;
1364                dvb_attach(tda829x_attach, fe0->dvb.frontend,
1365                           &dev->i2c_bus[1].i2c_adap, 0x42,
1366                           &tda829x_no_probe);
1367                dvb_attach(tda18271_attach, fe0->dvb.frontend,
1368                           0x60, &dev->i2c_bus[1].i2c_adap,
1369                           &hauppauge_hvr1200_tuner_config);
1370                break;
1371        case CX23885_BOARD_HAUPPAUGE_HVR1210:
1372                i2c_bus = &dev->i2c_bus[0];
1373                fe0->dvb.frontend = dvb_attach(tda10048_attach,
1374                        &hauppauge_hvr1210_config,
1375                        &i2c_bus->i2c_adap);
1376                if (fe0->dvb.frontend != NULL) {
1377                        dvb_attach(tda18271_attach, fe0->dvb.frontend,
1378                                0x60, &dev->i2c_bus[1].i2c_adap,
1379                                &hauppauge_hvr1210_tuner_config);
1380                }
1381                break;
1382        case CX23885_BOARD_HAUPPAUGE_HVR1400:
1383                i2c_bus = &dev->i2c_bus[0];
1384
1385                if (!dvb_attach(dib7000p_attach, &dib7000p_ops))
1386                        return -ENODEV;
1387
1388                fe0->dvb.frontend = dib7000p_ops.init(&i2c_bus->i2c_adap,
1389                        0x12, &hauppauge_hvr1400_dib7000_config);
1390                if (fe0->dvb.frontend != NULL) {
1391                        struct dvb_frontend *fe;
1392                        struct xc2028_config cfg = {
1393                                .i2c_adap  = &dev->i2c_bus[1].i2c_adap,
1394                                .i2c_addr  = 0x64,
1395                        };
1396                        static struct xc2028_ctrl ctl = {
1397                                .fname   = XC3028L_DEFAULT_FIRMWARE,
1398                                .max_len = 64,
1399                                .demod   = XC3028_FE_DIBCOM52,
1400                                /* This is true for all demods with
1401                                        v36 firmware? */
1402                                .type    = XC2028_D2633,
1403                        };
1404
1405                        fe = dvb_attach(xc2028_attach,
1406                                        fe0->dvb.frontend, &cfg);
1407                        if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
1408                                fe->ops.tuner_ops.set_config(fe, &ctl);
1409                }
1410                break;
1411        case CX23885_BOARD_DVICO_FUSIONHDTV_7_DUAL_EXP:
1412                i2c_bus = &dev->i2c_bus[port->nr - 1];
1413
1414                fe0->dvb.frontend = dvb_attach(s5h1409_attach,
1415                                                &dvico_s5h1409_config,
1416                                                &i2c_bus->i2c_adap);
1417                if (fe0->dvb.frontend == NULL)
1418                        fe0->dvb.frontend = dvb_attach(s5h1411_attach,
1419                                                        &dvico_s5h1411_config,
1420                                                        &i2c_bus->i2c_adap);
1421                if (fe0->dvb.frontend != NULL)
1422                        dvb_attach(xc5000_attach, fe0->dvb.frontend,
1423                                   &i2c_bus->i2c_adap,
1424                                   &dvico_xc5000_tunerconfig);
1425                break;
1426        case CX23885_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL_EXP: {
1427                i2c_bus = &dev->i2c_bus[port->nr - 1];
1428
1429                fe0->dvb.frontend = dvb_attach(zl10353_attach,
1430                                               &dvico_fusionhdtv_xc3028,
1431                                               &i2c_bus->i2c_adap);
1432                if (fe0->dvb.frontend != NULL) {
1433                        struct dvb_frontend      *fe;
1434                        struct xc2028_config      cfg = {
1435                                .i2c_adap  = &i2c_bus->i2c_adap,
1436                                .i2c_addr  = 0x61,
1437                        };
1438                        static struct xc2028_ctrl ctl = {
1439                                .fname       = XC2028_DEFAULT_FIRMWARE,
1440                                .max_len     = 64,
1441                                .demod       = XC3028_FE_ZARLINK456,
1442                        };
1443
1444                        fe = dvb_attach(xc2028_attach, fe0->dvb.frontend,
1445                                        &cfg);
1446                        if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
1447                                fe->ops.tuner_ops.set_config(fe, &ctl);
1448                }
1449                break;
1450        }
1451        case CX23885_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL_EXP2: {
1452                i2c_bus = &dev->i2c_bus[port->nr - 1];
1453                /* cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0); */
1454                /* cxusb_bluebird_gpio_pulse(adap->dev, 0x02, 1); */
1455
1456                if (!dvb_attach(dib7000p_attach, &dib7000p_ops))
1457                        return -ENODEV;
1458
1459                if (dib7000p_ops.i2c_enumeration(&i2c_bus->i2c_adap, 1, 0x12, &dib7070p_dib7000p_config) < 0) {
1460                        pr_warn("Unable to enumerate dib7000p\n");
1461                        return -ENODEV;
1462                }
1463                fe0->dvb.frontend = dib7000p_ops.init(&i2c_bus->i2c_adap, 0x80, &dib7070p_dib7000p_config);
1464                if (fe0->dvb.frontend != NULL) {
1465                        struct i2c_adapter *tun_i2c;
1466
1467                        fe0->dvb.frontend->sec_priv = kmemdup(&dib7000p_ops, sizeof(dib7000p_ops), GFP_KERNEL);
1468                        if (!fe0->dvb.frontend->sec_priv)
1469                                return -ENOMEM;
1470                        tun_i2c = dib7000p_ops.get_i2c_master(fe0->dvb.frontend, DIBX000_I2C_INTERFACE_TUNER, 1);
1471                        if (!dvb_attach(dib0070_attach, fe0->dvb.frontend, tun_i2c, &dib7070p_dib0070_config))
1472                                return -ENODEV;
1473                }
1474                break;
1475        }
1476        case CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H:
1477        case CX23885_BOARD_COMPRO_VIDEOMATE_E650F:
1478        case CX23885_BOARD_COMPRO_VIDEOMATE_E800:
1479                i2c_bus = &dev->i2c_bus[0];
1480
1481                fe0->dvb.frontend = dvb_attach(zl10353_attach,
1482                        &dvico_fusionhdtv_xc3028,
1483                        &i2c_bus->i2c_adap);
1484                if (fe0->dvb.frontend != NULL) {
1485                        struct dvb_frontend      *fe;
1486                        struct xc2028_config      cfg = {
1487                                .i2c_adap  = &dev->i2c_bus[1].i2c_adap,
1488                                .i2c_addr  = 0x61,
1489                        };
1490                        static struct xc2028_ctrl ctl = {
1491                                .fname       = XC2028_DEFAULT_FIRMWARE,
1492                                .max_len     = 64,
1493                                .demod       = XC3028_FE_ZARLINK456,
1494                        };
1495
1496                        fe = dvb_attach(xc2028_attach, fe0->dvb.frontend,
1497                                &cfg);
1498                        if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
1499                                fe->ops.tuner_ops.set_config(fe, &ctl);
1500                }
1501                break;
1502        case CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H_XC4000:
1503                i2c_bus = &dev->i2c_bus[0];
1504
1505                fe0->dvb.frontend = dvb_attach(zl10353_attach,
1506                                               &dvico_fusionhdtv_xc3028,
1507                                               &i2c_bus->i2c_adap);
1508                if (fe0->dvb.frontend != NULL) {
1509                        struct dvb_frontend     *fe;
1510                        struct xc4000_config    cfg = {
1511                                .i2c_address      = 0x61,
1512                                .default_pm       = 0,
1513                                .dvb_amplitude    = 134,
1514                                .set_smoothedcvbs = 1,
1515                                .if_khz           = 4560
1516                        };
1517
1518                        fe = dvb_attach(xc4000_attach, fe0->dvb.frontend,
1519                                        &dev->i2c_bus[1].i2c_adap, &cfg);
1520                        if (!fe) {
1521                                pr_err("%s/2: xc4000 attach failed\n",
1522                                       dev->name);
1523                                goto frontend_detach;
1524                        }
1525                }
1526                break;
1527        case CX23885_BOARD_TBS_6920:
1528                i2c_bus = &dev->i2c_bus[1];
1529
1530                fe0->dvb.frontend = dvb_attach(cx24116_attach,
1531                                        &tbs_cx24116_config,
1532                                        &i2c_bus->i2c_adap);
1533                if (fe0->dvb.frontend != NULL)
1534                        fe0->dvb.frontend->ops.set_voltage = f300_set_voltage;
1535
1536                break;
1537        case CX23885_BOARD_TBS_6980:
1538        case CX23885_BOARD_TBS_6981:
1539                i2c_bus = &dev->i2c_bus[1];
1540
1541                switch (port->nr) {
1542                /* PORT B */
1543                case 1:
1544                        fe0->dvb.frontend = dvb_attach(cx24117_attach,
1545                                        &tbs_cx24117_config,
1546                                        &i2c_bus->i2c_adap);
1547                        break;
1548                /* PORT C */
1549                case 2:
1550                        fe0->dvb.frontend = dvb_attach(cx24117_attach,
1551                                        &tbs_cx24117_config,
1552                                        &i2c_bus->i2c_adap);
1553                        break;
1554                }
1555                break;
1556        case CX23885_BOARD_TEVII_S470:
1557                i2c_bus = &dev->i2c_bus[1];
1558
1559                fe0->dvb.frontend = dvb_attach(ds3000_attach,
1560                                        &tevii_ds3000_config,
1561                                        &i2c_bus->i2c_adap);
1562                if (fe0->dvb.frontend != NULL) {
1563                        dvb_attach(ts2020_attach, fe0->dvb.frontend,
1564                                &tevii_ts2020_config, &i2c_bus->i2c_adap);
1565                        fe0->dvb.frontend->ops.set_voltage = f300_set_voltage;
1566                }
1567
1568                break;
1569        case CX23885_BOARD_DVBWORLD_2005:
1570                i2c_bus = &dev->i2c_bus[1];
1571
1572                fe0->dvb.frontend = dvb_attach(cx24116_attach,
1573                        &dvbworld_cx24116_config,
1574                        &i2c_bus->i2c_adap);
1575                break;
1576        case CX23885_BOARD_NETUP_DUAL_DVBS2_CI:
1577                i2c_bus = &dev->i2c_bus[0];
1578                switch (port->nr) {
1579                /* port B */
1580                case 1:
1581                        fe0->dvb.frontend = dvb_attach(stv0900_attach,
1582                                                        &netup_stv0900_config,
1583                                                        &i2c_bus->i2c_adap, 0);
1584                        if (fe0->dvb.frontend != NULL) {
1585                                if (dvb_attach(stv6110_attach,
1586                                                fe0->dvb.frontend,
1587                                                &netup_stv6110_tunerconfig_a,
1588                                                &i2c_bus->i2c_adap)) {
1589                                        if (!dvb_attach(lnbh24_attach,
1590                                                        fe0->dvb.frontend,
1591                                                        &i2c_bus->i2c_adap,
1592                                                        LNBH24_PCL | LNBH24_TTX,
1593                                                        LNBH24_TEN, 0x09))
1594                                                pr_err("No LNBH24 found!\n");
1595
1596                                }
1597                        }
1598                        break;
1599                /* port C */
1600                case 2:
1601                        fe0->dvb.frontend = dvb_attach(stv0900_attach,
1602                                                        &netup_stv0900_config,
1603                                                        &i2c_bus->i2c_adap, 1);
1604                        if (fe0->dvb.frontend != NULL) {
1605                                if (dvb_attach(stv6110_attach,
1606                                                fe0->dvb.frontend,
1607                                                &netup_stv6110_tunerconfig_b,
1608                                                &i2c_bus->i2c_adap)) {
1609                                        if (!dvb_attach(lnbh24_attach,
1610                                                        fe0->dvb.frontend,
1611                                                        &i2c_bus->i2c_adap,
1612                                                        LNBH24_PCL | LNBH24_TTX,
1613                                                        LNBH24_TEN, 0x0a))
1614                                                pr_err("No LNBH24 found!\n");
1615
1616                                }
1617                        }
1618                        break;
1619                }
1620                break;
1621        case CX23885_BOARD_MYGICA_X8506:
1622                i2c_bus = &dev->i2c_bus[0];
1623                i2c_bus2 = &dev->i2c_bus[1];
1624                fe0->dvb.frontend = dvb_attach(lgs8gxx_attach,
1625                        &mygica_x8506_lgs8gl5_config,
1626                        &i2c_bus->i2c_adap);
1627                if (fe0->dvb.frontend == NULL)
1628                        break;
1629                dvb_attach(xc5000_attach, fe0->dvb.frontend,
1630                           &i2c_bus2->i2c_adap, &mygica_x8506_xc5000_config);
1631                cx23885_set_frontend_hook(port, fe0->dvb.frontend);
1632                break;
1633        case CX23885_BOARD_MYGICA_X8507:
1634                i2c_bus = &dev->i2c_bus[0];
1635                i2c_bus2 = &dev->i2c_bus[1];
1636                fe0->dvb.frontend = dvb_attach(mb86a20s_attach,
1637                        &mygica_x8507_mb86a20s_config,
1638                        &i2c_bus->i2c_adap);
1639                if (fe0->dvb.frontend == NULL)
1640                        break;
1641
1642                dvb_attach(xc5000_attach, fe0->dvb.frontend,
1643                           &i2c_bus2->i2c_adap,
1644                           &mygica_x8507_xc5000_config);
1645                cx23885_set_frontend_hook(port, fe0->dvb.frontend);
1646                break;
1647        case CX23885_BOARD_MAGICPRO_PROHDTVE2:
1648                i2c_bus = &dev->i2c_bus[0];
1649                i2c_bus2 = &dev->i2c_bus[1];
1650                fe0->dvb.frontend = dvb_attach(lgs8gxx_attach,
1651                        &magicpro_prohdtve2_lgs8g75_config,
1652                        &i2c_bus->i2c_adap);
1653                if (fe0->dvb.frontend == NULL)
1654                        break;
1655                dvb_attach(xc5000_attach, fe0->dvb.frontend,
1656                           &i2c_bus2->i2c_adap,
1657                           &magicpro_prohdtve2_xc5000_config);
1658                cx23885_set_frontend_hook(port, fe0->dvb.frontend);
1659                break;
1660        case CX23885_BOARD_HAUPPAUGE_HVR1850:
1661                i2c_bus = &dev->i2c_bus[0];
1662                fe0->dvb.frontend = dvb_attach(s5h1411_attach,
1663                        &hcw_s5h1411_config,
1664                        &i2c_bus->i2c_adap);
1665                if (fe0->dvb.frontend == NULL)
1666                        break;
1667                dvb_attach(tda18271_attach, fe0->dvb.frontend,
1668                           0x60, &dev->i2c_bus[0].i2c_adap,
1669                           &hauppauge_tda18271_config);
1670
1671                tda18271_attach(&dev->ts1.analog_fe,
1672                        0x60, &dev->i2c_bus[1].i2c_adap,
1673                        &hauppauge_tda18271_config);
1674
1675                break;
1676        case CX23885_BOARD_HAUPPAUGE_HVR1290:
1677                i2c_bus = &dev->i2c_bus[0];
1678                fe0->dvb.frontend = dvb_attach(s5h1411_attach,
1679                        &hcw_s5h1411_config,
1680                        &i2c_bus->i2c_adap);
1681                if (fe0->dvb.frontend == NULL)
1682                        break;
1683                dvb_attach(tda18271_attach, fe0->dvb.frontend,
1684                           0x60, &dev->i2c_bus[0].i2c_adap,
1685                           &hauppauge_tda18271_config);
1686                break;
1687        case CX23885_BOARD_MYGICA_X8558PRO:
1688                switch (port->nr) {
1689                /* port B */
1690                case 1:
1691                        i2c_bus = &dev->i2c_bus[0];
1692                        fe0->dvb.frontend = dvb_attach(atbm8830_attach,
1693                                &mygica_x8558pro_atbm8830_cfg1,
1694                                &i2c_bus->i2c_adap);
1695                        if (fe0->dvb.frontend == NULL)
1696                                break;
1697                        dvb_attach(max2165_attach, fe0->dvb.frontend,
1698                                   &i2c_bus->i2c_adap,
1699                                   &mygic_x8558pro_max2165_cfg1);
1700                        break;
1701                /* port C */
1702                case 2:
1703                        i2c_bus = &dev->i2c_bus[1];
1704                        fe0->dvb.frontend = dvb_attach(atbm8830_attach,
1705                                &mygica_x8558pro_atbm8830_cfg2,
1706                                &i2c_bus->i2c_adap);
1707                        if (fe0->dvb.frontend == NULL)
1708                                break;
1709                        dvb_attach(max2165_attach, fe0->dvb.frontend,
1710                                   &i2c_bus->i2c_adap,
1711                                   &mygic_x8558pro_max2165_cfg2);
1712                }
1713                break;
1714        case CX23885_BOARD_NETUP_DUAL_DVB_T_C_CI_RF:
1715                if (port->nr > 2)
1716                        return 0;
1717
1718                i2c_bus = &dev->i2c_bus[0];
1719                mfe_shared = 1;/* MFE */
1720                port->frontends.gate = 0;/* not clear for me yet */
1721                /* ports B, C */
1722                /* MFE frontend 1 DVB-T */
1723                fe0->dvb.frontend = dvb_attach(stv0367ter_attach,
1724                                        &netup_stv0367_config[port->nr - 1],
1725                                        &i2c_bus->i2c_adap);
1726                if (fe0->dvb.frontend == NULL)
1727                        break;
1728                if (NULL == dvb_attach(xc5000_attach, fe0->dvb.frontend,
1729                                        &i2c_bus->i2c_adap,
1730                                        &netup_xc5000_config[port->nr - 1]))
1731                        goto frontend_detach;
1732                /* load xc5000 firmware */
1733                fe0->dvb.frontend->ops.tuner_ops.init(fe0->dvb.frontend);
1734
1735                /* MFE frontend 2 */
1736                fe1 = vb2_dvb_get_frontend(&port->frontends, 2);
1737                if (fe1 == NULL)
1738                        goto frontend_detach;
1739                /* DVB-C init */
1740                fe1->dvb.frontend = dvb_attach(stv0367cab_attach,
1741                                        &netup_stv0367_config[port->nr - 1],
1742                                        &i2c_bus->i2c_adap);
1743                if (fe1->dvb.frontend == NULL)
1744                        break;
1745
1746                fe1->dvb.frontend->id = 1;
1747                if (NULL == dvb_attach(xc5000_attach,
1748                                       fe1->dvb.frontend,
1749                                       &i2c_bus->i2c_adap,
1750                                       &netup_xc5000_config[port->nr - 1]))
1751                        goto frontend_detach;
1752                break;
1753        case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
1754                i2c_bus = &dev->i2c_bus[0];
1755                i2c_bus2 = &dev->i2c_bus[1];
1756
1757                switch (port->nr) {
1758                /* port b */
1759                case 1:
1760                        fe0->dvb.frontend = dvb_attach(drxk_attach,
1761                                        &terratec_drxk_config[0],
1762                                        &i2c_bus->i2c_adap);
1763                        if (fe0->dvb.frontend == NULL)
1764                                break;
1765                        if (!dvb_attach(mt2063_attach,
1766                                        fe0->dvb.frontend,
1767                                        &terratec_mt2063_config[0],
1768                                        &i2c_bus2->i2c_adap))
1769                                goto frontend_detach;
1770                        break;
1771                /* port c */
1772                case 2:
1773                        fe0->dvb.frontend = dvb_attach(drxk_attach,
1774                                        &terratec_drxk_config[1],
1775                                        &i2c_bus->i2c_adap);
1776                        if (fe0->dvb.frontend == NULL)
1777                                break;
1778                        if (!dvb_attach(mt2063_attach,
1779                                        fe0->dvb.frontend,
1780                                        &terratec_mt2063_config[1],
1781                                        &i2c_bus2->i2c_adap))
1782                                goto frontend_detach;
1783                        break;
1784                }
1785                break;
1786        case CX23885_BOARD_TEVII_S471:
1787                i2c_bus = &dev->i2c_bus[1];
1788
1789                fe0->dvb.frontend = dvb_attach(ds3000_attach,
1790                                        &tevii_ds3000_config,
1791                                        &i2c_bus->i2c_adap);
1792                if (fe0->dvb.frontend == NULL)
1793                        break;
1794                dvb_attach(ts2020_attach, fe0->dvb.frontend,
1795                           &tevii_ts2020_config, &i2c_bus->i2c_adap);
1796                break;
1797        case CX23885_BOARD_PROF_8000:
1798                i2c_bus = &dev->i2c_bus[0];
1799
1800                fe0->dvb.frontend = dvb_attach(stv090x_attach,
1801                                                &prof_8000_stv090x_config,
1802                                                &i2c_bus->i2c_adap,
1803                                                STV090x_DEMODULATOR_0);
1804                if (fe0->dvb.frontend == NULL)
1805                        break;
1806                if (!dvb_attach(stb6100_attach,
1807                                fe0->dvb.frontend,
1808                                &prof_8000_stb6100_config,
1809                                &i2c_bus->i2c_adap))
1810                        goto frontend_detach;
1811
1812                fe0->dvb.frontend->ops.set_voltage = p8000_set_voltage;
1813                break;
1814        case CX23885_BOARD_HAUPPAUGE_HVR4400: {
1815                struct tda10071_platform_data tda10071_pdata = hauppauge_tda10071_pdata;
1816                struct a8293_platform_data a8293_pdata = {};
1817
1818                i2c_bus = &dev->i2c_bus[0];
1819                i2c_bus2 = &dev->i2c_bus[1];
1820                switch (port->nr) {
1821                /* port b */
1822                case 1:
1823                        /* attach demod + tuner combo */
1824                        memset(&info, 0, sizeof(info));
1825                        strscpy(info.type, "tda10071_cx24118", I2C_NAME_SIZE);
1826                        info.addr = 0x05;
1827                        info.platform_data = &tda10071_pdata;
1828                        request_module("tda10071");
1829                        client_demod = i2c_new_device(&i2c_bus->i2c_adap, &info);
1830                        if (!client_demod || !client_demod->dev.driver)
1831                                goto frontend_detach;
1832                        if (!try_module_get(client_demod->dev.driver->owner)) {
1833                                i2c_unregister_device(client_demod);
1834                                goto frontend_detach;
1835                        }
1836                        fe0->dvb.frontend = tda10071_pdata.get_dvb_frontend(client_demod);
1837                        port->i2c_client_demod = client_demod;
1838
1839                        /* attach SEC */
1840                        a8293_pdata.dvb_frontend = fe0->dvb.frontend;
1841                        memset(&info, 0, sizeof(info));
1842                        strscpy(info.type, "a8293", I2C_NAME_SIZE);
1843                        info.addr = 0x0b;
1844                        info.platform_data = &a8293_pdata;
1845                        request_module("a8293");
1846                        client_sec = i2c_new_device(&i2c_bus->i2c_adap, &info);
1847                        if (!client_sec || !client_sec->dev.driver)
1848                                goto frontend_detach;
1849                        if (!try_module_get(client_sec->dev.driver->owner)) {
1850                                i2c_unregister_device(client_sec);
1851                                goto frontend_detach;
1852                        }
1853                        port->i2c_client_sec = client_sec;
1854                        break;
1855                /* port c */
1856                case 2:
1857                        /* attach frontend */
1858                        memset(&si2165_pdata, 0, sizeof(si2165_pdata));
1859                        si2165_pdata.fe = &fe0->dvb.frontend;
1860                        si2165_pdata.chip_mode = SI2165_MODE_PLL_XTAL;
1861                        si2165_pdata.ref_freq_hz = 16000000;
1862                        memset(&info, 0, sizeof(struct i2c_board_info));
1863                        strscpy(info.type, "si2165", I2C_NAME_SIZE);
1864                        info.addr = 0x64;
1865                        info.platform_data = &si2165_pdata;
1866                        request_module(info.type);
1867                        client_demod = i2c_new_device(&i2c_bus->i2c_adap, &info);
1868                        if (client_demod == NULL ||
1869                                        client_demod->dev.driver == NULL)
1870                                goto frontend_detach;
1871                        if (!try_module_get(client_demod->dev.driver->owner)) {
1872                                i2c_unregister_device(client_demod);
1873                                goto frontend_detach;
1874                        }
1875                        port->i2c_client_demod = client_demod;
1876
1877                        if (fe0->dvb.frontend == NULL)
1878                                break;
1879                        fe0->dvb.frontend->ops.i2c_gate_ctrl = NULL;
1880                        if (!dvb_attach(tda18271_attach,
1881                                        fe0->dvb.frontend,
1882                                        0x60, &i2c_bus2->i2c_adap,
1883                                  &hauppauge_hvr4400_tuner_config))
1884                                goto frontend_detach;
1885                        break;
1886                }
1887                break;
1888        }
1889        case CX23885_BOARD_HAUPPAUGE_STARBURST: {
1890                struct tda10071_platform_data tda10071_pdata = hauppauge_tda10071_pdata;
1891                struct a8293_platform_data a8293_pdata = {};
1892
1893                i2c_bus = &dev->i2c_bus[0];
1894
1895                /* attach demod + tuner combo */
1896                memset(&info, 0, sizeof(info));
1897                strscpy(info.type, "tda10071_cx24118", I2C_NAME_SIZE);
1898                info.addr = 0x05;
1899                info.platform_data = &tda10071_pdata;
1900                request_module("tda10071");
1901                client_demod = i2c_new_device(&i2c_bus->i2c_adap, &info);
1902                if (!client_demod || !client_demod->dev.driver)
1903                        goto frontend_detach;
1904                if (!try_module_get(client_demod->dev.driver->owner)) {
1905                        i2c_unregister_device(client_demod);
1906                        goto frontend_detach;
1907                }
1908                fe0->dvb.frontend = tda10071_pdata.get_dvb_frontend(client_demod);
1909                port->i2c_client_demod = client_demod;
1910
1911                /* attach SEC */
1912                a8293_pdata.dvb_frontend = fe0->dvb.frontend;
1913                memset(&info, 0, sizeof(info));
1914                strscpy(info.type, "a8293", I2C_NAME_SIZE);
1915                info.addr = 0x0b;
1916                info.platform_data = &a8293_pdata;
1917                request_module("a8293");
1918                client_sec = i2c_new_device(&i2c_bus->i2c_adap, &info);
1919                if (!client_sec || !client_sec->dev.driver)
1920                        goto frontend_detach;
1921                if (!try_module_get(client_sec->dev.driver->owner)) {
1922                        i2c_unregister_device(client_sec);
1923                        goto frontend_detach;
1924                }
1925                port->i2c_client_sec = client_sec;
1926                break;
1927        }
1928        case CX23885_BOARD_DVBSKY_T9580:
1929        case CX23885_BOARD_DVBSKY_S950:
1930                i2c_bus = &dev->i2c_bus[0];
1931                i2c_bus2 = &dev->i2c_bus[1];
1932                switch (port->nr) {
1933                /* port b - satellite */
1934                case 1:
1935                        /* attach frontend */
1936                        fe0->dvb.frontend = dvb_attach(m88ds3103_attach,
1937                                        &dvbsky_t9580_m88ds3103_config,
1938                                        &i2c_bus2->i2c_adap, &adapter);
1939                        if (fe0->dvb.frontend == NULL)
1940                                break;
1941
1942                        /* attach tuner */
1943                        memset(&ts2020_config, 0, sizeof(ts2020_config));
1944                        ts2020_config.fe = fe0->dvb.frontend;
1945                        ts2020_config.get_agc_pwm = m88ds3103_get_agc_pwm;
1946                        memset(&info, 0, sizeof(struct i2c_board_info));
1947                        strscpy(info.type, "ts2020", I2C_NAME_SIZE);
1948                        info.addr = 0x60;
1949                        info.platform_data = &ts2020_config;
1950                        request_module(info.type);
1951                        client_tuner = i2c_new_device(adapter, &info);
1952                        if (client_tuner == NULL ||
1953                                        client_tuner->dev.driver == NULL)
1954                                goto frontend_detach;
1955                        if (!try_module_get(client_tuner->dev.driver->owner)) {
1956                                i2c_unregister_device(client_tuner);
1957                                goto frontend_detach;
1958                        }
1959
1960                        /* delegate signal strength measurement to tuner */
1961                        fe0->dvb.frontend->ops.read_signal_strength =
1962                                fe0->dvb.frontend->ops.tuner_ops.get_rf_strength;
1963
1964                        /*
1965                         * for setting the voltage we need to set GPIOs on
1966                         * the card.
1967                         */
1968                        port->fe_set_voltage =
1969                                fe0->dvb.frontend->ops.set_voltage;
1970                        fe0->dvb.frontend->ops.set_voltage =
1971                                dvbsky_t9580_set_voltage;
1972
1973                        port->i2c_client_tuner = client_tuner;
1974
1975                        break;
1976                /* port c - terrestrial/cable */
1977                case 2:
1978                        /* attach frontend */
1979                        memset(&si2168_config, 0, sizeof(si2168_config));
1980                        si2168_config.i2c_adapter = &adapter;
1981                        si2168_config.fe = &fe0->dvb.frontend;
1982                        si2168_config.ts_mode = SI2168_TS_SERIAL;
1983                        memset(&info, 0, sizeof(struct i2c_board_info));
1984                        strscpy(info.type, "si2168", I2C_NAME_SIZE);
1985                        info.addr = 0x64;
1986                        info.platform_data = &si2168_config;
1987                        request_module(info.type);
1988                        client_demod = i2c_new_device(&i2c_bus->i2c_adap, &info);
1989                        if (client_demod == NULL ||
1990                                        client_demod->dev.driver == NULL)
1991                                goto frontend_detach;
1992                        if (!try_module_get(client_demod->dev.driver->owner)) {
1993                                i2c_unregister_device(client_demod);
1994                                goto frontend_detach;
1995                        }
1996                        port->i2c_client_demod = client_demod;
1997
1998                        /* attach tuner */
1999                        memset(&si2157_config, 0, sizeof(si2157_config));
2000                        si2157_config.fe = fe0->dvb.frontend;
2001                        si2157_config.if_port = 1;
2002                        memset(&info, 0, sizeof(struct i2c_board_info));
2003                        strscpy(info.type, "si2157", I2C_NAME_SIZE);
2004                        info.addr = 0x60;
2005                        info.platform_data = &si2157_config;
2006                        request_module(info.type);
2007                        client_tuner = i2c_new_device(adapter, &info);
2008                        if (client_tuner == NULL ||
2009                                        client_tuner->dev.driver == NULL)
2010                                goto frontend_detach;
2011
2012                        if (!try_module_get(client_tuner->dev.driver->owner)) {
2013                                i2c_unregister_device(client_tuner);
2014                                goto frontend_detach;
2015                        }
2016                        port->i2c_client_tuner = client_tuner;
2017                        break;
2018                }
2019                break;
2020        case CX23885_BOARD_DVBSKY_T980C:
2021        case CX23885_BOARD_TT_CT2_4500_CI:
2022                i2c_bus = &dev->i2c_bus[0];
2023                i2c_bus2 = &dev->i2c_bus[1];
2024
2025                /* attach frontend */
2026                memset(&si2168_config, 0, sizeof(si2168_config));
2027                si2168_config.i2c_adapter = &adapter;
2028                si2168_config.fe = &fe0->dvb.frontend;
2029                si2168_config.ts_mode = SI2168_TS_PARALLEL;
2030                memset(&info, 0, sizeof(struct i2c_board_info));
2031                strscpy(info.type, "si2168", I2C_NAME_SIZE);
2032                info.addr = 0x64;
2033                info.platform_data = &si2168_config;
2034                request_module(info.type);
2035                client_demod = i2c_new_device(&i2c_bus2->i2c_adap, &info);
2036                if (client_demod == NULL || client_demod->dev.driver == NULL)
2037                        goto frontend_detach;
2038                if (!try_module_get(client_demod->dev.driver->owner)) {
2039                        i2c_unregister_device(client_demod);
2040                        goto frontend_detach;
2041                }
2042                port->i2c_client_demod = client_demod;
2043
2044                /* attach tuner */
2045                memset(&si2157_config, 0, sizeof(si2157_config));
2046                si2157_config.fe = fe0->dvb.frontend;
2047                si2157_config.if_port = 1;
2048                memset(&info, 0, sizeof(struct i2c_board_info));
2049                strscpy(info.type, "si2157", I2C_NAME_SIZE);
2050                info.addr = 0x60;
2051                info.platform_data = &si2157_config;
2052                request_module(info.type);
2053                client_tuner = i2c_new_device(adapter, &info);
2054                if (client_tuner == NULL ||
2055                                client_tuner->dev.driver == NULL)
2056                        goto frontend_detach;
2057                if (!try_module_get(client_tuner->dev.driver->owner)) {
2058                        i2c_unregister_device(client_tuner);
2059                        goto frontend_detach;
2060                }
2061                port->i2c_client_tuner = client_tuner;
2062                break;
2063        case CX23885_BOARD_DVBSKY_S950C:
2064                i2c_bus = &dev->i2c_bus[0];
2065                i2c_bus2 = &dev->i2c_bus[1];
2066
2067                /* attach frontend */
2068                fe0->dvb.frontend = dvb_attach(m88ds3103_attach,
2069                                &dvbsky_s950c_m88ds3103_config,
2070                                &i2c_bus2->i2c_adap, &adapter);
2071                if (fe0->dvb.frontend == NULL)
2072                        break;
2073
2074                /* attach tuner */
2075                memset(&ts2020_config, 0, sizeof(ts2020_config));
2076                ts2020_config.fe = fe0->dvb.frontend;
2077                ts2020_config.get_agc_pwm = m88ds3103_get_agc_pwm;
2078                memset(&info, 0, sizeof(struct i2c_board_info));
2079                strscpy(info.type, "ts2020", I2C_NAME_SIZE);
2080                info.addr = 0x60;
2081                info.platform_data = &ts2020_config;
2082                request_module(info.type);
2083                client_tuner = i2c_new_device(adapter, &info);
2084                if (client_tuner == NULL || client_tuner->dev.driver == NULL)
2085                        goto frontend_detach;
2086                if (!try_module_get(client_tuner->dev.driver->owner)) {
2087                        i2c_unregister_device(client_tuner);
2088                        goto frontend_detach;
2089                }
2090
2091                /* delegate signal strength measurement to tuner */
2092                fe0->dvb.frontend->ops.read_signal_strength =
2093                        fe0->dvb.frontend->ops.tuner_ops.get_rf_strength;
2094
2095                port->i2c_client_tuner = client_tuner;
2096                break;
2097        case CX23885_BOARD_DVBSKY_S952:
2098                /* attach frontend */
2099                memset(&m88ds3103_pdata, 0, sizeof(m88ds3103_pdata));
2100                m88ds3103_pdata.clk = 27000000;
2101                m88ds3103_pdata.i2c_wr_max = 33;
2102                m88ds3103_pdata.agc = 0x99;
2103                m88ds3103_pdata.clk_out = M88DS3103_CLOCK_OUT_DISABLED;
2104                m88ds3103_pdata.lnb_en_pol = 1;
2105
2106                switch (port->nr) {
2107                /* port b */
2108                case 1:
2109                        i2c_bus = &dev->i2c_bus[1];
2110                        m88ds3103_pdata.ts_mode = M88DS3103_TS_PARALLEL;
2111                        m88ds3103_pdata.ts_clk = 16000;
2112                        m88ds3103_pdata.ts_clk_pol = 1;
2113                        p_set_voltage = dvbsky_t9580_set_voltage;
2114                        break;
2115                /* port c */
2116                case 2:
2117                        i2c_bus = &dev->i2c_bus[0];
2118                        m88ds3103_pdata.ts_mode = M88DS3103_TS_SERIAL;
2119                        m88ds3103_pdata.ts_clk = 96000;
2120                        m88ds3103_pdata.ts_clk_pol = 0;
2121                        p_set_voltage = dvbsky_s952_portc_set_voltage;
2122                        break;
2123                default:
2124                        return 0;
2125                }
2126
2127                memset(&info, 0, sizeof(info));
2128                strscpy(info.type, "m88ds3103", I2C_NAME_SIZE);
2129                info.addr = 0x68;
2130                info.platform_data = &m88ds3103_pdata;
2131                request_module(info.type);
2132                client_demod = i2c_new_device(&i2c_bus->i2c_adap, &info);
2133                if (client_demod == NULL || client_demod->dev.driver == NULL)
2134                        goto frontend_detach;
2135                if (!try_module_get(client_demod->dev.driver->owner)) {
2136                        i2c_unregister_device(client_demod);
2137                        goto frontend_detach;
2138                }
2139                port->i2c_client_demod = client_demod;
2140                adapter = m88ds3103_pdata.get_i2c_adapter(client_demod);
2141                fe0->dvb.frontend = m88ds3103_pdata.get_dvb_frontend(client_demod);
2142
2143                /* attach tuner */
2144                memset(&ts2020_config, 0, sizeof(ts2020_config));
2145                ts2020_config.fe = fe0->dvb.frontend;
2146                ts2020_config.get_agc_pwm = m88ds3103_get_agc_pwm;
2147                memset(&info, 0, sizeof(struct i2c_board_info));
2148                strscpy(info.type, "ts2020", I2C_NAME_SIZE);
2149                info.addr = 0x60;
2150                info.platform_data = &ts2020_config;
2151                request_module(info.type);
2152                client_tuner = i2c_new_device(adapter, &info);
2153                if (client_tuner == NULL || client_tuner->dev.driver == NULL)
2154                        goto frontend_detach;
2155                if (!try_module_get(client_tuner->dev.driver->owner)) {
2156                        i2c_unregister_device(client_tuner);
2157                        goto frontend_detach;
2158                }
2159
2160                /* delegate signal strength measurement to tuner */
2161                fe0->dvb.frontend->ops.read_signal_strength =
2162                        fe0->dvb.frontend->ops.tuner_ops.get_rf_strength;
2163
2164                /*
2165                 * for setting the voltage we need to set GPIOs on
2166                 * the card.
2167                 */
2168                port->fe_set_voltage =
2169                        fe0->dvb.frontend->ops.set_voltage;
2170                fe0->dvb.frontend->ops.set_voltage = p_set_voltage;
2171
2172                port->i2c_client_tuner = client_tuner;
2173                break;
2174        case CX23885_BOARD_DVBSKY_T982:
2175                memset(&si2168_config, 0, sizeof(si2168_config));
2176                switch (port->nr) {
2177                /* port b */
2178                case 1:
2179                        i2c_bus = &dev->i2c_bus[1];
2180                        si2168_config.ts_mode = SI2168_TS_PARALLEL;
2181                        break;
2182                /* port c */
2183                case 2:
2184                        i2c_bus = &dev->i2c_bus[0];
2185                        si2168_config.ts_mode = SI2168_TS_SERIAL;
2186                        break;
2187                }
2188
2189                /* attach frontend */
2190                si2168_config.i2c_adapter = &adapter;
2191                si2168_config.fe = &fe0->dvb.frontend;
2192                memset(&info, 0, sizeof(struct i2c_board_info));
2193                strscpy(info.type, "si2168", I2C_NAME_SIZE);
2194                info.addr = 0x64;
2195                info.platform_data = &si2168_config;
2196                request_module(info.type);
2197                client_demod = i2c_new_device(&i2c_bus->i2c_adap, &info);
2198                if (client_demod == NULL || client_demod->dev.driver == NULL)
2199                        goto frontend_detach;
2200                if (!try_module_get(client_demod->dev.driver->owner)) {
2201                        i2c_unregister_device(client_demod);
2202                        goto frontend_detach;
2203                }
2204                port->i2c_client_demod = client_demod;
2205
2206                /* attach tuner */
2207                memset(&si2157_config, 0, sizeof(si2157_config));
2208                si2157_config.fe = fe0->dvb.frontend;
2209                si2157_config.if_port = 1;
2210                memset(&info, 0, sizeof(struct i2c_board_info));
2211                strscpy(info.type, "si2157", I2C_NAME_SIZE);
2212                info.addr = 0x60;
2213                info.platform_data = &si2157_config;
2214                request_module(info.type);
2215                client_tuner = i2c_new_device(adapter, &info);
2216                if (client_tuner == NULL ||
2217                                client_tuner->dev.driver == NULL)
2218                        goto frontend_detach;
2219                if (!try_module_get(client_tuner->dev.driver->owner)) {
2220                        i2c_unregister_device(client_tuner);
2221                        goto frontend_detach;
2222                }
2223                port->i2c_client_tuner = client_tuner;
2224                break;
2225        case CX23885_BOARD_HAUPPAUGE_STARBURST2:
2226        case CX23885_BOARD_HAUPPAUGE_HVR5525:
2227                i2c_bus = &dev->i2c_bus[0];
2228                i2c_bus2 = &dev->i2c_bus[1];
2229
2230                switch (port->nr) {
2231
2232                /* port b - satellite */
2233                case 1:
2234                        /* attach frontend */
2235                        fe0->dvb.frontend = dvb_attach(m88ds3103_attach,
2236                                        &hauppauge_hvr5525_m88ds3103_config,
2237                                        &i2c_bus->i2c_adap, &adapter);
2238                        if (fe0->dvb.frontend == NULL)
2239                                break;
2240
2241                        /* attach SEC */
2242                        a8293_pdata.dvb_frontend = fe0->dvb.frontend;
2243                        memset(&info, 0, sizeof(info));
2244                        strscpy(info.type, "a8293", I2C_NAME_SIZE);
2245                        info.addr = 0x0b;
2246                        info.platform_data = &a8293_pdata;
2247                        request_module("a8293");
2248                        client_sec = i2c_new_device(&i2c_bus->i2c_adap, &info);
2249                        if (!client_sec || !client_sec->dev.driver)
2250                                goto frontend_detach;
2251                        if (!try_module_get(client_sec->dev.driver->owner)) {
2252                                i2c_unregister_device(client_sec);
2253                                goto frontend_detach;
2254                        }
2255                        port->i2c_client_sec = client_sec;
2256
2257                        /* attach tuner */
2258                        memset(&m88rs6000t_config, 0, sizeof(m88rs6000t_config));
2259                        m88rs6000t_config.fe = fe0->dvb.frontend;
2260                        memset(&info, 0, sizeof(struct i2c_board_info));
2261                        strscpy(info.type, "m88rs6000t", I2C_NAME_SIZE);
2262                        info.addr = 0x21;
2263                        info.platform_data = &m88rs6000t_config;
2264                        request_module("%s", info.type);
2265                        client_tuner = i2c_new_device(adapter, &info);
2266                        if (!client_tuner || !client_tuner->dev.driver)
2267                                goto frontend_detach;
2268                        if (!try_module_get(client_tuner->dev.driver->owner)) {
2269                                i2c_unregister_device(client_tuner);
2270                                goto frontend_detach;
2271                        }
2272                        port->i2c_client_tuner = client_tuner;
2273
2274                        /* delegate signal strength measurement to tuner */
2275                        fe0->dvb.frontend->ops.read_signal_strength =
2276                                fe0->dvb.frontend->ops.tuner_ops.get_rf_strength;
2277                        break;
2278                /* port c - terrestrial/cable */
2279                case 2:
2280                        /* attach frontend */
2281                        memset(&si2168_config, 0, sizeof(si2168_config));
2282                        si2168_config.i2c_adapter = &adapter;
2283                        si2168_config.fe = &fe0->dvb.frontend;
2284                        si2168_config.ts_mode = SI2168_TS_SERIAL;
2285                        memset(&info, 0, sizeof(struct i2c_board_info));
2286                        strscpy(info.type, "si2168", I2C_NAME_SIZE);
2287                        info.addr = 0x64;
2288                        info.platform_data = &si2168_config;
2289                        request_module("%s", info.type);
2290                        client_demod = i2c_new_device(&i2c_bus->i2c_adap, &info);
2291                        if (!client_demod || !client_demod->dev.driver)
2292                                goto frontend_detach;
2293                        if (!try_module_get(client_demod->dev.driver->owner)) {
2294                                i2c_unregister_device(client_demod);
2295                                goto frontend_detach;
2296                        }
2297                        port->i2c_client_demod = client_demod;
2298
2299                        /* attach tuner */
2300                        memset(&si2157_config, 0, sizeof(si2157_config));
2301                        si2157_config.fe = fe0->dvb.frontend;
2302                        si2157_config.if_port = 1;
2303                        memset(&info, 0, sizeof(struct i2c_board_info));
2304                        strscpy(info.type, "si2157", I2C_NAME_SIZE);
2305                        info.addr = 0x60;
2306                        info.platform_data = &si2157_config;
2307                        request_module("%s", info.type);
2308                        client_tuner = i2c_new_device(&i2c_bus2->i2c_adap, &info);
2309                        if (!client_tuner || !client_tuner->dev.driver) {
2310                                module_put(client_demod->dev.driver->owner);
2311                                i2c_unregister_device(client_demod);
2312                                port->i2c_client_demod = NULL;
2313                                goto frontend_detach;
2314                        }
2315                        if (!try_module_get(client_tuner->dev.driver->owner)) {
2316                                i2c_unregister_device(client_tuner);
2317                                module_put(client_demod->dev.driver->owner);
2318                                i2c_unregister_device(client_demod);
2319                                port->i2c_client_demod = NULL;
2320                                goto frontend_detach;
2321                        }
2322                        port->i2c_client_tuner = client_tuner;
2323                        break;
2324                }
2325                break;
2326        case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
2327        case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB_885:
2328                pr_info("%s(): board=%d port=%d\n", __func__,
2329                        dev->board, port->nr);
2330                switch (port->nr) {
2331                /* port b - Terrestrial/cable */
2332                case 1:
2333                        /* attach frontend */
2334                        memset(&si2168_config, 0, sizeof(si2168_config));
2335                        si2168_config.i2c_adapter = &adapter;
2336                        si2168_config.fe = &fe0->dvb.frontend;
2337                        si2168_config.ts_mode = SI2168_TS_SERIAL;
2338                        memset(&info, 0, sizeof(struct i2c_board_info));
2339                        strscpy(info.type, "si2168", I2C_NAME_SIZE);
2340                        info.addr = 0x64;
2341                        info.platform_data = &si2168_config;
2342                        request_module("%s", info.type);
2343                        client_demod = i2c_new_device(&dev->i2c_bus[0].i2c_adap, &info);
2344                        if (!client_demod || !client_demod->dev.driver)
2345                                goto frontend_detach;
2346                        if (!try_module_get(client_demod->dev.driver->owner)) {
2347                                i2c_unregister_device(client_demod);
2348                                goto frontend_detach;
2349                        }
2350                        port->i2c_client_demod = client_demod;
2351
2352                        /* attach tuner */
2353                        memset(&si2157_config, 0, sizeof(si2157_config));
2354                        si2157_config.fe = fe0->dvb.frontend;
2355                        si2157_config.if_port = 1;
2356                        memset(&info, 0, sizeof(struct i2c_board_info));
2357                        strscpy(info.type, "si2157", I2C_NAME_SIZE);
2358                        info.addr = 0x60;
2359                        info.platform_data = &si2157_config;
2360                        request_module("%s", info.type);
2361                        client_tuner = i2c_new_device(&dev->i2c_bus[1].i2c_adap, &info);
2362                        if (!client_tuner || !client_tuner->dev.driver) {
2363                                module_put(client_demod->dev.driver->owner);
2364                                i2c_unregister_device(client_demod);
2365                                port->i2c_client_demod = NULL;
2366                                goto frontend_detach;
2367                        }
2368                        if (!try_module_get(client_tuner->dev.driver->owner)) {
2369                                i2c_unregister_device(client_tuner);
2370                                module_put(client_demod->dev.driver->owner);
2371                                i2c_unregister_device(client_demod);
2372                                port->i2c_client_demod = NULL;
2373                                goto frontend_detach;
2374                        }
2375                        port->i2c_client_tuner = client_tuner;
2376                        break;
2377
2378                /* port c - terrestrial/cable */
2379                case 2:
2380                        /* attach frontend */
2381                        memset(&si2168_config, 0, sizeof(si2168_config));
2382                        si2168_config.i2c_adapter = &adapter;
2383                        si2168_config.fe = &fe0->dvb.frontend;
2384                        si2168_config.ts_mode = SI2168_TS_SERIAL;
2385                        memset(&info, 0, sizeof(struct i2c_board_info));
2386                        strscpy(info.type, "si2168", I2C_NAME_SIZE);
2387                        info.addr = 0x66;
2388                        info.platform_data = &si2168_config;
2389                        request_module("%s", info.type);
2390                        client_demod = i2c_new_device(&dev->i2c_bus[0].i2c_adap, &info);
2391                        if (!client_demod || !client_demod->dev.driver)
2392                                goto frontend_detach;
2393                        if (!try_module_get(client_demod->dev.driver->owner)) {
2394                                i2c_unregister_device(client_demod);
2395                                goto frontend_detach;
2396                        }
2397                        port->i2c_client_demod = client_demod;
2398
2399                        /* attach tuner */
2400                        memset(&si2157_config, 0, sizeof(si2157_config));
2401                        si2157_config.fe = fe0->dvb.frontend;
2402                        si2157_config.if_port = 1;
2403                        memset(&info, 0, sizeof(struct i2c_board_info));
2404                        strscpy(info.type, "si2157", I2C_NAME_SIZE);
2405                        info.addr = 0x62;
2406                        info.platform_data = &si2157_config;
2407                        request_module("%s", info.type);
2408                        client_tuner = i2c_new_device(&dev->i2c_bus[1].i2c_adap, &info);
2409                        if (!client_tuner || !client_tuner->dev.driver) {
2410                                module_put(client_demod->dev.driver->owner);
2411                                i2c_unregister_device(client_demod);
2412                                port->i2c_client_demod = NULL;
2413                                goto frontend_detach;
2414                        }
2415                        if (!try_module_get(client_tuner->dev.driver->owner)) {
2416                                i2c_unregister_device(client_tuner);
2417                                module_put(client_demod->dev.driver->owner);
2418                                i2c_unregister_device(client_demod);
2419                                port->i2c_client_demod = NULL;
2420                                goto frontend_detach;
2421                        }
2422                        port->i2c_client_tuner = client_tuner;
2423                        break;
2424                }
2425                break;
2426        case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
2427        case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC_885:
2428                pr_info("%s(): board=%d port=%d\n", __func__,
2429                        dev->board, port->nr);
2430                switch (port->nr) {
2431                /* port b - Terrestrial/cable */
2432                case 1:
2433                        /* attach frontend */
2434                        i2c_bus = &dev->i2c_bus[0];
2435                        fe0->dvb.frontend = dvb_attach(lgdt3306a_attach,
2436                                &hauppauge_quadHD_ATSC_a_config, &i2c_bus->i2c_adap);
2437                        if (fe0->dvb.frontend == NULL)
2438                                break;
2439
2440                        /* attach tuner */
2441                        memset(&si2157_config, 0, sizeof(si2157_config));
2442                        si2157_config.fe = fe0->dvb.frontend;
2443                        si2157_config.if_port = 1;
2444                        si2157_config.inversion = 1;
2445                        memset(&info, 0, sizeof(struct i2c_board_info));
2446                        strscpy(info.type, "si2157", I2C_NAME_SIZE);
2447                        info.addr = 0x60;
2448                        info.platform_data = &si2157_config;
2449                        request_module("%s", info.type);
2450                        client_tuner = i2c_new_device(&dev->i2c_bus[1].i2c_adap, &info);
2451                        if (!client_tuner || !client_tuner->dev.driver) {
2452                                module_put(client_demod->dev.driver->owner);
2453                                i2c_unregister_device(client_demod);
2454                                port->i2c_client_demod = NULL;
2455                                goto frontend_detach;
2456                        }
2457                        if (!try_module_get(client_tuner->dev.driver->owner)) {
2458                                i2c_unregister_device(client_tuner);
2459                                module_put(client_demod->dev.driver->owner);
2460                                i2c_unregister_device(client_demod);
2461                                port->i2c_client_demod = NULL;
2462                                goto frontend_detach;
2463                        }
2464                        port->i2c_client_tuner = client_tuner;
2465                        break;
2466
2467                /* port c - terrestrial/cable */
2468                case 2:
2469                        /* attach frontend */
2470                        i2c_bus = &dev->i2c_bus[0];
2471                        fe0->dvb.frontend = dvb_attach(lgdt3306a_attach,
2472                                &hauppauge_quadHD_ATSC_b_config, &i2c_bus->i2c_adap);
2473                        if (fe0->dvb.frontend == NULL)
2474                                break;
2475
2476                        /* attach tuner */
2477                        memset(&si2157_config, 0, sizeof(si2157_config));
2478                        si2157_config.fe = fe0->dvb.frontend;
2479                        si2157_config.if_port = 1;
2480                        si2157_config.inversion = 1;
2481                        memset(&info, 0, sizeof(struct i2c_board_info));
2482                        strscpy(info.type, "si2157", I2C_NAME_SIZE);
2483                        info.addr = 0x62;
2484                        info.platform_data = &si2157_config;
2485                        request_module("%s", info.type);
2486                        client_tuner = i2c_new_device(&dev->i2c_bus[1].i2c_adap, &info);
2487                        if (!client_tuner || !client_tuner->dev.driver) {
2488                                module_put(client_demod->dev.driver->owner);
2489                                i2c_unregister_device(client_demod);
2490                                port->i2c_client_demod = NULL;
2491                                goto frontend_detach;
2492                        }
2493                        if (!try_module_get(client_tuner->dev.driver->owner)) {
2494                                i2c_unregister_device(client_tuner);
2495                                module_put(client_demod->dev.driver->owner);
2496                                i2c_unregister_device(client_demod);
2497                                port->i2c_client_demod = NULL;
2498                                goto frontend_detach;
2499                        }
2500                        port->i2c_client_tuner = client_tuner;
2501                        break;
2502                }
2503                break;
2504        case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
2505                switch (port->nr) {
2506                /* port c - Terrestrial/cable */
2507                case 2:
2508                        /* attach frontend */
2509                        i2c_bus = &dev->i2c_bus[0];
2510                        fe0->dvb.frontend = dvb_attach(lgdt3306a_attach,
2511                                        &hauppauge_hvr1265k4_config,
2512                                        &i2c_bus->i2c_adap);
2513                        if (fe0->dvb.frontend == NULL)
2514                                break;
2515
2516                        /* attach tuner */
2517                        memset(&si2157_config, 0, sizeof(si2157_config));
2518                        si2157_config.fe = fe0->dvb.frontend;
2519                        si2157_config.if_port = 1;
2520                        si2157_config.inversion = 1;
2521                        memset(&info, 0, sizeof(struct i2c_board_info));
2522                        strscpy(info.type, "si2157", I2C_NAME_SIZE);
2523                        info.addr = 0x60;
2524                        info.platform_data = &si2157_config;
2525                        request_module("%s", info.type);
2526                        client_tuner = i2c_new_device(&dev->i2c_bus[1].i2c_adap, &info);
2527                        if (!client_tuner || !client_tuner->dev.driver)
2528                                goto frontend_detach;
2529
2530                        if (!try_module_get(client_tuner->dev.driver->owner)) {
2531                                i2c_unregister_device(client_tuner);
2532                                client_tuner = NULL;
2533                                goto frontend_detach;
2534                        }
2535                        port->i2c_client_tuner = client_tuner;
2536                        break;
2537                }
2538                break;
2539        default:
2540                pr_info("%s: The frontend of your DVB/ATSC card  isn't supported yet\n",
2541                        dev->name);
2542                break;
2543        }
2544
2545        if ((NULL == fe0->dvb.frontend) || (fe1 && NULL == fe1->dvb.frontend)) {
2546                pr_err("%s: frontend initialization failed\n",
2547                       dev->name);
2548                goto frontend_detach;
2549        }
2550
2551        /* define general-purpose callback pointer */
2552        fe0->dvb.frontend->callback = cx23885_tuner_callback;
2553        if (fe1)
2554                fe1->dvb.frontend->callback = cx23885_tuner_callback;
2555#if 0
2556        /* Ensure all frontends negotiate bus access */
2557        fe0->dvb.frontend->ops.ts_bus_ctrl = cx23885_dvb_bus_ctrl;
2558        if (fe1)
2559                fe1->dvb.frontend->ops.ts_bus_ctrl = cx23885_dvb_bus_ctrl;
2560#endif
2561
2562        /* Put the tuner in standby to keep it quiet */
2563        call_all(dev, tuner, standby);
2564
2565        if (fe0->dvb.frontend->ops.analog_ops.standby)
2566                fe0->dvb.frontend->ops.analog_ops.standby(fe0->dvb.frontend);
2567
2568        /* register everything */
2569        ret = vb2_dvb_register_bus(&port->frontends, THIS_MODULE, port,
2570                                   &dev->pci->dev, NULL,
2571                                   adapter_nr, mfe_shared);
2572        if (ret)
2573                goto frontend_detach;
2574
2575        ret = dvb_register_ci_mac(port);
2576        if (ret)
2577                goto frontend_detach;
2578
2579        return 0;
2580
2581frontend_detach:
2582        /* remove I2C client for SEC */
2583        client_sec = port->i2c_client_sec;
2584        if (client_sec) {
2585                module_put(client_sec->dev.driver->owner);
2586                i2c_unregister_device(client_sec);
2587                port->i2c_client_sec = NULL;
2588        }
2589
2590        /* remove I2C client for tuner */
2591        client_tuner = port->i2c_client_tuner;
2592        if (client_tuner) {
2593                module_put(client_tuner->dev.driver->owner);
2594                i2c_unregister_device(client_tuner);
2595                port->i2c_client_tuner = NULL;
2596        }
2597
2598        /* remove I2C client for demodulator */
2599        client_demod = port->i2c_client_demod;
2600        if (client_demod) {
2601                module_put(client_demod->dev.driver->owner);
2602                i2c_unregister_device(client_demod);
2603                port->i2c_client_demod = NULL;
2604        }
2605
2606        port->gate_ctrl = NULL;
2607        vb2_dvb_dealloc_frontends(&port->frontends);
2608        return -EINVAL;
2609}
2610
2611int cx23885_dvb_register(struct cx23885_tsport *port)
2612{
2613
2614        struct vb2_dvb_frontend *fe0;
2615        struct cx23885_dev *dev = port->dev;
2616        int err, i;
2617
2618        /* Here we need to allocate the correct number of frontends,
2619         * as reflected in the cards struct. The reality is that currently
2620         * no cx23885 boards support this - yet. But, if we don't modify this
2621         * code then the second frontend would never be allocated (later)
2622         * and fail with error before the attach in dvb_register().
2623         * Without these changes we risk an OOPS later. The changes here
2624         * are for safety, and should provide a good foundation for the
2625         * future addition of any multi-frontend cx23885 based boards.
2626         */
2627        pr_info("%s() allocating %d frontend(s)\n", __func__,
2628                port->num_frontends);
2629
2630        for (i = 1; i <= port->num_frontends; i++) {
2631                struct vb2_queue *q;
2632
2633                if (vb2_dvb_alloc_frontend(
2634                        &port->frontends, i) == NULL) {
2635                        pr_err("%s() failed to alloc\n", __func__);
2636                        return -ENOMEM;
2637                }
2638
2639                fe0 = vb2_dvb_get_frontend(&port->frontends, i);
2640                if (!fe0)
2641                        return -EINVAL;
2642
2643                dprintk(1, "%s\n", __func__);
2644                dprintk(1, " ->probed by Card=%d Name=%s, PCI %02x:%02x\n",
2645                        dev->board,
2646                        dev->name,
2647                        dev->pci_bus,
2648                        dev->pci_slot);
2649
2650                /* dvb stuff */
2651                /* We have to init the queue for each frontend on a port. */
2652                pr_info("%s: cx23885 based dvb card\n", dev->name);
2653                q = &fe0->dvb.dvbq;
2654                q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2655                q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
2656                q->gfp_flags = GFP_DMA32;
2657                q->min_buffers_needed = 2;
2658                q->drv_priv = port;
2659                q->buf_struct_size = sizeof(struct cx23885_buffer);
2660                q->ops = &dvb_qops;
2661                q->mem_ops = &vb2_dma_sg_memops;
2662                q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2663                q->lock = &dev->lock;
2664                q->dev = &dev->pci->dev;
2665
2666                err = vb2_queue_init(q);
2667                if (err < 0)
2668                        return err;
2669        }
2670        err = dvb_register(port);
2671        if (err != 0)
2672                pr_err("%s() dvb_register failed err = %d\n",
2673                       __func__, err);
2674
2675        return err;
2676}
2677
2678int cx23885_dvb_unregister(struct cx23885_tsport *port)
2679{
2680        struct vb2_dvb_frontend *fe0;
2681        struct i2c_client *client;
2682
2683        fe0 = vb2_dvb_get_frontend(&port->frontends, 1);
2684
2685        if (fe0 && fe0->dvb.frontend)
2686                vb2_dvb_unregister_bus(&port->frontends);
2687
2688        /* remove I2C client for CI */
2689        client = port->i2c_client_ci;
2690        if (client) {
2691                module_put(client->dev.driver->owner);
2692                i2c_unregister_device(client);
2693        }
2694
2695        /* remove I2C client for SEC */
2696        client = port->i2c_client_sec;
2697        if (client) {
2698                module_put(client->dev.driver->owner);
2699                i2c_unregister_device(client);
2700        }
2701
2702        /* remove I2C client for tuner */
2703        client = port->i2c_client_tuner;
2704        if (client) {
2705                module_put(client->dev.driver->owner);
2706                i2c_unregister_device(client);
2707        }
2708
2709        /* remove I2C client for demodulator */
2710        client = port->i2c_client_demod;
2711        if (client) {
2712                module_put(client->dev.driver->owner);
2713                i2c_unregister_device(client);
2714        }
2715
2716        switch (port->dev->board) {
2717        case CX23885_BOARD_NETUP_DUAL_DVBS2_CI:
2718                netup_ci_exit(port);
2719                break;
2720        case CX23885_BOARD_NETUP_DUAL_DVB_T_C_CI_RF:
2721                altera_ci_release(port->dev, port->nr);
2722                break;
2723        }
2724
2725        port->gate_ctrl = NULL;
2726
2727        return 0;
2728}
2729