blob: fa3ecbed1e586278254a19555407c62a2c11eaeb [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
34#define dprintk(arg...) do { \
35 if (debug) \
Mauro Carvalho Chehab353a2762008-04-18 22:24:01 -030036 printk(arg); \
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -030037 } while (0)
Steven Toth265a6512008-04-18 21:34:00 -030038
39/* 16 bit registers, 8 bit values */
Devin Heitmueller0c44bf32009-03-11 02:59:56 -030040int au8522_writereg(struct au8522_state *state, u16 reg, u8 data)
Steven Toth265a6512008-04-18 21:34:00 -030041{
42 int ret;
Devin Heitmueller083a1732009-03-11 03:00:19 -030043 u8 buf [] = { (reg >> 8) | 0x80, reg & 0xff, data };
Steven Toth265a6512008-04-18 21:34:00 -030044
45 struct i2c_msg msg = { .addr = state->config->demod_address,
46 .flags = 0, .buf = buf, .len = 3 };
47
48 ret = i2c_transfer(state->i2c, &msg, 1);
49
50 if (ret != 1)
51 printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
Michael Krufkyce1719a2008-04-02 18:59:48 -030052 "ret == %i)\n", __func__, reg, data, ret);
Steven Toth265a6512008-04-18 21:34:00 -030053
54 return (ret != 1) ? -1 : 0;
55}
56
Devin Heitmueller0c44bf32009-03-11 02:59:56 -030057u8 au8522_readreg(struct au8522_state *state, u16 reg)
Steven Toth265a6512008-04-18 21:34:00 -030058{
59 int ret;
Devin Heitmueller083a1732009-03-11 03:00:19 -030060 u8 b0 [] = { (reg >> 8) | 0x40, reg & 0xff };
Steven Toth265a6512008-04-18 21:34:00 -030061 u8 b1 [] = { 0 };
62
63 struct i2c_msg msg [] = {
64 { .addr = state->config->demod_address, .flags = 0,
65 .buf = b0, .len = 2 },
66 { .addr = state->config->demod_address, .flags = I2C_M_RD,
67 .buf = b1, .len = 1 } };
68
69 ret = i2c_transfer(state->i2c, msg, 2);
70
71 if (ret != 2)
Michael Krufkyce1719a2008-04-02 18:59:48 -030072 printk(KERN_ERR "%s: readreg error (ret == %i)\n",
73 __func__, ret);
Steven Toth265a6512008-04-18 21:34:00 -030074 return b1[0];
75}
76
Michael Krufkye059b0f2008-04-02 18:59:48 -030077static int au8522_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
Steven Toth265a6512008-04-18 21:34:00 -030078{
Michael Krufkye059b0f2008-04-02 18:59:48 -030079 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -030080
Michael Krufkyce1719a2008-04-02 18:59:48 -030081 dprintk("%s(%d)\n", __func__, enable);
Steven Toth265a6512008-04-18 21:34:00 -030082
83 if (enable)
84 return au8522_writereg(state, 0x106, 1);
85 else
86 return au8522_writereg(state, 0x106, 0);
87}
88
Michael Krufky0daa5de2008-04-10 04:24:56 -030089struct mse2snr_tab {
Steven Tothf01699b2008-04-10 02:01:48 -030090 u16 val;
91 u16 data;
Michael Krufky0daa5de2008-04-10 04:24:56 -030092};
93
94/* VSB SNR lookup table */
95static struct mse2snr_tab vsb_mse2snr_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -030096 { 0, 270 },
97 { 2, 250 },
98 { 3, 240 },
99 { 5, 230 },
100 { 7, 220 },
101 { 9, 210 },
102 { 12, 200 },
103 { 13, 195 },
104 { 15, 190 },
105 { 17, 185 },
106 { 19, 180 },
107 { 21, 175 },
108 { 24, 170 },
109 { 27, 165 },
110 { 31, 160 },
111 { 32, 158 },
112 { 33, 156 },
113 { 36, 152 },
114 { 37, 150 },
115 { 39, 148 },
116 { 40, 146 },
117 { 41, 144 },
118 { 43, 142 },
119 { 44, 140 },
120 { 48, 135 },
121 { 50, 130 },
122 { 43, 142 },
123 { 53, 125 },
124 { 56, 120 },
125 { 256, 115 },
126};
127
128/* QAM64 SNR lookup table */
Michael Krufky0daa5de2008-04-10 04:24:56 -0300129static struct mse2snr_tab qam64_mse2snr_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -0300130 { 15, 0 },
131 { 16, 290 },
132 { 17, 288 },
133 { 18, 286 },
134 { 19, 284 },
135 { 20, 282 },
136 { 21, 281 },
137 { 22, 279 },
138 { 23, 277 },
139 { 24, 275 },
140 { 25, 273 },
141 { 26, 271 },
142 { 27, 269 },
143 { 28, 268 },
144 { 29, 266 },
145 { 30, 264 },
146 { 31, 262 },
147 { 32, 260 },
148 { 33, 259 },
149 { 34, 258 },
150 { 35, 256 },
151 { 36, 255 },
152 { 37, 254 },
153 { 38, 252 },
154 { 39, 251 },
155 { 40, 250 },
156 { 41, 249 },
157 { 42, 248 },
158 { 43, 246 },
159 { 44, 245 },
160 { 45, 244 },
161 { 46, 242 },
162 { 47, 241 },
163 { 48, 240 },
164 { 50, 239 },
165 { 51, 238 },
166 { 53, 237 },
167 { 54, 236 },
168 { 56, 235 },
169 { 57, 234 },
170 { 59, 233 },
171 { 60, 232 },
172 { 62, 231 },
173 { 63, 230 },
174 { 65, 229 },
175 { 67, 228 },
176 { 68, 227 },
177 { 70, 226 },
178 { 71, 225 },
179 { 73, 224 },
180 { 74, 223 },
181 { 76, 222 },
182 { 78, 221 },
183 { 80, 220 },
184 { 82, 219 },
185 { 85, 218 },
186 { 88, 217 },
187 { 90, 216 },
188 { 92, 215 },
189 { 93, 214 },
190 { 94, 212 },
191 { 95, 211 },
192 { 97, 210 },
193 { 99, 209 },
194 { 101, 208 },
195 { 102, 207 },
196 { 104, 206 },
197 { 107, 205 },
198 { 111, 204 },
199 { 114, 203 },
200 { 118, 202 },
201 { 122, 201 },
202 { 125, 200 },
203 { 128, 199 },
204 { 130, 198 },
205 { 132, 197 },
206 { 256, 190 },
207};
208
209/* QAM256 SNR lookup table */
Michael Krufky0daa5de2008-04-10 04:24:56 -0300210static struct mse2snr_tab qam256_mse2snr_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -0300211 { 16, 0 },
212 { 17, 400 },
213 { 18, 398 },
214 { 19, 396 },
215 { 20, 394 },
216 { 21, 392 },
217 { 22, 390 },
218 { 23, 388 },
219 { 24, 386 },
220 { 25, 384 },
221 { 26, 382 },
222 { 27, 380 },
223 { 28, 379 },
224 { 29, 378 },
225 { 30, 377 },
226 { 31, 376 },
227 { 32, 375 },
228 { 33, 374 },
229 { 34, 373 },
230 { 35, 372 },
231 { 36, 371 },
232 { 37, 370 },
233 { 38, 362 },
234 { 39, 354 },
235 { 40, 346 },
236 { 41, 338 },
237 { 42, 330 },
238 { 43, 328 },
239 { 44, 326 },
240 { 45, 324 },
241 { 46, 322 },
242 { 47, 320 },
243 { 48, 319 },
244 { 49, 318 },
245 { 50, 317 },
246 { 51, 316 },
247 { 52, 315 },
248 { 53, 314 },
249 { 54, 313 },
250 { 55, 312 },
251 { 56, 311 },
252 { 57, 310 },
253 { 58, 308 },
254 { 59, 306 },
255 { 60, 304 },
256 { 61, 302 },
257 { 62, 300 },
258 { 63, 298 },
259 { 65, 295 },
260 { 68, 294 },
261 { 70, 293 },
262 { 73, 292 },
263 { 76, 291 },
264 { 78, 290 },
265 { 79, 289 },
266 { 81, 288 },
267 { 82, 287 },
268 { 83, 286 },
269 { 84, 285 },
270 { 85, 284 },
271 { 86, 283 },
272 { 88, 282 },
273 { 89, 281 },
274 { 256, 280 },
275};
276
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300277static int au8522_mse2snr_lookup(struct mse2snr_tab *tab, int sz, int mse,
278 u16 *snr)
Steven Tothf01699b2008-04-10 02:01:48 -0300279{
280 int i, ret = -EINVAL;
281 dprintk("%s()\n", __func__);
282
Michael Krufky0daa5de2008-04-10 04:24:56 -0300283 for (i = 0; i < sz; i++) {
284 if (mse < tab[i].val) {
285 *snr = tab[i].data;
Steven Tothf01699b2008-04-10 02:01:48 -0300286 ret = 0;
287 break;
288 }
289 }
290 dprintk("%s() snr=%d\n", __func__, *snr);
291 return ret;
292}
293
Michael Krufky2e7acd72008-09-03 16:46:35 -0300294static int au8522_set_if(struct dvb_frontend *fe, enum au8522_if_freq if_freq)
295{
296 struct au8522_state *state = fe->demodulator_priv;
Michael Krufkydf76de02008-09-03 16:46:40 -0300297 u8 r0b5, r0b6, r0b7;
298 char *ifmhz;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300299
300 switch (if_freq) {
301 case AU8522_IF_3_25MHZ:
Michael Krufkydf76de02008-09-03 16:46:40 -0300302 ifmhz = "3.25";
303 r0b5 = 0x00;
304 r0b6 = 0x3d;
305 r0b7 = 0xa0;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300306 break;
307 case AU8522_IF_4MHZ:
Michael Krufkydf76de02008-09-03 16:46:40 -0300308 ifmhz = "4.00";
309 r0b5 = 0x00;
310 r0b6 = 0x4b;
311 r0b7 = 0xd9;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300312 break;
313 case AU8522_IF_6MHZ:
Michael Krufkydf76de02008-09-03 16:46:40 -0300314 ifmhz = "6.00";
315 r0b5 = 0xfb;
316 r0b6 = 0x8e;
317 r0b7 = 0x39;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300318 break;
319 default:
320 dprintk("%s() IF Frequency not supported\n", __func__);
321 return -EINVAL;
322 }
Michael Krufkydf76de02008-09-03 16:46:40 -0300323 dprintk("%s() %s MHz\n", __func__, ifmhz);
324 au8522_writereg(state, 0x80b5, r0b5);
325 au8522_writereg(state, 0x80b6, r0b6);
326 au8522_writereg(state, 0x80b7, r0b7);
327
Michael Krufky2e7acd72008-09-03 16:46:35 -0300328 return 0;
329}
330
Steven Tothf01699b2008-04-10 02:01:48 -0300331/* VSB Modulation table */
332static struct {
333 u16 reg;
334 u16 data;
335} VSB_mod_tab[] = {
336 { 0x8090, 0x84 },
337 { 0x4092, 0x11 },
338 { 0x2005, 0x00 },
339 { 0x8091, 0x80 },
340 { 0x80a3, 0x0c },
341 { 0x80a4, 0xe8 },
342 { 0x8081, 0xc4 },
343 { 0x80a5, 0x40 },
344 { 0x80a7, 0x40 },
345 { 0x80a6, 0x67 },
346 { 0x8262, 0x20 },
347 { 0x821c, 0x30 },
348 { 0x80d8, 0x1a },
349 { 0x8227, 0xa0 },
350 { 0x8121, 0xff },
351 { 0x80a8, 0xf0 },
352 { 0x80a9, 0x05 },
353 { 0x80aa, 0x77 },
354 { 0x80ab, 0xf0 },
355 { 0x80ac, 0x05 },
356 { 0x80ad, 0x77 },
357 { 0x80ae, 0x41 },
358 { 0x80af, 0x66 },
359 { 0x821b, 0xcc },
360 { 0x821d, 0x80 },
Steven Tothf01699b2008-04-10 02:01:48 -0300361 { 0x80a4, 0xe8 },
362 { 0x8231, 0x13 },
363};
364
365/* QAM Modulation table */
366static struct {
367 u16 reg;
368 u16 data;
369} QAM_mod_tab[] = {
370 { 0x80a3, 0x09 },
371 { 0x80a4, 0x00 },
372 { 0x8081, 0xc4 },
373 { 0x80a5, 0x40 },
Steven Tothf01699b2008-04-10 02:01:48 -0300374 { 0x80aa, 0x77 },
375 { 0x80ad, 0x77 },
376 { 0x80a6, 0x67 },
377 { 0x8262, 0x20 },
378 { 0x821c, 0x30 },
379 { 0x80b8, 0x3e },
380 { 0x80b9, 0xf0 },
381 { 0x80ba, 0x01 },
382 { 0x80bb, 0x18 },
383 { 0x80bc, 0x50 },
384 { 0x80bd, 0x00 },
385 { 0x80be, 0xea },
386 { 0x80bf, 0xef },
387 { 0x80c0, 0xfc },
388 { 0x80c1, 0xbd },
389 { 0x80c2, 0x1f },
390 { 0x80c3, 0xfc },
391 { 0x80c4, 0xdd },
392 { 0x80c5, 0xaf },
393 { 0x80c6, 0x00 },
394 { 0x80c7, 0x38 },
395 { 0x80c8, 0x30 },
396 { 0x80c9, 0x05 },
397 { 0x80ca, 0x4a },
398 { 0x80cb, 0xd0 },
399 { 0x80cc, 0x01 },
400 { 0x80cd, 0xd9 },
401 { 0x80ce, 0x6f },
402 { 0x80cf, 0xf9 },
403 { 0x80d0, 0x70 },
404 { 0x80d1, 0xdf },
405 { 0x80d2, 0xf7 },
406 { 0x80d3, 0xc2 },
407 { 0x80d4, 0xdf },
408 { 0x80d5, 0x02 },
409 { 0x80d6, 0x9a },
410 { 0x80d7, 0xd0 },
411 { 0x8250, 0x0d },
412 { 0x8251, 0xcd },
413 { 0x8252, 0xe0 },
414 { 0x8253, 0x05 },
415 { 0x8254, 0xa7 },
416 { 0x8255, 0xff },
417 { 0x8256, 0xed },
418 { 0x8257, 0x5b },
419 { 0x8258, 0xae },
420 { 0x8259, 0xe6 },
421 { 0x825a, 0x3d },
422 { 0x825b, 0x0f },
423 { 0x825c, 0x0d },
424 { 0x825d, 0xea },
425 { 0x825e, 0xf2 },
426 { 0x825f, 0x51 },
427 { 0x8260, 0xf5 },
428 { 0x8261, 0x06 },
429 { 0x821a, 0x00 },
430 { 0x8546, 0x40 },
431 { 0x8210, 0x26 },
432 { 0x8211, 0xf6 },
433 { 0x8212, 0x84 },
434 { 0x8213, 0x02 },
435 { 0x8502, 0x01 },
436 { 0x8121, 0x04 },
437 { 0x8122, 0x04 },
438 { 0x852e, 0x10 },
439 { 0x80a4, 0xca },
440 { 0x80a7, 0x40 },
441 { 0x8526, 0x01 },
442};
443
Michael Krufkye059b0f2008-04-02 18:59:48 -0300444static int au8522_enable_modulation(struct dvb_frontend *fe,
445 fe_modulation_t m)
Steven Toth265a6512008-04-18 21:34:00 -0300446{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300447 struct au8522_state *state = fe->demodulator_priv;
Steven Tothf01699b2008-04-10 02:01:48 -0300448 int i;
Steven Toth265a6512008-04-18 21:34:00 -0300449
Michael Krufkyce1719a2008-04-02 18:59:48 -0300450 dprintk("%s(0x%08x)\n", __func__, m);
Steven Toth265a6512008-04-18 21:34:00 -0300451
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300452 switch (m) {
Steven Toth265a6512008-04-18 21:34:00 -0300453 case VSB_8:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300454 dprintk("%s() VSB_8\n", __func__);
Steven Tothf01699b2008-04-10 02:01:48 -0300455 for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
456 au8522_writereg(state,
457 VSB_mod_tab[i].reg,
458 VSB_mod_tab[i].data);
Michael Krufky2e7acd72008-09-03 16:46:35 -0300459 au8522_set_if(fe, state->config->vsb_if);
Steven Toth265a6512008-04-18 21:34:00 -0300460 break;
461 case QAM_64:
462 case QAM_256:
Steven Tothf01699b2008-04-10 02:01:48 -0300463 dprintk("%s() QAM 64/256\n", __func__);
464 for (i = 0; i < ARRAY_SIZE(QAM_mod_tab); i++)
465 au8522_writereg(state,
466 QAM_mod_tab[i].reg,
467 QAM_mod_tab[i].data);
Michael Krufky2e7acd72008-09-03 16:46:35 -0300468 au8522_set_if(fe, state->config->qam_if);
Steven Toth265a6512008-04-18 21:34:00 -0300469 break;
470 default:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300471 dprintk("%s() Invalid modulation\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300472 return -EINVAL;
473 }
474
475 state->current_modulation = m;
476
477 return 0;
478}
479
480/* Talk to the demod, set the FEC, GUARD, QAM settings etc */
Michael Krufkye059b0f2008-04-02 18:59:48 -0300481static int au8522_set_frontend(struct dvb_frontend *fe,
482 struct dvb_frontend_parameters *p)
Steven Toth265a6512008-04-18 21:34:00 -0300483{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300484 struct au8522_state *state = fe->demodulator_priv;
Michael Krufky74d50722008-06-13 20:33:23 -0300485 int ret = -EINVAL;
Steven Toth265a6512008-04-18 21:34:00 -0300486
Michael Krufkyce1719a2008-04-02 18:59:48 -0300487 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
Steven Toth265a6512008-04-18 21:34:00 -0300488
Michael Krufky74d50722008-06-13 20:33:23 -0300489 if ((state->current_frequency == p->frequency) &&
490 (state->current_modulation == p->u.vsb.modulation))
491 return 0;
Steven Toth265a6512008-04-18 21:34:00 -0300492
493 au8522_enable_modulation(fe, p->u.vsb.modulation);
494
495 /* Allow the demod to settle */
496 msleep(100);
497
498 if (fe->ops.tuner_ops.set_params) {
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300499 if (fe->ops.i2c_gate_ctrl)
500 fe->ops.i2c_gate_ctrl(fe, 1);
Michael Krufky74d50722008-06-13 20:33:23 -0300501 ret = fe->ops.tuner_ops.set_params(fe, p);
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300502 if (fe->ops.i2c_gate_ctrl)
503 fe->ops.i2c_gate_ctrl(fe, 0);
Steven Toth265a6512008-04-18 21:34:00 -0300504 }
505
Michael Krufky74d50722008-06-13 20:33:23 -0300506 if (ret < 0)
507 return ret;
508
509 state->current_frequency = p->frequency;
510
Steven Toth265a6512008-04-18 21:34:00 -0300511 return 0;
512}
513
514/* Reset the demod hardware and reset all of the configuration registers
515 to a default state. */
Devin Heitmueller0c44bf32009-03-11 02:59:56 -0300516int au8522_init(struct dvb_frontend *fe)
Steven Toth265a6512008-04-18 21:34:00 -0300517{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300518 struct au8522_state *state = fe->demodulator_priv;
Michael Krufkyce1719a2008-04-02 18:59:48 -0300519 dprintk("%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300520
521 au8522_writereg(state, 0xa4, 1 << 5);
522
523 au8522_i2c_gate_ctrl(fe, 1);
524
525 return 0;
526}
527
Michael Krufkyadeeac32008-04-05 20:55:14 -0300528static int au8522_led_gpio_enable(struct au8522_state *state, int onoff)
529{
530 struct au8522_led_config *led_config = state->config->led_cfg;
531 u8 val;
532
533 /* bail out if we cant control an LED */
534 if (!led_config || !led_config->gpio_output ||
535 !led_config->gpio_output_enable || !led_config->gpio_output_disable)
536 return 0;
537
538 val = au8522_readreg(state, 0x4000 |
539 (led_config->gpio_output & ~0xc000));
540 if (onoff) {
541 /* enable GPIO output */
542 val &= ~((led_config->gpio_output_enable >> 8) & 0xff);
543 val |= (led_config->gpio_output_enable & 0xff);
544 } else {
545 /* disable GPIO output */
546 val &= ~((led_config->gpio_output_disable >> 8) & 0xff);
547 val |= (led_config->gpio_output_disable & 0xff);
548 }
549 return au8522_writereg(state, 0x8000 |
550 (led_config->gpio_output & ~0xc000), val);
551}
552
553/* led = 0 | off
554 * led = 1 | signal ok
555 * led = 2 | signal strong
556 * led < 0 | only light led if leds are currently off
557 */
558static int au8522_led_ctrl(struct au8522_state *state, int led)
559{
560 struct au8522_led_config *led_config = state->config->led_cfg;
561 int i, ret = 0;
562
563 /* bail out if we cant control an LED */
564 if (!led_config || !led_config->gpio_leds ||
565 !led_config->num_led_states || !led_config->led_states)
566 return 0;
567
568 if (led < 0) {
569 /* if LED is already lit, then leave it as-is */
570 if (state->led_state)
571 return 0;
572 else
573 led *= -1;
574 }
575
576 /* toggle LED if changing state */
577 if (state->led_state != led) {
578 u8 val;
579
580 dprintk("%s: %d\n", __func__, led);
581
582 au8522_led_gpio_enable(state, 1);
583
584 val = au8522_readreg(state, 0x4000 |
585 (led_config->gpio_leds & ~0xc000));
586
587 /* start with all leds off */
588 for (i = 0; i < led_config->num_led_states; i++)
589 val &= ~led_config->led_states[i];
590
591 /* set selected LED state */
592 if (led < led_config->num_led_states)
593 val |= led_config->led_states[led];
594 else if (led_config->num_led_states)
595 val |=
596 led_config->led_states[led_config->num_led_states - 1];
597
598 ret = au8522_writereg(state, 0x8000 |
599 (led_config->gpio_leds & ~0xc000), val);
600 if (ret < 0)
601 return ret;
602
603 state->led_state = led;
604
605 if (led == 0)
606 au8522_led_gpio_enable(state, 0);
607 }
608
609 return 0;
610}
611
Devin Heitmueller0c44bf32009-03-11 02:59:56 -0300612int au8522_sleep(struct dvb_frontend *fe)
Michael Krufky74d50722008-06-13 20:33:23 -0300613{
614 struct au8522_state *state = fe->demodulator_priv;
615 dprintk("%s()\n", __func__);
616
Michael Krufkyadeeac32008-04-05 20:55:14 -0300617 /* turn off led */
618 au8522_led_ctrl(state, 0);
619
Devin Heitmueller7bf63ed2009-03-11 03:00:34 -0300620 /* Power down the chip */
621 au8522_writereg(state, 0xa4, 1 << 5);
622
Michael Krufky74d50722008-06-13 20:33:23 -0300623 state->current_frequency = 0;
624
625 return 0;
626}
627
Michael Krufkye059b0f2008-04-02 18:59:48 -0300628static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status)
Steven Toth265a6512008-04-18 21:34:00 -0300629{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300630 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300631 u8 reg;
632 u32 tuner_status = 0;
633
634 *status = 0;
635
636 if (state->current_modulation == VSB_8) {
Michael Krufkyce1719a2008-04-02 18:59:48 -0300637 dprintk("%s() Checking VSB_8\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300638 reg = au8522_readreg(state, 0x4088);
Steven Toth836c2852008-06-21 19:32:41 -0300639 if ((reg & 0x03) == 0x03)
640 *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
Steven Toth265a6512008-04-18 21:34:00 -0300641 } else {
Michael Krufkyce1719a2008-04-02 18:59:48 -0300642 dprintk("%s() Checking QAM\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300643 reg = au8522_readreg(state, 0x4541);
Michael Krufkye059b0f2008-04-02 18:59:48 -0300644 if (reg & 0x80)
Steven Toth265a6512008-04-18 21:34:00 -0300645 *status |= FE_HAS_VITERBI;
Michael Krufkye059b0f2008-04-02 18:59:48 -0300646 if (reg & 0x20)
Steven Toth265a6512008-04-18 21:34:00 -0300647 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
648 }
649
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300650 switch (state->config->status_mode) {
Steven Toth265a6512008-04-18 21:34:00 -0300651 case AU8522_DEMODLOCKING:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300652 dprintk("%s() DEMODLOCKING\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300653 if (*status & FE_HAS_VITERBI)
654 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
655 break;
656 case AU8522_TUNERLOCKING:
657 /* Get the tuner status */
Michael Krufkyce1719a2008-04-02 18:59:48 -0300658 dprintk("%s() TUNERLOCKING\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300659 if (fe->ops.tuner_ops.get_status) {
660 if (fe->ops.i2c_gate_ctrl)
661 fe->ops.i2c_gate_ctrl(fe, 1);
662
663 fe->ops.tuner_ops.get_status(fe, &tuner_status);
664
665 if (fe->ops.i2c_gate_ctrl)
666 fe->ops.i2c_gate_ctrl(fe, 0);
667 }
668 if (tuner_status)
669 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
670 break;
671 }
Michael Krufkyadeeac32008-04-05 20:55:14 -0300672 state->fe_status = *status;
673
674 if (*status & FE_HAS_LOCK)
675 /* turn on LED, if it isn't on already */
676 au8522_led_ctrl(state, -1);
677 else
678 /* turn off LED */
679 au8522_led_ctrl(state, 0);
Steven Toth265a6512008-04-18 21:34:00 -0300680
Michael Krufkyce1719a2008-04-02 18:59:48 -0300681 dprintk("%s() status 0x%08x\n", __func__, *status);
Steven Toth265a6512008-04-18 21:34:00 -0300682
683 return 0;
684}
685
Michael Krufkyadeeac32008-04-05 20:55:14 -0300686static int au8522_led_status(struct au8522_state *state, const u16 *snr)
687{
688 struct au8522_led_config *led_config = state->config->led_cfg;
689 int led;
690 u16 strong;
691
692 /* bail out if we cant control an LED */
693 if (!led_config)
694 return 0;
695
696 if (0 == (state->fe_status & FE_HAS_LOCK))
697 return au8522_led_ctrl(state, 0);
698 else if (state->current_modulation == QAM_256)
699 strong = led_config->qam256_strong;
700 else if (state->current_modulation == QAM_64)
701 strong = led_config->qam64_strong;
702 else /* (state->current_modulation == VSB_8) */
703 strong = led_config->vsb8_strong;
704
705 if (*snr >= strong)
706 led = 2;
707 else
708 led = 1;
709
710 if ((state->led_state) &&
711 (((strong < *snr) ? (*snr - strong) : (strong - *snr)) <= 10))
712 /* snr didn't change enough to bother
713 * changing the color of the led */
714 return 0;
715
716 return au8522_led_ctrl(state, led);
717}
718
Michael Krufkye059b0f2008-04-02 18:59:48 -0300719static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
Steven Toth265a6512008-04-18 21:34:00 -0300720{
Steven Tothf01699b2008-04-10 02:01:48 -0300721 struct au8522_state *state = fe->demodulator_priv;
722 int ret = -EINVAL;
723
Michael Krufkyce1719a2008-04-02 18:59:48 -0300724 dprintk("%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300725
Steven Tothf01699b2008-04-10 02:01:48 -0300726 if (state->current_modulation == QAM_256)
Michael Krufky0daa5de2008-04-10 04:24:56 -0300727 ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
728 ARRAY_SIZE(qam256_mse2snr_tab),
729 au8522_readreg(state, 0x4522),
730 snr);
Steven Tothf01699b2008-04-10 02:01:48 -0300731 else if (state->current_modulation == QAM_64)
Michael Krufky0daa5de2008-04-10 04:24:56 -0300732 ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
733 ARRAY_SIZE(qam64_mse2snr_tab),
734 au8522_readreg(state, 0x4522),
735 snr);
Steven Tothf01699b2008-04-10 02:01:48 -0300736 else /* VSB_8 */
Michael Krufky0daa5de2008-04-10 04:24:56 -0300737 ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
738 ARRAY_SIZE(vsb_mse2snr_tab),
739 au8522_readreg(state, 0x4311),
740 snr);
Steven Toth265a6512008-04-18 21:34:00 -0300741
Michael Krufkyadeeac32008-04-05 20:55:14 -0300742 if (state->config->led_cfg)
743 au8522_led_status(state, snr);
744
Steven Tothf01699b2008-04-10 02:01:48 -0300745 return ret;
Steven Toth265a6512008-04-18 21:34:00 -0300746}
747
Michael Krufkye059b0f2008-04-02 18:59:48 -0300748static int au8522_read_signal_strength(struct dvb_frontend *fe,
749 u16 *signal_strength)
Steven Toth265a6512008-04-18 21:34:00 -0300750{
751 return au8522_read_snr(fe, signal_strength);
752}
753
Michael Krufkye059b0f2008-04-02 18:59:48 -0300754static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
Steven Toth265a6512008-04-18 21:34:00 -0300755{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300756 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300757
Michael Krufky8973dc42008-04-05 23:08:08 -0300758 if (state->current_modulation == VSB_8)
759 *ucblocks = au8522_readreg(state, 0x4087);
760 else
761 *ucblocks = au8522_readreg(state, 0x4543);
Steven Toth265a6512008-04-18 21:34:00 -0300762
763 return 0;
764}
765
Michael Krufkye059b0f2008-04-02 18:59:48 -0300766static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
Steven Toth265a6512008-04-18 21:34:00 -0300767{
768 return au8522_read_ucblocks(fe, ber);
769}
770
Michael Krufkye059b0f2008-04-02 18:59:48 -0300771static int au8522_get_frontend(struct dvb_frontend *fe,
Steven Toth265a6512008-04-18 21:34:00 -0300772 struct dvb_frontend_parameters *p)
773{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300774 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300775
776 p->frequency = state->current_frequency;
777 p->u.vsb.modulation = state->current_modulation;
778
779 return 0;
780}
781
Michael Krufkye059b0f2008-04-02 18:59:48 -0300782static int au8522_get_tune_settings(struct dvb_frontend *fe,
783 struct dvb_frontend_tune_settings *tune)
Steven Toth265a6512008-04-18 21:34:00 -0300784{
785 tune->min_delay_ms = 1000;
786 return 0;
787}
788
Michael Krufkye059b0f2008-04-02 18:59:48 -0300789static void au8522_release(struct dvb_frontend *fe)
Steven Toth265a6512008-04-18 21:34:00 -0300790{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300791 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300792 kfree(state);
793}
794
795static struct dvb_frontend_ops au8522_ops;
796
Michael Krufkye059b0f2008-04-02 18:59:48 -0300797struct dvb_frontend *au8522_attach(const struct au8522_config *config,
798 struct i2c_adapter *i2c)
Steven Toth265a6512008-04-18 21:34:00 -0300799{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300800 struct au8522_state *state = NULL;
Steven Toth265a6512008-04-18 21:34:00 -0300801
802 /* allocate memory for the internal state */
803 state = kmalloc(sizeof(struct au8522_state), GFP_KERNEL);
804 if (state == NULL)
805 goto error;
806
807 /* setup the state */
808 state->config = config;
809 state->i2c = i2c;
810 /* create dvb_frontend */
811 memcpy(&state->frontend.ops, &au8522_ops,
812 sizeof(struct dvb_frontend_ops));
813 state->frontend.demodulator_priv = state;
814
815 if (au8522_init(&state->frontend) != 0) {
816 printk(KERN_ERR "%s: Failed to initialize correctly\n",
Michael Krufkyce1719a2008-04-02 18:59:48 -0300817 __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300818 goto error;
819 }
820
821 /* Note: Leaving the I2C gate open here. */
822 au8522_i2c_gate_ctrl(&state->frontend, 1);
823
824 return &state->frontend;
825
826error:
827 kfree(state);
828 return NULL;
829}
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300830EXPORT_SYMBOL(au8522_attach);
Steven Toth265a6512008-04-18 21:34:00 -0300831
832static struct dvb_frontend_ops au8522_ops = {
833
834 .info = {
835 .name = "Auvitek AU8522 QAM/8VSB Frontend",
836 .type = FE_ATSC,
837 .frequency_min = 54000000,
838 .frequency_max = 858000000,
839 .frequency_stepsize = 62500,
840 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
841 },
842
843 .init = au8522_init,
Michael Krufky74d50722008-06-13 20:33:23 -0300844 .sleep = au8522_sleep,
Steven Toth265a6512008-04-18 21:34:00 -0300845 .i2c_gate_ctrl = au8522_i2c_gate_ctrl,
846 .set_frontend = au8522_set_frontend,
847 .get_frontend = au8522_get_frontend,
848 .get_tune_settings = au8522_get_tune_settings,
849 .read_status = au8522_read_status,
850 .read_ber = au8522_read_ber,
851 .read_signal_strength = au8522_read_signal_strength,
852 .read_snr = au8522_read_snr,
853 .read_ucblocks = au8522_read_ucblocks,
854 .release = au8522_release,
855};
856
857module_param(debug, int, 0644);
858MODULE_PARM_DESC(debug, "Enable verbose debug messages");
859
860MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
861MODULE_AUTHOR("Steven Toth");
862MODULE_LICENSE("GPL");