blob: eabf9a68e7ec36362ec3aab2c46f076ebfa54273 [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"
30
31struct au8522_state {
32
Michael Krufkye059b0f2008-04-02 18:59:48 -030033 struct i2c_adapter *i2c;
Steven Toth265a6512008-04-18 21:34:00 -030034
35 /* configuration settings */
Michael Krufkye059b0f2008-04-02 18:59:48 -030036 const struct au8522_config *config;
Steven Toth265a6512008-04-18 21:34:00 -030037
38 struct dvb_frontend frontend;
39
40 u32 current_frequency;
41 fe_modulation_t current_modulation;
42
Michael Krufkyadeeac32008-04-05 20:55:14 -030043 u32 fe_status;
44 unsigned int led_state;
Steven Toth265a6512008-04-18 21:34:00 -030045};
46
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -030047static int debug;
48
49#define dprintk(arg...) do { \
50 if (debug) \
Mauro Carvalho Chehab353a2762008-04-18 22:24:01 -030051 printk(arg); \
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -030052 } while (0)
Steven Toth265a6512008-04-18 21:34:00 -030053
54/* 16 bit registers, 8 bit values */
Michael Krufkye059b0f2008-04-02 18:59:48 -030055static int au8522_writereg(struct au8522_state *state, u16 reg, u8 data)
Steven Toth265a6512008-04-18 21:34:00 -030056{
57 int ret;
58 u8 buf [] = { reg >> 8, reg & 0xff, data };
59
60 struct i2c_msg msg = { .addr = state->config->demod_address,
61 .flags = 0, .buf = buf, .len = 3 };
62
63 ret = i2c_transfer(state->i2c, &msg, 1);
64
65 if (ret != 1)
66 printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
Michael Krufkyce1719a2008-04-02 18:59:48 -030067 "ret == %i)\n", __func__, reg, data, ret);
Steven Toth265a6512008-04-18 21:34:00 -030068
69 return (ret != 1) ? -1 : 0;
70}
71
Michael Krufkye059b0f2008-04-02 18:59:48 -030072static u8 au8522_readreg(struct au8522_state *state, u16 reg)
Steven Toth265a6512008-04-18 21:34:00 -030073{
74 int ret;
75 u8 b0 [] = { reg >> 8, reg & 0xff };
76 u8 b1 [] = { 0 };
77
78 struct i2c_msg msg [] = {
79 { .addr = state->config->demod_address, .flags = 0,
80 .buf = b0, .len = 2 },
81 { .addr = state->config->demod_address, .flags = I2C_M_RD,
82 .buf = b1, .len = 1 } };
83
84 ret = i2c_transfer(state->i2c, msg, 2);
85
86 if (ret != 2)
Michael Krufkyce1719a2008-04-02 18:59:48 -030087 printk(KERN_ERR "%s: readreg error (ret == %i)\n",
88 __func__, ret);
Steven Toth265a6512008-04-18 21:34:00 -030089 return b1[0];
90}
91
Michael Krufkye059b0f2008-04-02 18:59:48 -030092static int au8522_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
Steven Toth265a6512008-04-18 21:34:00 -030093{
Michael Krufkye059b0f2008-04-02 18:59:48 -030094 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -030095
Michael Krufkyce1719a2008-04-02 18:59:48 -030096 dprintk("%s(%d)\n", __func__, enable);
Steven Toth265a6512008-04-18 21:34:00 -030097
98 if (enable)
99 return au8522_writereg(state, 0x106, 1);
100 else
101 return au8522_writereg(state, 0x106, 0);
102}
103
Michael Krufky0daa5de2008-04-10 04:24:56 -0300104struct mse2snr_tab {
Steven Tothf01699b2008-04-10 02:01:48 -0300105 u16 val;
106 u16 data;
Michael Krufky0daa5de2008-04-10 04:24:56 -0300107};
108
109/* VSB SNR lookup table */
110static struct mse2snr_tab vsb_mse2snr_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -0300111 { 0, 270 },
112 { 2, 250 },
113 { 3, 240 },
114 { 5, 230 },
115 { 7, 220 },
116 { 9, 210 },
117 { 12, 200 },
118 { 13, 195 },
119 { 15, 190 },
120 { 17, 185 },
121 { 19, 180 },
122 { 21, 175 },
123 { 24, 170 },
124 { 27, 165 },
125 { 31, 160 },
126 { 32, 158 },
127 { 33, 156 },
128 { 36, 152 },
129 { 37, 150 },
130 { 39, 148 },
131 { 40, 146 },
132 { 41, 144 },
133 { 43, 142 },
134 { 44, 140 },
135 { 48, 135 },
136 { 50, 130 },
137 { 43, 142 },
138 { 53, 125 },
139 { 56, 120 },
140 { 256, 115 },
141};
142
143/* QAM64 SNR lookup table */
Michael Krufky0daa5de2008-04-10 04:24:56 -0300144static struct mse2snr_tab qam64_mse2snr_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -0300145 { 15, 0 },
146 { 16, 290 },
147 { 17, 288 },
148 { 18, 286 },
149 { 19, 284 },
150 { 20, 282 },
151 { 21, 281 },
152 { 22, 279 },
153 { 23, 277 },
154 { 24, 275 },
155 { 25, 273 },
156 { 26, 271 },
157 { 27, 269 },
158 { 28, 268 },
159 { 29, 266 },
160 { 30, 264 },
161 { 31, 262 },
162 { 32, 260 },
163 { 33, 259 },
164 { 34, 258 },
165 { 35, 256 },
166 { 36, 255 },
167 { 37, 254 },
168 { 38, 252 },
169 { 39, 251 },
170 { 40, 250 },
171 { 41, 249 },
172 { 42, 248 },
173 { 43, 246 },
174 { 44, 245 },
175 { 45, 244 },
176 { 46, 242 },
177 { 47, 241 },
178 { 48, 240 },
179 { 50, 239 },
180 { 51, 238 },
181 { 53, 237 },
182 { 54, 236 },
183 { 56, 235 },
184 { 57, 234 },
185 { 59, 233 },
186 { 60, 232 },
187 { 62, 231 },
188 { 63, 230 },
189 { 65, 229 },
190 { 67, 228 },
191 { 68, 227 },
192 { 70, 226 },
193 { 71, 225 },
194 { 73, 224 },
195 { 74, 223 },
196 { 76, 222 },
197 { 78, 221 },
198 { 80, 220 },
199 { 82, 219 },
200 { 85, 218 },
201 { 88, 217 },
202 { 90, 216 },
203 { 92, 215 },
204 { 93, 214 },
205 { 94, 212 },
206 { 95, 211 },
207 { 97, 210 },
208 { 99, 209 },
209 { 101, 208 },
210 { 102, 207 },
211 { 104, 206 },
212 { 107, 205 },
213 { 111, 204 },
214 { 114, 203 },
215 { 118, 202 },
216 { 122, 201 },
217 { 125, 200 },
218 { 128, 199 },
219 { 130, 198 },
220 { 132, 197 },
221 { 256, 190 },
222};
223
224/* QAM256 SNR lookup table */
Michael Krufky0daa5de2008-04-10 04:24:56 -0300225static struct mse2snr_tab qam256_mse2snr_tab[] = {
Steven Tothf01699b2008-04-10 02:01:48 -0300226 { 16, 0 },
227 { 17, 400 },
228 { 18, 398 },
229 { 19, 396 },
230 { 20, 394 },
231 { 21, 392 },
232 { 22, 390 },
233 { 23, 388 },
234 { 24, 386 },
235 { 25, 384 },
236 { 26, 382 },
237 { 27, 380 },
238 { 28, 379 },
239 { 29, 378 },
240 { 30, 377 },
241 { 31, 376 },
242 { 32, 375 },
243 { 33, 374 },
244 { 34, 373 },
245 { 35, 372 },
246 { 36, 371 },
247 { 37, 370 },
248 { 38, 362 },
249 { 39, 354 },
250 { 40, 346 },
251 { 41, 338 },
252 { 42, 330 },
253 { 43, 328 },
254 { 44, 326 },
255 { 45, 324 },
256 { 46, 322 },
257 { 47, 320 },
258 { 48, 319 },
259 { 49, 318 },
260 { 50, 317 },
261 { 51, 316 },
262 { 52, 315 },
263 { 53, 314 },
264 { 54, 313 },
265 { 55, 312 },
266 { 56, 311 },
267 { 57, 310 },
268 { 58, 308 },
269 { 59, 306 },
270 { 60, 304 },
271 { 61, 302 },
272 { 62, 300 },
273 { 63, 298 },
274 { 65, 295 },
275 { 68, 294 },
276 { 70, 293 },
277 { 73, 292 },
278 { 76, 291 },
279 { 78, 290 },
280 { 79, 289 },
281 { 81, 288 },
282 { 82, 287 },
283 { 83, 286 },
284 { 84, 285 },
285 { 85, 284 },
286 { 86, 283 },
287 { 88, 282 },
288 { 89, 281 },
289 { 256, 280 },
290};
291
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300292static int au8522_mse2snr_lookup(struct mse2snr_tab *tab, int sz, int mse,
293 u16 *snr)
Steven Tothf01699b2008-04-10 02:01:48 -0300294{
295 int i, ret = -EINVAL;
296 dprintk("%s()\n", __func__);
297
Michael Krufky0daa5de2008-04-10 04:24:56 -0300298 for (i = 0; i < sz; i++) {
299 if (mse < tab[i].val) {
300 *snr = tab[i].data;
Steven Tothf01699b2008-04-10 02:01:48 -0300301 ret = 0;
302 break;
303 }
304 }
305 dprintk("%s() snr=%d\n", __func__, *snr);
306 return ret;
307}
308
Michael Krufky2e7acd72008-09-03 16:46:35 -0300309static int au8522_set_if(struct dvb_frontend *fe, enum au8522_if_freq if_freq)
310{
311 struct au8522_state *state = fe->demodulator_priv;
Michael Krufkydf76de02008-09-03 16:46:40 -0300312 u8 r0b5, r0b6, r0b7;
313 char *ifmhz;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300314
315 switch (if_freq) {
316 case AU8522_IF_3_25MHZ:
Michael Krufkydf76de02008-09-03 16:46:40 -0300317 ifmhz = "3.25";
318 r0b5 = 0x00;
319 r0b6 = 0x3d;
320 r0b7 = 0xa0;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300321 break;
322 case AU8522_IF_4MHZ:
Michael Krufkydf76de02008-09-03 16:46:40 -0300323 ifmhz = "4.00";
324 r0b5 = 0x00;
325 r0b6 = 0x4b;
326 r0b7 = 0xd9;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300327 break;
328 case AU8522_IF_6MHZ:
Michael Krufkydf76de02008-09-03 16:46:40 -0300329 ifmhz = "6.00";
330 r0b5 = 0xfb;
331 r0b6 = 0x8e;
332 r0b7 = 0x39;
Michael Krufky2e7acd72008-09-03 16:46:35 -0300333 break;
334 default:
335 dprintk("%s() IF Frequency not supported\n", __func__);
336 return -EINVAL;
337 }
Michael Krufkydf76de02008-09-03 16:46:40 -0300338 dprintk("%s() %s MHz\n", __func__, ifmhz);
339 au8522_writereg(state, 0x80b5, r0b5);
340 au8522_writereg(state, 0x80b6, r0b6);
341 au8522_writereg(state, 0x80b7, r0b7);
342
Michael Krufky2e7acd72008-09-03 16:46:35 -0300343 return 0;
344}
345
Steven Tothf01699b2008-04-10 02:01:48 -0300346/* VSB Modulation table */
347static struct {
348 u16 reg;
349 u16 data;
350} VSB_mod_tab[] = {
351 { 0x8090, 0x84 },
352 { 0x4092, 0x11 },
353 { 0x2005, 0x00 },
354 { 0x8091, 0x80 },
355 { 0x80a3, 0x0c },
356 { 0x80a4, 0xe8 },
357 { 0x8081, 0xc4 },
358 { 0x80a5, 0x40 },
359 { 0x80a7, 0x40 },
360 { 0x80a6, 0x67 },
361 { 0x8262, 0x20 },
362 { 0x821c, 0x30 },
363 { 0x80d8, 0x1a },
364 { 0x8227, 0xa0 },
365 { 0x8121, 0xff },
366 { 0x80a8, 0xf0 },
367 { 0x80a9, 0x05 },
368 { 0x80aa, 0x77 },
369 { 0x80ab, 0xf0 },
370 { 0x80ac, 0x05 },
371 { 0x80ad, 0x77 },
372 { 0x80ae, 0x41 },
373 { 0x80af, 0x66 },
374 { 0x821b, 0xcc },
375 { 0x821d, 0x80 },
Steven Tothf01699b2008-04-10 02:01:48 -0300376 { 0x80a4, 0xe8 },
377 { 0x8231, 0x13 },
378};
379
380/* QAM Modulation table */
381static struct {
382 u16 reg;
383 u16 data;
384} QAM_mod_tab[] = {
385 { 0x80a3, 0x09 },
386 { 0x80a4, 0x00 },
387 { 0x8081, 0xc4 },
388 { 0x80a5, 0x40 },
Steven Tothf01699b2008-04-10 02:01:48 -0300389 { 0x80aa, 0x77 },
390 { 0x80ad, 0x77 },
391 { 0x80a6, 0x67 },
392 { 0x8262, 0x20 },
393 { 0x821c, 0x30 },
394 { 0x80b8, 0x3e },
395 { 0x80b9, 0xf0 },
396 { 0x80ba, 0x01 },
397 { 0x80bb, 0x18 },
398 { 0x80bc, 0x50 },
399 { 0x80bd, 0x00 },
400 { 0x80be, 0xea },
401 { 0x80bf, 0xef },
402 { 0x80c0, 0xfc },
403 { 0x80c1, 0xbd },
404 { 0x80c2, 0x1f },
405 { 0x80c3, 0xfc },
406 { 0x80c4, 0xdd },
407 { 0x80c5, 0xaf },
408 { 0x80c6, 0x00 },
409 { 0x80c7, 0x38 },
410 { 0x80c8, 0x30 },
411 { 0x80c9, 0x05 },
412 { 0x80ca, 0x4a },
413 { 0x80cb, 0xd0 },
414 { 0x80cc, 0x01 },
415 { 0x80cd, 0xd9 },
416 { 0x80ce, 0x6f },
417 { 0x80cf, 0xf9 },
418 { 0x80d0, 0x70 },
419 { 0x80d1, 0xdf },
420 { 0x80d2, 0xf7 },
421 { 0x80d3, 0xc2 },
422 { 0x80d4, 0xdf },
423 { 0x80d5, 0x02 },
424 { 0x80d6, 0x9a },
425 { 0x80d7, 0xd0 },
426 { 0x8250, 0x0d },
427 { 0x8251, 0xcd },
428 { 0x8252, 0xe0 },
429 { 0x8253, 0x05 },
430 { 0x8254, 0xa7 },
431 { 0x8255, 0xff },
432 { 0x8256, 0xed },
433 { 0x8257, 0x5b },
434 { 0x8258, 0xae },
435 { 0x8259, 0xe6 },
436 { 0x825a, 0x3d },
437 { 0x825b, 0x0f },
438 { 0x825c, 0x0d },
439 { 0x825d, 0xea },
440 { 0x825e, 0xf2 },
441 { 0x825f, 0x51 },
442 { 0x8260, 0xf5 },
443 { 0x8261, 0x06 },
444 { 0x821a, 0x00 },
445 { 0x8546, 0x40 },
446 { 0x8210, 0x26 },
447 { 0x8211, 0xf6 },
448 { 0x8212, 0x84 },
449 { 0x8213, 0x02 },
450 { 0x8502, 0x01 },
451 { 0x8121, 0x04 },
452 { 0x8122, 0x04 },
453 { 0x852e, 0x10 },
454 { 0x80a4, 0xca },
455 { 0x80a7, 0x40 },
456 { 0x8526, 0x01 },
457};
458
Michael Krufkye059b0f2008-04-02 18:59:48 -0300459static int au8522_enable_modulation(struct dvb_frontend *fe,
460 fe_modulation_t m)
Steven Toth265a6512008-04-18 21:34:00 -0300461{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300462 struct au8522_state *state = fe->demodulator_priv;
Steven Tothf01699b2008-04-10 02:01:48 -0300463 int i;
Steven Toth265a6512008-04-18 21:34:00 -0300464
Michael Krufkyce1719a2008-04-02 18:59:48 -0300465 dprintk("%s(0x%08x)\n", __func__, m);
Steven Toth265a6512008-04-18 21:34:00 -0300466
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300467 switch (m) {
Steven Toth265a6512008-04-18 21:34:00 -0300468 case VSB_8:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300469 dprintk("%s() VSB_8\n", __func__);
Steven Tothf01699b2008-04-10 02:01:48 -0300470 for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
471 au8522_writereg(state,
472 VSB_mod_tab[i].reg,
473 VSB_mod_tab[i].data);
Michael Krufky2e7acd72008-09-03 16:46:35 -0300474 au8522_set_if(fe, state->config->vsb_if);
Steven Toth265a6512008-04-18 21:34:00 -0300475 break;
476 case QAM_64:
477 case QAM_256:
Steven Tothf01699b2008-04-10 02:01:48 -0300478 dprintk("%s() QAM 64/256\n", __func__);
479 for (i = 0; i < ARRAY_SIZE(QAM_mod_tab); i++)
480 au8522_writereg(state,
481 QAM_mod_tab[i].reg,
482 QAM_mod_tab[i].data);
Michael Krufky2e7acd72008-09-03 16:46:35 -0300483 au8522_set_if(fe, state->config->qam_if);
Steven Toth265a6512008-04-18 21:34:00 -0300484 break;
485 default:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300486 dprintk("%s() Invalid modulation\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300487 return -EINVAL;
488 }
489
490 state->current_modulation = m;
491
492 return 0;
493}
494
495/* Talk to the demod, set the FEC, GUARD, QAM settings etc */
Michael Krufkye059b0f2008-04-02 18:59:48 -0300496static int au8522_set_frontend(struct dvb_frontend *fe,
497 struct dvb_frontend_parameters *p)
Steven Toth265a6512008-04-18 21:34:00 -0300498{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300499 struct au8522_state *state = fe->demodulator_priv;
Michael Krufky74d50722008-06-13 20:33:23 -0300500 int ret = -EINVAL;
Steven Toth265a6512008-04-18 21:34:00 -0300501
Michael Krufkyce1719a2008-04-02 18:59:48 -0300502 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
Steven Toth265a6512008-04-18 21:34:00 -0300503
Michael Krufky74d50722008-06-13 20:33:23 -0300504 if ((state->current_frequency == p->frequency) &&
505 (state->current_modulation == p->u.vsb.modulation))
506 return 0;
Steven Toth265a6512008-04-18 21:34:00 -0300507
508 au8522_enable_modulation(fe, p->u.vsb.modulation);
509
510 /* Allow the demod to settle */
511 msleep(100);
512
513 if (fe->ops.tuner_ops.set_params) {
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300514 if (fe->ops.i2c_gate_ctrl)
515 fe->ops.i2c_gate_ctrl(fe, 1);
Michael Krufky74d50722008-06-13 20:33:23 -0300516 ret = fe->ops.tuner_ops.set_params(fe, p);
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300517 if (fe->ops.i2c_gate_ctrl)
518 fe->ops.i2c_gate_ctrl(fe, 0);
Steven Toth265a6512008-04-18 21:34:00 -0300519 }
520
Michael Krufky74d50722008-06-13 20:33:23 -0300521 if (ret < 0)
522 return ret;
523
524 state->current_frequency = p->frequency;
525
Steven Toth265a6512008-04-18 21:34:00 -0300526 return 0;
527}
528
529/* Reset the demod hardware and reset all of the configuration registers
530 to a default state. */
Michael Krufkye059b0f2008-04-02 18:59:48 -0300531static int au8522_init(struct dvb_frontend *fe)
Steven Toth265a6512008-04-18 21:34:00 -0300532{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300533 struct au8522_state *state = fe->demodulator_priv;
Michael Krufkyce1719a2008-04-02 18:59:48 -0300534 dprintk("%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300535
536 au8522_writereg(state, 0xa4, 1 << 5);
537
538 au8522_i2c_gate_ctrl(fe, 1);
539
540 return 0;
541}
542
Michael Krufkyadeeac32008-04-05 20:55:14 -0300543static int au8522_led_gpio_enable(struct au8522_state *state, int onoff)
544{
545 struct au8522_led_config *led_config = state->config->led_cfg;
546 u8 val;
547
548 /* bail out if we cant control an LED */
549 if (!led_config || !led_config->gpio_output ||
550 !led_config->gpio_output_enable || !led_config->gpio_output_disable)
551 return 0;
552
553 val = au8522_readreg(state, 0x4000 |
554 (led_config->gpio_output & ~0xc000));
555 if (onoff) {
556 /* enable GPIO output */
557 val &= ~((led_config->gpio_output_enable >> 8) & 0xff);
558 val |= (led_config->gpio_output_enable & 0xff);
559 } else {
560 /* disable GPIO output */
561 val &= ~((led_config->gpio_output_disable >> 8) & 0xff);
562 val |= (led_config->gpio_output_disable & 0xff);
563 }
564 return au8522_writereg(state, 0x8000 |
565 (led_config->gpio_output & ~0xc000), val);
566}
567
568/* led = 0 | off
569 * led = 1 | signal ok
570 * led = 2 | signal strong
571 * led < 0 | only light led if leds are currently off
572 */
573static int au8522_led_ctrl(struct au8522_state *state, int led)
574{
575 struct au8522_led_config *led_config = state->config->led_cfg;
576 int i, ret = 0;
577
578 /* bail out if we cant control an LED */
579 if (!led_config || !led_config->gpio_leds ||
580 !led_config->num_led_states || !led_config->led_states)
581 return 0;
582
583 if (led < 0) {
584 /* if LED is already lit, then leave it as-is */
585 if (state->led_state)
586 return 0;
587 else
588 led *= -1;
589 }
590
591 /* toggle LED if changing state */
592 if (state->led_state != led) {
593 u8 val;
594
595 dprintk("%s: %d\n", __func__, led);
596
597 au8522_led_gpio_enable(state, 1);
598
599 val = au8522_readreg(state, 0x4000 |
600 (led_config->gpio_leds & ~0xc000));
601
602 /* start with all leds off */
603 for (i = 0; i < led_config->num_led_states; i++)
604 val &= ~led_config->led_states[i];
605
606 /* set selected LED state */
607 if (led < led_config->num_led_states)
608 val |= led_config->led_states[led];
609 else if (led_config->num_led_states)
610 val |=
611 led_config->led_states[led_config->num_led_states - 1];
612
613 ret = au8522_writereg(state, 0x8000 |
614 (led_config->gpio_leds & ~0xc000), val);
615 if (ret < 0)
616 return ret;
617
618 state->led_state = led;
619
620 if (led == 0)
621 au8522_led_gpio_enable(state, 0);
622 }
623
624 return 0;
625}
626
Michael Krufky74d50722008-06-13 20:33:23 -0300627static int au8522_sleep(struct dvb_frontend *fe)
628{
629 struct au8522_state *state = fe->demodulator_priv;
630 dprintk("%s()\n", __func__);
631
Michael Krufkyadeeac32008-04-05 20:55:14 -0300632 /* turn off led */
633 au8522_led_ctrl(state, 0);
634
Michael Krufky74d50722008-06-13 20:33:23 -0300635 state->current_frequency = 0;
636
637 return 0;
638}
639
Michael Krufkye059b0f2008-04-02 18:59:48 -0300640static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status)
Steven Toth265a6512008-04-18 21:34:00 -0300641{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300642 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300643 u8 reg;
644 u32 tuner_status = 0;
645
646 *status = 0;
647
648 if (state->current_modulation == VSB_8) {
Michael Krufkyce1719a2008-04-02 18:59:48 -0300649 dprintk("%s() Checking VSB_8\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300650 reg = au8522_readreg(state, 0x4088);
Steven Toth836c2852008-06-21 19:32:41 -0300651 if ((reg & 0x03) == 0x03)
652 *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
Steven Toth265a6512008-04-18 21:34:00 -0300653 } else {
Michael Krufkyce1719a2008-04-02 18:59:48 -0300654 dprintk("%s() Checking QAM\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300655 reg = au8522_readreg(state, 0x4541);
Michael Krufkye059b0f2008-04-02 18:59:48 -0300656 if (reg & 0x80)
Steven Toth265a6512008-04-18 21:34:00 -0300657 *status |= FE_HAS_VITERBI;
Michael Krufkye059b0f2008-04-02 18:59:48 -0300658 if (reg & 0x20)
Steven Toth265a6512008-04-18 21:34:00 -0300659 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
660 }
661
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300662 switch (state->config->status_mode) {
Steven Toth265a6512008-04-18 21:34:00 -0300663 case AU8522_DEMODLOCKING:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300664 dprintk("%s() DEMODLOCKING\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300665 if (*status & FE_HAS_VITERBI)
666 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
667 break;
668 case AU8522_TUNERLOCKING:
669 /* Get the tuner status */
Michael Krufkyce1719a2008-04-02 18:59:48 -0300670 dprintk("%s() TUNERLOCKING\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300671 if (fe->ops.tuner_ops.get_status) {
672 if (fe->ops.i2c_gate_ctrl)
673 fe->ops.i2c_gate_ctrl(fe, 1);
674
675 fe->ops.tuner_ops.get_status(fe, &tuner_status);
676
677 if (fe->ops.i2c_gate_ctrl)
678 fe->ops.i2c_gate_ctrl(fe, 0);
679 }
680 if (tuner_status)
681 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
682 break;
683 }
Michael Krufkyadeeac32008-04-05 20:55:14 -0300684 state->fe_status = *status;
685
686 if (*status & FE_HAS_LOCK)
687 /* turn on LED, if it isn't on already */
688 au8522_led_ctrl(state, -1);
689 else
690 /* turn off LED */
691 au8522_led_ctrl(state, 0);
Steven Toth265a6512008-04-18 21:34:00 -0300692
Michael Krufkyce1719a2008-04-02 18:59:48 -0300693 dprintk("%s() status 0x%08x\n", __func__, *status);
Steven Toth265a6512008-04-18 21:34:00 -0300694
695 return 0;
696}
697
Michael Krufkyadeeac32008-04-05 20:55:14 -0300698static int au8522_led_status(struct au8522_state *state, const u16 *snr)
699{
700 struct au8522_led_config *led_config = state->config->led_cfg;
701 int led;
702 u16 strong;
703
704 /* bail out if we cant control an LED */
705 if (!led_config)
706 return 0;
707
708 if (0 == (state->fe_status & FE_HAS_LOCK))
709 return au8522_led_ctrl(state, 0);
710 else if (state->current_modulation == QAM_256)
711 strong = led_config->qam256_strong;
712 else if (state->current_modulation == QAM_64)
713 strong = led_config->qam64_strong;
714 else /* (state->current_modulation == VSB_8) */
715 strong = led_config->vsb8_strong;
716
717 if (*snr >= strong)
718 led = 2;
719 else
720 led = 1;
721
722 if ((state->led_state) &&
723 (((strong < *snr) ? (*snr - strong) : (strong - *snr)) <= 10))
724 /* snr didn't change enough to bother
725 * changing the color of the led */
726 return 0;
727
728 return au8522_led_ctrl(state, led);
729}
730
Michael Krufkye059b0f2008-04-02 18:59:48 -0300731static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
Steven Toth265a6512008-04-18 21:34:00 -0300732{
Steven Tothf01699b2008-04-10 02:01:48 -0300733 struct au8522_state *state = fe->demodulator_priv;
734 int ret = -EINVAL;
735
Michael Krufkyce1719a2008-04-02 18:59:48 -0300736 dprintk("%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300737
Steven Tothf01699b2008-04-10 02:01:48 -0300738 if (state->current_modulation == QAM_256)
Michael Krufky0daa5de2008-04-10 04:24:56 -0300739 ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
740 ARRAY_SIZE(qam256_mse2snr_tab),
741 au8522_readreg(state, 0x4522),
742 snr);
Steven Tothf01699b2008-04-10 02:01:48 -0300743 else if (state->current_modulation == QAM_64)
Michael Krufky0daa5de2008-04-10 04:24:56 -0300744 ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
745 ARRAY_SIZE(qam64_mse2snr_tab),
746 au8522_readreg(state, 0x4522),
747 snr);
Steven Tothf01699b2008-04-10 02:01:48 -0300748 else /* VSB_8 */
Michael Krufky0daa5de2008-04-10 04:24:56 -0300749 ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
750 ARRAY_SIZE(vsb_mse2snr_tab),
751 au8522_readreg(state, 0x4311),
752 snr);
Steven Toth265a6512008-04-18 21:34:00 -0300753
Michael Krufkyadeeac32008-04-05 20:55:14 -0300754 if (state->config->led_cfg)
755 au8522_led_status(state, snr);
756
Steven Tothf01699b2008-04-10 02:01:48 -0300757 return ret;
Steven Toth265a6512008-04-18 21:34:00 -0300758}
759
Michael Krufkye059b0f2008-04-02 18:59:48 -0300760static int au8522_read_signal_strength(struct dvb_frontend *fe,
761 u16 *signal_strength)
Steven Toth265a6512008-04-18 21:34:00 -0300762{
763 return au8522_read_snr(fe, signal_strength);
764}
765
Michael Krufkye059b0f2008-04-02 18:59:48 -0300766static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
Steven Toth265a6512008-04-18 21:34:00 -0300767{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300768 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300769
Michael Krufky8973dc42008-04-05 23:08:08 -0300770 if (state->current_modulation == VSB_8)
771 *ucblocks = au8522_readreg(state, 0x4087);
772 else
773 *ucblocks = au8522_readreg(state, 0x4543);
Steven Toth265a6512008-04-18 21:34:00 -0300774
775 return 0;
776}
777
Michael Krufkye059b0f2008-04-02 18:59:48 -0300778static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
Steven Toth265a6512008-04-18 21:34:00 -0300779{
780 return au8522_read_ucblocks(fe, ber);
781}
782
Michael Krufkye059b0f2008-04-02 18:59:48 -0300783static int au8522_get_frontend(struct dvb_frontend *fe,
Steven Toth265a6512008-04-18 21:34:00 -0300784 struct dvb_frontend_parameters *p)
785{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300786 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300787
788 p->frequency = state->current_frequency;
789 p->u.vsb.modulation = state->current_modulation;
790
791 return 0;
792}
793
Michael Krufkye059b0f2008-04-02 18:59:48 -0300794static int au8522_get_tune_settings(struct dvb_frontend *fe,
795 struct dvb_frontend_tune_settings *tune)
Steven Toth265a6512008-04-18 21:34:00 -0300796{
797 tune->min_delay_ms = 1000;
798 return 0;
799}
800
Michael Krufkye059b0f2008-04-02 18:59:48 -0300801static void au8522_release(struct dvb_frontend *fe)
Steven Toth265a6512008-04-18 21:34:00 -0300802{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300803 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300804 kfree(state);
805}
806
807static struct dvb_frontend_ops au8522_ops;
808
Michael Krufkye059b0f2008-04-02 18:59:48 -0300809struct dvb_frontend *au8522_attach(const struct au8522_config *config,
810 struct i2c_adapter *i2c)
Steven Toth265a6512008-04-18 21:34:00 -0300811{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300812 struct au8522_state *state = NULL;
Steven Toth265a6512008-04-18 21:34:00 -0300813
814 /* allocate memory for the internal state */
815 state = kmalloc(sizeof(struct au8522_state), GFP_KERNEL);
816 if (state == NULL)
817 goto error;
818
819 /* setup the state */
820 state->config = config;
821 state->i2c = i2c;
822 /* create dvb_frontend */
823 memcpy(&state->frontend.ops, &au8522_ops,
824 sizeof(struct dvb_frontend_ops));
825 state->frontend.demodulator_priv = state;
826
827 if (au8522_init(&state->frontend) != 0) {
828 printk(KERN_ERR "%s: Failed to initialize correctly\n",
Michael Krufkyce1719a2008-04-02 18:59:48 -0300829 __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300830 goto error;
831 }
832
833 /* Note: Leaving the I2C gate open here. */
834 au8522_i2c_gate_ctrl(&state->frontend, 1);
835
836 return &state->frontend;
837
838error:
839 kfree(state);
840 return NULL;
841}
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300842EXPORT_SYMBOL(au8522_attach);
Steven Toth265a6512008-04-18 21:34:00 -0300843
844static struct dvb_frontend_ops au8522_ops = {
845
846 .info = {
847 .name = "Auvitek AU8522 QAM/8VSB Frontend",
848 .type = FE_ATSC,
849 .frequency_min = 54000000,
850 .frequency_max = 858000000,
851 .frequency_stepsize = 62500,
852 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
853 },
854
855 .init = au8522_init,
Michael Krufky74d50722008-06-13 20:33:23 -0300856 .sleep = au8522_sleep,
Steven Toth265a6512008-04-18 21:34:00 -0300857 .i2c_gate_ctrl = au8522_i2c_gate_ctrl,
858 .set_frontend = au8522_set_frontend,
859 .get_frontend = au8522_get_frontend,
860 .get_tune_settings = au8522_get_tune_settings,
861 .read_status = au8522_read_status,
862 .read_ber = au8522_read_ber,
863 .read_signal_strength = au8522_read_signal_strength,
864 .read_snr = au8522_read_snr,
865 .read_ucblocks = au8522_read_ucblocks,
866 .release = au8522_release,
867};
868
869module_param(debug, int, 0644);
870MODULE_PARM_DESC(debug, "Enable verbose debug messages");
871
872MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
873MODULE_AUTHOR("Steven Toth");
874MODULE_LICENSE("GPL");