Merge tag '4.16-minor-rc-SMB3-fixes' of git://git.samba.org/sfrench/cifs-2.6
[sfrench/cifs-2.6.git] / drivers / media / dvb-frontends / au8522_dig.c
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 <media/dvb_frontend.h>
28 #include "au8522.h"
29 #include "au8522_priv.h"
30
31 static int debug;
32 static int zv_mode = 1; /* default to on */
33
34 #define dprintk(arg...)\
35         do { if (debug)\
36                 printk(arg);\
37         } while (0)
38
39 struct mse2snr_tab {
40         u16 val;
41         u16 data;
42 };
43
44 /* VSB SNR lookup table */
45 static 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 */
79 static 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 */
160 static 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
227 static 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
244 static 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, 0x00b5, r0b5);
275         au8522_writereg(state, 0x00b6, r0b6);
276         au8522_writereg(state, 0x00b7, r0b7);
277
278         return 0;
279 }
280
281 /* VSB Modulation table */
282 static struct {
283         u16 reg;
284         u16 data;
285 } VSB_mod_tab[] = {
286         { 0x0090, 0x84 },
287         { 0x2005, 0x00 },
288         { 0x0091, 0x80 },
289         { 0x00a3, 0x0c },
290         { 0x00a4, 0xe8 },
291         { 0x0081, 0xc4 },
292         { 0x00a5, 0x40 },
293         { 0x00a7, 0x40 },
294         { 0x00a6, 0x67 },
295         { 0x0262, 0x20 },
296         { 0x021c, 0x30 },
297         { 0x00d8, 0x1a },
298         { 0x0227, 0xa0 },
299         { 0x0121, 0xff },
300         { 0x00a8, 0xf0 },
301         { 0x00a9, 0x05 },
302         { 0x00aa, 0x77 },
303         { 0x00ab, 0xf0 },
304         { 0x00ac, 0x05 },
305         { 0x00ad, 0x77 },
306         { 0x00ae, 0x41 },
307         { 0x00af, 0x66 },
308         { 0x021b, 0xcc },
309         { 0x021d, 0x80 },
310         { 0x00a4, 0xe8 },
311         { 0x0231, 0x13 },
312 };
313
314 /* QAM64 Modulation table */
315 static struct {
316         u16 reg;
317         u16 data;
318 } QAM64_mod_tab[] = {
319         { 0x00a3, 0x09 },
320         { 0x00a4, 0x00 },
321         { 0x0081, 0xc4 },
322         { 0x00a5, 0x40 },
323         { 0x00aa, 0x77 },
324         { 0x00ad, 0x77 },
325         { 0x00a6, 0x67 },
326         { 0x0262, 0x20 },
327         { 0x021c, 0x30 },
328         { 0x00b8, 0x3e },
329         { 0x00b9, 0xf0 },
330         { 0x00ba, 0x01 },
331         { 0x00bb, 0x18 },
332         { 0x00bc, 0x50 },
333         { 0x00bd, 0x00 },
334         { 0x00be, 0xea },
335         { 0x00bf, 0xef },
336         { 0x00c0, 0xfc },
337         { 0x00c1, 0xbd },
338         { 0x00c2, 0x1f },
339         { 0x00c3, 0xfc },
340         { 0x00c4, 0xdd },
341         { 0x00c5, 0xaf },
342         { 0x00c6, 0x00 },
343         { 0x00c7, 0x38 },
344         { 0x00c8, 0x30 },
345         { 0x00c9, 0x05 },
346         { 0x00ca, 0x4a },
347         { 0x00cb, 0xd0 },
348         { 0x00cc, 0x01 },
349         { 0x00cd, 0xd9 },
350         { 0x00ce, 0x6f },
351         { 0x00cf, 0xf9 },
352         { 0x00d0, 0x70 },
353         { 0x00d1, 0xdf },
354         { 0x00d2, 0xf7 },
355         { 0x00d3, 0xc2 },
356         { 0x00d4, 0xdf },
357         { 0x00d5, 0x02 },
358         { 0x00d6, 0x9a },
359         { 0x00d7, 0xd0 },
360         { 0x0250, 0x0d },
361         { 0x0251, 0xcd },
362         { 0x0252, 0xe0 },
363         { 0x0253, 0x05 },
364         { 0x0254, 0xa7 },
365         { 0x0255, 0xff },
366         { 0x0256, 0xed },
367         { 0x0257, 0x5b },
368         { 0x0258, 0xae },
369         { 0x0259, 0xe6 },
370         { 0x025a, 0x3d },
371         { 0x025b, 0x0f },
372         { 0x025c, 0x0d },
373         { 0x025d, 0xea },
374         { 0x025e, 0xf2 },
375         { 0x025f, 0x51 },
376         { 0x0260, 0xf5 },
377         { 0x0261, 0x06 },
378         { 0x021a, 0x00 },
379         { 0x0546, 0x40 },
380         { 0x0210, 0xc7 },
381         { 0x0211, 0xaa },
382         { 0x0212, 0xab },
383         { 0x0213, 0x02 },
384         { 0x0502, 0x00 },
385         { 0x0121, 0x04 },
386         { 0x0122, 0x04 },
387         { 0x052e, 0x10 },
388         { 0x00a4, 0xca },
389         { 0x00a7, 0x40 },
390         { 0x0526, 0x01 },
391 };
392
393 /* QAM256 Modulation table */
394 static struct {
395         u16 reg;
396         u16 data;
397 } QAM256_mod_tab[] = {
398         { 0x00a3, 0x09 },
399         { 0x00a4, 0x00 },
400         { 0x0081, 0xc4 },
401         { 0x00a5, 0x40 },
402         { 0x00aa, 0x77 },
403         { 0x00ad, 0x77 },
404         { 0x00a6, 0x67 },
405         { 0x0262, 0x20 },
406         { 0x021c, 0x30 },
407         { 0x00b8, 0x3e },
408         { 0x00b9, 0xf0 },
409         { 0x00ba, 0x01 },
410         { 0x00bb, 0x18 },
411         { 0x00bc, 0x50 },
412         { 0x00bd, 0x00 },
413         { 0x00be, 0xea },
414         { 0x00bf, 0xef },
415         { 0x00c0, 0xfc },
416         { 0x00c1, 0xbd },
417         { 0x00c2, 0x1f },
418         { 0x00c3, 0xfc },
419         { 0x00c4, 0xdd },
420         { 0x00c5, 0xaf },
421         { 0x00c6, 0x00 },
422         { 0x00c7, 0x38 },
423         { 0x00c8, 0x30 },
424         { 0x00c9, 0x05 },
425         { 0x00ca, 0x4a },
426         { 0x00cb, 0xd0 },
427         { 0x00cc, 0x01 },
428         { 0x00cd, 0xd9 },
429         { 0x00ce, 0x6f },
430         { 0x00cf, 0xf9 },
431         { 0x00d0, 0x70 },
432         { 0x00d1, 0xdf },
433         { 0x00d2, 0xf7 },
434         { 0x00d3, 0xc2 },
435         { 0x00d4, 0xdf },
436         { 0x00d5, 0x02 },
437         { 0x00d6, 0x9a },
438         { 0x00d7, 0xd0 },
439         { 0x0250, 0x0d },
440         { 0x0251, 0xcd },
441         { 0x0252, 0xe0 },
442         { 0x0253, 0x05 },
443         { 0x0254, 0xa7 },
444         { 0x0255, 0xff },
445         { 0x0256, 0xed },
446         { 0x0257, 0x5b },
447         { 0x0258, 0xae },
448         { 0x0259, 0xe6 },
449         { 0x025a, 0x3d },
450         { 0x025b, 0x0f },
451         { 0x025c, 0x0d },
452         { 0x025d, 0xea },
453         { 0x025e, 0xf2 },
454         { 0x025f, 0x51 },
455         { 0x0260, 0xf5 },
456         { 0x0261, 0x06 },
457         { 0x021a, 0x00 },
458         { 0x0546, 0x40 },
459         { 0x0210, 0x26 },
460         { 0x0211, 0xf6 },
461         { 0x0212, 0x84 },
462         { 0x0213, 0x02 },
463         { 0x0502, 0x01 },
464         { 0x0121, 0x04 },
465         { 0x0122, 0x04 },
466         { 0x052e, 0x10 },
467         { 0x00a4, 0xca },
468         { 0x00a7, 0x40 },
469         { 0x0526, 0x01 },
470 };
471
472 static struct {
473         u16 reg;
474         u16 data;
475 } QAM256_mod_tab_zv_mode[] = {
476         { 0x80a3, 0x09 },
477         { 0x80a4, 0x00 },
478         { 0x8081, 0xc4 },
479         { 0x80a5, 0x40 },
480         { 0x80b5, 0xfb },
481         { 0x80b6, 0x8e },
482         { 0x80b7, 0x39 },
483         { 0x80aa, 0x77 },
484         { 0x80ad, 0x77 },
485         { 0x80a6, 0x67 },
486         { 0x8262, 0x20 },
487         { 0x821c, 0x30 },
488         { 0x80b8, 0x3e },
489         { 0x80b9, 0xf0 },
490         { 0x80ba, 0x01 },
491         { 0x80bb, 0x18 },
492         { 0x80bc, 0x50 },
493         { 0x80bd, 0x00 },
494         { 0x80be, 0xea },
495         { 0x80bf, 0xef },
496         { 0x80c0, 0xfc },
497         { 0x80c1, 0xbd },
498         { 0x80c2, 0x1f },
499         { 0x80c3, 0xfc },
500         { 0x80c4, 0xdd },
501         { 0x80c5, 0xaf },
502         { 0x80c6, 0x00 },
503         { 0x80c7, 0x38 },
504         { 0x80c8, 0x30 },
505         { 0x80c9, 0x05 },
506         { 0x80ca, 0x4a },
507         { 0x80cb, 0xd0 },
508         { 0x80cc, 0x01 },
509         { 0x80cd, 0xd9 },
510         { 0x80ce, 0x6f },
511         { 0x80cf, 0xf9 },
512         { 0x80d0, 0x70 },
513         { 0x80d1, 0xdf },
514         { 0x80d2, 0xf7 },
515         { 0x80d3, 0xc2 },
516         { 0x80d4, 0xdf },
517         { 0x80d5, 0x02 },
518         { 0x80d6, 0x9a },
519         { 0x80d7, 0xd0 },
520         { 0x8250, 0x0d },
521         { 0x8251, 0xcd },
522         { 0x8252, 0xe0 },
523         { 0x8253, 0x05 },
524         { 0x8254, 0xa7 },
525         { 0x8255, 0xff },
526         { 0x8256, 0xed },
527         { 0x8257, 0x5b },
528         { 0x8258, 0xae },
529         { 0x8259, 0xe6 },
530         { 0x825a, 0x3d },
531         { 0x825b, 0x0f },
532         { 0x825c, 0x0d },
533         { 0x825d, 0xea },
534         { 0x825e, 0xf2 },
535         { 0x825f, 0x51 },
536         { 0x8260, 0xf5 },
537         { 0x8261, 0x06 },
538         { 0x821a, 0x01 },
539         { 0x8546, 0x40 },
540         { 0x8210, 0x26 },
541         { 0x8211, 0xf6 },
542         { 0x8212, 0x84 },
543         { 0x8213, 0x02 },
544         { 0x8502, 0x01 },
545         { 0x8121, 0x04 },
546         { 0x8122, 0x04 },
547         { 0x852e, 0x10 },
548         { 0x80a4, 0xca },
549         { 0x80a7, 0x40 },
550         { 0x8526, 0x01 },
551 };
552
553 static int au8522_enable_modulation(struct dvb_frontend *fe,
554                                     enum fe_modulation m)
555 {
556         struct au8522_state *state = fe->demodulator_priv;
557         int i;
558
559         dprintk("%s(0x%08x)\n", __func__, m);
560
561         switch (m) {
562         case VSB_8:
563                 dprintk("%s() VSB_8\n", __func__);
564                 for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
565                         au8522_writereg(state,
566                                 VSB_mod_tab[i].reg,
567                                 VSB_mod_tab[i].data);
568                 au8522_set_if(fe, state->config.vsb_if);
569                 break;
570         case QAM_64:
571                 dprintk("%s() QAM 64\n", __func__);
572                 for (i = 0; i < ARRAY_SIZE(QAM64_mod_tab); i++)
573                         au8522_writereg(state,
574                                 QAM64_mod_tab[i].reg,
575                                 QAM64_mod_tab[i].data);
576                 au8522_set_if(fe, state->config.qam_if);
577                 break;
578         case QAM_256:
579                 if (zv_mode) {
580                         dprintk("%s() QAM 256 (zv_mode)\n", __func__);
581                         for (i = 0; i < ARRAY_SIZE(QAM256_mod_tab_zv_mode); i++)
582                                 au8522_writereg(state,
583                                         QAM256_mod_tab_zv_mode[i].reg,
584                                         QAM256_mod_tab_zv_mode[i].data);
585                         au8522_set_if(fe, state->config.qam_if);
586                         msleep(100);
587                         au8522_writereg(state, 0x821a, 0x00);
588                 } else {
589                         dprintk("%s() QAM 256\n", __func__);
590                         for (i = 0; i < ARRAY_SIZE(QAM256_mod_tab); i++)
591                                 au8522_writereg(state,
592                                         QAM256_mod_tab[i].reg,
593                                         QAM256_mod_tab[i].data);
594                         au8522_set_if(fe, state->config.qam_if);
595                 }
596                 break;
597         default:
598                 dprintk("%s() Invalid modulation\n", __func__);
599                 return -EINVAL;
600         }
601
602         state->current_modulation = m;
603
604         return 0;
605 }
606
607 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
608 static int au8522_set_frontend(struct dvb_frontend *fe)
609 {
610         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
611         struct au8522_state *state = fe->demodulator_priv;
612         int ret = -EINVAL;
613
614         dprintk("%s(frequency=%d)\n", __func__, c->frequency);
615
616         if ((state->current_frequency == c->frequency) &&
617             (state->current_modulation == c->modulation))
618                 return 0;
619
620         if (fe->ops.tuner_ops.set_params) {
621                 if (fe->ops.i2c_gate_ctrl)
622                         fe->ops.i2c_gate_ctrl(fe, 1);
623                 ret = fe->ops.tuner_ops.set_params(fe);
624                 if (fe->ops.i2c_gate_ctrl)
625                         fe->ops.i2c_gate_ctrl(fe, 0);
626         }
627
628         if (ret < 0)
629                 return ret;
630
631         /* Allow the tuner to settle */
632         if (zv_mode) {
633                 dprintk("%s() increase tuner settling time for zv_mode\n",
634                         __func__);
635                 msleep(250);
636         } else
637                 msleep(100);
638
639         au8522_enable_modulation(fe, c->modulation);
640
641         state->current_frequency = c->frequency;
642
643         return 0;
644 }
645
646 static int au8522_read_status(struct dvb_frontend *fe, enum fe_status *status)
647 {
648         struct au8522_state *state = fe->demodulator_priv;
649         u8 reg;
650         u32 tuner_status = 0;
651
652         *status = 0;
653
654         if (state->current_modulation == VSB_8) {
655                 dprintk("%s() Checking VSB_8\n", __func__);
656                 reg = au8522_readreg(state, 0x0088);
657                 if ((reg & 0x03) == 0x03)
658                         *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
659         } else {
660                 dprintk("%s() Checking QAM\n", __func__);
661                 reg = au8522_readreg(state, 0x0541);
662                 if (reg & 0x80)
663                         *status |= FE_HAS_VITERBI;
664                 if (reg & 0x20)
665                         *status |= FE_HAS_LOCK | FE_HAS_SYNC;
666         }
667
668         switch (state->config.status_mode) {
669         case AU8522_DEMODLOCKING:
670                 dprintk("%s() DEMODLOCKING\n", __func__);
671                 if (*status & FE_HAS_VITERBI)
672                         *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
673                 break;
674         case AU8522_TUNERLOCKING:
675                 /* Get the tuner status */
676                 dprintk("%s() TUNERLOCKING\n", __func__);
677                 if (fe->ops.tuner_ops.get_status) {
678                         if (fe->ops.i2c_gate_ctrl)
679                                 fe->ops.i2c_gate_ctrl(fe, 1);
680
681                         fe->ops.tuner_ops.get_status(fe, &tuner_status);
682
683                         if (fe->ops.i2c_gate_ctrl)
684                                 fe->ops.i2c_gate_ctrl(fe, 0);
685                 }
686                 if (tuner_status)
687                         *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
688                 break;
689         }
690         state->fe_status = *status;
691
692         if (*status & FE_HAS_LOCK)
693                 /* turn on LED, if it isn't on already */
694                 au8522_led_ctrl(state, -1);
695         else
696                 /* turn off LED */
697                 au8522_led_ctrl(state, 0);
698
699         dprintk("%s() status 0x%08x\n", __func__, *status);
700
701         return 0;
702 }
703
704 static int au8522_led_status(struct au8522_state *state, const u16 *snr)
705 {
706         struct au8522_led_config *led_config = state->config.led_cfg;
707         int led;
708         u16 strong;
709
710         /* bail out if we can't control an LED */
711         if (!led_config)
712                 return 0;
713
714         if (0 == (state->fe_status & FE_HAS_LOCK))
715                 return au8522_led_ctrl(state, 0);
716         else if (state->current_modulation == QAM_256)
717                 strong = led_config->qam256_strong;
718         else if (state->current_modulation == QAM_64)
719                 strong = led_config->qam64_strong;
720         else /* (state->current_modulation == VSB_8) */
721                 strong = led_config->vsb8_strong;
722
723         if (*snr >= strong)
724                 led = 2;
725         else
726                 led = 1;
727
728         if ((state->led_state) &&
729             (((strong < *snr) ? (*snr - strong) : (strong - *snr)) <= 10))
730                 /* snr didn't change enough to bother
731                  * changing the color of the led */
732                 return 0;
733
734         return au8522_led_ctrl(state, led);
735 }
736
737 static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
738 {
739         struct au8522_state *state = fe->demodulator_priv;
740         int ret = -EINVAL;
741
742         dprintk("%s()\n", __func__);
743
744         if (state->current_modulation == QAM_256)
745                 ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
746                                             ARRAY_SIZE(qam256_mse2snr_tab),
747                                             au8522_readreg(state, 0x0522),
748                                             snr);
749         else if (state->current_modulation == QAM_64)
750                 ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
751                                             ARRAY_SIZE(qam64_mse2snr_tab),
752                                             au8522_readreg(state, 0x0522),
753                                             snr);
754         else /* VSB_8 */
755                 ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
756                                             ARRAY_SIZE(vsb_mse2snr_tab),
757                                             au8522_readreg(state, 0x0311),
758                                             snr);
759
760         if (state->config.led_cfg)
761                 au8522_led_status(state, snr);
762
763         return ret;
764 }
765
766 static int au8522_read_signal_strength(struct dvb_frontend *fe,
767                                        u16 *signal_strength)
768 {
769         /* borrowed from lgdt330x.c
770          *
771          * Calculate strength from SNR up to 35dB
772          * Even though the SNR can go higher than 35dB,
773          * there is some comfort factor in having a range of
774          * strong signals that can show at 100%
775          */
776         u16 snr;
777         u32 tmp;
778         int ret = au8522_read_snr(fe, &snr);
779
780         *signal_strength = 0;
781
782         if (0 == ret) {
783                 /* The following calculation method was chosen
784                  * purely for the sake of code re-use from the
785                  * other demod drivers that use this method */
786
787                 /* Convert from SNR in dB * 10 to 8.24 fixed-point */
788                 tmp = (snr * ((1 << 24) / 10));
789
790                 /* Convert from 8.24 fixed-point to
791                  * scale the range 0 - 35*2^24 into 0 - 65535*/
792                 if (tmp >= 8960 * 0x10000)
793                         *signal_strength = 0xffff;
794                 else
795                         *signal_strength = tmp / 8960;
796         }
797
798         return ret;
799 }
800
801 static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
802 {
803         struct au8522_state *state = fe->demodulator_priv;
804
805         if (state->current_modulation == VSB_8)
806                 *ucblocks = au8522_readreg(state, 0x0087);
807         else
808                 *ucblocks = au8522_readreg(state, 0x0543);
809
810         return 0;
811 }
812
813 static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
814 {
815         return au8522_read_ucblocks(fe, ber);
816 }
817
818 static int au8522_get_frontend(struct dvb_frontend *fe,
819                                struct dtv_frontend_properties *c)
820 {
821         struct au8522_state *state = fe->demodulator_priv;
822
823         c->frequency = state->current_frequency;
824         c->modulation = state->current_modulation;
825
826         return 0;
827 }
828
829 static int au8522_get_tune_settings(struct dvb_frontend *fe,
830                                     struct dvb_frontend_tune_settings *tune)
831 {
832         tune->min_delay_ms = 1000;
833         return 0;
834 }
835
836 static const struct dvb_frontend_ops au8522_ops;
837
838
839 static void au8522_release(struct dvb_frontend *fe)
840 {
841         struct au8522_state *state = fe->demodulator_priv;
842         au8522_release_state(state);
843 }
844
845 struct dvb_frontend *au8522_attach(const struct au8522_config *config,
846                                    struct i2c_adapter *i2c)
847 {
848         struct au8522_state *state = NULL;
849         int instance;
850
851         /* allocate memory for the internal state */
852         instance = au8522_get_state(&state, i2c, config->demod_address);
853         switch (instance) {
854         case 0:
855                 dprintk("%s state allocation failed\n", __func__);
856                 break;
857         case 1:
858                 /* new demod instance */
859                 dprintk("%s using new instance\n", __func__);
860                 break;
861         default:
862                 /* existing demod instance */
863                 dprintk("%s using existing instance\n", __func__);
864                 break;
865         }
866
867         /* setup the state */
868         state->config = *config;
869         state->i2c = i2c;
870         state->operational_mode = AU8522_DIGITAL_MODE;
871
872         /* create dvb_frontend */
873         memcpy(&state->frontend.ops, &au8522_ops,
874                sizeof(struct dvb_frontend_ops));
875         state->frontend.demodulator_priv = state;
876
877         state->frontend.ops.analog_ops.i2c_gate_ctrl = au8522_analog_i2c_gate_ctrl;
878
879         if (au8522_init(&state->frontend) != 0) {
880                 printk(KERN_ERR "%s: Failed to initialize correctly\n",
881                         __func__);
882                 goto error;
883         }
884
885         /* Note: Leaving the I2C gate open here. */
886         au8522_i2c_gate_ctrl(&state->frontend, 1);
887
888         return &state->frontend;
889
890 error:
891         au8522_release_state(state);
892         return NULL;
893 }
894 EXPORT_SYMBOL(au8522_attach);
895
896 static const struct dvb_frontend_ops au8522_ops = {
897         .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
898         .info = {
899                 .name                   = "Auvitek AU8522 QAM/8VSB Frontend",
900                 .frequency_min          = 54000000,
901                 .frequency_max          = 858000000,
902                 .frequency_stepsize     = 62500,
903                 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
904         },
905
906         .init                 = au8522_init,
907         .sleep                = au8522_sleep,
908         .i2c_gate_ctrl        = au8522_i2c_gate_ctrl,
909         .set_frontend         = au8522_set_frontend,
910         .get_frontend         = au8522_get_frontend,
911         .get_tune_settings    = au8522_get_tune_settings,
912         .read_status          = au8522_read_status,
913         .read_ber             = au8522_read_ber,
914         .read_signal_strength = au8522_read_signal_strength,
915         .read_snr             = au8522_read_snr,
916         .read_ucblocks        = au8522_read_ucblocks,
917         .release              = au8522_release,
918 };
919
920 module_param(debug, int, 0644);
921 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
922
923 module_param(zv_mode, int, 0644);
924 MODULE_PARM_DESC(zv_mode, "Turn on/off ZeeVee modulator compatibility mode (default:on).\n"
925         "\t\ton - modified AU8522 QAM256 initialization.\n"
926         "\t\tProvides faster lock when using ZeeVee modulator based sources");
927
928 MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
929 MODULE_AUTHOR("Steven Toth");
930 MODULE_LICENSE("GPL");