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