linux/drivers/media/dvb-frontends/af9013.c
<<
>>
Prefs
   1/*
   2 * Afatech AF9013 demodulator driver
   3 *
   4 * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
   5 * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
   6 *
   7 * Thanks to Afatech who kindly provided information.
   8 *
   9 *    This program is free software; you can redistribute it and/or modify
  10 *    it under the terms of the GNU General Public License as published by
  11 *    the Free Software Foundation; either version 2 of the License, or
  12 *    (at your option) any later version.
  13 *
  14 *    This program is distributed in the hope that it will be useful,
  15 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17 *    GNU General Public License for more details.
  18 *
  19 *    You should have received a copy of the GNU General Public License
  20 *    along with this program; if not, write to the Free Software
  21 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22 *
  23 */
  24
  25#include "af9013_priv.h"
  26
  27/* Max transfer size done by I2C transfer functions */
  28#define MAX_XFER_SIZE  64
  29
  30struct af9013_state {
  31        struct i2c_adapter *i2c;
  32        struct dvb_frontend fe;
  33        struct af9013_config config;
  34
  35        /* tuner/demod RF and IF AGC limits used for signal strength calc */
  36        u8 signal_strength_en, rf_50, rf_80, if_50, if_80;
  37        u16 signal_strength;
  38        u32 ber;
  39        u32 ucblocks;
  40        u16 snr;
  41        u32 bandwidth_hz;
  42        fe_status_t fe_status;
  43        unsigned long set_frontend_jiffies;
  44        unsigned long read_status_jiffies;
  45        bool first_tune;
  46        bool i2c_gate_state;
  47        unsigned int statistics_step:3;
  48        struct delayed_work statistics_work;
  49};
  50
  51/* write multiple registers */
  52static int af9013_wr_regs_i2c(struct af9013_state *priv, u8 mbox, u16 reg,
  53        const u8 *val, int len)
  54{
  55        int ret;
  56        u8 buf[MAX_XFER_SIZE];
  57        struct i2c_msg msg[1] = {
  58                {
  59                        .addr = priv->config.i2c_addr,
  60                        .flags = 0,
  61                        .len = 3 + len,
  62                        .buf = buf,
  63                }
  64        };
  65
  66        if (3 + len > sizeof(buf)) {
  67                dev_warn(&priv->i2c->dev,
  68                         "%s: i2c wr reg=%04x: len=%d is too big!\n",
  69                         KBUILD_MODNAME, reg, len);
  70                return -EINVAL;
  71        }
  72
  73        buf[0] = (reg >> 8) & 0xff;
  74        buf[1] = (reg >> 0) & 0xff;
  75        buf[2] = mbox;
  76        memcpy(&buf[3], val, len);
  77
  78        ret = i2c_transfer(priv->i2c, msg, 1);
  79        if (ret == 1) {
  80                ret = 0;
  81        } else {
  82                dev_warn(&priv->i2c->dev, "%s: i2c wr failed=%d reg=%04x " \
  83                                "len=%d\n", KBUILD_MODNAME, ret, reg, len);
  84                ret = -EREMOTEIO;
  85        }
  86        return ret;
  87}
  88
  89/* read multiple registers */
  90static int af9013_rd_regs_i2c(struct af9013_state *priv, u8 mbox, u16 reg,
  91        u8 *val, int len)
  92{
  93        int ret;
  94        u8 buf[3];
  95        struct i2c_msg msg[2] = {
  96                {
  97                        .addr = priv->config.i2c_addr,
  98                        .flags = 0,
  99                        .len = 3,
 100                        .buf = buf,
 101                }, {
 102                        .addr = priv->config.i2c_addr,
 103                        .flags = I2C_M_RD,
 104                        .len = len,
 105                        .buf = val,
 106                }
 107        };
 108
 109        buf[0] = (reg >> 8) & 0xff;
 110        buf[1] = (reg >> 0) & 0xff;
 111        buf[2] = mbox;
 112
 113        ret = i2c_transfer(priv->i2c, msg, 2);
 114        if (ret == 2) {
 115                ret = 0;
 116        } else {
 117                dev_warn(&priv->i2c->dev, "%s: i2c rd failed=%d reg=%04x " \
 118                                "len=%d\n", KBUILD_MODNAME, ret, reg, len);
 119                ret = -EREMOTEIO;
 120        }
 121        return ret;
 122}
 123
 124/* write multiple registers */
 125static int af9013_wr_regs(struct af9013_state *priv, u16 reg, const u8 *val,
 126        int len)
 127{
 128        int ret, i;
 129        u8 mbox = (0 << 7)|(0 << 6)|(1 << 1)|(1 << 0);
 130
 131        if ((priv->config.ts_mode == AF9013_TS_USB) &&
 132                ((reg & 0xff00) != 0xff00) && ((reg & 0xff00) != 0xae00)) {
 133                mbox |= ((len - 1) << 2);
 134                ret = af9013_wr_regs_i2c(priv, mbox, reg, val, len);
 135        } else {
 136                for (i = 0; i < len; i++) {
 137                        ret = af9013_wr_regs_i2c(priv, mbox, reg+i, val+i, 1);
 138                        if (ret)
 139                                goto err;
 140                }
 141        }
 142
 143err:
 144        return 0;
 145}
 146
 147/* read multiple registers */
 148static int af9013_rd_regs(struct af9013_state *priv, u16 reg, u8 *val, int len)
 149{
 150        int ret, i;
 151        u8 mbox = (0 << 7)|(0 << 6)|(1 << 1)|(0 << 0);
 152
 153        if ((priv->config.ts_mode == AF9013_TS_USB) &&
 154                ((reg & 0xff00) != 0xff00) && ((reg & 0xff00) != 0xae00)) {
 155                mbox |= ((len - 1) << 2);
 156                ret = af9013_rd_regs_i2c(priv, mbox, reg, val, len);
 157        } else {
 158                for (i = 0; i < len; i++) {
 159                        ret = af9013_rd_regs_i2c(priv, mbox, reg+i, val+i, 1);
 160                        if (ret)
 161                                goto err;
 162                }
 163        }
 164
 165err:
 166        return 0;
 167}
 168
 169/* write single register */
 170static int af9013_wr_reg(struct af9013_state *priv, u16 reg, u8 val)
 171{
 172        return af9013_wr_regs(priv, reg, &val, 1);
 173}
 174
 175/* read single register */
 176static int af9013_rd_reg(struct af9013_state *priv, u16 reg, u8 *val)
 177{
 178        return af9013_rd_regs(priv, reg, val, 1);
 179}
 180
 181static int af9013_write_ofsm_regs(struct af9013_state *state, u16 reg, u8 *val,
 182        u8 len)
 183{
 184        u8 mbox = (1 << 7)|(1 << 6)|((len - 1) << 2)|(1 << 1)|(1 << 0);
 185        return af9013_wr_regs_i2c(state, mbox, reg, val, len);
 186}
 187
 188static int af9013_wr_reg_bits(struct af9013_state *state, u16 reg, int pos,
 189        int len, u8 val)
 190{
 191        int ret;
 192        u8 tmp, mask;
 193
 194        /* no need for read if whole reg is written */
 195        if (len != 8) {
 196                ret = af9013_rd_reg(state, reg, &tmp);
 197                if (ret)
 198                        return ret;
 199
 200                mask = (0xff >> (8 - len)) << pos;
 201                val <<= pos;
 202                tmp &= ~mask;
 203                val |= tmp;
 204        }
 205
 206        return af9013_wr_reg(state, reg, val);
 207}
 208
 209static int af9013_rd_reg_bits(struct af9013_state *state, u16 reg, int pos,
 210        int len, u8 *val)
 211{
 212        int ret;
 213        u8 tmp;
 214
 215        ret = af9013_rd_reg(state, reg, &tmp);
 216        if (ret)
 217                return ret;
 218
 219        *val = (tmp >> pos);
 220        *val &= (0xff >> (8 - len));
 221
 222        return 0;
 223}
 224
 225static int af9013_set_gpio(struct af9013_state *state, u8 gpio, u8 gpioval)
 226{
 227        int ret;
 228        u8 pos;
 229        u16 addr;
 230
 231        dev_dbg(&state->i2c->dev, "%s: gpio=%d gpioval=%02x\n",
 232                        __func__, gpio, gpioval);
 233
 234        /*
 235         * GPIO0 & GPIO1 0xd735
 236         * GPIO2 & GPIO3 0xd736
 237         */
 238
 239        switch (gpio) {
 240        case 0:
 241        case 1:
 242                addr = 0xd735;
 243                break;
 244        case 2:
 245        case 3:
 246                addr = 0xd736;
 247                break;
 248
 249        default:
 250                dev_err(&state->i2c->dev, "%s: invalid gpio=%d\n",
 251                                KBUILD_MODNAME, gpio);
 252                ret = -EINVAL;
 253                goto err;
 254        }
 255
 256        switch (gpio) {
 257        case 0:
 258        case 2:
 259                pos = 0;
 260                break;
 261        case 1:
 262        case 3:
 263        default:
 264                pos = 4;
 265                break;
 266        }
 267
 268        ret = af9013_wr_reg_bits(state, addr, pos, 4, gpioval);
 269        if (ret)
 270                goto err;
 271
 272        return ret;
 273err:
 274        dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
 275        return ret;
 276}
 277
 278static u32 af9013_div(struct af9013_state *state, u32 a, u32 b, u32 x)
 279{
 280        u32 r = 0, c = 0, i;
 281
 282        dev_dbg(&state->i2c->dev, "%s: a=%d b=%d x=%d\n", __func__, a, b, x);
 283
 284        if (a > b) {
 285                c = a / b;
 286                a = a - c * b;
 287        }
 288
 289        for (i = 0; i < x; i++) {
 290                if (a >= b) {
 291                        r += 1;
 292                        a -= b;
 293                }
 294                a <<= 1;
 295                r <<= 1;
 296        }
 297        r = (c << (u32)x) + r;
 298
 299        dev_dbg(&state->i2c->dev, "%s: a=%d b=%d x=%d r=%d r=%x\n",
 300                        __func__, a, b, x, r, r);
 301
 302        return r;
 303}
 304
 305static int af9013_power_ctrl(struct af9013_state *state, u8 onoff)
 306{
 307        int ret, i;
 308        u8 tmp;
 309
 310        dev_dbg(&state->i2c->dev, "%s: onoff=%d\n", __func__, onoff);
 311
 312        /* enable reset */
 313        ret = af9013_wr_reg_bits(state, 0xd417, 4, 1, 1);
 314        if (ret)
 315                goto err;
 316
 317        /* start reset mechanism */
 318        ret = af9013_wr_reg(state, 0xaeff, 1);
 319        if (ret)
 320                goto err;
 321
 322        /* wait reset performs */
 323        for (i = 0; i < 150; i++) {
 324                ret = af9013_rd_reg_bits(state, 0xd417, 1, 1, &tmp);
 325                if (ret)
 326                        goto err;
 327
 328                if (tmp)
 329                        break; /* reset done */
 330
 331                usleep_range(5000, 25000);
 332        }
 333
 334        if (!tmp)
 335                return -ETIMEDOUT;
 336
 337        if (onoff) {
 338                /* clear reset */
 339                ret = af9013_wr_reg_bits(state, 0xd417, 1, 1, 0);
 340                if (ret)
 341                        goto err;
 342
 343                /* disable reset */
 344                ret = af9013_wr_reg_bits(state, 0xd417, 4, 1, 0);
 345
 346                /* power on */
 347                ret = af9013_wr_reg_bits(state, 0xd73a, 3, 1, 0);
 348        } else {
 349                /* power off */
 350                ret = af9013_wr_reg_bits(state, 0xd73a, 3, 1, 1);
 351        }
 352
 353        return ret;
 354err:
 355        dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
 356        return ret;
 357}
 358
 359static int af9013_statistics_ber_unc_start(struct dvb_frontend *fe)
 360{
 361        struct af9013_state *state = fe->demodulator_priv;
 362        int ret;
 363
 364        dev_dbg(&state->i2c->dev, "%s:\n", __func__);
 365
 366        /* reset and start BER counter */
 367        ret = af9013_wr_reg_bits(state, 0xd391, 4, 1, 1);
 368        if (ret)
 369                goto err;
 370
 371        return ret;
 372err:
 373        dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
 374        return ret;
 375}
 376
 377static int af9013_statistics_ber_unc_result(struct dvb_frontend *fe)
 378{
 379        struct af9013_state *state = fe->demodulator_priv;
 380        int ret;
 381        u8 buf[5];
 382
 383        dev_dbg(&state->i2c->dev, "%s:\n", __func__);
 384
 385        /* check if error bit count is ready */
 386        ret = af9013_rd_reg_bits(state, 0xd391, 4, 1, &buf[0]);
 387        if (ret)
 388                goto err;
 389
 390        if (!buf[0]) {
 391                dev_dbg(&state->i2c->dev, "%s: not ready\n", __func__);
 392                return 0;
 393        }
 394
 395        ret = af9013_rd_regs(state, 0xd387, buf, 5);
 396        if (ret)
 397                goto err;
 398
 399        state->ber = (buf[2] << 16) | (buf[1] << 8) | buf[0];
 400        state->ucblocks += (buf[4] << 8) | buf[3];
 401
 402        return ret;
 403err:
 404        dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
 405        return ret;
 406}
 407
 408static int af9013_statistics_snr_start(struct dvb_frontend *fe)
 409{
 410        struct af9013_state *state = fe->demodulator_priv;
 411        int ret;
 412
 413        dev_dbg(&state->i2c->dev, "%s:\n", __func__);
 414
 415        /* start SNR meas */
 416        ret = af9013_wr_reg_bits(state, 0xd2e1, 3, 1, 1);
 417        if (ret)
 418                goto err;
 419
 420        return ret;
 421err:
 422        dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
 423        return ret;
 424}
 425
 426static int af9013_statistics_snr_result(struct dvb_frontend *fe)
 427{
 428        struct af9013_state *state = fe->demodulator_priv;
 429        int ret, i, len;
 430        u8 buf[3], tmp;
 431        u32 snr_val;
 432        const struct af9013_snr *uninitialized_var(snr_lut);
 433
 434        dev_dbg(&state->i2c->dev, "%s:\n", __func__);
 435
 436        /* check if SNR ready */
 437        ret = af9013_rd_reg_bits(state, 0xd2e1, 3, 1, &tmp);
 438        if (ret)
 439                goto err;
 440
 441        if (!tmp) {
 442                dev_dbg(&state->i2c->dev, "%s: not ready\n", __func__);
 443                return 0;
 444        }
 445
 446        /* read value */
 447        ret = af9013_rd_regs(state, 0xd2e3, buf, 3);
 448        if (ret)
 449                goto err;
 450
 451        snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0];
 452
 453        /* read current modulation */
 454        ret = af9013_rd_reg(state, 0xd3c1, &tmp);
 455        if (ret)
 456                goto err;
 457
 458        switch ((tmp >> 6) & 3) {
 459        case 0:
 460                len = ARRAY_SIZE(qpsk_snr_lut);
 461                snr_lut = qpsk_snr_lut;
 462                break;
 463        case 1:
 464                len = ARRAY_SIZE(qam16_snr_lut);
 465                snr_lut = qam16_snr_lut;
 466                break;
 467        case 2:
 468                len = ARRAY_SIZE(qam64_snr_lut);
 469                snr_lut = qam64_snr_lut;
 470                break;
 471        default:
 472                goto err;
 473                break;
 474        }
 475
 476        for (i = 0; i < len; i++) {
 477                tmp = snr_lut[i].snr;
 478
 479                if (snr_val < snr_lut[i].val)
 480                        break;
 481        }
 482        state->snr = tmp * 10; /* dB/10 */
 483
 484        return ret;
 485err:
 486        dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
 487        return ret;
 488}
 489
 490static int af9013_statistics_signal_strength(struct dvb_frontend *fe)
 491{
 492        struct af9013_state *state = fe->demodulator_priv;
 493        int ret = 0;
 494        u8 buf[2], rf_gain, if_gain;
 495        int signal_strength;
 496
 497        dev_dbg(&state->i2c->dev, "%s:\n", __func__);
 498
 499        if (!state->signal_strength_en)
 500                return 0;
 501
 502        ret = af9013_rd_regs(state, 0xd07c, buf, 2);
 503        if (ret)
 504                goto err;
 505
 506        rf_gain = buf[0];
 507        if_gain = buf[1];
 508
 509        signal_strength = (0xffff / \
 510                (9 * (state->rf_50 + state->if_50) - \
 511                11 * (state->rf_80 + state->if_80))) * \
 512                (10 * (rf_gain + if_gain) - \
 513                11 * (state->rf_80 + state->if_80));
 514        if (signal_strength < 0)
 515                signal_strength = 0;
 516        else if (signal_strength > 0xffff)
 517                signal_strength = 0xffff;
 518
 519        state->signal_strength = signal_strength;
 520
 521        return ret;
 522err:
 523        dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
 524        return ret;
 525}
 526
 527static void af9013_statistics_work(struct work_struct *work)
 528{
 529        struct af9013_state *state = container_of(work,
 530                struct af9013_state, statistics_work.work);
 531        unsigned int next_msec;
 532
 533        /* update only signal strength when demod is not locked */
 534        if (!(state->fe_status & FE_HAS_LOCK)) {
 535                state->statistics_step = 0;
 536                state->ber = 0;
 537                state->snr = 0;
 538        }
 539
 540        switch (state->statistics_step) {
 541        default:
 542                state->statistics_step = 0;
 543                /* fall-through */
 544        case 0:
 545                af9013_statistics_signal_strength(&state->fe);
 546                state->statistics_step++;
 547                next_msec = 300;
 548                break;
 549        case 1:
 550                af9013_statistics_snr_start(&state->fe);
 551                state->statistics_step++;
 552                next_msec = 200;
 553                break;
 554        case 2:
 555                af9013_statistics_ber_unc_start(&state->fe);
 556                state->statistics_step++;
 557                next_msec = 1000;
 558                break;
 559        case 3:
 560                af9013_statistics_snr_result(&state->fe);
 561                state->statistics_step++;
 562                next_msec = 400;
 563                break;
 564        case 4:
 565                af9013_statistics_ber_unc_result(&state->fe);
 566                state->statistics_step++;
 567                next_msec = 100;
 568                break;
 569        }
 570
 571        schedule_delayed_work(&state->statistics_work,
 572                msecs_to_jiffies(next_msec));
 573}
 574
 575static int af9013_get_tune_settings(struct dvb_frontend *fe,
 576        struct dvb_frontend_tune_settings *fesettings)
 577{
 578        fesettings->min_delay_ms = 800;
 579        fesettings->step_size = 0;
 580        fesettings->max_drift = 0;
 581
 582        return 0;
 583}
 584
 585static int af9013_set_frontend(struct dvb_frontend *fe)
 586{
 587        struct af9013_state *state = fe->demodulator_priv;
 588        struct dtv_frontend_properties *c = &fe->dtv_property_cache;
 589        int ret, i, sampling_freq;
 590        bool auto_mode, spec_inv;
 591        u8 buf[6];
 592        u32 if_frequency, freq_cw;
 593
 594        dev_dbg(&state->i2c->dev, "%s: frequency=%d bandwidth_hz=%d\n",
 595                        __func__, c->frequency, c->bandwidth_hz);
 596
 597        /* program tuner */
 598        if (fe->ops.tuner_ops.set_params)
 599                fe->ops.tuner_ops.set_params(fe);
 600
 601        /* program CFOE coefficients */
 602        if (c->bandwidth_hz != state->bandwidth_hz) {
 603                for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) {
 604                        if (coeff_lut[i].clock == state->config.clock &&
 605                                coeff_lut[i].bandwidth_hz == c->bandwidth_hz) {
 606                                break;
 607                        }
 608                }
 609
 610                ret = af9013_wr_regs(state, 0xae00, coeff_lut[i].val,
 611                        sizeof(coeff_lut[i].val));
 612        }
 613
 614        /* program frequency control */
 615        if (c->bandwidth_hz != state->bandwidth_hz || state->first_tune) {
 616                /* get used IF frequency */
 617                if (fe->ops.tuner_ops.get_if_frequency)
 618                        fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
 619                else
 620                        if_frequency = state->config.if_frequency;
 621
 622                dev_dbg(&state->i2c->dev, "%s: if_frequency=%d\n",
 623                                __func__, if_frequency);
 624
 625                sampling_freq = if_frequency;
 626
 627                while (sampling_freq > (state->config.clock / 2))
 628                        sampling_freq -= state->config.clock;
 629
 630                if (sampling_freq < 0) {
 631                        sampling_freq *= -1;
 632                        spec_inv = state->config.spec_inv;
 633                } else {
 634                        spec_inv = !state->config.spec_inv;
 635                }
 636
 637                freq_cw = af9013_div(state, sampling_freq, state->config.clock,
 638                                23);
 639
 640                if (spec_inv)
 641                        freq_cw = 0x800000 - freq_cw;
 642
 643                buf[0] = (freq_cw >>  0) & 0xff;
 644                buf[1] = (freq_cw >>  8) & 0xff;
 645                buf[2] = (freq_cw >> 16) & 0x7f;
 646
 647                freq_cw = 0x800000 - freq_cw;
 648
 649                buf[3] = (freq_cw >>  0) & 0xff;
 650                buf[4] = (freq_cw >>  8) & 0xff;
 651                buf[5] = (freq_cw >> 16) & 0x7f;
 652
 653                ret = af9013_wr_regs(state, 0xd140, buf, 3);
 654                if (ret)
 655                        goto err;
 656
 657                ret = af9013_wr_regs(state, 0x9be7, buf, 6);
 658                if (ret)
 659                        goto err;
 660        }
 661
 662        /* clear TPS lock flag */
 663        ret = af9013_wr_reg_bits(state, 0xd330, 3, 1, 1);
 664        if (ret)
 665                goto err;
 666
 667        /* clear MPEG2 lock flag */
 668        ret = af9013_wr_reg_bits(state, 0xd507, 6, 1, 0);
 669        if (ret)
 670                goto err;
 671
 672        /* empty channel function */
 673        ret = af9013_wr_reg_bits(state, 0x9bfe, 0, 1, 0);
 674        if (ret)
 675                goto err;
 676
 677        /* empty DVB-T channel function */
 678        ret = af9013_wr_reg_bits(state, 0x9bc2, 0, 1, 0);
 679        if (ret)
 680                goto err;
 681
 682        /* transmission parameters */
 683        auto_mode = false;
 684        memset(buf, 0, 3);
 685
 686        switch (c->transmission_mode) {
 687        case TRANSMISSION_MODE_AUTO:
 688                auto_mode = 1;
 689                break;
 690        case TRANSMISSION_MODE_2K:
 691                break;
 692        case TRANSMISSION_MODE_8K:
 693                buf[0] |= (1 << 0);
 694                break;
 695        default:
 696                dev_dbg(&state->i2c->dev, "%s: invalid transmission_mode\n",
 697                                __func__);
 698                auto_mode = 1;
 699        }
 700
 701        switch (c->guard_interval) {
 702        case GUARD_INTERVAL_AUTO:
 703                auto_mode = 1;
 704                break;
 705        case GUARD_INTERVAL_1_32:
 706                break;
 707        case GUARD_INTERVAL_1_16:
 708                buf[0] |= (1 << 2);
 709                break;
 710        case GUARD_INTERVAL_1_8:
 711                buf[0] |= (2 << 2);
 712                break;
 713        case GUARD_INTERVAL_1_4:
 714                buf[0] |= (3 << 2);
 715                break;
 716        default:
 717                dev_dbg(&state->i2c->dev, "%s: invalid guard_interval\n",
 718                                __func__);
 719                auto_mode = 1;
 720        }
 721
 722        switch (c->hierarchy) {
 723        case HIERARCHY_AUTO:
 724                auto_mode = 1;
 725                break;
 726        case HIERARCHY_NONE:
 727                break;
 728        case HIERARCHY_1:
 729                buf[0] |= (1 << 4);
 730                break;
 731        case HIERARCHY_2:
 732                buf[0] |= (2 << 4);
 733                break;
 734        case HIERARCHY_4:
 735                buf[0] |= (3 << 4);
 736                break;
 737        default:
 738                dev_dbg(&state->i2c->dev, "%s: invalid hierarchy\n", __func__);
 739                auto_mode = 1;
 740        }
 741
 742        switch (c->modulation) {
 743        case QAM_AUTO:
 744                auto_mode = 1;
 745                break;
 746        case QPSK:
 747                break;
 748        case QAM_16:
 749                buf[1] |= (1 << 6);
 750                break;
 751        case QAM_64:
 752                buf[1] |= (2 << 6);
 753                break;
 754        default:
 755                dev_dbg(&state->i2c->dev, "%s: invalid modulation\n", __func__);
 756                auto_mode = 1;
 757        }
 758
 759        /* Use HP. How and which case we can switch to LP? */
 760        buf[1] |= (1 << 4);
 761
 762        switch (c->code_rate_HP) {
 763        case FEC_AUTO:
 764                auto_mode = 1;
 765                break;
 766        case FEC_1_2:
 767                break;
 768        case FEC_2_3:
 769                buf[2] |= (1 << 0);
 770                break;
 771        case FEC_3_4:
 772                buf[2] |= (2 << 0);
 773                break;
 774        case FEC_5_6:
 775                buf[2] |= (3 << 0);
 776                break;
 777        case FEC_7_8:
 778                buf[2] |= (4 << 0);
 779                break;
 780        default:
 781                dev_dbg(&state->i2c->dev, "%s: invalid code_rate_HP\n",
 782                                __func__);
 783                auto_mode = 1;
 784        }
 785
 786        switch (c->code_rate_LP) {
 787        case FEC_AUTO:
 788                auto_mode = 1;
 789                break;
 790        case FEC_1_2:
 791                break;
 792        case FEC_2_3:
 793                buf[2] |= (1 << 3);
 794                break;
 795        case FEC_3_4:
 796                buf[2] |= (2 << 3);
 797                break;
 798        case FEC_5_6:
 799                buf[2] |= (3 << 3);
 800                break;
 801        case FEC_7_8:
 802                buf[2] |= (4 << 3);
 803                break;
 804        case FEC_NONE:
 805                break;
 806        default:
 807                dev_dbg(&state->i2c->dev, "%s: invalid code_rate_LP\n",
 808                                __func__);
 809                auto_mode = 1;
 810        }
 811
 812        switch (c->bandwidth_hz) {
 813        case 6000000:
 814                break;
 815        case 7000000:
 816                buf[1] |= (1 << 2);
 817                break;
 818        case 8000000:
 819                buf[1] |= (2 << 2);
 820                break;
 821        default:
 822                dev_dbg(&state->i2c->dev, "%s: invalid bandwidth_hz\n",
 823                                __func__);
 824                ret = -EINVAL;
 825                goto err;
 826        }
 827
 828        ret = af9013_wr_regs(state, 0xd3c0, buf, 3);
 829        if (ret)
 830                goto err;
 831
 832        if (auto_mode) {
 833                /* clear easy mode flag */
 834                ret = af9013_wr_reg(state, 0xaefd, 0);
 835                if (ret)
 836                        goto err;
 837
 838                dev_dbg(&state->i2c->dev, "%s: auto params\n", __func__);
 839        } else {
 840                /* set easy mode flag */
 841                ret = af9013_wr_reg(state, 0xaefd, 1);
 842                if (ret)
 843                        goto err;
 844
 845                ret = af9013_wr_reg(state, 0xaefe, 0);
 846                if (ret)
 847                        goto err;
 848
 849                dev_dbg(&state->i2c->dev, "%s: manual params\n", __func__);
 850        }
 851
 852        /* tune */
 853        ret = af9013_wr_reg(state, 0xffff, 0);
 854        if (ret)
 855                goto err;
 856
 857        state->bandwidth_hz = c->bandwidth_hz;
 858        state->set_frontend_jiffies = jiffies;
 859        state->first_tune = false;
 860
 861        return ret;
 862err:
 863        dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
 864        return ret;
 865}
 866
 867static int af9013_get_frontend(struct dvb_frontend *fe)
 868{
 869        struct dtv_frontend_properties *c = &fe->dtv_property_cache;
 870        struct af9013_state *state = fe->demodulator_priv;
 871        int ret;
 872        u8 buf[3];
 873
 874        dev_dbg(&state->i2c->dev, "%s:\n", __func__);
 875
 876        ret = af9013_rd_regs(state, 0xd3c0, buf, 3);
 877        if (ret)
 878                goto err;
 879
 880        switch ((buf[1] >> 6) & 3) {
 881        case 0:
 882                c->modulation = QPSK;
 883                break;
 884        case 1:
 885                c->modulation = QAM_16;
 886                break;
 887        case 2:
 888                c->modulation = QAM_64;
 889                break;
 890        }
 891
 892        switch ((buf[0] >> 0) & 3) {
 893        case 0:
 894                c->transmission_mode = TRANSMISSION_MODE_2K;
 895                break;
 896        case 1:
 897                c->transmission_mode = TRANSMISSION_MODE_8K;
 898        }
 899
 900        switch ((buf[0] >> 2) & 3) {
 901        case 0:
 902                c->guard_interval = GUARD_INTERVAL_1_32;
 903                break;
 904        case 1:
 905                c->guard_interval = GUARD_INTERVAL_1_16;
 906                break;
 907        case 2:
 908                c->guard_interval = GUARD_INTERVAL_1_8;
 909                break;
 910        case 3:
 911                c->guard_interval = GUARD_INTERVAL_1_4;
 912                break;
 913        }
 914
 915        switch ((buf[0] >> 4) & 7) {
 916        case 0:
 917                c->hierarchy = HIERARCHY_NONE;
 918                break;
 919        case 1:
 920                c->hierarchy = HIERARCHY_1;
 921                break;
 922        case 2:
 923                c->hierarchy = HIERARCHY_2;
 924                break;
 925        case 3:
 926                c->hierarchy = HIERARCHY_4;
 927                break;
 928        }
 929
 930        switch ((buf[2] >> 0) & 7) {
 931        case 0:
 932                c->code_rate_HP = FEC_1_2;
 933                break;
 934        case 1:
 935                c->code_rate_HP = FEC_2_3;
 936                break;
 937        case 2:
 938                c->code_rate_HP = FEC_3_4;
 939                break;
 940        case 3:
 941                c->code_rate_HP = FEC_5_6;
 942                break;
 943        case 4:
 944                c->code_rate_HP = FEC_7_8;
 945                break;
 946        }
 947
 948        switch ((buf[2] >> 3) & 7) {
 949        case 0:
 950                c->code_rate_LP = FEC_1_2;
 951                break;
 952        case 1:
 953                c->code_rate_LP = FEC_2_3;
 954                break;
 955        case 2:
 956                c->code_rate_LP = FEC_3_4;
 957                break;
 958        case 3:
 959                c->code_rate_LP = FEC_5_6;
 960                break;
 961        case 4:
 962                c->code_rate_LP = FEC_7_8;
 963                break;
 964        }
 965
 966        switch ((buf[1] >> 2) & 3) {
 967        case 0:
 968                c->bandwidth_hz = 6000000;
 969                break;
 970        case 1:
 971                c->bandwidth_hz = 7000000;
 972                break;
 973        case 2:
 974                c->bandwidth_hz = 8000000;
 975                break;
 976        }
 977
 978        return ret;
 979err:
 980        dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
 981        return ret;
 982}
 983
 984static int af9013_read_status(struct dvb_frontend *fe, fe_status_t *status)
 985{
 986        struct af9013_state *state = fe->demodulator_priv;
 987        int ret;
 988        u8 tmp;
 989
 990        /*
 991         * Return status from the cache if it is younger than 2000ms with the
 992         * exception of last tune is done during 4000ms.
 993         */
 994        if (time_is_after_jiffies(
 995                state->read_status_jiffies + msecs_to_jiffies(2000)) &&
 996                time_is_before_jiffies(
 997                state->set_frontend_jiffies + msecs_to_jiffies(4000))
 998        ) {
 999                        *status = state->fe_status;
1000                        return 0;
1001        } else {
1002                *status = 0;
1003        }
1004
1005        /* MPEG2 lock */
1006        ret = af9013_rd_reg_bits(state, 0xd507, 6, 1, &tmp);
1007        if (ret)
1008                goto err;
1009
1010        if (tmp)
1011                *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
1012                        FE_HAS_SYNC | FE_HAS_LOCK;
1013
1014        if (!*status) {
1015                /* TPS lock */
1016                ret = af9013_rd_reg_bits(state, 0xd330, 3, 1, &tmp);
1017                if (ret)
1018                        goto err;
1019
1020                if (tmp)
1021                        *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
1022                                FE_HAS_VITERBI;
1023        }
1024
1025        state->fe_status = *status;
1026        state->read_status_jiffies = jiffies;
1027
1028        return ret;
1029err:
1030        dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
1031        return ret;
1032}
1033
1034static int af9013_read_snr(struct dvb_frontend *fe, u16 *snr)
1035{
1036        struct af9013_state *state = fe->demodulator_priv;
1037        *snr = state->snr;
1038        return 0;
1039}
1040
1041static int af9013_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
1042{
1043        struct af9013_state *state = fe->demodulator_priv;
1044        *strength = state->signal_strength;
1045        return 0;
1046}
1047
1048static int af9013_read_ber(struct dvb_frontend *fe, u32 *ber)
1049{
1050        struct af9013_state *state = fe->demodulator_priv;
1051        *ber = state->ber;
1052        return 0;
1053}
1054
1055static int af9013_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
1056{
1057        struct af9013_state *state = fe->demodulator_priv;
1058        *ucblocks = state->ucblocks;
1059        return 0;
1060}
1061
1062static int af9013_init(struct dvb_frontend *fe)
1063{
1064        struct af9013_state *state = fe->demodulator_priv;
1065        int ret, i, len;
1066        u8 buf[3], tmp;
1067        u32 adc_cw;
1068        const struct af9013_reg_bit *init;
1069
1070        dev_dbg(&state->i2c->dev, "%s:\n", __func__);
1071
1072        /* power on */
1073        ret = af9013_power_ctrl(state, 1);
1074        if (ret)
1075                goto err;
1076
1077        /* enable ADC */
1078        ret = af9013_wr_reg(state, 0xd73a, 0xa4);
1079        if (ret)
1080                goto err;
1081
1082        /* write API version to firmware */
1083        ret = af9013_wr_regs(state, 0x9bf2, state->config.api_version, 4);
1084        if (ret)
1085                goto err;
1086
1087        /* program ADC control */
1088        switch (state->config.clock) {
1089        case 28800000: /* 28.800 MHz */
1090                tmp = 0;
1091                break;
1092        case 20480000: /* 20.480 MHz */
1093                tmp = 1;
1094                break;
1095        case 28000000: /* 28.000 MHz */
1096                tmp = 2;
1097                break;
1098        case 25000000: /* 25.000 MHz */
1099                tmp = 3;
1100                break;
1101        default:
1102                dev_err(&state->i2c->dev, "%s: invalid clock\n",
1103                                KBUILD_MODNAME);
1104                return -EINVAL;
1105        }
1106
1107        adc_cw = af9013_div(state, state->config.clock, 1000000ul, 19);
1108        buf[0] = (adc_cw >>  0) & 0xff;
1109        buf[1] = (adc_cw >>  8) & 0xff;
1110        buf[2] = (adc_cw >> 16) & 0xff;
1111
1112        ret = af9013_wr_regs(state, 0xd180, buf, 3);
1113        if (ret)
1114                goto err;
1115
1116        ret = af9013_wr_reg_bits(state, 0x9bd2, 0, 4, tmp);
1117        if (ret)
1118                goto err;
1119
1120        /* set I2C master clock */
1121        ret = af9013_wr_reg(state, 0xd416, 0x14);
1122        if (ret)
1123                goto err;
1124
1125        /* set 16 embx */
1126        ret = af9013_wr_reg_bits(state, 0xd700, 1, 1, 1);
1127        if (ret)
1128                goto err;
1129
1130        /* set no trigger */
1131        ret = af9013_wr_reg_bits(state, 0xd700, 2, 1, 0);
1132        if (ret)
1133                goto err;
1134
1135        /* set read-update bit for constellation */
1136        ret = af9013_wr_reg_bits(state, 0xd371, 1, 1, 1);
1137        if (ret)
1138                goto err;
1139
1140        /* settings for mp2if */
1141        if (state->config.ts_mode == AF9013_TS_USB) {
1142                /* AF9015 split PSB to 1.5k + 0.5k */
1143                ret = af9013_wr_reg_bits(state, 0xd50b, 2, 1, 1);
1144                if (ret)
1145                        goto err;
1146        } else {
1147                /* AF9013 change the output bit to data7 */
1148                ret = af9013_wr_reg_bits(state, 0xd500, 3, 1, 1);
1149                if (ret)
1150                        goto err;
1151
1152                /* AF9013 set mpeg to full speed */
1153                ret = af9013_wr_reg_bits(state, 0xd502, 4, 1, 1);
1154                if (ret)
1155                        goto err;
1156        }
1157
1158        ret = af9013_wr_reg_bits(state, 0xd520, 4, 1, 1);
1159        if (ret)
1160                goto err;
1161
1162        /* load OFSM settings */
1163        dev_dbg(&state->i2c->dev, "%s: load ofsm settings\n", __func__);
1164        len = ARRAY_SIZE(ofsm_init);
1165        init = ofsm_init;
1166        for (i = 0; i < len; i++) {
1167                ret = af9013_wr_reg_bits(state, init[i].addr, init[i].pos,
1168                        init[i].len, init[i].val);
1169                if (ret)
1170                        goto err;
1171        }
1172
1173        /* load tuner specific settings */
1174        dev_dbg(&state->i2c->dev, "%s: load tuner specific settings\n",
1175                        __func__);
1176        switch (state->config.tuner) {
1177        case AF9013_TUNER_MXL5003D:
1178                len = ARRAY_SIZE(tuner_init_mxl5003d);
1179                init = tuner_init_mxl5003d;
1180                break;
1181        case AF9013_TUNER_MXL5005D:
1182        case AF9013_TUNER_MXL5005R:
1183        case AF9013_TUNER_MXL5007T:
1184                len = ARRAY_SIZE(tuner_init_mxl5005);
1185                init = tuner_init_mxl5005;
1186                break;
1187        case AF9013_TUNER_ENV77H11D5:
1188                len = ARRAY_SIZE(tuner_init_env77h11d5);
1189                init = tuner_init_env77h11d5;
1190                break;
1191        case AF9013_TUNER_MT2060:
1192                len = ARRAY_SIZE(tuner_init_mt2060);
1193                init = tuner_init_mt2060;
1194                break;
1195        case AF9013_TUNER_MC44S803:
1196                len = ARRAY_SIZE(tuner_init_mc44s803);
1197                init = tuner_init_mc44s803;
1198                break;
1199        case AF9013_TUNER_QT1010:
1200        case AF9013_TUNER_QT1010A:
1201                len = ARRAY_SIZE(tuner_init_qt1010);
1202                init = tuner_init_qt1010;
1203                break;
1204        case AF9013_TUNER_MT2060_2:
1205                len = ARRAY_SIZE(tuner_init_mt2060_2);
1206                init = tuner_init_mt2060_2;
1207                break;
1208        case AF9013_TUNER_TDA18271:
1209        case AF9013_TUNER_TDA18218:
1210                len = ARRAY_SIZE(tuner_init_tda18271);
1211                init = tuner_init_tda18271;
1212                break;
1213        case AF9013_TUNER_UNKNOWN:
1214        default:
1215                len = ARRAY_SIZE(tuner_init_unknown);
1216                init = tuner_init_unknown;
1217                break;
1218        }
1219
1220        for (i = 0; i < len; i++) {
1221                ret = af9013_wr_reg_bits(state, init[i].addr, init[i].pos,
1222                        init[i].len, init[i].val);
1223                if (ret)
1224                        goto err;
1225        }
1226
1227        /* TS mode */
1228        ret = af9013_wr_reg_bits(state, 0xd500, 1, 2, state->config.ts_mode);
1229        if (ret)
1230                goto err;
1231
1232        /* enable lock led */
1233        ret = af9013_wr_reg_bits(state, 0xd730, 0, 1, 1);
1234        if (ret)
1235                goto err;
1236
1237        /* check if we support signal strength */
1238        if (!state->signal_strength_en) {
1239                ret = af9013_rd_reg_bits(state, 0x9bee, 0, 1,
1240                        &state->signal_strength_en);
1241                if (ret)
1242                        goto err;
1243        }
1244
1245        /* read values needed for signal strength calculation */
1246        if (state->signal_strength_en && !state->rf_50) {
1247                ret = af9013_rd_reg(state, 0x9bbd, &state->rf_50);
1248                if (ret)
1249                        goto err;
1250
1251                ret = af9013_rd_reg(state, 0x9bd0, &state->rf_80);
1252                if (ret)
1253                        goto err;
1254
1255                ret = af9013_rd_reg(state, 0x9be2, &state->if_50);
1256                if (ret)
1257                        goto err;
1258
1259                ret = af9013_rd_reg(state, 0x9be4, &state->if_80);
1260                if (ret)
1261                        goto err;
1262        }
1263
1264        /* SNR */
1265        ret = af9013_wr_reg(state, 0xd2e2, 1);
1266        if (ret)
1267                goto err;
1268
1269        /* BER / UCB */
1270        buf[0] = (10000 >> 0) & 0xff;
1271        buf[1] = (10000 >> 8) & 0xff;
1272        ret = af9013_wr_regs(state, 0xd385, buf, 2);
1273        if (ret)
1274                goto err;
1275
1276        /* enable FEC monitor */
1277        ret = af9013_wr_reg_bits(state, 0xd392, 1, 1, 1);
1278        if (ret)
1279                goto err;
1280
1281        state->first_tune = true;
1282        schedule_delayed_work(&state->statistics_work, msecs_to_jiffies(400));
1283
1284        return ret;
1285err:
1286        dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
1287        return ret;
1288}
1289
1290static int af9013_sleep(struct dvb_frontend *fe)
1291{
1292        struct af9013_state *state = fe->demodulator_priv;
1293        int ret;
1294
1295        dev_dbg(&state->i2c->dev, "%s:\n", __func__);
1296
1297        /* stop statistics polling */
1298        cancel_delayed_work_sync(&state->statistics_work);
1299
1300        /* disable lock led */
1301        ret = af9013_wr_reg_bits(state, 0xd730, 0, 1, 0);
1302        if (ret)
1303                goto err;
1304
1305        /* power off */
1306        ret = af9013_power_ctrl(state, 0);
1307        if (ret)
1308                goto err;
1309
1310        return ret;
1311err:
1312        dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
1313        return ret;
1314}
1315
1316static int af9013_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
1317{
1318        int ret;
1319        struct af9013_state *state = fe->demodulator_priv;
1320
1321        dev_dbg(&state->i2c->dev, "%s: enable=%d\n", __func__, enable);
1322
1323        /* gate already open or close */
1324        if (state->i2c_gate_state == enable)
1325                return 0;
1326
1327        if (state->config.ts_mode == AF9013_TS_USB)
1328                ret = af9013_wr_reg_bits(state, 0xd417, 3, 1, enable);
1329        else
1330                ret = af9013_wr_reg_bits(state, 0xd607, 2, 1, enable);
1331        if (ret)
1332                goto err;
1333
1334        state->i2c_gate_state = enable;
1335
1336        return ret;
1337err:
1338        dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
1339        return ret;
1340}
1341
1342static void af9013_release(struct dvb_frontend *fe)
1343{
1344        struct af9013_state *state = fe->demodulator_priv;
1345        kfree(state);
1346}
1347
1348static struct dvb_frontend_ops af9013_ops;
1349
1350static int af9013_download_firmware(struct af9013_state *state)
1351{
1352        int i, len, remaining, ret;
1353        const struct firmware *fw;
1354        u16 checksum = 0;
1355        u8 val;
1356        u8 fw_params[4];
1357        u8 *fw_file = AF9013_FIRMWARE;
1358
1359        msleep(100);
1360        /* check whether firmware is already running */
1361        ret = af9013_rd_reg(state, 0x98be, &val);
1362        if (ret)
1363                goto err;
1364        else
1365                dev_dbg(&state->i2c->dev, "%s: firmware status=%02x\n",
1366                                __func__, val);
1367
1368        if (val == 0x0c) /* fw is running, no need for download */
1369                goto exit;
1370
1371        dev_info(&state->i2c->dev, "%s: found a '%s' in cold state, will try " \
1372                        "to load a firmware\n",
1373                        KBUILD_MODNAME, af9013_ops.info.name);
1374
1375        /* request the firmware, this will block and timeout */
1376        ret = request_firmware(&fw, fw_file, state->i2c->dev.parent);
1377        if (ret) {
1378                dev_info(&state->i2c->dev, "%s: did not find the firmware " \
1379                        "file. (%s) Please see linux/Documentation/dvb/ for " \
1380                        "more details on firmware-problems. (%d)\n",
1381                        KBUILD_MODNAME, fw_file, ret);
1382                goto err;
1383        }
1384
1385        dev_info(&state->i2c->dev, "%s: downloading firmware from file '%s'\n",
1386                        KBUILD_MODNAME, fw_file);
1387
1388        /* calc checksum */
1389        for (i = 0; i < fw->size; i++)
1390                checksum += fw->data[i];
1391
1392        fw_params[0] = checksum >> 8;
1393        fw_params[1] = checksum & 0xff;
1394        fw_params[2] = fw->size >> 8;
1395        fw_params[3] = fw->size & 0xff;
1396
1397        /* write fw checksum & size */
1398        ret = af9013_write_ofsm_regs(state, 0x50fc,
1399                fw_params, sizeof(fw_params));
1400        if (ret)
1401                goto err_release;
1402
1403        #define FW_ADDR 0x5100 /* firmware start address */
1404        #define LEN_MAX 16 /* max packet size */
1405        for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) {
1406                len = remaining;
1407                if (len > LEN_MAX)
1408                        len = LEN_MAX;
1409
1410                ret = af9013_write_ofsm_regs(state,
1411                        FW_ADDR + fw->size - remaining,
1412                        (u8 *) &fw->data[fw->size - remaining], len);
1413                if (ret) {
1414                        dev_err(&state->i2c->dev,
1415                                        "%s: firmware download failed=%d\n",
1416                                        KBUILD_MODNAME, ret);
1417                        goto err_release;
1418                }
1419        }
1420
1421        /* request boot firmware */
1422        ret = af9013_wr_reg(state, 0xe205, 1);
1423        if (ret)
1424                goto err_release;
1425
1426        for (i = 0; i < 15; i++) {
1427                msleep(100);
1428
1429                /* check firmware status */
1430                ret = af9013_rd_reg(state, 0x98be, &val);
1431                if (ret)
1432                        goto err_release;
1433
1434                dev_dbg(&state->i2c->dev, "%s: firmware status=%02x\n",
1435                                __func__, val);
1436
1437                if (val == 0x0c || val == 0x04) /* success or fail */
1438                        break;
1439        }
1440
1441        if (val == 0x04) {
1442                dev_err(&state->i2c->dev, "%s: firmware did not run\n",
1443                                KBUILD_MODNAME);
1444                ret = -ENODEV;
1445        } else if (val != 0x0c) {
1446                dev_err(&state->i2c->dev, "%s: firmware boot timeout\n",
1447                                KBUILD_MODNAME);
1448                ret = -ENODEV;
1449        }
1450
1451err_release:
1452        release_firmware(fw);
1453err:
1454exit:
1455        if (!ret)
1456                dev_info(&state->i2c->dev, "%s: found a '%s' in warm state\n",
1457                                KBUILD_MODNAME, af9013_ops.info.name);
1458        return ret;
1459}
1460
1461struct dvb_frontend *af9013_attach(const struct af9013_config *config,
1462        struct i2c_adapter *i2c)
1463{
1464        int ret;
1465        struct af9013_state *state = NULL;
1466        u8 buf[4], i;
1467
1468        /* allocate memory for the internal state */
1469        state = kzalloc(sizeof(struct af9013_state), GFP_KERNEL);
1470        if (state == NULL)
1471                goto err;
1472
1473        /* setup the state */
1474        state->i2c = i2c;
1475        memcpy(&state->config, config, sizeof(struct af9013_config));
1476
1477        /* download firmware */
1478        if (state->config.ts_mode != AF9013_TS_USB) {
1479                ret = af9013_download_firmware(state);
1480                if (ret)
1481                        goto err;
1482        }
1483
1484        /* firmware version */
1485        ret = af9013_rd_regs(state, 0x5103, buf, 4);
1486        if (ret)
1487                goto err;
1488
1489        dev_info(&state->i2c->dev, "%s: firmware version %d.%d.%d.%d\n",
1490                        KBUILD_MODNAME, buf[0], buf[1], buf[2], buf[3]);
1491
1492        /* set GPIOs */
1493        for (i = 0; i < sizeof(state->config.gpio); i++) {
1494                ret = af9013_set_gpio(state, i, state->config.gpio[i]);
1495                if (ret)
1496                        goto err;
1497        }
1498
1499        /* create dvb_frontend */
1500        memcpy(&state->fe.ops, &af9013_ops,
1501                sizeof(struct dvb_frontend_ops));
1502        state->fe.demodulator_priv = state;
1503
1504        INIT_DELAYED_WORK(&state->statistics_work, af9013_statistics_work);
1505
1506        return &state->fe;
1507err:
1508        kfree(state);
1509        return NULL;
1510}
1511EXPORT_SYMBOL(af9013_attach);
1512
1513static struct dvb_frontend_ops af9013_ops = {
1514        .delsys = { SYS_DVBT },
1515        .info = {
1516                .name = "Afatech AF9013",
1517                .frequency_min = 174000000,
1518                .frequency_max = 862000000,
1519                .frequency_stepsize = 250000,
1520                .frequency_tolerance = 0,
1521                .caps = FE_CAN_FEC_1_2 |
1522                        FE_CAN_FEC_2_3 |
1523                        FE_CAN_FEC_3_4 |
1524                        FE_CAN_FEC_5_6 |
1525                        FE_CAN_FEC_7_8 |
1526                        FE_CAN_FEC_AUTO |
1527                        FE_CAN_QPSK |
1528                        FE_CAN_QAM_16 |
1529                        FE_CAN_QAM_64 |
1530                        FE_CAN_QAM_AUTO |
1531                        FE_CAN_TRANSMISSION_MODE_AUTO |
1532                        FE_CAN_GUARD_INTERVAL_AUTO |
1533                        FE_CAN_HIERARCHY_AUTO |
1534                        FE_CAN_RECOVER |
1535                        FE_CAN_MUTE_TS
1536        },
1537
1538        .release = af9013_release,
1539
1540        .init = af9013_init,
1541        .sleep = af9013_sleep,
1542
1543        .get_tune_settings = af9013_get_tune_settings,
1544        .set_frontend = af9013_set_frontend,
1545        .get_frontend = af9013_get_frontend,
1546
1547        .read_status = af9013_read_status,
1548        .read_snr = af9013_read_snr,
1549        .read_signal_strength = af9013_read_signal_strength,
1550        .read_ber = af9013_read_ber,
1551        .read_ucblocks = af9013_read_ucblocks,
1552
1553        .i2c_gate_ctrl = af9013_i2c_gate_ctrl,
1554};
1555
1556MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1557MODULE_DESCRIPTION("Afatech AF9013 DVB-T demodulator driver");
1558MODULE_LICENSE("GPL");
1559MODULE_FIRMWARE(AF9013_FIRMWARE);
1560