blob: e340faacc78b938105c44d5c88bcf655bde51930 [file] [log] [blame]
Steven Toth265a6512008-04-18 21:34:00 -03001/*
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
Michael Krufkye059b0f2008-04-02 18:59:48 -030034 struct i2c_adapter *i2c;
Steven Toth265a6512008-04-18 21:34:00 -030035
36 /* configuration settings */
Michael Krufkye059b0f2008-04-02 18:59:48 -030037 const struct au8522_config *config;
Steven Toth265a6512008-04-18 21:34:00 -030038
39 struct dvb_frontend frontend;
40
41 u32 current_frequency;
42 fe_modulation_t current_modulation;
43
44};
45
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -030046static int debug;
47
48#define dprintk(arg...) do { \
49 if (debug) \
50 printk(##arg); } \
51 } while (0)
Steven Toth265a6512008-04-18 21:34:00 -030052
53/* 16 bit registers, 8 bit values */
Michael Krufkye059b0f2008-04-02 18:59:48 -030054static int au8522_writereg(struct au8522_state *state, u16 reg, u8 data)
Steven Toth265a6512008-04-18 21:34:00 -030055{
56 int ret;
57 u8 buf [] = { reg >> 8, reg & 0xff, data };
58
59 struct i2c_msg msg = { .addr = state->config->demod_address,
60 .flags = 0, .buf = buf, .len = 3 };
61
62 ret = i2c_transfer(state->i2c, &msg, 1);
63
64 if (ret != 1)
65 printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
Michael Krufkyce1719a2008-04-02 18:59:48 -030066 "ret == %i)\n", __func__, reg, data, ret);
Steven Toth265a6512008-04-18 21:34:00 -030067
68 return (ret != 1) ? -1 : 0;
69}
70
Michael Krufkye059b0f2008-04-02 18:59:48 -030071static u8 au8522_readreg(struct au8522_state *state, u16 reg)
Steven Toth265a6512008-04-18 21:34:00 -030072{
73 int ret;
74 u8 b0 [] = { reg >> 8, reg & 0xff };
75 u8 b1 [] = { 0 };
76
77 struct i2c_msg msg [] = {
78 { .addr = state->config->demod_address, .flags = 0,
79 .buf = b0, .len = 2 },
80 { .addr = state->config->demod_address, .flags = I2C_M_RD,
81 .buf = b1, .len = 1 } };
82
83 ret = i2c_transfer(state->i2c, msg, 2);
84
85 if (ret != 2)
Michael Krufkyce1719a2008-04-02 18:59:48 -030086 printk(KERN_ERR "%s: readreg error (ret == %i)\n",
87 __func__, ret);
Steven Toth265a6512008-04-18 21:34:00 -030088 return b1[0];
89}
90
Michael Krufkye059b0f2008-04-02 18:59:48 -030091static int au8522_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
Steven Toth265a6512008-04-18 21:34:00 -030092{
Michael Krufkye059b0f2008-04-02 18:59:48 -030093 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -030094
Michael Krufkyce1719a2008-04-02 18:59:48 -030095 dprintk("%s(%d)\n", __func__, enable);
Steven Toth265a6512008-04-18 21:34:00 -030096
97 if (enable)
98 return au8522_writereg(state, 0x106, 1);
99 else
100 return au8522_writereg(state, 0x106, 0);
101}
102
Michael Krufky0daa5de2008-04-10 04:24:56 -0300103struct mse2snr_tab {
Steven Tothf01699b2008-04-10 02:01:48 -0300104 u16 val;
105 u16 data;
Michael Krufky0daa5de2008-04-10 04:24:56 -0300106};
107
108/* VSB SNR lookup table */
109static struct mse2snr_tab vsb_mse2snr_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -0300110 { 0, 270 },
111 { 2, 250 },
112 { 3, 240 },
113 { 5, 230 },
114 { 7, 220 },
115 { 9, 210 },
116 { 12, 200 },
117 { 13, 195 },
118 { 15, 190 },
119 { 17, 185 },
120 { 19, 180 },
121 { 21, 175 },
122 { 24, 170 },
123 { 27, 165 },
124 { 31, 160 },
125 { 32, 158 },
126 { 33, 156 },
127 { 36, 152 },
128 { 37, 150 },
129 { 39, 148 },
130 { 40, 146 },
131 { 41, 144 },
132 { 43, 142 },
133 { 44, 140 },
134 { 48, 135 },
135 { 50, 130 },
136 { 43, 142 },
137 { 53, 125 },
138 { 56, 120 },
139 { 256, 115 },
140};
141
142/* QAM64 SNR lookup table */
Michael Krufky0daa5de2008-04-10 04:24:56 -0300143static struct mse2snr_tab qam64_mse2snr_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -0300144 { 15, 0 },
145 { 16, 290 },
146 { 17, 288 },
147 { 18, 286 },
148 { 19, 284 },
149 { 20, 282 },
150 { 21, 281 },
151 { 22, 279 },
152 { 23, 277 },
153 { 24, 275 },
154 { 25, 273 },
155 { 26, 271 },
156 { 27, 269 },
157 { 28, 268 },
158 { 29, 266 },
159 { 30, 264 },
160 { 31, 262 },
161 { 32, 260 },
162 { 33, 259 },
163 { 34, 258 },
164 { 35, 256 },
165 { 36, 255 },
166 { 37, 254 },
167 { 38, 252 },
168 { 39, 251 },
169 { 40, 250 },
170 { 41, 249 },
171 { 42, 248 },
172 { 43, 246 },
173 { 44, 245 },
174 { 45, 244 },
175 { 46, 242 },
176 { 47, 241 },
177 { 48, 240 },
178 { 50, 239 },
179 { 51, 238 },
180 { 53, 237 },
181 { 54, 236 },
182 { 56, 235 },
183 { 57, 234 },
184 { 59, 233 },
185 { 60, 232 },
186 { 62, 231 },
187 { 63, 230 },
188 { 65, 229 },
189 { 67, 228 },
190 { 68, 227 },
191 { 70, 226 },
192 { 71, 225 },
193 { 73, 224 },
194 { 74, 223 },
195 { 76, 222 },
196 { 78, 221 },
197 { 80, 220 },
198 { 82, 219 },
199 { 85, 218 },
200 { 88, 217 },
201 { 90, 216 },
202 { 92, 215 },
203 { 93, 214 },
204 { 94, 212 },
205 { 95, 211 },
206 { 97, 210 },
207 { 99, 209 },
208 { 101, 208 },
209 { 102, 207 },
210 { 104, 206 },
211 { 107, 205 },
212 { 111, 204 },
213 { 114, 203 },
214 { 118, 202 },
215 { 122, 201 },
216 { 125, 200 },
217 { 128, 199 },
218 { 130, 198 },
219 { 132, 197 },
220 { 256, 190 },
221};
222
223/* QAM256 SNR lookup table */
Michael Krufky0daa5de2008-04-10 04:24:56 -0300224static struct mse2snr_tab qam256_mse2snr_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -0300225 { 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
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300291static int au8522_mse2snr_lookup(struct mse2snr_tab *tab, int sz, int mse,
292 u16 *snr)
Steven Tothf01699b2008-04-10 02:01:48 -0300293{
294 int i, ret = -EINVAL;
295 dprintk("%s()\n", __func__);
296
Michael Krufky0daa5de2008-04-10 04:24:56 -0300297 for (i = 0; i < sz; i++) {
298 if (mse < tab[i].val) {
299 *snr = tab[i].data;
Steven Tothf01699b2008-04-10 02:01:48 -0300300 ret = 0;
301 break;
302 }
303 }
304 dprintk("%s() snr=%d\n", __func__, *snr);
305 return ret;
306}
307
308/* VSB Modulation table */
309static struct {
310 u16 reg;
311 u16 data;
312} VSB_mod_tab[] = {
313 { 0x8090, 0x84 },
314 { 0x4092, 0x11 },
315 { 0x2005, 0x00 },
316 { 0x8091, 0x80 },
317 { 0x80a3, 0x0c },
318 { 0x80a4, 0xe8 },
319 { 0x8081, 0xc4 },
320 { 0x80a5, 0x40 },
321 { 0x80a7, 0x40 },
322 { 0x80a6, 0x67 },
323 { 0x8262, 0x20 },
324 { 0x821c, 0x30 },
325 { 0x80d8, 0x1a },
326 { 0x8227, 0xa0 },
327 { 0x8121, 0xff },
328 { 0x80a8, 0xf0 },
329 { 0x80a9, 0x05 },
330 { 0x80aa, 0x77 },
331 { 0x80ab, 0xf0 },
332 { 0x80ac, 0x05 },
333 { 0x80ad, 0x77 },
334 { 0x80ae, 0x41 },
335 { 0x80af, 0x66 },
336 { 0x821b, 0xcc },
337 { 0x821d, 0x80 },
338 { 0x80b5, 0xfb },
339 { 0x80b6, 0x8e },
340 { 0x80b7, 0x39 },
341 { 0x80a4, 0xe8 },
342 { 0x8231, 0x13 },
343};
344
345/* QAM Modulation table */
346static struct {
347 u16 reg;
348 u16 data;
349} QAM_mod_tab[] = {
350 { 0x80a3, 0x09 },
351 { 0x80a4, 0x00 },
352 { 0x8081, 0xc4 },
353 { 0x80a5, 0x40 },
354 { 0x80b5, 0xfb },
355 { 0x80b6, 0x8e },
356 { 0x80b7, 0x39 },
357 { 0x80aa, 0x77 },
358 { 0x80ad, 0x77 },
359 { 0x80a6, 0x67 },
360 { 0x8262, 0x20 },
361 { 0x821c, 0x30 },
362 { 0x80b8, 0x3e },
363 { 0x80b9, 0xf0 },
364 { 0x80ba, 0x01 },
365 { 0x80bb, 0x18 },
366 { 0x80bc, 0x50 },
367 { 0x80bd, 0x00 },
368 { 0x80be, 0xea },
369 { 0x80bf, 0xef },
370 { 0x80c0, 0xfc },
371 { 0x80c1, 0xbd },
372 { 0x80c2, 0x1f },
373 { 0x80c3, 0xfc },
374 { 0x80c4, 0xdd },
375 { 0x80c5, 0xaf },
376 { 0x80c6, 0x00 },
377 { 0x80c7, 0x38 },
378 { 0x80c8, 0x30 },
379 { 0x80c9, 0x05 },
380 { 0x80ca, 0x4a },
381 { 0x80cb, 0xd0 },
382 { 0x80cc, 0x01 },
383 { 0x80cd, 0xd9 },
384 { 0x80ce, 0x6f },
385 { 0x80cf, 0xf9 },
386 { 0x80d0, 0x70 },
387 { 0x80d1, 0xdf },
388 { 0x80d2, 0xf7 },
389 { 0x80d3, 0xc2 },
390 { 0x80d4, 0xdf },
391 { 0x80d5, 0x02 },
392 { 0x80d6, 0x9a },
393 { 0x80d7, 0xd0 },
394 { 0x8250, 0x0d },
395 { 0x8251, 0xcd },
396 { 0x8252, 0xe0 },
397 { 0x8253, 0x05 },
398 { 0x8254, 0xa7 },
399 { 0x8255, 0xff },
400 { 0x8256, 0xed },
401 { 0x8257, 0x5b },
402 { 0x8258, 0xae },
403 { 0x8259, 0xe6 },
404 { 0x825a, 0x3d },
405 { 0x825b, 0x0f },
406 { 0x825c, 0x0d },
407 { 0x825d, 0xea },
408 { 0x825e, 0xf2 },
409 { 0x825f, 0x51 },
410 { 0x8260, 0xf5 },
411 { 0x8261, 0x06 },
412 { 0x821a, 0x00 },
413 { 0x8546, 0x40 },
414 { 0x8210, 0x26 },
415 { 0x8211, 0xf6 },
416 { 0x8212, 0x84 },
417 { 0x8213, 0x02 },
418 { 0x8502, 0x01 },
419 { 0x8121, 0x04 },
420 { 0x8122, 0x04 },
421 { 0x852e, 0x10 },
422 { 0x80a4, 0xca },
423 { 0x80a7, 0x40 },
424 { 0x8526, 0x01 },
425};
426
Michael Krufkye059b0f2008-04-02 18:59:48 -0300427static int au8522_enable_modulation(struct dvb_frontend *fe,
428 fe_modulation_t m)
Steven Toth265a6512008-04-18 21:34:00 -0300429{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300430 struct au8522_state *state = fe->demodulator_priv;
Steven Tothf01699b2008-04-10 02:01:48 -0300431 int i;
Steven Toth265a6512008-04-18 21:34:00 -0300432
Michael Krufkyce1719a2008-04-02 18:59:48 -0300433 dprintk("%s(0x%08x)\n", __func__, m);
Steven Toth265a6512008-04-18 21:34:00 -0300434
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300435 switch (m) {
Steven Toth265a6512008-04-18 21:34:00 -0300436 case VSB_8:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300437 dprintk("%s() VSB_8\n", __func__);
Steven Tothf01699b2008-04-10 02:01:48 -0300438 for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
439 au8522_writereg(state,
440 VSB_mod_tab[i].reg,
441 VSB_mod_tab[i].data);
Steven Toth265a6512008-04-18 21:34:00 -0300442 break;
443 case QAM_64:
444 case QAM_256:
Steven Tothf01699b2008-04-10 02:01:48 -0300445 dprintk("%s() QAM 64/256\n", __func__);
446 for (i = 0; i < ARRAY_SIZE(QAM_mod_tab); i++)
447 au8522_writereg(state,
448 QAM_mod_tab[i].reg,
449 QAM_mod_tab[i].data);
Steven Toth265a6512008-04-18 21:34:00 -0300450 break;
451 default:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300452 dprintk("%s() Invalid modulation\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300453 return -EINVAL;
454 }
455
456 state->current_modulation = m;
457
458 return 0;
459}
460
461/* Talk to the demod, set the FEC, GUARD, QAM settings etc */
Michael Krufkye059b0f2008-04-02 18:59:48 -0300462static int au8522_set_frontend(struct dvb_frontend *fe,
463 struct dvb_frontend_parameters *p)
Steven Toth265a6512008-04-18 21:34:00 -0300464{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300465 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300466
Michael Krufkyce1719a2008-04-02 18:59:48 -0300467 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
Steven Toth265a6512008-04-18 21:34:00 -0300468
469 state->current_frequency = p->frequency;
470
471 au8522_enable_modulation(fe, p->u.vsb.modulation);
472
473 /* Allow the demod to settle */
474 msleep(100);
475
476 if (fe->ops.tuner_ops.set_params) {
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300477 if (fe->ops.i2c_gate_ctrl)
478 fe->ops.i2c_gate_ctrl(fe, 1);
Steven Toth265a6512008-04-18 21:34:00 -0300479 fe->ops.tuner_ops.set_params(fe, p);
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300480 if (fe->ops.i2c_gate_ctrl)
481 fe->ops.i2c_gate_ctrl(fe, 0);
Steven Toth265a6512008-04-18 21:34:00 -0300482 }
483
484 return 0;
485}
486
487/* Reset the demod hardware and reset all of the configuration registers
488 to a default state. */
Michael Krufkye059b0f2008-04-02 18:59:48 -0300489static int au8522_init(struct dvb_frontend *fe)
Steven Toth265a6512008-04-18 21:34:00 -0300490{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300491 struct au8522_state *state = fe->demodulator_priv;
Michael Krufkyce1719a2008-04-02 18:59:48 -0300492 dprintk("%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300493
494 au8522_writereg(state, 0xa4, 1 << 5);
495
496 au8522_i2c_gate_ctrl(fe, 1);
497
498 return 0;
499}
500
Michael Krufkye059b0f2008-04-02 18:59:48 -0300501static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status)
Steven Toth265a6512008-04-18 21:34:00 -0300502{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300503 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300504 u8 reg;
505 u32 tuner_status = 0;
506
507 *status = 0;
508
509 if (state->current_modulation == VSB_8) {
Michael Krufkyce1719a2008-04-02 18:59:48 -0300510 dprintk("%s() Checking VSB_8\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300511 reg = au8522_readreg(state, 0x4088);
Michael Krufkye059b0f2008-04-02 18:59:48 -0300512 if (reg & 0x01)
Steven Toth265a6512008-04-18 21:34:00 -0300513 *status |= FE_HAS_VITERBI;
Michael Krufkye059b0f2008-04-02 18:59:48 -0300514 if (reg & 0x02)
Steven Toth265a6512008-04-18 21:34:00 -0300515 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
516 } else {
Michael Krufkyce1719a2008-04-02 18:59:48 -0300517 dprintk("%s() Checking QAM\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300518 reg = au8522_readreg(state, 0x4541);
Michael Krufkye059b0f2008-04-02 18:59:48 -0300519 if (reg & 0x80)
Steven Toth265a6512008-04-18 21:34:00 -0300520 *status |= FE_HAS_VITERBI;
Michael Krufkye059b0f2008-04-02 18:59:48 -0300521 if (reg & 0x20)
Steven Toth265a6512008-04-18 21:34:00 -0300522 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
523 }
524
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300525 switch (state->config->status_mode) {
Steven Toth265a6512008-04-18 21:34:00 -0300526 case AU8522_DEMODLOCKING:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300527 dprintk("%s() DEMODLOCKING\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300528 if (*status & FE_HAS_VITERBI)
529 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
530 break;
531 case AU8522_TUNERLOCKING:
532 /* Get the tuner status */
Michael Krufkyce1719a2008-04-02 18:59:48 -0300533 dprintk("%s() TUNERLOCKING\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300534 if (fe->ops.tuner_ops.get_status) {
535 if (fe->ops.i2c_gate_ctrl)
536 fe->ops.i2c_gate_ctrl(fe, 1);
537
538 fe->ops.tuner_ops.get_status(fe, &tuner_status);
539
540 if (fe->ops.i2c_gate_ctrl)
541 fe->ops.i2c_gate_ctrl(fe, 0);
542 }
543 if (tuner_status)
544 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
545 break;
546 }
547
Michael Krufkyce1719a2008-04-02 18:59:48 -0300548 dprintk("%s() status 0x%08x\n", __func__, *status);
Steven Toth265a6512008-04-18 21:34:00 -0300549
550 return 0;
551}
552
Michael Krufkye059b0f2008-04-02 18:59:48 -0300553static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
Steven Toth265a6512008-04-18 21:34:00 -0300554{
Steven Tothf01699b2008-04-10 02:01:48 -0300555 struct au8522_state *state = fe->demodulator_priv;
556 int ret = -EINVAL;
557
Michael Krufkyce1719a2008-04-02 18:59:48 -0300558 dprintk("%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300559
Steven Tothf01699b2008-04-10 02:01:48 -0300560 if (state->current_modulation == QAM_256)
Michael Krufky0daa5de2008-04-10 04:24:56 -0300561 ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
562 ARRAY_SIZE(qam256_mse2snr_tab),
563 au8522_readreg(state, 0x4522),
564 snr);
Steven Tothf01699b2008-04-10 02:01:48 -0300565 else if (state->current_modulation == QAM_64)
Michael Krufky0daa5de2008-04-10 04:24:56 -0300566 ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
567 ARRAY_SIZE(qam64_mse2snr_tab),
568 au8522_readreg(state, 0x4522),
569 snr);
Steven Tothf01699b2008-04-10 02:01:48 -0300570 else /* VSB_8 */
Michael Krufky0daa5de2008-04-10 04:24:56 -0300571 ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
572 ARRAY_SIZE(vsb_mse2snr_tab),
573 au8522_readreg(state, 0x4311),
574 snr);
Steven Toth265a6512008-04-18 21:34:00 -0300575
Steven Tothf01699b2008-04-10 02:01:48 -0300576 return ret;
Steven Toth265a6512008-04-18 21:34:00 -0300577}
578
Michael Krufkye059b0f2008-04-02 18:59:48 -0300579static int au8522_read_signal_strength(struct dvb_frontend *fe,
580 u16 *signal_strength)
Steven Toth265a6512008-04-18 21:34:00 -0300581{
582 return au8522_read_snr(fe, signal_strength);
583}
584
Michael Krufkye059b0f2008-04-02 18:59:48 -0300585static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
Steven Toth265a6512008-04-18 21:34:00 -0300586{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300587 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300588
Michael Krufky8973dc42008-04-05 23:08:08 -0300589 if (state->current_modulation == VSB_8)
590 *ucblocks = au8522_readreg(state, 0x4087);
591 else
592 *ucblocks = au8522_readreg(state, 0x4543);
Steven Toth265a6512008-04-18 21:34:00 -0300593
594 return 0;
595}
596
Michael Krufkye059b0f2008-04-02 18:59:48 -0300597static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
Steven Toth265a6512008-04-18 21:34:00 -0300598{
599 return au8522_read_ucblocks(fe, ber);
600}
601
Michael Krufkye059b0f2008-04-02 18:59:48 -0300602static int au8522_get_frontend(struct dvb_frontend *fe,
Steven Toth265a6512008-04-18 21:34:00 -0300603 struct dvb_frontend_parameters *p)
604{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300605 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300606
607 p->frequency = state->current_frequency;
608 p->u.vsb.modulation = state->current_modulation;
609
610 return 0;
611}
612
Michael Krufkye059b0f2008-04-02 18:59:48 -0300613static int au8522_get_tune_settings(struct dvb_frontend *fe,
614 struct dvb_frontend_tune_settings *tune)
Steven Toth265a6512008-04-18 21:34:00 -0300615{
616 tune->min_delay_ms = 1000;
617 return 0;
618}
619
Michael Krufkye059b0f2008-04-02 18:59:48 -0300620static void au8522_release(struct dvb_frontend *fe)
Steven Toth265a6512008-04-18 21:34:00 -0300621{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300622 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300623 kfree(state);
624}
625
626static struct dvb_frontend_ops au8522_ops;
627
Michael Krufkye059b0f2008-04-02 18:59:48 -0300628struct dvb_frontend *au8522_attach(const struct au8522_config *config,
629 struct i2c_adapter *i2c)
Steven Toth265a6512008-04-18 21:34:00 -0300630{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300631 struct au8522_state *state = NULL;
Steven Toth265a6512008-04-18 21:34:00 -0300632
633 /* allocate memory for the internal state */
634 state = kmalloc(sizeof(struct au8522_state), GFP_KERNEL);
635 if (state == NULL)
636 goto error;
637
638 /* setup the state */
639 state->config = config;
640 state->i2c = i2c;
641 /* create dvb_frontend */
642 memcpy(&state->frontend.ops, &au8522_ops,
643 sizeof(struct dvb_frontend_ops));
644 state->frontend.demodulator_priv = state;
645
646 if (au8522_init(&state->frontend) != 0) {
647 printk(KERN_ERR "%s: Failed to initialize correctly\n",
Michael Krufkyce1719a2008-04-02 18:59:48 -0300648 __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300649 goto error;
650 }
651
652 /* Note: Leaving the I2C gate open here. */
653 au8522_i2c_gate_ctrl(&state->frontend, 1);
654
655 return &state->frontend;
656
657error:
658 kfree(state);
659 return NULL;
660}
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300661EXPORT_SYMBOL(au8522_attach);
Steven Toth265a6512008-04-18 21:34:00 -0300662
663static struct dvb_frontend_ops au8522_ops = {
664
665 .info = {
666 .name = "Auvitek AU8522 QAM/8VSB Frontend",
667 .type = FE_ATSC,
668 .frequency_min = 54000000,
669 .frequency_max = 858000000,
670 .frequency_stepsize = 62500,
671 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
672 },
673
674 .init = au8522_init,
675 .i2c_gate_ctrl = au8522_i2c_gate_ctrl,
676 .set_frontend = au8522_set_frontend,
677 .get_frontend = au8522_get_frontend,
678 .get_tune_settings = au8522_get_tune_settings,
679 .read_status = au8522_read_status,
680 .read_ber = au8522_read_ber,
681 .read_signal_strength = au8522_read_signal_strength,
682 .read_snr = au8522_read_snr,
683 .read_ucblocks = au8522_read_ucblocks,
684 .release = au8522_release,
685};
686
687module_param(debug, int, 0644);
688MODULE_PARM_DESC(debug, "Enable verbose debug messages");
689
690MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
691MODULE_AUTHOR("Steven Toth");
692MODULE_LICENSE("GPL");