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