blob: b04346687c689ab4d5fa0440c44d60ab918dc707 [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);
37
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -030038#define dprintk(arg...) do { \
39 if (debug) \
Mauro Carvalho Chehab353a2762008-04-18 22:24:01 -030040 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 Heitmueller083a1732009-03-11 03:00:19 -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 Heitmueller083a1732009-03-11 03:00:19 -030064 u8 b0 [] = { (reg >> 8) | 0x40, reg & 0xff };
Steven Toth265a6512008-04-18 21:34:00 -030065 u8 b1 [] = { 0 };
66
67 struct i2c_msg msg [] = {
68 { .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
369/* QAM Modulation table */
370static struct {
371 u16 reg;
372 u16 data;
373} QAM_mod_tab[] = {
374 { 0x80a3, 0x09 },
375 { 0x80a4, 0x00 },
376 { 0x8081, 0xc4 },
377 { 0x80a5, 0x40 },
Steven Tothf01699b2008-04-10 02:01:48 -0300378 { 0x80aa, 0x77 },
379 { 0x80ad, 0x77 },
380 { 0x80a6, 0x67 },
381 { 0x8262, 0x20 },
382 { 0x821c, 0x30 },
383 { 0x80b8, 0x3e },
384 { 0x80b9, 0xf0 },
385 { 0x80ba, 0x01 },
386 { 0x80bb, 0x18 },
387 { 0x80bc, 0x50 },
388 { 0x80bd, 0x00 },
389 { 0x80be, 0xea },
390 { 0x80bf, 0xef },
391 { 0x80c0, 0xfc },
392 { 0x80c1, 0xbd },
393 { 0x80c2, 0x1f },
394 { 0x80c3, 0xfc },
395 { 0x80c4, 0xdd },
396 { 0x80c5, 0xaf },
397 { 0x80c6, 0x00 },
398 { 0x80c7, 0x38 },
399 { 0x80c8, 0x30 },
400 { 0x80c9, 0x05 },
401 { 0x80ca, 0x4a },
402 { 0x80cb, 0xd0 },
403 { 0x80cc, 0x01 },
404 { 0x80cd, 0xd9 },
405 { 0x80ce, 0x6f },
406 { 0x80cf, 0xf9 },
407 { 0x80d0, 0x70 },
408 { 0x80d1, 0xdf },
409 { 0x80d2, 0xf7 },
410 { 0x80d3, 0xc2 },
411 { 0x80d4, 0xdf },
412 { 0x80d5, 0x02 },
413 { 0x80d6, 0x9a },
414 { 0x80d7, 0xd0 },
415 { 0x8250, 0x0d },
416 { 0x8251, 0xcd },
417 { 0x8252, 0xe0 },
418 { 0x8253, 0x05 },
419 { 0x8254, 0xa7 },
420 { 0x8255, 0xff },
421 { 0x8256, 0xed },
422 { 0x8257, 0x5b },
423 { 0x8258, 0xae },
424 { 0x8259, 0xe6 },
425 { 0x825a, 0x3d },
426 { 0x825b, 0x0f },
427 { 0x825c, 0x0d },
428 { 0x825d, 0xea },
429 { 0x825e, 0xf2 },
430 { 0x825f, 0x51 },
431 { 0x8260, 0xf5 },
432 { 0x8261, 0x06 },
433 { 0x821a, 0x00 },
434 { 0x8546, 0x40 },
435 { 0x8210, 0x26 },
436 { 0x8211, 0xf6 },
437 { 0x8212, 0x84 },
438 { 0x8213, 0x02 },
439 { 0x8502, 0x01 },
440 { 0x8121, 0x04 },
441 { 0x8122, 0x04 },
442 { 0x852e, 0x10 },
443 { 0x80a4, 0xca },
444 { 0x80a7, 0x40 },
445 { 0x8526, 0x01 },
446};
447
Michael Krufkye059b0f2008-04-02 18:59:48 -0300448static int au8522_enable_modulation(struct dvb_frontend *fe,
449 fe_modulation_t m)
Steven Toth265a6512008-04-18 21:34:00 -0300450{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300451 struct au8522_state *state = fe->demodulator_priv;
Steven Tothf01699b2008-04-10 02:01:48 -0300452 int i;
Steven Toth265a6512008-04-18 21:34:00 -0300453
Michael Krufkyce1719a2008-04-02 18:59:48 -0300454 dprintk("%s(0x%08x)\n", __func__, m);
Steven Toth265a6512008-04-18 21:34:00 -0300455
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300456 switch (m) {
Steven Toth265a6512008-04-18 21:34:00 -0300457 case VSB_8:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300458 dprintk("%s() VSB_8\n", __func__);
Steven Tothf01699b2008-04-10 02:01:48 -0300459 for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
460 au8522_writereg(state,
461 VSB_mod_tab[i].reg,
462 VSB_mod_tab[i].data);
Michael Krufky2e7acd72008-09-03 16:46:35 -0300463 au8522_set_if(fe, state->config->vsb_if);
Steven Toth265a6512008-04-18 21:34:00 -0300464 break;
465 case QAM_64:
466 case QAM_256:
Steven Tothf01699b2008-04-10 02:01:48 -0300467 dprintk("%s() QAM 64/256\n", __func__);
468 for (i = 0; i < ARRAY_SIZE(QAM_mod_tab); i++)
469 au8522_writereg(state,
470 QAM_mod_tab[i].reg,
471 QAM_mod_tab[i].data);
Michael Krufky2e7acd72008-09-03 16:46:35 -0300472 au8522_set_if(fe, state->config->qam_if);
Steven Toth265a6512008-04-18 21:34:00 -0300473 break;
474 default:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300475 dprintk("%s() Invalid modulation\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300476 return -EINVAL;
477 }
478
479 state->current_modulation = m;
480
481 return 0;
482}
483
484/* Talk to the demod, set the FEC, GUARD, QAM settings etc */
Michael Krufkye059b0f2008-04-02 18:59:48 -0300485static int au8522_set_frontend(struct dvb_frontend *fe,
486 struct dvb_frontend_parameters *p)
Steven Toth265a6512008-04-18 21:34:00 -0300487{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300488 struct au8522_state *state = fe->demodulator_priv;
Michael Krufky74d50722008-06-13 20:33:23 -0300489 int ret = -EINVAL;
Steven Toth265a6512008-04-18 21:34:00 -0300490
Michael Krufkyce1719a2008-04-02 18:59:48 -0300491 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
Steven Toth265a6512008-04-18 21:34:00 -0300492
Michael Krufky74d50722008-06-13 20:33:23 -0300493 if ((state->current_frequency == p->frequency) &&
494 (state->current_modulation == p->u.vsb.modulation))
495 return 0;
Steven Toth265a6512008-04-18 21:34:00 -0300496
497 au8522_enable_modulation(fe, p->u.vsb.modulation);
498
499 /* Allow the demod to settle */
500 msleep(100);
501
502 if (fe->ops.tuner_ops.set_params) {
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300503 if (fe->ops.i2c_gate_ctrl)
504 fe->ops.i2c_gate_ctrl(fe, 1);
Michael Krufky74d50722008-06-13 20:33:23 -0300505 ret = fe->ops.tuner_ops.set_params(fe, p);
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300506 if (fe->ops.i2c_gate_ctrl)
507 fe->ops.i2c_gate_ctrl(fe, 0);
Steven Toth265a6512008-04-18 21:34:00 -0300508 }
509
Michael Krufky74d50722008-06-13 20:33:23 -0300510 if (ret < 0)
511 return ret;
512
513 state->current_frequency = p->frequency;
514
Steven Toth265a6512008-04-18 21:34:00 -0300515 return 0;
516}
517
518/* Reset the demod hardware and reset all of the configuration registers
519 to a default state. */
Devin Heitmueller0c44bf32009-03-11 02:59:56 -0300520int au8522_init(struct dvb_frontend *fe)
Steven Toth265a6512008-04-18 21:34:00 -0300521{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300522 struct au8522_state *state = fe->demodulator_priv;
Michael Krufkyce1719a2008-04-02 18:59:48 -0300523 dprintk("%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300524
525 au8522_writereg(state, 0xa4, 1 << 5);
526
527 au8522_i2c_gate_ctrl(fe, 1);
528
529 return 0;
530}
531
Michael Krufkyadeeac32008-04-05 20:55:14 -0300532static int au8522_led_gpio_enable(struct au8522_state *state, int onoff)
533{
534 struct au8522_led_config *led_config = state->config->led_cfg;
535 u8 val;
536
537 /* bail out if we cant control an LED */
538 if (!led_config || !led_config->gpio_output ||
539 !led_config->gpio_output_enable || !led_config->gpio_output_disable)
540 return 0;
541
542 val = au8522_readreg(state, 0x4000 |
543 (led_config->gpio_output & ~0xc000));
544 if (onoff) {
545 /* enable GPIO output */
546 val &= ~((led_config->gpio_output_enable >> 8) & 0xff);
547 val |= (led_config->gpio_output_enable & 0xff);
548 } else {
549 /* disable GPIO output */
550 val &= ~((led_config->gpio_output_disable >> 8) & 0xff);
551 val |= (led_config->gpio_output_disable & 0xff);
552 }
553 return au8522_writereg(state, 0x8000 |
554 (led_config->gpio_output & ~0xc000), val);
555}
556
557/* led = 0 | off
558 * led = 1 | signal ok
559 * led = 2 | signal strong
560 * led < 0 | only light led if leds are currently off
561 */
562static int au8522_led_ctrl(struct au8522_state *state, int led)
563{
564 struct au8522_led_config *led_config = state->config->led_cfg;
565 int i, ret = 0;
566
567 /* bail out if we cant control an LED */
568 if (!led_config || !led_config->gpio_leds ||
569 !led_config->num_led_states || !led_config->led_states)
570 return 0;
571
572 if (led < 0) {
573 /* if LED is already lit, then leave it as-is */
574 if (state->led_state)
575 return 0;
576 else
577 led *= -1;
578 }
579
580 /* toggle LED if changing state */
581 if (state->led_state != led) {
582 u8 val;
583
584 dprintk("%s: %d\n", __func__, led);
585
586 au8522_led_gpio_enable(state, 1);
587
588 val = au8522_readreg(state, 0x4000 |
589 (led_config->gpio_leds & ~0xc000));
590
591 /* start with all leds off */
592 for (i = 0; i < led_config->num_led_states; i++)
593 val &= ~led_config->led_states[i];
594
595 /* set selected LED state */
596 if (led < led_config->num_led_states)
597 val |= led_config->led_states[led];
598 else if (led_config->num_led_states)
599 val |=
600 led_config->led_states[led_config->num_led_states - 1];
601
602 ret = au8522_writereg(state, 0x8000 |
603 (led_config->gpio_leds & ~0xc000), val);
604 if (ret < 0)
605 return ret;
606
607 state->led_state = led;
608
609 if (led == 0)
610 au8522_led_gpio_enable(state, 0);
611 }
612
613 return 0;
614}
615
Devin Heitmueller0c44bf32009-03-11 02:59:56 -0300616int au8522_sleep(struct dvb_frontend *fe)
Michael Krufky74d50722008-06-13 20:33:23 -0300617{
618 struct au8522_state *state = fe->demodulator_priv;
619 dprintk("%s()\n", __func__);
620
Michael Krufkyadeeac32008-04-05 20:55:14 -0300621 /* turn off led */
622 au8522_led_ctrl(state, 0);
623
Devin Heitmueller7bf63ed2009-03-11 03:00:34 -0300624 /* Power down the chip */
625 au8522_writereg(state, 0xa4, 1 << 5);
626
Michael Krufky74d50722008-06-13 20:33:23 -0300627 state->current_frequency = 0;
628
629 return 0;
630}
631
Michael Krufkye059b0f2008-04-02 18:59:48 -0300632static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status)
Steven Toth265a6512008-04-18 21:34:00 -0300633{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300634 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300635 u8 reg;
636 u32 tuner_status = 0;
637
638 *status = 0;
639
640 if (state->current_modulation == VSB_8) {
Michael Krufkyce1719a2008-04-02 18:59:48 -0300641 dprintk("%s() Checking VSB_8\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300642 reg = au8522_readreg(state, 0x4088);
Steven Toth836c2852008-06-21 19:32:41 -0300643 if ((reg & 0x03) == 0x03)
644 *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
Steven Toth265a6512008-04-18 21:34:00 -0300645 } else {
Michael Krufkyce1719a2008-04-02 18:59:48 -0300646 dprintk("%s() Checking QAM\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300647 reg = au8522_readreg(state, 0x4541);
Michael Krufkye059b0f2008-04-02 18:59:48 -0300648 if (reg & 0x80)
Steven Toth265a6512008-04-18 21:34:00 -0300649 *status |= FE_HAS_VITERBI;
Michael Krufkye059b0f2008-04-02 18:59:48 -0300650 if (reg & 0x20)
Steven Toth265a6512008-04-18 21:34:00 -0300651 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
652 }
653
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300654 switch (state->config->status_mode) {
Steven Toth265a6512008-04-18 21:34:00 -0300655 case AU8522_DEMODLOCKING:
Michael Krufkyce1719a2008-04-02 18:59:48 -0300656 dprintk("%s() DEMODLOCKING\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300657 if (*status & FE_HAS_VITERBI)
658 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
659 break;
660 case AU8522_TUNERLOCKING:
661 /* Get the tuner status */
Michael Krufkyce1719a2008-04-02 18:59:48 -0300662 dprintk("%s() TUNERLOCKING\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300663 if (fe->ops.tuner_ops.get_status) {
664 if (fe->ops.i2c_gate_ctrl)
665 fe->ops.i2c_gate_ctrl(fe, 1);
666
667 fe->ops.tuner_ops.get_status(fe, &tuner_status);
668
669 if (fe->ops.i2c_gate_ctrl)
670 fe->ops.i2c_gate_ctrl(fe, 0);
671 }
672 if (tuner_status)
673 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
674 break;
675 }
Michael Krufkyadeeac32008-04-05 20:55:14 -0300676 state->fe_status = *status;
677
678 if (*status & FE_HAS_LOCK)
679 /* turn on LED, if it isn't on already */
680 au8522_led_ctrl(state, -1);
681 else
682 /* turn off LED */
683 au8522_led_ctrl(state, 0);
Steven Toth265a6512008-04-18 21:34:00 -0300684
Michael Krufkyce1719a2008-04-02 18:59:48 -0300685 dprintk("%s() status 0x%08x\n", __func__, *status);
Steven Toth265a6512008-04-18 21:34:00 -0300686
687 return 0;
688}
689
Michael Krufkyadeeac32008-04-05 20:55:14 -0300690static int au8522_led_status(struct au8522_state *state, const u16 *snr)
691{
692 struct au8522_led_config *led_config = state->config->led_cfg;
693 int led;
694 u16 strong;
695
696 /* bail out if we cant control an LED */
697 if (!led_config)
698 return 0;
699
700 if (0 == (state->fe_status & FE_HAS_LOCK))
701 return au8522_led_ctrl(state, 0);
702 else if (state->current_modulation == QAM_256)
703 strong = led_config->qam256_strong;
704 else if (state->current_modulation == QAM_64)
705 strong = led_config->qam64_strong;
706 else /* (state->current_modulation == VSB_8) */
707 strong = led_config->vsb8_strong;
708
709 if (*snr >= strong)
710 led = 2;
711 else
712 led = 1;
713
714 if ((state->led_state) &&
715 (((strong < *snr) ? (*snr - strong) : (strong - *snr)) <= 10))
716 /* snr didn't change enough to bother
717 * changing the color of the led */
718 return 0;
719
720 return au8522_led_ctrl(state, led);
721}
722
Michael Krufkye059b0f2008-04-02 18:59:48 -0300723static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
Steven Toth265a6512008-04-18 21:34:00 -0300724{
Steven Tothf01699b2008-04-10 02:01:48 -0300725 struct au8522_state *state = fe->demodulator_priv;
726 int ret = -EINVAL;
727
Michael Krufkyce1719a2008-04-02 18:59:48 -0300728 dprintk("%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300729
Steven Tothf01699b2008-04-10 02:01:48 -0300730 if (state->current_modulation == QAM_256)
Michael Krufky0daa5de2008-04-10 04:24:56 -0300731 ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
732 ARRAY_SIZE(qam256_mse2snr_tab),
733 au8522_readreg(state, 0x4522),
734 snr);
Steven Tothf01699b2008-04-10 02:01:48 -0300735 else if (state->current_modulation == QAM_64)
Michael Krufky0daa5de2008-04-10 04:24:56 -0300736 ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
737 ARRAY_SIZE(qam64_mse2snr_tab),
738 au8522_readreg(state, 0x4522),
739 snr);
Steven Tothf01699b2008-04-10 02:01:48 -0300740 else /* VSB_8 */
Michael Krufky0daa5de2008-04-10 04:24:56 -0300741 ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
742 ARRAY_SIZE(vsb_mse2snr_tab),
743 au8522_readreg(state, 0x4311),
744 snr);
Steven Toth265a6512008-04-18 21:34:00 -0300745
Michael Krufkyadeeac32008-04-05 20:55:14 -0300746 if (state->config->led_cfg)
747 au8522_led_status(state, snr);
748
Steven Tothf01699b2008-04-10 02:01:48 -0300749 return ret;
Steven Toth265a6512008-04-18 21:34:00 -0300750}
751
Michael Krufkye059b0f2008-04-02 18:59:48 -0300752static int au8522_read_signal_strength(struct dvb_frontend *fe,
753 u16 *signal_strength)
Steven Toth265a6512008-04-18 21:34:00 -0300754{
755 return au8522_read_snr(fe, signal_strength);
756}
757
Michael Krufkye059b0f2008-04-02 18:59:48 -0300758static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
Steven Toth265a6512008-04-18 21:34:00 -0300759{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300760 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300761
Michael Krufky8973dc42008-04-05 23:08:08 -0300762 if (state->current_modulation == VSB_8)
763 *ucblocks = au8522_readreg(state, 0x4087);
764 else
765 *ucblocks = au8522_readreg(state, 0x4543);
Steven Toth265a6512008-04-18 21:34:00 -0300766
767 return 0;
768}
769
Michael Krufkye059b0f2008-04-02 18:59:48 -0300770static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
Steven Toth265a6512008-04-18 21:34:00 -0300771{
772 return au8522_read_ucblocks(fe, ber);
773}
774
Michael Krufkye059b0f2008-04-02 18:59:48 -0300775static int au8522_get_frontend(struct dvb_frontend *fe,
Steven Toth265a6512008-04-18 21:34:00 -0300776 struct dvb_frontend_parameters *p)
777{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300778 struct au8522_state *state = fe->demodulator_priv;
Steven Toth265a6512008-04-18 21:34:00 -0300779
780 p->frequency = state->current_frequency;
781 p->u.vsb.modulation = state->current_modulation;
782
783 return 0;
784}
785
Michael Krufkye059b0f2008-04-02 18:59:48 -0300786static int au8522_get_tune_settings(struct dvb_frontend *fe,
787 struct dvb_frontend_tune_settings *tune)
Steven Toth265a6512008-04-18 21:34:00 -0300788{
789 tune->min_delay_ms = 1000;
790 return 0;
791}
792
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300793static struct dvb_frontend_ops au8522_ops;
794
795int au8522_get_state(struct au8522_state **state, struct i2c_adapter *i2c,
796 u8 client_address)
797{
798 return hybrid_tuner_request_state(struct au8522_state, (*state),
799 hybrid_tuner_instance_list,
800 i2c, client_address, "au8522");
801}
802
803void au8522_release_state(struct au8522_state *state)
804{
805 if (state != NULL)
806 hybrid_tuner_release_state(state);
807}
808
809
Michael Krufkye059b0f2008-04-02 18:59:48 -0300810static void au8522_release(struct dvb_frontend *fe)
Steven Toth265a6512008-04-18 21:34:00 -0300811{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300812 struct au8522_state *state = fe->demodulator_priv;
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300813 au8522_release_state(state);
Steven Toth265a6512008-04-18 21:34:00 -0300814}
815
Michael Krufkye059b0f2008-04-02 18:59:48 -0300816struct dvb_frontend *au8522_attach(const struct au8522_config *config,
817 struct i2c_adapter *i2c)
Steven Toth265a6512008-04-18 21:34:00 -0300818{
Michael Krufkye059b0f2008-04-02 18:59:48 -0300819 struct au8522_state *state = NULL;
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300820 int instance;
Steven Toth265a6512008-04-18 21:34:00 -0300821
822 /* allocate memory for the internal state */
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300823 instance = au8522_get_state(&state, i2c, config->demod_address);
824 switch (instance) {
825 case 0:
826 dprintk("%s state allocation failed\n", __func__);
827 break;
828 case 1:
829 /* new demod instance */
830 dprintk("%s using new instance\n", __func__);
831 break;
832 default:
833 /* existing demod instance */
834 dprintk("%s using existing instance\n", __func__);
835 break;
836 }
Steven Toth265a6512008-04-18 21:34:00 -0300837
838 /* setup the state */
839 state->config = config;
840 state->i2c = i2c;
841 /* create dvb_frontend */
842 memcpy(&state->frontend.ops, &au8522_ops,
843 sizeof(struct dvb_frontend_ops));
844 state->frontend.demodulator_priv = state;
845
846 if (au8522_init(&state->frontend) != 0) {
847 printk(KERN_ERR "%s: Failed to initialize correctly\n",
Michael Krufkyce1719a2008-04-02 18:59:48 -0300848 __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300849 goto error;
850 }
851
852 /* Note: Leaving the I2C gate open here. */
853 au8522_i2c_gate_ctrl(&state->frontend, 1);
854
855 return &state->frontend;
856
857error:
Devin Heitmueller209fdf62009-03-11 03:00:36 -0300858 au8522_release_state(state);
Steven Toth265a6512008-04-18 21:34:00 -0300859 return NULL;
860}
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300861EXPORT_SYMBOL(au8522_attach);
Steven Toth265a6512008-04-18 21:34:00 -0300862
863static struct dvb_frontend_ops au8522_ops = {
864
865 .info = {
866 .name = "Auvitek AU8522 QAM/8VSB Frontend",
867 .type = FE_ATSC,
868 .frequency_min = 54000000,
869 .frequency_max = 858000000,
870 .frequency_stepsize = 62500,
871 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
872 },
873
874 .init = au8522_init,
Michael Krufky74d50722008-06-13 20:33:23 -0300875 .sleep = au8522_sleep,
Steven Toth265a6512008-04-18 21:34:00 -0300876 .i2c_gate_ctrl = au8522_i2c_gate_ctrl,
877 .set_frontend = au8522_set_frontend,
878 .get_frontend = au8522_get_frontend,
879 .get_tune_settings = au8522_get_tune_settings,
880 .read_status = au8522_read_status,
881 .read_ber = au8522_read_ber,
882 .read_signal_strength = au8522_read_signal_strength,
883 .read_snr = au8522_read_snr,
884 .read_ucblocks = au8522_read_ucblocks,
885 .release = au8522_release,
886};
887
888module_param(debug, int, 0644);
889MODULE_PARM_DESC(debug, "Enable verbose debug messages");
890
891MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
892MODULE_AUTHOR("Steven Toth");
893MODULE_LICENSE("GPL");