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