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