Merge tag 'v3.7-rc3' into for-linus to sync up with recent USB changes
[pandora-kernel.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 "dvb_frontend.h"
28 #include "au8522.h"
29 #include "au8522_priv.h"
30
31 static int debug;
32
33 #define dprintk(arg...)\
34         do { if (debug)\
35                 printk(arg);\
36         } while (0)
37
38 struct mse2snr_tab {
39         u16 val;
40         u16 data;
41 };
42
43 /* VSB SNR lookup table */
44 static struct mse2snr_tab vsb_mse2snr_tab[] = {
45         {   0, 270 },
46         {   2, 250 },
47         {   3, 240 },
48         {   5, 230 },
49         {   7, 220 },
50         {   9, 210 },
51         {  12, 200 },
52         {  13, 195 },
53         {  15, 190 },
54         {  17, 185 },
55         {  19, 180 },
56         {  21, 175 },
57         {  24, 170 },
58         {  27, 165 },
59         {  31, 160 },
60         {  32, 158 },
61         {  33, 156 },
62         {  36, 152 },
63         {  37, 150 },
64         {  39, 148 },
65         {  40, 146 },
66         {  41, 144 },
67         {  43, 142 },
68         {  44, 140 },
69         {  48, 135 },
70         {  50, 130 },
71         {  43, 142 },
72         {  53, 125 },
73         {  56, 120 },
74         { 256, 115 },
75 };
76
77 /* QAM64 SNR lookup table */
78 static struct mse2snr_tab qam64_mse2snr_tab[] = {
79         {  15,   0 },
80         {  16, 290 },
81         {  17, 288 },
82         {  18, 286 },
83         {  19, 284 },
84         {  20, 282 },
85         {  21, 281 },
86         {  22, 279 },
87         {  23, 277 },
88         {  24, 275 },
89         {  25, 273 },
90         {  26, 271 },
91         {  27, 269 },
92         {  28, 268 },
93         {  29, 266 },
94         {  30, 264 },
95         {  31, 262 },
96         {  32, 260 },
97         {  33, 259 },
98         {  34, 258 },
99         {  35, 256 },
100         {  36, 255 },
101         {  37, 254 },
102         {  38, 252 },
103         {  39, 251 },
104         {  40, 250 },
105         {  41, 249 },
106         {  42, 248 },
107         {  43, 246 },
108         {  44, 245 },
109         {  45, 244 },
110         {  46, 242 },
111         {  47, 241 },
112         {  48, 240 },
113         {  50, 239 },
114         {  51, 238 },
115         {  53, 237 },
116         {  54, 236 },
117         {  56, 235 },
118         {  57, 234 },
119         {  59, 233 },
120         {  60, 232 },
121         {  62, 231 },
122         {  63, 230 },
123         {  65, 229 },
124         {  67, 228 },
125         {  68, 227 },
126         {  70, 226 },
127         {  71, 225 },
128         {  73, 224 },
129         {  74, 223 },
130         {  76, 222 },
131         {  78, 221 },
132         {  80, 220 },
133         {  82, 219 },
134         {  85, 218 },
135         {  88, 217 },
136         {  90, 216 },
137         {  92, 215 },
138         {  93, 214 },
139         {  94, 212 },
140         {  95, 211 },
141         {  97, 210 },
142         {  99, 209 },
143         { 101, 208 },
144         { 102, 207 },
145         { 104, 206 },
146         { 107, 205 },
147         { 111, 204 },
148         { 114, 203 },
149         { 118, 202 },
150         { 122, 201 },
151         { 125, 200 },
152         { 128, 199 },
153         { 130, 198 },
154         { 132, 197 },
155         { 256, 190 },
156 };
157
158 /* QAM256 SNR lookup table */
159 static struct mse2snr_tab qam256_mse2snr_tab[] = {
160         {  15,   0 },
161         {  16, 400 },
162         {  17, 398 },
163         {  18, 396 },
164         {  19, 394 },
165         {  20, 392 },
166         {  21, 390 },
167         {  22, 388 },
168         {  23, 386 },
169         {  24, 384 },
170         {  25, 382 },
171         {  26, 380 },
172         {  27, 379 },
173         {  28, 378 },
174         {  29, 377 },
175         {  30, 376 },
176         {  31, 375 },
177         {  32, 374 },
178         {  33, 373 },
179         {  34, 372 },
180         {  35, 371 },
181         {  36, 370 },
182         {  37, 362 },
183         {  38, 354 },
184         {  39, 346 },
185         {  40, 338 },
186         {  41, 330 },
187         {  42, 328 },
188         {  43, 326 },
189         {  44, 324 },
190         {  45, 322 },
191         {  46, 320 },
192         {  47, 319 },
193         {  48, 318 },
194         {  49, 317 },
195         {  50, 316 },
196         {  51, 315 },
197         {  52, 314 },
198         {  53, 313 },
199         {  54, 312 },
200         {  55, 311 },
201         {  56, 310 },
202         {  57, 308 },
203         {  58, 306 },
204         {  59, 304 },
205         {  60, 302 },
206         {  61, 300 },
207         {  62, 298 },
208         {  65, 295 },
209         {  68, 294 },
210         {  70, 293 },
211         {  73, 292 },
212         {  76, 291 },
213         {  78, 290 },
214         {  79, 289 },
215         {  81, 288 },
216         {  82, 287 },
217         {  83, 286 },
218         {  84, 285 },
219         {  85, 284 },
220         {  86, 283 },
221         {  88, 282 },
222         {  89, 281 },
223         { 256, 280 },
224 };
225
226 static int au8522_mse2snr_lookup(struct mse2snr_tab *tab, int sz, int mse,
227                                  u16 *snr)
228 {
229         int i, ret = -EINVAL;
230         dprintk("%s()\n", __func__);
231
232         for (i = 0; i < sz; i++) {
233                 if (mse < tab[i].val) {
234                         *snr = tab[i].data;
235                         ret = 0;
236                         break;
237                 }
238         }
239         dprintk("%s() snr=%d\n", __func__, *snr);
240         return ret;
241 }
242
243 static int au8522_set_if(struct dvb_frontend *fe, enum au8522_if_freq if_freq)
244 {
245         struct au8522_state *state = fe->demodulator_priv;
246         u8 r0b5, r0b6, r0b7;
247         char *ifmhz;
248
249         switch (if_freq) {
250         case AU8522_IF_3_25MHZ:
251                 ifmhz = "3.25";
252                 r0b5 = 0x00;
253                 r0b6 = 0x3d;
254                 r0b7 = 0xa0;
255                 break;
256         case AU8522_IF_4MHZ:
257                 ifmhz = "4.00";
258                 r0b5 = 0x00;
259                 r0b6 = 0x4b;
260                 r0b7 = 0xd9;
261                 break;
262         case AU8522_IF_6MHZ:
263                 ifmhz = "6.00";
264                 r0b5 = 0xfb;
265                 r0b6 = 0x8e;
266                 r0b7 = 0x39;
267                 break;
268         default:
269                 dprintk("%s() IF Frequency not supported\n", __func__);
270                 return -EINVAL;
271         }
272         dprintk("%s() %s MHz\n", __func__, ifmhz);
273         au8522_writereg(state, 0x80b5, r0b5);
274         au8522_writereg(state, 0x80b6, r0b6);
275         au8522_writereg(state, 0x80b7, r0b7);
276
277         return 0;
278 }
279
280 /* VSB Modulation table */
281 static struct {
282         u16 reg;
283         u16 data;
284 } VSB_mod_tab[] = {
285         { 0x8090, 0x84 },
286         { 0x4092, 0x11 },
287         { 0x2005, 0x00 },
288         { 0x8091, 0x80 },
289         { 0x80a3, 0x0c },
290         { 0x80a4, 0xe8 },
291         { 0x8081, 0xc4 },
292         { 0x80a5, 0x40 },
293         { 0x80a7, 0x40 },
294         { 0x80a6, 0x67 },
295         { 0x8262, 0x20 },
296         { 0x821c, 0x30 },
297         { 0x80d8, 0x1a },
298         { 0x8227, 0xa0 },
299         { 0x8121, 0xff },
300         { 0x80a8, 0xf0 },
301         { 0x80a9, 0x05 },
302         { 0x80aa, 0x77 },
303         { 0x80ab, 0xf0 },
304         { 0x80ac, 0x05 },
305         { 0x80ad, 0x77 },
306         { 0x80ae, 0x41 },
307         { 0x80af, 0x66 },
308         { 0x821b, 0xcc },
309         { 0x821d, 0x80 },
310         { 0x80a4, 0xe8 },
311         { 0x8231, 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         { 0x80a3, 0x09 },
399         { 0x80a4, 0x00 },
400         { 0x8081, 0xc4 },
401         { 0x80a5, 0x40 },
402         { 0x80aa, 0x77 },
403         { 0x80ad, 0x77 },
404         { 0x80a6, 0x67 },
405         { 0x8262, 0x20 },
406         { 0x821c, 0x30 },
407         { 0x80b8, 0x3e },
408         { 0x80b9, 0xf0 },
409         { 0x80ba, 0x01 },
410         { 0x80bb, 0x18 },
411         { 0x80bc, 0x50 },
412         { 0x80bd, 0x00 },
413         { 0x80be, 0xea },
414         { 0x80bf, 0xef },
415         { 0x80c0, 0xfc },
416         { 0x80c1, 0xbd },
417         { 0x80c2, 0x1f },
418         { 0x80c3, 0xfc },
419         { 0x80c4, 0xdd },
420         { 0x80c5, 0xaf },
421         { 0x80c6, 0x00 },
422         { 0x80c7, 0x38 },
423         { 0x80c8, 0x30 },
424         { 0x80c9, 0x05 },
425         { 0x80ca, 0x4a },
426         { 0x80cb, 0xd0 },
427         { 0x80cc, 0x01 },
428         { 0x80cd, 0xd9 },
429         { 0x80ce, 0x6f },
430         { 0x80cf, 0xf9 },
431         { 0x80d0, 0x70 },
432         { 0x80d1, 0xdf },
433         { 0x80d2, 0xf7 },
434         { 0x80d3, 0xc2 },
435         { 0x80d4, 0xdf },
436         { 0x80d5, 0x02 },
437         { 0x80d6, 0x9a },
438         { 0x80d7, 0xd0 },
439         { 0x8250, 0x0d },
440         { 0x8251, 0xcd },
441         { 0x8252, 0xe0 },
442         { 0x8253, 0x05 },
443         { 0x8254, 0xa7 },
444         { 0x8255, 0xff },
445         { 0x8256, 0xed },
446         { 0x8257, 0x5b },
447         { 0x8258, 0xae },
448         { 0x8259, 0xe6 },
449         { 0x825a, 0x3d },
450         { 0x825b, 0x0f },
451         { 0x825c, 0x0d },
452         { 0x825d, 0xea },
453         { 0x825e, 0xf2 },
454         { 0x825f, 0x51 },
455         { 0x8260, 0xf5 },
456         { 0x8261, 0x06 },
457         { 0x821a, 0x00 },
458         { 0x8546, 0x40 },
459         { 0x8210, 0x26 },
460         { 0x8211, 0xf6 },
461         { 0x8212, 0x84 },
462         { 0x8213, 0x02 },
463         { 0x8502, 0x01 },
464         { 0x8121, 0x04 },
465         { 0x8122, 0x04 },
466         { 0x852e, 0x10 },
467         { 0x80a4, 0xca },
468         { 0x80a7, 0x40 },
469         { 0x8526, 0x01 },
470 };
471
472 static int au8522_enable_modulation(struct dvb_frontend *fe,
473                                     fe_modulation_t m)
474 {
475         struct au8522_state *state = fe->demodulator_priv;
476         int i;
477
478         dprintk("%s(0x%08x)\n", __func__, m);
479
480         switch (m) {
481         case VSB_8:
482                 dprintk("%s() VSB_8\n", __func__);
483                 for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
484                         au8522_writereg(state,
485                                 VSB_mod_tab[i].reg,
486                                 VSB_mod_tab[i].data);
487                 au8522_set_if(fe, state->config->vsb_if);
488                 break;
489         case QAM_64:
490                 dprintk("%s() QAM 64\n", __func__);
491                 for (i = 0; i < ARRAY_SIZE(QAM64_mod_tab); i++)
492                         au8522_writereg(state,
493                                 QAM64_mod_tab[i].reg,
494                                 QAM64_mod_tab[i].data);
495                 au8522_set_if(fe, state->config->qam_if);
496                 break;
497         case QAM_256:
498                 dprintk("%s() QAM 256\n", __func__);
499                 for (i = 0; i < ARRAY_SIZE(QAM256_mod_tab); i++)
500                         au8522_writereg(state,
501                                 QAM256_mod_tab[i].reg,
502                                 QAM256_mod_tab[i].data);
503                 au8522_set_if(fe, state->config->qam_if);
504                 break;
505         default:
506                 dprintk("%s() Invalid modulation\n", __func__);
507                 return -EINVAL;
508         }
509
510         state->current_modulation = m;
511
512         return 0;
513 }
514
515 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
516 static int au8522_set_frontend(struct dvb_frontend *fe)
517 {
518         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
519         struct au8522_state *state = fe->demodulator_priv;
520         int ret = -EINVAL;
521
522         dprintk("%s(frequency=%d)\n", __func__, c->frequency);
523
524         if ((state->current_frequency == c->frequency) &&
525             (state->current_modulation == c->modulation))
526                 return 0;
527
528         if (fe->ops.tuner_ops.set_params) {
529                 if (fe->ops.i2c_gate_ctrl)
530                         fe->ops.i2c_gate_ctrl(fe, 1);
531                 ret = fe->ops.tuner_ops.set_params(fe);
532                 if (fe->ops.i2c_gate_ctrl)
533                         fe->ops.i2c_gate_ctrl(fe, 0);
534         }
535
536         if (ret < 0)
537                 return ret;
538
539         /* Allow the tuner to settle */
540         msleep(100);
541
542         au8522_enable_modulation(fe, c->modulation);
543
544         state->current_frequency = c->frequency;
545
546         return 0;
547 }
548
549 static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status)
550 {
551         struct au8522_state *state = fe->demodulator_priv;
552         u8 reg;
553         u32 tuner_status = 0;
554
555         *status = 0;
556
557         if (state->current_modulation == VSB_8) {
558                 dprintk("%s() Checking VSB_8\n", __func__);
559                 reg = au8522_readreg(state, 0x4088);
560                 if ((reg & 0x03) == 0x03)
561                         *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
562         } else {
563                 dprintk("%s() Checking QAM\n", __func__);
564                 reg = au8522_readreg(state, 0x4541);
565                 if (reg & 0x80)
566                         *status |= FE_HAS_VITERBI;
567                 if (reg & 0x20)
568                         *status |= FE_HAS_LOCK | FE_HAS_SYNC;
569         }
570
571         switch (state->config->status_mode) {
572         case AU8522_DEMODLOCKING:
573                 dprintk("%s() DEMODLOCKING\n", __func__);
574                 if (*status & FE_HAS_VITERBI)
575                         *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
576                 break;
577         case AU8522_TUNERLOCKING:
578                 /* Get the tuner status */
579                 dprintk("%s() TUNERLOCKING\n", __func__);
580                 if (fe->ops.tuner_ops.get_status) {
581                         if (fe->ops.i2c_gate_ctrl)
582                                 fe->ops.i2c_gate_ctrl(fe, 1);
583
584                         fe->ops.tuner_ops.get_status(fe, &tuner_status);
585
586                         if (fe->ops.i2c_gate_ctrl)
587                                 fe->ops.i2c_gate_ctrl(fe, 0);
588                 }
589                 if (tuner_status)
590                         *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
591                 break;
592         }
593         state->fe_status = *status;
594
595         if (*status & FE_HAS_LOCK)
596                 /* turn on LED, if it isn't on already */
597                 au8522_led_ctrl(state, -1);
598         else
599                 /* turn off LED */
600                 au8522_led_ctrl(state, 0);
601
602         dprintk("%s() status 0x%08x\n", __func__, *status);
603
604         return 0;
605 }
606
607 static int au8522_led_status(struct au8522_state *state, const u16 *snr)
608 {
609         struct au8522_led_config *led_config = state->config->led_cfg;
610         int led;
611         u16 strong;
612
613         /* bail out if we can't control an LED */
614         if (!led_config)
615                 return 0;
616
617         if (0 == (state->fe_status & FE_HAS_LOCK))
618                 return au8522_led_ctrl(state, 0);
619         else if (state->current_modulation == QAM_256)
620                 strong = led_config->qam256_strong;
621         else if (state->current_modulation == QAM_64)
622                 strong = led_config->qam64_strong;
623         else /* (state->current_modulation == VSB_8) */
624                 strong = led_config->vsb8_strong;
625
626         if (*snr >= strong)
627                 led = 2;
628         else
629                 led = 1;
630
631         if ((state->led_state) &&
632             (((strong < *snr) ? (*snr - strong) : (strong - *snr)) <= 10))
633                 /* snr didn't change enough to bother
634                  * changing the color of the led */
635                 return 0;
636
637         return au8522_led_ctrl(state, led);
638 }
639
640 static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
641 {
642         struct au8522_state *state = fe->demodulator_priv;
643         int ret = -EINVAL;
644
645         dprintk("%s()\n", __func__);
646
647         if (state->current_modulation == QAM_256)
648                 ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
649                                             ARRAY_SIZE(qam256_mse2snr_tab),
650                                             au8522_readreg(state, 0x4522),
651                                             snr);
652         else if (state->current_modulation == QAM_64)
653                 ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
654                                             ARRAY_SIZE(qam64_mse2snr_tab),
655                                             au8522_readreg(state, 0x4522),
656                                             snr);
657         else /* VSB_8 */
658                 ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
659                                             ARRAY_SIZE(vsb_mse2snr_tab),
660                                             au8522_readreg(state, 0x4311),
661                                             snr);
662
663         if (state->config->led_cfg)
664                 au8522_led_status(state, snr);
665
666         return ret;
667 }
668
669 static int au8522_read_signal_strength(struct dvb_frontend *fe,
670                                        u16 *signal_strength)
671 {
672         /* borrowed from lgdt330x.c
673          *
674          * Calculate strength from SNR up to 35dB
675          * Even though the SNR can go higher than 35dB,
676          * there is some comfort factor in having a range of
677          * strong signals that can show at 100%
678          */
679         u16 snr;
680         u32 tmp;
681         int ret = au8522_read_snr(fe, &snr);
682
683         *signal_strength = 0;
684
685         if (0 == ret) {
686                 /* The following calculation method was chosen
687                  * purely for the sake of code re-use from the
688                  * other demod drivers that use this method */
689
690                 /* Convert from SNR in dB * 10 to 8.24 fixed-point */
691                 tmp = (snr * ((1 << 24) / 10));
692
693                 /* Convert from 8.24 fixed-point to
694                  * scale the range 0 - 35*2^24 into 0 - 65535*/
695                 if (tmp >= 8960 * 0x10000)
696                         *signal_strength = 0xffff;
697                 else
698                         *signal_strength = tmp / 8960;
699         }
700
701         return ret;
702 }
703
704 static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
705 {
706         struct au8522_state *state = fe->demodulator_priv;
707
708         if (state->current_modulation == VSB_8)
709                 *ucblocks = au8522_readreg(state, 0x4087);
710         else
711                 *ucblocks = au8522_readreg(state, 0x4543);
712
713         return 0;
714 }
715
716 static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
717 {
718         return au8522_read_ucblocks(fe, ber);
719 }
720
721 static int au8522_get_frontend(struct dvb_frontend *fe)
722 {
723         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
724         struct au8522_state *state = fe->demodulator_priv;
725
726         c->frequency = state->current_frequency;
727         c->modulation = state->current_modulation;
728
729         return 0;
730 }
731
732 static int au8522_get_tune_settings(struct dvb_frontend *fe,
733                                     struct dvb_frontend_tune_settings *tune)
734 {
735         tune->min_delay_ms = 1000;
736         return 0;
737 }
738
739 static struct dvb_frontend_ops au8522_ops;
740
741
742 static void au8522_release(struct dvb_frontend *fe)
743 {
744         struct au8522_state *state = fe->demodulator_priv;
745         au8522_release_state(state);
746 }
747
748 struct dvb_frontend *au8522_attach(const struct au8522_config *config,
749                                    struct i2c_adapter *i2c)
750 {
751         struct au8522_state *state = NULL;
752         int instance;
753
754         /* allocate memory for the internal state */
755         instance = au8522_get_state(&state, i2c, config->demod_address);
756         switch (instance) {
757         case 0:
758                 dprintk("%s state allocation failed\n", __func__);
759                 break;
760         case 1:
761                 /* new demod instance */
762                 dprintk("%s using new instance\n", __func__);
763                 break;
764         default:
765                 /* existing demod instance */
766                 dprintk("%s using existing instance\n", __func__);
767                 break;
768         }
769
770         /* setup the state */
771         state->config = config;
772         state->i2c = i2c;
773         state->operational_mode = AU8522_DIGITAL_MODE;
774
775         /* create dvb_frontend */
776         memcpy(&state->frontend.ops, &au8522_ops,
777                sizeof(struct dvb_frontend_ops));
778         state->frontend.demodulator_priv = state;
779
780         state->frontend.ops.analog_ops.i2c_gate_ctrl = au8522_analog_i2c_gate_ctrl;
781
782         if (au8522_init(&state->frontend) != 0) {
783                 printk(KERN_ERR "%s: Failed to initialize correctly\n",
784                         __func__);
785                 goto error;
786         }
787
788         /* Note: Leaving the I2C gate open here. */
789         au8522_i2c_gate_ctrl(&state->frontend, 1);
790
791         return &state->frontend;
792
793 error:
794         au8522_release_state(state);
795         return NULL;
796 }
797 EXPORT_SYMBOL(au8522_attach);
798
799 static struct dvb_frontend_ops au8522_ops = {
800         .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
801         .info = {
802                 .name                   = "Auvitek AU8522 QAM/8VSB Frontend",
803                 .frequency_min          = 54000000,
804                 .frequency_max          = 858000000,
805                 .frequency_stepsize     = 62500,
806                 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
807         },
808
809         .init                 = au8522_init,
810         .sleep                = au8522_sleep,
811         .i2c_gate_ctrl        = au8522_i2c_gate_ctrl,
812         .set_frontend         = au8522_set_frontend,
813         .get_frontend         = au8522_get_frontend,
814         .get_tune_settings    = au8522_get_tune_settings,
815         .read_status          = au8522_read_status,
816         .read_ber             = au8522_read_ber,
817         .read_signal_strength = au8522_read_signal_strength,
818         .read_snr             = au8522_read_snr,
819         .read_ucblocks        = au8522_read_ucblocks,
820         .release              = au8522_release,
821 };
822
823 module_param(debug, int, 0644);
824 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
825
826 MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
827 MODULE_AUTHOR("Steven Toth");
828 MODULE_LICENSE("GPL");