Commit | Line | Data |
---|---|---|
265a6510 ST |
1 | /* |
2 | Auvitek AU8522 QAM/8VSB demodulator driver | |
3 | ||
6d897616 | 4 | Copyright (C) 2008 Steven Toth <stoth@linuxtv.org> |
265a6510 ST |
5 | |
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" | |
265a6510 | 29 | #include "au8522.h" |
0c44bf36 | 30 | #include "au8522_priv.h" |
265a6510 | 31 | |
18d73c58 MCC |
32 | static int debug; |
33 | ||
209fdf66 DH |
34 | /* Despite the name "hybrid_tuner", the framework works just as well for |
35 | hybrid demodulators as well... */ | |
36 | static LIST_HEAD(hybrid_tuner_instance_list); | |
4ff5ed44 | 37 | static DEFINE_MUTEX(au8522_list_mutex); |
209fdf66 | 38 | |
62899a28 DH |
39 | #define dprintk(arg...)\ |
40 | do { if (debug)\ | |
41 | printk(arg);\ | |
18d73c58 | 42 | } while (0) |
265a6510 ST |
43 | |
44 | /* 16 bit registers, 8 bit values */ | |
0c44bf36 | 45 | int au8522_writereg(struct au8522_state *state, u16 reg, u8 data) |
265a6510 ST |
46 | { |
47 | int ret; | |
62899a28 | 48 | u8 buf[] = { (reg >> 8) | 0x80, reg & 0xff, data }; |
265a6510 ST |
49 | |
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, " | |
ce1719a6 | 57 | "ret == %i)\n", __func__, reg, data, ret); |
265a6510 ST |
58 | |
59 | return (ret != 1) ? -1 : 0; | |
60 | } | |
61 | ||
0c44bf36 | 62 | u8 au8522_readreg(struct au8522_state *state, u16 reg) |
265a6510 ST |
63 | { |
64 | int ret; | |
62899a28 DH |
65 | u8 b0[] = { (reg >> 8) | 0x40, reg & 0xff }; |
66 | u8 b1[] = { 0 }; | |
265a6510 | 67 | |
62899a28 | 68 | struct i2c_msg msg[] = { |
265a6510 ST |
69 | { .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) | |
ce1719a6 MK |
77 | printk(KERN_ERR "%s: readreg error (ret == %i)\n", |
78 | __func__, ret); | |
265a6510 ST |
79 | return b1[0]; |
80 | } | |
81 | ||
e059b0fa | 82 | static int au8522_i2c_gate_ctrl(struct dvb_frontend *fe, int enable) |
265a6510 | 83 | { |
e059b0fa | 84 | struct au8522_state *state = fe->demodulator_priv; |
265a6510 | 85 | |
ce1719a6 | 86 | dprintk("%s(%d)\n", __func__, enable); |
265a6510 ST |
87 | |
88 | if (enable) | |
89 | return au8522_writereg(state, 0x106, 1); | |
90 | else | |
91 | return au8522_writereg(state, 0x106, 0); | |
92 | } | |
93 | ||
0daa5de7 | 94 | struct mse2snr_tab { |
f01699b4 ST |
95 | u16 val; |
96 | u16 data; | |
0daa5de7 MK |
97 | }; |
98 | ||
99 | /* VSB SNR lookup table */ | |
100 | static struct mse2snr_tab vsb_mse2snr_tab[] = { | |
f01699b4 ST |
101 | { 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 */ | |
0daa5de7 | 134 | static struct mse2snr_tab qam64_mse2snr_tab[] = { |
f01699b4 ST |
135 | { 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 */ | |
0daa5de7 | 215 | static struct mse2snr_tab qam256_mse2snr_tab[] = { |
f01699b4 ST |
216 | { 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 | ||
18d73c58 MCC |
282 | static int au8522_mse2snr_lookup(struct mse2snr_tab *tab, int sz, int mse, |
283 | u16 *snr) | |
f01699b4 ST |
284 | { |
285 | int i, ret = -EINVAL; | |
286 | dprintk("%s()\n", __func__); | |
287 | ||
0daa5de7 MK |
288 | for (i = 0; i < sz; i++) { |
289 | if (mse < tab[i].val) { | |
290 | *snr = tab[i].data; | |
f01699b4 ST |
291 | ret = 0; |
292 | break; | |
293 | } | |
294 | } | |
295 | dprintk("%s() snr=%d\n", __func__, *snr); | |
296 | return ret; | |
297 | } | |
298 | ||
2e7acd75 MK |
299 | static int au8522_set_if(struct dvb_frontend *fe, enum au8522_if_freq if_freq) |
300 | { | |
301 | struct au8522_state *state = fe->demodulator_priv; | |
df76de09 MK |
302 | u8 r0b5, r0b6, r0b7; |
303 | char *ifmhz; | |
2e7acd75 MK |
304 | |
305 | switch (if_freq) { | |
306 | case AU8522_IF_3_25MHZ: | |
df76de09 MK |
307 | ifmhz = "3.25"; |
308 | r0b5 = 0x00; | |
309 | r0b6 = 0x3d; | |
310 | r0b7 = 0xa0; | |
2e7acd75 MK |
311 | break; |
312 | case AU8522_IF_4MHZ: | |
df76de09 MK |
313 | ifmhz = "4.00"; |
314 | r0b5 = 0x00; | |
315 | r0b6 = 0x4b; | |
316 | r0b7 = 0xd9; | |
2e7acd75 MK |
317 | break; |
318 | case AU8522_IF_6MHZ: | |
df76de09 MK |
319 | ifmhz = "6.00"; |
320 | r0b5 = 0xfb; | |
321 | r0b6 = 0x8e; | |
322 | r0b7 = 0x39; | |
2e7acd75 MK |
323 | break; |
324 | default: | |
325 | dprintk("%s() IF Frequency not supported\n", __func__); | |
326 | return -EINVAL; | |
327 | } | |
df76de09 MK |
328 | 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 | ||
2e7acd75 MK |
333 | return 0; |
334 | } | |
335 | ||
f01699b4 ST |
336 | /* VSB Modulation table */ |
337 | static 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 }, | |
f01699b4 ST |
366 | { 0x80a4, 0xe8 }, |
367 | { 0x8231, 0x13 }, | |
368 | }; | |
369 | ||
370 | /* QAM Modulation table */ | |
371 | static struct { | |
372 | u16 reg; | |
373 | u16 data; | |
374 | } QAM_mod_tab[] = { | |
375 | { 0x80a3, 0x09 }, | |
376 | { 0x80a4, 0x00 }, | |
377 | { 0x8081, 0xc4 }, | |
378 | { 0x80a5, 0x40 }, | |
f01699b4 ST |
379 | { 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 | ||
e059b0fa MK |
449 | static int au8522_enable_modulation(struct dvb_frontend *fe, |
450 | fe_modulation_t m) | |
265a6510 | 451 | { |
e059b0fa | 452 | struct au8522_state *state = fe->demodulator_priv; |
f01699b4 | 453 | int i; |
265a6510 | 454 | |
ce1719a6 | 455 | dprintk("%s(0x%08x)\n", __func__, m); |
265a6510 | 456 | |
18d73c58 | 457 | switch (m) { |
265a6510 | 458 | case VSB_8: |
ce1719a6 | 459 | dprintk("%s() VSB_8\n", __func__); |
f01699b4 ST |
460 | 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); | |
2e7acd75 | 464 | au8522_set_if(fe, state->config->vsb_if); |
265a6510 ST |
465 | break; |
466 | case QAM_64: | |
467 | case QAM_256: | |
f01699b4 ST |
468 | 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); | |
2e7acd75 | 473 | au8522_set_if(fe, state->config->qam_if); |
265a6510 ST |
474 | break; |
475 | default: | |
ce1719a6 | 476 | dprintk("%s() Invalid modulation\n", __func__); |
265a6510 ST |
477 | 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 */ | |
e059b0fa MK |
486 | static int au8522_set_frontend(struct dvb_frontend *fe, |
487 | struct dvb_frontend_parameters *p) | |
265a6510 | 488 | { |
e059b0fa | 489 | struct au8522_state *state = fe->demodulator_priv; |
74d50724 | 490 | int ret = -EINVAL; |
265a6510 | 491 | |
ce1719a6 | 492 | dprintk("%s(frequency=%d)\n", __func__, p->frequency); |
265a6510 | 493 | |
74d50724 MK |
494 | if ((state->current_frequency == p->frequency) && |
495 | (state->current_modulation == p->u.vsb.modulation)) | |
496 | return 0; | |
265a6510 ST |
497 | |
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) { | |
18d73c58 MCC |
504 | if (fe->ops.i2c_gate_ctrl) |
505 | fe->ops.i2c_gate_ctrl(fe, 1); | |
74d50724 | 506 | ret = fe->ops.tuner_ops.set_params(fe, p); |
18d73c58 MCC |
507 | if (fe->ops.i2c_gate_ctrl) |
508 | fe->ops.i2c_gate_ctrl(fe, 0); | |
265a6510 ST |
509 | } |
510 | ||
74d50724 MK |
511 | if (ret < 0) |
512 | return ret; | |
513 | ||
514 | state->current_frequency = p->frequency; | |
515 | ||
265a6510 ST |
516 | return 0; |
517 | } | |
518 | ||
519 | /* Reset the demod hardware and reset all of the configuration registers | |
520 | to a default state. */ | |
0c44bf36 | 521 | int au8522_init(struct dvb_frontend *fe) |
265a6510 | 522 | { |
e059b0fa | 523 | struct au8522_state *state = fe->demodulator_priv; |
ce1719a6 | 524 | dprintk("%s()\n", __func__); |
265a6510 ST |
525 | |
526 | au8522_writereg(state, 0xa4, 1 << 5); | |
527 | ||
528 | au8522_i2c_gate_ctrl(fe, 1); | |
529 | ||
530 | return 0; | |
531 | } | |
532 | ||
adeeac3b MK |
533 | static 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 | */ | |
563 | static 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 | ||
0c44bf36 | 617 | int au8522_sleep(struct dvb_frontend *fe) |
74d50724 MK |
618 | { |
619 | struct au8522_state *state = fe->demodulator_priv; | |
620 | dprintk("%s()\n", __func__); | |
621 | ||
adeeac3b MK |
622 | /* turn off led */ |
623 | au8522_led_ctrl(state, 0); | |
624 | ||
7bf63eda DH |
625 | /* Power down the chip */ |
626 | au8522_writereg(state, 0xa4, 1 << 5); | |
627 | ||
74d50724 MK |
628 | state->current_frequency = 0; |
629 | ||
630 | return 0; | |
631 | } | |
632 | ||
e059b0fa | 633 | static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status) |
265a6510 | 634 | { |
e059b0fa | 635 | struct au8522_state *state = fe->demodulator_priv; |
265a6510 ST |
636 | u8 reg; |
637 | u32 tuner_status = 0; | |
638 | ||
639 | *status = 0; | |
640 | ||
641 | if (state->current_modulation == VSB_8) { | |
ce1719a6 | 642 | dprintk("%s() Checking VSB_8\n", __func__); |
265a6510 | 643 | reg = au8522_readreg(state, 0x4088); |
836c2858 ST |
644 | if ((reg & 0x03) == 0x03) |
645 | *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI; | |
265a6510 | 646 | } else { |
ce1719a6 | 647 | dprintk("%s() Checking QAM\n", __func__); |
265a6510 | 648 | reg = au8522_readreg(state, 0x4541); |
e059b0fa | 649 | if (reg & 0x80) |
265a6510 | 650 | *status |= FE_HAS_VITERBI; |
e059b0fa | 651 | if (reg & 0x20) |
265a6510 ST |
652 | *status |= FE_HAS_LOCK | FE_HAS_SYNC; |
653 | } | |
654 | ||
18d73c58 | 655 | switch (state->config->status_mode) { |
265a6510 | 656 | case AU8522_DEMODLOCKING: |
ce1719a6 | 657 | dprintk("%s() DEMODLOCKING\n", __func__); |
265a6510 ST |
658 | if (*status & FE_HAS_VITERBI) |
659 | *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL; | |
660 | break; | |
661 | case AU8522_TUNERLOCKING: | |
662 | /* Get the tuner status */ | |
ce1719a6 | 663 | dprintk("%s() TUNERLOCKING\n", __func__); |
265a6510 ST |
664 | 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 | } | |
adeeac3b MK |
677 | 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); | |
265a6510 | 685 | |
ce1719a6 | 686 | dprintk("%s() status 0x%08x\n", __func__, *status); |
265a6510 ST |
687 | |
688 | return 0; | |
689 | } | |
690 | ||
adeeac3b MK |
691 | static 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 | ||
f01699b4 | 724 | static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr) |
fb8152cb MK |
725 | { |
726 | struct au8522_state *state = fe->demodulator_priv; | |
f01699b4 | 727 | int ret = -EINVAL; |
fb8152cb | 728 | |
ce1719a6 | 729 | dprintk("%s()\n", __func__); |
265a6510 | 730 | |
f01699b4 | 731 | if (state->current_modulation == QAM_256) |
0daa5de7 MK |
732 | ret = au8522_mse2snr_lookup(qam256_mse2snr_tab, |
733 | ARRAY_SIZE(qam256_mse2snr_tab), | |
734 | au8522_readreg(state, 0x4522), | |
735 | snr); | |
f01699b4 | 736 | else if (state->current_modulation == QAM_64) |
0daa5de7 MK |
737 | ret = au8522_mse2snr_lookup(qam64_mse2snr_tab, |
738 | ARRAY_SIZE(qam64_mse2snr_tab), | |
739 | au8522_readreg(state, 0x4522), | |
740 | snr); | |
f01699b4 | 741 | else /* VSB_8 */ |
0daa5de7 MK |
742 | ret = au8522_mse2snr_lookup(vsb_mse2snr_tab, |
743 | ARRAY_SIZE(vsb_mse2snr_tab), | |
744 | au8522_readreg(state, 0x4311), | |
745 | snr); | |
f01699b4 | 746 | |
adeeac3b MK |
747 | if (state->config->led_cfg) |
748 | au8522_led_status(state, snr); | |
749 | ||
f01699b4 | 750 | return ret; |
265a6510 ST |
751 | } |
752 | ||
e059b0fa MK |
753 | static int au8522_read_signal_strength(struct dvb_frontend *fe, |
754 | u16 *signal_strength) | |
265a6510 ST |
755 | { |
756 | return au8522_read_snr(fe, signal_strength); | |
757 | } | |
758 | ||
e059b0fa | 759 | static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) |
265a6510 | 760 | { |
e059b0fa | 761 | struct au8522_state *state = fe->demodulator_priv; |
265a6510 | 762 | |
8973dc4b MK |
763 | if (state->current_modulation == VSB_8) |
764 | *ucblocks = au8522_readreg(state, 0x4087); | |
765 | else | |
766 | *ucblocks = au8522_readreg(state, 0x4543); | |
265a6510 ST |
767 | |
768 | return 0; | |
769 | } | |
770 | ||
e059b0fa | 771 | static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber) |
265a6510 ST |
772 | { |
773 | return au8522_read_ucblocks(fe, ber); | |
774 | } | |
775 | ||
e059b0fa | 776 | static int au8522_get_frontend(struct dvb_frontend *fe, |
265a6510 ST |
777 | struct dvb_frontend_parameters *p) |
778 | { | |
e059b0fa | 779 | struct au8522_state *state = fe->demodulator_priv; |
265a6510 ST |
780 | |
781 | p->frequency = state->current_frequency; | |
782 | p->u.vsb.modulation = state->current_modulation; | |
783 | ||
784 | return 0; | |
785 | } | |
786 | ||
e059b0fa MK |
787 | static int au8522_get_tune_settings(struct dvb_frontend *fe, |
788 | struct dvb_frontend_tune_settings *tune) | |
265a6510 ST |
789 | { |
790 | tune->min_delay_ms = 1000; | |
791 | return 0; | |
792 | } | |
793 | ||
209fdf66 DH |
794 | static struct dvb_frontend_ops au8522_ops; |
795 | ||
796 | int au8522_get_state(struct au8522_state **state, struct i2c_adapter *i2c, | |
797 | u8 client_address) | |
798 | { | |
4ff5ed44 DH |
799 | 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; | |
209fdf66 DH |
808 | } |
809 | ||
810 | void au8522_release_state(struct au8522_state *state) | |
811 | { | |
4ff5ed44 | 812 | mutex_lock(&au8522_list_mutex); |
209fdf66 DH |
813 | if (state != NULL) |
814 | hybrid_tuner_release_state(state); | |
4ff5ed44 | 815 | mutex_unlock(&au8522_list_mutex); |
209fdf66 DH |
816 | } |
817 | ||
818 | ||
e059b0fa | 819 | static void au8522_release(struct dvb_frontend *fe) |
265a6510 | 820 | { |
e059b0fa | 821 | struct au8522_state *state = fe->demodulator_priv; |
209fdf66 | 822 | au8522_release_state(state); |
265a6510 ST |
823 | } |
824 | ||
e059b0fa MK |
825 | struct dvb_frontend *au8522_attach(const struct au8522_config *config, |
826 | struct i2c_adapter *i2c) | |
265a6510 | 827 | { |
e059b0fa | 828 | struct au8522_state *state = NULL; |
209fdf66 | 829 | int instance; |
265a6510 ST |
830 | |
831 | /* allocate memory for the internal state */ | |
209fdf66 DH |
832 | 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 | } | |
265a6510 ST |
846 | |
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", | |
ce1719a6 | 857 | __func__); |
265a6510 ST |
858 | 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 | ||
866 | error: | |
209fdf66 | 867 | au8522_release_state(state); |
265a6510 ST |
868 | return NULL; |
869 | } | |
18d73c58 | 870 | EXPORT_SYMBOL(au8522_attach); |
265a6510 ST |
871 | |
872 | static 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, | |
74d50724 | 884 | .sleep = au8522_sleep, |
265a6510 ST |
885 | .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 | ||
897 | module_param(debug, int, 0644); | |
898 | MODULE_PARM_DESC(debug, "Enable verbose debug messages"); | |
899 | ||
900 | MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver"); | |
901 | MODULE_AUTHOR("Steven Toth"); | |
902 | MODULE_LICENSE("GPL"); |