blob: 0b82cc2a1e161c7603e900ab6bff8d4ffc9effee [file] [log] [blame]
Steven Toth265a6512008-04-18 21:34:00 -03001/*
2 Auvitek AU8522 QAM/8VSB demodulator driver
3
Steven Toth6d897612008-09-03 17:12:12 -03004 Copyright (C) 2008 Steven Toth <stoth@linuxtv.org>
Steven Toth265a6512008-04-18 21:34:00 -03005
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"
Steven Toth265a6512008-04-18 21:34:00 -030029#include "au8522.h"
30
31struct au8522_state {
32
Michael Krufkye059b0f2008-04-02 18:59:48 -030033 struct i2c_adapter *i2c;
Steven Toth265a6512008-04-18 21:34:00 -030034
35 /* configuration settings */
Michael Krufkye059b0f2008-04-02 18:59:48 -030036 const struct au8522_config *config;
Steven Toth265a6512008-04-18 21:34:00 -030037
38 struct dvb_frontend frontend;
39
40 u32 current_frequency;
41 fe_modulation_t current_modulation;
42
43};
44
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -030045static int debug;
46
47#define dprintk(arg...) do { \
48 if (debug) \
Mauro Carvalho Chehab353a2762008-04-18 22:24:01 -030049 printk(arg); \
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -030050 } while (0)
Steven Toth265a6512008-04-18 21:34:00 -030051
52/* 16 bit registers, 8 bit values */
Michael Krufkye059b0f2008-04-02 18:59:48 -030053static int au8522_writereg(struct au8522_state *state, u16 reg, u8 data)
Steven Toth265a6512008-04-18 21:34:00 -030054{
55 int ret;
56 u8 buf [] = { reg >> 8, reg & 0xff, data };
57
58 struct i2c_msg msg = { .addr = state->config->demod_address,
59 .flags = 0, .buf = buf, .len = 3 };
60
61 ret = i2c_transfer(state->i2c, &msg, 1);
62
63 if (ret != 1)
64 printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
Michael Krufkyce1719a2008-04-02 18:59:48 -030065 "ret == %i)\n", __func__, reg, data, ret);
Steven Toth265a6512008-04-18 21:34:00 -030066
67 return (ret != 1) ? -1 : 0;
68}
69
Michael Krufkye059b0f2008-04-02 18:59:48 -030070static u8 au8522_readreg(struct au8522_state *state, u16 reg)
Steven Toth265a6512008-04-18 21:34:00 -030071{
72 int ret;
73 u8 b0 [] = { reg >> 8, reg & 0xff };
74 u8 b1 [] = { 0 };
75
76 struct i2c_msg msg [] = {
77 { .addr = state->config->demod_address, .flags = 0,
78 .buf = b0, .len = 2 },
79 { .addr = state->config->demod_address, .flags = I2C_M_RD,
80 .buf = b1, .len = 1 } };
81
82 ret = i2c_transfer(state->i2c, msg, 2);
83
84 if (ret != 2)
Michael Krufkyce1719a2008-04-02 18:59:48 -030085 printk(KERN_ERR "%s: readreg error (ret == %i)\n",
86 __func__, ret);
Steven Toth265a6512008-04-18 21:34:00 -030087 return b1[0];
88}
89
Michael Krufkye059b0f2008-04-02 18:59:48 -030090static int au8522_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
Steven Toth265a6512008-04-18 21:34:00 -030091{
Michael Krufkye059b0f2008-04-02 18:59:48 -030092 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -030093
Michael Krufkyce1719a2008-04-02 18:59:48 -030094 dprintk("%s(%d)\n", __func__, enable);
Steven Toth265a6512008-04-18 21:34:00 -030095
96 if (enable)
97 return au8522_writereg(state, 0x106, 1);
98 else
99 return au8522_writereg(state, 0x106, 0);
100}
101
Michael Krufky0daa5de2008-04-10 04:24:56 -0300102struct mse2snr_tab {
Steven Tothf01699b2008-04-10 02:01:48 -0300103 u16 val;
104 u16 data;
Michael Krufky0daa5de2008-04-10 04:24:56 -0300105};
106
107/* VSB SNR lookup table */
108static struct mse2snr_tab vsb_mse2snr_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -0300109 { 0, 270 },
110 { 2, 250 },
111 { 3, 240 },
112 { 5, 230 },
113 { 7, 220 },
114 { 9, 210 },
115 { 12, 200 },
116 { 13, 195 },
117 { 15, 190 },
118 { 17, 185 },
119 { 19, 180 },
120 { 21, 175 },
121 { 24, 170 },
122 { 27, 165 },
123 { 31, 160 },
124 { 32, 158 },
125 { 33, 156 },
126 { 36, 152 },
127 { 37, 150 },
128 { 39, 148 },
129 { 40, 146 },
130 { 41, 144 },
131 { 43, 142 },
132 { 44, 140 },
133 { 48, 135 },
134 { 50, 130 },
135 { 43, 142 },
136 { 53, 125 },
137 { 56, 120 },
138 { 256, 115 },
139};
140
141/* QAM64 SNR lookup table */
Michael Krufky0daa5de2008-04-10 04:24:56 -0300142static struct mse2snr_tab qam64_mse2snr_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -0300143 { 15, 0 },
144 { 16, 290 },
145 { 17, 288 },
146 { 18, 286 },
147 { 19, 284 },
148 { 20, 282 },
149 { 21, 281 },
150 { 22, 279 },
151 { 23, 277 },
152 { 24, 275 },
153 { 25, 273 },
154 { 26, 271 },
155 { 27, 269 },
156 { 28, 268 },
157 { 29, 266 },
158 { 30, 264 },
159 { 31, 262 },
160 { 32, 260 },
161 { 33, 259 },
162 { 34, 258 },
163 { 35, 256 },
164 { 36, 255 },
165 { 37, 254 },
166 { 38, 252 },
167 { 39, 251 },
168 { 40, 250 },
169 { 41, 249 },
170 { 42, 248 },
171 { 43, 246 },
172 { 44, 245 },
173 { 45, 244 },
174 { 46, 242 },
175 { 47, 241 },
176 { 48, 240 },
177 { 50, 239 },
178 { 51, 238 },
179 { 53, 237 },
180 { 54, 236 },
181 { 56, 235 },
182 { 57, 234 },
183 { 59, 233 },
184 { 60, 232 },
185 { 62, 231 },
186 { 63, 230 },
187 { 65, 229 },
188 { 67, 228 },
189 { 68, 227 },
190 { 70, 226 },
191 { 71, 225 },
192 { 73, 224 },
193 { 74, 223 },
194 { 76, 222 },
195 { 78, 221 },
196 { 80, 220 },
197 { 82, 219 },
198 { 85, 218 },
199 { 88, 217 },
200 { 90, 216 },
201 { 92, 215 },
202 { 93, 214 },
203 { 94, 212 },
204 { 95, 211 },
205 { 97, 210 },
206 { 99, 209 },
207 { 101, 208 },
208 { 102, 207 },
209 { 104, 206 },
210 { 107, 205 },
211 { 111, 204 },
212 { 114, 203 },
213 { 118, 202 },
214 { 122, 201 },
215 { 125, 200 },
216 { 128, 199 },
217 { 130, 198 },
218 { 132, 197 },
219 { 256, 190 },
220};
221
222/* QAM256 SNR lookup table */
Michael Krufky0daa5de2008-04-10 04:24:56 -0300223static struct mse2snr_tab qam256_mse2snr_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -0300224 { 16, 0 },
225 { 17, 400 },
226 { 18, 398 },
227 { 19, 396 },
228 { 20, 394 },
229 { 21, 392 },
230 { 22, 390 },
231 { 23, 388 },
232 { 24, 386 },
233 { 25, 384 },
234 { 26, 382 },
235 { 27, 380 },
236 { 28, 379 },
237 { 29, 378 },
238 { 30, 377 },
239 { 31, 376 },
240 { 32, 375 },
241 { 33, 374 },
242 { 34, 373 },
243 { 35, 372 },
244 { 36, 371 },
245 { 37, 370 },
246 { 38, 362 },
247 { 39, 354 },
248 { 40, 346 },
249 { 41, 338 },
250 { 42, 330 },
251 { 43, 328 },
252 { 44, 326 },
253 { 45, 324 },
254 { 46, 322 },
255 { 47, 320 },
256 { 48, 319 },
257 { 49, 318 },
258 { 50, 317 },
259 { 51, 316 },
260 { 52, 315 },
261 { 53, 314 },
262 { 54, 313 },
263 { 55, 312 },
264 { 56, 311 },
265 { 57, 310 },
266 { 58, 308 },
267 { 59, 306 },
268 { 60, 304 },
269 { 61, 302 },
270 { 62, 300 },
271 { 63, 298 },
272 { 65, 295 },
273 { 68, 294 },
274 { 70, 293 },
275 { 73, 292 },
276 { 76, 291 },
277 { 78, 290 },
278 { 79, 289 },
279 { 81, 288 },
280 { 82, 287 },
281 { 83, 286 },
282 { 84, 285 },
283 { 85, 284 },
284 { 86, 283 },
285 { 88, 282 },
286 { 89, 281 },
287 { 256, 280 },
288};
289
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300290static int au8522_mse2snr_lookup(struct mse2snr_tab *tab, int sz, int mse,
291 u16 *snr)
Steven Tothf01699b2008-04-10 02:01:48 -0300292{
293 int i, ret = -EINVAL;
294 dprintk("%s()\n", __func__);
295
Michael Krufky0daa5de2008-04-10 04:24:56 -0300296 for (i = 0; i < sz; i++) {
297 if (mse < tab[i].val) {
298 *snr = tab[i].data;
Steven Tothf01699b2008-04-10 02:01:48 -0300299 ret = 0;
300 break;
301 }
302 }
303 dprintk("%s() snr=%d\n", __func__, *snr);
304 return ret;
305}
306
Michael Krufky2e7acd72008-09-03 16:46:35 -0300307static int au8522_set_if(struct dvb_frontend *fe, enum au8522_if_freq if_freq)
308{
309 struct au8522_state *state = fe->demodulator_priv;
Michael Krufkydf76de02008-09-03 16:46:40 -0300310 u8 r0b5, r0b6, r0b7;
311 char *ifmhz;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300312
313 switch (if_freq) {
314 case AU8522_IF_3_25MHZ:
Michael Krufkydf76de02008-09-03 16:46:40 -0300315 ifmhz = "3.25";
316 r0b5 = 0x00;
317 r0b6 = 0x3d;
318 r0b7 = 0xa0;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300319 break;
320 case AU8522_IF_4MHZ:
Michael Krufkydf76de02008-09-03 16:46:40 -0300321 ifmhz = "4.00";
322 r0b5 = 0x00;
323 r0b6 = 0x4b;
324 r0b7 = 0xd9;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300325 break;
326 case AU8522_IF_6MHZ:
Michael Krufkydf76de02008-09-03 16:46:40 -0300327 ifmhz = "6.00";
328 r0b5 = 0xfb;
329 r0b6 = 0x8e;
330 r0b7 = 0x39;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300331 break;
332 default:
333 dprintk("%s() IF Frequency not supported\n", __func__);
334 return -EINVAL;
335 }
Michael Krufkydf76de02008-09-03 16:46:40 -0300336 dprintk("%s() %s MHz\n", __func__, ifmhz);
337 au8522_writereg(state, 0x80b5, r0b5);
338 au8522_writereg(state, 0x80b6, r0b6);
339 au8522_writereg(state, 0x80b7, r0b7);
340
Michael Krufky2e7acd72008-09-03 16:46:35 -0300341 return 0;
342}
343
Steven Tothf01699b2008-04-10 02:01:48 -0300344/* VSB Modulation table */
345static struct {
346 u16 reg;
347 u16 data;
348} VSB_mod_tab[] = {
349 { 0x8090, 0x84 },
350 { 0x4092, 0x11 },
351 { 0x2005, 0x00 },
352 { 0x8091, 0x80 },
353 { 0x80a3, 0x0c },
354 { 0x80a4, 0xe8 },
355 { 0x8081, 0xc4 },
356 { 0x80a5, 0x40 },
357 { 0x80a7, 0x40 },
358 { 0x80a6, 0x67 },
359 { 0x8262, 0x20 },
360 { 0x821c, 0x30 },
361 { 0x80d8, 0x1a },
362 { 0x8227, 0xa0 },
363 { 0x8121, 0xff },
364 { 0x80a8, 0xf0 },
365 { 0x80a9, 0x05 },
366 { 0x80aa, 0x77 },
367 { 0x80ab, 0xf0 },
368 { 0x80ac, 0x05 },
369 { 0x80ad, 0x77 },
370 { 0x80ae, 0x41 },
371 { 0x80af, 0x66 },
372 { 0x821b, 0xcc },
373 { 0x821d, 0x80 },
Steven Tothf01699b2008-04-10 02:01:48 -0300374 { 0x80a4, 0xe8 },
375 { 0x8231, 0x13 },
376};
377
378/* QAM Modulation table */
379static struct {
380 u16 reg;
381 u16 data;
382} QAM_mod_tab[] = {
383 { 0x80a3, 0x09 },
384 { 0x80a4, 0x00 },
385 { 0x8081, 0xc4 },
386 { 0x80a5, 0x40 },
Steven Tothf01699b2008-04-10 02:01:48 -0300387 { 0x80aa, 0x77 },
388 { 0x80ad, 0x77 },
389 { 0x80a6, 0x67 },
390 { 0x8262, 0x20 },
391 { 0x821c, 0x30 },
392 { 0x80b8, 0x3e },
393 { 0x80b9, 0xf0 },
394 { 0x80ba, 0x01 },
395 { 0x80bb, 0x18 },
396 { 0x80bc, 0x50 },
397 { 0x80bd, 0x00 },
398 { 0x80be, 0xea },
399 { 0x80bf, 0xef },
400 { 0x80c0, 0xfc },
401 { 0x80c1, 0xbd },
402 { 0x80c2, 0x1f },
403 { 0x80c3, 0xfc },
404 { 0x80c4, 0xdd },
405 { 0x80c5, 0xaf },
406 { 0x80c6, 0x00 },
407 { 0x80c7, 0x38 },
408 { 0x80c8, 0x30 },
409 { 0x80c9, 0x05 },
410 { 0x80ca, 0x4a },
411 { 0x80cb, 0xd0 },
412 { 0x80cc, 0x01 },
413 { 0x80cd, 0xd9 },
414 { 0x80ce, 0x6f },
415 { 0x80cf, 0xf9 },
416 { 0x80d0, 0x70 },
417 { 0x80d1, 0xdf },
418 { 0x80d2, 0xf7 },
419 { 0x80d3, 0xc2 },
420 { 0x80d4, 0xdf },
421 { 0x80d5, 0x02 },
422 { 0x80d6, 0x9a },
423 { 0x80d7, 0xd0 },
424 { 0x8250, 0x0d },
425 { 0x8251, 0xcd },
426 { 0x8252, 0xe0 },
427 { 0x8253, 0x05 },
428 { 0x8254, 0xa7 },
429 { 0x8255, 0xff },
430 { 0x8256, 0xed },
431 { 0x8257, 0x5b },
432 { 0x8258, 0xae },
433 { 0x8259, 0xe6 },
434 { 0x825a, 0x3d },
435 { 0x825b, 0x0f },
436 { 0x825c, 0x0d },
437 { 0x825d, 0xea },
438 { 0x825e, 0xf2 },
439 { 0x825f, 0x51 },
440 { 0x8260, 0xf5 },
441 { 0x8261, 0x06 },
442 { 0x821a, 0x00 },
443 { 0x8546, 0x40 },
444 { 0x8210, 0x26 },
445 { 0x8211, 0xf6 },
446 { 0x8212, 0x84 },
447 { 0x8213, 0x02 },
448 { 0x8502, 0x01 },
449 { 0x8121, 0x04 },
450 { 0x8122, 0x04 },
451 { 0x852e, 0x10 },
452 { 0x80a4, 0xca },
453 { 0x80a7, 0x40 },
454 { 0x8526, 0x01 },
455};
456
Michael Krufkye059b0f2008-04-02 18:59:48 -0300457static int au8522_enable_modulation(struct dvb_frontend *fe,
458 fe_modulation_t m)
Steven Toth265a6512008-04-18 21:34:00 -0300459{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300460 struct au8522_state *state = fe->demodulator_priv;
Steven Tothf01699b2008-04-10 02:01:48 -0300461 int i;
Steven Toth265a6512008-04-18 21:34:00 -0300462
Michael Krufkyce1719a2008-04-02 18:59:48 -0300463 dprintk("%s(0x%08x)\n", __func__, m);
Steven Toth265a6512008-04-18 21:34:00 -0300464
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300465 switch (m) {
Steven Toth265a6512008-04-18 21:34:00 -0300466 case VSB_8:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300467 dprintk("%s() VSB_8\n", __func__);
Steven Tothf01699b2008-04-10 02:01:48 -0300468 for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
469 au8522_writereg(state,
470 VSB_mod_tab[i].reg,
471 VSB_mod_tab[i].data);
Michael Krufky2e7acd72008-09-03 16:46:35 -0300472 au8522_set_if(fe, state->config->vsb_if);
Steven Toth265a6512008-04-18 21:34:00 -0300473 break;
474 case QAM_64:
475 case QAM_256:
Steven Tothf01699b2008-04-10 02:01:48 -0300476 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);
Michael Krufky2e7acd72008-09-03 16:46:35 -0300481 au8522_set_if(fe, state->config->qam_if);
Steven Toth265a6512008-04-18 21:34:00 -0300482 break;
483 default:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300484 dprintk("%s() Invalid modulation\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300485 return -EINVAL;
486 }
487
488 state->current_modulation = m;
489
490 return 0;
491}
492
493/* Talk to the demod, set the FEC, GUARD, QAM settings etc */
Michael Krufkye059b0f2008-04-02 18:59:48 -0300494static int au8522_set_frontend(struct dvb_frontend *fe,
495 struct dvb_frontend_parameters *p)
Steven Toth265a6512008-04-18 21:34:00 -0300496{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300497 struct au8522_state *state = fe->demodulator_priv;
Michael Krufky74d50722008-06-13 20:33:23 -0300498 int ret = -EINVAL;
Steven Toth265a6512008-04-18 21:34:00 -0300499
Michael Krufkyce1719a2008-04-02 18:59:48 -0300500 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
Steven Toth265a6512008-04-18 21:34:00 -0300501
Michael Krufky74d50722008-06-13 20:33:23 -0300502 if ((state->current_frequency == p->frequency) &&
503 (state->current_modulation == p->u.vsb.modulation))
504 return 0;
Steven Toth265a6512008-04-18 21:34:00 -0300505
506 au8522_enable_modulation(fe, p->u.vsb.modulation);
507
508 /* Allow the demod to settle */
509 msleep(100);
510
511 if (fe->ops.tuner_ops.set_params) {
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300512 if (fe->ops.i2c_gate_ctrl)
513 fe->ops.i2c_gate_ctrl(fe, 1);
Michael Krufky74d50722008-06-13 20:33:23 -0300514 ret = fe->ops.tuner_ops.set_params(fe, p);
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300515 if (fe->ops.i2c_gate_ctrl)
516 fe->ops.i2c_gate_ctrl(fe, 0);
Steven Toth265a6512008-04-18 21:34:00 -0300517 }
518
Michael Krufky74d50722008-06-13 20:33:23 -0300519 if (ret < 0)
520 return ret;
521
522 state->current_frequency = p->frequency;
523
Steven Toth265a6512008-04-18 21:34:00 -0300524 return 0;
525}
526
527/* Reset the demod hardware and reset all of the configuration registers
528 to a default state. */
Michael Krufkye059b0f2008-04-02 18:59:48 -0300529static int au8522_init(struct dvb_frontend *fe)
Steven Toth265a6512008-04-18 21:34:00 -0300530{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300531 struct au8522_state *state = fe->demodulator_priv;
Michael Krufkyce1719a2008-04-02 18:59:48 -0300532 dprintk("%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300533
534 au8522_writereg(state, 0xa4, 1 << 5);
535
536 au8522_i2c_gate_ctrl(fe, 1);
537
538 return 0;
539}
540
Michael Krufky74d50722008-06-13 20:33:23 -0300541static int au8522_sleep(struct dvb_frontend *fe)
542{
543 struct au8522_state *state = fe->demodulator_priv;
544 dprintk("%s()\n", __func__);
545
546 state->current_frequency = 0;
547
548 return 0;
549}
550
Michael Krufkye059b0f2008-04-02 18:59:48 -0300551static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status)
Steven Toth265a6512008-04-18 21:34:00 -0300552{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300553 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300554 u8 reg;
555 u32 tuner_status = 0;
556
557 *status = 0;
558
559 if (state->current_modulation == VSB_8) {
Michael Krufkyce1719a2008-04-02 18:59:48 -0300560 dprintk("%s() Checking VSB_8\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300561 reg = au8522_readreg(state, 0x4088);
Steven Toth836c2852008-06-21 19:32:41 -0300562 if ((reg & 0x03) == 0x03)
563 *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
Steven Toth265a6512008-04-18 21:34:00 -0300564 } else {
Michael Krufkyce1719a2008-04-02 18:59:48 -0300565 dprintk("%s() Checking QAM\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300566 reg = au8522_readreg(state, 0x4541);
Michael Krufkye059b0f2008-04-02 18:59:48 -0300567 if (reg & 0x80)
Steven Toth265a6512008-04-18 21:34:00 -0300568 *status |= FE_HAS_VITERBI;
Michael Krufkye059b0f2008-04-02 18:59:48 -0300569 if (reg & 0x20)
Steven Toth265a6512008-04-18 21:34:00 -0300570 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
571 }
572
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300573 switch (state->config->status_mode) {
Steven Toth265a6512008-04-18 21:34:00 -0300574 case AU8522_DEMODLOCKING:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300575 dprintk("%s() DEMODLOCKING\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300576 if (*status & FE_HAS_VITERBI)
577 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
578 break;
579 case AU8522_TUNERLOCKING:
580 /* Get the tuner status */
Michael Krufkyce1719a2008-04-02 18:59:48 -0300581 dprintk("%s() TUNERLOCKING\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300582 if (fe->ops.tuner_ops.get_status) {
583 if (fe->ops.i2c_gate_ctrl)
584 fe->ops.i2c_gate_ctrl(fe, 1);
585
586 fe->ops.tuner_ops.get_status(fe, &tuner_status);
587
588 if (fe->ops.i2c_gate_ctrl)
589 fe->ops.i2c_gate_ctrl(fe, 0);
590 }
591 if (tuner_status)
592 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
593 break;
594 }
595
Michael Krufkyce1719a2008-04-02 18:59:48 -0300596 dprintk("%s() status 0x%08x\n", __func__, *status);
Steven Toth265a6512008-04-18 21:34:00 -0300597
598 return 0;
599}
600
Michael Krufkye059b0f2008-04-02 18:59:48 -0300601static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
Steven Toth265a6512008-04-18 21:34:00 -0300602{
Steven Tothf01699b2008-04-10 02:01:48 -0300603 struct au8522_state *state = fe->demodulator_priv;
604 int ret = -EINVAL;
605
Michael Krufkyce1719a2008-04-02 18:59:48 -0300606 dprintk("%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300607
Steven Tothf01699b2008-04-10 02:01:48 -0300608 if (state->current_modulation == QAM_256)
Michael Krufky0daa5de2008-04-10 04:24:56 -0300609 ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
610 ARRAY_SIZE(qam256_mse2snr_tab),
611 au8522_readreg(state, 0x4522),
612 snr);
Steven Tothf01699b2008-04-10 02:01:48 -0300613 else if (state->current_modulation == QAM_64)
Michael Krufky0daa5de2008-04-10 04:24:56 -0300614 ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
615 ARRAY_SIZE(qam64_mse2snr_tab),
616 au8522_readreg(state, 0x4522),
617 snr);
Steven Tothf01699b2008-04-10 02:01:48 -0300618 else /* VSB_8 */
Michael Krufky0daa5de2008-04-10 04:24:56 -0300619 ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
620 ARRAY_SIZE(vsb_mse2snr_tab),
621 au8522_readreg(state, 0x4311),
622 snr);
Steven Toth265a6512008-04-18 21:34:00 -0300623
Steven Tothf01699b2008-04-10 02:01:48 -0300624 return ret;
Steven Toth265a6512008-04-18 21:34:00 -0300625}
626
Michael Krufkye059b0f2008-04-02 18:59:48 -0300627static int au8522_read_signal_strength(struct dvb_frontend *fe,
628 u16 *signal_strength)
Steven Toth265a6512008-04-18 21:34:00 -0300629{
630 return au8522_read_snr(fe, signal_strength);
631}
632
Michael Krufkye059b0f2008-04-02 18:59:48 -0300633static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
Steven Toth265a6512008-04-18 21:34:00 -0300634{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300635 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300636
Michael Krufky8973dc42008-04-05 23:08:08 -0300637 if (state->current_modulation == VSB_8)
638 *ucblocks = au8522_readreg(state, 0x4087);
639 else
640 *ucblocks = au8522_readreg(state, 0x4543);
Steven Toth265a6512008-04-18 21:34:00 -0300641
642 return 0;
643}
644
Michael Krufkye059b0f2008-04-02 18:59:48 -0300645static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
Steven Toth265a6512008-04-18 21:34:00 -0300646{
647 return au8522_read_ucblocks(fe, ber);
648}
649
Michael Krufkye059b0f2008-04-02 18:59:48 -0300650static int au8522_get_frontend(struct dvb_frontend *fe,
Steven Toth265a6512008-04-18 21:34:00 -0300651 struct dvb_frontend_parameters *p)
652{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300653 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300654
655 p->frequency = state->current_frequency;
656 p->u.vsb.modulation = state->current_modulation;
657
658 return 0;
659}
660
Michael Krufkye059b0f2008-04-02 18:59:48 -0300661static int au8522_get_tune_settings(struct dvb_frontend *fe,
662 struct dvb_frontend_tune_settings *tune)
Steven Toth265a6512008-04-18 21:34:00 -0300663{
664 tune->min_delay_ms = 1000;
665 return 0;
666}
667
Michael Krufkye059b0f2008-04-02 18:59:48 -0300668static void au8522_release(struct dvb_frontend *fe)
Steven Toth265a6512008-04-18 21:34:00 -0300669{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300670 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300671 kfree(state);
672}
673
674static struct dvb_frontend_ops au8522_ops;
675
Michael Krufkye059b0f2008-04-02 18:59:48 -0300676struct dvb_frontend *au8522_attach(const struct au8522_config *config,
677 struct i2c_adapter *i2c)
Steven Toth265a6512008-04-18 21:34:00 -0300678{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300679 struct au8522_state *state = NULL;
Steven Toth265a6512008-04-18 21:34:00 -0300680
681 /* allocate memory for the internal state */
682 state = kmalloc(sizeof(struct au8522_state), GFP_KERNEL);
683 if (state == NULL)
684 goto error;
685
686 /* setup the state */
687 state->config = config;
688 state->i2c = i2c;
689 /* create dvb_frontend */
690 memcpy(&state->frontend.ops, &au8522_ops,
691 sizeof(struct dvb_frontend_ops));
692 state->frontend.demodulator_priv = state;
693
694 if (au8522_init(&state->frontend) != 0) {
695 printk(KERN_ERR "%s: Failed to initialize correctly\n",
Michael Krufkyce1719a2008-04-02 18:59:48 -0300696 __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300697 goto error;
698 }
699
700 /* Note: Leaving the I2C gate open here. */
701 au8522_i2c_gate_ctrl(&state->frontend, 1);
702
703 return &state->frontend;
704
705error:
706 kfree(state);
707 return NULL;
708}
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300709EXPORT_SYMBOL(au8522_attach);
Steven Toth265a6512008-04-18 21:34:00 -0300710
711static struct dvb_frontend_ops au8522_ops = {
712
713 .info = {
714 .name = "Auvitek AU8522 QAM/8VSB Frontend",
715 .type = FE_ATSC,
716 .frequency_min = 54000000,
717 .frequency_max = 858000000,
718 .frequency_stepsize = 62500,
719 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
720 },
721
722 .init = au8522_init,
Michael Krufky74d50722008-06-13 20:33:23 -0300723 .sleep = au8522_sleep,
Steven Toth265a6512008-04-18 21:34:00 -0300724 .i2c_gate_ctrl = au8522_i2c_gate_ctrl,
725 .set_frontend = au8522_set_frontend,
726 .get_frontend = au8522_get_frontend,
727 .get_tune_settings = au8522_get_tune_settings,
728 .read_status = au8522_read_status,
729 .read_ber = au8522_read_ber,
730 .read_signal_strength = au8522_read_signal_strength,
731 .read_snr = au8522_read_snr,
732 .read_ucblocks = au8522_read_ucblocks,
733 .release = au8522_release,
734};
735
736module_param(debug, int, 0644);
737MODULE_PARM_DESC(debug, "Enable verbose debug messages");
738
739MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
740MODULE_AUTHOR("Steven Toth");
741MODULE_LICENSE("GPL");