blob: 35731258bb0a7b6d2025c7df0192c5e5bba8a2d7 [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
370/* QAM Modulation table */
371static struct {
372 u16 reg;
373 u16 data;
374} QAM_mod_tab[] = {
375 { 0x80a3, 0x09 },
376 { 0x80a4, 0x00 },
377 { 0x8081, 0xc4 },
378 { 0x80a5, 0x40 },
Steven Tothf01699b2008-04-10 02:01:48 -0300379 { 0x80aa, 0x77 },
380 { 0x80ad, 0x77 },
381 { 0x80a6, 0x67 },
382 { 0x8262, 0x20 },
383 { 0x821c, 0x30 },
384 { 0x80b8, 0x3e },
385 { 0x80b9, 0xf0 },
386 { 0x80ba, 0x01 },
387 { 0x80bb, 0x18 },
388 { 0x80bc, 0x50 },
389 { 0x80bd, 0x00 },
390 { 0x80be, 0xea },
391 { 0x80bf, 0xef },
392 { 0x80c0, 0xfc },
393 { 0x80c1, 0xbd },
394 { 0x80c2, 0x1f },
395 { 0x80c3, 0xfc },
396 { 0x80c4, 0xdd },
397 { 0x80c5, 0xaf },
398 { 0x80c6, 0x00 },
399 { 0x80c7, 0x38 },
400 { 0x80c8, 0x30 },
401 { 0x80c9, 0x05 },
402 { 0x80ca, 0x4a },
403 { 0x80cb, 0xd0 },
404 { 0x80cc, 0x01 },
405 { 0x80cd, 0xd9 },
406 { 0x80ce, 0x6f },
407 { 0x80cf, 0xf9 },
408 { 0x80d0, 0x70 },
409 { 0x80d1, 0xdf },
410 { 0x80d2, 0xf7 },
411 { 0x80d3, 0xc2 },
412 { 0x80d4, 0xdf },
413 { 0x80d5, 0x02 },
414 { 0x80d6, 0x9a },
415 { 0x80d7, 0xd0 },
416 { 0x8250, 0x0d },
417 { 0x8251, 0xcd },
418 { 0x8252, 0xe0 },
419 { 0x8253, 0x05 },
420 { 0x8254, 0xa7 },
421 { 0x8255, 0xff },
422 { 0x8256, 0xed },
423 { 0x8257, 0x5b },
424 { 0x8258, 0xae },
425 { 0x8259, 0xe6 },
426 { 0x825a, 0x3d },
427 { 0x825b, 0x0f },
428 { 0x825c, 0x0d },
429 { 0x825d, 0xea },
430 { 0x825e, 0xf2 },
431 { 0x825f, 0x51 },
432 { 0x8260, 0xf5 },
433 { 0x8261, 0x06 },
434 { 0x821a, 0x00 },
435 { 0x8546, 0x40 },
436 { 0x8210, 0x26 },
437 { 0x8211, 0xf6 },
438 { 0x8212, 0x84 },
439 { 0x8213, 0x02 },
440 { 0x8502, 0x01 },
441 { 0x8121, 0x04 },
442 { 0x8122, 0x04 },
443 { 0x852e, 0x10 },
444 { 0x80a4, 0xca },
445 { 0x80a7, 0x40 },
446 { 0x8526, 0x01 },
447};
448
Michael Krufkye059b0f2008-04-02 18:59:48 -0300449static int au8522_enable_modulation(struct dvb_frontend *fe,
450 fe_modulation_t m)
Steven Toth265a6512008-04-18 21:34:00 -0300451{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300452 struct au8522_state *state = fe->demodulator_priv;
Steven Tothf01699b2008-04-10 02:01:48 -0300453 int i;
Steven Toth265a6512008-04-18 21:34:00 -0300454
Michael Krufkyce1719a2008-04-02 18:59:48 -0300455 dprintk("%s(0x%08x)\n", __func__, m);
Steven Toth265a6512008-04-18 21:34:00 -0300456
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300457 switch (m) {
Steven Toth265a6512008-04-18 21:34:00 -0300458 case VSB_8:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300459 dprintk("%s() VSB_8\n", __func__);
Steven Tothf01699b2008-04-10 02:01:48 -0300460 for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
461 au8522_writereg(state,
462 VSB_mod_tab[i].reg,
463 VSB_mod_tab[i].data);
Michael Krufky2e7acd72008-09-03 16:46:35 -0300464 au8522_set_if(fe, state->config->vsb_if);
Steven Toth265a6512008-04-18 21:34:00 -0300465 break;
466 case QAM_64:
467 case QAM_256:
Steven Tothf01699b2008-04-10 02:01:48 -0300468 dprintk("%s() QAM 64/256\n", __func__);
469 for (i = 0; i < ARRAY_SIZE(QAM_mod_tab); i++)
470 au8522_writereg(state,
471 QAM_mod_tab[i].reg,
472 QAM_mod_tab[i].data);
Michael Krufky2e7acd72008-09-03 16:46:35 -0300473 au8522_set_if(fe, state->config->qam_if);
Steven Toth265a6512008-04-18 21:34:00 -0300474 break;
475 default:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300476 dprintk("%s() Invalid modulation\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300477 return -EINVAL;
478 }
479
480 state->current_modulation = m;
481
482 return 0;
483}
484
485/* Talk to the demod, set the FEC, GUARD, QAM settings etc */
Michael Krufkye059b0f2008-04-02 18:59:48 -0300486static int au8522_set_frontend(struct dvb_frontend *fe,
487 struct dvb_frontend_parameters *p)
Steven Toth265a6512008-04-18 21:34:00 -0300488{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300489 struct au8522_state *state = fe->demodulator_priv;
Michael Krufky74d50722008-06-13 20:33:23 -0300490 int ret = -EINVAL;
Steven Toth265a6512008-04-18 21:34:00 -0300491
Michael Krufkyce1719a2008-04-02 18:59:48 -0300492 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
Steven Toth265a6512008-04-18 21:34:00 -0300493
Michael Krufky74d50722008-06-13 20:33:23 -0300494 if ((state->current_frequency == p->frequency) &&
495 (state->current_modulation == p->u.vsb.modulation))
496 return 0;
Steven Toth265a6512008-04-18 21:34:00 -0300497
498 au8522_enable_modulation(fe, p->u.vsb.modulation);
499
500 /* Allow the demod to settle */
501 msleep(100);
502
503 if (fe->ops.tuner_ops.set_params) {
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300504 if (fe->ops.i2c_gate_ctrl)
505 fe->ops.i2c_gate_ctrl(fe, 1);
Michael Krufky74d50722008-06-13 20:33:23 -0300506 ret = fe->ops.tuner_ops.set_params(fe, p);
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300507 if (fe->ops.i2c_gate_ctrl)
508 fe->ops.i2c_gate_ctrl(fe, 0);
Steven Toth265a6512008-04-18 21:34:00 -0300509 }
510
Michael Krufky74d50722008-06-13 20:33:23 -0300511 if (ret < 0)
512 return ret;
513
514 state->current_frequency = p->frequency;
515
Steven Toth265a6512008-04-18 21:34:00 -0300516 return 0;
517}
518
519/* Reset the demod hardware and reset all of the configuration registers
520 to a default state. */
Devin Heitmueller0c44bf32009-03-11 02:59:56 -0300521int au8522_init(struct dvb_frontend *fe)
Steven Toth265a6512008-04-18 21:34:00 -0300522{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300523 struct au8522_state *state = fe->demodulator_priv;
Michael Krufkyce1719a2008-04-02 18:59:48 -0300524 dprintk("%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300525
526 au8522_writereg(state, 0xa4, 1 << 5);
527
528 au8522_i2c_gate_ctrl(fe, 1);
529
530 return 0;
531}
532
Michael Krufkyadeeac32008-04-05 20:55:14 -0300533static int au8522_led_gpio_enable(struct au8522_state *state, int onoff)
534{
535 struct au8522_led_config *led_config = state->config->led_cfg;
536 u8 val;
537
538 /* bail out if we cant control an LED */
539 if (!led_config || !led_config->gpio_output ||
540 !led_config->gpio_output_enable || !led_config->gpio_output_disable)
541 return 0;
542
543 val = au8522_readreg(state, 0x4000 |
544 (led_config->gpio_output & ~0xc000));
545 if (onoff) {
546 /* enable GPIO output */
547 val &= ~((led_config->gpio_output_enable >> 8) & 0xff);
548 val |= (led_config->gpio_output_enable & 0xff);
549 } else {
550 /* disable GPIO output */
551 val &= ~((led_config->gpio_output_disable >> 8) & 0xff);
552 val |= (led_config->gpio_output_disable & 0xff);
553 }
554 return au8522_writereg(state, 0x8000 |
555 (led_config->gpio_output & ~0xc000), val);
556}
557
558/* led = 0 | off
559 * led = 1 | signal ok
560 * led = 2 | signal strong
561 * led < 0 | only light led if leds are currently off
562 */
563static int au8522_led_ctrl(struct au8522_state *state, int led)
564{
565 struct au8522_led_config *led_config = state->config->led_cfg;
566 int i, ret = 0;
567
568 /* bail out if we cant control an LED */
569 if (!led_config || !led_config->gpio_leds ||
570 !led_config->num_led_states || !led_config->led_states)
571 return 0;
572
573 if (led < 0) {
574 /* if LED is already lit, then leave it as-is */
575 if (state->led_state)
576 return 0;
577 else
578 led *= -1;
579 }
580
581 /* toggle LED if changing state */
582 if (state->led_state != led) {
583 u8 val;
584
585 dprintk("%s: %d\n", __func__, led);
586
587 au8522_led_gpio_enable(state, 1);
588
589 val = au8522_readreg(state, 0x4000 |
590 (led_config->gpio_leds & ~0xc000));
591
592 /* start with all leds off */
593 for (i = 0; i < led_config->num_led_states; i++)
594 val &= ~led_config->led_states[i];
595
596 /* set selected LED state */
597 if (led < led_config->num_led_states)
598 val |= led_config->led_states[led];
599 else if (led_config->num_led_states)
600 val |=
601 led_config->led_states[led_config->num_led_states - 1];
602
603 ret = au8522_writereg(state, 0x8000 |
604 (led_config->gpio_leds & ~0xc000), val);
605 if (ret < 0)
606 return ret;
607
608 state->led_state = led;
609
610 if (led == 0)
611 au8522_led_gpio_enable(state, 0);
612 }
613
614 return 0;
615}
616
Devin Heitmueller0c44bf32009-03-11 02:59:56 -0300617int au8522_sleep(struct dvb_frontend *fe)
Michael Krufky74d50722008-06-13 20:33:23 -0300618{
619 struct au8522_state *state = fe->demodulator_priv;
620 dprintk("%s()\n", __func__);
621
Michael Krufkyadeeac32008-04-05 20:55:14 -0300622 /* turn off led */
623 au8522_led_ctrl(state, 0);
624
Devin Heitmueller7bf63ed2009-03-11 03:00:34 -0300625 /* Power down the chip */
626 au8522_writereg(state, 0xa4, 1 << 5);
627
Michael Krufky74d50722008-06-13 20:33:23 -0300628 state->current_frequency = 0;
629
630 return 0;
631}
632
Michael Krufkye059b0f2008-04-02 18:59:48 -0300633static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status)
Steven Toth265a6512008-04-18 21:34:00 -0300634{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300635 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300636 u8 reg;
637 u32 tuner_status = 0;
638
639 *status = 0;
640
641 if (state->current_modulation == VSB_8) {
Michael Krufkyce1719a2008-04-02 18:59:48 -0300642 dprintk("%s() Checking VSB_8\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300643 reg = au8522_readreg(state, 0x4088);
Steven Toth836c2852008-06-21 19:32:41 -0300644 if ((reg & 0x03) == 0x03)
645 *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
Steven Toth265a6512008-04-18 21:34:00 -0300646 } else {
Michael Krufkyce1719a2008-04-02 18:59:48 -0300647 dprintk("%s() Checking QAM\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300648 reg = au8522_readreg(state, 0x4541);
Michael Krufkye059b0f2008-04-02 18:59:48 -0300649 if (reg & 0x80)
Steven Toth265a6512008-04-18 21:34:00 -0300650 *status |= FE_HAS_VITERBI;
Michael Krufkye059b0f2008-04-02 18:59:48 -0300651 if (reg & 0x20)
Steven Toth265a6512008-04-18 21:34:00 -0300652 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
653 }
654
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300655 switch (state->config->status_mode) {
Steven Toth265a6512008-04-18 21:34:00 -0300656 case AU8522_DEMODLOCKING:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300657 dprintk("%s() DEMODLOCKING\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300658 if (*status & FE_HAS_VITERBI)
659 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
660 break;
661 case AU8522_TUNERLOCKING:
662 /* Get the tuner status */
Michael Krufkyce1719a2008-04-02 18:59:48 -0300663 dprintk("%s() TUNERLOCKING\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300664 if (fe->ops.tuner_ops.get_status) {
665 if (fe->ops.i2c_gate_ctrl)
666 fe->ops.i2c_gate_ctrl(fe, 1);
667
668 fe->ops.tuner_ops.get_status(fe, &tuner_status);
669
670 if (fe->ops.i2c_gate_ctrl)
671 fe->ops.i2c_gate_ctrl(fe, 0);
672 }
673 if (tuner_status)
674 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
675 break;
676 }
Michael Krufkyadeeac32008-04-05 20:55:14 -0300677 state->fe_status = *status;
678
679 if (*status & FE_HAS_LOCK)
680 /* turn on LED, if it isn't on already */
681 au8522_led_ctrl(state, -1);
682 else
683 /* turn off LED */
684 au8522_led_ctrl(state, 0);
Steven Toth265a6512008-04-18 21:34:00 -0300685
Michael Krufkyce1719a2008-04-02 18:59:48 -0300686 dprintk("%s() status 0x%08x\n", __func__, *status);
Steven Toth265a6512008-04-18 21:34:00 -0300687
688 return 0;
689}
690
Michael Krufkyadeeac32008-04-05 20:55:14 -0300691static int au8522_led_status(struct au8522_state *state, const u16 *snr)
692{
693 struct au8522_led_config *led_config = state->config->led_cfg;
694 int led;
695 u16 strong;
696
697 /* bail out if we cant control an LED */
698 if (!led_config)
699 return 0;
700
701 if (0 == (state->fe_status & FE_HAS_LOCK))
702 return au8522_led_ctrl(state, 0);
703 else if (state->current_modulation == QAM_256)
704 strong = led_config->qam256_strong;
705 else if (state->current_modulation == QAM_64)
706 strong = led_config->qam64_strong;
707 else /* (state->current_modulation == VSB_8) */
708 strong = led_config->vsb8_strong;
709
710 if (*snr >= strong)
711 led = 2;
712 else
713 led = 1;
714
715 if ((state->led_state) &&
716 (((strong < *snr) ? (*snr - strong) : (strong - *snr)) <= 10))
717 /* snr didn't change enough to bother
718 * changing the color of the led */
719 return 0;
720
721 return au8522_led_ctrl(state, led);
722}
723
Michael Krufkye059b0f2008-04-02 18:59:48 -0300724static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
Steven Toth265a6512008-04-18 21:34:00 -0300725{
Steven Tothf01699b2008-04-10 02:01:48 -0300726 struct au8522_state *state = fe->demodulator_priv;
727 int ret = -EINVAL;
728
Michael Krufkyce1719a2008-04-02 18:59:48 -0300729 dprintk("%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300730
Steven Tothf01699b2008-04-10 02:01:48 -0300731 if (state->current_modulation == QAM_256)
Michael Krufky0daa5de2008-04-10 04:24:56 -0300732 ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
733 ARRAY_SIZE(qam256_mse2snr_tab),
734 au8522_readreg(state, 0x4522),
735 snr);
Steven Tothf01699b2008-04-10 02:01:48 -0300736 else if (state->current_modulation == QAM_64)
Michael Krufky0daa5de2008-04-10 04:24:56 -0300737 ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
738 ARRAY_SIZE(qam64_mse2snr_tab),
739 au8522_readreg(state, 0x4522),
740 snr);
Steven Tothf01699b2008-04-10 02:01:48 -0300741 else /* VSB_8 */
Michael Krufky0daa5de2008-04-10 04:24:56 -0300742 ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
743 ARRAY_SIZE(vsb_mse2snr_tab),
744 au8522_readreg(state, 0x4311),
745 snr);
Steven Toth265a6512008-04-18 21:34:00 -0300746
Michael Krufkyadeeac32008-04-05 20:55:14 -0300747 if (state->config->led_cfg)
748 au8522_led_status(state, snr);
749
Steven Tothf01699b2008-04-10 02:01:48 -0300750 return ret;
Steven Toth265a6512008-04-18 21:34:00 -0300751}
752
Michael Krufkye059b0f2008-04-02 18:59:48 -0300753static int au8522_read_signal_strength(struct dvb_frontend *fe,
754 u16 *signal_strength)
Steven Toth265a6512008-04-18 21:34:00 -0300755{
756 return au8522_read_snr(fe, signal_strength);
757}
758
Michael Krufkye059b0f2008-04-02 18:59:48 -0300759static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
Steven Toth265a6512008-04-18 21:34:00 -0300760{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300761 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300762
Michael Krufky8973dc42008-04-05 23:08:08 -0300763 if (state->current_modulation == VSB_8)
764 *ucblocks = au8522_readreg(state, 0x4087);
765 else
766 *ucblocks = au8522_readreg(state, 0x4543);
Steven Toth265a6512008-04-18 21:34:00 -0300767
768 return 0;
769}
770
Michael Krufkye059b0f2008-04-02 18:59:48 -0300771static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
Steven Toth265a6512008-04-18 21:34:00 -0300772{
773 return au8522_read_ucblocks(fe, ber);
774}
775
Michael Krufkye059b0f2008-04-02 18:59:48 -0300776static int au8522_get_frontend(struct dvb_frontend *fe,
Steven Toth265a6512008-04-18 21:34:00 -0300777 struct dvb_frontend_parameters *p)
778{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300779 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300780
781 p->frequency = state->current_frequency;
782 p->u.vsb.modulation = state->current_modulation;
783
784 return 0;
785}
786
Michael Krufkye059b0f2008-04-02 18:59:48 -0300787static int au8522_get_tune_settings(struct dvb_frontend *fe,
788 struct dvb_frontend_tune_settings *tune)
Steven Toth265a6512008-04-18 21:34:00 -0300789{
790 tune->min_delay_ms = 1000;
791 return 0;
792}
793
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300794static struct dvb_frontend_ops au8522_ops;
795
796int au8522_get_state(struct au8522_state **state, struct i2c_adapter *i2c,
797 u8 client_address)
798{
Devin Heitmueller4ff5ed42009-03-11 03:00:45 -0300799 int ret;
800
801 mutex_lock(&au8522_list_mutex);
802 ret = hybrid_tuner_request_state(struct au8522_state, (*state),
803 hybrid_tuner_instance_list,
804 i2c, client_address, "au8522");
805 mutex_unlock(&au8522_list_mutex);
806
807 return ret;
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300808}
809
810void au8522_release_state(struct au8522_state *state)
811{
Devin Heitmueller4ff5ed42009-03-11 03:00:45 -0300812 mutex_lock(&au8522_list_mutex);
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300813 if (state != NULL)
814 hybrid_tuner_release_state(state);
Devin Heitmueller4ff5ed42009-03-11 03:00:45 -0300815 mutex_unlock(&au8522_list_mutex);
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300816}
817
818
Michael Krufkye059b0f2008-04-02 18:59:48 -0300819static void au8522_release(struct dvb_frontend *fe)
Steven Toth265a6512008-04-18 21:34:00 -0300820{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300821 struct au8522_state *state = fe->demodulator_priv;
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300822 au8522_release_state(state);
Steven Toth265a6512008-04-18 21:34:00 -0300823}
824
Michael Krufkye059b0f2008-04-02 18:59:48 -0300825struct dvb_frontend *au8522_attach(const struct au8522_config *config,
826 struct i2c_adapter *i2c)
Steven Toth265a6512008-04-18 21:34:00 -0300827{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300828 struct au8522_state *state = NULL;
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300829 int instance;
Steven Toth265a6512008-04-18 21:34:00 -0300830
831 /* allocate memory for the internal state */
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300832 instance = au8522_get_state(&state, i2c, config->demod_address);
833 switch (instance) {
834 case 0:
835 dprintk("%s state allocation failed\n", __func__);
836 break;
837 case 1:
838 /* new demod instance */
839 dprintk("%s using new instance\n", __func__);
840 break;
841 default:
842 /* existing demod instance */
843 dprintk("%s using existing instance\n", __func__);
844 break;
845 }
Steven Toth265a6512008-04-18 21:34:00 -0300846
847 /* setup the state */
848 state->config = config;
849 state->i2c = i2c;
850 /* create dvb_frontend */
851 memcpy(&state->frontend.ops, &au8522_ops,
852 sizeof(struct dvb_frontend_ops));
853 state->frontend.demodulator_priv = state;
854
855 if (au8522_init(&state->frontend) != 0) {
856 printk(KERN_ERR "%s: Failed to initialize correctly\n",
Michael Krufkyce1719a2008-04-02 18:59:48 -0300857 __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300858 goto error;
859 }
860
861 /* Note: Leaving the I2C gate open here. */
862 au8522_i2c_gate_ctrl(&state->frontend, 1);
863
864 return &state->frontend;
865
866error:
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300867 au8522_release_state(state);
Steven Toth265a6512008-04-18 21:34:00 -0300868 return NULL;
869}
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300870EXPORT_SYMBOL(au8522_attach);
Steven Toth265a6512008-04-18 21:34:00 -0300871
872static struct dvb_frontend_ops au8522_ops = {
873
874 .info = {
875 .name = "Auvitek AU8522 QAM/8VSB Frontend",
876 .type = FE_ATSC,
877 .frequency_min = 54000000,
878 .frequency_max = 858000000,
879 .frequency_stepsize = 62500,
880 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
881 },
882
883 .init = au8522_init,
Michael Krufky74d50722008-06-13 20:33:23 -0300884 .sleep = au8522_sleep,
Steven Toth265a6512008-04-18 21:34:00 -0300885 .i2c_gate_ctrl = au8522_i2c_gate_ctrl,
886 .set_frontend = au8522_set_frontend,
887 .get_frontend = au8522_get_frontend,
888 .get_tune_settings = au8522_get_tune_settings,
889 .read_status = au8522_read_status,
890 .read_ber = au8522_read_ber,
891 .read_signal_strength = au8522_read_signal_strength,
892 .read_snr = au8522_read_snr,
893 .read_ucblocks = au8522_read_ucblocks,
894 .release = au8522_release,
895};
896
897module_param(debug, int, 0644);
898MODULE_PARM_DESC(debug, "Enable verbose debug messages");
899
900MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
901MODULE_AUTHOR("Steven Toth");
902MODULE_LICENSE("GPL");