linux/drivers/media/dvb-frontends/au8522_dig.c
<<
>>
Prefs
   1/*
   2    Auvitek AU8522 QAM/8VSB demodulator driver
   3
   4    Copyright (C) 2008 Steven Toth <stoth@linuxtv.org>
   5
   6    This program is free software; you can redistribute it and/or modify
   7    it under the terms of the GNU General Public License as published by
   8    the Free Software Foundation; either version 2 of the License, or
   9    (at your option) any later version.
  10
  11    This program is distributed in the hope that it will be useful,
  12    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14    GNU General Public License for more details.
  15
  16    You should have received a copy of the GNU General Public License
  17    along with this program; if not, write to the Free Software
  18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19
  20*/
  21
  22#include <linux/kernel.h>
  23#include <linux/init.h>
  24#include <linux/module.h>
  25#include <linux/string.h>
  26#include <linux/delay.h>
  27#include "dvb_frontend.h"
  28#include "au8522.h"
  29#include "au8522_priv.h"
  30
  31static int debug;
  32static int zv_mode = 1; /* default to on */
  33
  34#define dprintk(arg...)\
  35        do { if (debug)\
  36                printk(arg);\
  37        } while (0)
  38
  39struct mse2snr_tab {
  40        u16 val;
  41        u16 data;
  42};
  43
  44/* VSB SNR lookup table */
  45static struct mse2snr_tab vsb_mse2snr_tab[] = {
  46        {   0, 270 },
  47        {   2, 250 },
  48        {   3, 240 },
  49        {   5, 230 },
  50        {   7, 220 },
  51        {   9, 210 },
  52        {  12, 200 },
  53        {  13, 195 },
  54        {  15, 190 },
  55        {  17, 185 },
  56        {  19, 180 },
  57        {  21, 175 },
  58        {  24, 170 },
  59        {  27, 165 },
  60        {  31, 160 },
  61        {  32, 158 },
  62        {  33, 156 },
  63        {  36, 152 },
  64        {  37, 150 },
  65        {  39, 148 },
  66        {  40, 146 },
  67        {  41, 144 },
  68        {  43, 142 },
  69        {  44, 140 },
  70        {  48, 135 },
  71        {  50, 130 },
  72        {  43, 142 },
  73        {  53, 125 },
  74        {  56, 120 },
  75        { 256, 115 },
  76};
  77
  78/* QAM64 SNR lookup table */
  79static struct mse2snr_tab qam64_mse2snr_tab[] = {
  80        {  15,   0 },
  81        {  16, 290 },
  82        {  17, 288 },
  83        {  18, 286 },
  84        {  19, 284 },
  85        {  20, 282 },
  86        {  21, 281 },
  87        {  22, 279 },
  88        {  23, 277 },
  89        {  24, 275 },
  90        {  25, 273 },
  91        {  26, 271 },
  92        {  27, 269 },
  93        {  28, 268 },
  94        {  29, 266 },
  95        {  30, 264 },
  96        {  31, 262 },
  97        {  32, 260 },
  98        {  33, 259 },
  99        {  34, 258 },
 100        {  35, 256 },
 101        {  36, 255 },
 102        {  37, 254 },
 103        {  38, 252 },
 104        {  39, 251 },
 105        {  40, 250 },
 106        {  41, 249 },
 107        {  42, 248 },
 108        {  43, 246 },
 109        {  44, 245 },
 110        {  45, 244 },
 111        {  46, 242 },
 112        {  47, 241 },
 113        {  48, 240 },
 114        {  50, 239 },
 115        {  51, 238 },
 116        {  53, 237 },
 117        {  54, 236 },
 118        {  56, 235 },
 119        {  57, 234 },
 120        {  59, 233 },
 121        {  60, 232 },
 122        {  62, 231 },
 123        {  63, 230 },
 124        {  65, 229 },
 125        {  67, 228 },
 126        {  68, 227 },
 127        {  70, 226 },
 128        {  71, 225 },
 129        {  73, 224 },
 130        {  74, 223 },
 131        {  76, 222 },
 132        {  78, 221 },
 133        {  80, 220 },
 134        {  82, 219 },
 135        {  85, 218 },
 136        {  88, 217 },
 137        {  90, 216 },
 138        {  92, 215 },
 139        {  93, 214 },
 140        {  94, 212 },
 141        {  95, 211 },
 142        {  97, 210 },
 143        {  99, 209 },
 144        { 101, 208 },
 145        { 102, 207 },
 146        { 104, 206 },
 147        { 107, 205 },
 148        { 111, 204 },
 149        { 114, 203 },
 150        { 118, 202 },
 151        { 122, 201 },
 152        { 125, 200 },
 153        { 128, 199 },
 154        { 130, 198 },
 155        { 132, 197 },
 156        { 256, 190 },
 157};
 158
 159/* QAM256 SNR lookup table */
 160static struct mse2snr_tab qam256_mse2snr_tab[] = {
 161        {  15,   0 },
 162        {  16, 400 },
 163        {  17, 398 },
 164        {  18, 396 },
 165        {  19, 394 },
 166        {  20, 392 },
 167        {  21, 390 },
 168        {  22, 388 },
 169        {  23, 386 },
 170        {  24, 384 },
 171        {  25, 382 },
 172        {  26, 380 },
 173        {  27, 379 },
 174        {  28, 378 },
 175        {  29, 377 },
 176        {  30, 376 },
 177        {  31, 375 },
 178        {  32, 374 },
 179        {  33, 373 },
 180        {  34, 372 },
 181        {  35, 371 },
 182        {  36, 370 },
 183        {  37, 362 },
 184        {  38, 354 },
 185        {  39, 346 },
 186        {  40, 338 },
 187        {  41, 330 },
 188        {  42, 328 },
 189        {  43, 326 },
 190        {  44, 324 },
 191        {  45, 322 },
 192        {  46, 320 },
 193        {  47, 319 },
 194        {  48, 318 },
 195        {  49, 317 },
 196        {  50, 316 },
 197        {  51, 315 },
 198        {  52, 314 },
 199        {  53, 313 },
 200        {  54, 312 },
 201        {  55, 311 },
 202        {  56, 310 },
 203        {  57, 308 },
 204        {  58, 306 },
 205        {  59, 304 },
 206        {  60, 302 },
 207        {  61, 300 },
 208        {  62, 298 },
 209        {  65, 295 },
 210        {  68, 294 },
 211        {  70, 293 },
 212        {  73, 292 },
 213        {  76, 291 },
 214        {  78, 290 },
 215        {  79, 289 },
 216        {  81, 288 },
 217        {  82, 287 },
 218        {  83, 286 },
 219        {  84, 285 },
 220        {  85, 284 },
 221        {  86, 283 },
 222        {  88, 282 },
 223        {  89, 281 },
 224        { 256, 280 },
 225};
 226
 227static int au8522_mse2snr_lookup(struct mse2snr_tab *tab, int sz, int mse,
 228                                 u16 *snr)
 229{
 230        int i, ret = -EINVAL;
 231        dprintk("%s()\n", __func__);
 232
 233        for (i = 0; i < sz; i++) {
 234                if (mse < tab[i].val) {
 235                        *snr = tab[i].data;
 236                        ret = 0;
 237                        break;
 238                }
 239        }
 240        dprintk("%s() snr=%d\n", __func__, *snr);
 241        return ret;
 242}
 243
 244static int au8522_set_if(struct dvb_frontend *fe, enum au8522_if_freq if_freq)
 245{
 246        struct au8522_state *state = fe->demodulator_priv;
 247        u8 r0b5, r0b6, r0b7;
 248        char *ifmhz;
 249
 250        switch (if_freq) {
 251        case AU8522_IF_3_25MHZ:
 252                ifmhz = "3.25";
 253                r0b5 = 0x00;
 254                r0b6 = 0x3d;
 255                r0b7 = 0xa0;
 256                break;
 257        case AU8522_IF_4MHZ:
 258                ifmhz = "4.00";
 259                r0b5 = 0x00;
 260                r0b6 = 0x4b;
 261                r0b7 = 0xd9;
 262                break;
 263        case AU8522_IF_6MHZ:
 264                ifmhz = "6.00";
 265                r0b5 = 0xfb;
 266                r0b6 = 0x8e;
 267                r0b7 = 0x39;
 268                break;
 269        default:
 270                dprintk("%s() IF Frequency not supported\n", __func__);
 271                return -EINVAL;
 272        }
 273        dprintk("%s() %s MHz\n", __func__, ifmhz);
 274        au8522_writereg(state, 0x80b5, r0b5);
 275        au8522_writereg(state, 0x80b6, r0b6);
 276        au8522_writereg(state, 0x80b7, r0b7);
 277
 278        return 0;
 279}
 280
 281/* VSB Modulation table */
 282static struct {
 283        u16 reg;
 284        u16 data;
 285} VSB_mod_tab[] = {
 286        { 0x8090, 0x84 },
 287        { 0x4092, 0x11 },
 288        { 0x2005, 0x00 },
 289        { 0x8091, 0x80 },
 290        { 0x80a3, 0x0c },
 291        { 0x80a4, 0xe8 },
 292        { 0x8081, 0xc4 },
 293        { 0x80a5, 0x40 },
 294        { 0x80a7, 0x40 },
 295        { 0x80a6, 0x67 },
 296        { 0x8262, 0x20 },
 297        { 0x821c, 0x30 },
 298        { 0x80d8, 0x1a },
 299        { 0x8227, 0xa0 },
 300        { 0x8121, 0xff },
 301        { 0x80a8, 0xf0 },
 302        { 0x80a9, 0x05 },
 303        { 0x80aa, 0x77 },
 304        { 0x80ab, 0xf0 },
 305        { 0x80ac, 0x05 },
 306        { 0x80ad, 0x77 },
 307        { 0x80ae, 0x41 },
 308        { 0x80af, 0x66 },
 309        { 0x821b, 0xcc },
 310        { 0x821d, 0x80 },
 311        { 0x80a4, 0xe8 },
 312        { 0x8231, 0x13 },
 313};
 314
 315/* QAM64 Modulation table */
 316static struct {
 317        u16 reg;
 318        u16 data;
 319} QAM64_mod_tab[] = {
 320        { 0x00a3, 0x09 },
 321        { 0x00a4, 0x00 },
 322        { 0x0081, 0xc4 },
 323        { 0x00a5, 0x40 },
 324        { 0x00aa, 0x77 },
 325        { 0x00ad, 0x77 },
 326        { 0x00a6, 0x67 },
 327        { 0x0262, 0x20 },
 328        { 0x021c, 0x30 },
 329        { 0x00b8, 0x3e },
 330        { 0x00b9, 0xf0 },
 331        { 0x00ba, 0x01 },
 332        { 0x00bb, 0x18 },
 333        { 0x00bc, 0x50 },
 334        { 0x00bd, 0x00 },
 335        { 0x00be, 0xea },
 336        { 0x00bf, 0xef },
 337        { 0x00c0, 0xfc },
 338        { 0x00c1, 0xbd },
 339        { 0x00c2, 0x1f },
 340        { 0x00c3, 0xfc },
 341        { 0x00c4, 0xdd },
 342        { 0x00c5, 0xaf },
 343        { 0x00c6, 0x00 },
 344        { 0x00c7, 0x38 },
 345        { 0x00c8, 0x30 },
 346        { 0x00c9, 0x05 },
 347        { 0x00ca, 0x4a },
 348        { 0x00cb, 0xd0 },
 349        { 0x00cc, 0x01 },
 350        { 0x00cd, 0xd9 },
 351        { 0x00ce, 0x6f },
 352        { 0x00cf, 0xf9 },
 353        { 0x00d0, 0x70 },
 354        { 0x00d1, 0xdf },
 355        { 0x00d2, 0xf7 },
 356        { 0x00d3, 0xc2 },
 357        { 0x00d4, 0xdf },
 358        { 0x00d5, 0x02 },
 359        { 0x00d6, 0x9a },
 360        { 0x00d7, 0xd0 },
 361        { 0x0250, 0x0d },
 362        { 0x0251, 0xcd },
 363        { 0x0252, 0xe0 },
 364        { 0x0253, 0x05 },
 365        { 0x0254, 0xa7 },
 366        { 0x0255, 0xff },
 367        { 0x0256, 0xed },
 368        { 0x0257, 0x5b },
 369        { 0x0258, 0xae },
 370        { 0x0259, 0xe6 },
 371        { 0x025a, 0x3d },
 372        { 0x025b, 0x0f },
 373        { 0x025c, 0x0d },
 374        { 0x025d, 0xea },
 375        { 0x025e, 0xf2 },
 376        { 0x025f, 0x51 },
 377        { 0x0260, 0xf5 },
 378        { 0x0261, 0x06 },
 379        { 0x021a, 0x00 },
 380        { 0x0546, 0x40 },
 381        { 0x0210, 0xc7 },
 382        { 0x0211, 0xaa },
 383        { 0x0212, 0xab },
 384        { 0x0213, 0x02 },
 385        { 0x0502, 0x00 },
 386        { 0x0121, 0x04 },
 387        { 0x0122, 0x04 },
 388        { 0x052e, 0x10 },
 389        { 0x00a4, 0xca },
 390        { 0x00a7, 0x40 },
 391        { 0x0526, 0x01 },
 392};
 393
 394/* QAM256 Modulation table */
 395static struct {
 396        u16 reg;
 397        u16 data;
 398} QAM256_mod_tab[] = {
 399        { 0x80a3, 0x09 },
 400        { 0x80a4, 0x00 },
 401        { 0x8081, 0xc4 },
 402        { 0x80a5, 0x40 },
 403        { 0x80aa, 0x77 },
 404        { 0x80ad, 0x77 },
 405        { 0x80a6, 0x67 },
 406        { 0x8262, 0x20 },
 407        { 0x821c, 0x30 },
 408        { 0x80b8, 0x3e },
 409        { 0x80b9, 0xf0 },
 410        { 0x80ba, 0x01 },
 411        { 0x80bb, 0x18 },
 412        { 0x80bc, 0x50 },
 413        { 0x80bd, 0x00 },
 414        { 0x80be, 0xea },
 415        { 0x80bf, 0xef },
 416        { 0x80c0, 0xfc },
 417        { 0x80c1, 0xbd },
 418        { 0x80c2, 0x1f },
 419        { 0x80c3, 0xfc },
 420        { 0x80c4, 0xdd },
 421        { 0x80c5, 0xaf },
 422        { 0x80c6, 0x00 },
 423        { 0x80c7, 0x38 },
 424        { 0x80c8, 0x30 },
 425        { 0x80c9, 0x05 },
 426        { 0x80ca, 0x4a },
 427        { 0x80cb, 0xd0 },
 428        { 0x80cc, 0x01 },
 429        { 0x80cd, 0xd9 },
 430        { 0x80ce, 0x6f },
 431        { 0x80cf, 0xf9 },
 432        { 0x80d0, 0x70 },
 433        { 0x80d1, 0xdf },
 434        { 0x80d2, 0xf7 },
 435        { 0x80d3, 0xc2 },
 436        { 0x80d4, 0xdf },
 437        { 0x80d5, 0x02 },
 438        { 0x80d6, 0x9a },
 439        { 0x80d7, 0xd0 },
 440        { 0x8250, 0x0d },
 441        { 0x8251, 0xcd },
 442        { 0x8252, 0xe0 },
 443        { 0x8253, 0x05 },
 444        { 0x8254, 0xa7 },
 445        { 0x8255, 0xff },
 446        { 0x8256, 0xed },
 447        { 0x8257, 0x5b },
 448        { 0x8258, 0xae },
 449        { 0x8259, 0xe6 },
 450        { 0x825a, 0x3d },
 451        { 0x825b, 0x0f },
 452        { 0x825c, 0x0d },
 453        { 0x825d, 0xea },
 454        { 0x825e, 0xf2 },
 455        { 0x825f, 0x51 },
 456        { 0x8260, 0xf5 },
 457        { 0x8261, 0x06 },
 458        { 0x821a, 0x00 },
 459        { 0x8546, 0x40 },
 460        { 0x8210, 0x26 },
 461        { 0x8211, 0xf6 },
 462        { 0x8212, 0x84 },
 463        { 0x8213, 0x02 },
 464        { 0x8502, 0x01 },
 465        { 0x8121, 0x04 },
 466        { 0x8122, 0x04 },
 467        { 0x852e, 0x10 },
 468        { 0x80a4, 0xca },
 469        { 0x80a7, 0x40 },
 470        { 0x8526, 0x01 },
 471};
 472
 473static struct {
 474        u16 reg;
 475        u16 data;
 476} QAM256_mod_tab_zv_mode[] = {
 477        { 0x80a3, 0x09 },
 478        { 0x80a4, 0x00 },
 479        { 0x8081, 0xc4 },
 480        { 0x80a5, 0x40 },
 481        { 0x80b5, 0xfb },
 482        { 0x80b6, 0x8e },
 483        { 0x80b7, 0x39 },
 484        { 0x80aa, 0x77 },
 485        { 0x80ad, 0x77 },
 486        { 0x80a6, 0x67 },
 487        { 0x8262, 0x20 },
 488        { 0x821c, 0x30 },
 489        { 0x80b8, 0x3e },
 490        { 0x80b9, 0xf0 },
 491        { 0x80ba, 0x01 },
 492        { 0x80bb, 0x18 },
 493        { 0x80bc, 0x50 },
 494        { 0x80bd, 0x00 },
 495        { 0x80be, 0xea },
 496        { 0x80bf, 0xef },
 497        { 0x80c0, 0xfc },
 498        { 0x80c1, 0xbd },
 499        { 0x80c2, 0x1f },
 500        { 0x80c3, 0xfc },
 501        { 0x80c4, 0xdd },
 502        { 0x80c5, 0xaf },
 503        { 0x80c6, 0x00 },
 504        { 0x80c7, 0x38 },
 505        { 0x80c8, 0x30 },
 506        { 0x80c9, 0x05 },
 507        { 0x80ca, 0x4a },
 508        { 0x80cb, 0xd0 },
 509        { 0x80cc, 0x01 },
 510        { 0x80cd, 0xd9 },
 511        { 0x80ce, 0x6f },
 512        { 0x80cf, 0xf9 },
 513        { 0x80d0, 0x70 },
 514        { 0x80d1, 0xdf },
 515        { 0x80d2, 0xf7 },
 516        { 0x80d3, 0xc2 },
 517        { 0x80d4, 0xdf },
 518        { 0x80d5, 0x02 },
 519        { 0x80d6, 0x9a },
 520        { 0x80d7, 0xd0 },
 521        { 0x8250, 0x0d },
 522        { 0x8251, 0xcd },
 523        { 0x8252, 0xe0 },
 524        { 0x8253, 0x05 },
 525        { 0x8254, 0xa7 },
 526        { 0x8255, 0xff },
 527        { 0x8256, 0xed },
 528        { 0x8257, 0x5b },
 529        { 0x8258, 0xae },
 530        { 0x8259, 0xe6 },
 531        { 0x825a, 0x3d },
 532        { 0x825b, 0x0f },
 533        { 0x825c, 0x0d },
 534        { 0x825d, 0xea },
 535        { 0x825e, 0xf2 },
 536        { 0x825f, 0x51 },
 537        { 0x8260, 0xf5 },
 538        { 0x8261, 0x06 },
 539        { 0x821a, 0x01 },
 540        { 0x8546, 0x40 },
 541        { 0x8210, 0x26 },
 542        { 0x8211, 0xf6 },
 543        { 0x8212, 0x84 },
 544        { 0x8213, 0x02 },
 545        { 0x8502, 0x01 },
 546        { 0x8121, 0x04 },
 547        { 0x8122, 0x04 },
 548        { 0x852e, 0x10 },
 549        { 0x80a4, 0xca },
 550        { 0x80a7, 0x40 },
 551        { 0x8526, 0x01 },
 552};
 553
 554static int au8522_enable_modulation(struct dvb_frontend *fe,
 555                                    enum fe_modulation m)
 556{
 557        struct au8522_state *state = fe->demodulator_priv;
 558        int i;
 559
 560        dprintk("%s(0x%08x)\n", __func__, m);
 561
 562        switch (m) {
 563        case VSB_8:
 564                dprintk("%s() VSB_8\n", __func__);
 565                for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
 566                        au8522_writereg(state,
 567                                VSB_mod_tab[i].reg,
 568                                VSB_mod_tab[i].data);
 569                au8522_set_if(fe, state->config.vsb_if);
 570                break;
 571        case QAM_64:
 572                dprintk("%s() QAM 64\n", __func__);
 573                for (i = 0; i < ARRAY_SIZE(QAM64_mod_tab); i++)
 574                        au8522_writereg(state,
 575                                QAM64_mod_tab[i].reg,
 576                                QAM64_mod_tab[i].data);
 577                au8522_set_if(fe, state->config.qam_if);
 578                break;
 579        case QAM_256:
 580                if (zv_mode) {
 581                        dprintk("%s() QAM 256 (zv_mode)\n", __func__);
 582                        for (i = 0; i < ARRAY_SIZE(QAM256_mod_tab_zv_mode); i++)
 583                                au8522_writereg(state,
 584                                        QAM256_mod_tab_zv_mode[i].reg,
 585                                        QAM256_mod_tab_zv_mode[i].data);
 586                        au8522_set_if(fe, state->config.qam_if);
 587                        msleep(100);
 588                        au8522_writereg(state, 0x821a, 0x00);
 589                } else {
 590                        dprintk("%s() QAM 256\n", __func__);
 591                        for (i = 0; i < ARRAY_SIZE(QAM256_mod_tab); i++)
 592                                au8522_writereg(state,
 593                                        QAM256_mod_tab[i].reg,
 594                                        QAM256_mod_tab[i].data);
 595                        au8522_set_if(fe, state->config.qam_if);
 596                }
 597                break;
 598        default:
 599                dprintk("%s() Invalid modulation\n", __func__);
 600                return -EINVAL;
 601        }
 602
 603        state->current_modulation = m;
 604
 605        return 0;
 606}
 607
 608/* Talk to the demod, set the FEC, GUARD, QAM settings etc */
 609static int au8522_set_frontend(struct dvb_frontend *fe)
 610{
 611        struct dtv_frontend_properties *c = &fe->dtv_property_cache;
 612        struct au8522_state *state = fe->demodulator_priv;
 613        int ret = -EINVAL;
 614
 615        dprintk("%s(frequency=%d)\n", __func__, c->frequency);
 616
 617        if ((state->current_frequency == c->frequency) &&
 618            (state->current_modulation == c->modulation))
 619                return 0;
 620
 621        if (fe->ops.tuner_ops.set_params) {
 622                if (fe->ops.i2c_gate_ctrl)
 623                        fe->ops.i2c_gate_ctrl(fe, 1);
 624                ret = fe->ops.tuner_ops.set_params(fe);
 625                if (fe->ops.i2c_gate_ctrl)
 626                        fe->ops.i2c_gate_ctrl(fe, 0);
 627        }
 628
 629        if (ret < 0)
 630                return ret;
 631
 632        /* Allow the tuner to settle */
 633        if (zv_mode) {
 634                dprintk("%s() increase tuner settling time for zv_mode\n",
 635                        __func__);
 636                msleep(250);
 637        } else
 638                msleep(100);
 639
 640        au8522_enable_modulation(fe, c->modulation);
 641
 642        state->current_frequency = c->frequency;
 643
 644        return 0;
 645}
 646
 647static int au8522_read_status(struct dvb_frontend *fe, enum fe_status *status)
 648{
 649        struct au8522_state *state = fe->demodulator_priv;
 650        u8 reg;
 651        u32 tuner_status = 0;
 652
 653        *status = 0;
 654
 655        if (state->current_modulation == VSB_8) {
 656                dprintk("%s() Checking VSB_8\n", __func__);
 657                reg = au8522_readreg(state, 0x4088);
 658                if ((reg & 0x03) == 0x03)
 659                        *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
 660        } else {
 661                dprintk("%s() Checking QAM\n", __func__);
 662                reg = au8522_readreg(state, 0x4541);
 663                if (reg & 0x80)
 664                        *status |= FE_HAS_VITERBI;
 665                if (reg & 0x20)
 666                        *status |= FE_HAS_LOCK | FE_HAS_SYNC;
 667        }
 668
 669        switch (state->config.status_mode) {
 670        case AU8522_DEMODLOCKING:
 671                dprintk("%s() DEMODLOCKING\n", __func__);
 672                if (*status & FE_HAS_VITERBI)
 673                        *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
 674                break;
 675        case AU8522_TUNERLOCKING:
 676                /* Get the tuner status */
 677                dprintk("%s() TUNERLOCKING\n", __func__);
 678                if (fe->ops.tuner_ops.get_status) {
 679                        if (fe->ops.i2c_gate_ctrl)
 680                                fe->ops.i2c_gate_ctrl(fe, 1);
 681
 682                        fe->ops.tuner_ops.get_status(fe, &tuner_status);
 683
 684                        if (fe->ops.i2c_gate_ctrl)
 685                                fe->ops.i2c_gate_ctrl(fe, 0);
 686                }
 687                if (tuner_status)
 688                        *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
 689                break;
 690        }
 691        state->fe_status = *status;
 692
 693        if (*status & FE_HAS_LOCK)
 694                /* turn on LED, if it isn't on already */
 695                au8522_led_ctrl(state, -1);
 696        else
 697                /* turn off LED */
 698                au8522_led_ctrl(state, 0);
 699
 700        dprintk("%s() status 0x%08x\n", __func__, *status);
 701
 702        return 0;
 703}
 704
 705static int au8522_led_status(struct au8522_state *state, const u16 *snr)
 706{
 707        struct au8522_led_config *led_config = state->config.led_cfg;
 708        int led;
 709        u16 strong;
 710
 711        /* bail out if we can't control an LED */
 712        if (!led_config)
 713                return 0;
 714
 715        if (0 == (state->fe_status & FE_HAS_LOCK))
 716                return au8522_led_ctrl(state, 0);
 717        else if (state->current_modulation == QAM_256)
 718                strong = led_config->qam256_strong;
 719        else if (state->current_modulation == QAM_64)
 720                strong = led_config->qam64_strong;
 721        else /* (state->current_modulation == VSB_8) */
 722                strong = led_config->vsb8_strong;
 723
 724        if (*snr >= strong)
 725                led = 2;
 726        else
 727                led = 1;
 728
 729        if ((state->led_state) &&
 730            (((strong < *snr) ? (*snr - strong) : (strong - *snr)) <= 10))
 731                /* snr didn't change enough to bother
 732                 * changing the color of the led */
 733                return 0;
 734
 735        return au8522_led_ctrl(state, led);
 736}
 737
 738static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
 739{
 740        struct au8522_state *state = fe->demodulator_priv;
 741        int ret = -EINVAL;
 742
 743        dprintk("%s()\n", __func__);
 744
 745        if (state->current_modulation == QAM_256)
 746                ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
 747                                            ARRAY_SIZE(qam256_mse2snr_tab),
 748                                            au8522_readreg(state, 0x4522),
 749                                            snr);
 750        else if (state->current_modulation == QAM_64)
 751                ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
 752                                            ARRAY_SIZE(qam64_mse2snr_tab),
 753                                            au8522_readreg(state, 0x4522),
 754                                            snr);
 755        else /* VSB_8 */
 756                ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
 757                                            ARRAY_SIZE(vsb_mse2snr_tab),
 758                                            au8522_readreg(state, 0x4311),
 759                                            snr);
 760
 761        if (state->config.led_cfg)
 762                au8522_led_status(state, snr);
 763
 764        return ret;
 765}
 766
 767static int au8522_read_signal_strength(struct dvb_frontend *fe,
 768                                       u16 *signal_strength)
 769{
 770        /* borrowed from lgdt330x.c
 771         *
 772         * Calculate strength from SNR up to 35dB
 773         * Even though the SNR can go higher than 35dB,
 774         * there is some comfort factor in having a range of
 775         * strong signals that can show at 100%
 776         */
 777        u16 snr;
 778        u32 tmp;
 779        int ret = au8522_read_snr(fe, &snr);
 780
 781        *signal_strength = 0;
 782
 783        if (0 == ret) {
 784                /* The following calculation method was chosen
 785                 * purely for the sake of code re-use from the
 786                 * other demod drivers that use this method */
 787
 788                /* Convert from SNR in dB * 10 to 8.24 fixed-point */
 789                tmp = (snr * ((1 << 24) / 10));
 790
 791                /* Convert from 8.24 fixed-point to
 792                 * scale the range 0 - 35*2^24 into 0 - 65535*/
 793                if (tmp >= 8960 * 0x10000)
 794                        *signal_strength = 0xffff;
 795                else
 796                        *signal_strength = tmp / 8960;
 797        }
 798
 799        return ret;
 800}
 801
 802static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
 803{
 804        struct au8522_state *state = fe->demodulator_priv;
 805
 806        if (state->current_modulation == VSB_8)
 807                *ucblocks = au8522_readreg(state, 0x4087);
 808        else
 809                *ucblocks = au8522_readreg(state, 0x4543);
 810
 811        return 0;
 812}
 813
 814static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
 815{
 816        return au8522_read_ucblocks(fe, ber);
 817}
 818
 819static int au8522_get_frontend(struct dvb_frontend *fe,
 820                               struct dtv_frontend_properties *c)
 821{
 822        struct au8522_state *state = fe->demodulator_priv;
 823
 824        c->frequency = state->current_frequency;
 825        c->modulation = state->current_modulation;
 826
 827        return 0;
 828}
 829
 830static int au8522_get_tune_settings(struct dvb_frontend *fe,
 831                                    struct dvb_frontend_tune_settings *tune)
 832{
 833        tune->min_delay_ms = 1000;
 834        return 0;
 835}
 836
 837static const struct dvb_frontend_ops au8522_ops;
 838
 839
 840static void au8522_release(struct dvb_frontend *fe)
 841{
 842        struct au8522_state *state = fe->demodulator_priv;
 843        au8522_release_state(state);
 844}
 845
 846struct dvb_frontend *au8522_attach(const struct au8522_config *config,
 847                                   struct i2c_adapter *i2c)
 848{
 849        struct au8522_state *state = NULL;
 850        int instance;
 851
 852        /* allocate memory for the internal state */
 853        instance = au8522_get_state(&state, i2c, config->demod_address);
 854        switch (instance) {
 855        case 0:
 856                dprintk("%s state allocation failed\n", __func__);
 857                break;
 858        case 1:
 859                /* new demod instance */
 860                dprintk("%s using new instance\n", __func__);
 861                break;
 862        default:
 863                /* existing demod instance */
 864                dprintk("%s using existing instance\n", __func__);
 865                break;
 866        }
 867
 868        /* setup the state */
 869        state->config = *config;
 870        state->i2c = i2c;
 871        state->operational_mode = AU8522_DIGITAL_MODE;
 872
 873        /* create dvb_frontend */
 874        memcpy(&state->frontend.ops, &au8522_ops,
 875               sizeof(struct dvb_frontend_ops));
 876        state->frontend.demodulator_priv = state;
 877
 878        state->frontend.ops.analog_ops.i2c_gate_ctrl = au8522_analog_i2c_gate_ctrl;
 879
 880        if (au8522_init(&state->frontend) != 0) {
 881                printk(KERN_ERR "%s: Failed to initialize correctly\n",
 882                        __func__);
 883                goto error;
 884        }
 885
 886        /* Note: Leaving the I2C gate open here. */
 887        au8522_i2c_gate_ctrl(&state->frontend, 1);
 888
 889        return &state->frontend;
 890
 891error:
 892        au8522_release_state(state);
 893        return NULL;
 894}
 895EXPORT_SYMBOL(au8522_attach);
 896
 897static const struct dvb_frontend_ops au8522_ops = {
 898        .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
 899        .info = {
 900                .name                   = "Auvitek AU8522 QAM/8VSB Frontend",
 901                .frequency_min          = 54000000,
 902                .frequency_max          = 858000000,
 903                .frequency_stepsize     = 62500,
 904                .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
 905        },
 906
 907        .init                 = au8522_init,
 908        .sleep                = au8522_sleep,
 909        .i2c_gate_ctrl        = au8522_i2c_gate_ctrl,
 910        .set_frontend         = au8522_set_frontend,
 911        .get_frontend         = au8522_get_frontend,
 912        .get_tune_settings    = au8522_get_tune_settings,
 913        .read_status          = au8522_read_status,
 914        .read_ber             = au8522_read_ber,
 915        .read_signal_strength = au8522_read_signal_strength,
 916        .read_snr             = au8522_read_snr,
 917        .read_ucblocks        = au8522_read_ucblocks,
 918        .release              = au8522_release,
 919};
 920
 921module_param(debug, int, 0644);
 922MODULE_PARM_DESC(debug, "Enable verbose debug messages");
 923
 924module_param(zv_mode, int, 0644);
 925MODULE_PARM_DESC(zv_mode, "Turn on/off ZeeVee modulator compatibility mode (default:on).\n"
 926        "\t\ton - modified AU8522 QAM256 initialization.\n"
 927        "\t\tProvides faster lock when using ZeeVee modulator based sources");
 928
 929MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
 930MODULE_AUTHOR("Steven Toth");
 931MODULE_LICENSE("GPL");
 932