]>
Commit | Line | Data |
---|---|---|
825b9670 | 1 | /* |
c89f66f6 | 2 | * Afatech AF9013 demodulator driver |
825b9670 AP |
3 | * |
4 | * Copyright (C) 2007 Antti Palosaari <crope@iki.fi> | |
5 | * | |
6 | * Thanks to Afatech who kindly provided information. | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | * | |
22 | */ | |
23 | ||
24 | #include <linux/kernel.h> | |
25 | #include <linux/module.h> | |
26 | #include <linux/moduleparam.h> | |
27 | #include <linux/init.h> | |
28 | #include <linux/delay.h> | |
29 | #include <linux/string.h> | |
30 | #include <linux/slab.h> | |
31 | #include <linux/firmware.h> | |
32 | ||
33 | #include "dvb_frontend.h" | |
34 | #include "af9013_priv.h" | |
35 | #include "af9013.h" | |
36 | ||
37 | int af9013_debug; | |
38 | ||
39 | struct af9013_state { | |
40 | struct i2c_adapter *i2c; | |
41 | struct dvb_frontend frontend; | |
42 | ||
43 | struct af9013_config config; | |
44 | ||
9e35cd22 AP |
45 | /* tuner/demod RF and IF AGC limits used for signal strength calc */ |
46 | u8 signal_strength_en, rf_50, rf_80, if_50, if_80; | |
825b9670 AP |
47 | u16 signal_strength; |
48 | u32 ber; | |
49 | u32 ucblocks; | |
50 | u16 snr; | |
51 | u32 frequency; | |
52 | unsigned long next_statistics_check; | |
53 | }; | |
54 | ||
55 | static u8 regmask[8] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff }; | |
56 | ||
57 | static int af9013_write_regs(struct af9013_state *state, u8 mbox, u16 reg, | |
58 | u8 *val, u8 len) | |
59 | { | |
60 | u8 buf[3+len]; | |
61 | struct i2c_msg msg = { | |
62 | .addr = state->config.demod_address, | |
63 | .flags = 0, | |
64 | .len = sizeof(buf), | |
65 | .buf = buf }; | |
66 | ||
67 | buf[0] = reg >> 8; | |
68 | buf[1] = reg & 0xff; | |
69 | buf[2] = mbox; | |
70 | memcpy(&buf[3], val, len); | |
71 | ||
72 | if (i2c_transfer(state->i2c, &msg, 1) != 1) { | |
73 | warn("I2C write failed reg:%04x len:%d", reg, len); | |
74 | return -EREMOTEIO; | |
75 | } | |
76 | return 0; | |
77 | } | |
78 | ||
79 | static int af9013_write_ofdm_regs(struct af9013_state *state, u16 reg, u8 *val, | |
80 | u8 len) | |
81 | { | |
82 | u8 mbox = (1 << 0)|(1 << 1)|((len - 1) << 2)|(0 << 6)|(0 << 7); | |
83 | return af9013_write_regs(state, mbox, reg, val, len); | |
84 | } | |
85 | ||
86 | static int af9013_write_ofsm_regs(struct af9013_state *state, u16 reg, u8 *val, | |
87 | u8 len) | |
88 | { | |
89 | u8 mbox = (1 << 0)|(1 << 1)|((len - 1) << 2)|(1 << 6)|(1 << 7); | |
90 | return af9013_write_regs(state, mbox, reg, val, len); | |
91 | } | |
92 | ||
93 | /* write single register */ | |
94 | static int af9013_write_reg(struct af9013_state *state, u16 reg, u8 val) | |
95 | { | |
96 | return af9013_write_ofdm_regs(state, reg, &val, 1); | |
97 | } | |
98 | ||
99 | /* read single register */ | |
100 | static int af9013_read_reg(struct af9013_state *state, u16 reg, u8 *val) | |
101 | { | |
102 | u8 obuf[3] = { reg >> 8, reg & 0xff, 0 }; | |
103 | u8 ibuf[1]; | |
104 | struct i2c_msg msg[2] = { | |
105 | { | |
106 | .addr = state->config.demod_address, | |
107 | .flags = 0, | |
108 | .len = sizeof(obuf), | |
109 | .buf = obuf | |
110 | }, { | |
111 | .addr = state->config.demod_address, | |
112 | .flags = I2C_M_RD, | |
113 | .len = sizeof(ibuf), | |
114 | .buf = ibuf | |
115 | } | |
116 | }; | |
117 | ||
118 | if (i2c_transfer(state->i2c, msg, 2) != 2) { | |
119 | warn("I2C read failed reg:%04x", reg); | |
120 | return -EREMOTEIO; | |
121 | } | |
122 | *val = ibuf[0]; | |
123 | return 0; | |
124 | } | |
125 | ||
126 | static int af9013_write_reg_bits(struct af9013_state *state, u16 reg, u8 pos, | |
127 | u8 len, u8 val) | |
128 | { | |
129 | int ret; | |
130 | u8 tmp, mask; | |
131 | ||
132 | ret = af9013_read_reg(state, reg, &tmp); | |
133 | if (ret) | |
134 | return ret; | |
135 | ||
136 | mask = regmask[len - 1] << pos; | |
137 | tmp = (tmp & ~mask) | ((val << pos) & mask); | |
138 | ||
139 | return af9013_write_reg(state, reg, tmp); | |
140 | } | |
141 | ||
142 | static int af9013_read_reg_bits(struct af9013_state *state, u16 reg, u8 pos, | |
143 | u8 len, u8 *val) | |
144 | { | |
145 | int ret; | |
146 | u8 tmp; | |
147 | ||
148 | ret = af9013_read_reg(state, reg, &tmp); | |
149 | if (ret) | |
150 | return ret; | |
151 | *val = (tmp >> pos) & regmask[len - 1]; | |
152 | return 0; | |
153 | } | |
154 | ||
155 | static int af9013_set_gpio(struct af9013_state *state, u8 gpio, u8 gpioval) | |
156 | { | |
157 | int ret; | |
158 | u8 pos; | |
159 | u16 addr; | |
160 | deb_info("%s: gpio:%d gpioval:%02x\n", __func__, gpio, gpioval); | |
161 | ||
162 | /* GPIO0 & GPIO1 0xd735 | |
163 | GPIO2 & GPIO3 0xd736 */ | |
164 | ||
165 | switch (gpio) { | |
166 | case 0: | |
167 | case 1: | |
168 | addr = 0xd735; | |
169 | break; | |
170 | case 2: | |
171 | case 3: | |
172 | addr = 0xd736; | |
173 | break; | |
174 | ||
175 | default: | |
176 | err("invalid gpio:%d\n", gpio); | |
177 | ret = -EINVAL; | |
178 | goto error; | |
179 | }; | |
180 | ||
181 | switch (gpio) { | |
182 | case 0: | |
183 | case 2: | |
184 | pos = 0; | |
185 | break; | |
186 | case 1: | |
187 | case 3: | |
188 | default: | |
189 | pos = 4; | |
190 | break; | |
191 | }; | |
192 | ||
193 | ret = af9013_write_reg_bits(state, addr, pos, 4, gpioval); | |
194 | ||
195 | error: | |
196 | return ret; | |
197 | } | |
198 | ||
199 | static u32 af913_div(u32 a, u32 b, u32 x) | |
200 | { | |
201 | u32 r = 0, c = 0, i; | |
202 | deb_info("%s: a:%d b:%d x:%d\n", __func__, a, b, x); | |
203 | ||
204 | if (a > b) { | |
205 | c = a / b; | |
206 | a = a - c * b; | |
207 | } | |
208 | ||
209 | for (i = 0; i < x; i++) { | |
210 | if (a >= b) { | |
211 | r += 1; | |
212 | a -= b; | |
213 | } | |
214 | a <<= 1; | |
215 | r <<= 1; | |
216 | } | |
217 | r = (c << (u32)x) + r; | |
218 | ||
219 | deb_info("%s: a:%d b:%d x:%d r:%d r:%x\n", __func__, a, b, x, r, r); | |
220 | return r; | |
221 | } | |
222 | ||
223 | static int af9013_set_coeff(struct af9013_state *state, fe_bandwidth_t bw) | |
224 | { | |
edb709b6 | 225 | int ret, i, j, found; |
825b9670 AP |
226 | deb_info("%s: adc_clock:%d bw:%d\n", __func__, |
227 | state->config.adc_clock, bw); | |
228 | ||
9b22edd4 AP |
229 | /* lookup coeff from table */ |
230 | for (i = 0, found = 0; i < ARRAY_SIZE(coeff_table); i++) { | |
231 | if (coeff_table[i].adc_clock == state->config.adc_clock && | |
232 | coeff_table[i].bw == bw) { | |
233 | found = 1; | |
825b9670 | 234 | break; |
825b9670 | 235 | } |
825b9670 | 236 | } |
9b22edd4 AP |
237 | |
238 | if (!found) { | |
239 | err("invalid bw or clock"); | |
240 | ret = -EINVAL; | |
241 | goto error; | |
825b9670 AP |
242 | } |
243 | ||
edb709b6 AP |
244 | deb_info("%s: coeff: ", __func__); |
245 | debug_dump(coeff_table[i].val, sizeof(coeff_table[i].val), deb_info); | |
825b9670 AP |
246 | |
247 | /* program */ | |
edb709b6 AP |
248 | for (j = 0; j < sizeof(coeff_table[i].val); j++) { |
249 | ret = af9013_write_reg(state, 0xae00 + j, | |
250 | coeff_table[i].val[j]); | |
825b9670 AP |
251 | if (ret) |
252 | break; | |
253 | } | |
254 | ||
9b22edd4 | 255 | error: |
825b9670 AP |
256 | return ret; |
257 | } | |
258 | ||
259 | static int af9013_set_adc_ctrl(struct af9013_state *state) | |
260 | { | |
261 | int ret; | |
262 | u8 buf[3], tmp, i; | |
263 | u32 adc_cw; | |
264 | ||
265 | deb_info("%s: adc_clock:%d\n", __func__, state->config.adc_clock); | |
266 | ||
267 | /* adc frequency type */ | |
268 | switch (state->config.adc_clock) { | |
269 | case 28800: /* 28.800 MHz */ | |
270 | tmp = 0; | |
271 | break; | |
272 | case 20480: /* 20.480 MHz */ | |
273 | tmp = 1; | |
274 | break; | |
275 | case 28000: /* 28.000 MHz */ | |
276 | tmp = 2; | |
277 | break; | |
278 | case 25000: /* 25.000 MHz */ | |
279 | tmp = 3; | |
280 | break; | |
281 | default: | |
282 | err("invalid xtal"); | |
283 | return -EINVAL; | |
284 | } | |
285 | ||
286 | adc_cw = af913_div(state->config.adc_clock*1000, 1000000ul, 19ul); | |
287 | ||
288 | buf[0] = (u8) ((adc_cw & 0x000000ff)); | |
289 | buf[1] = (u8) ((adc_cw & 0x0000ff00) >> 8); | |
290 | buf[2] = (u8) ((adc_cw & 0x00ff0000) >> 16); | |
291 | ||
292 | deb_info("%s: adc_cw:", __func__); | |
293 | debug_dump(buf, sizeof(buf), deb_info); | |
294 | ||
295 | /* program */ | |
296 | for (i = 0; i < sizeof(buf); i++) { | |
297 | ret = af9013_write_reg(state, 0xd180 + i, buf[i]); | |
298 | if (ret) | |
299 | goto error; | |
300 | } | |
301 | ret = af9013_write_reg_bits(state, 0x9bd2, 0, 4, tmp); | |
302 | error: | |
303 | return ret; | |
304 | } | |
305 | ||
6d42b218 AP |
306 | static int af9013_set_freq_ctrl(struct af9013_state *state, |
307 | struct dvb_frontend *fe) | |
825b9670 AP |
308 | { |
309 | int ret; | |
310 | u16 addr; | |
311 | u8 buf[3], i, j; | |
312 | u32 adc_freq, freq_cw; | |
313 | s8 bfs_spec_inv; | |
314 | int if_sample_freq; | |
315 | ||
316 | for (j = 0; j < 3; j++) { | |
317 | if (j == 0) { | |
318 | addr = 0xd140; /* fcw normal */ | |
319 | bfs_spec_inv = state->config.rf_spec_inv ? -1 : 1; | |
320 | } else if (j == 1) { | |
321 | addr = 0x9be7; /* fcw dummy ram */ | |
322 | bfs_spec_inv = state->config.rf_spec_inv ? -1 : 1; | |
323 | } else { | |
324 | addr = 0x9bea; /* fcw inverted */ | |
325 | bfs_spec_inv = state->config.rf_spec_inv ? 1 : -1; | |
326 | } | |
327 | ||
6d42b218 AP |
328 | adc_freq = state->config.adc_clock * 1000; |
329 | ||
330 | /* get used IF frequency */ | |
331 | if (fe->ops.tuner_ops.get_if_frequency) | |
332 | fe->ops.tuner_ops.get_if_frequency(fe, &if_sample_freq); | |
333 | else | |
334 | if_sample_freq = state->config.tuner_if * 1000; | |
825b9670 AP |
335 | |
336 | while (if_sample_freq > (adc_freq / 2)) | |
337 | if_sample_freq = if_sample_freq - adc_freq; | |
338 | ||
339 | if (if_sample_freq >= 0) | |
340 | bfs_spec_inv = bfs_spec_inv * (-1); | |
341 | else | |
342 | if_sample_freq = if_sample_freq * (-1); | |
343 | ||
344 | freq_cw = af913_div(if_sample_freq, adc_freq, 23ul); | |
345 | ||
346 | if (bfs_spec_inv == -1) | |
347 | freq_cw = 0x00800000 - freq_cw; | |
348 | ||
349 | buf[0] = (u8) ((freq_cw & 0x000000ff)); | |
350 | buf[1] = (u8) ((freq_cw & 0x0000ff00) >> 8); | |
351 | buf[2] = (u8) ((freq_cw & 0x007f0000) >> 16); | |
352 | ||
353 | ||
354 | deb_info("%s: freq_cw:", __func__); | |
355 | debug_dump(buf, sizeof(buf), deb_info); | |
356 | ||
357 | /* program */ | |
358 | for (i = 0; i < sizeof(buf); i++) { | |
359 | ret = af9013_write_reg(state, addr++, buf[i]); | |
360 | if (ret) | |
361 | goto error; | |
362 | } | |
363 | } | |
364 | error: | |
365 | return ret; | |
366 | } | |
367 | ||
368 | static int af9013_set_ofdm_params(struct af9013_state *state, | |
369 | struct dvb_ofdm_parameters *params, u8 *auto_mode) | |
370 | { | |
371 | int ret; | |
372 | u8 i, buf[3] = {0, 0, 0}; | |
373 | *auto_mode = 0; /* set if parameters are requested to auto set */ | |
374 | ||
a2f5a811 AP |
375 | /* Try auto-detect transmission parameters in case of AUTO requested or |
376 | garbage parameters given by application for compatibility. | |
377 | MPlayer seems to provide garbage parameters currently. */ | |
378 | ||
825b9670 AP |
379 | switch (params->transmission_mode) { |
380 | case TRANSMISSION_MODE_AUTO: | |
381 | *auto_mode = 1; | |
382 | case TRANSMISSION_MODE_2K: | |
383 | break; | |
384 | case TRANSMISSION_MODE_8K: | |
385 | buf[0] |= (1 << 0); | |
386 | break; | |
387 | default: | |
a2f5a811 AP |
388 | deb_info("%s: invalid transmission_mode\n", __func__); |
389 | *auto_mode = 1; | |
825b9670 AP |
390 | } |
391 | ||
392 | switch (params->guard_interval) { | |
393 | case GUARD_INTERVAL_AUTO: | |
394 | *auto_mode = 1; | |
395 | case GUARD_INTERVAL_1_32: | |
396 | break; | |
397 | case GUARD_INTERVAL_1_16: | |
398 | buf[0] |= (1 << 2); | |
399 | break; | |
400 | case GUARD_INTERVAL_1_8: | |
401 | buf[0] |= (2 << 2); | |
402 | break; | |
403 | case GUARD_INTERVAL_1_4: | |
404 | buf[0] |= (3 << 2); | |
405 | break; | |
406 | default: | |
a2f5a811 AP |
407 | deb_info("%s: invalid guard_interval\n", __func__); |
408 | *auto_mode = 1; | |
825b9670 AP |
409 | } |
410 | ||
411 | switch (params->hierarchy_information) { | |
412 | case HIERARCHY_AUTO: | |
413 | *auto_mode = 1; | |
414 | case HIERARCHY_NONE: | |
415 | break; | |
416 | case HIERARCHY_1: | |
417 | buf[0] |= (1 << 4); | |
418 | break; | |
419 | case HIERARCHY_2: | |
420 | buf[0] |= (2 << 4); | |
421 | break; | |
422 | case HIERARCHY_4: | |
423 | buf[0] |= (3 << 4); | |
424 | break; | |
425 | default: | |
a2f5a811 AP |
426 | deb_info("%s: invalid hierarchy_information\n", __func__); |
427 | *auto_mode = 1; | |
825b9670 AP |
428 | }; |
429 | ||
430 | switch (params->constellation) { | |
431 | case QAM_AUTO: | |
432 | *auto_mode = 1; | |
433 | case QPSK: | |
434 | break; | |
435 | case QAM_16: | |
436 | buf[1] |= (1 << 6); | |
437 | break; | |
438 | case QAM_64: | |
439 | buf[1] |= (2 << 6); | |
440 | break; | |
441 | default: | |
a2f5a811 AP |
442 | deb_info("%s: invalid constellation\n", __func__); |
443 | *auto_mode = 1; | |
825b9670 AP |
444 | } |
445 | ||
446 | /* Use HP. How and which case we can switch to LP? */ | |
447 | buf[1] |= (1 << 4); | |
448 | ||
449 | switch (params->code_rate_HP) { | |
450 | case FEC_AUTO: | |
451 | *auto_mode = 1; | |
452 | case FEC_1_2: | |
453 | break; | |
454 | case FEC_2_3: | |
455 | buf[2] |= (1 << 0); | |
456 | break; | |
457 | case FEC_3_4: | |
458 | buf[2] |= (2 << 0); | |
459 | break; | |
460 | case FEC_5_6: | |
461 | buf[2] |= (3 << 0); | |
462 | break; | |
463 | case FEC_7_8: | |
464 | buf[2] |= (4 << 0); | |
465 | break; | |
466 | default: | |
a2f5a811 AP |
467 | deb_info("%s: invalid code_rate_HP\n", __func__); |
468 | *auto_mode = 1; | |
825b9670 AP |
469 | } |
470 | ||
471 | switch (params->code_rate_LP) { | |
472 | case FEC_AUTO: | |
473 | /* if HIERARCHY_NONE and FEC_NONE then LP FEC is set to FEC_AUTO | |
474 | by dvb_frontend.c for compatibility */ | |
475 | if (params->hierarchy_information != HIERARCHY_NONE) | |
476 | *auto_mode = 1; | |
477 | case FEC_1_2: | |
478 | break; | |
479 | case FEC_2_3: | |
480 | buf[2] |= (1 << 3); | |
481 | break; | |
482 | case FEC_3_4: | |
483 | buf[2] |= (2 << 3); | |
484 | break; | |
485 | case FEC_5_6: | |
486 | buf[2] |= (3 << 3); | |
487 | break; | |
488 | case FEC_7_8: | |
489 | buf[2] |= (4 << 3); | |
490 | break; | |
491 | case FEC_NONE: | |
492 | if (params->hierarchy_information == HIERARCHY_AUTO) | |
493 | break; | |
494 | default: | |
a2f5a811 AP |
495 | deb_info("%s: invalid code_rate_LP\n", __func__); |
496 | *auto_mode = 1; | |
825b9670 AP |
497 | } |
498 | ||
499 | switch (params->bandwidth) { | |
500 | case BANDWIDTH_6_MHZ: | |
501 | break; | |
502 | case BANDWIDTH_7_MHZ: | |
503 | buf[1] |= (1 << 2); | |
504 | break; | |
505 | case BANDWIDTH_8_MHZ: | |
506 | buf[1] |= (2 << 2); | |
507 | break; | |
508 | default: | |
a2f5a811 AP |
509 | deb_info("%s: invalid bandwidth\n", __func__); |
510 | buf[1] |= (2 << 2); /* cannot auto-detect BW, try 8 MHz */ | |
825b9670 AP |
511 | } |
512 | ||
513 | /* program */ | |
514 | for (i = 0; i < sizeof(buf); i++) { | |
515 | ret = af9013_write_reg(state, 0xd3c0 + i, buf[i]); | |
516 | if (ret) | |
517 | break; | |
518 | } | |
519 | ||
520 | return ret; | |
521 | } | |
522 | ||
523 | static int af9013_reset(struct af9013_state *state, u8 sleep) | |
524 | { | |
525 | int ret; | |
526 | u8 tmp, i; | |
527 | deb_info("%s\n", __func__); | |
528 | ||
529 | /* enable OFDM reset */ | |
530 | ret = af9013_write_reg_bits(state, 0xd417, 4, 1, 1); | |
531 | if (ret) | |
532 | goto error; | |
533 | ||
534 | /* start reset mechanism */ | |
535 | ret = af9013_write_reg(state, 0xaeff, 1); | |
536 | if (ret) | |
537 | goto error; | |
538 | ||
539 | /* reset is done when bit 1 is set */ | |
540 | for (i = 0; i < 150; i++) { | |
541 | ret = af9013_read_reg_bits(state, 0xd417, 1, 1, &tmp); | |
542 | if (ret) | |
543 | goto error; | |
544 | if (tmp) | |
545 | break; /* reset done */ | |
546 | msleep(10); | |
547 | } | |
548 | if (!tmp) | |
549 | return -ETIMEDOUT; | |
550 | ||
551 | /* don't clear reset when going to sleep */ | |
552 | if (!sleep) { | |
553 | /* clear OFDM reset */ | |
554 | ret = af9013_write_reg_bits(state, 0xd417, 1, 1, 0); | |
555 | if (ret) | |
556 | goto error; | |
557 | ||
558 | /* disable OFDM reset */ | |
559 | ret = af9013_write_reg_bits(state, 0xd417, 4, 1, 0); | |
560 | } | |
561 | error: | |
562 | return ret; | |
563 | } | |
564 | ||
565 | static int af9013_power_ctrl(struct af9013_state *state, u8 onoff) | |
566 | { | |
567 | int ret; | |
568 | deb_info("%s: onoff:%d\n", __func__, onoff); | |
569 | ||
570 | if (onoff) { | |
571 | /* power on */ | |
572 | ret = af9013_write_reg_bits(state, 0xd73a, 3, 1, 0); | |
573 | if (ret) | |
574 | goto error; | |
575 | ret = af9013_write_reg_bits(state, 0xd417, 1, 1, 0); | |
576 | if (ret) | |
577 | goto error; | |
578 | ret = af9013_write_reg_bits(state, 0xd417, 4, 1, 0); | |
579 | } else { | |
580 | /* power off */ | |
581 | ret = af9013_reset(state, 1); | |
582 | if (ret) | |
583 | goto error; | |
584 | ret = af9013_write_reg_bits(state, 0xd73a, 3, 1, 1); | |
585 | } | |
586 | error: | |
587 | return ret; | |
588 | } | |
589 | ||
590 | static int af9013_lock_led(struct af9013_state *state, u8 onoff) | |
591 | { | |
592 | deb_info("%s: onoff:%d\n", __func__, onoff); | |
593 | ||
594 | return af9013_write_reg_bits(state, 0xd730, 0, 1, onoff); | |
595 | } | |
596 | ||
597 | static int af9013_set_frontend(struct dvb_frontend *fe, | |
598 | struct dvb_frontend_parameters *params) | |
599 | { | |
600 | struct af9013_state *state = fe->demodulator_priv; | |
601 | int ret; | |
602 | u8 auto_mode; /* auto set TPS */ | |
603 | ||
604 | deb_info("%s: freq:%d bw:%d\n", __func__, params->frequency, | |
605 | params->u.ofdm.bandwidth); | |
606 | ||
607 | state->frequency = params->frequency; | |
608 | ||
737fabf0 AP |
609 | /* program tuner */ |
610 | if (fe->ops.tuner_ops.set_params) | |
611 | fe->ops.tuner_ops.set_params(fe, params); | |
612 | ||
825b9670 AP |
613 | /* program CFOE coefficients */ |
614 | ret = af9013_set_coeff(state, params->u.ofdm.bandwidth); | |
615 | if (ret) | |
616 | goto error; | |
617 | ||
618 | /* program frequency control */ | |
6d42b218 | 619 | ret = af9013_set_freq_ctrl(state, fe); |
825b9670 AP |
620 | if (ret) |
621 | goto error; | |
622 | ||
623 | /* clear TPS lock flag (inverted flag) */ | |
624 | ret = af9013_write_reg_bits(state, 0xd330, 3, 1, 1); | |
625 | if (ret) | |
626 | goto error; | |
627 | ||
628 | /* clear MPEG2 lock flag */ | |
629 | ret = af9013_write_reg_bits(state, 0xd507, 6, 1, 0); | |
630 | if (ret) | |
631 | goto error; | |
632 | ||
633 | /* empty channel function */ | |
634 | ret = af9013_write_reg_bits(state, 0x9bfe, 0, 1, 0); | |
635 | if (ret) | |
636 | goto error; | |
637 | ||
638 | /* empty DVB-T channel function */ | |
639 | ret = af9013_write_reg_bits(state, 0x9bc2, 0, 1, 0); | |
640 | if (ret) | |
641 | goto error; | |
642 | ||
825b9670 AP |
643 | /* program TPS and bandwidth, check if auto mode needed */ |
644 | ret = af9013_set_ofdm_params(state, ¶ms->u.ofdm, &auto_mode); | |
645 | if (ret) | |
646 | goto error; | |
647 | ||
648 | if (auto_mode) { | |
649 | /* clear easy mode flag */ | |
650 | ret = af9013_write_reg(state, 0xaefd, 0); | |
651 | deb_info("%s: auto TPS\n", __func__); | |
652 | } else { | |
653 | /* set easy mode flag */ | |
654 | ret = af9013_write_reg(state, 0xaefd, 1); | |
655 | if (ret) | |
656 | goto error; | |
657 | ret = af9013_write_reg(state, 0xaefe, 0); | |
658 | deb_info("%s: manual TPS\n", __func__); | |
659 | } | |
660 | if (ret) | |
661 | goto error; | |
662 | ||
663 | /* everything is set, lets try to receive channel - OFSM GO! */ | |
664 | ret = af9013_write_reg(state, 0xffff, 0); | |
665 | if (ret) | |
666 | goto error; | |
667 | ||
668 | error: | |
669 | return ret; | |
670 | } | |
671 | ||
672 | static int af9013_get_frontend(struct dvb_frontend *fe, | |
673 | struct dvb_frontend_parameters *p) | |
674 | { | |
675 | struct af9013_state *state = fe->demodulator_priv; | |
676 | int ret; | |
677 | u8 i, buf[3]; | |
678 | deb_info("%s\n", __func__); | |
679 | ||
680 | /* read TPS registers */ | |
681 | for (i = 0; i < 3; i++) { | |
682 | ret = af9013_read_reg(state, 0xd3c0 + i, &buf[i]); | |
683 | if (ret) | |
684 | goto error; | |
685 | } | |
686 | ||
687 | switch ((buf[1] >> 6) & 3) { | |
688 | case 0: | |
689 | p->u.ofdm.constellation = QPSK; | |
690 | break; | |
691 | case 1: | |
692 | p->u.ofdm.constellation = QAM_16; | |
693 | break; | |
694 | case 2: | |
695 | p->u.ofdm.constellation = QAM_64; | |
696 | break; | |
697 | } | |
698 | ||
699 | switch ((buf[0] >> 0) & 3) { | |
700 | case 0: | |
701 | p->u.ofdm.transmission_mode = TRANSMISSION_MODE_2K; | |
702 | break; | |
703 | case 1: | |
704 | p->u.ofdm.transmission_mode = TRANSMISSION_MODE_8K; | |
705 | } | |
706 | ||
707 | switch ((buf[0] >> 2) & 3) { | |
708 | case 0: | |
709 | p->u.ofdm.guard_interval = GUARD_INTERVAL_1_32; | |
710 | break; | |
711 | case 1: | |
712 | p->u.ofdm.guard_interval = GUARD_INTERVAL_1_16; | |
713 | break; | |
714 | case 2: | |
715 | p->u.ofdm.guard_interval = GUARD_INTERVAL_1_8; | |
716 | break; | |
717 | case 3: | |
718 | p->u.ofdm.guard_interval = GUARD_INTERVAL_1_4; | |
719 | break; | |
720 | } | |
721 | ||
722 | switch ((buf[0] >> 4) & 7) { | |
723 | case 0: | |
724 | p->u.ofdm.hierarchy_information = HIERARCHY_NONE; | |
725 | break; | |
726 | case 1: | |
727 | p->u.ofdm.hierarchy_information = HIERARCHY_1; | |
728 | break; | |
729 | case 2: | |
730 | p->u.ofdm.hierarchy_information = HIERARCHY_2; | |
731 | break; | |
732 | case 3: | |
733 | p->u.ofdm.hierarchy_information = HIERARCHY_4; | |
734 | break; | |
735 | } | |
736 | ||
737 | switch ((buf[2] >> 0) & 7) { | |
738 | case 0: | |
739 | p->u.ofdm.code_rate_HP = FEC_1_2; | |
740 | break; | |
741 | case 1: | |
742 | p->u.ofdm.code_rate_HP = FEC_2_3; | |
743 | break; | |
744 | case 2: | |
745 | p->u.ofdm.code_rate_HP = FEC_3_4; | |
746 | break; | |
747 | case 3: | |
748 | p->u.ofdm.code_rate_HP = FEC_5_6; | |
749 | break; | |
750 | case 4: | |
751 | p->u.ofdm.code_rate_HP = FEC_7_8; | |
752 | break; | |
753 | } | |
754 | ||
755 | switch ((buf[2] >> 3) & 7) { | |
756 | case 0: | |
757 | p->u.ofdm.code_rate_LP = FEC_1_2; | |
758 | break; | |
759 | case 1: | |
760 | p->u.ofdm.code_rate_LP = FEC_2_3; | |
761 | break; | |
762 | case 2: | |
763 | p->u.ofdm.code_rate_LP = FEC_3_4; | |
764 | break; | |
765 | case 3: | |
766 | p->u.ofdm.code_rate_LP = FEC_5_6; | |
767 | break; | |
768 | case 4: | |
769 | p->u.ofdm.code_rate_LP = FEC_7_8; | |
770 | break; | |
771 | } | |
772 | ||
773 | switch ((buf[1] >> 2) & 3) { | |
774 | case 0: | |
775 | p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ; | |
776 | break; | |
777 | case 1: | |
778 | p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ; | |
779 | break; | |
780 | case 2: | |
781 | p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ; | |
782 | break; | |
783 | } | |
784 | ||
785 | p->inversion = INVERSION_AUTO; | |
786 | p->frequency = state->frequency; | |
787 | ||
788 | error: | |
789 | return ret; | |
790 | } | |
791 | ||
792 | static int af9013_update_ber_unc(struct dvb_frontend *fe) | |
793 | { | |
794 | struct af9013_state *state = fe->demodulator_priv; | |
795 | int ret; | |
796 | u8 buf[3], i; | |
797 | u32 error_bit_count = 0; | |
798 | u32 total_bit_count = 0; | |
799 | u32 abort_packet_count = 0; | |
825b9670 AP |
800 | |
801 | state->ber = 0; | |
802 | ||
803 | /* check if error bit count is ready */ | |
804 | ret = af9013_read_reg_bits(state, 0xd391, 4, 1, &buf[0]); | |
805 | if (ret) | |
806 | goto error; | |
807 | if (!buf[0]) | |
808 | goto exit; | |
809 | ||
810 | /* get RSD packet abort count */ | |
811 | for (i = 0; i < 2; i++) { | |
812 | ret = af9013_read_reg(state, 0xd38a + i, &buf[i]); | |
813 | if (ret) | |
814 | goto error; | |
815 | } | |
816 | abort_packet_count = (buf[1] << 8) + buf[0]; | |
817 | ||
818 | /* get error bit count */ | |
819 | for (i = 0; i < 3; i++) { | |
820 | ret = af9013_read_reg(state, 0xd387 + i, &buf[i]); | |
821 | if (ret) | |
822 | goto error; | |
823 | } | |
824 | error_bit_count = (buf[2] << 16) + (buf[1] << 8) + buf[0]; | |
825 | error_bit_count = error_bit_count - abort_packet_count * 8 * 8; | |
826 | ||
827 | /* get used RSD counting period (10000 RSD packets used) */ | |
828 | for (i = 0; i < 2; i++) { | |
829 | ret = af9013_read_reg(state, 0xd385 + i, &buf[i]); | |
830 | if (ret) | |
831 | goto error; | |
832 | } | |
833 | total_bit_count = (buf[1] << 8) + buf[0]; | |
834 | total_bit_count = total_bit_count - abort_packet_count; | |
835 | total_bit_count = total_bit_count * 204 * 8; | |
836 | ||
28f947a9 AP |
837 | if (total_bit_count) |
838 | state->ber = error_bit_count * 1000000000 / total_bit_count; | |
825b9670 AP |
839 | |
840 | state->ucblocks += abort_packet_count; | |
841 | ||
842 | deb_info("%s: err bits:%d total bits:%d abort count:%d\n", __func__, | |
843 | error_bit_count, total_bit_count, abort_packet_count); | |
844 | ||
845 | /* set BER counting range */ | |
846 | ret = af9013_write_reg(state, 0xd385, 10000 & 0xff); | |
847 | if (ret) | |
848 | goto error; | |
849 | ret = af9013_write_reg(state, 0xd386, 10000 >> 8); | |
850 | if (ret) | |
851 | goto error; | |
852 | /* reset and start BER counter */ | |
853 | ret = af9013_write_reg_bits(state, 0xd391, 4, 1, 1); | |
854 | if (ret) | |
855 | goto error; | |
856 | ||
857 | exit: | |
858 | error: | |
859 | return ret; | |
860 | } | |
861 | ||
862 | static int af9013_update_snr(struct dvb_frontend *fe) | |
863 | { | |
864 | struct af9013_state *state = fe->demodulator_priv; | |
865 | int ret; | |
866 | u8 buf[3], i, len; | |
867 | u32 quant = 0; | |
4d543096 | 868 | struct snr_table *uninitialized_var(snr_table); |
825b9670 AP |
869 | |
870 | /* check if quantizer ready (for snr) */ | |
871 | ret = af9013_read_reg_bits(state, 0xd2e1, 3, 1, &buf[0]); | |
872 | if (ret) | |
873 | goto error; | |
874 | if (buf[0]) { | |
875 | /* quantizer ready - read it */ | |
876 | for (i = 0; i < 3; i++) { | |
877 | ret = af9013_read_reg(state, 0xd2e3 + i, &buf[i]); | |
878 | if (ret) | |
879 | goto error; | |
880 | } | |
881 | quant = (buf[2] << 16) + (buf[1] << 8) + buf[0]; | |
882 | ||
883 | /* read current constellation */ | |
884 | ret = af9013_read_reg(state, 0xd3c1, &buf[0]); | |
885 | if (ret) | |
886 | goto error; | |
887 | ||
888 | switch ((buf[0] >> 6) & 3) { | |
889 | case 0: | |
890 | len = ARRAY_SIZE(qpsk_snr_table); | |
891 | snr_table = qpsk_snr_table; | |
892 | break; | |
893 | case 1: | |
894 | len = ARRAY_SIZE(qam16_snr_table); | |
895 | snr_table = qam16_snr_table; | |
896 | break; | |
897 | case 2: | |
898 | len = ARRAY_SIZE(qam64_snr_table); | |
899 | snr_table = qam64_snr_table; | |
900 | break; | |
901 | default: | |
902 | len = 0; | |
903 | break; | |
904 | } | |
905 | ||
906 | if (len) { | |
907 | for (i = 0; i < len; i++) { | |
908 | if (quant < snr_table[i].val) { | |
909 | state->snr = snr_table[i].snr * 10; | |
910 | break; | |
911 | } | |
912 | } | |
913 | } | |
914 | ||
915 | /* set quantizer super frame count */ | |
916 | ret = af9013_write_reg(state, 0xd2e2, 1); | |
917 | if (ret) | |
918 | goto error; | |
919 | ||
920 | /* check quantizer availability */ | |
921 | for (i = 0; i < 10; i++) { | |
922 | msleep(10); | |
923 | ret = af9013_read_reg_bits(state, 0xd2e6, 0, 1, | |
924 | &buf[0]); | |
925 | if (ret) | |
926 | goto error; | |
927 | if (!buf[0]) | |
928 | break; | |
929 | } | |
930 | ||
931 | /* reset quantizer */ | |
932 | ret = af9013_write_reg_bits(state, 0xd2e1, 3, 1, 1); | |
933 | if (ret) | |
934 | goto error; | |
935 | } | |
936 | ||
937 | error: | |
938 | return ret; | |
939 | } | |
940 | ||
941 | static int af9013_update_signal_strength(struct dvb_frontend *fe) | |
942 | { | |
943 | struct af9013_state *state = fe->demodulator_priv; | |
465a9e3a | 944 | int ret = 0; |
9e35cd22 | 945 | u8 rf_gain, if_gain; |
825b9670 AP |
946 | int signal_strength; |
947 | ||
948 | deb_info("%s\n", __func__); | |
949 | ||
9e35cd22 | 950 | if (state->signal_strength_en) { |
825b9670 AP |
951 | ret = af9013_read_reg(state, 0xd07c, &rf_gain); |
952 | if (ret) | |
953 | goto error; | |
954 | ret = af9013_read_reg(state, 0xd07d, &if_gain); | |
955 | if (ret) | |
956 | goto error; | |
9e35cd22 AP |
957 | signal_strength = (0xffff / \ |
958 | (9 * (state->rf_50 + state->if_50) - \ | |
959 | 11 * (state->rf_80 + state->if_80))) * \ | |
960 | (10 * (rf_gain + if_gain) - \ | |
961 | 11 * (state->rf_80 + state->if_80)); | |
825b9670 AP |
962 | if (signal_strength < 0) |
963 | signal_strength = 0; | |
964 | else if (signal_strength > 0xffff) | |
965 | signal_strength = 0xffff; | |
966 | ||
967 | state->signal_strength = signal_strength; | |
9e35cd22 AP |
968 | } else { |
969 | state->signal_strength = 0; | |
825b9670 AP |
970 | } |
971 | ||
972 | error: | |
973 | return ret; | |
974 | } | |
975 | ||
976 | static int af9013_update_statistics(struct dvb_frontend *fe) | |
977 | { | |
978 | struct af9013_state *state = fe->demodulator_priv; | |
979 | int ret; | |
980 | ||
981 | if (time_before(jiffies, state->next_statistics_check)) | |
982 | return 0; | |
983 | ||
984 | /* set minimum statistic update interval */ | |
985 | state->next_statistics_check = jiffies + msecs_to_jiffies(1200); | |
986 | ||
987 | ret = af9013_update_signal_strength(fe); | |
988 | if (ret) | |
989 | goto error; | |
990 | ret = af9013_update_snr(fe); | |
991 | if (ret) | |
992 | goto error; | |
993 | ret = af9013_update_ber_unc(fe); | |
994 | if (ret) | |
995 | goto error; | |
996 | ||
997 | error: | |
998 | return ret; | |
999 | } | |
1000 | ||
1001 | static int af9013_get_tune_settings(struct dvb_frontend *fe, | |
1002 | struct dvb_frontend_tune_settings *fesettings) | |
1003 | { | |
1004 | fesettings->min_delay_ms = 800; | |
1005 | fesettings->step_size = 0; | |
1006 | fesettings->max_drift = 0; | |
1007 | ||
1008 | return 0; | |
1009 | } | |
1010 | ||
1011 | static int af9013_read_status(struct dvb_frontend *fe, fe_status_t *status) | |
1012 | { | |
1013 | struct af9013_state *state = fe->demodulator_priv; | |
1014 | int ret = 0; | |
1015 | u8 tmp; | |
1016 | *status = 0; | |
1017 | ||
825b9670 AP |
1018 | /* MPEG2 lock */ |
1019 | ret = af9013_read_reg_bits(state, 0xd507, 6, 1, &tmp); | |
1020 | if (ret) | |
1021 | goto error; | |
1022 | if (tmp) | |
8af5e381 AP |
1023 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI | |
1024 | FE_HAS_SYNC | FE_HAS_LOCK; | |
825b9670 | 1025 | |
8af5e381 AP |
1026 | if (!*status) { |
1027 | /* TPS lock */ | |
1028 | ret = af9013_read_reg_bits(state, 0xd330, 3, 1, &tmp); | |
825b9670 AP |
1029 | if (ret) |
1030 | goto error; | |
1031 | if (tmp) | |
8af5e381 AP |
1032 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | |
1033 | FE_HAS_VITERBI; | |
825b9670 AP |
1034 | } |
1035 | ||
8af5e381 | 1036 | if (!*status) { |
825b9670 AP |
1037 | /* CFO lock */ |
1038 | ret = af9013_read_reg_bits(state, 0xd333, 7, 1, &tmp); | |
1039 | if (ret) | |
1040 | goto error; | |
1041 | if (tmp) | |
8af5e381 | 1042 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER; |
825b9670 AP |
1043 | } |
1044 | ||
8af5e381 | 1045 | if (!*status) { |
825b9670 AP |
1046 | /* SFOE lock */ |
1047 | ret = af9013_read_reg_bits(state, 0xd334, 6, 1, &tmp); | |
1048 | if (ret) | |
1049 | goto error; | |
1050 | if (tmp) | |
8af5e381 AP |
1051 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER; |
1052 | } | |
1053 | ||
1054 | if (!*status) { | |
1055 | /* AGC lock */ | |
1056 | ret = af9013_read_reg_bits(state, 0xd1a0, 6, 1, &tmp); | |
1057 | if (ret) | |
1058 | goto error; | |
1059 | if (tmp) | |
1060 | *status |= FE_HAS_SIGNAL; | |
825b9670 AP |
1061 | } |
1062 | ||
1063 | ret = af9013_update_statistics(fe); | |
1064 | ||
1065 | error: | |
1066 | return ret; | |
1067 | } | |
1068 | ||
1069 | ||
1070 | static int af9013_read_ber(struct dvb_frontend *fe, u32 *ber) | |
1071 | { | |
1072 | struct af9013_state *state = fe->demodulator_priv; | |
1073 | int ret; | |
1074 | ret = af9013_update_statistics(fe); | |
1075 | *ber = state->ber; | |
1076 | return ret; | |
1077 | } | |
1078 | ||
1079 | static int af9013_read_signal_strength(struct dvb_frontend *fe, u16 *strength) | |
1080 | { | |
1081 | struct af9013_state *state = fe->demodulator_priv; | |
1082 | int ret; | |
1083 | ret = af9013_update_statistics(fe); | |
1084 | *strength = state->signal_strength; | |
1085 | return ret; | |
1086 | } | |
1087 | ||
1088 | static int af9013_read_snr(struct dvb_frontend *fe, u16 *snr) | |
1089 | { | |
1090 | struct af9013_state *state = fe->demodulator_priv; | |
1091 | int ret; | |
1092 | ret = af9013_update_statistics(fe); | |
1093 | *snr = state->snr; | |
1094 | return ret; | |
1095 | } | |
1096 | ||
1097 | static int af9013_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) | |
1098 | { | |
1099 | struct af9013_state *state = fe->demodulator_priv; | |
1100 | int ret; | |
1101 | ret = af9013_update_statistics(fe); | |
1102 | *ucblocks = state->ucblocks; | |
1103 | return ret; | |
1104 | } | |
1105 | ||
1106 | static int af9013_sleep(struct dvb_frontend *fe) | |
1107 | { | |
1108 | struct af9013_state *state = fe->demodulator_priv; | |
1109 | int ret; | |
1110 | deb_info("%s\n", __func__); | |
1111 | ||
1112 | ret = af9013_lock_led(state, 0); | |
1113 | if (ret) | |
1114 | goto error; | |
1115 | ||
1116 | ret = af9013_power_ctrl(state, 0); | |
1117 | error: | |
1118 | return ret; | |
1119 | } | |
1120 | ||
1121 | static int af9013_init(struct dvb_frontend *fe) | |
1122 | { | |
1123 | struct af9013_state *state = fe->demodulator_priv; | |
1124 | int ret, i, len; | |
1125 | u8 tmp0, tmp1; | |
1126 | struct regdesc *init; | |
1127 | deb_info("%s\n", __func__); | |
1128 | ||
1129 | /* reset OFDM */ | |
1130 | ret = af9013_reset(state, 0); | |
1131 | if (ret) | |
1132 | goto error; | |
1133 | ||
1134 | /* power on */ | |
1135 | ret = af9013_power_ctrl(state, 1); | |
1136 | if (ret) | |
1137 | goto error; | |
1138 | ||
1139 | /* enable ADC */ | |
1140 | ret = af9013_write_reg(state, 0xd73a, 0xa4); | |
1141 | if (ret) | |
1142 | goto error; | |
1143 | ||
1144 | /* write API version to firmware */ | |
1145 | for (i = 0; i < sizeof(state->config.api_version); i++) { | |
1146 | ret = af9013_write_reg(state, 0x9bf2 + i, | |
1147 | state->config.api_version[i]); | |
1148 | if (ret) | |
1149 | goto error; | |
1150 | } | |
1151 | ||
1152 | /* program ADC control */ | |
1153 | ret = af9013_set_adc_ctrl(state); | |
1154 | if (ret) | |
1155 | goto error; | |
1156 | ||
1157 | /* set I2C master clock */ | |
1158 | ret = af9013_write_reg(state, 0xd416, 0x14); | |
1159 | if (ret) | |
1160 | goto error; | |
1161 | ||
1162 | /* set 16 embx */ | |
1163 | ret = af9013_write_reg_bits(state, 0xd700, 1, 1, 1); | |
1164 | if (ret) | |
1165 | goto error; | |
1166 | ||
1167 | /* set no trigger */ | |
1168 | ret = af9013_write_reg_bits(state, 0xd700, 2, 1, 0); | |
1169 | if (ret) | |
1170 | goto error; | |
1171 | ||
1172 | /* set read-update bit for constellation */ | |
1173 | ret = af9013_write_reg_bits(state, 0xd371, 1, 1, 1); | |
1174 | if (ret) | |
1175 | goto error; | |
1176 | ||
1177 | /* enable FEC monitor */ | |
1178 | ret = af9013_write_reg_bits(state, 0xd392, 1, 1, 1); | |
1179 | if (ret) | |
1180 | goto error; | |
1181 | ||
1182 | /* load OFSM settings */ | |
1183 | deb_info("%s: load ofsm settings\n", __func__); | |
1184 | len = ARRAY_SIZE(ofsm_init); | |
1185 | init = ofsm_init; | |
1186 | for (i = 0; i < len; i++) { | |
1187 | ret = af9013_write_reg_bits(state, init[i].addr, init[i].pos, | |
1188 | init[i].len, init[i].val); | |
1189 | if (ret) | |
1190 | goto error; | |
1191 | } | |
1192 | ||
1193 | /* load tuner specific settings */ | |
1194 | deb_info("%s: load tuner specific settings\n", __func__); | |
1195 | switch (state->config.tuner) { | |
1196 | case AF9013_TUNER_MXL5003D: | |
1197 | len = ARRAY_SIZE(tuner_init_mxl5003d); | |
1198 | init = tuner_init_mxl5003d; | |
1199 | break; | |
1200 | case AF9013_TUNER_MXL5005D: | |
1201 | case AF9013_TUNER_MXL5005R: | |
a4f31d0d | 1202 | case AF9013_TUNER_MXL5007T: |
825b9670 AP |
1203 | len = ARRAY_SIZE(tuner_init_mxl5005); |
1204 | init = tuner_init_mxl5005; | |
1205 | break; | |
1206 | case AF9013_TUNER_ENV77H11D5: | |
1207 | len = ARRAY_SIZE(tuner_init_env77h11d5); | |
1208 | init = tuner_init_env77h11d5; | |
1209 | break; | |
1210 | case AF9013_TUNER_MT2060: | |
1211 | len = ARRAY_SIZE(tuner_init_mt2060); | |
1212 | init = tuner_init_mt2060; | |
1213 | break; | |
1214 | case AF9013_TUNER_MC44S803: | |
1215 | len = ARRAY_SIZE(tuner_init_mc44s803); | |
1216 | init = tuner_init_mc44s803; | |
1217 | break; | |
1218 | case AF9013_TUNER_QT1010: | |
1219 | case AF9013_TUNER_QT1010A: | |
1220 | len = ARRAY_SIZE(tuner_init_qt1010); | |
1221 | init = tuner_init_qt1010; | |
1222 | break; | |
1223 | case AF9013_TUNER_MT2060_2: | |
1224 | len = ARRAY_SIZE(tuner_init_mt2060_2); | |
1225 | init = tuner_init_mt2060_2; | |
1226 | break; | |
1227 | case AF9013_TUNER_TDA18271: | |
2158e509 | 1228 | case AF9013_TUNER_TDA18218: |
825b9670 AP |
1229 | len = ARRAY_SIZE(tuner_init_tda18271); |
1230 | init = tuner_init_tda18271; | |
1231 | break; | |
1232 | case AF9013_TUNER_UNKNOWN: | |
1233 | default: | |
1234 | len = ARRAY_SIZE(tuner_init_unknown); | |
1235 | init = tuner_init_unknown; | |
1236 | break; | |
1237 | } | |
1238 | ||
1239 | for (i = 0; i < len; i++) { | |
1240 | ret = af9013_write_reg_bits(state, init[i].addr, init[i].pos, | |
1241 | init[i].len, init[i].val); | |
1242 | if (ret) | |
1243 | goto error; | |
1244 | } | |
1245 | ||
1246 | /* set TS mode */ | |
1247 | deb_info("%s: setting ts mode\n", __func__); | |
1248 | tmp0 = 0; /* parallel mode */ | |
1249 | tmp1 = 0; /* serial mode */ | |
1250 | switch (state->config.output_mode) { | |
1251 | case AF9013_OUTPUT_MODE_PARALLEL: | |
1252 | tmp0 = 1; | |
1253 | break; | |
1254 | case AF9013_OUTPUT_MODE_SERIAL: | |
1255 | tmp1 = 1; | |
1256 | break; | |
1257 | case AF9013_OUTPUT_MODE_USB: | |
1258 | /* usb mode for AF9015 */ | |
1259 | default: | |
1260 | break; | |
1261 | } | |
1262 | ret = af9013_write_reg_bits(state, 0xd500, 1, 1, tmp0); /* parallel */ | |
1263 | if (ret) | |
1264 | goto error; | |
1265 | ret = af9013_write_reg_bits(state, 0xd500, 2, 1, tmp1); /* serial */ | |
1266 | if (ret) | |
1267 | goto error; | |
1268 | ||
1269 | /* enable lock led */ | |
1270 | ret = af9013_lock_led(state, 1); | |
1271 | if (ret) | |
1272 | goto error; | |
1273 | ||
9e35cd22 AP |
1274 | /* read values needed for signal strength calculation */ |
1275 | ret = af9013_read_reg_bits(state, 0x9bee, 0, 1, | |
1276 | &state->signal_strength_en); | |
1277 | if (ret) | |
1278 | goto error; | |
1279 | ||
1280 | if (state->signal_strength_en) { | |
1281 | ret = af9013_read_reg(state, 0x9bbd, &state->rf_50); | |
1282 | if (ret) | |
1283 | goto error; | |
1284 | ret = af9013_read_reg(state, 0x9bd0, &state->rf_80); | |
1285 | if (ret) | |
1286 | goto error; | |
1287 | ret = af9013_read_reg(state, 0x9be2, &state->if_50); | |
1288 | if (ret) | |
1289 | goto error; | |
1290 | ret = af9013_read_reg(state, 0x9be4, &state->if_80); | |
1291 | if (ret) | |
1292 | goto error; | |
1293 | } | |
1294 | ||
825b9670 AP |
1295 | error: |
1296 | return ret; | |
1297 | } | |
1298 | ||
1299 | static struct dvb_frontend_ops af9013_ops; | |
1300 | ||
1301 | static int af9013_download_firmware(struct af9013_state *state) | |
1302 | { | |
6ed9d560 | 1303 | int i, len, remaining, ret; |
825b9670 | 1304 | const struct firmware *fw; |
825b9670 AP |
1305 | u16 checksum = 0; |
1306 | u8 val; | |
1307 | u8 fw_params[4]; | |
825b9670 AP |
1308 | u8 *fw_file = AF9013_DEFAULT_FIRMWARE; |
1309 | ||
1310 | msleep(100); | |
1311 | /* check whether firmware is already running */ | |
1312 | ret = af9013_read_reg(state, 0x98be, &val); | |
1313 | if (ret) | |
1314 | goto error; | |
1315 | else | |
1316 | deb_info("%s: firmware status:%02x\n", __func__, val); | |
1317 | ||
1318 | if (val == 0x0c) /* fw is running, no need for download */ | |
1319 | goto exit; | |
1320 | ||
1321 | info("found a '%s' in cold state, will try to load a firmware", | |
1322 | af9013_ops.info.name); | |
1323 | ||
1324 | /* request the firmware, this will block and timeout */ | |
e9785250 | 1325 | ret = request_firmware(&fw, fw_file, state->i2c->dev.parent); |
825b9670 AP |
1326 | if (ret) { |
1327 | err("did not find the firmware file. (%s) " | |
1328 | "Please see linux/Documentation/dvb/ for more details" \ | |
1329 | " on firmware-problems. (%d)", | |
1330 | fw_file, ret); | |
1331 | goto error; | |
1332 | } | |
1333 | ||
1334 | info("downloading firmware from file '%s'", fw_file); | |
1335 | ||
1336 | /* calc checksum */ | |
1337 | for (i = 0; i < fw->size; i++) | |
1338 | checksum += fw->data[i]; | |
1339 | ||
1340 | fw_params[0] = checksum >> 8; | |
1341 | fw_params[1] = checksum & 0xff; | |
1342 | fw_params[2] = fw->size >> 8; | |
1343 | fw_params[3] = fw->size & 0xff; | |
1344 | ||
1345 | /* write fw checksum & size */ | |
1346 | ret = af9013_write_ofsm_regs(state, 0x50fc, | |
1347 | fw_params, sizeof(fw_params)); | |
1348 | if (ret) | |
1349 | goto error_release; | |
1350 | ||
6ed9d560 AP |
1351 | #define FW_ADDR 0x5100 /* firmware start address */ |
1352 | #define LEN_MAX 16 /* max packet size */ | |
1353 | for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) { | |
1354 | len = remaining; | |
1355 | if (len > LEN_MAX) | |
1356 | len = LEN_MAX; | |
825b9670 | 1357 | |
6ed9d560 AP |
1358 | ret = af9013_write_ofsm_regs(state, |
1359 | FW_ADDR + fw->size - remaining, | |
1360 | (u8 *) &fw->data[fw->size - remaining], len); | |
825b9670 | 1361 | if (ret) { |
6ed9d560 | 1362 | err("firmware download failed:%d", ret); |
825b9670 AP |
1363 | goto error_release; |
1364 | } | |
1365 | } | |
1366 | ||
825b9670 AP |
1367 | /* request boot firmware */ |
1368 | ret = af9013_write_reg(state, 0xe205, 1); | |
1369 | if (ret) | |
1370 | goto error_release; | |
1371 | ||
1372 | for (i = 0; i < 15; i++) { | |
1373 | msleep(100); | |
1374 | ||
1375 | /* check firmware status */ | |
1376 | ret = af9013_read_reg(state, 0x98be, &val); | |
1377 | if (ret) | |
1378 | goto error_release; | |
1379 | ||
1380 | deb_info("%s: firmware status:%02x\n", __func__, val); | |
1381 | ||
1382 | if (val == 0x0c || val == 0x04) /* success or fail */ | |
1383 | break; | |
1384 | } | |
1385 | ||
1386 | if (val == 0x04) { | |
1387 | err("firmware did not run"); | |
1388 | ret = -1; | |
1389 | } else if (val != 0x0c) { | |
1390 | err("firmware boot timeout"); | |
1391 | ret = -1; | |
1392 | } | |
1393 | ||
1394 | error_release: | |
1395 | release_firmware(fw); | |
1396 | error: | |
1397 | exit: | |
1398 | if (!ret) | |
1399 | info("found a '%s' in warm state.", af9013_ops.info.name); | |
1400 | return ret; | |
1401 | } | |
1402 | ||
1403 | static int af9013_i2c_gate_ctrl(struct dvb_frontend *fe, int enable) | |
1404 | { | |
1405 | int ret; | |
1406 | struct af9013_state *state = fe->demodulator_priv; | |
1407 | deb_info("%s: enable:%d\n", __func__, enable); | |
1408 | ||
1409 | if (state->config.output_mode == AF9013_OUTPUT_MODE_USB) | |
1410 | ret = af9013_write_reg_bits(state, 0xd417, 3, 1, enable); | |
1411 | else | |
1412 | ret = af9013_write_reg_bits(state, 0xd607, 2, 1, enable); | |
1413 | ||
1414 | return ret; | |
1415 | } | |
1416 | ||
1417 | static void af9013_release(struct dvb_frontend *fe) | |
1418 | { | |
1419 | struct af9013_state *state = fe->demodulator_priv; | |
1420 | kfree(state); | |
1421 | } | |
1422 | ||
1423 | static struct dvb_frontend_ops af9013_ops; | |
1424 | ||
1425 | struct dvb_frontend *af9013_attach(const struct af9013_config *config, | |
1426 | struct i2c_adapter *i2c) | |
1427 | { | |
1428 | int ret; | |
1429 | struct af9013_state *state = NULL; | |
ce99efa5 | 1430 | u8 buf[4], i; |
825b9670 AP |
1431 | |
1432 | /* allocate memory for the internal state */ | |
1433 | state = kzalloc(sizeof(struct af9013_state), GFP_KERNEL); | |
1434 | if (state == NULL) | |
1435 | goto error; | |
1436 | ||
1437 | /* setup the state */ | |
1438 | state->i2c = i2c; | |
1439 | memcpy(&state->config, config, sizeof(struct af9013_config)); | |
1440 | ||
825b9670 AP |
1441 | /* download firmware */ |
1442 | if (state->config.output_mode != AF9013_OUTPUT_MODE_USB) { | |
1443 | ret = af9013_download_firmware(state); | |
1444 | if (ret) | |
1445 | goto error; | |
1446 | } | |
1447 | ||
1448 | /* firmware version */ | |
ce99efa5 | 1449 | for (i = 0; i < 4; i++) { |
825b9670 AP |
1450 | ret = af9013_read_reg(state, 0x5103 + i, &buf[i]); |
1451 | if (ret) | |
1452 | goto error; | |
1453 | } | |
ce99efa5 | 1454 | info("firmware version:%d.%d.%d.%d", buf[0], buf[1], buf[2], buf[3]); |
825b9670 | 1455 | |
109a2990 AP |
1456 | /* chip version */ |
1457 | ret = af9013_read_reg_bits(state, 0xd733, 4, 4, &buf[2]); | |
1458 | if (ret) | |
1459 | goto error; | |
1460 | ||
1461 | /* ROM version */ | |
1462 | for (i = 0; i < 2; i++) { | |
1463 | ret = af9013_read_reg(state, 0x116b + i, &buf[i]); | |
1464 | if (ret) | |
1465 | goto error; | |
1466 | } | |
1467 | deb_info("%s: chip version:%d ROM version:%d.%d\n", __func__, | |
1468 | buf[2], buf[0], buf[1]); | |
1469 | ||
825b9670 AP |
1470 | /* settings for mp2if */ |
1471 | if (state->config.output_mode == AF9013_OUTPUT_MODE_USB) { | |
1472 | /* AF9015 split PSB to 1.5k + 0.5k */ | |
1473 | ret = af9013_write_reg_bits(state, 0xd50b, 2, 1, 1); | |
1474 | } else { | |
1475 | /* AF9013 change the output bit to data7 */ | |
1476 | ret = af9013_write_reg_bits(state, 0xd500, 3, 1, 1); | |
1477 | if (ret) | |
1478 | goto error; | |
1479 | /* AF9013 set mpeg to full speed */ | |
1480 | ret = af9013_write_reg_bits(state, 0xd502, 4, 1, 1); | |
1481 | } | |
1482 | if (ret) | |
1483 | goto error; | |
1484 | ret = af9013_write_reg_bits(state, 0xd520, 4, 1, 1); | |
1485 | if (ret) | |
1486 | goto error; | |
1487 | ||
1488 | /* set GPIOs */ | |
1489 | for (i = 0; i < sizeof(state->config.gpio); i++) { | |
1490 | ret = af9013_set_gpio(state, i, state->config.gpio[i]); | |
1491 | if (ret) | |
1492 | goto error; | |
1493 | } | |
1494 | ||
1495 | /* create dvb_frontend */ | |
1496 | memcpy(&state->frontend.ops, &af9013_ops, | |
1497 | sizeof(struct dvb_frontend_ops)); | |
1498 | state->frontend.demodulator_priv = state; | |
1499 | ||
1500 | return &state->frontend; | |
1501 | error: | |
1502 | kfree(state); | |
1503 | return NULL; | |
1504 | } | |
1505 | EXPORT_SYMBOL(af9013_attach); | |
1506 | ||
1507 | static struct dvb_frontend_ops af9013_ops = { | |
1508 | .info = { | |
1509 | .name = "Afatech AF9013 DVB-T", | |
1510 | .type = FE_OFDM, | |
1511 | .frequency_min = 174000000, | |
1512 | .frequency_max = 862000000, | |
1513 | .frequency_stepsize = 250000, | |
1514 | .frequency_tolerance = 0, | |
1515 | .caps = | |
1516 | FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 | | |
1517 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO | | |
1518 | FE_CAN_QPSK | FE_CAN_QAM_16 | | |
1519 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO | | |
1520 | FE_CAN_TRANSMISSION_MODE_AUTO | | |
1521 | FE_CAN_GUARD_INTERVAL_AUTO | | |
1522 | FE_CAN_HIERARCHY_AUTO | | |
1523 | FE_CAN_RECOVER | | |
1524 | FE_CAN_MUTE_TS | |
1525 | }, | |
1526 | ||
1527 | .release = af9013_release, | |
1528 | .init = af9013_init, | |
1529 | .sleep = af9013_sleep, | |
1530 | .i2c_gate_ctrl = af9013_i2c_gate_ctrl, | |
1531 | ||
1532 | .set_frontend = af9013_set_frontend, | |
1533 | .get_frontend = af9013_get_frontend, | |
1534 | ||
1535 | .get_tune_settings = af9013_get_tune_settings, | |
1536 | ||
1537 | .read_status = af9013_read_status, | |
1538 | .read_ber = af9013_read_ber, | |
1539 | .read_signal_strength = af9013_read_signal_strength, | |
1540 | .read_snr = af9013_read_snr, | |
1541 | .read_ucblocks = af9013_read_ucblocks, | |
1542 | }; | |
1543 | ||
1544 | module_param_named(debug, af9013_debug, int, 0644); | |
1545 | MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off)."); | |
1546 | ||
1547 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); | |
1548 | MODULE_DESCRIPTION("Afatech AF9013 DVB-T demodulator driver"); | |
1549 | MODULE_LICENSE("GPL"); |