[ARM] pxa: update defconfig for Verdex Pro
[linux-2.6/verdex.git] / drivers / media / dvb / frontends / au8522_dig.c
blob956b80f4979ca95af361162ec6adde484a1c9c89
1 /*
2 Auvitek AU8522 QAM/8VSB demodulator driver
4 Copyright (C) 2008 Steven Toth <stoth@linuxtv.org>
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.
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.
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.
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 #include "au8522_priv.h"
32 static int debug;
34 /* Despite the name "hybrid_tuner", the framework works just as well for
35 hybrid demodulators as well... */
36 static LIST_HEAD(hybrid_tuner_instance_list);
37 static DEFINE_MUTEX(au8522_list_mutex);
39 #define dprintk(arg...)\
40 do { if (debug)\
41 printk(arg);\
42 } while (0)
44 /* 16 bit registers, 8 bit values */
45 int au8522_writereg(struct au8522_state *state, u16 reg, u8 data)
47 int ret;
48 u8 buf[] = { (reg >> 8) | 0x80, reg & 0xff, data };
50 struct i2c_msg msg = { .addr = state->config->demod_address,
51 .flags = 0, .buf = buf, .len = 3 };
53 ret = i2c_transfer(state->i2c, &msg, 1);
55 if (ret != 1)
56 printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
57 "ret == %i)\n", __func__, reg, data, ret);
59 return (ret != 1) ? -1 : 0;
62 u8 au8522_readreg(struct au8522_state *state, u16 reg)
64 int ret;
65 u8 b0[] = { (reg >> 8) | 0x40, reg & 0xff };
66 u8 b1[] = { 0 };
68 struct i2c_msg msg[] = {
69 { .addr = state->config->demod_address, .flags = 0,
70 .buf = b0, .len = 2 },
71 { .addr = state->config->demod_address, .flags = I2C_M_RD,
72 .buf = b1, .len = 1 } };
74 ret = i2c_transfer(state->i2c, msg, 2);
76 if (ret != 2)
77 printk(KERN_ERR "%s: readreg error (ret == %i)\n",
78 __func__, ret);
79 return b1[0];
82 static int au8522_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
84 struct au8522_state *state = fe->demodulator_priv;
86 dprintk("%s(%d)\n", __func__, enable);
88 if (enable)
89 return au8522_writereg(state, 0x106, 1);
90 else
91 return au8522_writereg(state, 0x106, 0);
94 struct mse2snr_tab {
95 u16 val;
96 u16 data;
99 /* VSB SNR lookup table */
100 static struct mse2snr_tab vsb_mse2snr_tab[] = {
101 { 0, 270 },
102 { 2, 250 },
103 { 3, 240 },
104 { 5, 230 },
105 { 7, 220 },
106 { 9, 210 },
107 { 12, 200 },
108 { 13, 195 },
109 { 15, 190 },
110 { 17, 185 },
111 { 19, 180 },
112 { 21, 175 },
113 { 24, 170 },
114 { 27, 165 },
115 { 31, 160 },
116 { 32, 158 },
117 { 33, 156 },
118 { 36, 152 },
119 { 37, 150 },
120 { 39, 148 },
121 { 40, 146 },
122 { 41, 144 },
123 { 43, 142 },
124 { 44, 140 },
125 { 48, 135 },
126 { 50, 130 },
127 { 43, 142 },
128 { 53, 125 },
129 { 56, 120 },
130 { 256, 115 },
133 /* QAM64 SNR lookup table */
134 static struct mse2snr_tab qam64_mse2snr_tab[] = {
135 { 15, 0 },
136 { 16, 290 },
137 { 17, 288 },
138 { 18, 286 },
139 { 19, 284 },
140 { 20, 282 },
141 { 21, 281 },
142 { 22, 279 },
143 { 23, 277 },
144 { 24, 275 },
145 { 25, 273 },
146 { 26, 271 },
147 { 27, 269 },
148 { 28, 268 },
149 { 29, 266 },
150 { 30, 264 },
151 { 31, 262 },
152 { 32, 260 },
153 { 33, 259 },
154 { 34, 258 },
155 { 35, 256 },
156 { 36, 255 },
157 { 37, 254 },
158 { 38, 252 },
159 { 39, 251 },
160 { 40, 250 },
161 { 41, 249 },
162 { 42, 248 },
163 { 43, 246 },
164 { 44, 245 },
165 { 45, 244 },
166 { 46, 242 },
167 { 47, 241 },
168 { 48, 240 },
169 { 50, 239 },
170 { 51, 238 },
171 { 53, 237 },
172 { 54, 236 },
173 { 56, 235 },
174 { 57, 234 },
175 { 59, 233 },
176 { 60, 232 },
177 { 62, 231 },
178 { 63, 230 },
179 { 65, 229 },
180 { 67, 228 },
181 { 68, 227 },
182 { 70, 226 },
183 { 71, 225 },
184 { 73, 224 },
185 { 74, 223 },
186 { 76, 222 },
187 { 78, 221 },
188 { 80, 220 },
189 { 82, 219 },
190 { 85, 218 },
191 { 88, 217 },
192 { 90, 216 },
193 { 92, 215 },
194 { 93, 214 },
195 { 94, 212 },
196 { 95, 211 },
197 { 97, 210 },
198 { 99, 209 },
199 { 101, 208 },
200 { 102, 207 },
201 { 104, 206 },
202 { 107, 205 },
203 { 111, 204 },
204 { 114, 203 },
205 { 118, 202 },
206 { 122, 201 },
207 { 125, 200 },
208 { 128, 199 },
209 { 130, 198 },
210 { 132, 197 },
211 { 256, 190 },
214 /* QAM256 SNR lookup table */
215 static struct mse2snr_tab qam256_mse2snr_tab[] = {
216 { 16, 0 },
217 { 17, 400 },
218 { 18, 398 },
219 { 19, 396 },
220 { 20, 394 },
221 { 21, 392 },
222 { 22, 390 },
223 { 23, 388 },
224 { 24, 386 },
225 { 25, 384 },
226 { 26, 382 },
227 { 27, 380 },
228 { 28, 379 },
229 { 29, 378 },
230 { 30, 377 },
231 { 31, 376 },
232 { 32, 375 },
233 { 33, 374 },
234 { 34, 373 },
235 { 35, 372 },
236 { 36, 371 },
237 { 37, 370 },
238 { 38, 362 },
239 { 39, 354 },
240 { 40, 346 },
241 { 41, 338 },
242 { 42, 330 },
243 { 43, 328 },
244 { 44, 326 },
245 { 45, 324 },
246 { 46, 322 },
247 { 47, 320 },
248 { 48, 319 },
249 { 49, 318 },
250 { 50, 317 },
251 { 51, 316 },
252 { 52, 315 },
253 { 53, 314 },
254 { 54, 313 },
255 { 55, 312 },
256 { 56, 311 },
257 { 57, 310 },
258 { 58, 308 },
259 { 59, 306 },
260 { 60, 304 },
261 { 61, 302 },
262 { 62, 300 },
263 { 63, 298 },
264 { 65, 295 },
265 { 68, 294 },
266 { 70, 293 },
267 { 73, 292 },
268 { 76, 291 },
269 { 78, 290 },
270 { 79, 289 },
271 { 81, 288 },
272 { 82, 287 },
273 { 83, 286 },
274 { 84, 285 },
275 { 85, 284 },
276 { 86, 283 },
277 { 88, 282 },
278 { 89, 281 },
279 { 256, 280 },
282 static int au8522_mse2snr_lookup(struct mse2snr_tab *tab, int sz, int mse,
283 u16 *snr)
285 int i, ret = -EINVAL;
286 dprintk("%s()\n", __func__);
288 for (i = 0; i < sz; i++) {
289 if (mse < tab[i].val) {
290 *snr = tab[i].data;
291 ret = 0;
292 break;
295 dprintk("%s() snr=%d\n", __func__, *snr);
296 return ret;
299 static int au8522_set_if(struct dvb_frontend *fe, enum au8522_if_freq if_freq)
301 struct au8522_state *state = fe->demodulator_priv;
302 u8 r0b5, r0b6, r0b7;
303 char *ifmhz;
305 switch (if_freq) {
306 case AU8522_IF_3_25MHZ:
307 ifmhz = "3.25";
308 r0b5 = 0x00;
309 r0b6 = 0x3d;
310 r0b7 = 0xa0;
311 break;
312 case AU8522_IF_4MHZ:
313 ifmhz = "4.00";
314 r0b5 = 0x00;
315 r0b6 = 0x4b;
316 r0b7 = 0xd9;
317 break;
318 case AU8522_IF_6MHZ:
319 ifmhz = "6.00";
320 r0b5 = 0xfb;
321 r0b6 = 0x8e;
322 r0b7 = 0x39;
323 break;
324 default:
325 dprintk("%s() IF Frequency not supported\n", __func__);
326 return -EINVAL;
328 dprintk("%s() %s MHz\n", __func__, ifmhz);
329 au8522_writereg(state, 0x80b5, r0b5);
330 au8522_writereg(state, 0x80b6, r0b6);
331 au8522_writereg(state, 0x80b7, r0b7);
333 return 0;
336 /* VSB Modulation table */
337 static struct {
338 u16 reg;
339 u16 data;
340 } VSB_mod_tab[] = {
341 { 0x8090, 0x84 },
342 { 0x4092, 0x11 },
343 { 0x2005, 0x00 },
344 { 0x8091, 0x80 },
345 { 0x80a3, 0x0c },
346 { 0x80a4, 0xe8 },
347 { 0x8081, 0xc4 },
348 { 0x80a5, 0x40 },
349 { 0x80a7, 0x40 },
350 { 0x80a6, 0x67 },
351 { 0x8262, 0x20 },
352 { 0x821c, 0x30 },
353 { 0x80d8, 0x1a },
354 { 0x8227, 0xa0 },
355 { 0x8121, 0xff },
356 { 0x80a8, 0xf0 },
357 { 0x80a9, 0x05 },
358 { 0x80aa, 0x77 },
359 { 0x80ab, 0xf0 },
360 { 0x80ac, 0x05 },
361 { 0x80ad, 0x77 },
362 { 0x80ae, 0x41 },
363 { 0x80af, 0x66 },
364 { 0x821b, 0xcc },
365 { 0x821d, 0x80 },
366 { 0x80a4, 0xe8 },
367 { 0x8231, 0x13 },
370 /* QAM64 Modulation table */
371 static struct {
372 u16 reg;
373 u16 data;
374 } QAM64_mod_tab[] = {
375 { 0x00a3, 0x09 },
376 { 0x00a4, 0x00 },
377 { 0x0081, 0xc4 },
378 { 0x00a5, 0x40 },
379 { 0x00aa, 0x77 },
380 { 0x00ad, 0x77 },
381 { 0x00a6, 0x67 },
382 { 0x0262, 0x20 },
383 { 0x021c, 0x30 },
384 { 0x00b8, 0x3e },
385 { 0x00b9, 0xf0 },
386 { 0x00ba, 0x01 },
387 { 0x00bb, 0x18 },
388 { 0x00bc, 0x50 },
389 { 0x00bd, 0x00 },
390 { 0x00be, 0xea },
391 { 0x00bf, 0xef },
392 { 0x00c0, 0xfc },
393 { 0x00c1, 0xbd },
394 { 0x00c2, 0x1f },
395 { 0x00c3, 0xfc },
396 { 0x00c4, 0xdd },
397 { 0x00c5, 0xaf },
398 { 0x00c6, 0x00 },
399 { 0x00c7, 0x38 },
400 { 0x00c8, 0x30 },
401 { 0x00c9, 0x05 },
402 { 0x00ca, 0x4a },
403 { 0x00cb, 0xd0 },
404 { 0x00cc, 0x01 },
405 { 0x00cd, 0xd9 },
406 { 0x00ce, 0x6f },
407 { 0x00cf, 0xf9 },
408 { 0x00d0, 0x70 },
409 { 0x00d1, 0xdf },
410 { 0x00d2, 0xf7 },
411 { 0x00d3, 0xc2 },
412 { 0x00d4, 0xdf },
413 { 0x00d5, 0x02 },
414 { 0x00d6, 0x9a },
415 { 0x00d7, 0xd0 },
416 { 0x0250, 0x0d },
417 { 0x0251, 0xcd },
418 { 0x0252, 0xe0 },
419 { 0x0253, 0x05 },
420 { 0x0254, 0xa7 },
421 { 0x0255, 0xff },
422 { 0x0256, 0xed },
423 { 0x0257, 0x5b },
424 { 0x0258, 0xae },
425 { 0x0259, 0xe6 },
426 { 0x025a, 0x3d },
427 { 0x025b, 0x0f },
428 { 0x025c, 0x0d },
429 { 0x025d, 0xea },
430 { 0x025e, 0xf2 },
431 { 0x025f, 0x51 },
432 { 0x0260, 0xf5 },
433 { 0x0261, 0x06 },
434 { 0x021a, 0x00 },
435 { 0x0546, 0x40 },
436 { 0x0210, 0xc7 },
437 { 0x0211, 0xaa },
438 { 0x0212, 0xab },
439 { 0x0213, 0x02 },
440 { 0x0502, 0x00 },
441 { 0x0121, 0x04 },
442 { 0x0122, 0x04 },
443 { 0x052e, 0x10 },
444 { 0x00a4, 0xca },
445 { 0x00a7, 0x40 },
446 { 0x0526, 0x01 },
449 /* QAM256 Modulation table */
450 static struct {
451 u16 reg;
452 u16 data;
453 } QAM256_mod_tab[] = {
454 { 0x80a3, 0x09 },
455 { 0x80a4, 0x00 },
456 { 0x8081, 0xc4 },
457 { 0x80a5, 0x40 },
458 { 0x80aa, 0x77 },
459 { 0x80ad, 0x77 },
460 { 0x80a6, 0x67 },
461 { 0x8262, 0x20 },
462 { 0x821c, 0x30 },
463 { 0x80b8, 0x3e },
464 { 0x80b9, 0xf0 },
465 { 0x80ba, 0x01 },
466 { 0x80bb, 0x18 },
467 { 0x80bc, 0x50 },
468 { 0x80bd, 0x00 },
469 { 0x80be, 0xea },
470 { 0x80bf, 0xef },
471 { 0x80c0, 0xfc },
472 { 0x80c1, 0xbd },
473 { 0x80c2, 0x1f },
474 { 0x80c3, 0xfc },
475 { 0x80c4, 0xdd },
476 { 0x80c5, 0xaf },
477 { 0x80c6, 0x00 },
478 { 0x80c7, 0x38 },
479 { 0x80c8, 0x30 },
480 { 0x80c9, 0x05 },
481 { 0x80ca, 0x4a },
482 { 0x80cb, 0xd0 },
483 { 0x80cc, 0x01 },
484 { 0x80cd, 0xd9 },
485 { 0x80ce, 0x6f },
486 { 0x80cf, 0xf9 },
487 { 0x80d0, 0x70 },
488 { 0x80d1, 0xdf },
489 { 0x80d2, 0xf7 },
490 { 0x80d3, 0xc2 },
491 { 0x80d4, 0xdf },
492 { 0x80d5, 0x02 },
493 { 0x80d6, 0x9a },
494 { 0x80d7, 0xd0 },
495 { 0x8250, 0x0d },
496 { 0x8251, 0xcd },
497 { 0x8252, 0xe0 },
498 { 0x8253, 0x05 },
499 { 0x8254, 0xa7 },
500 { 0x8255, 0xff },
501 { 0x8256, 0xed },
502 { 0x8257, 0x5b },
503 { 0x8258, 0xae },
504 { 0x8259, 0xe6 },
505 { 0x825a, 0x3d },
506 { 0x825b, 0x0f },
507 { 0x825c, 0x0d },
508 { 0x825d, 0xea },
509 { 0x825e, 0xf2 },
510 { 0x825f, 0x51 },
511 { 0x8260, 0xf5 },
512 { 0x8261, 0x06 },
513 { 0x821a, 0x00 },
514 { 0x8546, 0x40 },
515 { 0x8210, 0x26 },
516 { 0x8211, 0xf6 },
517 { 0x8212, 0x84 },
518 { 0x8213, 0x02 },
519 { 0x8502, 0x01 },
520 { 0x8121, 0x04 },
521 { 0x8122, 0x04 },
522 { 0x852e, 0x10 },
523 { 0x80a4, 0xca },
524 { 0x80a7, 0x40 },
525 { 0x8526, 0x01 },
528 static int au8522_enable_modulation(struct dvb_frontend *fe,
529 fe_modulation_t m)
531 struct au8522_state *state = fe->demodulator_priv;
532 int i;
534 dprintk("%s(0x%08x)\n", __func__, m);
536 switch (m) {
537 case VSB_8:
538 dprintk("%s() VSB_8\n", __func__);
539 for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
540 au8522_writereg(state,
541 VSB_mod_tab[i].reg,
542 VSB_mod_tab[i].data);
543 au8522_set_if(fe, state->config->vsb_if);
544 break;
545 case QAM_64:
546 dprintk("%s() QAM 64\n", __func__);
547 for (i = 0; i < ARRAY_SIZE(QAM64_mod_tab); i++)
548 au8522_writereg(state,
549 QAM64_mod_tab[i].reg,
550 QAM64_mod_tab[i].data);
551 au8522_set_if(fe, state->config->qam_if);
552 break;
553 case QAM_256:
554 dprintk("%s() QAM 256\n", __func__);
555 for (i = 0; i < ARRAY_SIZE(QAM256_mod_tab); i++)
556 au8522_writereg(state,
557 QAM256_mod_tab[i].reg,
558 QAM256_mod_tab[i].data);
559 au8522_set_if(fe, state->config->qam_if);
560 break;
561 default:
562 dprintk("%s() Invalid modulation\n", __func__);
563 return -EINVAL;
566 state->current_modulation = m;
568 return 0;
571 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
572 static int au8522_set_frontend(struct dvb_frontend *fe,
573 struct dvb_frontend_parameters *p)
575 struct au8522_state *state = fe->demodulator_priv;
576 int ret = -EINVAL;
578 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
580 if ((state->current_frequency == p->frequency) &&
581 (state->current_modulation == p->u.vsb.modulation))
582 return 0;
584 au8522_enable_modulation(fe, p->u.vsb.modulation);
586 /* Allow the demod to settle */
587 msleep(100);
589 if (fe->ops.tuner_ops.set_params) {
590 if (fe->ops.i2c_gate_ctrl)
591 fe->ops.i2c_gate_ctrl(fe, 1);
592 ret = fe->ops.tuner_ops.set_params(fe, p);
593 if (fe->ops.i2c_gate_ctrl)
594 fe->ops.i2c_gate_ctrl(fe, 0);
597 if (ret < 0)
598 return ret;
600 state->current_frequency = p->frequency;
602 return 0;
605 /* Reset the demod hardware and reset all of the configuration registers
606 to a default state. */
607 int au8522_init(struct dvb_frontend *fe)
609 struct au8522_state *state = fe->demodulator_priv;
610 dprintk("%s()\n", __func__);
612 au8522_writereg(state, 0xa4, 1 << 5);
614 au8522_i2c_gate_ctrl(fe, 1);
616 return 0;
619 static int au8522_led_gpio_enable(struct au8522_state *state, int onoff)
621 struct au8522_led_config *led_config = state->config->led_cfg;
622 u8 val;
624 /* bail out if we cant control an LED */
625 if (!led_config || !led_config->gpio_output ||
626 !led_config->gpio_output_enable || !led_config->gpio_output_disable)
627 return 0;
629 val = au8522_readreg(state, 0x4000 |
630 (led_config->gpio_output & ~0xc000));
631 if (onoff) {
632 /* enable GPIO output */
633 val &= ~((led_config->gpio_output_enable >> 8) & 0xff);
634 val |= (led_config->gpio_output_enable & 0xff);
635 } else {
636 /* disable GPIO output */
637 val &= ~((led_config->gpio_output_disable >> 8) & 0xff);
638 val |= (led_config->gpio_output_disable & 0xff);
640 return au8522_writereg(state, 0x8000 |
641 (led_config->gpio_output & ~0xc000), val);
644 /* led = 0 | off
645 * led = 1 | signal ok
646 * led = 2 | signal strong
647 * led < 0 | only light led if leds are currently off
649 static int au8522_led_ctrl(struct au8522_state *state, int led)
651 struct au8522_led_config *led_config = state->config->led_cfg;
652 int i, ret = 0;
654 /* bail out if we cant control an LED */
655 if (!led_config || !led_config->gpio_leds ||
656 !led_config->num_led_states || !led_config->led_states)
657 return 0;
659 if (led < 0) {
660 /* if LED is already lit, then leave it as-is */
661 if (state->led_state)
662 return 0;
663 else
664 led *= -1;
667 /* toggle LED if changing state */
668 if (state->led_state != led) {
669 u8 val;
671 dprintk("%s: %d\n", __func__, led);
673 au8522_led_gpio_enable(state, 1);
675 val = au8522_readreg(state, 0x4000 |
676 (led_config->gpio_leds & ~0xc000));
678 /* start with all leds off */
679 for (i = 0; i < led_config->num_led_states; i++)
680 val &= ~led_config->led_states[i];
682 /* set selected LED state */
683 if (led < led_config->num_led_states)
684 val |= led_config->led_states[led];
685 else if (led_config->num_led_states)
686 val |=
687 led_config->led_states[led_config->num_led_states - 1];
689 ret = au8522_writereg(state, 0x8000 |
690 (led_config->gpio_leds & ~0xc000), val);
691 if (ret < 0)
692 return ret;
694 state->led_state = led;
696 if (led == 0)
697 au8522_led_gpio_enable(state, 0);
700 return 0;
703 int au8522_sleep(struct dvb_frontend *fe)
705 struct au8522_state *state = fe->demodulator_priv;
706 dprintk("%s()\n", __func__);
708 /* turn off led */
709 au8522_led_ctrl(state, 0);
711 /* Power down the chip */
712 au8522_writereg(state, 0xa4, 1 << 5);
714 state->current_frequency = 0;
716 return 0;
719 static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status)
721 struct au8522_state *state = fe->demodulator_priv;
722 u8 reg;
723 u32 tuner_status = 0;
725 *status = 0;
727 if (state->current_modulation == VSB_8) {
728 dprintk("%s() Checking VSB_8\n", __func__);
729 reg = au8522_readreg(state, 0x4088);
730 if ((reg & 0x03) == 0x03)
731 *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
732 } else {
733 dprintk("%s() Checking QAM\n", __func__);
734 reg = au8522_readreg(state, 0x4541);
735 if (reg & 0x80)
736 *status |= FE_HAS_VITERBI;
737 if (reg & 0x20)
738 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
741 switch (state->config->status_mode) {
742 case AU8522_DEMODLOCKING:
743 dprintk("%s() DEMODLOCKING\n", __func__);
744 if (*status & FE_HAS_VITERBI)
745 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
746 break;
747 case AU8522_TUNERLOCKING:
748 /* Get the tuner status */
749 dprintk("%s() TUNERLOCKING\n", __func__);
750 if (fe->ops.tuner_ops.get_status) {
751 if (fe->ops.i2c_gate_ctrl)
752 fe->ops.i2c_gate_ctrl(fe, 1);
754 fe->ops.tuner_ops.get_status(fe, &tuner_status);
756 if (fe->ops.i2c_gate_ctrl)
757 fe->ops.i2c_gate_ctrl(fe, 0);
759 if (tuner_status)
760 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
761 break;
763 state->fe_status = *status;
765 if (*status & FE_HAS_LOCK)
766 /* turn on LED, if it isn't on already */
767 au8522_led_ctrl(state, -1);
768 else
769 /* turn off LED */
770 au8522_led_ctrl(state, 0);
772 dprintk("%s() status 0x%08x\n", __func__, *status);
774 return 0;
777 static int au8522_led_status(struct au8522_state *state, const u16 *snr)
779 struct au8522_led_config *led_config = state->config->led_cfg;
780 int led;
781 u16 strong;
783 /* bail out if we cant control an LED */
784 if (!led_config)
785 return 0;
787 if (0 == (state->fe_status & FE_HAS_LOCK))
788 return au8522_led_ctrl(state, 0);
789 else if (state->current_modulation == QAM_256)
790 strong = led_config->qam256_strong;
791 else if (state->current_modulation == QAM_64)
792 strong = led_config->qam64_strong;
793 else /* (state->current_modulation == VSB_8) */
794 strong = led_config->vsb8_strong;
796 if (*snr >= strong)
797 led = 2;
798 else
799 led = 1;
801 if ((state->led_state) &&
802 (((strong < *snr) ? (*snr - strong) : (strong - *snr)) <= 10))
803 /* snr didn't change enough to bother
804 * changing the color of the led */
805 return 0;
807 return au8522_led_ctrl(state, led);
810 static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
812 struct au8522_state *state = fe->demodulator_priv;
813 int ret = -EINVAL;
815 dprintk("%s()\n", __func__);
817 if (state->current_modulation == QAM_256)
818 ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
819 ARRAY_SIZE(qam256_mse2snr_tab),
820 au8522_readreg(state, 0x4522),
821 snr);
822 else if (state->current_modulation == QAM_64)
823 ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
824 ARRAY_SIZE(qam64_mse2snr_tab),
825 au8522_readreg(state, 0x4522),
826 snr);
827 else /* VSB_8 */
828 ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
829 ARRAY_SIZE(vsb_mse2snr_tab),
830 au8522_readreg(state, 0x4311),
831 snr);
833 if (state->config->led_cfg)
834 au8522_led_status(state, snr);
836 return ret;
839 static int au8522_read_signal_strength(struct dvb_frontend *fe,
840 u16 *signal_strength)
842 return au8522_read_snr(fe, signal_strength);
845 static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
847 struct au8522_state *state = fe->demodulator_priv;
849 if (state->current_modulation == VSB_8)
850 *ucblocks = au8522_readreg(state, 0x4087);
851 else
852 *ucblocks = au8522_readreg(state, 0x4543);
854 return 0;
857 static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
859 return au8522_read_ucblocks(fe, ber);
862 static int au8522_get_frontend(struct dvb_frontend *fe,
863 struct dvb_frontend_parameters *p)
865 struct au8522_state *state = fe->demodulator_priv;
867 p->frequency = state->current_frequency;
868 p->u.vsb.modulation = state->current_modulation;
870 return 0;
873 static int au8522_get_tune_settings(struct dvb_frontend *fe,
874 struct dvb_frontend_tune_settings *tune)
876 tune->min_delay_ms = 1000;
877 return 0;
880 static struct dvb_frontend_ops au8522_ops;
882 int au8522_get_state(struct au8522_state **state, struct i2c_adapter *i2c,
883 u8 client_address)
885 int ret;
887 mutex_lock(&au8522_list_mutex);
888 ret = hybrid_tuner_request_state(struct au8522_state, (*state),
889 hybrid_tuner_instance_list,
890 i2c, client_address, "au8522");
891 mutex_unlock(&au8522_list_mutex);
893 return ret;
896 void au8522_release_state(struct au8522_state *state)
898 mutex_lock(&au8522_list_mutex);
899 if (state != NULL)
900 hybrid_tuner_release_state(state);
901 mutex_unlock(&au8522_list_mutex);
905 static void au8522_release(struct dvb_frontend *fe)
907 struct au8522_state *state = fe->demodulator_priv;
908 au8522_release_state(state);
911 struct dvb_frontend *au8522_attach(const struct au8522_config *config,
912 struct i2c_adapter *i2c)
914 struct au8522_state *state = NULL;
915 int instance;
917 /* allocate memory for the internal state */
918 instance = au8522_get_state(&state, i2c, config->demod_address);
919 switch (instance) {
920 case 0:
921 dprintk("%s state allocation failed\n", __func__);
922 break;
923 case 1:
924 /* new demod instance */
925 dprintk("%s using new instance\n", __func__);
926 break;
927 default:
928 /* existing demod instance */
929 dprintk("%s using existing instance\n", __func__);
930 break;
933 /* setup the state */
934 state->config = config;
935 state->i2c = i2c;
936 /* create dvb_frontend */
937 memcpy(&state->frontend.ops, &au8522_ops,
938 sizeof(struct dvb_frontend_ops));
939 state->frontend.demodulator_priv = state;
941 if (au8522_init(&state->frontend) != 0) {
942 printk(KERN_ERR "%s: Failed to initialize correctly\n",
943 __func__);
944 goto error;
947 /* Note: Leaving the I2C gate open here. */
948 au8522_i2c_gate_ctrl(&state->frontend, 1);
950 return &state->frontend;
952 error:
953 au8522_release_state(state);
954 return NULL;
956 EXPORT_SYMBOL(au8522_attach);
958 static struct dvb_frontend_ops au8522_ops = {
960 .info = {
961 .name = "Auvitek AU8522 QAM/8VSB Frontend",
962 .type = FE_ATSC,
963 .frequency_min = 54000000,
964 .frequency_max = 858000000,
965 .frequency_stepsize = 62500,
966 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
969 .init = au8522_init,
970 .sleep = au8522_sleep,
971 .i2c_gate_ctrl = au8522_i2c_gate_ctrl,
972 .set_frontend = au8522_set_frontend,
973 .get_frontend = au8522_get_frontend,
974 .get_tune_settings = au8522_get_tune_settings,
975 .read_status = au8522_read_status,
976 .read_ber = au8522_read_ber,
977 .read_signal_strength = au8522_read_signal_strength,
978 .read_snr = au8522_read_snr,
979 .read_ucblocks = au8522_read_ucblocks,
980 .release = au8522_release,
983 module_param(debug, int, 0644);
984 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
986 MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
987 MODULE_AUTHOR("Steven Toth");
988 MODULE_LICENSE("GPL");