cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

au8522_dig.c (19235B)


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