blob: a68974f6d7081f2bc877b43e3c9481d49de45dc9 [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>
Steven Toth265a6512008-04-18 21:34:00 -030026#include <linux/delay.h>
27#include "dvb_frontend.h"
Steven Toth265a6512008-04-18 21:34:00 -030028#include "au8522.h"
Devin Heitmueller0c44bf32009-03-11 02:59:56 -030029#include "au8522_priv.h"
Steven Toth265a6512008-04-18 21:34:00 -030030
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -030031static int debug;
32
Devin Heitmueller62899a22009-03-15 18:48:52 -030033#define dprintk(arg...)\
34 do { if (debug)\
35 printk(arg);\
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -030036 } while (0)
Steven Toth265a6512008-04-18 21:34:00 -030037
Michael Krufky0daa5de2008-04-10 04:24:56 -030038struct mse2snr_tab {
Steven Tothf01699b2008-04-10 02:01:48 -030039 u16 val;
40 u16 data;
Michael Krufky0daa5de2008-04-10 04:24:56 -030041};
42
43/* VSB SNR lookup table */
44static struct mse2snr_tab vsb_mse2snr_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -030045 { 0, 270 },
46 { 2, 250 },
47 { 3, 240 },
48 { 5, 230 },
49 { 7, 220 },
50 { 9, 210 },
51 { 12, 200 },
52 { 13, 195 },
53 { 15, 190 },
54 { 17, 185 },
55 { 19, 180 },
56 { 21, 175 },
57 { 24, 170 },
58 { 27, 165 },
59 { 31, 160 },
60 { 32, 158 },
61 { 33, 156 },
62 { 36, 152 },
63 { 37, 150 },
64 { 39, 148 },
65 { 40, 146 },
66 { 41, 144 },
67 { 43, 142 },
68 { 44, 140 },
69 { 48, 135 },
70 { 50, 130 },
71 { 43, 142 },
72 { 53, 125 },
73 { 56, 120 },
74 { 256, 115 },
75};
76
77/* QAM64 SNR lookup table */
Michael Krufky0daa5de2008-04-10 04:24:56 -030078static struct mse2snr_tab qam64_mse2snr_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -030079 { 15, 0 },
80 { 16, 290 },
81 { 17, 288 },
82 { 18, 286 },
83 { 19, 284 },
84 { 20, 282 },
85 { 21, 281 },
86 { 22, 279 },
87 { 23, 277 },
88 { 24, 275 },
89 { 25, 273 },
90 { 26, 271 },
91 { 27, 269 },
92 { 28, 268 },
93 { 29, 266 },
94 { 30, 264 },
95 { 31, 262 },
96 { 32, 260 },
97 { 33, 259 },
98 { 34, 258 },
99 { 35, 256 },
100 { 36, 255 },
101 { 37, 254 },
102 { 38, 252 },
103 { 39, 251 },
104 { 40, 250 },
105 { 41, 249 },
106 { 42, 248 },
107 { 43, 246 },
108 { 44, 245 },
109 { 45, 244 },
110 { 46, 242 },
111 { 47, 241 },
112 { 48, 240 },
113 { 50, 239 },
114 { 51, 238 },
115 { 53, 237 },
116 { 54, 236 },
117 { 56, 235 },
118 { 57, 234 },
119 { 59, 233 },
120 { 60, 232 },
121 { 62, 231 },
122 { 63, 230 },
123 { 65, 229 },
124 { 67, 228 },
125 { 68, 227 },
126 { 70, 226 },
127 { 71, 225 },
128 { 73, 224 },
129 { 74, 223 },
130 { 76, 222 },
131 { 78, 221 },
132 { 80, 220 },
133 { 82, 219 },
134 { 85, 218 },
135 { 88, 217 },
136 { 90, 216 },
137 { 92, 215 },
138 { 93, 214 },
139 { 94, 212 },
140 { 95, 211 },
141 { 97, 210 },
142 { 99, 209 },
143 { 101, 208 },
144 { 102, 207 },
145 { 104, 206 },
146 { 107, 205 },
147 { 111, 204 },
148 { 114, 203 },
149 { 118, 202 },
150 { 122, 201 },
151 { 125, 200 },
152 { 128, 199 },
153 { 130, 198 },
154 { 132, 197 },
155 { 256, 190 },
156};
157
158/* QAM256 SNR lookup table */
Michael Krufky0daa5de2008-04-10 04:24:56 -0300159static struct mse2snr_tab qam256_mse2snr_tab[] = {
Devin Heitmuellerb0c48782012-08-06 22:46:52 -0300160 { 15, 0 },
161 { 16, 400 },
162 { 17, 398 },
163 { 18, 396 },
164 { 19, 394 },
165 { 20, 392 },
166 { 21, 390 },
167 { 22, 388 },
168 { 23, 386 },
169 { 24, 384 },
170 { 25, 382 },
171 { 26, 380 },
172 { 27, 379 },
173 { 28, 378 },
174 { 29, 377 },
175 { 30, 376 },
176 { 31, 375 },
177 { 32, 374 },
178 { 33, 373 },
179 { 34, 372 },
180 { 35, 371 },
181 { 36, 370 },
182 { 37, 362 },
183 { 38, 354 },
184 { 39, 346 },
185 { 40, 338 },
186 { 41, 330 },
187 { 42, 328 },
188 { 43, 326 },
189 { 44, 324 },
190 { 45, 322 },
191 { 46, 320 },
192 { 47, 319 },
193 { 48, 318 },
194 { 49, 317 },
195 { 50, 316 },
196 { 51, 315 },
197 { 52, 314 },
198 { 53, 313 },
199 { 54, 312 },
200 { 55, 311 },
201 { 56, 310 },
202 { 57, 308 },
203 { 58, 306 },
204 { 59, 304 },
205 { 60, 302 },
206 { 61, 300 },
207 { 62, 298 },
Steven Tothf01699b2008-04-10 02:01:48 -0300208 { 65, 295 },
209 { 68, 294 },
210 { 70, 293 },
211 { 73, 292 },
212 { 76, 291 },
213 { 78, 290 },
214 { 79, 289 },
215 { 81, 288 },
216 { 82, 287 },
217 { 83, 286 },
218 { 84, 285 },
219 { 85, 284 },
220 { 86, 283 },
221 { 88, 282 },
222 { 89, 281 },
223 { 256, 280 },
224};
225
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300226static int au8522_mse2snr_lookup(struct mse2snr_tab *tab, int sz, int mse,
227 u16 *snr)
Steven Tothf01699b2008-04-10 02:01:48 -0300228{
229 int i, ret = -EINVAL;
230 dprintk("%s()\n", __func__);
231
Michael Krufky0daa5de2008-04-10 04:24:56 -0300232 for (i = 0; i < sz; i++) {
233 if (mse < tab[i].val) {
234 *snr = tab[i].data;
Steven Tothf01699b2008-04-10 02:01:48 -0300235 ret = 0;
236 break;
237 }
238 }
239 dprintk("%s() snr=%d\n", __func__, *snr);
240 return ret;
241}
242
Michael Krufky2e7acd72008-09-03 16:46:35 -0300243static int au8522_set_if(struct dvb_frontend *fe, enum au8522_if_freq if_freq)
244{
245 struct au8522_state *state = fe->demodulator_priv;
Michael Krufkydf76de02008-09-03 16:46:40 -0300246 u8 r0b5, r0b6, r0b7;
247 char *ifmhz;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300248
249 switch (if_freq) {
250 case AU8522_IF_3_25MHZ:
Michael Krufkydf76de02008-09-03 16:46:40 -0300251 ifmhz = "3.25";
252 r0b5 = 0x00;
253 r0b6 = 0x3d;
254 r0b7 = 0xa0;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300255 break;
256 case AU8522_IF_4MHZ:
Michael Krufkydf76de02008-09-03 16:46:40 -0300257 ifmhz = "4.00";
258 r0b5 = 0x00;
259 r0b6 = 0x4b;
260 r0b7 = 0xd9;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300261 break;
262 case AU8522_IF_6MHZ:
Michael Krufkydf76de02008-09-03 16:46:40 -0300263 ifmhz = "6.00";
264 r0b5 = 0xfb;
265 r0b6 = 0x8e;
266 r0b7 = 0x39;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300267 break;
268 default:
269 dprintk("%s() IF Frequency not supported\n", __func__);
270 return -EINVAL;
271 }
Michael Krufkydf76de02008-09-03 16:46:40 -0300272 dprintk("%s() %s MHz\n", __func__, ifmhz);
273 au8522_writereg(state, 0x80b5, r0b5);
274 au8522_writereg(state, 0x80b6, r0b6);
275 au8522_writereg(state, 0x80b7, r0b7);
276
Michael Krufky2e7acd72008-09-03 16:46:35 -0300277 return 0;
278}
279
Steven Tothf01699b2008-04-10 02:01:48 -0300280/* VSB Modulation table */
281static struct {
282 u16 reg;
283 u16 data;
284} VSB_mod_tab[] = {
285 { 0x8090, 0x84 },
286 { 0x4092, 0x11 },
287 { 0x2005, 0x00 },
288 { 0x8091, 0x80 },
289 { 0x80a3, 0x0c },
290 { 0x80a4, 0xe8 },
291 { 0x8081, 0xc4 },
292 { 0x80a5, 0x40 },
293 { 0x80a7, 0x40 },
294 { 0x80a6, 0x67 },
295 { 0x8262, 0x20 },
296 { 0x821c, 0x30 },
297 { 0x80d8, 0x1a },
298 { 0x8227, 0xa0 },
299 { 0x8121, 0xff },
300 { 0x80a8, 0xf0 },
301 { 0x80a9, 0x05 },
302 { 0x80aa, 0x77 },
303 { 0x80ab, 0xf0 },
304 { 0x80ac, 0x05 },
305 { 0x80ad, 0x77 },
306 { 0x80ae, 0x41 },
307 { 0x80af, 0x66 },
308 { 0x821b, 0xcc },
309 { 0x821d, 0x80 },
Steven Tothf01699b2008-04-10 02:01:48 -0300310 { 0x80a4, 0xe8 },
311 { 0x8231, 0x13 },
312};
313
Frank Dischner040d4cb2009-06-14 23:05:20 -0300314/* QAM64 Modulation table */
Steven Tothf01699b2008-04-10 02:01:48 -0300315static struct {
316 u16 reg;
317 u16 data;
Frank Dischner040d4cb2009-06-14 23:05:20 -0300318} QAM64_mod_tab[] = {
319 { 0x00a3, 0x09 },
320 { 0x00a4, 0x00 },
321 { 0x0081, 0xc4 },
322 { 0x00a5, 0x40 },
323 { 0x00aa, 0x77 },
324 { 0x00ad, 0x77 },
325 { 0x00a6, 0x67 },
326 { 0x0262, 0x20 },
327 { 0x021c, 0x30 },
328 { 0x00b8, 0x3e },
329 { 0x00b9, 0xf0 },
330 { 0x00ba, 0x01 },
331 { 0x00bb, 0x18 },
332 { 0x00bc, 0x50 },
333 { 0x00bd, 0x00 },
334 { 0x00be, 0xea },
335 { 0x00bf, 0xef },
336 { 0x00c0, 0xfc },
337 { 0x00c1, 0xbd },
338 { 0x00c2, 0x1f },
339 { 0x00c3, 0xfc },
340 { 0x00c4, 0xdd },
341 { 0x00c5, 0xaf },
342 { 0x00c6, 0x00 },
343 { 0x00c7, 0x38 },
344 { 0x00c8, 0x30 },
345 { 0x00c9, 0x05 },
346 { 0x00ca, 0x4a },
347 { 0x00cb, 0xd0 },
348 { 0x00cc, 0x01 },
349 { 0x00cd, 0xd9 },
350 { 0x00ce, 0x6f },
351 { 0x00cf, 0xf9 },
352 { 0x00d0, 0x70 },
353 { 0x00d1, 0xdf },
354 { 0x00d2, 0xf7 },
355 { 0x00d3, 0xc2 },
356 { 0x00d4, 0xdf },
357 { 0x00d5, 0x02 },
358 { 0x00d6, 0x9a },
359 { 0x00d7, 0xd0 },
360 { 0x0250, 0x0d },
361 { 0x0251, 0xcd },
362 { 0x0252, 0xe0 },
363 { 0x0253, 0x05 },
364 { 0x0254, 0xa7 },
365 { 0x0255, 0xff },
366 { 0x0256, 0xed },
367 { 0x0257, 0x5b },
368 { 0x0258, 0xae },
369 { 0x0259, 0xe6 },
370 { 0x025a, 0x3d },
371 { 0x025b, 0x0f },
372 { 0x025c, 0x0d },
373 { 0x025d, 0xea },
374 { 0x025e, 0xf2 },
375 { 0x025f, 0x51 },
376 { 0x0260, 0xf5 },
377 { 0x0261, 0x06 },
378 { 0x021a, 0x00 },
379 { 0x0546, 0x40 },
380 { 0x0210, 0xc7 },
381 { 0x0211, 0xaa },
382 { 0x0212, 0xab },
383 { 0x0213, 0x02 },
384 { 0x0502, 0x00 },
385 { 0x0121, 0x04 },
386 { 0x0122, 0x04 },
387 { 0x052e, 0x10 },
388 { 0x00a4, 0xca },
389 { 0x00a7, 0x40 },
390 { 0x0526, 0x01 },
391};
392
393/* QAM256 Modulation table */
394static struct {
395 u16 reg;
396 u16 data;
397} QAM256_mod_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -0300398 { 0x80a3, 0x09 },
399 { 0x80a4, 0x00 },
400 { 0x8081, 0xc4 },
401 { 0x80a5, 0x40 },
Steven Tothf01699b2008-04-10 02:01:48 -0300402 { 0x80aa, 0x77 },
403 { 0x80ad, 0x77 },
404 { 0x80a6, 0x67 },
405 { 0x8262, 0x20 },
406 { 0x821c, 0x30 },
407 { 0x80b8, 0x3e },
408 { 0x80b9, 0xf0 },
409 { 0x80ba, 0x01 },
410 { 0x80bb, 0x18 },
411 { 0x80bc, 0x50 },
412 { 0x80bd, 0x00 },
413 { 0x80be, 0xea },
414 { 0x80bf, 0xef },
415 { 0x80c0, 0xfc },
416 { 0x80c1, 0xbd },
417 { 0x80c2, 0x1f },
418 { 0x80c3, 0xfc },
419 { 0x80c4, 0xdd },
420 { 0x80c5, 0xaf },
421 { 0x80c6, 0x00 },
422 { 0x80c7, 0x38 },
423 { 0x80c8, 0x30 },
424 { 0x80c9, 0x05 },
425 { 0x80ca, 0x4a },
426 { 0x80cb, 0xd0 },
427 { 0x80cc, 0x01 },
428 { 0x80cd, 0xd9 },
429 { 0x80ce, 0x6f },
430 { 0x80cf, 0xf9 },
431 { 0x80d0, 0x70 },
432 { 0x80d1, 0xdf },
433 { 0x80d2, 0xf7 },
434 { 0x80d3, 0xc2 },
435 { 0x80d4, 0xdf },
436 { 0x80d5, 0x02 },
437 { 0x80d6, 0x9a },
438 { 0x80d7, 0xd0 },
439 { 0x8250, 0x0d },
440 { 0x8251, 0xcd },
441 { 0x8252, 0xe0 },
442 { 0x8253, 0x05 },
443 { 0x8254, 0xa7 },
444 { 0x8255, 0xff },
445 { 0x8256, 0xed },
446 { 0x8257, 0x5b },
447 { 0x8258, 0xae },
448 { 0x8259, 0xe6 },
449 { 0x825a, 0x3d },
450 { 0x825b, 0x0f },
451 { 0x825c, 0x0d },
452 { 0x825d, 0xea },
453 { 0x825e, 0xf2 },
454 { 0x825f, 0x51 },
455 { 0x8260, 0xf5 },
456 { 0x8261, 0x06 },
457 { 0x821a, 0x00 },
458 { 0x8546, 0x40 },
459 { 0x8210, 0x26 },
460 { 0x8211, 0xf6 },
461 { 0x8212, 0x84 },
462 { 0x8213, 0x02 },
463 { 0x8502, 0x01 },
464 { 0x8121, 0x04 },
465 { 0x8122, 0x04 },
466 { 0x852e, 0x10 },
467 { 0x80a4, 0xca },
468 { 0x80a7, 0x40 },
469 { 0x8526, 0x01 },
470};
471
Michael Krufkye059b0f2008-04-02 18:59:48 -0300472static int au8522_enable_modulation(struct dvb_frontend *fe,
473 fe_modulation_t m)
Steven Toth265a6512008-04-18 21:34:00 -0300474{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300475 struct au8522_state *state = fe->demodulator_priv;
Steven Tothf01699b2008-04-10 02:01:48 -0300476 int i;
Steven Toth265a6512008-04-18 21:34:00 -0300477
Michael Krufkyce1719a2008-04-02 18:59:48 -0300478 dprintk("%s(0x%08x)\n", __func__, m);
Steven Toth265a6512008-04-18 21:34:00 -0300479
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300480 switch (m) {
Steven Toth265a6512008-04-18 21:34:00 -0300481 case VSB_8:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300482 dprintk("%s() VSB_8\n", __func__);
Steven Tothf01699b2008-04-10 02:01:48 -0300483 for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
484 au8522_writereg(state,
485 VSB_mod_tab[i].reg,
486 VSB_mod_tab[i].data);
Michael Krufky2e7acd72008-09-03 16:46:35 -0300487 au8522_set_if(fe, state->config->vsb_if);
Steven Toth265a6512008-04-18 21:34:00 -0300488 break;
489 case QAM_64:
Frank Dischner040d4cb2009-06-14 23:05:20 -0300490 dprintk("%s() QAM 64\n", __func__);
491 for (i = 0; i < ARRAY_SIZE(QAM64_mod_tab); i++)
Steven Tothf01699b2008-04-10 02:01:48 -0300492 au8522_writereg(state,
Frank Dischner040d4cb2009-06-14 23:05:20 -0300493 QAM64_mod_tab[i].reg,
494 QAM64_mod_tab[i].data);
495 au8522_set_if(fe, state->config->qam_if);
496 break;
497 case QAM_256:
498 dprintk("%s() QAM 256\n", __func__);
499 for (i = 0; i < ARRAY_SIZE(QAM256_mod_tab); i++)
500 au8522_writereg(state,
501 QAM256_mod_tab[i].reg,
502 QAM256_mod_tab[i].data);
Michael Krufky2e7acd72008-09-03 16:46:35 -0300503 au8522_set_if(fe, state->config->qam_if);
Steven Toth265a6512008-04-18 21:34:00 -0300504 break;
505 default:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300506 dprintk("%s() Invalid modulation\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300507 return -EINVAL;
508 }
509
510 state->current_modulation = m;
511
512 return 0;
513}
514
515/* Talk to the demod, set the FEC, GUARD, QAM settings etc */
Mauro Carvalho Chehab9b6a1322011-12-22 09:53:20 -0300516static int au8522_set_frontend(struct dvb_frontend *fe)
Steven Toth265a6512008-04-18 21:34:00 -0300517{
Mauro Carvalho Chehab9b6a1322011-12-22 09:53:20 -0300518 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Michael Krufkye059b0f2008-04-02 18:59:48 -0300519 struct au8522_state *state = fe->demodulator_priv;
Michael Krufky74d50722008-06-13 20:33:23 -0300520 int ret = -EINVAL;
Steven Toth265a6512008-04-18 21:34:00 -0300521
Mauro Carvalho Chehab9b6a1322011-12-22 09:53:20 -0300522 dprintk("%s(frequency=%d)\n", __func__, c->frequency);
Steven Toth265a6512008-04-18 21:34:00 -0300523
Mauro Carvalho Chehab9b6a1322011-12-22 09:53:20 -0300524 if ((state->current_frequency == c->frequency) &&
525 (state->current_modulation == c->modulation))
Michael Krufky74d50722008-06-13 20:33:23 -0300526 return 0;
Steven Toth265a6512008-04-18 21:34:00 -0300527
Steven Toth265a6512008-04-18 21:34:00 -0300528 if (fe->ops.tuner_ops.set_params) {
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300529 if (fe->ops.i2c_gate_ctrl)
530 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -0300531 ret = fe->ops.tuner_ops.set_params(fe);
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300532 if (fe->ops.i2c_gate_ctrl)
533 fe->ops.i2c_gate_ctrl(fe, 0);
Steven Toth265a6512008-04-18 21:34:00 -0300534 }
535
Michael Krufky74d50722008-06-13 20:33:23 -0300536 if (ret < 0)
537 return ret;
538
Michael Krufkyef3d2dc2012-03-12 17:07:22 -0300539 /* Allow the tuner to settle */
540 msleep(100);
541
542 au8522_enable_modulation(fe, c->modulation);
543
Mauro Carvalho Chehab9b6a1322011-12-22 09:53:20 -0300544 state->current_frequency = c->frequency;
Michael Krufky74d50722008-06-13 20:33:23 -0300545
Steven Toth265a6512008-04-18 21:34:00 -0300546 return 0;
547}
548
Michael Krufkye059b0f2008-04-02 18:59:48 -0300549static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status)
Steven Toth265a6512008-04-18 21:34:00 -0300550{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300551 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300552 u8 reg;
553 u32 tuner_status = 0;
554
555 *status = 0;
556
557 if (state->current_modulation == VSB_8) {
Michael Krufkyce1719a2008-04-02 18:59:48 -0300558 dprintk("%s() Checking VSB_8\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300559 reg = au8522_readreg(state, 0x4088);
Steven Toth836c2852008-06-21 19:32:41 -0300560 if ((reg & 0x03) == 0x03)
561 *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
Steven Toth265a6512008-04-18 21:34:00 -0300562 } else {
Michael Krufkyce1719a2008-04-02 18:59:48 -0300563 dprintk("%s() Checking QAM\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300564 reg = au8522_readreg(state, 0x4541);
Michael Krufkye059b0f2008-04-02 18:59:48 -0300565 if (reg & 0x80)
Steven Toth265a6512008-04-18 21:34:00 -0300566 *status |= FE_HAS_VITERBI;
Michael Krufkye059b0f2008-04-02 18:59:48 -0300567 if (reg & 0x20)
Steven Toth265a6512008-04-18 21:34:00 -0300568 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
569 }
570
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300571 switch (state->config->status_mode) {
Steven Toth265a6512008-04-18 21:34:00 -0300572 case AU8522_DEMODLOCKING:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300573 dprintk("%s() DEMODLOCKING\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300574 if (*status & FE_HAS_VITERBI)
575 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
576 break;
577 case AU8522_TUNERLOCKING:
578 /* Get the tuner status */
Michael Krufkyce1719a2008-04-02 18:59:48 -0300579 dprintk("%s() TUNERLOCKING\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300580 if (fe->ops.tuner_ops.get_status) {
581 if (fe->ops.i2c_gate_ctrl)
582 fe->ops.i2c_gate_ctrl(fe, 1);
583
584 fe->ops.tuner_ops.get_status(fe, &tuner_status);
585
586 if (fe->ops.i2c_gate_ctrl)
587 fe->ops.i2c_gate_ctrl(fe, 0);
588 }
589 if (tuner_status)
590 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
591 break;
592 }
Michael Krufkyadeeac32008-04-05 20:55:14 -0300593 state->fe_status = *status;
594
595 if (*status & FE_HAS_LOCK)
596 /* turn on LED, if it isn't on already */
597 au8522_led_ctrl(state, -1);
598 else
599 /* turn off LED */
600 au8522_led_ctrl(state, 0);
Steven Toth265a6512008-04-18 21:34:00 -0300601
Michael Krufkyce1719a2008-04-02 18:59:48 -0300602 dprintk("%s() status 0x%08x\n", __func__, *status);
Steven Toth265a6512008-04-18 21:34:00 -0300603
604 return 0;
605}
606
Michael Krufkyadeeac32008-04-05 20:55:14 -0300607static int au8522_led_status(struct au8522_state *state, const u16 *snr)
608{
609 struct au8522_led_config *led_config = state->config->led_cfg;
610 int led;
611 u16 strong;
612
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300613 /* bail out if we can't control an LED */
Michael Krufkyadeeac32008-04-05 20:55:14 -0300614 if (!led_config)
615 return 0;
616
617 if (0 == (state->fe_status & FE_HAS_LOCK))
618 return au8522_led_ctrl(state, 0);
619 else if (state->current_modulation == QAM_256)
620 strong = led_config->qam256_strong;
621 else if (state->current_modulation == QAM_64)
622 strong = led_config->qam64_strong;
623 else /* (state->current_modulation == VSB_8) */
624 strong = led_config->vsb8_strong;
625
626 if (*snr >= strong)
627 led = 2;
628 else
629 led = 1;
630
631 if ((state->led_state) &&
632 (((strong < *snr) ? (*snr - strong) : (strong - *snr)) <= 10))
633 /* snr didn't change enough to bother
634 * changing the color of the led */
635 return 0;
636
637 return au8522_led_ctrl(state, led);
638}
639
Michael Krufkye059b0f2008-04-02 18:59:48 -0300640static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
Steven Toth265a6512008-04-18 21:34:00 -0300641{
Steven Tothf01699b2008-04-10 02:01:48 -0300642 struct au8522_state *state = fe->demodulator_priv;
643 int ret = -EINVAL;
644
Michael Krufkyce1719a2008-04-02 18:59:48 -0300645 dprintk("%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300646
Steven Tothf01699b2008-04-10 02:01:48 -0300647 if (state->current_modulation == QAM_256)
Michael Krufky0daa5de2008-04-10 04:24:56 -0300648 ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
649 ARRAY_SIZE(qam256_mse2snr_tab),
650 au8522_readreg(state, 0x4522),
651 snr);
Steven Tothf01699b2008-04-10 02:01:48 -0300652 else if (state->current_modulation == QAM_64)
Michael Krufky0daa5de2008-04-10 04:24:56 -0300653 ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
654 ARRAY_SIZE(qam64_mse2snr_tab),
655 au8522_readreg(state, 0x4522),
656 snr);
Steven Tothf01699b2008-04-10 02:01:48 -0300657 else /* VSB_8 */
Michael Krufky0daa5de2008-04-10 04:24:56 -0300658 ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
659 ARRAY_SIZE(vsb_mse2snr_tab),
660 au8522_readreg(state, 0x4311),
661 snr);
Steven Toth265a6512008-04-18 21:34:00 -0300662
Michael Krufkyadeeac32008-04-05 20:55:14 -0300663 if (state->config->led_cfg)
664 au8522_led_status(state, snr);
665
Steven Tothf01699b2008-04-10 02:01:48 -0300666 return ret;
Steven Toth265a6512008-04-18 21:34:00 -0300667}
668
Michael Krufkye059b0f2008-04-02 18:59:48 -0300669static int au8522_read_signal_strength(struct dvb_frontend *fe,
670 u16 *signal_strength)
Steven Toth265a6512008-04-18 21:34:00 -0300671{
Michael Krufky5279b1f2011-10-31 08:11:00 -0300672 /* borrowed from lgdt330x.c
673 *
674 * Calculate strength from SNR up to 35dB
675 * Even though the SNR can go higher than 35dB,
676 * there is some comfort factor in having a range of
677 * strong signals that can show at 100%
678 */
679 u16 snr;
680 u32 tmp;
681 int ret = au8522_read_snr(fe, &snr);
682
683 *signal_strength = 0;
684
685 if (0 == ret) {
686 /* The following calculation method was chosen
687 * purely for the sake of code re-use from the
688 * other demod drivers that use this method */
689
690 /* Convert from SNR in dB * 10 to 8.24 fixed-point */
691 tmp = (snr * ((1 << 24) / 10));
692
693 /* Convert from 8.24 fixed-point to
694 * scale the range 0 - 35*2^24 into 0 - 65535*/
695 if (tmp >= 8960 * 0x10000)
696 *signal_strength = 0xffff;
697 else
698 *signal_strength = tmp / 8960;
699 }
700
701 return ret;
Steven Toth265a6512008-04-18 21:34:00 -0300702}
703
Michael Krufkye059b0f2008-04-02 18:59:48 -0300704static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
Steven Toth265a6512008-04-18 21:34:00 -0300705{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300706 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300707
Michael Krufky8973dc42008-04-05 23:08:08 -0300708 if (state->current_modulation == VSB_8)
709 *ucblocks = au8522_readreg(state, 0x4087);
710 else
711 *ucblocks = au8522_readreg(state, 0x4543);
Steven Toth265a6512008-04-18 21:34:00 -0300712
713 return 0;
714}
715
Michael Krufkye059b0f2008-04-02 18:59:48 -0300716static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
Steven Toth265a6512008-04-18 21:34:00 -0300717{
718 return au8522_read_ucblocks(fe, ber);
719}
720
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300721static int au8522_get_frontend(struct dvb_frontend *fe)
Steven Toth265a6512008-04-18 21:34:00 -0300722{
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300723 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Michael Krufkye059b0f2008-04-02 18:59:48 -0300724 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300725
Mauro Carvalho Chehab9b6a1322011-12-22 09:53:20 -0300726 c->frequency = state->current_frequency;
727 c->modulation = state->current_modulation;
Steven Toth265a6512008-04-18 21:34:00 -0300728
729 return 0;
730}
731
Michael Krufkye059b0f2008-04-02 18:59:48 -0300732static int au8522_get_tune_settings(struct dvb_frontend *fe,
733 struct dvb_frontend_tune_settings *tune)
Steven Toth265a6512008-04-18 21:34:00 -0300734{
735 tune->min_delay_ms = 1000;
736 return 0;
737}
738
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300739static struct dvb_frontend_ops au8522_ops;
740
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300741
Michael Krufkye059b0f2008-04-02 18:59:48 -0300742static void au8522_release(struct dvb_frontend *fe)
Steven Toth265a6512008-04-18 21:34:00 -0300743{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300744 struct au8522_state *state = fe->demodulator_priv;
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300745 au8522_release_state(state);
Steven Toth265a6512008-04-18 21:34:00 -0300746}
747
Michael Krufkye059b0f2008-04-02 18:59:48 -0300748struct dvb_frontend *au8522_attach(const struct au8522_config *config,
749 struct i2c_adapter *i2c)
Steven Toth265a6512008-04-18 21:34:00 -0300750{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300751 struct au8522_state *state = NULL;
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300752 int instance;
Steven Toth265a6512008-04-18 21:34:00 -0300753
754 /* allocate memory for the internal state */
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300755 instance = au8522_get_state(&state, i2c, config->demod_address);
756 switch (instance) {
757 case 0:
758 dprintk("%s state allocation failed\n", __func__);
759 break;
760 case 1:
761 /* new demod instance */
762 dprintk("%s using new instance\n", __func__);
763 break;
764 default:
765 /* existing demod instance */
766 dprintk("%s using existing instance\n", __func__);
767 break;
768 }
Steven Toth265a6512008-04-18 21:34:00 -0300769
770 /* setup the state */
771 state->config = config;
772 state->i2c = i2c;
Devin Heitmueller7f2c9832010-02-17 22:47:55 -0300773 state->operational_mode = AU8522_DIGITAL_MODE;
774
Steven Toth265a6512008-04-18 21:34:00 -0300775 /* create dvb_frontend */
776 memcpy(&state->frontend.ops, &au8522_ops,
777 sizeof(struct dvb_frontend_ops));
778 state->frontend.demodulator_priv = state;
779
Devin Heitmueller4a03daf2012-08-06 22:47:02 -0300780 state->frontend.ops.analog_ops.i2c_gate_ctrl = au8522_analog_i2c_gate_ctrl;
781
Steven Toth265a6512008-04-18 21:34:00 -0300782 if (au8522_init(&state->frontend) != 0) {
783 printk(KERN_ERR "%s: Failed to initialize correctly\n",
Michael Krufkyce1719a2008-04-02 18:59:48 -0300784 __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300785 goto error;
786 }
787
788 /* Note: Leaving the I2C gate open here. */
789 au8522_i2c_gate_ctrl(&state->frontend, 1);
790
791 return &state->frontend;
792
793error:
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300794 au8522_release_state(state);
Steven Toth265a6512008-04-18 21:34:00 -0300795 return NULL;
796}
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300797EXPORT_SYMBOL(au8522_attach);
Steven Toth265a6512008-04-18 21:34:00 -0300798
799static struct dvb_frontend_ops au8522_ops = {
Mauro Carvalho Chehab9b6a1322011-12-22 09:53:20 -0300800 .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
Steven Toth265a6512008-04-18 21:34:00 -0300801 .info = {
802 .name = "Auvitek AU8522 QAM/8VSB Frontend",
Steven Toth265a6512008-04-18 21:34:00 -0300803 .frequency_min = 54000000,
804 .frequency_max = 858000000,
805 .frequency_stepsize = 62500,
806 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
807 },
808
809 .init = au8522_init,
Michael Krufky74d50722008-06-13 20:33:23 -0300810 .sleep = au8522_sleep,
Steven Toth265a6512008-04-18 21:34:00 -0300811 .i2c_gate_ctrl = au8522_i2c_gate_ctrl,
Mauro Carvalho Chehab9b6a1322011-12-22 09:53:20 -0300812 .set_frontend = au8522_set_frontend,
813 .get_frontend = au8522_get_frontend,
Steven Toth265a6512008-04-18 21:34:00 -0300814 .get_tune_settings = au8522_get_tune_settings,
815 .read_status = au8522_read_status,
816 .read_ber = au8522_read_ber,
817 .read_signal_strength = au8522_read_signal_strength,
818 .read_snr = au8522_read_snr,
819 .read_ucblocks = au8522_read_ucblocks,
820 .release = au8522_release,
821};
822
823module_param(debug, int, 0644);
824MODULE_PARM_DESC(debug, "Enable verbose debug messages");
825
826MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
827MODULE_AUTHOR("Steven Toth");
828MODULE_LICENSE("GPL");