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 | ||
34 | #define dprintk(arg...) do { \ | |
35 | if (debug) \ | |
353a2761 | 36 | printk(arg); \ |
18d73c58 | 37 | } while (0) |
265a6510 ST |
38 | |
39 | /* 16 bit registers, 8 bit values */ | |
0c44bf36 | 40 | int au8522_writereg(struct au8522_state *state, u16 reg, u8 data) |
265a6510 ST |
41 | { |
42 | int ret; | |
43 | u8 buf [] = { reg >> 8, reg & 0xff, data }; | |
44 | ||
45 | struct i2c_msg msg = { .addr = state->config->demod_address, | |
46 | .flags = 0, .buf = buf, .len = 3 }; | |
47 | ||
48 | ret = i2c_transfer(state->i2c, &msg, 1); | |
49 | ||
50 | if (ret != 1) | |
51 | printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, " | |
ce1719a6 | 52 | "ret == %i)\n", __func__, reg, data, ret); |
265a6510 ST |
53 | |
54 | return (ret != 1) ? -1 : 0; | |
55 | } | |
56 | ||
0c44bf36 | 57 | u8 au8522_readreg(struct au8522_state *state, u16 reg) |
265a6510 ST |
58 | { |
59 | int ret; | |
60 | u8 b0 [] = { reg >> 8, reg & 0xff }; | |
61 | u8 b1 [] = { 0 }; | |
62 | ||
63 | struct i2c_msg msg [] = { | |
64 | { .addr = state->config->demod_address, .flags = 0, | |
65 | .buf = b0, .len = 2 }, | |
66 | { .addr = state->config->demod_address, .flags = I2C_M_RD, | |
67 | .buf = b1, .len = 1 } }; | |
68 | ||
69 | ret = i2c_transfer(state->i2c, msg, 2); | |
70 | ||
71 | if (ret != 2) | |
ce1719a6 MK |
72 | printk(KERN_ERR "%s: readreg error (ret == %i)\n", |
73 | __func__, ret); | |
265a6510 ST |
74 | return b1[0]; |
75 | } | |
76 | ||
e059b0fa | 77 | static int au8522_i2c_gate_ctrl(struct dvb_frontend *fe, int enable) |
265a6510 | 78 | { |
e059b0fa | 79 | struct au8522_state *state = fe->demodulator_priv; |
265a6510 | 80 | |
ce1719a6 | 81 | dprintk("%s(%d)\n", __func__, enable); |
265a6510 ST |
82 | |
83 | if (enable) | |
84 | return au8522_writereg(state, 0x106, 1); | |
85 | else | |
86 | return au8522_writereg(state, 0x106, 0); | |
87 | } | |
88 | ||
0daa5de7 | 89 | struct mse2snr_tab { |
f01699b4 ST |
90 | u16 val; |
91 | u16 data; | |
0daa5de7 MK |
92 | }; |
93 | ||
94 | /* VSB SNR lookup table */ | |
95 | static struct mse2snr_tab vsb_mse2snr_tab[] = { | |
f01699b4 ST |
96 | { 0, 270 }, |
97 | { 2, 250 }, | |
98 | { 3, 240 }, | |
99 | { 5, 230 }, | |
100 | { 7, 220 }, | |
101 | { 9, 210 }, | |
102 | { 12, 200 }, | |
103 | { 13, 195 }, | |
104 | { 15, 190 }, | |
105 | { 17, 185 }, | |
106 | { 19, 180 }, | |
107 | { 21, 175 }, | |
108 | { 24, 170 }, | |
109 | { 27, 165 }, | |
110 | { 31, 160 }, | |
111 | { 32, 158 }, | |
112 | { 33, 156 }, | |
113 | { 36, 152 }, | |
114 | { 37, 150 }, | |
115 | { 39, 148 }, | |
116 | { 40, 146 }, | |
117 | { 41, 144 }, | |
118 | { 43, 142 }, | |
119 | { 44, 140 }, | |
120 | { 48, 135 }, | |
121 | { 50, 130 }, | |
122 | { 43, 142 }, | |
123 | { 53, 125 }, | |
124 | { 56, 120 }, | |
125 | { 256, 115 }, | |
126 | }; | |
127 | ||
128 | /* QAM64 SNR lookup table */ | |
0daa5de7 | 129 | static struct mse2snr_tab qam64_mse2snr_tab[] = { |
f01699b4 ST |
130 | { 15, 0 }, |
131 | { 16, 290 }, | |
132 | { 17, 288 }, | |
133 | { 18, 286 }, | |
134 | { 19, 284 }, | |
135 | { 20, 282 }, | |
136 | { 21, 281 }, | |
137 | { 22, 279 }, | |
138 | { 23, 277 }, | |
139 | { 24, 275 }, | |
140 | { 25, 273 }, | |
141 | { 26, 271 }, | |
142 | { 27, 269 }, | |
143 | { 28, 268 }, | |
144 | { 29, 266 }, | |
145 | { 30, 264 }, | |
146 | { 31, 262 }, | |
147 | { 32, 260 }, | |
148 | { 33, 259 }, | |
149 | { 34, 258 }, | |
150 | { 35, 256 }, | |
151 | { 36, 255 }, | |
152 | { 37, 254 }, | |
153 | { 38, 252 }, | |
154 | { 39, 251 }, | |
155 | { 40, 250 }, | |
156 | { 41, 249 }, | |
157 | { 42, 248 }, | |
158 | { 43, 246 }, | |
159 | { 44, 245 }, | |
160 | { 45, 244 }, | |
161 | { 46, 242 }, | |
162 | { 47, 241 }, | |
163 | { 48, 240 }, | |
164 | { 50, 239 }, | |
165 | { 51, 238 }, | |
166 | { 53, 237 }, | |
167 | { 54, 236 }, | |
168 | { 56, 235 }, | |
169 | { 57, 234 }, | |
170 | { 59, 233 }, | |
171 | { 60, 232 }, | |
172 | { 62, 231 }, | |
173 | { 63, 230 }, | |
174 | { 65, 229 }, | |
175 | { 67, 228 }, | |
176 | { 68, 227 }, | |
177 | { 70, 226 }, | |
178 | { 71, 225 }, | |
179 | { 73, 224 }, | |
180 | { 74, 223 }, | |
181 | { 76, 222 }, | |
182 | { 78, 221 }, | |
183 | { 80, 220 }, | |
184 | { 82, 219 }, | |
185 | { 85, 218 }, | |
186 | { 88, 217 }, | |
187 | { 90, 216 }, | |
188 | { 92, 215 }, | |
189 | { 93, 214 }, | |
190 | { 94, 212 }, | |
191 | { 95, 211 }, | |
192 | { 97, 210 }, | |
193 | { 99, 209 }, | |
194 | { 101, 208 }, | |
195 | { 102, 207 }, | |
196 | { 104, 206 }, | |
197 | { 107, 205 }, | |
198 | { 111, 204 }, | |
199 | { 114, 203 }, | |
200 | { 118, 202 }, | |
201 | { 122, 201 }, | |
202 | { 125, 200 }, | |
203 | { 128, 199 }, | |
204 | { 130, 198 }, | |
205 | { 132, 197 }, | |
206 | { 256, 190 }, | |
207 | }; | |
208 | ||
209 | /* QAM256 SNR lookup table */ | |
0daa5de7 | 210 | static struct mse2snr_tab qam256_mse2snr_tab[] = { |
f01699b4 ST |
211 | { 16, 0 }, |
212 | { 17, 400 }, | |
213 | { 18, 398 }, | |
214 | { 19, 396 }, | |
215 | { 20, 394 }, | |
216 | { 21, 392 }, | |
217 | { 22, 390 }, | |
218 | { 23, 388 }, | |
219 | { 24, 386 }, | |
220 | { 25, 384 }, | |
221 | { 26, 382 }, | |
222 | { 27, 380 }, | |
223 | { 28, 379 }, | |
224 | { 29, 378 }, | |
225 | { 30, 377 }, | |
226 | { 31, 376 }, | |
227 | { 32, 375 }, | |
228 | { 33, 374 }, | |
229 | { 34, 373 }, | |
230 | { 35, 372 }, | |
231 | { 36, 371 }, | |
232 | { 37, 370 }, | |
233 | { 38, 362 }, | |
234 | { 39, 354 }, | |
235 | { 40, 346 }, | |
236 | { 41, 338 }, | |
237 | { 42, 330 }, | |
238 | { 43, 328 }, | |
239 | { 44, 326 }, | |
240 | { 45, 324 }, | |
241 | { 46, 322 }, | |
242 | { 47, 320 }, | |
243 | { 48, 319 }, | |
244 | { 49, 318 }, | |
245 | { 50, 317 }, | |
246 | { 51, 316 }, | |
247 | { 52, 315 }, | |
248 | { 53, 314 }, | |
249 | { 54, 313 }, | |
250 | { 55, 312 }, | |
251 | { 56, 311 }, | |
252 | { 57, 310 }, | |
253 | { 58, 308 }, | |
254 | { 59, 306 }, | |
255 | { 60, 304 }, | |
256 | { 61, 302 }, | |
257 | { 62, 300 }, | |
258 | { 63, 298 }, | |
259 | { 65, 295 }, | |
260 | { 68, 294 }, | |
261 | { 70, 293 }, | |
262 | { 73, 292 }, | |
263 | { 76, 291 }, | |
264 | { 78, 290 }, | |
265 | { 79, 289 }, | |
266 | { 81, 288 }, | |
267 | { 82, 287 }, | |
268 | { 83, 286 }, | |
269 | { 84, 285 }, | |
270 | { 85, 284 }, | |
271 | { 86, 283 }, | |
272 | { 88, 282 }, | |
273 | { 89, 281 }, | |
274 | { 256, 280 }, | |
275 | }; | |
276 | ||
18d73c58 MCC |
277 | static int au8522_mse2snr_lookup(struct mse2snr_tab *tab, int sz, int mse, |
278 | u16 *snr) | |
f01699b4 ST |
279 | { |
280 | int i, ret = -EINVAL; | |
281 | dprintk("%s()\n", __func__); | |
282 | ||
0daa5de7 MK |
283 | for (i = 0; i < sz; i++) { |
284 | if (mse < tab[i].val) { | |
285 | *snr = tab[i].data; | |
f01699b4 ST |
286 | ret = 0; |
287 | break; | |
288 | } | |
289 | } | |
290 | dprintk("%s() snr=%d\n", __func__, *snr); | |
291 | return ret; | |
292 | } | |
293 | ||
2e7acd75 MK |
294 | static int au8522_set_if(struct dvb_frontend *fe, enum au8522_if_freq if_freq) |
295 | { | |
296 | struct au8522_state *state = fe->demodulator_priv; | |
df76de09 MK |
297 | u8 r0b5, r0b6, r0b7; |
298 | char *ifmhz; | |
2e7acd75 MK |
299 | |
300 | switch (if_freq) { | |
301 | case AU8522_IF_3_25MHZ: | |
df76de09 MK |
302 | ifmhz = "3.25"; |
303 | r0b5 = 0x00; | |
304 | r0b6 = 0x3d; | |
305 | r0b7 = 0xa0; | |
2e7acd75 MK |
306 | break; |
307 | case AU8522_IF_4MHZ: | |
df76de09 MK |
308 | ifmhz = "4.00"; |
309 | r0b5 = 0x00; | |
310 | r0b6 = 0x4b; | |
311 | r0b7 = 0xd9; | |
2e7acd75 MK |
312 | break; |
313 | case AU8522_IF_6MHZ: | |
df76de09 MK |
314 | ifmhz = "6.00"; |
315 | r0b5 = 0xfb; | |
316 | r0b6 = 0x8e; | |
317 | r0b7 = 0x39; | |
2e7acd75 MK |
318 | break; |
319 | default: | |
320 | dprintk("%s() IF Frequency not supported\n", __func__); | |
321 | return -EINVAL; | |
322 | } | |
df76de09 MK |
323 | dprintk("%s() %s MHz\n", __func__, ifmhz); |
324 | au8522_writereg(state, 0x80b5, r0b5); | |
325 | au8522_writereg(state, 0x80b6, r0b6); | |
326 | au8522_writereg(state, 0x80b7, r0b7); | |
327 | ||
2e7acd75 MK |
328 | return 0; |
329 | } | |
330 | ||
f01699b4 ST |
331 | /* VSB Modulation table */ |
332 | static struct { | |
333 | u16 reg; | |
334 | u16 data; | |
335 | } VSB_mod_tab[] = { | |
336 | { 0x8090, 0x84 }, | |
337 | { 0x4092, 0x11 }, | |
338 | { 0x2005, 0x00 }, | |
339 | { 0x8091, 0x80 }, | |
340 | { 0x80a3, 0x0c }, | |
341 | { 0x80a4, 0xe8 }, | |
342 | { 0x8081, 0xc4 }, | |
343 | { 0x80a5, 0x40 }, | |
344 | { 0x80a7, 0x40 }, | |
345 | { 0x80a6, 0x67 }, | |
346 | { 0x8262, 0x20 }, | |
347 | { 0x821c, 0x30 }, | |
348 | { 0x80d8, 0x1a }, | |
349 | { 0x8227, 0xa0 }, | |
350 | { 0x8121, 0xff }, | |
351 | { 0x80a8, 0xf0 }, | |
352 | { 0x80a9, 0x05 }, | |
353 | { 0x80aa, 0x77 }, | |
354 | { 0x80ab, 0xf0 }, | |
355 | { 0x80ac, 0x05 }, | |
356 | { 0x80ad, 0x77 }, | |
357 | { 0x80ae, 0x41 }, | |
358 | { 0x80af, 0x66 }, | |
359 | { 0x821b, 0xcc }, | |
360 | { 0x821d, 0x80 }, | |
f01699b4 ST |
361 | { 0x80a4, 0xe8 }, |
362 | { 0x8231, 0x13 }, | |
363 | }; | |
364 | ||
365 | /* QAM Modulation table */ | |
366 | static struct { | |
367 | u16 reg; | |
368 | u16 data; | |
369 | } QAM_mod_tab[] = { | |
370 | { 0x80a3, 0x09 }, | |
371 | { 0x80a4, 0x00 }, | |
372 | { 0x8081, 0xc4 }, | |
373 | { 0x80a5, 0x40 }, | |
f01699b4 ST |
374 | { 0x80aa, 0x77 }, |
375 | { 0x80ad, 0x77 }, | |
376 | { 0x80a6, 0x67 }, | |
377 | { 0x8262, 0x20 }, | |
378 | { 0x821c, 0x30 }, | |
379 | { 0x80b8, 0x3e }, | |
380 | { 0x80b9, 0xf0 }, | |
381 | { 0x80ba, 0x01 }, | |
382 | { 0x80bb, 0x18 }, | |
383 | { 0x80bc, 0x50 }, | |
384 | { 0x80bd, 0x00 }, | |
385 | { 0x80be, 0xea }, | |
386 | { 0x80bf, 0xef }, | |
387 | { 0x80c0, 0xfc }, | |
388 | { 0x80c1, 0xbd }, | |
389 | { 0x80c2, 0x1f }, | |
390 | { 0x80c3, 0xfc }, | |
391 | { 0x80c4, 0xdd }, | |
392 | { 0x80c5, 0xaf }, | |
393 | { 0x80c6, 0x00 }, | |
394 | { 0x80c7, 0x38 }, | |
395 | { 0x80c8, 0x30 }, | |
396 | { 0x80c9, 0x05 }, | |
397 | { 0x80ca, 0x4a }, | |
398 | { 0x80cb, 0xd0 }, | |
399 | { 0x80cc, 0x01 }, | |
400 | { 0x80cd, 0xd9 }, | |
401 | { 0x80ce, 0x6f }, | |
402 | { 0x80cf, 0xf9 }, | |
403 | { 0x80d0, 0x70 }, | |
404 | { 0x80d1, 0xdf }, | |
405 | { 0x80d2, 0xf7 }, | |
406 | { 0x80d3, 0xc2 }, | |
407 | { 0x80d4, 0xdf }, | |
408 | { 0x80d5, 0x02 }, | |
409 | { 0x80d6, 0x9a }, | |
410 | { 0x80d7, 0xd0 }, | |
411 | { 0x8250, 0x0d }, | |
412 | { 0x8251, 0xcd }, | |
413 | { 0x8252, 0xe0 }, | |
414 | { 0x8253, 0x05 }, | |
415 | { 0x8254, 0xa7 }, | |
416 | { 0x8255, 0xff }, | |
417 | { 0x8256, 0xed }, | |
418 | { 0x8257, 0x5b }, | |
419 | { 0x8258, 0xae }, | |
420 | { 0x8259, 0xe6 }, | |
421 | { 0x825a, 0x3d }, | |
422 | { 0x825b, 0x0f }, | |
423 | { 0x825c, 0x0d }, | |
424 | { 0x825d, 0xea }, | |
425 | { 0x825e, 0xf2 }, | |
426 | { 0x825f, 0x51 }, | |
427 | { 0x8260, 0xf5 }, | |
428 | { 0x8261, 0x06 }, | |
429 | { 0x821a, 0x00 }, | |
430 | { 0x8546, 0x40 }, | |
431 | { 0x8210, 0x26 }, | |
432 | { 0x8211, 0xf6 }, | |
433 | { 0x8212, 0x84 }, | |
434 | { 0x8213, 0x02 }, | |
435 | { 0x8502, 0x01 }, | |
436 | { 0x8121, 0x04 }, | |
437 | { 0x8122, 0x04 }, | |
438 | { 0x852e, 0x10 }, | |
439 | { 0x80a4, 0xca }, | |
440 | { 0x80a7, 0x40 }, | |
441 | { 0x8526, 0x01 }, | |
442 | }; | |
443 | ||
e059b0fa MK |
444 | static int au8522_enable_modulation(struct dvb_frontend *fe, |
445 | fe_modulation_t m) | |
265a6510 | 446 | { |
e059b0fa | 447 | struct au8522_state *state = fe->demodulator_priv; |
f01699b4 | 448 | int i; |
265a6510 | 449 | |
ce1719a6 | 450 | dprintk("%s(0x%08x)\n", __func__, m); |
265a6510 | 451 | |
18d73c58 | 452 | switch (m) { |
265a6510 | 453 | case VSB_8: |
ce1719a6 | 454 | dprintk("%s() VSB_8\n", __func__); |
f01699b4 ST |
455 | for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++) |
456 | au8522_writereg(state, | |
457 | VSB_mod_tab[i].reg, | |
458 | VSB_mod_tab[i].data); | |
2e7acd75 | 459 | au8522_set_if(fe, state->config->vsb_if); |
265a6510 ST |
460 | break; |
461 | case QAM_64: | |
462 | case QAM_256: | |
f01699b4 ST |
463 | dprintk("%s() QAM 64/256\n", __func__); |
464 | for (i = 0; i < ARRAY_SIZE(QAM_mod_tab); i++) | |
465 | au8522_writereg(state, | |
466 | QAM_mod_tab[i].reg, | |
467 | QAM_mod_tab[i].data); | |
2e7acd75 | 468 | au8522_set_if(fe, state->config->qam_if); |
265a6510 ST |
469 | break; |
470 | default: | |
ce1719a6 | 471 | dprintk("%s() Invalid modulation\n", __func__); |
265a6510 ST |
472 | return -EINVAL; |
473 | } | |
474 | ||
475 | state->current_modulation = m; | |
476 | ||
477 | return 0; | |
478 | } | |
479 | ||
480 | /* Talk to the demod, set the FEC, GUARD, QAM settings etc */ | |
e059b0fa MK |
481 | static int au8522_set_frontend(struct dvb_frontend *fe, |
482 | struct dvb_frontend_parameters *p) | |
265a6510 | 483 | { |
e059b0fa | 484 | struct au8522_state *state = fe->demodulator_priv; |
74d50724 | 485 | int ret = -EINVAL; |
265a6510 | 486 | |
ce1719a6 | 487 | dprintk("%s(frequency=%d)\n", __func__, p->frequency); |
265a6510 | 488 | |
74d50724 MK |
489 | if ((state->current_frequency == p->frequency) && |
490 | (state->current_modulation == p->u.vsb.modulation)) | |
491 | return 0; | |
265a6510 ST |
492 | |
493 | au8522_enable_modulation(fe, p->u.vsb.modulation); | |
494 | ||
495 | /* Allow the demod to settle */ | |
496 | msleep(100); | |
497 | ||
498 | if (fe->ops.tuner_ops.set_params) { | |
18d73c58 MCC |
499 | if (fe->ops.i2c_gate_ctrl) |
500 | fe->ops.i2c_gate_ctrl(fe, 1); | |
74d50724 | 501 | ret = fe->ops.tuner_ops.set_params(fe, p); |
18d73c58 MCC |
502 | if (fe->ops.i2c_gate_ctrl) |
503 | fe->ops.i2c_gate_ctrl(fe, 0); | |
265a6510 ST |
504 | } |
505 | ||
74d50724 MK |
506 | if (ret < 0) |
507 | return ret; | |
508 | ||
509 | state->current_frequency = p->frequency; | |
510 | ||
265a6510 ST |
511 | return 0; |
512 | } | |
513 | ||
514 | /* Reset the demod hardware and reset all of the configuration registers | |
515 | to a default state. */ | |
0c44bf36 | 516 | int au8522_init(struct dvb_frontend *fe) |
265a6510 | 517 | { |
e059b0fa | 518 | struct au8522_state *state = fe->demodulator_priv; |
ce1719a6 | 519 | dprintk("%s()\n", __func__); |
265a6510 ST |
520 | |
521 | au8522_writereg(state, 0xa4, 1 << 5); | |
522 | ||
523 | au8522_i2c_gate_ctrl(fe, 1); | |
524 | ||
525 | return 0; | |
526 | } | |
527 | ||
adeeac3b MK |
528 | static int au8522_led_gpio_enable(struct au8522_state *state, int onoff) |
529 | { | |
530 | struct au8522_led_config *led_config = state->config->led_cfg; | |
531 | u8 val; | |
532 | ||
533 | /* bail out if we cant control an LED */ | |
534 | if (!led_config || !led_config->gpio_output || | |
535 | !led_config->gpio_output_enable || !led_config->gpio_output_disable) | |
536 | return 0; | |
537 | ||
538 | val = au8522_readreg(state, 0x4000 | | |
539 | (led_config->gpio_output & ~0xc000)); | |
540 | if (onoff) { | |
541 | /* enable GPIO output */ | |
542 | val &= ~((led_config->gpio_output_enable >> 8) & 0xff); | |
543 | val |= (led_config->gpio_output_enable & 0xff); | |
544 | } else { | |
545 | /* disable GPIO output */ | |
546 | val &= ~((led_config->gpio_output_disable >> 8) & 0xff); | |
547 | val |= (led_config->gpio_output_disable & 0xff); | |
548 | } | |
549 | return au8522_writereg(state, 0x8000 | | |
550 | (led_config->gpio_output & ~0xc000), val); | |
551 | } | |
552 | ||
553 | /* led = 0 | off | |
554 | * led = 1 | signal ok | |
555 | * led = 2 | signal strong | |
556 | * led < 0 | only light led if leds are currently off | |
557 | */ | |
558 | static int au8522_led_ctrl(struct au8522_state *state, int led) | |
559 | { | |
560 | struct au8522_led_config *led_config = state->config->led_cfg; | |
561 | int i, ret = 0; | |
562 | ||
563 | /* bail out if we cant control an LED */ | |
564 | if (!led_config || !led_config->gpio_leds || | |
565 | !led_config->num_led_states || !led_config->led_states) | |
566 | return 0; | |
567 | ||
568 | if (led < 0) { | |
569 | /* if LED is already lit, then leave it as-is */ | |
570 | if (state->led_state) | |
571 | return 0; | |
572 | else | |
573 | led *= -1; | |
574 | } | |
575 | ||
576 | /* toggle LED if changing state */ | |
577 | if (state->led_state != led) { | |
578 | u8 val; | |
579 | ||
580 | dprintk("%s: %d\n", __func__, led); | |
581 | ||
582 | au8522_led_gpio_enable(state, 1); | |
583 | ||
584 | val = au8522_readreg(state, 0x4000 | | |
585 | (led_config->gpio_leds & ~0xc000)); | |
586 | ||
587 | /* start with all leds off */ | |
588 | for (i = 0; i < led_config->num_led_states; i++) | |
589 | val &= ~led_config->led_states[i]; | |
590 | ||
591 | /* set selected LED state */ | |
592 | if (led < led_config->num_led_states) | |
593 | val |= led_config->led_states[led]; | |
594 | else if (led_config->num_led_states) | |
595 | val |= | |
596 | led_config->led_states[led_config->num_led_states - 1]; | |
597 | ||
598 | ret = au8522_writereg(state, 0x8000 | | |
599 | (led_config->gpio_leds & ~0xc000), val); | |
600 | if (ret < 0) | |
601 | return ret; | |
602 | ||
603 | state->led_state = led; | |
604 | ||
605 | if (led == 0) | |
606 | au8522_led_gpio_enable(state, 0); | |
607 | } | |
608 | ||
609 | return 0; | |
610 | } | |
611 | ||
0c44bf36 | 612 | int au8522_sleep(struct dvb_frontend *fe) |
74d50724 MK |
613 | { |
614 | struct au8522_state *state = fe->demodulator_priv; | |
615 | dprintk("%s()\n", __func__); | |
616 | ||
adeeac3b MK |
617 | /* turn off led */ |
618 | au8522_led_ctrl(state, 0); | |
619 | ||
74d50724 MK |
620 | state->current_frequency = 0; |
621 | ||
622 | return 0; | |
623 | } | |
624 | ||
e059b0fa | 625 | static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status) |
265a6510 | 626 | { |
e059b0fa | 627 | struct au8522_state *state = fe->demodulator_priv; |
265a6510 ST |
628 | u8 reg; |
629 | u32 tuner_status = 0; | |
630 | ||
631 | *status = 0; | |
632 | ||
633 | if (state->current_modulation == VSB_8) { | |
ce1719a6 | 634 | dprintk("%s() Checking VSB_8\n", __func__); |
265a6510 | 635 | reg = au8522_readreg(state, 0x4088); |
836c2858 ST |
636 | if ((reg & 0x03) == 0x03) |
637 | *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI; | |
265a6510 | 638 | } else { |
ce1719a6 | 639 | dprintk("%s() Checking QAM\n", __func__); |
265a6510 | 640 | reg = au8522_readreg(state, 0x4541); |
e059b0fa | 641 | if (reg & 0x80) |
265a6510 | 642 | *status |= FE_HAS_VITERBI; |
e059b0fa | 643 | if (reg & 0x20) |
265a6510 ST |
644 | *status |= FE_HAS_LOCK | FE_HAS_SYNC; |
645 | } | |
646 | ||
18d73c58 | 647 | switch (state->config->status_mode) { |
265a6510 | 648 | case AU8522_DEMODLOCKING: |
ce1719a6 | 649 | dprintk("%s() DEMODLOCKING\n", __func__); |
265a6510 ST |
650 | if (*status & FE_HAS_VITERBI) |
651 | *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL; | |
652 | break; | |
653 | case AU8522_TUNERLOCKING: | |
654 | /* Get the tuner status */ | |
ce1719a6 | 655 | dprintk("%s() TUNERLOCKING\n", __func__); |
265a6510 ST |
656 | if (fe->ops.tuner_ops.get_status) { |
657 | if (fe->ops.i2c_gate_ctrl) | |
658 | fe->ops.i2c_gate_ctrl(fe, 1); | |
659 | ||
660 | fe->ops.tuner_ops.get_status(fe, &tuner_status); | |
661 | ||
662 | if (fe->ops.i2c_gate_ctrl) | |
663 | fe->ops.i2c_gate_ctrl(fe, 0); | |
664 | } | |
665 | if (tuner_status) | |
666 | *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL; | |
667 | break; | |
668 | } | |
adeeac3b MK |
669 | state->fe_status = *status; |
670 | ||
671 | if (*status & FE_HAS_LOCK) | |
672 | /* turn on LED, if it isn't on already */ | |
673 | au8522_led_ctrl(state, -1); | |
674 | else | |
675 | /* turn off LED */ | |
676 | au8522_led_ctrl(state, 0); | |
265a6510 | 677 | |
ce1719a6 | 678 | dprintk("%s() status 0x%08x\n", __func__, *status); |
265a6510 ST |
679 | |
680 | return 0; | |
681 | } | |
682 | ||
adeeac3b MK |
683 | static int au8522_led_status(struct au8522_state *state, const u16 *snr) |
684 | { | |
685 | struct au8522_led_config *led_config = state->config->led_cfg; | |
686 | int led; | |
687 | u16 strong; | |
688 | ||
689 | /* bail out if we cant control an LED */ | |
690 | if (!led_config) | |
691 | return 0; | |
692 | ||
693 | if (0 == (state->fe_status & FE_HAS_LOCK)) | |
694 | return au8522_led_ctrl(state, 0); | |
695 | else if (state->current_modulation == QAM_256) | |
696 | strong = led_config->qam256_strong; | |
697 | else if (state->current_modulation == QAM_64) | |
698 | strong = led_config->qam64_strong; | |
699 | else /* (state->current_modulation == VSB_8) */ | |
700 | strong = led_config->vsb8_strong; | |
701 | ||
702 | if (*snr >= strong) | |
703 | led = 2; | |
704 | else | |
705 | led = 1; | |
706 | ||
707 | if ((state->led_state) && | |
708 | (((strong < *snr) ? (*snr - strong) : (strong - *snr)) <= 10)) | |
709 | /* snr didn't change enough to bother | |
710 | * changing the color of the led */ | |
711 | return 0; | |
712 | ||
713 | return au8522_led_ctrl(state, led); | |
714 | } | |
715 | ||
f01699b4 | 716 | static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr) |
fb8152cb MK |
717 | { |
718 | struct au8522_state *state = fe->demodulator_priv; | |
f01699b4 | 719 | int ret = -EINVAL; |
fb8152cb | 720 | |
ce1719a6 | 721 | dprintk("%s()\n", __func__); |
265a6510 | 722 | |
f01699b4 | 723 | if (state->current_modulation == QAM_256) |
0daa5de7 MK |
724 | ret = au8522_mse2snr_lookup(qam256_mse2snr_tab, |
725 | ARRAY_SIZE(qam256_mse2snr_tab), | |
726 | au8522_readreg(state, 0x4522), | |
727 | snr); | |
f01699b4 | 728 | else if (state->current_modulation == QAM_64) |
0daa5de7 MK |
729 | ret = au8522_mse2snr_lookup(qam64_mse2snr_tab, |
730 | ARRAY_SIZE(qam64_mse2snr_tab), | |
731 | au8522_readreg(state, 0x4522), | |
732 | snr); | |
f01699b4 | 733 | else /* VSB_8 */ |
0daa5de7 MK |
734 | ret = au8522_mse2snr_lookup(vsb_mse2snr_tab, |
735 | ARRAY_SIZE(vsb_mse2snr_tab), | |
736 | au8522_readreg(state, 0x4311), | |
737 | snr); | |
f01699b4 | 738 | |
adeeac3b MK |
739 | if (state->config->led_cfg) |
740 | au8522_led_status(state, snr); | |
741 | ||
f01699b4 | 742 | return ret; |
265a6510 ST |
743 | } |
744 | ||
e059b0fa MK |
745 | static int au8522_read_signal_strength(struct dvb_frontend *fe, |
746 | u16 *signal_strength) | |
265a6510 ST |
747 | { |
748 | return au8522_read_snr(fe, signal_strength); | |
749 | } | |
750 | ||
e059b0fa | 751 | static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) |
265a6510 | 752 | { |
e059b0fa | 753 | struct au8522_state *state = fe->demodulator_priv; |
265a6510 | 754 | |
8973dc4b MK |
755 | if (state->current_modulation == VSB_8) |
756 | *ucblocks = au8522_readreg(state, 0x4087); | |
757 | else | |
758 | *ucblocks = au8522_readreg(state, 0x4543); | |
265a6510 ST |
759 | |
760 | return 0; | |
761 | } | |
762 | ||
e059b0fa | 763 | static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber) |
265a6510 ST |
764 | { |
765 | return au8522_read_ucblocks(fe, ber); | |
766 | } | |
767 | ||
e059b0fa | 768 | static int au8522_get_frontend(struct dvb_frontend *fe, |
265a6510 ST |
769 | struct dvb_frontend_parameters *p) |
770 | { | |
e059b0fa | 771 | struct au8522_state *state = fe->demodulator_priv; |
265a6510 ST |
772 | |
773 | p->frequency = state->current_frequency; | |
774 | p->u.vsb.modulation = state->current_modulation; | |
775 | ||
776 | return 0; | |
777 | } | |
778 | ||
e059b0fa MK |
779 | static int au8522_get_tune_settings(struct dvb_frontend *fe, |
780 | struct dvb_frontend_tune_settings *tune) | |
265a6510 ST |
781 | { |
782 | tune->min_delay_ms = 1000; | |
783 | return 0; | |
784 | } | |
785 | ||
e059b0fa | 786 | static void au8522_release(struct dvb_frontend *fe) |
265a6510 | 787 | { |
e059b0fa | 788 | struct au8522_state *state = fe->demodulator_priv; |
265a6510 ST |
789 | kfree(state); |
790 | } | |
791 | ||
792 | static struct dvb_frontend_ops au8522_ops; | |
793 | ||
e059b0fa MK |
794 | struct dvb_frontend *au8522_attach(const struct au8522_config *config, |
795 | struct i2c_adapter *i2c) | |
265a6510 | 796 | { |
e059b0fa | 797 | struct au8522_state *state = NULL; |
265a6510 ST |
798 | |
799 | /* allocate memory for the internal state */ | |
800 | state = kmalloc(sizeof(struct au8522_state), GFP_KERNEL); | |
801 | if (state == NULL) | |
802 | goto error; | |
803 | ||
804 | /* setup the state */ | |
805 | state->config = config; | |
806 | state->i2c = i2c; | |
807 | /* create dvb_frontend */ | |
808 | memcpy(&state->frontend.ops, &au8522_ops, | |
809 | sizeof(struct dvb_frontend_ops)); | |
810 | state->frontend.demodulator_priv = state; | |
811 | ||
812 | if (au8522_init(&state->frontend) != 0) { | |
813 | printk(KERN_ERR "%s: Failed to initialize correctly\n", | |
ce1719a6 | 814 | __func__); |
265a6510 ST |
815 | goto error; |
816 | } | |
817 | ||
818 | /* Note: Leaving the I2C gate open here. */ | |
819 | au8522_i2c_gate_ctrl(&state->frontend, 1); | |
820 | ||
821 | return &state->frontend; | |
822 | ||
823 | error: | |
824 | kfree(state); | |
825 | return NULL; | |
826 | } | |
18d73c58 | 827 | EXPORT_SYMBOL(au8522_attach); |
265a6510 ST |
828 | |
829 | static struct dvb_frontend_ops au8522_ops = { | |
830 | ||
831 | .info = { | |
832 | .name = "Auvitek AU8522 QAM/8VSB Frontend", | |
833 | .type = FE_ATSC, | |
834 | .frequency_min = 54000000, | |
835 | .frequency_max = 858000000, | |
836 | .frequency_stepsize = 62500, | |
837 | .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB | |
838 | }, | |
839 | ||
840 | .init = au8522_init, | |
74d50724 | 841 | .sleep = au8522_sleep, |
265a6510 ST |
842 | .i2c_gate_ctrl = au8522_i2c_gate_ctrl, |
843 | .set_frontend = au8522_set_frontend, | |
844 | .get_frontend = au8522_get_frontend, | |
845 | .get_tune_settings = au8522_get_tune_settings, | |
846 | .read_status = au8522_read_status, | |
847 | .read_ber = au8522_read_ber, | |
848 | .read_signal_strength = au8522_read_signal_strength, | |
849 | .read_snr = au8522_read_snr, | |
850 | .read_ucblocks = au8522_read_ucblocks, | |
851 | .release = au8522_release, | |
852 | }; | |
853 | ||
854 | module_param(debug, int, 0644); | |
855 | MODULE_PARM_DESC(debug, "Enable verbose debug messages"); | |
856 | ||
857 | MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver"); | |
858 | MODULE_AUTHOR("Steven Toth"); | |
859 | MODULE_LICENSE("GPL"); |