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