blob: 7ed326e43fc4c6dd518430a141be039f3257a434 [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;
Richard Vollkommerdcc33cc2014-10-25 17:17:23 -030032static int zv_mode = 1; /* default to on */
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -030033
Devin Heitmueller62899a22009-03-15 18:48:52 -030034#define dprintk(arg...)\
35 do { if (debug)\
36 printk(arg);\
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -030037 } while (0)
Steven Toth265a6512008-04-18 21:34:00 -030038
Michael Krufky0daa5de2008-04-10 04:24:56 -030039struct mse2snr_tab {
Steven Tothf01699b2008-04-10 02:01:48 -030040 u16 val;
41 u16 data;
Michael Krufky0daa5de2008-04-10 04:24:56 -030042};
43
44/* VSB SNR lookup table */
45static struct mse2snr_tab vsb_mse2snr_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -030046 { 0, 270 },
47 { 2, 250 },
48 { 3, 240 },
49 { 5, 230 },
50 { 7, 220 },
51 { 9, 210 },
52 { 12, 200 },
53 { 13, 195 },
54 { 15, 190 },
55 { 17, 185 },
56 { 19, 180 },
57 { 21, 175 },
58 { 24, 170 },
59 { 27, 165 },
60 { 31, 160 },
61 { 32, 158 },
62 { 33, 156 },
63 { 36, 152 },
64 { 37, 150 },
65 { 39, 148 },
66 { 40, 146 },
67 { 41, 144 },
68 { 43, 142 },
69 { 44, 140 },
70 { 48, 135 },
71 { 50, 130 },
72 { 43, 142 },
73 { 53, 125 },
74 { 56, 120 },
75 { 256, 115 },
76};
77
78/* QAM64 SNR lookup table */
Michael Krufky0daa5de2008-04-10 04:24:56 -030079static struct mse2snr_tab qam64_mse2snr_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -030080 { 15, 0 },
81 { 16, 290 },
82 { 17, 288 },
83 { 18, 286 },
84 { 19, 284 },
85 { 20, 282 },
86 { 21, 281 },
87 { 22, 279 },
88 { 23, 277 },
89 { 24, 275 },
90 { 25, 273 },
91 { 26, 271 },
92 { 27, 269 },
93 { 28, 268 },
94 { 29, 266 },
95 { 30, 264 },
96 { 31, 262 },
97 { 32, 260 },
98 { 33, 259 },
99 { 34, 258 },
100 { 35, 256 },
101 { 36, 255 },
102 { 37, 254 },
103 { 38, 252 },
104 { 39, 251 },
105 { 40, 250 },
106 { 41, 249 },
107 { 42, 248 },
108 { 43, 246 },
109 { 44, 245 },
110 { 45, 244 },
111 { 46, 242 },
112 { 47, 241 },
113 { 48, 240 },
114 { 50, 239 },
115 { 51, 238 },
116 { 53, 237 },
117 { 54, 236 },
118 { 56, 235 },
119 { 57, 234 },
120 { 59, 233 },
121 { 60, 232 },
122 { 62, 231 },
123 { 63, 230 },
124 { 65, 229 },
125 { 67, 228 },
126 { 68, 227 },
127 { 70, 226 },
128 { 71, 225 },
129 { 73, 224 },
130 { 74, 223 },
131 { 76, 222 },
132 { 78, 221 },
133 { 80, 220 },
134 { 82, 219 },
135 { 85, 218 },
136 { 88, 217 },
137 { 90, 216 },
138 { 92, 215 },
139 { 93, 214 },
140 { 94, 212 },
141 { 95, 211 },
142 { 97, 210 },
143 { 99, 209 },
144 { 101, 208 },
145 { 102, 207 },
146 { 104, 206 },
147 { 107, 205 },
148 { 111, 204 },
149 { 114, 203 },
150 { 118, 202 },
151 { 122, 201 },
152 { 125, 200 },
153 { 128, 199 },
154 { 130, 198 },
155 { 132, 197 },
156 { 256, 190 },
157};
158
159/* QAM256 SNR lookup table */
Michael Krufky0daa5de2008-04-10 04:24:56 -0300160static struct mse2snr_tab qam256_mse2snr_tab[] = {
Devin Heitmuellerb0c48782012-08-06 22:46:52 -0300161 { 15, 0 },
162 { 16, 400 },
163 { 17, 398 },
164 { 18, 396 },
165 { 19, 394 },
166 { 20, 392 },
167 { 21, 390 },
168 { 22, 388 },
169 { 23, 386 },
170 { 24, 384 },
171 { 25, 382 },
172 { 26, 380 },
173 { 27, 379 },
174 { 28, 378 },
175 { 29, 377 },
176 { 30, 376 },
177 { 31, 375 },
178 { 32, 374 },
179 { 33, 373 },
180 { 34, 372 },
181 { 35, 371 },
182 { 36, 370 },
183 { 37, 362 },
184 { 38, 354 },
185 { 39, 346 },
186 { 40, 338 },
187 { 41, 330 },
188 { 42, 328 },
189 { 43, 326 },
190 { 44, 324 },
191 { 45, 322 },
192 { 46, 320 },
193 { 47, 319 },
194 { 48, 318 },
195 { 49, 317 },
196 { 50, 316 },
197 { 51, 315 },
198 { 52, 314 },
199 { 53, 313 },
200 { 54, 312 },
201 { 55, 311 },
202 { 56, 310 },
203 { 57, 308 },
204 { 58, 306 },
205 { 59, 304 },
206 { 60, 302 },
207 { 61, 300 },
208 { 62, 298 },
Steven Tothf01699b2008-04-10 02:01:48 -0300209 { 65, 295 },
210 { 68, 294 },
211 { 70, 293 },
212 { 73, 292 },
213 { 76, 291 },
214 { 78, 290 },
215 { 79, 289 },
216 { 81, 288 },
217 { 82, 287 },
218 { 83, 286 },
219 { 84, 285 },
220 { 85, 284 },
221 { 86, 283 },
222 { 88, 282 },
223 { 89, 281 },
224 { 256, 280 },
225};
226
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300227static int au8522_mse2snr_lookup(struct mse2snr_tab *tab, int sz, int mse,
228 u16 *snr)
Steven Tothf01699b2008-04-10 02:01:48 -0300229{
230 int i, ret = -EINVAL;
231 dprintk("%s()\n", __func__);
232
Michael Krufky0daa5de2008-04-10 04:24:56 -0300233 for (i = 0; i < sz; i++) {
234 if (mse < tab[i].val) {
235 *snr = tab[i].data;
Steven Tothf01699b2008-04-10 02:01:48 -0300236 ret = 0;
237 break;
238 }
239 }
240 dprintk("%s() snr=%d\n", __func__, *snr);
241 return ret;
242}
243
Michael Krufky2e7acd72008-09-03 16:46:35 -0300244static int au8522_set_if(struct dvb_frontend *fe, enum au8522_if_freq if_freq)
245{
246 struct au8522_state *state = fe->demodulator_priv;
Michael Krufkydf76de02008-09-03 16:46:40 -0300247 u8 r0b5, r0b6, r0b7;
248 char *ifmhz;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300249
250 switch (if_freq) {
251 case AU8522_IF_3_25MHZ:
Michael Krufkydf76de02008-09-03 16:46:40 -0300252 ifmhz = "3.25";
253 r0b5 = 0x00;
254 r0b6 = 0x3d;
255 r0b7 = 0xa0;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300256 break;
257 case AU8522_IF_4MHZ:
Michael Krufkydf76de02008-09-03 16:46:40 -0300258 ifmhz = "4.00";
259 r0b5 = 0x00;
260 r0b6 = 0x4b;
261 r0b7 = 0xd9;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300262 break;
263 case AU8522_IF_6MHZ:
Michael Krufkydf76de02008-09-03 16:46:40 -0300264 ifmhz = "6.00";
265 r0b5 = 0xfb;
266 r0b6 = 0x8e;
267 r0b7 = 0x39;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300268 break;
269 default:
270 dprintk("%s() IF Frequency not supported\n", __func__);
271 return -EINVAL;
272 }
Michael Krufkydf76de02008-09-03 16:46:40 -0300273 dprintk("%s() %s MHz\n", __func__, ifmhz);
274 au8522_writereg(state, 0x80b5, r0b5);
275 au8522_writereg(state, 0x80b6, r0b6);
276 au8522_writereg(state, 0x80b7, r0b7);
277
Michael Krufky2e7acd72008-09-03 16:46:35 -0300278 return 0;
279}
280
Steven Tothf01699b2008-04-10 02:01:48 -0300281/* VSB Modulation table */
282static struct {
283 u16 reg;
284 u16 data;
285} VSB_mod_tab[] = {
286 { 0x8090, 0x84 },
287 { 0x4092, 0x11 },
288 { 0x2005, 0x00 },
289 { 0x8091, 0x80 },
290 { 0x80a3, 0x0c },
291 { 0x80a4, 0xe8 },
292 { 0x8081, 0xc4 },
293 { 0x80a5, 0x40 },
294 { 0x80a7, 0x40 },
295 { 0x80a6, 0x67 },
296 { 0x8262, 0x20 },
297 { 0x821c, 0x30 },
298 { 0x80d8, 0x1a },
299 { 0x8227, 0xa0 },
300 { 0x8121, 0xff },
301 { 0x80a8, 0xf0 },
302 { 0x80a9, 0x05 },
303 { 0x80aa, 0x77 },
304 { 0x80ab, 0xf0 },
305 { 0x80ac, 0x05 },
306 { 0x80ad, 0x77 },
307 { 0x80ae, 0x41 },
308 { 0x80af, 0x66 },
309 { 0x821b, 0xcc },
310 { 0x821d, 0x80 },
Steven Tothf01699b2008-04-10 02:01:48 -0300311 { 0x80a4, 0xe8 },
312 { 0x8231, 0x13 },
313};
314
Frank Dischner040d4cb2009-06-14 23:05:20 -0300315/* QAM64 Modulation table */
Steven Tothf01699b2008-04-10 02:01:48 -0300316static struct {
317 u16 reg;
318 u16 data;
Frank Dischner040d4cb2009-06-14 23:05:20 -0300319} QAM64_mod_tab[] = {
320 { 0x00a3, 0x09 },
321 { 0x00a4, 0x00 },
322 { 0x0081, 0xc4 },
323 { 0x00a5, 0x40 },
324 { 0x00aa, 0x77 },
325 { 0x00ad, 0x77 },
326 { 0x00a6, 0x67 },
327 { 0x0262, 0x20 },
328 { 0x021c, 0x30 },
329 { 0x00b8, 0x3e },
330 { 0x00b9, 0xf0 },
331 { 0x00ba, 0x01 },
332 { 0x00bb, 0x18 },
333 { 0x00bc, 0x50 },
334 { 0x00bd, 0x00 },
335 { 0x00be, 0xea },
336 { 0x00bf, 0xef },
337 { 0x00c0, 0xfc },
338 { 0x00c1, 0xbd },
339 { 0x00c2, 0x1f },
340 { 0x00c3, 0xfc },
341 { 0x00c4, 0xdd },
342 { 0x00c5, 0xaf },
343 { 0x00c6, 0x00 },
344 { 0x00c7, 0x38 },
345 { 0x00c8, 0x30 },
346 { 0x00c9, 0x05 },
347 { 0x00ca, 0x4a },
348 { 0x00cb, 0xd0 },
349 { 0x00cc, 0x01 },
350 { 0x00cd, 0xd9 },
351 { 0x00ce, 0x6f },
352 { 0x00cf, 0xf9 },
353 { 0x00d0, 0x70 },
354 { 0x00d1, 0xdf },
355 { 0x00d2, 0xf7 },
356 { 0x00d3, 0xc2 },
357 { 0x00d4, 0xdf },
358 { 0x00d5, 0x02 },
359 { 0x00d6, 0x9a },
360 { 0x00d7, 0xd0 },
361 { 0x0250, 0x0d },
362 { 0x0251, 0xcd },
363 { 0x0252, 0xe0 },
364 { 0x0253, 0x05 },
365 { 0x0254, 0xa7 },
366 { 0x0255, 0xff },
367 { 0x0256, 0xed },
368 { 0x0257, 0x5b },
369 { 0x0258, 0xae },
370 { 0x0259, 0xe6 },
371 { 0x025a, 0x3d },
372 { 0x025b, 0x0f },
373 { 0x025c, 0x0d },
374 { 0x025d, 0xea },
375 { 0x025e, 0xf2 },
376 { 0x025f, 0x51 },
377 { 0x0260, 0xf5 },
378 { 0x0261, 0x06 },
379 { 0x021a, 0x00 },
380 { 0x0546, 0x40 },
381 { 0x0210, 0xc7 },
382 { 0x0211, 0xaa },
383 { 0x0212, 0xab },
384 { 0x0213, 0x02 },
385 { 0x0502, 0x00 },
386 { 0x0121, 0x04 },
387 { 0x0122, 0x04 },
388 { 0x052e, 0x10 },
389 { 0x00a4, 0xca },
390 { 0x00a7, 0x40 },
391 { 0x0526, 0x01 },
392};
393
394/* QAM256 Modulation table */
395static struct {
396 u16 reg;
397 u16 data;
398} QAM256_mod_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -0300399 { 0x80a3, 0x09 },
400 { 0x80a4, 0x00 },
401 { 0x8081, 0xc4 },
402 { 0x80a5, 0x40 },
Steven Tothf01699b2008-04-10 02:01:48 -0300403 { 0x80aa, 0x77 },
404 { 0x80ad, 0x77 },
405 { 0x80a6, 0x67 },
406 { 0x8262, 0x20 },
407 { 0x821c, 0x30 },
408 { 0x80b8, 0x3e },
409 { 0x80b9, 0xf0 },
410 { 0x80ba, 0x01 },
411 { 0x80bb, 0x18 },
412 { 0x80bc, 0x50 },
413 { 0x80bd, 0x00 },
414 { 0x80be, 0xea },
415 { 0x80bf, 0xef },
416 { 0x80c0, 0xfc },
417 { 0x80c1, 0xbd },
418 { 0x80c2, 0x1f },
419 { 0x80c3, 0xfc },
420 { 0x80c4, 0xdd },
421 { 0x80c5, 0xaf },
422 { 0x80c6, 0x00 },
423 { 0x80c7, 0x38 },
424 { 0x80c8, 0x30 },
425 { 0x80c9, 0x05 },
426 { 0x80ca, 0x4a },
427 { 0x80cb, 0xd0 },
428 { 0x80cc, 0x01 },
429 { 0x80cd, 0xd9 },
430 { 0x80ce, 0x6f },
431 { 0x80cf, 0xf9 },
432 { 0x80d0, 0x70 },
433 { 0x80d1, 0xdf },
434 { 0x80d2, 0xf7 },
435 { 0x80d3, 0xc2 },
436 { 0x80d4, 0xdf },
437 { 0x80d5, 0x02 },
438 { 0x80d6, 0x9a },
439 { 0x80d7, 0xd0 },
440 { 0x8250, 0x0d },
441 { 0x8251, 0xcd },
442 { 0x8252, 0xe0 },
443 { 0x8253, 0x05 },
444 { 0x8254, 0xa7 },
445 { 0x8255, 0xff },
446 { 0x8256, 0xed },
447 { 0x8257, 0x5b },
448 { 0x8258, 0xae },
449 { 0x8259, 0xe6 },
450 { 0x825a, 0x3d },
451 { 0x825b, 0x0f },
452 { 0x825c, 0x0d },
453 { 0x825d, 0xea },
454 { 0x825e, 0xf2 },
455 { 0x825f, 0x51 },
456 { 0x8260, 0xf5 },
457 { 0x8261, 0x06 },
458 { 0x821a, 0x00 },
459 { 0x8546, 0x40 },
460 { 0x8210, 0x26 },
461 { 0x8211, 0xf6 },
462 { 0x8212, 0x84 },
463 { 0x8213, 0x02 },
464 { 0x8502, 0x01 },
465 { 0x8121, 0x04 },
466 { 0x8122, 0x04 },
467 { 0x852e, 0x10 },
468 { 0x80a4, 0xca },
469 { 0x80a7, 0x40 },
470 { 0x8526, 0x01 },
471};
472
Richard Vollkommerdcc33cc2014-10-25 17:17:23 -0300473static struct {
474 u16 reg;
475 u16 data;
476} QAM256_mod_tab_zv_mode[] = {
477 { 0x80a3, 0x09 },
478 { 0x80a4, 0x00 },
479 { 0x8081, 0xc4 },
480 { 0x80a5, 0x40 },
481 { 0x80b5, 0xfb },
482 { 0x80b6, 0x8e },
483 { 0x80b7, 0x39 },
484 { 0x80aa, 0x77 },
485 { 0x80ad, 0x77 },
486 { 0x80a6, 0x67 },
487 { 0x8262, 0x20 },
488 { 0x821c, 0x30 },
489 { 0x80b8, 0x3e },
490 { 0x80b9, 0xf0 },
491 { 0x80ba, 0x01 },
492 { 0x80bb, 0x18 },
493 { 0x80bc, 0x50 },
494 { 0x80bd, 0x00 },
495 { 0x80be, 0xea },
496 { 0x80bf, 0xef },
497 { 0x80c0, 0xfc },
498 { 0x80c1, 0xbd },
499 { 0x80c2, 0x1f },
500 { 0x80c3, 0xfc },
501 { 0x80c4, 0xdd },
502 { 0x80c5, 0xaf },
503 { 0x80c6, 0x00 },
504 { 0x80c7, 0x38 },
505 { 0x80c8, 0x30 },
506 { 0x80c9, 0x05 },
507 { 0x80ca, 0x4a },
508 { 0x80cb, 0xd0 },
509 { 0x80cc, 0x01 },
510 { 0x80cd, 0xd9 },
511 { 0x80ce, 0x6f },
512 { 0x80cf, 0xf9 },
513 { 0x80d0, 0x70 },
514 { 0x80d1, 0xdf },
515 { 0x80d2, 0xf7 },
516 { 0x80d3, 0xc2 },
517 { 0x80d4, 0xdf },
518 { 0x80d5, 0x02 },
519 { 0x80d6, 0x9a },
520 { 0x80d7, 0xd0 },
521 { 0x8250, 0x0d },
522 { 0x8251, 0xcd },
523 { 0x8252, 0xe0 },
524 { 0x8253, 0x05 },
525 { 0x8254, 0xa7 },
526 { 0x8255, 0xff },
527 { 0x8256, 0xed },
528 { 0x8257, 0x5b },
529 { 0x8258, 0xae },
530 { 0x8259, 0xe6 },
531 { 0x825a, 0x3d },
532 { 0x825b, 0x0f },
533 { 0x825c, 0x0d },
534 { 0x825d, 0xea },
535 { 0x825e, 0xf2 },
536 { 0x825f, 0x51 },
537 { 0x8260, 0xf5 },
538 { 0x8261, 0x06 },
539 { 0x821a, 0x01 },
540 { 0x8546, 0x40 },
541 { 0x8210, 0x26 },
542 { 0x8211, 0xf6 },
543 { 0x8212, 0x84 },
544 { 0x8213, 0x02 },
545 { 0x8502, 0x01 },
546 { 0x8121, 0x04 },
547 { 0x8122, 0x04 },
548 { 0x852e, 0x10 },
549 { 0x80a4, 0xca },
550 { 0x80a7, 0x40 },
551 { 0x8526, 0x01 },
552};
553
Michael Krufkye059b0f2008-04-02 18:59:48 -0300554static int au8522_enable_modulation(struct dvb_frontend *fe,
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300555 enum fe_modulation m)
Steven Toth265a6512008-04-18 21:34:00 -0300556{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300557 struct au8522_state *state = fe->demodulator_priv;
Steven Tothf01699b2008-04-10 02:01:48 -0300558 int i;
Steven Toth265a6512008-04-18 21:34:00 -0300559
Michael Krufkyce1719a2008-04-02 18:59:48 -0300560 dprintk("%s(0x%08x)\n", __func__, m);
Steven Toth265a6512008-04-18 21:34:00 -0300561
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300562 switch (m) {
Steven Toth265a6512008-04-18 21:34:00 -0300563 case VSB_8:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300564 dprintk("%s() VSB_8\n", __func__);
Steven Tothf01699b2008-04-10 02:01:48 -0300565 for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
566 au8522_writereg(state,
567 VSB_mod_tab[i].reg,
568 VSB_mod_tab[i].data);
Mauro Carvalho Chehabaa377632015-12-21 14:38:31 -0200569 au8522_set_if(fe, state->config.vsb_if);
Steven Toth265a6512008-04-18 21:34:00 -0300570 break;
571 case QAM_64:
Frank Dischner040d4cb2009-06-14 23:05:20 -0300572 dprintk("%s() QAM 64\n", __func__);
573 for (i = 0; i < ARRAY_SIZE(QAM64_mod_tab); i++)
Steven Tothf01699b2008-04-10 02:01:48 -0300574 au8522_writereg(state,
Frank Dischner040d4cb2009-06-14 23:05:20 -0300575 QAM64_mod_tab[i].reg,
576 QAM64_mod_tab[i].data);
Mauro Carvalho Chehabaa377632015-12-21 14:38:31 -0200577 au8522_set_if(fe, state->config.qam_if);
Frank Dischner040d4cb2009-06-14 23:05:20 -0300578 break;
579 case QAM_256:
Richard Vollkommerdcc33cc2014-10-25 17:17:23 -0300580 if (zv_mode) {
581 dprintk("%s() QAM 256 (zv_mode)\n", __func__);
582 for (i = 0; i < ARRAY_SIZE(QAM256_mod_tab_zv_mode); i++)
583 au8522_writereg(state,
584 QAM256_mod_tab_zv_mode[i].reg,
585 QAM256_mod_tab_zv_mode[i].data);
Mauro Carvalho Chehabaa377632015-12-21 14:38:31 -0200586 au8522_set_if(fe, state->config.qam_if);
Richard Vollkommerdcc33cc2014-10-25 17:17:23 -0300587 msleep(100);
588 au8522_writereg(state, 0x821a, 0x00);
589 } else {
590 dprintk("%s() QAM 256\n", __func__);
591 for (i = 0; i < ARRAY_SIZE(QAM256_mod_tab); i++)
592 au8522_writereg(state,
593 QAM256_mod_tab[i].reg,
594 QAM256_mod_tab[i].data);
Mauro Carvalho Chehabaa377632015-12-21 14:38:31 -0200595 au8522_set_if(fe, state->config.qam_if);
Richard Vollkommerdcc33cc2014-10-25 17:17:23 -0300596 }
Steven Toth265a6512008-04-18 21:34:00 -0300597 break;
598 default:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300599 dprintk("%s() Invalid modulation\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300600 return -EINVAL;
601 }
602
603 state->current_modulation = m;
604
605 return 0;
606}
607
608/* Talk to the demod, set the FEC, GUARD, QAM settings etc */
Mauro Carvalho Chehab9b6a1322011-12-22 09:53:20 -0300609static int au8522_set_frontend(struct dvb_frontend *fe)
Steven Toth265a6512008-04-18 21:34:00 -0300610{
Mauro Carvalho Chehab9b6a1322011-12-22 09:53:20 -0300611 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Michael Krufkye059b0f2008-04-02 18:59:48 -0300612 struct au8522_state *state = fe->demodulator_priv;
Michael Krufky74d50722008-06-13 20:33:23 -0300613 int ret = -EINVAL;
Steven Toth265a6512008-04-18 21:34:00 -0300614
Mauro Carvalho Chehab9b6a1322011-12-22 09:53:20 -0300615 dprintk("%s(frequency=%d)\n", __func__, c->frequency);
Steven Toth265a6512008-04-18 21:34:00 -0300616
Mauro Carvalho Chehab9b6a1322011-12-22 09:53:20 -0300617 if ((state->current_frequency == c->frequency) &&
618 (state->current_modulation == c->modulation))
Michael Krufky74d50722008-06-13 20:33:23 -0300619 return 0;
Steven Toth265a6512008-04-18 21:34:00 -0300620
Steven Toth265a6512008-04-18 21:34:00 -0300621 if (fe->ops.tuner_ops.set_params) {
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300622 if (fe->ops.i2c_gate_ctrl)
623 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -0300624 ret = fe->ops.tuner_ops.set_params(fe);
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300625 if (fe->ops.i2c_gate_ctrl)
626 fe->ops.i2c_gate_ctrl(fe, 0);
Steven Toth265a6512008-04-18 21:34:00 -0300627 }
628
Michael Krufky74d50722008-06-13 20:33:23 -0300629 if (ret < 0)
630 return ret;
631
Michael Krufkyef3d2dc2012-03-12 17:07:22 -0300632 /* Allow the tuner to settle */
Richard Vollkommerdcc33cc2014-10-25 17:17:23 -0300633 if (zv_mode) {
634 dprintk("%s() increase tuner settling time for zv_mode\n",
635 __func__);
636 msleep(250);
637 } else
638 msleep(100);
Michael Krufkyef3d2dc2012-03-12 17:07:22 -0300639
640 au8522_enable_modulation(fe, c->modulation);
641
Mauro Carvalho Chehab9b6a1322011-12-22 09:53:20 -0300642 state->current_frequency = c->frequency;
Michael Krufky74d50722008-06-13 20:33:23 -0300643
Steven Toth265a6512008-04-18 21:34:00 -0300644 return 0;
645}
646
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300647static int au8522_read_status(struct dvb_frontend *fe, enum fe_status *status)
Steven Toth265a6512008-04-18 21:34:00 -0300648{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300649 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300650 u8 reg;
651 u32 tuner_status = 0;
652
653 *status = 0;
654
655 if (state->current_modulation == VSB_8) {
Michael Krufkyce1719a2008-04-02 18:59:48 -0300656 dprintk("%s() Checking VSB_8\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300657 reg = au8522_readreg(state, 0x4088);
Steven Toth836c2852008-06-21 19:32:41 -0300658 if ((reg & 0x03) == 0x03)
659 *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
Steven Toth265a6512008-04-18 21:34:00 -0300660 } else {
Michael Krufkyce1719a2008-04-02 18:59:48 -0300661 dprintk("%s() Checking QAM\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300662 reg = au8522_readreg(state, 0x4541);
Michael Krufkye059b0f2008-04-02 18:59:48 -0300663 if (reg & 0x80)
Steven Toth265a6512008-04-18 21:34:00 -0300664 *status |= FE_HAS_VITERBI;
Michael Krufkye059b0f2008-04-02 18:59:48 -0300665 if (reg & 0x20)
Steven Toth265a6512008-04-18 21:34:00 -0300666 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
667 }
668
Mauro Carvalho Chehabaa377632015-12-21 14:38:31 -0200669 switch (state->config.status_mode) {
Steven Toth265a6512008-04-18 21:34:00 -0300670 case AU8522_DEMODLOCKING:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300671 dprintk("%s() DEMODLOCKING\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300672 if (*status & FE_HAS_VITERBI)
673 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
674 break;
675 case AU8522_TUNERLOCKING:
676 /* Get the tuner status */
Michael Krufkyce1719a2008-04-02 18:59:48 -0300677 dprintk("%s() TUNERLOCKING\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300678 if (fe->ops.tuner_ops.get_status) {
679 if (fe->ops.i2c_gate_ctrl)
680 fe->ops.i2c_gate_ctrl(fe, 1);
681
682 fe->ops.tuner_ops.get_status(fe, &tuner_status);
683
684 if (fe->ops.i2c_gate_ctrl)
685 fe->ops.i2c_gate_ctrl(fe, 0);
686 }
687 if (tuner_status)
688 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
689 break;
690 }
Michael Krufkyadeeac32008-04-05 20:55:14 -0300691 state->fe_status = *status;
692
693 if (*status & FE_HAS_LOCK)
694 /* turn on LED, if it isn't on already */
695 au8522_led_ctrl(state, -1);
696 else
697 /* turn off LED */
698 au8522_led_ctrl(state, 0);
Steven Toth265a6512008-04-18 21:34:00 -0300699
Michael Krufkyce1719a2008-04-02 18:59:48 -0300700 dprintk("%s() status 0x%08x\n", __func__, *status);
Steven Toth265a6512008-04-18 21:34:00 -0300701
702 return 0;
703}
704
Michael Krufkyadeeac32008-04-05 20:55:14 -0300705static int au8522_led_status(struct au8522_state *state, const u16 *snr)
706{
Mauro Carvalho Chehabaa377632015-12-21 14:38:31 -0200707 struct au8522_led_config *led_config = state->config.led_cfg;
Michael Krufkyadeeac32008-04-05 20:55:14 -0300708 int led;
709 u16 strong;
710
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300711 /* bail out if we can't control an LED */
Michael Krufkyadeeac32008-04-05 20:55:14 -0300712 if (!led_config)
713 return 0;
714
715 if (0 == (state->fe_status & FE_HAS_LOCK))
716 return au8522_led_ctrl(state, 0);
717 else if (state->current_modulation == QAM_256)
718 strong = led_config->qam256_strong;
719 else if (state->current_modulation == QAM_64)
720 strong = led_config->qam64_strong;
721 else /* (state->current_modulation == VSB_8) */
722 strong = led_config->vsb8_strong;
723
724 if (*snr >= strong)
725 led = 2;
726 else
727 led = 1;
728
729 if ((state->led_state) &&
730 (((strong < *snr) ? (*snr - strong) : (strong - *snr)) <= 10))
731 /* snr didn't change enough to bother
732 * changing the color of the led */
733 return 0;
734
735 return au8522_led_ctrl(state, led);
736}
737
Michael Krufkye059b0f2008-04-02 18:59:48 -0300738static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
Steven Toth265a6512008-04-18 21:34:00 -0300739{
Steven Tothf01699b2008-04-10 02:01:48 -0300740 struct au8522_state *state = fe->demodulator_priv;
741 int ret = -EINVAL;
742
Michael Krufkyce1719a2008-04-02 18:59:48 -0300743 dprintk("%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300744
Steven Tothf01699b2008-04-10 02:01:48 -0300745 if (state->current_modulation == QAM_256)
Michael Krufky0daa5de2008-04-10 04:24:56 -0300746 ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
747 ARRAY_SIZE(qam256_mse2snr_tab),
748 au8522_readreg(state, 0x4522),
749 snr);
Steven Tothf01699b2008-04-10 02:01:48 -0300750 else if (state->current_modulation == QAM_64)
Michael Krufky0daa5de2008-04-10 04:24:56 -0300751 ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
752 ARRAY_SIZE(qam64_mse2snr_tab),
753 au8522_readreg(state, 0x4522),
754 snr);
Steven Tothf01699b2008-04-10 02:01:48 -0300755 else /* VSB_8 */
Michael Krufky0daa5de2008-04-10 04:24:56 -0300756 ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
757 ARRAY_SIZE(vsb_mse2snr_tab),
758 au8522_readreg(state, 0x4311),
759 snr);
Steven Toth265a6512008-04-18 21:34:00 -0300760
Mauro Carvalho Chehabaa377632015-12-21 14:38:31 -0200761 if (state->config.led_cfg)
Michael Krufkyadeeac32008-04-05 20:55:14 -0300762 au8522_led_status(state, snr);
763
Steven Tothf01699b2008-04-10 02:01:48 -0300764 return ret;
Steven Toth265a6512008-04-18 21:34:00 -0300765}
766
Michael Krufkye059b0f2008-04-02 18:59:48 -0300767static int au8522_read_signal_strength(struct dvb_frontend *fe,
768 u16 *signal_strength)
Steven Toth265a6512008-04-18 21:34:00 -0300769{
Michael Krufky5279b1f2011-10-31 08:11:00 -0300770 /* borrowed from lgdt330x.c
771 *
772 * Calculate strength from SNR up to 35dB
773 * Even though the SNR can go higher than 35dB,
774 * there is some comfort factor in having a range of
775 * strong signals that can show at 100%
776 */
777 u16 snr;
778 u32 tmp;
779 int ret = au8522_read_snr(fe, &snr);
780
781 *signal_strength = 0;
782
783 if (0 == ret) {
784 /* The following calculation method was chosen
785 * purely for the sake of code re-use from the
786 * other demod drivers that use this method */
787
788 /* Convert from SNR in dB * 10 to 8.24 fixed-point */
789 tmp = (snr * ((1 << 24) / 10));
790
791 /* Convert from 8.24 fixed-point to
792 * scale the range 0 - 35*2^24 into 0 - 65535*/
793 if (tmp >= 8960 * 0x10000)
794 *signal_strength = 0xffff;
795 else
796 *signal_strength = tmp / 8960;
797 }
798
799 return ret;
Steven Toth265a6512008-04-18 21:34:00 -0300800}
801
Michael Krufkye059b0f2008-04-02 18:59:48 -0300802static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
Steven Toth265a6512008-04-18 21:34:00 -0300803{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300804 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300805
Michael Krufky8973dc42008-04-05 23:08:08 -0300806 if (state->current_modulation == VSB_8)
807 *ucblocks = au8522_readreg(state, 0x4087);
808 else
809 *ucblocks = au8522_readreg(state, 0x4543);
Steven Toth265a6512008-04-18 21:34:00 -0300810
811 return 0;
812}
813
Michael Krufkye059b0f2008-04-02 18:59:48 -0300814static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
Steven Toth265a6512008-04-18 21:34:00 -0300815{
816 return au8522_read_ucblocks(fe, ber);
817}
818
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -0200819static int au8522_get_frontend(struct dvb_frontend *fe,
820 struct dtv_frontend_properties *c)
Steven Toth265a6512008-04-18 21:34:00 -0300821{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300822 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300823
Mauro Carvalho Chehab9b6a1322011-12-22 09:53:20 -0300824 c->frequency = state->current_frequency;
825 c->modulation = state->current_modulation;
Steven Toth265a6512008-04-18 21:34:00 -0300826
827 return 0;
828}
829
Michael Krufkye059b0f2008-04-02 18:59:48 -0300830static int au8522_get_tune_settings(struct dvb_frontend *fe,
831 struct dvb_frontend_tune_settings *tune)
Steven Toth265a6512008-04-18 21:34:00 -0300832{
833 tune->min_delay_ms = 1000;
834 return 0;
835}
836
Max Kellermannbd336e62016-08-09 18:32:21 -0300837static const struct dvb_frontend_ops au8522_ops;
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300838
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300839
Michael Krufkye059b0f2008-04-02 18:59:48 -0300840static void au8522_release(struct dvb_frontend *fe)
Steven Toth265a6512008-04-18 21:34:00 -0300841{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300842 struct au8522_state *state = fe->demodulator_priv;
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300843 au8522_release_state(state);
Steven Toth265a6512008-04-18 21:34:00 -0300844}
845
Michael Krufkye059b0f2008-04-02 18:59:48 -0300846struct dvb_frontend *au8522_attach(const struct au8522_config *config,
847 struct i2c_adapter *i2c)
Steven Toth265a6512008-04-18 21:34:00 -0300848{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300849 struct au8522_state *state = NULL;
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300850 int instance;
Steven Toth265a6512008-04-18 21:34:00 -0300851
852 /* allocate memory for the internal state */
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300853 instance = au8522_get_state(&state, i2c, config->demod_address);
854 switch (instance) {
855 case 0:
856 dprintk("%s state allocation failed\n", __func__);
857 break;
858 case 1:
859 /* new demod instance */
860 dprintk("%s using new instance\n", __func__);
861 break;
862 default:
863 /* existing demod instance */
864 dprintk("%s using existing instance\n", __func__);
865 break;
866 }
Steven Toth265a6512008-04-18 21:34:00 -0300867
868 /* setup the state */
Mauro Carvalho Chehabaa377632015-12-21 14:38:31 -0200869 state->config = *config;
Steven Toth265a6512008-04-18 21:34:00 -0300870 state->i2c = i2c;
Devin Heitmueller7f2c9832010-02-17 22:47:55 -0300871 state->operational_mode = AU8522_DIGITAL_MODE;
872
Steven Toth265a6512008-04-18 21:34:00 -0300873 /* create dvb_frontend */
874 memcpy(&state->frontend.ops, &au8522_ops,
875 sizeof(struct dvb_frontend_ops));
876 state->frontend.demodulator_priv = state;
877
Devin Heitmueller4a03daf2012-08-06 22:47:02 -0300878 state->frontend.ops.analog_ops.i2c_gate_ctrl = au8522_analog_i2c_gate_ctrl;
879
Steven Toth265a6512008-04-18 21:34:00 -0300880 if (au8522_init(&state->frontend) != 0) {
881 printk(KERN_ERR "%s: Failed to initialize correctly\n",
Michael Krufkyce1719a2008-04-02 18:59:48 -0300882 __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300883 goto error;
884 }
885
886 /* Note: Leaving the I2C gate open here. */
887 au8522_i2c_gate_ctrl(&state->frontend, 1);
888
889 return &state->frontend;
890
891error:
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300892 au8522_release_state(state);
Steven Toth265a6512008-04-18 21:34:00 -0300893 return NULL;
894}
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300895EXPORT_SYMBOL(au8522_attach);
Steven Toth265a6512008-04-18 21:34:00 -0300896
Max Kellermannbd336e62016-08-09 18:32:21 -0300897static const struct dvb_frontend_ops au8522_ops = {
Mauro Carvalho Chehab9b6a1322011-12-22 09:53:20 -0300898 .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
Steven Toth265a6512008-04-18 21:34:00 -0300899 .info = {
900 .name = "Auvitek AU8522 QAM/8VSB Frontend",
Steven Toth265a6512008-04-18 21:34:00 -0300901 .frequency_min = 54000000,
902 .frequency_max = 858000000,
903 .frequency_stepsize = 62500,
904 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
905 },
906
907 .init = au8522_init,
Michael Krufky74d50722008-06-13 20:33:23 -0300908 .sleep = au8522_sleep,
Steven Toth265a6512008-04-18 21:34:00 -0300909 .i2c_gate_ctrl = au8522_i2c_gate_ctrl,
Mauro Carvalho Chehab9b6a1322011-12-22 09:53:20 -0300910 .set_frontend = au8522_set_frontend,
911 .get_frontend = au8522_get_frontend,
Steven Toth265a6512008-04-18 21:34:00 -0300912 .get_tune_settings = au8522_get_tune_settings,
913 .read_status = au8522_read_status,
914 .read_ber = au8522_read_ber,
915 .read_signal_strength = au8522_read_signal_strength,
916 .read_snr = au8522_read_snr,
917 .read_ucblocks = au8522_read_ucblocks,
918 .release = au8522_release,
919};
920
921module_param(debug, int, 0644);
922MODULE_PARM_DESC(debug, "Enable verbose debug messages");
923
Richard Vollkommerdcc33cc2014-10-25 17:17:23 -0300924module_param(zv_mode, int, 0644);
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300925MODULE_PARM_DESC(zv_mode, "Turn on/off ZeeVee modulator compatibility mode (default:on).\n"
Richard Vollkommerdcc33cc2014-10-25 17:17:23 -0300926 "\t\ton - modified AU8522 QAM256 initialization.\n"
927 "\t\tProvides faster lock when using ZeeVee modulator based sources");
928
Steven Toth265a6512008-04-18 21:34:00 -0300929MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
930MODULE_AUTHOR("Steven Toth");
931MODULE_LICENSE("GPL");