blob: 956b80f4979ca95af361162ec6adde484a1c9c89 [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"
Devin Heitmueller0c44bf32009-03-11 02:59:56 -030030#include "au8522_priv.h"
Steven Toth265a6512008-04-18 21:34:00 -030031
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -030032static int debug;
33
Devin Heitmueller209fdf62009-03-11 03:00:36 -030034/* Despite the name "hybrid_tuner", the framework works just as well for
35 hybrid demodulators as well... */
36static LIST_HEAD(hybrid_tuner_instance_list);
Devin Heitmueller4ff5ed42009-03-11 03:00:45 -030037static DEFINE_MUTEX(au8522_list_mutex);
Devin Heitmueller209fdf62009-03-11 03:00:36 -030038
Devin Heitmueller62899a22009-03-15 18:48:52 -030039#define dprintk(arg...)\
40 do { if (debug)\
41 printk(arg);\
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -030042 } while (0)
Steven Toth265a6512008-04-18 21:34:00 -030043
44/* 16 bit registers, 8 bit values */
Devin Heitmueller0c44bf32009-03-11 02:59:56 -030045int au8522_writereg(struct au8522_state *state, u16 reg, u8 data)
Steven Toth265a6512008-04-18 21:34:00 -030046{
47 int ret;
Devin Heitmueller62899a22009-03-15 18:48:52 -030048 u8 buf[] = { (reg >> 8) | 0x80, reg & 0xff, data };
Steven Toth265a6512008-04-18 21:34:00 -030049
50 struct i2c_msg msg = { .addr = state->config->demod_address,
51 .flags = 0, .buf = buf, .len = 3 };
52
53 ret = i2c_transfer(state->i2c, &msg, 1);
54
55 if (ret != 1)
56 printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
Michael Krufkyce1719a2008-04-02 18:59:48 -030057 "ret == %i)\n", __func__, reg, data, ret);
Steven Toth265a6512008-04-18 21:34:00 -030058
59 return (ret != 1) ? -1 : 0;
60}
61
Devin Heitmueller0c44bf32009-03-11 02:59:56 -030062u8 au8522_readreg(struct au8522_state *state, u16 reg)
Steven Toth265a6512008-04-18 21:34:00 -030063{
64 int ret;
Devin Heitmueller62899a22009-03-15 18:48:52 -030065 u8 b0[] = { (reg >> 8) | 0x40, reg & 0xff };
66 u8 b1[] = { 0 };
Steven Toth265a6512008-04-18 21:34:00 -030067
Devin Heitmueller62899a22009-03-15 18:48:52 -030068 struct i2c_msg msg[] = {
Steven Toth265a6512008-04-18 21:34:00 -030069 { .addr = state->config->demod_address, .flags = 0,
70 .buf = b0, .len = 2 },
71 { .addr = state->config->demod_address, .flags = I2C_M_RD,
72 .buf = b1, .len = 1 } };
73
74 ret = i2c_transfer(state->i2c, msg, 2);
75
76 if (ret != 2)
Michael Krufkyce1719a2008-04-02 18:59:48 -030077 printk(KERN_ERR "%s: readreg error (ret == %i)\n",
78 __func__, ret);
Steven Toth265a6512008-04-18 21:34:00 -030079 return b1[0];
80}
81
Michael Krufkye059b0f2008-04-02 18:59:48 -030082static int au8522_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
Steven Toth265a6512008-04-18 21:34:00 -030083{
Michael Krufkye059b0f2008-04-02 18:59:48 -030084 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -030085
Michael Krufkyce1719a2008-04-02 18:59:48 -030086 dprintk("%s(%d)\n", __func__, enable);
Steven Toth265a6512008-04-18 21:34:00 -030087
88 if (enable)
89 return au8522_writereg(state, 0x106, 1);
90 else
91 return au8522_writereg(state, 0x106, 0);
92}
93
Michael Krufky0daa5de2008-04-10 04:24:56 -030094struct mse2snr_tab {
Steven Tothf01699b2008-04-10 02:01:48 -030095 u16 val;
96 u16 data;
Michael Krufky0daa5de2008-04-10 04:24:56 -030097};
98
99/* VSB SNR lookup table */
100static struct mse2snr_tab vsb_mse2snr_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -0300101 { 0, 270 },
102 { 2, 250 },
103 { 3, 240 },
104 { 5, 230 },
105 { 7, 220 },
106 { 9, 210 },
107 { 12, 200 },
108 { 13, 195 },
109 { 15, 190 },
110 { 17, 185 },
111 { 19, 180 },
112 { 21, 175 },
113 { 24, 170 },
114 { 27, 165 },
115 { 31, 160 },
116 { 32, 158 },
117 { 33, 156 },
118 { 36, 152 },
119 { 37, 150 },
120 { 39, 148 },
121 { 40, 146 },
122 { 41, 144 },
123 { 43, 142 },
124 { 44, 140 },
125 { 48, 135 },
126 { 50, 130 },
127 { 43, 142 },
128 { 53, 125 },
129 { 56, 120 },
130 { 256, 115 },
131};
132
133/* QAM64 SNR lookup table */
Michael Krufky0daa5de2008-04-10 04:24:56 -0300134static struct mse2snr_tab qam64_mse2snr_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -0300135 { 15, 0 },
136 { 16, 290 },
137 { 17, 288 },
138 { 18, 286 },
139 { 19, 284 },
140 { 20, 282 },
141 { 21, 281 },
142 { 22, 279 },
143 { 23, 277 },
144 { 24, 275 },
145 { 25, 273 },
146 { 26, 271 },
147 { 27, 269 },
148 { 28, 268 },
149 { 29, 266 },
150 { 30, 264 },
151 { 31, 262 },
152 { 32, 260 },
153 { 33, 259 },
154 { 34, 258 },
155 { 35, 256 },
156 { 36, 255 },
157 { 37, 254 },
158 { 38, 252 },
159 { 39, 251 },
160 { 40, 250 },
161 { 41, 249 },
162 { 42, 248 },
163 { 43, 246 },
164 { 44, 245 },
165 { 45, 244 },
166 { 46, 242 },
167 { 47, 241 },
168 { 48, 240 },
169 { 50, 239 },
170 { 51, 238 },
171 { 53, 237 },
172 { 54, 236 },
173 { 56, 235 },
174 { 57, 234 },
175 { 59, 233 },
176 { 60, 232 },
177 { 62, 231 },
178 { 63, 230 },
179 { 65, 229 },
180 { 67, 228 },
181 { 68, 227 },
182 { 70, 226 },
183 { 71, 225 },
184 { 73, 224 },
185 { 74, 223 },
186 { 76, 222 },
187 { 78, 221 },
188 { 80, 220 },
189 { 82, 219 },
190 { 85, 218 },
191 { 88, 217 },
192 { 90, 216 },
193 { 92, 215 },
194 { 93, 214 },
195 { 94, 212 },
196 { 95, 211 },
197 { 97, 210 },
198 { 99, 209 },
199 { 101, 208 },
200 { 102, 207 },
201 { 104, 206 },
202 { 107, 205 },
203 { 111, 204 },
204 { 114, 203 },
205 { 118, 202 },
206 { 122, 201 },
207 { 125, 200 },
208 { 128, 199 },
209 { 130, 198 },
210 { 132, 197 },
211 { 256, 190 },
212};
213
214/* QAM256 SNR lookup table */
Michael Krufky0daa5de2008-04-10 04:24:56 -0300215static struct mse2snr_tab qam256_mse2snr_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -0300216 { 16, 0 },
217 { 17, 400 },
218 { 18, 398 },
219 { 19, 396 },
220 { 20, 394 },
221 { 21, 392 },
222 { 22, 390 },
223 { 23, 388 },
224 { 24, 386 },
225 { 25, 384 },
226 { 26, 382 },
227 { 27, 380 },
228 { 28, 379 },
229 { 29, 378 },
230 { 30, 377 },
231 { 31, 376 },
232 { 32, 375 },
233 { 33, 374 },
234 { 34, 373 },
235 { 35, 372 },
236 { 36, 371 },
237 { 37, 370 },
238 { 38, 362 },
239 { 39, 354 },
240 { 40, 346 },
241 { 41, 338 },
242 { 42, 330 },
243 { 43, 328 },
244 { 44, 326 },
245 { 45, 324 },
246 { 46, 322 },
247 { 47, 320 },
248 { 48, 319 },
249 { 49, 318 },
250 { 50, 317 },
251 { 51, 316 },
252 { 52, 315 },
253 { 53, 314 },
254 { 54, 313 },
255 { 55, 312 },
256 { 56, 311 },
257 { 57, 310 },
258 { 58, 308 },
259 { 59, 306 },
260 { 60, 304 },
261 { 61, 302 },
262 { 62, 300 },
263 { 63, 298 },
264 { 65, 295 },
265 { 68, 294 },
266 { 70, 293 },
267 { 73, 292 },
268 { 76, 291 },
269 { 78, 290 },
270 { 79, 289 },
271 { 81, 288 },
272 { 82, 287 },
273 { 83, 286 },
274 { 84, 285 },
275 { 85, 284 },
276 { 86, 283 },
277 { 88, 282 },
278 { 89, 281 },
279 { 256, 280 },
280};
281
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300282static int au8522_mse2snr_lookup(struct mse2snr_tab *tab, int sz, int mse,
283 u16 *snr)
Steven Tothf01699b2008-04-10 02:01:48 -0300284{
285 int i, ret = -EINVAL;
286 dprintk("%s()\n", __func__);
287
Michael Krufky0daa5de2008-04-10 04:24:56 -0300288 for (i = 0; i < sz; i++) {
289 if (mse < tab[i].val) {
290 *snr = tab[i].data;
Steven Tothf01699b2008-04-10 02:01:48 -0300291 ret = 0;
292 break;
293 }
294 }
295 dprintk("%s() snr=%d\n", __func__, *snr);
296 return ret;
297}
298
Michael Krufky2e7acd72008-09-03 16:46:35 -0300299static int au8522_set_if(struct dvb_frontend *fe, enum au8522_if_freq if_freq)
300{
301 struct au8522_state *state = fe->demodulator_priv;
Michael Krufkydf76de02008-09-03 16:46:40 -0300302 u8 r0b5, r0b6, r0b7;
303 char *ifmhz;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300304
305 switch (if_freq) {
306 case AU8522_IF_3_25MHZ:
Michael Krufkydf76de02008-09-03 16:46:40 -0300307 ifmhz = "3.25";
308 r0b5 = 0x00;
309 r0b6 = 0x3d;
310 r0b7 = 0xa0;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300311 break;
312 case AU8522_IF_4MHZ:
Michael Krufkydf76de02008-09-03 16:46:40 -0300313 ifmhz = "4.00";
314 r0b5 = 0x00;
315 r0b6 = 0x4b;
316 r0b7 = 0xd9;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300317 break;
318 case AU8522_IF_6MHZ:
Michael Krufkydf76de02008-09-03 16:46:40 -0300319 ifmhz = "6.00";
320 r0b5 = 0xfb;
321 r0b6 = 0x8e;
322 r0b7 = 0x39;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300323 break;
324 default:
325 dprintk("%s() IF Frequency not supported\n", __func__);
326 return -EINVAL;
327 }
Michael Krufkydf76de02008-09-03 16:46:40 -0300328 dprintk("%s() %s MHz\n", __func__, ifmhz);
329 au8522_writereg(state, 0x80b5, r0b5);
330 au8522_writereg(state, 0x80b6, r0b6);
331 au8522_writereg(state, 0x80b7, r0b7);
332
Michael Krufky2e7acd72008-09-03 16:46:35 -0300333 return 0;
334}
335
Steven Tothf01699b2008-04-10 02:01:48 -0300336/* VSB Modulation table */
337static struct {
338 u16 reg;
339 u16 data;
340} VSB_mod_tab[] = {
341 { 0x8090, 0x84 },
342 { 0x4092, 0x11 },
343 { 0x2005, 0x00 },
344 { 0x8091, 0x80 },
345 { 0x80a3, 0x0c },
346 { 0x80a4, 0xe8 },
347 { 0x8081, 0xc4 },
348 { 0x80a5, 0x40 },
349 { 0x80a7, 0x40 },
350 { 0x80a6, 0x67 },
351 { 0x8262, 0x20 },
352 { 0x821c, 0x30 },
353 { 0x80d8, 0x1a },
354 { 0x8227, 0xa0 },
355 { 0x8121, 0xff },
356 { 0x80a8, 0xf0 },
357 { 0x80a9, 0x05 },
358 { 0x80aa, 0x77 },
359 { 0x80ab, 0xf0 },
360 { 0x80ac, 0x05 },
361 { 0x80ad, 0x77 },
362 { 0x80ae, 0x41 },
363 { 0x80af, 0x66 },
364 { 0x821b, 0xcc },
365 { 0x821d, 0x80 },
Steven Tothf01699b2008-04-10 02:01:48 -0300366 { 0x80a4, 0xe8 },
367 { 0x8231, 0x13 },
368};
369
Frank Dischner040d4cb2009-06-14 23:05:20 -0300370/* QAM64 Modulation table */
Steven Tothf01699b2008-04-10 02:01:48 -0300371static struct {
372 u16 reg;
373 u16 data;
Frank Dischner040d4cb2009-06-14 23:05:20 -0300374} QAM64_mod_tab[] = {
375 { 0x00a3, 0x09 },
376 { 0x00a4, 0x00 },
377 { 0x0081, 0xc4 },
378 { 0x00a5, 0x40 },
379 { 0x00aa, 0x77 },
380 { 0x00ad, 0x77 },
381 { 0x00a6, 0x67 },
382 { 0x0262, 0x20 },
383 { 0x021c, 0x30 },
384 { 0x00b8, 0x3e },
385 { 0x00b9, 0xf0 },
386 { 0x00ba, 0x01 },
387 { 0x00bb, 0x18 },
388 { 0x00bc, 0x50 },
389 { 0x00bd, 0x00 },
390 { 0x00be, 0xea },
391 { 0x00bf, 0xef },
392 { 0x00c0, 0xfc },
393 { 0x00c1, 0xbd },
394 { 0x00c2, 0x1f },
395 { 0x00c3, 0xfc },
396 { 0x00c4, 0xdd },
397 { 0x00c5, 0xaf },
398 { 0x00c6, 0x00 },
399 { 0x00c7, 0x38 },
400 { 0x00c8, 0x30 },
401 { 0x00c9, 0x05 },
402 { 0x00ca, 0x4a },
403 { 0x00cb, 0xd0 },
404 { 0x00cc, 0x01 },
405 { 0x00cd, 0xd9 },
406 { 0x00ce, 0x6f },
407 { 0x00cf, 0xf9 },
408 { 0x00d0, 0x70 },
409 { 0x00d1, 0xdf },
410 { 0x00d2, 0xf7 },
411 { 0x00d3, 0xc2 },
412 { 0x00d4, 0xdf },
413 { 0x00d5, 0x02 },
414 { 0x00d6, 0x9a },
415 { 0x00d7, 0xd0 },
416 { 0x0250, 0x0d },
417 { 0x0251, 0xcd },
418 { 0x0252, 0xe0 },
419 { 0x0253, 0x05 },
420 { 0x0254, 0xa7 },
421 { 0x0255, 0xff },
422 { 0x0256, 0xed },
423 { 0x0257, 0x5b },
424 { 0x0258, 0xae },
425 { 0x0259, 0xe6 },
426 { 0x025a, 0x3d },
427 { 0x025b, 0x0f },
428 { 0x025c, 0x0d },
429 { 0x025d, 0xea },
430 { 0x025e, 0xf2 },
431 { 0x025f, 0x51 },
432 { 0x0260, 0xf5 },
433 { 0x0261, 0x06 },
434 { 0x021a, 0x00 },
435 { 0x0546, 0x40 },
436 { 0x0210, 0xc7 },
437 { 0x0211, 0xaa },
438 { 0x0212, 0xab },
439 { 0x0213, 0x02 },
440 { 0x0502, 0x00 },
441 { 0x0121, 0x04 },
442 { 0x0122, 0x04 },
443 { 0x052e, 0x10 },
444 { 0x00a4, 0xca },
445 { 0x00a7, 0x40 },
446 { 0x0526, 0x01 },
447};
448
449/* QAM256 Modulation table */
450static struct {
451 u16 reg;
452 u16 data;
453} QAM256_mod_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -0300454 { 0x80a3, 0x09 },
455 { 0x80a4, 0x00 },
456 { 0x8081, 0xc4 },
457 { 0x80a5, 0x40 },
Steven Tothf01699b2008-04-10 02:01:48 -0300458 { 0x80aa, 0x77 },
459 { 0x80ad, 0x77 },
460 { 0x80a6, 0x67 },
461 { 0x8262, 0x20 },
462 { 0x821c, 0x30 },
463 { 0x80b8, 0x3e },
464 { 0x80b9, 0xf0 },
465 { 0x80ba, 0x01 },
466 { 0x80bb, 0x18 },
467 { 0x80bc, 0x50 },
468 { 0x80bd, 0x00 },
469 { 0x80be, 0xea },
470 { 0x80bf, 0xef },
471 { 0x80c0, 0xfc },
472 { 0x80c1, 0xbd },
473 { 0x80c2, 0x1f },
474 { 0x80c3, 0xfc },
475 { 0x80c4, 0xdd },
476 { 0x80c5, 0xaf },
477 { 0x80c6, 0x00 },
478 { 0x80c7, 0x38 },
479 { 0x80c8, 0x30 },
480 { 0x80c9, 0x05 },
481 { 0x80ca, 0x4a },
482 { 0x80cb, 0xd0 },
483 { 0x80cc, 0x01 },
484 { 0x80cd, 0xd9 },
485 { 0x80ce, 0x6f },
486 { 0x80cf, 0xf9 },
487 { 0x80d0, 0x70 },
488 { 0x80d1, 0xdf },
489 { 0x80d2, 0xf7 },
490 { 0x80d3, 0xc2 },
491 { 0x80d4, 0xdf },
492 { 0x80d5, 0x02 },
493 { 0x80d6, 0x9a },
494 { 0x80d7, 0xd0 },
495 { 0x8250, 0x0d },
496 { 0x8251, 0xcd },
497 { 0x8252, 0xe0 },
498 { 0x8253, 0x05 },
499 { 0x8254, 0xa7 },
500 { 0x8255, 0xff },
501 { 0x8256, 0xed },
502 { 0x8257, 0x5b },
503 { 0x8258, 0xae },
504 { 0x8259, 0xe6 },
505 { 0x825a, 0x3d },
506 { 0x825b, 0x0f },
507 { 0x825c, 0x0d },
508 { 0x825d, 0xea },
509 { 0x825e, 0xf2 },
510 { 0x825f, 0x51 },
511 { 0x8260, 0xf5 },
512 { 0x8261, 0x06 },
513 { 0x821a, 0x00 },
514 { 0x8546, 0x40 },
515 { 0x8210, 0x26 },
516 { 0x8211, 0xf6 },
517 { 0x8212, 0x84 },
518 { 0x8213, 0x02 },
519 { 0x8502, 0x01 },
520 { 0x8121, 0x04 },
521 { 0x8122, 0x04 },
522 { 0x852e, 0x10 },
523 { 0x80a4, 0xca },
524 { 0x80a7, 0x40 },
525 { 0x8526, 0x01 },
526};
527
Michael Krufkye059b0f2008-04-02 18:59:48 -0300528static int au8522_enable_modulation(struct dvb_frontend *fe,
529 fe_modulation_t m)
Steven Toth265a6512008-04-18 21:34:00 -0300530{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300531 struct au8522_state *state = fe->demodulator_priv;
Steven Tothf01699b2008-04-10 02:01:48 -0300532 int i;
Steven Toth265a6512008-04-18 21:34:00 -0300533
Michael Krufkyce1719a2008-04-02 18:59:48 -0300534 dprintk("%s(0x%08x)\n", __func__, m);
Steven Toth265a6512008-04-18 21:34:00 -0300535
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300536 switch (m) {
Steven Toth265a6512008-04-18 21:34:00 -0300537 case VSB_8:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300538 dprintk("%s() VSB_8\n", __func__);
Steven Tothf01699b2008-04-10 02:01:48 -0300539 for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
540 au8522_writereg(state,
541 VSB_mod_tab[i].reg,
542 VSB_mod_tab[i].data);
Michael Krufky2e7acd72008-09-03 16:46:35 -0300543 au8522_set_if(fe, state->config->vsb_if);
Steven Toth265a6512008-04-18 21:34:00 -0300544 break;
545 case QAM_64:
Frank Dischner040d4cb2009-06-14 23:05:20 -0300546 dprintk("%s() QAM 64\n", __func__);
547 for (i = 0; i < ARRAY_SIZE(QAM64_mod_tab); i++)
Steven Tothf01699b2008-04-10 02:01:48 -0300548 au8522_writereg(state,
Frank Dischner040d4cb2009-06-14 23:05:20 -0300549 QAM64_mod_tab[i].reg,
550 QAM64_mod_tab[i].data);
551 au8522_set_if(fe, state->config->qam_if);
552 break;
553 case QAM_256:
554 dprintk("%s() QAM 256\n", __func__);
555 for (i = 0; i < ARRAY_SIZE(QAM256_mod_tab); i++)
556 au8522_writereg(state,
557 QAM256_mod_tab[i].reg,
558 QAM256_mod_tab[i].data);
Michael Krufky2e7acd72008-09-03 16:46:35 -0300559 au8522_set_if(fe, state->config->qam_if);
Steven Toth265a6512008-04-18 21:34:00 -0300560 break;
561 default:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300562 dprintk("%s() Invalid modulation\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300563 return -EINVAL;
564 }
565
566 state->current_modulation = m;
567
568 return 0;
569}
570
571/* Talk to the demod, set the FEC, GUARD, QAM settings etc */
Michael Krufkye059b0f2008-04-02 18:59:48 -0300572static int au8522_set_frontend(struct dvb_frontend *fe,
573 struct dvb_frontend_parameters *p)
Steven Toth265a6512008-04-18 21:34:00 -0300574{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300575 struct au8522_state *state = fe->demodulator_priv;
Michael Krufky74d50722008-06-13 20:33:23 -0300576 int ret = -EINVAL;
Steven Toth265a6512008-04-18 21:34:00 -0300577
Michael Krufkyce1719a2008-04-02 18:59:48 -0300578 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
Steven Toth265a6512008-04-18 21:34:00 -0300579
Michael Krufky74d50722008-06-13 20:33:23 -0300580 if ((state->current_frequency == p->frequency) &&
581 (state->current_modulation == p->u.vsb.modulation))
582 return 0;
Steven Toth265a6512008-04-18 21:34:00 -0300583
584 au8522_enable_modulation(fe, p->u.vsb.modulation);
585
586 /* Allow the demod to settle */
587 msleep(100);
588
589 if (fe->ops.tuner_ops.set_params) {
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300590 if (fe->ops.i2c_gate_ctrl)
591 fe->ops.i2c_gate_ctrl(fe, 1);
Michael Krufky74d50722008-06-13 20:33:23 -0300592 ret = fe->ops.tuner_ops.set_params(fe, p);
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300593 if (fe->ops.i2c_gate_ctrl)
594 fe->ops.i2c_gate_ctrl(fe, 0);
Steven Toth265a6512008-04-18 21:34:00 -0300595 }
596
Michael Krufky74d50722008-06-13 20:33:23 -0300597 if (ret < 0)
598 return ret;
599
600 state->current_frequency = p->frequency;
601
Steven Toth265a6512008-04-18 21:34:00 -0300602 return 0;
603}
604
605/* Reset the demod hardware and reset all of the configuration registers
606 to a default state. */
Devin Heitmueller0c44bf32009-03-11 02:59:56 -0300607int au8522_init(struct dvb_frontend *fe)
Steven Toth265a6512008-04-18 21:34:00 -0300608{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300609 struct au8522_state *state = fe->demodulator_priv;
Michael Krufkyce1719a2008-04-02 18:59:48 -0300610 dprintk("%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300611
612 au8522_writereg(state, 0xa4, 1 << 5);
613
614 au8522_i2c_gate_ctrl(fe, 1);
615
616 return 0;
617}
618
Michael Krufkyadeeac32008-04-05 20:55:14 -0300619static int au8522_led_gpio_enable(struct au8522_state *state, int onoff)
620{
621 struct au8522_led_config *led_config = state->config->led_cfg;
622 u8 val;
623
624 /* bail out if we cant control an LED */
625 if (!led_config || !led_config->gpio_output ||
626 !led_config->gpio_output_enable || !led_config->gpio_output_disable)
627 return 0;
628
629 val = au8522_readreg(state, 0x4000 |
630 (led_config->gpio_output & ~0xc000));
631 if (onoff) {
632 /* enable GPIO output */
633 val &= ~((led_config->gpio_output_enable >> 8) & 0xff);
634 val |= (led_config->gpio_output_enable & 0xff);
635 } else {
636 /* disable GPIO output */
637 val &= ~((led_config->gpio_output_disable >> 8) & 0xff);
638 val |= (led_config->gpio_output_disable & 0xff);
639 }
640 return au8522_writereg(state, 0x8000 |
641 (led_config->gpio_output & ~0xc000), val);
642}
643
644/* led = 0 | off
645 * led = 1 | signal ok
646 * led = 2 | signal strong
647 * led < 0 | only light led if leds are currently off
648 */
649static int au8522_led_ctrl(struct au8522_state *state, int led)
650{
651 struct au8522_led_config *led_config = state->config->led_cfg;
652 int i, ret = 0;
653
654 /* bail out if we cant control an LED */
655 if (!led_config || !led_config->gpio_leds ||
656 !led_config->num_led_states || !led_config->led_states)
657 return 0;
658
659 if (led < 0) {
660 /* if LED is already lit, then leave it as-is */
661 if (state->led_state)
662 return 0;
663 else
664 led *= -1;
665 }
666
667 /* toggle LED if changing state */
668 if (state->led_state != led) {
669 u8 val;
670
671 dprintk("%s: %d\n", __func__, led);
672
673 au8522_led_gpio_enable(state, 1);
674
675 val = au8522_readreg(state, 0x4000 |
676 (led_config->gpio_leds & ~0xc000));
677
678 /* start with all leds off */
679 for (i = 0; i < led_config->num_led_states; i++)
680 val &= ~led_config->led_states[i];
681
682 /* set selected LED state */
683 if (led < led_config->num_led_states)
684 val |= led_config->led_states[led];
685 else if (led_config->num_led_states)
686 val |=
687 led_config->led_states[led_config->num_led_states - 1];
688
689 ret = au8522_writereg(state, 0x8000 |
690 (led_config->gpio_leds & ~0xc000), val);
691 if (ret < 0)
692 return ret;
693
694 state->led_state = led;
695
696 if (led == 0)
697 au8522_led_gpio_enable(state, 0);
698 }
699
700 return 0;
701}
702
Devin Heitmueller0c44bf32009-03-11 02:59:56 -0300703int au8522_sleep(struct dvb_frontend *fe)
Michael Krufky74d50722008-06-13 20:33:23 -0300704{
705 struct au8522_state *state = fe->demodulator_priv;
706 dprintk("%s()\n", __func__);
707
Michael Krufkyadeeac32008-04-05 20:55:14 -0300708 /* turn off led */
709 au8522_led_ctrl(state, 0);
710
Devin Heitmueller7bf63ed2009-03-11 03:00:34 -0300711 /* Power down the chip */
712 au8522_writereg(state, 0xa4, 1 << 5);
713
Michael Krufky74d50722008-06-13 20:33:23 -0300714 state->current_frequency = 0;
715
716 return 0;
717}
718
Michael Krufkye059b0f2008-04-02 18:59:48 -0300719static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status)
Steven Toth265a6512008-04-18 21:34:00 -0300720{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300721 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300722 u8 reg;
723 u32 tuner_status = 0;
724
725 *status = 0;
726
727 if (state->current_modulation == VSB_8) {
Michael Krufkyce1719a2008-04-02 18:59:48 -0300728 dprintk("%s() Checking VSB_8\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300729 reg = au8522_readreg(state, 0x4088);
Steven Toth836c2852008-06-21 19:32:41 -0300730 if ((reg & 0x03) == 0x03)
731 *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
Steven Toth265a6512008-04-18 21:34:00 -0300732 } else {
Michael Krufkyce1719a2008-04-02 18:59:48 -0300733 dprintk("%s() Checking QAM\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300734 reg = au8522_readreg(state, 0x4541);
Michael Krufkye059b0f2008-04-02 18:59:48 -0300735 if (reg & 0x80)
Steven Toth265a6512008-04-18 21:34:00 -0300736 *status |= FE_HAS_VITERBI;
Michael Krufkye059b0f2008-04-02 18:59:48 -0300737 if (reg & 0x20)
Steven Toth265a6512008-04-18 21:34:00 -0300738 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
739 }
740
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300741 switch (state->config->status_mode) {
Steven Toth265a6512008-04-18 21:34:00 -0300742 case AU8522_DEMODLOCKING:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300743 dprintk("%s() DEMODLOCKING\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300744 if (*status & FE_HAS_VITERBI)
745 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
746 break;
747 case AU8522_TUNERLOCKING:
748 /* Get the tuner status */
Michael Krufkyce1719a2008-04-02 18:59:48 -0300749 dprintk("%s() TUNERLOCKING\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300750 if (fe->ops.tuner_ops.get_status) {
751 if (fe->ops.i2c_gate_ctrl)
752 fe->ops.i2c_gate_ctrl(fe, 1);
753
754 fe->ops.tuner_ops.get_status(fe, &tuner_status);
755
756 if (fe->ops.i2c_gate_ctrl)
757 fe->ops.i2c_gate_ctrl(fe, 0);
758 }
759 if (tuner_status)
760 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
761 break;
762 }
Michael Krufkyadeeac32008-04-05 20:55:14 -0300763 state->fe_status = *status;
764
765 if (*status & FE_HAS_LOCK)
766 /* turn on LED, if it isn't on already */
767 au8522_led_ctrl(state, -1);
768 else
769 /* turn off LED */
770 au8522_led_ctrl(state, 0);
Steven Toth265a6512008-04-18 21:34:00 -0300771
Michael Krufkyce1719a2008-04-02 18:59:48 -0300772 dprintk("%s() status 0x%08x\n", __func__, *status);
Steven Toth265a6512008-04-18 21:34:00 -0300773
774 return 0;
775}
776
Michael Krufkyadeeac32008-04-05 20:55:14 -0300777static int au8522_led_status(struct au8522_state *state, const u16 *snr)
778{
779 struct au8522_led_config *led_config = state->config->led_cfg;
780 int led;
781 u16 strong;
782
783 /* bail out if we cant control an LED */
784 if (!led_config)
785 return 0;
786
787 if (0 == (state->fe_status & FE_HAS_LOCK))
788 return au8522_led_ctrl(state, 0);
789 else if (state->current_modulation == QAM_256)
790 strong = led_config->qam256_strong;
791 else if (state->current_modulation == QAM_64)
792 strong = led_config->qam64_strong;
793 else /* (state->current_modulation == VSB_8) */
794 strong = led_config->vsb8_strong;
795
796 if (*snr >= strong)
797 led = 2;
798 else
799 led = 1;
800
801 if ((state->led_state) &&
802 (((strong < *snr) ? (*snr - strong) : (strong - *snr)) <= 10))
803 /* snr didn't change enough to bother
804 * changing the color of the led */
805 return 0;
806
807 return au8522_led_ctrl(state, led);
808}
809
Michael Krufkye059b0f2008-04-02 18:59:48 -0300810static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
Steven Toth265a6512008-04-18 21:34:00 -0300811{
Steven Tothf01699b2008-04-10 02:01:48 -0300812 struct au8522_state *state = fe->demodulator_priv;
813 int ret = -EINVAL;
814
Michael Krufkyce1719a2008-04-02 18:59:48 -0300815 dprintk("%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300816
Steven Tothf01699b2008-04-10 02:01:48 -0300817 if (state->current_modulation == QAM_256)
Michael Krufky0daa5de2008-04-10 04:24:56 -0300818 ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
819 ARRAY_SIZE(qam256_mse2snr_tab),
820 au8522_readreg(state, 0x4522),
821 snr);
Steven Tothf01699b2008-04-10 02:01:48 -0300822 else if (state->current_modulation == QAM_64)
Michael Krufky0daa5de2008-04-10 04:24:56 -0300823 ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
824 ARRAY_SIZE(qam64_mse2snr_tab),
825 au8522_readreg(state, 0x4522),
826 snr);
Steven Tothf01699b2008-04-10 02:01:48 -0300827 else /* VSB_8 */
Michael Krufky0daa5de2008-04-10 04:24:56 -0300828 ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
829 ARRAY_SIZE(vsb_mse2snr_tab),
830 au8522_readreg(state, 0x4311),
831 snr);
Steven Toth265a6512008-04-18 21:34:00 -0300832
Michael Krufkyadeeac32008-04-05 20:55:14 -0300833 if (state->config->led_cfg)
834 au8522_led_status(state, snr);
835
Steven Tothf01699b2008-04-10 02:01:48 -0300836 return ret;
Steven Toth265a6512008-04-18 21:34:00 -0300837}
838
Michael Krufkye059b0f2008-04-02 18:59:48 -0300839static int au8522_read_signal_strength(struct dvb_frontend *fe,
840 u16 *signal_strength)
Steven Toth265a6512008-04-18 21:34:00 -0300841{
842 return au8522_read_snr(fe, signal_strength);
843}
844
Michael Krufkye059b0f2008-04-02 18:59:48 -0300845static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
Steven Toth265a6512008-04-18 21:34:00 -0300846{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300847 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300848
Michael Krufky8973dc42008-04-05 23:08:08 -0300849 if (state->current_modulation == VSB_8)
850 *ucblocks = au8522_readreg(state, 0x4087);
851 else
852 *ucblocks = au8522_readreg(state, 0x4543);
Steven Toth265a6512008-04-18 21:34:00 -0300853
854 return 0;
855}
856
Michael Krufkye059b0f2008-04-02 18:59:48 -0300857static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
Steven Toth265a6512008-04-18 21:34:00 -0300858{
859 return au8522_read_ucblocks(fe, ber);
860}
861
Michael Krufkye059b0f2008-04-02 18:59:48 -0300862static int au8522_get_frontend(struct dvb_frontend *fe,
Steven Toth265a6512008-04-18 21:34:00 -0300863 struct dvb_frontend_parameters *p)
864{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300865 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300866
867 p->frequency = state->current_frequency;
868 p->u.vsb.modulation = state->current_modulation;
869
870 return 0;
871}
872
Michael Krufkye059b0f2008-04-02 18:59:48 -0300873static int au8522_get_tune_settings(struct dvb_frontend *fe,
874 struct dvb_frontend_tune_settings *tune)
Steven Toth265a6512008-04-18 21:34:00 -0300875{
876 tune->min_delay_ms = 1000;
877 return 0;
878}
879
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300880static struct dvb_frontend_ops au8522_ops;
881
882int au8522_get_state(struct au8522_state **state, struct i2c_adapter *i2c,
883 u8 client_address)
884{
Devin Heitmueller4ff5ed42009-03-11 03:00:45 -0300885 int ret;
886
887 mutex_lock(&au8522_list_mutex);
888 ret = hybrid_tuner_request_state(struct au8522_state, (*state),
889 hybrid_tuner_instance_list,
890 i2c, client_address, "au8522");
891 mutex_unlock(&au8522_list_mutex);
892
893 return ret;
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300894}
895
896void au8522_release_state(struct au8522_state *state)
897{
Devin Heitmueller4ff5ed42009-03-11 03:00:45 -0300898 mutex_lock(&au8522_list_mutex);
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300899 if (state != NULL)
900 hybrid_tuner_release_state(state);
Devin Heitmueller4ff5ed42009-03-11 03:00:45 -0300901 mutex_unlock(&au8522_list_mutex);
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300902}
903
904
Michael Krufkye059b0f2008-04-02 18:59:48 -0300905static void au8522_release(struct dvb_frontend *fe)
Steven Toth265a6512008-04-18 21:34:00 -0300906{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300907 struct au8522_state *state = fe->demodulator_priv;
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300908 au8522_release_state(state);
Steven Toth265a6512008-04-18 21:34:00 -0300909}
910
Michael Krufkye059b0f2008-04-02 18:59:48 -0300911struct dvb_frontend *au8522_attach(const struct au8522_config *config,
912 struct i2c_adapter *i2c)
Steven Toth265a6512008-04-18 21:34:00 -0300913{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300914 struct au8522_state *state = NULL;
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300915 int instance;
Steven Toth265a6512008-04-18 21:34:00 -0300916
917 /* allocate memory for the internal state */
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300918 instance = au8522_get_state(&state, i2c, config->demod_address);
919 switch (instance) {
920 case 0:
921 dprintk("%s state allocation failed\n", __func__);
922 break;
923 case 1:
924 /* new demod instance */
925 dprintk("%s using new instance\n", __func__);
926 break;
927 default:
928 /* existing demod instance */
929 dprintk("%s using existing instance\n", __func__);
930 break;
931 }
Steven Toth265a6512008-04-18 21:34:00 -0300932
933 /* setup the state */
934 state->config = config;
935 state->i2c = i2c;
936 /* create dvb_frontend */
937 memcpy(&state->frontend.ops, &au8522_ops,
938 sizeof(struct dvb_frontend_ops));
939 state->frontend.demodulator_priv = state;
940
941 if (au8522_init(&state->frontend) != 0) {
942 printk(KERN_ERR "%s: Failed to initialize correctly\n",
Michael Krufkyce1719a2008-04-02 18:59:48 -0300943 __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300944 goto error;
945 }
946
947 /* Note: Leaving the I2C gate open here. */
948 au8522_i2c_gate_ctrl(&state->frontend, 1);
949
950 return &state->frontend;
951
952error:
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300953 au8522_release_state(state);
Steven Toth265a6512008-04-18 21:34:00 -0300954 return NULL;
955}
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300956EXPORT_SYMBOL(au8522_attach);
Steven Toth265a6512008-04-18 21:34:00 -0300957
958static struct dvb_frontend_ops au8522_ops = {
959
960 .info = {
961 .name = "Auvitek AU8522 QAM/8VSB Frontend",
962 .type = FE_ATSC,
963 .frequency_min = 54000000,
964 .frequency_max = 858000000,
965 .frequency_stepsize = 62500,
966 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
967 },
968
969 .init = au8522_init,
Michael Krufky74d50722008-06-13 20:33:23 -0300970 .sleep = au8522_sleep,
Steven Toth265a6512008-04-18 21:34:00 -0300971 .i2c_gate_ctrl = au8522_i2c_gate_ctrl,
972 .set_frontend = au8522_set_frontend,
973 .get_frontend = au8522_get_frontend,
974 .get_tune_settings = au8522_get_tune_settings,
975 .read_status = au8522_read_status,
976 .read_ber = au8522_read_ber,
977 .read_signal_strength = au8522_read_signal_strength,
978 .read_snr = au8522_read_snr,
979 .read_ucblocks = au8522_read_ucblocks,
980 .release = au8522_release,
981};
982
983module_param(debug, int, 0644);
984MODULE_PARM_DESC(debug, "Enable verbose debug messages");
985
986MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
987MODULE_AUTHOR("Steven Toth");
988MODULE_LICENSE("GPL");