V4L/DVB (7637): au0828: Typo
[linux-2.6-block.git] / drivers / media / dvb / frontends / au8522.c
CommitLineData
265a6510
ST
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
32struct au8522_state {
33
e059b0fa 34 struct i2c_adapter *i2c;
265a6510
ST
35
36 /* configuration settings */
e059b0fa 37 const struct au8522_config *config;
265a6510
ST
38
39 struct dvb_frontend frontend;
40
41 u32 current_frequency;
42 fe_modulation_t current_modulation;
43
44};
45
46static int debug = 0;
47#define dprintk if (debug) printk
48
49/* 16 bit registers, 8 bit values */
e059b0fa 50static int au8522_writereg(struct au8522_state *state, u16 reg, u8 data)
265a6510
ST
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, "
ce1719a6 62 "ret == %i)\n", __func__, reg, data, ret);
265a6510
ST
63
64 return (ret != 1) ? -1 : 0;
65}
66
e059b0fa 67static u8 au8522_readreg(struct au8522_state *state, u16 reg)
265a6510
ST
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)
ce1719a6
MK
82 printk(KERN_ERR "%s: readreg error (ret == %i)\n",
83 __func__, ret);
265a6510
ST
84 return b1[0];
85}
86
e059b0fa 87static int au8522_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
265a6510 88{
e059b0fa 89 struct au8522_state *state = fe->demodulator_priv;
265a6510 90
ce1719a6 91 dprintk("%s(%d)\n", __func__, enable);
265a6510
ST
92
93 if (enable)
94 return au8522_writereg(state, 0x106, 1);
95 else
96 return au8522_writereg(state, 0x106, 0);
97}
98
0daa5de7 99struct mse2snr_tab {
f01699b4
ST
100 u16 val;
101 u16 data;
0daa5de7
MK
102};
103
104/* VSB SNR lookup table */
105static struct mse2snr_tab vsb_mse2snr_tab[] = {
f01699b4
ST
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 */
0daa5de7 139static struct mse2snr_tab qam64_mse2snr_tab[] = {
f01699b4
ST
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 */
0daa5de7 220static struct mse2snr_tab qam256_mse2snr_tab[] = {
f01699b4
ST
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
0daa5de7 287static int au8522_mse2snr_lookup(struct mse2snr_tab *tab, int sz, int mse, u16 *snr)
f01699b4
ST
288{
289 int i, ret = -EINVAL;
290 dprintk("%s()\n", __func__);
291
0daa5de7
MK
292 for (i = 0; i < sz; i++) {
293 if (mse < tab[i].val) {
294 *snr = tab[i].data;
f01699b4
ST
295 ret = 0;
296 break;
297 }
298 }
299 dprintk("%s() snr=%d\n", __func__, *snr);
300 return ret;
301}
302
303/* VSB Modulation table */
304static 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 */
341static 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
e059b0fa
MK
422static int au8522_enable_modulation(struct dvb_frontend *fe,
423 fe_modulation_t m)
265a6510 424{
e059b0fa 425 struct au8522_state *state = fe->demodulator_priv;
f01699b4 426 int i;
265a6510 427
ce1719a6 428 dprintk("%s(0x%08x)\n", __func__, m);
265a6510
ST
429
430 switch(m) {
431 case VSB_8:
ce1719a6 432 dprintk("%s() VSB_8\n", __func__);
f01699b4
ST
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);
265a6510
ST
437 break;
438 case QAM_64:
439 case QAM_256:
f01699b4
ST
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);
265a6510
ST
445 break;
446 default:
ce1719a6 447 dprintk("%s() Invalid modulation\n", __func__);
265a6510
ST
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 */
e059b0fa
MK
457static int au8522_set_frontend(struct dvb_frontend *fe,
458 struct dvb_frontend_parameters *p)
265a6510 459{
e059b0fa 460 struct au8522_state *state = fe->demodulator_priv;
265a6510 461
ce1719a6 462 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
265a6510
ST
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. */
e059b0fa 482static int au8522_init(struct dvb_frontend *fe)
265a6510 483{
e059b0fa 484 struct au8522_state *state = fe->demodulator_priv;
ce1719a6 485 dprintk("%s()\n", __func__);
265a6510
ST
486
487 au8522_writereg(state, 0xa4, 1 << 5);
488
489 au8522_i2c_gate_ctrl(fe, 1);
490
491 return 0;
492}
493
e059b0fa 494static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status)
265a6510 495{
e059b0fa 496 struct au8522_state *state = fe->demodulator_priv;
265a6510
ST
497 u8 reg;
498 u32 tuner_status = 0;
499
500 *status = 0;
501
502 if (state->current_modulation == VSB_8) {
ce1719a6 503 dprintk("%s() Checking VSB_8\n", __func__);
265a6510 504 reg = au8522_readreg(state, 0x4088);
e059b0fa 505 if (reg & 0x01)
265a6510 506 *status |= FE_HAS_VITERBI;
e059b0fa 507 if (reg & 0x02)
265a6510
ST
508 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
509 } else {
ce1719a6 510 dprintk("%s() Checking QAM\n", __func__);
265a6510 511 reg = au8522_readreg(state, 0x4541);
e059b0fa 512 if (reg & 0x80)
265a6510 513 *status |= FE_HAS_VITERBI;
e059b0fa 514 if (reg & 0x20)
265a6510
ST
515 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
516 }
517
518 switch(state->config->status_mode) {
519 case AU8522_DEMODLOCKING:
ce1719a6 520 dprintk("%s() DEMODLOCKING\n", __func__);
265a6510
ST
521 if (*status & FE_HAS_VITERBI)
522 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
523 break;
524 case AU8522_TUNERLOCKING:
525 /* Get the tuner status */
ce1719a6 526 dprintk("%s() TUNERLOCKING\n", __func__);
265a6510
ST
527 if (fe->ops.tuner_ops.get_status) {
528 if (fe->ops.i2c_gate_ctrl)
529 fe->ops.i2c_gate_ctrl(fe, 1);
530
531 fe->ops.tuner_ops.get_status(fe, &tuner_status);
532
533 if (fe->ops.i2c_gate_ctrl)
534 fe->ops.i2c_gate_ctrl(fe, 0);
535 }
536 if (tuner_status)
537 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
538 break;
539 }
540
ce1719a6 541 dprintk("%s() status 0x%08x\n", __func__, *status);
265a6510
ST
542
543 return 0;
544}
545
f01699b4 546static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
fb8152cb
MK
547{
548 struct au8522_state *state = fe->demodulator_priv;
f01699b4 549 int ret = -EINVAL;
fb8152cb 550
ce1719a6 551 dprintk("%s()\n", __func__);
265a6510 552
f01699b4 553 if (state->current_modulation == QAM_256)
0daa5de7
MK
554 ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
555 ARRAY_SIZE(qam256_mse2snr_tab),
556 au8522_readreg(state, 0x4522),
557 snr);
f01699b4 558 else if (state->current_modulation == QAM_64)
0daa5de7
MK
559 ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
560 ARRAY_SIZE(qam64_mse2snr_tab),
561 au8522_readreg(state, 0x4522),
562 snr);
f01699b4 563 else /* VSB_8 */
0daa5de7
MK
564 ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
565 ARRAY_SIZE(vsb_mse2snr_tab),
566 au8522_readreg(state, 0x4311),
567 snr);
f01699b4
ST
568
569 return ret;
265a6510
ST
570}
571
e059b0fa
MK
572static int au8522_read_signal_strength(struct dvb_frontend *fe,
573 u16 *signal_strength)
265a6510
ST
574{
575 return au8522_read_snr(fe, signal_strength);
576}
577
e059b0fa 578static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
265a6510 579{
e059b0fa 580 struct au8522_state *state = fe->demodulator_priv;
265a6510 581
8973dc4b
MK
582 if (state->current_modulation == VSB_8)
583 *ucblocks = au8522_readreg(state, 0x4087);
584 else
585 *ucblocks = au8522_readreg(state, 0x4543);
265a6510
ST
586
587 return 0;
588}
589
e059b0fa 590static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
265a6510
ST
591{
592 return au8522_read_ucblocks(fe, ber);
593}
594
e059b0fa 595static int au8522_get_frontend(struct dvb_frontend *fe,
265a6510
ST
596 struct dvb_frontend_parameters *p)
597{
e059b0fa 598 struct au8522_state *state = fe->demodulator_priv;
265a6510
ST
599
600 p->frequency = state->current_frequency;
601 p->u.vsb.modulation = state->current_modulation;
602
603 return 0;
604}
605
e059b0fa
MK
606static int au8522_get_tune_settings(struct dvb_frontend *fe,
607 struct dvb_frontend_tune_settings *tune)
265a6510
ST
608{
609 tune->min_delay_ms = 1000;
610 return 0;
611}
612
e059b0fa 613static void au8522_release(struct dvb_frontend *fe)
265a6510 614{
e059b0fa 615 struct au8522_state *state = fe->demodulator_priv;
265a6510
ST
616 kfree(state);
617}
618
619static struct dvb_frontend_ops au8522_ops;
620
e059b0fa
MK
621struct dvb_frontend *au8522_attach(const struct au8522_config *config,
622 struct i2c_adapter *i2c)
265a6510 623{
e059b0fa 624 struct au8522_state *state = NULL;
265a6510
ST
625
626 /* allocate memory for the internal state */
627 state = kmalloc(sizeof(struct au8522_state), GFP_KERNEL);
628 if (state == NULL)
629 goto error;
630
631 /* setup the state */
632 state->config = config;
633 state->i2c = i2c;
634 /* create dvb_frontend */
635 memcpy(&state->frontend.ops, &au8522_ops,
636 sizeof(struct dvb_frontend_ops));
637 state->frontend.demodulator_priv = state;
638
639 if (au8522_init(&state->frontend) != 0) {
640 printk(KERN_ERR "%s: Failed to initialize correctly\n",
ce1719a6 641 __func__);
265a6510
ST
642 goto error;
643 }
644
645 /* Note: Leaving the I2C gate open here. */
646 au8522_i2c_gate_ctrl(&state->frontend, 1);
647
648 return &state->frontend;
649
650error:
651 kfree(state);
652 return NULL;
653}
654
655static struct dvb_frontend_ops au8522_ops = {
656
657 .info = {
658 .name = "Auvitek AU8522 QAM/8VSB Frontend",
659 .type = FE_ATSC,
660 .frequency_min = 54000000,
661 .frequency_max = 858000000,
662 .frequency_stepsize = 62500,
663 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
664 },
665
666 .init = au8522_init,
667 .i2c_gate_ctrl = au8522_i2c_gate_ctrl,
668 .set_frontend = au8522_set_frontend,
669 .get_frontend = au8522_get_frontend,
670 .get_tune_settings = au8522_get_tune_settings,
671 .read_status = au8522_read_status,
672 .read_ber = au8522_read_ber,
673 .read_signal_strength = au8522_read_signal_strength,
674 .read_snr = au8522_read_snr,
675 .read_ucblocks = au8522_read_ucblocks,
676 .release = au8522_release,
677};
678
679module_param(debug, int, 0644);
680MODULE_PARM_DESC(debug, "Enable verbose debug messages");
681
682MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
683MODULE_AUTHOR("Steven Toth");
684MODULE_LICENSE("GPL");
685
686EXPORT_SYMBOL(au8522_attach);
687
688/*
689 * Local variables:
690 * c-basic-offset: 8
691 */