Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003     Auvitek AU8522 QAM/8VSB demodulator driver
0004 
0005     Copyright (C) 2008 Steven Toth <stoth@linuxtv.org>
0006 
0007 
0008 */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/init.h>
0012 #include <linux/module.h>
0013 #include <linux/string.h>
0014 #include <linux/delay.h>
0015 #include <media/dvb_frontend.h>
0016 #include "au8522.h"
0017 #include "au8522_priv.h"
0018 
0019 static int debug;
0020 static int zv_mode = 1; /* default to on */
0021 
0022 #define dprintk(arg...)\
0023     do { if (debug)\
0024         printk(arg);\
0025     } while (0)
0026 
0027 struct mse2snr_tab {
0028     u16 val;
0029     u16 data;
0030 };
0031 
0032 /* VSB SNR lookup table */
0033 static struct mse2snr_tab vsb_mse2snr_tab[] = {
0034     {   0, 270 },
0035     {   2, 250 },
0036     {   3, 240 },
0037     {   5, 230 },
0038     {   7, 220 },
0039     {   9, 210 },
0040     {  12, 200 },
0041     {  13, 195 },
0042     {  15, 190 },
0043     {  17, 185 },
0044     {  19, 180 },
0045     {  21, 175 },
0046     {  24, 170 },
0047     {  27, 165 },
0048     {  31, 160 },
0049     {  32, 158 },
0050     {  33, 156 },
0051     {  36, 152 },
0052     {  37, 150 },
0053     {  39, 148 },
0054     {  40, 146 },
0055     {  41, 144 },
0056     {  43, 142 },
0057     {  44, 140 },
0058     {  48, 135 },
0059     {  50, 130 },
0060     {  43, 142 },
0061     {  53, 125 },
0062     {  56, 120 },
0063     { 256, 115 },
0064 };
0065 
0066 /* QAM64 SNR lookup table */
0067 static struct mse2snr_tab qam64_mse2snr_tab[] = {
0068     {  15,   0 },
0069     {  16, 290 },
0070     {  17, 288 },
0071     {  18, 286 },
0072     {  19, 284 },
0073     {  20, 282 },
0074     {  21, 281 },
0075     {  22, 279 },
0076     {  23, 277 },
0077     {  24, 275 },
0078     {  25, 273 },
0079     {  26, 271 },
0080     {  27, 269 },
0081     {  28, 268 },
0082     {  29, 266 },
0083     {  30, 264 },
0084     {  31, 262 },
0085     {  32, 260 },
0086     {  33, 259 },
0087     {  34, 258 },
0088     {  35, 256 },
0089     {  36, 255 },
0090     {  37, 254 },
0091     {  38, 252 },
0092     {  39, 251 },
0093     {  40, 250 },
0094     {  41, 249 },
0095     {  42, 248 },
0096     {  43, 246 },
0097     {  44, 245 },
0098     {  45, 244 },
0099     {  46, 242 },
0100     {  47, 241 },
0101     {  48, 240 },
0102     {  50, 239 },
0103     {  51, 238 },
0104     {  53, 237 },
0105     {  54, 236 },
0106     {  56, 235 },
0107     {  57, 234 },
0108     {  59, 233 },
0109     {  60, 232 },
0110     {  62, 231 },
0111     {  63, 230 },
0112     {  65, 229 },
0113     {  67, 228 },
0114     {  68, 227 },
0115     {  70, 226 },
0116     {  71, 225 },
0117     {  73, 224 },
0118     {  74, 223 },
0119     {  76, 222 },
0120     {  78, 221 },
0121     {  80, 220 },
0122     {  82, 219 },
0123     {  85, 218 },
0124     {  88, 217 },
0125     {  90, 216 },
0126     {  92, 215 },
0127     {  93, 214 },
0128     {  94, 212 },
0129     {  95, 211 },
0130     {  97, 210 },
0131     {  99, 209 },
0132     { 101, 208 },
0133     { 102, 207 },
0134     { 104, 206 },
0135     { 107, 205 },
0136     { 111, 204 },
0137     { 114, 203 },
0138     { 118, 202 },
0139     { 122, 201 },
0140     { 125, 200 },
0141     { 128, 199 },
0142     { 130, 198 },
0143     { 132, 197 },
0144     { 256, 190 },
0145 };
0146 
0147 /* QAM256 SNR lookup table */
0148 static struct mse2snr_tab qam256_mse2snr_tab[] = {
0149     {  15,   0 },
0150     {  16, 400 },
0151     {  17, 398 },
0152     {  18, 396 },
0153     {  19, 394 },
0154     {  20, 392 },
0155     {  21, 390 },
0156     {  22, 388 },
0157     {  23, 386 },
0158     {  24, 384 },
0159     {  25, 382 },
0160     {  26, 380 },
0161     {  27, 379 },
0162     {  28, 378 },
0163     {  29, 377 },
0164     {  30, 376 },
0165     {  31, 375 },
0166     {  32, 374 },
0167     {  33, 373 },
0168     {  34, 372 },
0169     {  35, 371 },
0170     {  36, 370 },
0171     {  37, 362 },
0172     {  38, 354 },
0173     {  39, 346 },
0174     {  40, 338 },
0175     {  41, 330 },
0176     {  42, 328 },
0177     {  43, 326 },
0178     {  44, 324 },
0179     {  45, 322 },
0180     {  46, 320 },
0181     {  47, 319 },
0182     {  48, 318 },
0183     {  49, 317 },
0184     {  50, 316 },
0185     {  51, 315 },
0186     {  52, 314 },
0187     {  53, 313 },
0188     {  54, 312 },
0189     {  55, 311 },
0190     {  56, 310 },
0191     {  57, 308 },
0192     {  58, 306 },
0193     {  59, 304 },
0194     {  60, 302 },
0195     {  61, 300 },
0196     {  62, 298 },
0197     {  65, 295 },
0198     {  68, 294 },
0199     {  70, 293 },
0200     {  73, 292 },
0201     {  76, 291 },
0202     {  78, 290 },
0203     {  79, 289 },
0204     {  81, 288 },
0205     {  82, 287 },
0206     {  83, 286 },
0207     {  84, 285 },
0208     {  85, 284 },
0209     {  86, 283 },
0210     {  88, 282 },
0211     {  89, 281 },
0212     { 256, 280 },
0213 };
0214 
0215 static int au8522_mse2snr_lookup(struct mse2snr_tab *tab, int sz, int mse,
0216                  u16 *snr)
0217 {
0218     int i, ret = -EINVAL;
0219     dprintk("%s()\n", __func__);
0220 
0221     for (i = 0; i < sz; i++) {
0222         if (mse < tab[i].val) {
0223             *snr = tab[i].data;
0224             ret = 0;
0225             break;
0226         }
0227     }
0228     dprintk("%s() snr=%d\n", __func__, *snr);
0229     return ret;
0230 }
0231 
0232 static int au8522_set_if(struct dvb_frontend *fe, enum au8522_if_freq if_freq)
0233 {
0234     struct au8522_state *state = fe->demodulator_priv;
0235     u8 r0b5, r0b6, r0b7;
0236     char *ifmhz;
0237 
0238     switch (if_freq) {
0239     case AU8522_IF_3_25MHZ:
0240         ifmhz = "3.25";
0241         r0b5 = 0x00;
0242         r0b6 = 0x3d;
0243         r0b7 = 0xa0;
0244         break;
0245     case AU8522_IF_4MHZ:
0246         ifmhz = "4.00";
0247         r0b5 = 0x00;
0248         r0b6 = 0x4b;
0249         r0b7 = 0xd9;
0250         break;
0251     case AU8522_IF_6MHZ:
0252         ifmhz = "6.00";
0253         r0b5 = 0xfb;
0254         r0b6 = 0x8e;
0255         r0b7 = 0x39;
0256         break;
0257     default:
0258         dprintk("%s() IF Frequency not supported\n", __func__);
0259         return -EINVAL;
0260     }
0261     dprintk("%s() %s MHz\n", __func__, ifmhz);
0262     au8522_writereg(state, 0x00b5, r0b5);
0263     au8522_writereg(state, 0x00b6, r0b6);
0264     au8522_writereg(state, 0x00b7, r0b7);
0265 
0266     return 0;
0267 }
0268 
0269 /* VSB Modulation table */
0270 static struct {
0271     u16 reg;
0272     u16 data;
0273 } VSB_mod_tab[] = {
0274     { 0x0090, 0x84 },
0275     { 0x2005, 0x00 },
0276     { 0x0091, 0x80 },
0277     { 0x00a3, 0x0c },
0278     { 0x00a4, 0xe8 },
0279     { 0x0081, 0xc4 },
0280     { 0x00a5, 0x40 },
0281     { 0x00a7, 0x40 },
0282     { 0x00a6, 0x67 },
0283     { 0x0262, 0x20 },
0284     { 0x021c, 0x30 },
0285     { 0x00d8, 0x1a },
0286     { 0x0227, 0xa0 },
0287     { 0x0121, 0xff },
0288     { 0x00a8, 0xf0 },
0289     { 0x00a9, 0x05 },
0290     { 0x00aa, 0x77 },
0291     { 0x00ab, 0xf0 },
0292     { 0x00ac, 0x05 },
0293     { 0x00ad, 0x77 },
0294     { 0x00ae, 0x41 },
0295     { 0x00af, 0x66 },
0296     { 0x021b, 0xcc },
0297     { 0x021d, 0x80 },
0298     { 0x00a4, 0xe8 },
0299     { 0x0231, 0x13 },
0300 };
0301 
0302 /* QAM64 Modulation table */
0303 static struct {
0304     u16 reg;
0305     u16 data;
0306 } QAM64_mod_tab[] = {
0307     { 0x00a3, 0x09 },
0308     { 0x00a4, 0x00 },
0309     { 0x0081, 0xc4 },
0310     { 0x00a5, 0x40 },
0311     { 0x00aa, 0x77 },
0312     { 0x00ad, 0x77 },
0313     { 0x00a6, 0x67 },
0314     { 0x0262, 0x20 },
0315     { 0x021c, 0x30 },
0316     { 0x00b8, 0x3e },
0317     { 0x00b9, 0xf0 },
0318     { 0x00ba, 0x01 },
0319     { 0x00bb, 0x18 },
0320     { 0x00bc, 0x50 },
0321     { 0x00bd, 0x00 },
0322     { 0x00be, 0xea },
0323     { 0x00bf, 0xef },
0324     { 0x00c0, 0xfc },
0325     { 0x00c1, 0xbd },
0326     { 0x00c2, 0x1f },
0327     { 0x00c3, 0xfc },
0328     { 0x00c4, 0xdd },
0329     { 0x00c5, 0xaf },
0330     { 0x00c6, 0x00 },
0331     { 0x00c7, 0x38 },
0332     { 0x00c8, 0x30 },
0333     { 0x00c9, 0x05 },
0334     { 0x00ca, 0x4a },
0335     { 0x00cb, 0xd0 },
0336     { 0x00cc, 0x01 },
0337     { 0x00cd, 0xd9 },
0338     { 0x00ce, 0x6f },
0339     { 0x00cf, 0xf9 },
0340     { 0x00d0, 0x70 },
0341     { 0x00d1, 0xdf },
0342     { 0x00d2, 0xf7 },
0343     { 0x00d3, 0xc2 },
0344     { 0x00d4, 0xdf },
0345     { 0x00d5, 0x02 },
0346     { 0x00d6, 0x9a },
0347     { 0x00d7, 0xd0 },
0348     { 0x0250, 0x0d },
0349     { 0x0251, 0xcd },
0350     { 0x0252, 0xe0 },
0351     { 0x0253, 0x05 },
0352     { 0x0254, 0xa7 },
0353     { 0x0255, 0xff },
0354     { 0x0256, 0xed },
0355     { 0x0257, 0x5b },
0356     { 0x0258, 0xae },
0357     { 0x0259, 0xe6 },
0358     { 0x025a, 0x3d },
0359     { 0x025b, 0x0f },
0360     { 0x025c, 0x0d },
0361     { 0x025d, 0xea },
0362     { 0x025e, 0xf2 },
0363     { 0x025f, 0x51 },
0364     { 0x0260, 0xf5 },
0365     { 0x0261, 0x06 },
0366     { 0x021a, 0x00 },
0367     { 0x0546, 0x40 },
0368     { 0x0210, 0xc7 },
0369     { 0x0211, 0xaa },
0370     { 0x0212, 0xab },
0371     { 0x0213, 0x02 },
0372     { 0x0502, 0x00 },
0373     { 0x0121, 0x04 },
0374     { 0x0122, 0x04 },
0375     { 0x052e, 0x10 },
0376     { 0x00a4, 0xca },
0377     { 0x00a7, 0x40 },
0378     { 0x0526, 0x01 },
0379 };
0380 
0381 /* QAM256 Modulation table */
0382 static struct {
0383     u16 reg;
0384     u16 data;
0385 } QAM256_mod_tab[] = {
0386     { 0x00a3, 0x09 },
0387     { 0x00a4, 0x00 },
0388     { 0x0081, 0xc4 },
0389     { 0x00a5, 0x40 },
0390     { 0x00aa, 0x77 },
0391     { 0x00ad, 0x77 },
0392     { 0x00a6, 0x67 },
0393     { 0x0262, 0x20 },
0394     { 0x021c, 0x30 },
0395     { 0x00b8, 0x3e },
0396     { 0x00b9, 0xf0 },
0397     { 0x00ba, 0x01 },
0398     { 0x00bb, 0x18 },
0399     { 0x00bc, 0x50 },
0400     { 0x00bd, 0x00 },
0401     { 0x00be, 0xea },
0402     { 0x00bf, 0xef },
0403     { 0x00c0, 0xfc },
0404     { 0x00c1, 0xbd },
0405     { 0x00c2, 0x1f },
0406     { 0x00c3, 0xfc },
0407     { 0x00c4, 0xdd },
0408     { 0x00c5, 0xaf },
0409     { 0x00c6, 0x00 },
0410     { 0x00c7, 0x38 },
0411     { 0x00c8, 0x30 },
0412     { 0x00c9, 0x05 },
0413     { 0x00ca, 0x4a },
0414     { 0x00cb, 0xd0 },
0415     { 0x00cc, 0x01 },
0416     { 0x00cd, 0xd9 },
0417     { 0x00ce, 0x6f },
0418     { 0x00cf, 0xf9 },
0419     { 0x00d0, 0x70 },
0420     { 0x00d1, 0xdf },
0421     { 0x00d2, 0xf7 },
0422     { 0x00d3, 0xc2 },
0423     { 0x00d4, 0xdf },
0424     { 0x00d5, 0x02 },
0425     { 0x00d6, 0x9a },
0426     { 0x00d7, 0xd0 },
0427     { 0x0250, 0x0d },
0428     { 0x0251, 0xcd },
0429     { 0x0252, 0xe0 },
0430     { 0x0253, 0x05 },
0431     { 0x0254, 0xa7 },
0432     { 0x0255, 0xff },
0433     { 0x0256, 0xed },
0434     { 0x0257, 0x5b },
0435     { 0x0258, 0xae },
0436     { 0x0259, 0xe6 },
0437     { 0x025a, 0x3d },
0438     { 0x025b, 0x0f },
0439     { 0x025c, 0x0d },
0440     { 0x025d, 0xea },
0441     { 0x025e, 0xf2 },
0442     { 0x025f, 0x51 },
0443     { 0x0260, 0xf5 },
0444     { 0x0261, 0x06 },
0445     { 0x021a, 0x00 },
0446     { 0x0546, 0x40 },
0447     { 0x0210, 0x26 },
0448     { 0x0211, 0xf6 },
0449     { 0x0212, 0x84 },
0450     { 0x0213, 0x02 },
0451     { 0x0502, 0x01 },
0452     { 0x0121, 0x04 },
0453     { 0x0122, 0x04 },
0454     { 0x052e, 0x10 },
0455     { 0x00a4, 0xca },
0456     { 0x00a7, 0x40 },
0457     { 0x0526, 0x01 },
0458 };
0459 
0460 static struct {
0461     u16 reg;
0462     u16 data;
0463 } QAM256_mod_tab_zv_mode[] = {
0464     { 0x80a3, 0x09 },
0465     { 0x80a4, 0x00 },
0466     { 0x8081, 0xc4 },
0467     { 0x80a5, 0x40 },
0468     { 0x80b5, 0xfb },
0469     { 0x80b6, 0x8e },
0470     { 0x80b7, 0x39 },
0471     { 0x80aa, 0x77 },
0472     { 0x80ad, 0x77 },
0473     { 0x80a6, 0x67 },
0474     { 0x8262, 0x20 },
0475     { 0x821c, 0x30 },
0476     { 0x80b8, 0x3e },
0477     { 0x80b9, 0xf0 },
0478     { 0x80ba, 0x01 },
0479     { 0x80bb, 0x18 },
0480     { 0x80bc, 0x50 },
0481     { 0x80bd, 0x00 },
0482     { 0x80be, 0xea },
0483     { 0x80bf, 0xef },
0484     { 0x80c0, 0xfc },
0485     { 0x80c1, 0xbd },
0486     { 0x80c2, 0x1f },
0487     { 0x80c3, 0xfc },
0488     { 0x80c4, 0xdd },
0489     { 0x80c5, 0xaf },
0490     { 0x80c6, 0x00 },
0491     { 0x80c7, 0x38 },
0492     { 0x80c8, 0x30 },
0493     { 0x80c9, 0x05 },
0494     { 0x80ca, 0x4a },
0495     { 0x80cb, 0xd0 },
0496     { 0x80cc, 0x01 },
0497     { 0x80cd, 0xd9 },
0498     { 0x80ce, 0x6f },
0499     { 0x80cf, 0xf9 },
0500     { 0x80d0, 0x70 },
0501     { 0x80d1, 0xdf },
0502     { 0x80d2, 0xf7 },
0503     { 0x80d3, 0xc2 },
0504     { 0x80d4, 0xdf },
0505     { 0x80d5, 0x02 },
0506     { 0x80d6, 0x9a },
0507     { 0x80d7, 0xd0 },
0508     { 0x8250, 0x0d },
0509     { 0x8251, 0xcd },
0510     { 0x8252, 0xe0 },
0511     { 0x8253, 0x05 },
0512     { 0x8254, 0xa7 },
0513     { 0x8255, 0xff },
0514     { 0x8256, 0xed },
0515     { 0x8257, 0x5b },
0516     { 0x8258, 0xae },
0517     { 0x8259, 0xe6 },
0518     { 0x825a, 0x3d },
0519     { 0x825b, 0x0f },
0520     { 0x825c, 0x0d },
0521     { 0x825d, 0xea },
0522     { 0x825e, 0xf2 },
0523     { 0x825f, 0x51 },
0524     { 0x8260, 0xf5 },
0525     { 0x8261, 0x06 },
0526     { 0x821a, 0x01 },
0527     { 0x8546, 0x40 },
0528     { 0x8210, 0x26 },
0529     { 0x8211, 0xf6 },
0530     { 0x8212, 0x84 },
0531     { 0x8213, 0x02 },
0532     { 0x8502, 0x01 },
0533     { 0x8121, 0x04 },
0534     { 0x8122, 0x04 },
0535     { 0x852e, 0x10 },
0536     { 0x80a4, 0xca },
0537     { 0x80a7, 0x40 },
0538     { 0x8526, 0x01 },
0539 };
0540 
0541 static int au8522_enable_modulation(struct dvb_frontend *fe,
0542                     enum fe_modulation m)
0543 {
0544     struct au8522_state *state = fe->demodulator_priv;
0545     int i;
0546 
0547     dprintk("%s(0x%08x)\n", __func__, m);
0548 
0549     switch (m) {
0550     case VSB_8:
0551         dprintk("%s() VSB_8\n", __func__);
0552         for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
0553             au8522_writereg(state,
0554                 VSB_mod_tab[i].reg,
0555                 VSB_mod_tab[i].data);
0556         au8522_set_if(fe, state->config.vsb_if);
0557         break;
0558     case QAM_64:
0559         dprintk("%s() QAM 64\n", __func__);
0560         for (i = 0; i < ARRAY_SIZE(QAM64_mod_tab); i++)
0561             au8522_writereg(state,
0562                 QAM64_mod_tab[i].reg,
0563                 QAM64_mod_tab[i].data);
0564         au8522_set_if(fe, state->config.qam_if);
0565         break;
0566     case QAM_256:
0567         if (zv_mode) {
0568             dprintk("%s() QAM 256 (zv_mode)\n", __func__);
0569             for (i = 0; i < ARRAY_SIZE(QAM256_mod_tab_zv_mode); i++)
0570                 au8522_writereg(state,
0571                     QAM256_mod_tab_zv_mode[i].reg,
0572                     QAM256_mod_tab_zv_mode[i].data);
0573             au8522_set_if(fe, state->config.qam_if);
0574             msleep(100);
0575             au8522_writereg(state, 0x821a, 0x00);
0576         } else {
0577             dprintk("%s() QAM 256\n", __func__);
0578             for (i = 0; i < ARRAY_SIZE(QAM256_mod_tab); i++)
0579                 au8522_writereg(state,
0580                     QAM256_mod_tab[i].reg,
0581                     QAM256_mod_tab[i].data);
0582             au8522_set_if(fe, state->config.qam_if);
0583         }
0584         break;
0585     default:
0586         dprintk("%s() Invalid modulation\n", __func__);
0587         return -EINVAL;
0588     }
0589 
0590     state->current_modulation = m;
0591 
0592     return 0;
0593 }
0594 
0595 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
0596 static int au8522_set_frontend(struct dvb_frontend *fe)
0597 {
0598     struct dtv_frontend_properties *c = &fe->dtv_property_cache;
0599     struct au8522_state *state = fe->demodulator_priv;
0600     int ret = -EINVAL;
0601 
0602     dprintk("%s(frequency=%d)\n", __func__, c->frequency);
0603 
0604     if ((state->current_frequency == c->frequency) &&
0605         (state->current_modulation == c->modulation))
0606         return 0;
0607 
0608     if (fe->ops.tuner_ops.set_params) {
0609         if (fe->ops.i2c_gate_ctrl)
0610             fe->ops.i2c_gate_ctrl(fe, 1);
0611         ret = fe->ops.tuner_ops.set_params(fe);
0612         if (fe->ops.i2c_gate_ctrl)
0613             fe->ops.i2c_gate_ctrl(fe, 0);
0614     }
0615 
0616     if (ret < 0)
0617         return ret;
0618 
0619     /* Allow the tuner to settle */
0620     if (zv_mode) {
0621         dprintk("%s() increase tuner settling time for zv_mode\n",
0622             __func__);
0623         msleep(250);
0624     } else
0625         msleep(100);
0626 
0627     au8522_enable_modulation(fe, c->modulation);
0628 
0629     state->current_frequency = c->frequency;
0630 
0631     return 0;
0632 }
0633 
0634 static int au8522_read_status(struct dvb_frontend *fe, enum fe_status *status)
0635 {
0636     struct au8522_state *state = fe->demodulator_priv;
0637     u8 reg;
0638     u32 tuner_status = 0;
0639 
0640     *status = 0;
0641 
0642     if (state->current_modulation == VSB_8) {
0643         dprintk("%s() Checking VSB_8\n", __func__);
0644         reg = au8522_readreg(state, 0x0088);
0645         if ((reg & 0x03) == 0x03)
0646             *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
0647     } else {
0648         dprintk("%s() Checking QAM\n", __func__);
0649         reg = au8522_readreg(state, 0x0541);
0650         if (reg & 0x80)
0651             *status |= FE_HAS_VITERBI;
0652         if (reg & 0x20)
0653             *status |= FE_HAS_LOCK | FE_HAS_SYNC;
0654     }
0655 
0656     switch (state->config.status_mode) {
0657     case AU8522_DEMODLOCKING:
0658         dprintk("%s() DEMODLOCKING\n", __func__);
0659         if (*status & FE_HAS_VITERBI)
0660             *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
0661         break;
0662     case AU8522_TUNERLOCKING:
0663         /* Get the tuner status */
0664         dprintk("%s() TUNERLOCKING\n", __func__);
0665         if (fe->ops.tuner_ops.get_status) {
0666             if (fe->ops.i2c_gate_ctrl)
0667                 fe->ops.i2c_gate_ctrl(fe, 1);
0668 
0669             fe->ops.tuner_ops.get_status(fe, &tuner_status);
0670 
0671             if (fe->ops.i2c_gate_ctrl)
0672                 fe->ops.i2c_gate_ctrl(fe, 0);
0673         }
0674         if (tuner_status)
0675             *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
0676         break;
0677     }
0678     state->fe_status = *status;
0679 
0680     if (*status & FE_HAS_LOCK)
0681         /* turn on LED, if it isn't on already */
0682         au8522_led_ctrl(state, -1);
0683     else
0684         /* turn off LED */
0685         au8522_led_ctrl(state, 0);
0686 
0687     dprintk("%s() status 0x%08x\n", __func__, *status);
0688 
0689     return 0;
0690 }
0691 
0692 static int au8522_led_status(struct au8522_state *state, const u16 *snr)
0693 {
0694     struct au8522_led_config *led_config = state->config.led_cfg;
0695     int led;
0696     u16 strong;
0697 
0698     /* bail out if we can't control an LED */
0699     if (!led_config)
0700         return 0;
0701 
0702     if (0 == (state->fe_status & FE_HAS_LOCK))
0703         return au8522_led_ctrl(state, 0);
0704     else if (state->current_modulation == QAM_256)
0705         strong = led_config->qam256_strong;
0706     else if (state->current_modulation == QAM_64)
0707         strong = led_config->qam64_strong;
0708     else /* (state->current_modulation == VSB_8) */
0709         strong = led_config->vsb8_strong;
0710 
0711     if (*snr >= strong)
0712         led = 2;
0713     else
0714         led = 1;
0715 
0716     if ((state->led_state) &&
0717         (((strong < *snr) ? (*snr - strong) : (strong - *snr)) <= 10))
0718         /* snr didn't change enough to bother
0719          * changing the color of the led */
0720         return 0;
0721 
0722     return au8522_led_ctrl(state, led);
0723 }
0724 
0725 static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
0726 {
0727     struct au8522_state *state = fe->demodulator_priv;
0728     int ret = -EINVAL;
0729 
0730     dprintk("%s()\n", __func__);
0731 
0732     if (state->current_modulation == QAM_256)
0733         ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
0734                         ARRAY_SIZE(qam256_mse2snr_tab),
0735                         au8522_readreg(state, 0x0522),
0736                         snr);
0737     else if (state->current_modulation == QAM_64)
0738         ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
0739                         ARRAY_SIZE(qam64_mse2snr_tab),
0740                         au8522_readreg(state, 0x0522),
0741                         snr);
0742     else /* VSB_8 */
0743         ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
0744                         ARRAY_SIZE(vsb_mse2snr_tab),
0745                         au8522_readreg(state, 0x0311),
0746                         snr);
0747 
0748     if (state->config.led_cfg)
0749         au8522_led_status(state, snr);
0750 
0751     return ret;
0752 }
0753 
0754 static int au8522_read_signal_strength(struct dvb_frontend *fe,
0755                        u16 *signal_strength)
0756 {
0757     /* borrowed from lgdt330x.c
0758      *
0759      * Calculate strength from SNR up to 35dB
0760      * Even though the SNR can go higher than 35dB,
0761      * there is some comfort factor in having a range of
0762      * strong signals that can show at 100%
0763      */
0764     u16 snr;
0765     u32 tmp;
0766     int ret = au8522_read_snr(fe, &snr);
0767 
0768     *signal_strength = 0;
0769 
0770     if (0 == ret) {
0771         /* The following calculation method was chosen
0772          * purely for the sake of code re-use from the
0773          * other demod drivers that use this method */
0774 
0775         /* Convert from SNR in dB * 10 to 8.24 fixed-point */
0776         tmp = (snr * ((1 << 24) / 10));
0777 
0778         /* Convert from 8.24 fixed-point to
0779          * scale the range 0 - 35*2^24 into 0 - 65535*/
0780         if (tmp >= 8960 * 0x10000)
0781             *signal_strength = 0xffff;
0782         else
0783             *signal_strength = tmp / 8960;
0784     }
0785 
0786     return ret;
0787 }
0788 
0789 static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
0790 {
0791     struct au8522_state *state = fe->demodulator_priv;
0792 
0793     if (state->current_modulation == VSB_8)
0794         *ucblocks = au8522_readreg(state, 0x0087);
0795     else
0796         *ucblocks = au8522_readreg(state, 0x0543);
0797 
0798     return 0;
0799 }
0800 
0801 static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
0802 {
0803     return au8522_read_ucblocks(fe, ber);
0804 }
0805 
0806 static int au8522_get_frontend(struct dvb_frontend *fe,
0807                    struct dtv_frontend_properties *c)
0808 {
0809     struct au8522_state *state = fe->demodulator_priv;
0810 
0811     c->frequency = state->current_frequency;
0812     c->modulation = state->current_modulation;
0813 
0814     return 0;
0815 }
0816 
0817 static int au8522_get_tune_settings(struct dvb_frontend *fe,
0818                     struct dvb_frontend_tune_settings *tune)
0819 {
0820     tune->min_delay_ms = 1000;
0821     return 0;
0822 }
0823 
0824 static const struct dvb_frontend_ops au8522_ops;
0825 
0826 
0827 static void au8522_release(struct dvb_frontend *fe)
0828 {
0829     struct au8522_state *state = fe->demodulator_priv;
0830     au8522_release_state(state);
0831 }
0832 
0833 struct dvb_frontend *au8522_attach(const struct au8522_config *config,
0834                    struct i2c_adapter *i2c)
0835 {
0836     struct au8522_state *state = NULL;
0837     int instance;
0838 
0839     /* allocate memory for the internal state */
0840     instance = au8522_get_state(&state, i2c, config->demod_address);
0841     switch (instance) {
0842     case 0:
0843         dprintk("%s state allocation failed\n", __func__);
0844         break;
0845     case 1:
0846         /* new demod instance */
0847         dprintk("%s using new instance\n", __func__);
0848         break;
0849     default:
0850         /* existing demod instance */
0851         dprintk("%s using existing instance\n", __func__);
0852         break;
0853     }
0854 
0855     /* setup the state */
0856     state->config = *config;
0857     state->i2c = i2c;
0858     state->operational_mode = AU8522_DIGITAL_MODE;
0859 
0860     /* create dvb_frontend */
0861     memcpy(&state->frontend.ops, &au8522_ops,
0862            sizeof(struct dvb_frontend_ops));
0863     state->frontend.demodulator_priv = state;
0864 
0865     state->frontend.ops.analog_ops.i2c_gate_ctrl = au8522_analog_i2c_gate_ctrl;
0866 
0867     if (au8522_init(&state->frontend) != 0) {
0868         printk(KERN_ERR "%s: Failed to initialize correctly\n",
0869             __func__);
0870         goto error;
0871     }
0872 
0873     /* Note: Leaving the I2C gate open here. */
0874     au8522_i2c_gate_ctrl(&state->frontend, 1);
0875 
0876     return &state->frontend;
0877 
0878 error:
0879     au8522_release_state(state);
0880     return NULL;
0881 }
0882 EXPORT_SYMBOL(au8522_attach);
0883 
0884 static const struct dvb_frontend_ops au8522_ops = {
0885     .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
0886     .info = {
0887         .name           = "Auvitek AU8522 QAM/8VSB Frontend",
0888         .frequency_min_hz   =  54 * MHz,
0889         .frequency_max_hz   = 858 * MHz,
0890         .frequency_stepsize_hz  = 62500,
0891         .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
0892     },
0893 
0894     .init                 = au8522_init,
0895     .sleep                = au8522_sleep,
0896     .i2c_gate_ctrl        = au8522_i2c_gate_ctrl,
0897     .set_frontend         = au8522_set_frontend,
0898     .get_frontend         = au8522_get_frontend,
0899     .get_tune_settings    = au8522_get_tune_settings,
0900     .read_status          = au8522_read_status,
0901     .read_ber             = au8522_read_ber,
0902     .read_signal_strength = au8522_read_signal_strength,
0903     .read_snr             = au8522_read_snr,
0904     .read_ucblocks        = au8522_read_ucblocks,
0905     .release              = au8522_release,
0906 };
0907 
0908 module_param(debug, int, 0644);
0909 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
0910 
0911 module_param(zv_mode, int, 0644);
0912 MODULE_PARM_DESC(zv_mode, "Turn on/off ZeeVee modulator compatibility mode (default:on).\n"
0913     "\t\ton - modified AU8522 QAM256 initialization.\n"
0914     "\t\tProvides faster lock when using ZeeVee modulator based sources");
0915 
0916 MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
0917 MODULE_AUTHOR("Steven Toth");
0918 MODULE_LICENSE("GPL");