Linux 2.6.34-rc3
[pohmelfs.git] / drivers / media / dvb / frontends / af9013.c
blob12e018b4107daec6758c7571ef3bb3bd4e244159
1 /*
2 * DVB USB Linux driver for Afatech AF9015 DVB-T USB2.0 receiver
4 * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
6 * Thanks to Afatech who kindly provided information.
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.
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.
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.
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>
33 #include "dvb_frontend.h"
34 #include "af9013_priv.h"
35 #include "af9013.h"
37 int af9013_debug;
39 struct af9013_state {
40 struct i2c_adapter *i2c;
41 struct dvb_frontend frontend;
43 struct af9013_config config;
45 u16 signal_strength;
46 u32 ber;
47 u32 ucblocks;
48 u16 snr;
49 u32 frequency;
50 unsigned long next_statistics_check;
53 static u8 regmask[8] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
55 static int af9013_write_regs(struct af9013_state *state, u8 mbox, u16 reg,
56 u8 *val, u8 len)
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 };
65 buf[0] = reg >> 8;
66 buf[1] = reg & 0xff;
67 buf[2] = mbox;
68 memcpy(&buf[3], val, len);
70 if (i2c_transfer(state->i2c, &msg, 1) != 1) {
71 warn("I2C write failed reg:%04x len:%d", reg, len);
72 return -EREMOTEIO;
74 return 0;
77 static int af9013_write_ofdm_regs(struct af9013_state *state, u16 reg, u8 *val,
78 u8 len)
80 u8 mbox = (1 << 0)|(1 << 1)|((len - 1) << 2)|(0 << 6)|(0 << 7);
81 return af9013_write_regs(state, mbox, reg, val, len);
84 static int af9013_write_ofsm_regs(struct af9013_state *state, u16 reg, u8 *val,
85 u8 len)
87 u8 mbox = (1 << 0)|(1 << 1)|((len - 1) << 2)|(1 << 6)|(1 << 7);
88 return af9013_write_regs(state, mbox, reg, val, len);
91 /* write single register */
92 static int af9013_write_reg(struct af9013_state *state, u16 reg, u8 val)
94 return af9013_write_ofdm_regs(state, reg, &val, 1);
97 /* read single register */
98 static int af9013_read_reg(struct af9013_state *state, u16 reg, u8 *val)
100 u8 obuf[3] = { reg >> 8, reg & 0xff, 0 };
101 u8 ibuf[1];
102 struct i2c_msg msg[2] = {
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
116 if (i2c_transfer(state->i2c, msg, 2) != 2) {
117 warn("I2C read failed reg:%04x", reg);
118 return -EREMOTEIO;
120 *val = ibuf[0];
121 return 0;
124 static int af9013_write_reg_bits(struct af9013_state *state, u16 reg, u8 pos,
125 u8 len, u8 val)
127 int ret;
128 u8 tmp, mask;
130 ret = af9013_read_reg(state, reg, &tmp);
131 if (ret)
132 return ret;
134 mask = regmask[len - 1] << pos;
135 tmp = (tmp & ~mask) | ((val << pos) & mask);
137 return af9013_write_reg(state, reg, tmp);
140 static int af9013_read_reg_bits(struct af9013_state *state, u16 reg, u8 pos,
141 u8 len, u8 *val)
143 int ret;
144 u8 tmp;
146 ret = af9013_read_reg(state, reg, &tmp);
147 if (ret)
148 return ret;
149 *val = (tmp >> pos) & regmask[len - 1];
150 return 0;
153 static int af9013_set_gpio(struct af9013_state *state, u8 gpio, u8 gpioval)
155 int ret;
156 u8 pos;
157 u16 addr;
158 deb_info("%s: gpio:%d gpioval:%02x\n", __func__, gpio, gpioval);
160 /* GPIO0 & GPIO1 0xd735
161 GPIO2 & GPIO3 0xd736 */
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;
173 default:
174 err("invalid gpio:%d\n", gpio);
175 ret = -EINVAL;
176 goto error;
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;
191 ret = af9013_write_reg_bits(state, addr, pos, 4, gpioval);
193 error:
194 return ret;
197 static u32 af913_div(u32 a, u32 b, u32 x)
199 u32 r = 0, c = 0, i;
200 deb_info("%s: a:%d b:%d x:%d\n", __func__, a, b, x);
202 if (a > b) {
203 c = a / b;
204 a = a - c * b;
207 for (i = 0; i < x; i++) {
208 if (a >= b) {
209 r += 1;
210 a -= b;
212 a <<= 1;
213 r <<= 1;
215 r = (c << (u32)x) + r;
217 deb_info("%s: a:%d b:%d x:%d r:%d r:%x\n", __func__, a, b, x, r, r);
218 return r;
221 static int af9013_set_coeff(struct af9013_state *state, fe_bandwidth_t bw)
223 int ret = 0;
224 u8 i = 0;
225 u8 buf[24];
226 u32 uninitialized_var(ns_coeff1_2048nu);
227 u32 uninitialized_var(ns_coeff1_8191nu);
228 u32 uninitialized_var(ns_coeff1_8192nu);
229 u32 uninitialized_var(ns_coeff1_8193nu);
230 u32 uninitialized_var(ns_coeff2_2k);
231 u32 uninitialized_var(ns_coeff2_8k);
233 deb_info("%s: adc_clock:%d bw:%d\n", __func__,
234 state->config.adc_clock, bw);
236 switch (state->config.adc_clock) {
237 case 28800: /* 28.800 MHz */
238 switch (bw) {
239 case BANDWIDTH_6_MHZ:
240 ns_coeff1_2048nu = 0x01e79e7a;
241 ns_coeff1_8191nu = 0x0079eb6e;
242 ns_coeff1_8192nu = 0x0079e79e;
243 ns_coeff1_8193nu = 0x0079e3cf;
244 ns_coeff2_2k = 0x00f3cf3d;
245 ns_coeff2_8k = 0x003cf3cf;
246 break;
247 case BANDWIDTH_7_MHZ:
248 ns_coeff1_2048nu = 0x0238e38e;
249 ns_coeff1_8191nu = 0x008e3d55;
250 ns_coeff1_8192nu = 0x008e38e4;
251 ns_coeff1_8193nu = 0x008e3472;
252 ns_coeff2_2k = 0x011c71c7;
253 ns_coeff2_8k = 0x00471c72;
254 break;
255 case BANDWIDTH_8_MHZ:
256 ns_coeff1_2048nu = 0x028a28a3;
257 ns_coeff1_8191nu = 0x00a28f3d;
258 ns_coeff1_8192nu = 0x00a28a29;
259 ns_coeff1_8193nu = 0x00a28514;
260 ns_coeff2_2k = 0x01451451;
261 ns_coeff2_8k = 0x00514514;
262 break;
263 default:
264 ret = -EINVAL;
266 break;
267 case 20480: /* 20.480 MHz */
268 switch (bw) {
269 case BANDWIDTH_6_MHZ:
270 ns_coeff1_2048nu = 0x02adb6dc;
271 ns_coeff1_8191nu = 0x00ab7313;
272 ns_coeff1_8192nu = 0x00ab6db7;
273 ns_coeff1_8193nu = 0x00ab685c;
274 ns_coeff2_2k = 0x0156db6e;
275 ns_coeff2_8k = 0x0055b6dc;
276 break;
277 case BANDWIDTH_7_MHZ:
278 ns_coeff1_2048nu = 0x03200001;
279 ns_coeff1_8191nu = 0x00c80640;
280 ns_coeff1_8192nu = 0x00c80000;
281 ns_coeff1_8193nu = 0x00c7f9c0;
282 ns_coeff2_2k = 0x01900000;
283 ns_coeff2_8k = 0x00640000;
284 break;
285 case BANDWIDTH_8_MHZ:
286 ns_coeff1_2048nu = 0x03924926;
287 ns_coeff1_8191nu = 0x00e4996e;
288 ns_coeff1_8192nu = 0x00e49249;
289 ns_coeff1_8193nu = 0x00e48b25;
290 ns_coeff2_2k = 0x01c92493;
291 ns_coeff2_8k = 0x00724925;
292 break;
293 default:
294 ret = -EINVAL;
296 break;
297 case 28000: /* 28.000 MHz */
298 switch (bw) {
299 case BANDWIDTH_6_MHZ:
300 ns_coeff1_2048nu = 0x01f58d10;
301 ns_coeff1_8191nu = 0x007d672f;
302 ns_coeff1_8192nu = 0x007d6344;
303 ns_coeff1_8193nu = 0x007d5f59;
304 ns_coeff2_2k = 0x00fac688;
305 ns_coeff2_8k = 0x003eb1a2;
306 break;
307 case BANDWIDTH_7_MHZ:
308 ns_coeff1_2048nu = 0x02492492;
309 ns_coeff1_8191nu = 0x00924db7;
310 ns_coeff1_8192nu = 0x00924925;
311 ns_coeff1_8193nu = 0x00924492;
312 ns_coeff2_2k = 0x01249249;
313 ns_coeff2_8k = 0x00492492;
314 break;
315 case BANDWIDTH_8_MHZ:
316 ns_coeff1_2048nu = 0x029cbc15;
317 ns_coeff1_8191nu = 0x00a7343f;
318 ns_coeff1_8192nu = 0x00a72f05;
319 ns_coeff1_8193nu = 0x00a729cc;
320 ns_coeff2_2k = 0x014e5e0a;
321 ns_coeff2_8k = 0x00539783;
322 break;
323 default:
324 ret = -EINVAL;
326 break;
327 case 25000: /* 25.000 MHz */
328 switch (bw) {
329 case BANDWIDTH_6_MHZ:
330 ns_coeff1_2048nu = 0x0231bcb5;
331 ns_coeff1_8191nu = 0x008c7391;
332 ns_coeff1_8192nu = 0x008c6f2d;
333 ns_coeff1_8193nu = 0x008c6aca;
334 ns_coeff2_2k = 0x0118de5b;
335 ns_coeff2_8k = 0x00463797;
336 break;
337 case BANDWIDTH_7_MHZ:
338 ns_coeff1_2048nu = 0x028f5c29;
339 ns_coeff1_8191nu = 0x00a3dc29;
340 ns_coeff1_8192nu = 0x00a3d70a;
341 ns_coeff1_8193nu = 0x00a3d1ec;
342 ns_coeff2_2k = 0x0147ae14;
343 ns_coeff2_8k = 0x0051eb85;
344 break;
345 case BANDWIDTH_8_MHZ:
346 ns_coeff1_2048nu = 0x02ecfb9d;
347 ns_coeff1_8191nu = 0x00bb44c1;
348 ns_coeff1_8192nu = 0x00bb3ee7;
349 ns_coeff1_8193nu = 0x00bb390d;
350 ns_coeff2_2k = 0x01767dce;
351 ns_coeff2_8k = 0x005d9f74;
352 break;
353 default:
354 ret = -EINVAL;
356 break;
357 default:
358 err("invalid xtal");
359 return -EINVAL;
361 if (ret) {
362 err("invalid bandwidth");
363 return ret;
366 buf[i++] = (u8) ((ns_coeff1_2048nu & 0x03000000) >> 24);
367 buf[i++] = (u8) ((ns_coeff1_2048nu & 0x00ff0000) >> 16);
368 buf[i++] = (u8) ((ns_coeff1_2048nu & 0x0000ff00) >> 8);
369 buf[i++] = (u8) ((ns_coeff1_2048nu & 0x000000ff));
370 buf[i++] = (u8) ((ns_coeff2_2k & 0x01c00000) >> 22);
371 buf[i++] = (u8) ((ns_coeff2_2k & 0x003fc000) >> 14);
372 buf[i++] = (u8) ((ns_coeff2_2k & 0x00003fc0) >> 6);
373 buf[i++] = (u8) ((ns_coeff2_2k & 0x0000003f));
374 buf[i++] = (u8) ((ns_coeff1_8191nu & 0x03000000) >> 24);
375 buf[i++] = (u8) ((ns_coeff1_8191nu & 0x00ffc000) >> 16);
376 buf[i++] = (u8) ((ns_coeff1_8191nu & 0x0000ff00) >> 8);
377 buf[i++] = (u8) ((ns_coeff1_8191nu & 0x000000ff));
378 buf[i++] = (u8) ((ns_coeff1_8192nu & 0x03000000) >> 24);
379 buf[i++] = (u8) ((ns_coeff1_8192nu & 0x00ffc000) >> 16);
380 buf[i++] = (u8) ((ns_coeff1_8192nu & 0x0000ff00) >> 8);
381 buf[i++] = (u8) ((ns_coeff1_8192nu & 0x000000ff));
382 buf[i++] = (u8) ((ns_coeff1_8193nu & 0x03000000) >> 24);
383 buf[i++] = (u8) ((ns_coeff1_8193nu & 0x00ffc000) >> 16);
384 buf[i++] = (u8) ((ns_coeff1_8193nu & 0x0000ff00) >> 8);
385 buf[i++] = (u8) ((ns_coeff1_8193nu & 0x000000ff));
386 buf[i++] = (u8) ((ns_coeff2_8k & 0x01c00000) >> 22);
387 buf[i++] = (u8) ((ns_coeff2_8k & 0x003fc000) >> 14);
388 buf[i++] = (u8) ((ns_coeff2_8k & 0x00003fc0) >> 6);
389 buf[i++] = (u8) ((ns_coeff2_8k & 0x0000003f));
391 deb_info("%s: coeff:", __func__);
392 debug_dump(buf, sizeof(buf), deb_info);
394 /* program */
395 for (i = 0; i < sizeof(buf); i++) {
396 ret = af9013_write_reg(state, 0xae00 + i, buf[i]);
397 if (ret)
398 break;
401 return ret;
404 static int af9013_set_adc_ctrl(struct af9013_state *state)
406 int ret;
407 u8 buf[3], tmp, i;
408 u32 adc_cw;
410 deb_info("%s: adc_clock:%d\n", __func__, state->config.adc_clock);
412 /* adc frequency type */
413 switch (state->config.adc_clock) {
414 case 28800: /* 28.800 MHz */
415 tmp = 0;
416 break;
417 case 20480: /* 20.480 MHz */
418 tmp = 1;
419 break;
420 case 28000: /* 28.000 MHz */
421 tmp = 2;
422 break;
423 case 25000: /* 25.000 MHz */
424 tmp = 3;
425 break;
426 default:
427 err("invalid xtal");
428 return -EINVAL;
431 adc_cw = af913_div(state->config.adc_clock*1000, 1000000ul, 19ul);
433 buf[0] = (u8) ((adc_cw & 0x000000ff));
434 buf[1] = (u8) ((adc_cw & 0x0000ff00) >> 8);
435 buf[2] = (u8) ((adc_cw & 0x00ff0000) >> 16);
437 deb_info("%s: adc_cw:", __func__);
438 debug_dump(buf, sizeof(buf), deb_info);
440 /* program */
441 for (i = 0; i < sizeof(buf); i++) {
442 ret = af9013_write_reg(state, 0xd180 + i, buf[i]);
443 if (ret)
444 goto error;
446 ret = af9013_write_reg_bits(state, 0x9bd2, 0, 4, tmp);
447 error:
448 return ret;
451 static int af9013_set_freq_ctrl(struct af9013_state *state, fe_bandwidth_t bw)
453 int ret;
454 u16 addr;
455 u8 buf[3], i, j;
456 u32 adc_freq, freq_cw;
457 s8 bfs_spec_inv;
458 int if_sample_freq;
460 for (j = 0; j < 3; j++) {
461 if (j == 0) {
462 addr = 0xd140; /* fcw normal */
463 bfs_spec_inv = state->config.rf_spec_inv ? -1 : 1;
464 } else if (j == 1) {
465 addr = 0x9be7; /* fcw dummy ram */
466 bfs_spec_inv = state->config.rf_spec_inv ? -1 : 1;
467 } else {
468 addr = 0x9bea; /* fcw inverted */
469 bfs_spec_inv = state->config.rf_spec_inv ? 1 : -1;
472 adc_freq = state->config.adc_clock * 1000;
473 if_sample_freq = state->config.tuner_if * 1000;
475 /* TDA18271 uses different sampling freq for every bw */
476 if (state->config.tuner == AF9013_TUNER_TDA18271) {
477 switch (bw) {
478 case BANDWIDTH_6_MHZ:
479 if_sample_freq = 3300000; /* 3.3 MHz */
480 break;
481 case BANDWIDTH_7_MHZ:
482 if_sample_freq = 3800000; /* 3.8 MHz */
483 break;
484 case BANDWIDTH_8_MHZ:
485 default:
486 if_sample_freq = 4300000; /* 4.3 MHz */
487 break;
491 while (if_sample_freq > (adc_freq / 2))
492 if_sample_freq = if_sample_freq - adc_freq;
494 if (if_sample_freq >= 0)
495 bfs_spec_inv = bfs_spec_inv * (-1);
496 else
497 if_sample_freq = if_sample_freq * (-1);
499 freq_cw = af913_div(if_sample_freq, adc_freq, 23ul);
501 if (bfs_spec_inv == -1)
502 freq_cw = 0x00800000 - freq_cw;
504 buf[0] = (u8) ((freq_cw & 0x000000ff));
505 buf[1] = (u8) ((freq_cw & 0x0000ff00) >> 8);
506 buf[2] = (u8) ((freq_cw & 0x007f0000) >> 16);
509 deb_info("%s: freq_cw:", __func__);
510 debug_dump(buf, sizeof(buf), deb_info);
512 /* program */
513 for (i = 0; i < sizeof(buf); i++) {
514 ret = af9013_write_reg(state, addr++, buf[i]);
515 if (ret)
516 goto error;
519 error:
520 return ret;
523 static int af9013_set_ofdm_params(struct af9013_state *state,
524 struct dvb_ofdm_parameters *params, u8 *auto_mode)
526 int ret;
527 u8 i, buf[3] = {0, 0, 0};
528 *auto_mode = 0; /* set if parameters are requested to auto set */
530 /* Try auto-detect transmission parameters in case of AUTO requested or
531 garbage parameters given by application for compatibility.
532 MPlayer seems to provide garbage parameters currently. */
534 switch (params->transmission_mode) {
535 case TRANSMISSION_MODE_AUTO:
536 *auto_mode = 1;
537 case TRANSMISSION_MODE_2K:
538 break;
539 case TRANSMISSION_MODE_8K:
540 buf[0] |= (1 << 0);
541 break;
542 default:
543 deb_info("%s: invalid transmission_mode\n", __func__);
544 *auto_mode = 1;
547 switch (params->guard_interval) {
548 case GUARD_INTERVAL_AUTO:
549 *auto_mode = 1;
550 case GUARD_INTERVAL_1_32:
551 break;
552 case GUARD_INTERVAL_1_16:
553 buf[0] |= (1 << 2);
554 break;
555 case GUARD_INTERVAL_1_8:
556 buf[0] |= (2 << 2);
557 break;
558 case GUARD_INTERVAL_1_4:
559 buf[0] |= (3 << 2);
560 break;
561 default:
562 deb_info("%s: invalid guard_interval\n", __func__);
563 *auto_mode = 1;
566 switch (params->hierarchy_information) {
567 case HIERARCHY_AUTO:
568 *auto_mode = 1;
569 case HIERARCHY_NONE:
570 break;
571 case HIERARCHY_1:
572 buf[0] |= (1 << 4);
573 break;
574 case HIERARCHY_2:
575 buf[0] |= (2 << 4);
576 break;
577 case HIERARCHY_4:
578 buf[0] |= (3 << 4);
579 break;
580 default:
581 deb_info("%s: invalid hierarchy_information\n", __func__);
582 *auto_mode = 1;
585 switch (params->constellation) {
586 case QAM_AUTO:
587 *auto_mode = 1;
588 case QPSK:
589 break;
590 case QAM_16:
591 buf[1] |= (1 << 6);
592 break;
593 case QAM_64:
594 buf[1] |= (2 << 6);
595 break;
596 default:
597 deb_info("%s: invalid constellation\n", __func__);
598 *auto_mode = 1;
601 /* Use HP. How and which case we can switch to LP? */
602 buf[1] |= (1 << 4);
604 switch (params->code_rate_HP) {
605 case FEC_AUTO:
606 *auto_mode = 1;
607 case FEC_1_2:
608 break;
609 case FEC_2_3:
610 buf[2] |= (1 << 0);
611 break;
612 case FEC_3_4:
613 buf[2] |= (2 << 0);
614 break;
615 case FEC_5_6:
616 buf[2] |= (3 << 0);
617 break;
618 case FEC_7_8:
619 buf[2] |= (4 << 0);
620 break;
621 default:
622 deb_info("%s: invalid code_rate_HP\n", __func__);
623 *auto_mode = 1;
626 switch (params->code_rate_LP) {
627 case FEC_AUTO:
628 /* if HIERARCHY_NONE and FEC_NONE then LP FEC is set to FEC_AUTO
629 by dvb_frontend.c for compatibility */
630 if (params->hierarchy_information != HIERARCHY_NONE)
631 *auto_mode = 1;
632 case FEC_1_2:
633 break;
634 case FEC_2_3:
635 buf[2] |= (1 << 3);
636 break;
637 case FEC_3_4:
638 buf[2] |= (2 << 3);
639 break;
640 case FEC_5_6:
641 buf[2] |= (3 << 3);
642 break;
643 case FEC_7_8:
644 buf[2] |= (4 << 3);
645 break;
646 case FEC_NONE:
647 if (params->hierarchy_information == HIERARCHY_AUTO)
648 break;
649 default:
650 deb_info("%s: invalid code_rate_LP\n", __func__);
651 *auto_mode = 1;
654 switch (params->bandwidth) {
655 case BANDWIDTH_6_MHZ:
656 break;
657 case BANDWIDTH_7_MHZ:
658 buf[1] |= (1 << 2);
659 break;
660 case BANDWIDTH_8_MHZ:
661 buf[1] |= (2 << 2);
662 break;
663 default:
664 deb_info("%s: invalid bandwidth\n", __func__);
665 buf[1] |= (2 << 2); /* cannot auto-detect BW, try 8 MHz */
668 /* program */
669 for (i = 0; i < sizeof(buf); i++) {
670 ret = af9013_write_reg(state, 0xd3c0 + i, buf[i]);
671 if (ret)
672 break;
675 return ret;
678 static int af9013_reset(struct af9013_state *state, u8 sleep)
680 int ret;
681 u8 tmp, i;
682 deb_info("%s\n", __func__);
684 /* enable OFDM reset */
685 ret = af9013_write_reg_bits(state, 0xd417, 4, 1, 1);
686 if (ret)
687 goto error;
689 /* start reset mechanism */
690 ret = af9013_write_reg(state, 0xaeff, 1);
691 if (ret)
692 goto error;
694 /* reset is done when bit 1 is set */
695 for (i = 0; i < 150; i++) {
696 ret = af9013_read_reg_bits(state, 0xd417, 1, 1, &tmp);
697 if (ret)
698 goto error;
699 if (tmp)
700 break; /* reset done */
701 msleep(10);
703 if (!tmp)
704 return -ETIMEDOUT;
706 /* don't clear reset when going to sleep */
707 if (!sleep) {
708 /* clear OFDM reset */
709 ret = af9013_write_reg_bits(state, 0xd417, 1, 1, 0);
710 if (ret)
711 goto error;
713 /* disable OFDM reset */
714 ret = af9013_write_reg_bits(state, 0xd417, 4, 1, 0);
716 error:
717 return ret;
720 static int af9013_power_ctrl(struct af9013_state *state, u8 onoff)
722 int ret;
723 deb_info("%s: onoff:%d\n", __func__, onoff);
725 if (onoff) {
726 /* power on */
727 ret = af9013_write_reg_bits(state, 0xd73a, 3, 1, 0);
728 if (ret)
729 goto error;
730 ret = af9013_write_reg_bits(state, 0xd417, 1, 1, 0);
731 if (ret)
732 goto error;
733 ret = af9013_write_reg_bits(state, 0xd417, 4, 1, 0);
734 } else {
735 /* power off */
736 ret = af9013_reset(state, 1);
737 if (ret)
738 goto error;
739 ret = af9013_write_reg_bits(state, 0xd73a, 3, 1, 1);
741 error:
742 return ret;
745 static int af9013_lock_led(struct af9013_state *state, u8 onoff)
747 deb_info("%s: onoff:%d\n", __func__, onoff);
749 return af9013_write_reg_bits(state, 0xd730, 0, 1, onoff);
752 static int af9013_set_frontend(struct dvb_frontend *fe,
753 struct dvb_frontend_parameters *params)
755 struct af9013_state *state = fe->demodulator_priv;
756 int ret;
757 u8 auto_mode; /* auto set TPS */
759 deb_info("%s: freq:%d bw:%d\n", __func__, params->frequency,
760 params->u.ofdm.bandwidth);
762 state->frequency = params->frequency;
764 /* program CFOE coefficients */
765 ret = af9013_set_coeff(state, params->u.ofdm.bandwidth);
766 if (ret)
767 goto error;
769 /* program frequency control */
770 ret = af9013_set_freq_ctrl(state, params->u.ofdm.bandwidth);
771 if (ret)
772 goto error;
774 /* clear TPS lock flag (inverted flag) */
775 ret = af9013_write_reg_bits(state, 0xd330, 3, 1, 1);
776 if (ret)
777 goto error;
779 /* clear MPEG2 lock flag */
780 ret = af9013_write_reg_bits(state, 0xd507, 6, 1, 0);
781 if (ret)
782 goto error;
784 /* empty channel function */
785 ret = af9013_write_reg_bits(state, 0x9bfe, 0, 1, 0);
786 if (ret)
787 goto error;
789 /* empty DVB-T channel function */
790 ret = af9013_write_reg_bits(state, 0x9bc2, 0, 1, 0);
791 if (ret)
792 goto error;
794 /* program tuner */
795 if (fe->ops.tuner_ops.set_params)
796 fe->ops.tuner_ops.set_params(fe, params);
798 /* program TPS and bandwidth, check if auto mode needed */
799 ret = af9013_set_ofdm_params(state, &params->u.ofdm, &auto_mode);
800 if (ret)
801 goto error;
803 if (auto_mode) {
804 /* clear easy mode flag */
805 ret = af9013_write_reg(state, 0xaefd, 0);
806 deb_info("%s: auto TPS\n", __func__);
807 } else {
808 /* set easy mode flag */
809 ret = af9013_write_reg(state, 0xaefd, 1);
810 if (ret)
811 goto error;
812 ret = af9013_write_reg(state, 0xaefe, 0);
813 deb_info("%s: manual TPS\n", __func__);
815 if (ret)
816 goto error;
818 /* everything is set, lets try to receive channel - OFSM GO! */
819 ret = af9013_write_reg(state, 0xffff, 0);
820 if (ret)
821 goto error;
823 error:
824 return ret;
827 static int af9013_get_frontend(struct dvb_frontend *fe,
828 struct dvb_frontend_parameters *p)
830 struct af9013_state *state = fe->demodulator_priv;
831 int ret;
832 u8 i, buf[3];
833 deb_info("%s\n", __func__);
835 /* read TPS registers */
836 for (i = 0; i < 3; i++) {
837 ret = af9013_read_reg(state, 0xd3c0 + i, &buf[i]);
838 if (ret)
839 goto error;
842 switch ((buf[1] >> 6) & 3) {
843 case 0:
844 p->u.ofdm.constellation = QPSK;
845 break;
846 case 1:
847 p->u.ofdm.constellation = QAM_16;
848 break;
849 case 2:
850 p->u.ofdm.constellation = QAM_64;
851 break;
854 switch ((buf[0] >> 0) & 3) {
855 case 0:
856 p->u.ofdm.transmission_mode = TRANSMISSION_MODE_2K;
857 break;
858 case 1:
859 p->u.ofdm.transmission_mode = TRANSMISSION_MODE_8K;
862 switch ((buf[0] >> 2) & 3) {
863 case 0:
864 p->u.ofdm.guard_interval = GUARD_INTERVAL_1_32;
865 break;
866 case 1:
867 p->u.ofdm.guard_interval = GUARD_INTERVAL_1_16;
868 break;
869 case 2:
870 p->u.ofdm.guard_interval = GUARD_INTERVAL_1_8;
871 break;
872 case 3:
873 p->u.ofdm.guard_interval = GUARD_INTERVAL_1_4;
874 break;
877 switch ((buf[0] >> 4) & 7) {
878 case 0:
879 p->u.ofdm.hierarchy_information = HIERARCHY_NONE;
880 break;
881 case 1:
882 p->u.ofdm.hierarchy_information = HIERARCHY_1;
883 break;
884 case 2:
885 p->u.ofdm.hierarchy_information = HIERARCHY_2;
886 break;
887 case 3:
888 p->u.ofdm.hierarchy_information = HIERARCHY_4;
889 break;
892 switch ((buf[2] >> 0) & 7) {
893 case 0:
894 p->u.ofdm.code_rate_HP = FEC_1_2;
895 break;
896 case 1:
897 p->u.ofdm.code_rate_HP = FEC_2_3;
898 break;
899 case 2:
900 p->u.ofdm.code_rate_HP = FEC_3_4;
901 break;
902 case 3:
903 p->u.ofdm.code_rate_HP = FEC_5_6;
904 break;
905 case 4:
906 p->u.ofdm.code_rate_HP = FEC_7_8;
907 break;
910 switch ((buf[2] >> 3) & 7) {
911 case 0:
912 p->u.ofdm.code_rate_LP = FEC_1_2;
913 break;
914 case 1:
915 p->u.ofdm.code_rate_LP = FEC_2_3;
916 break;
917 case 2:
918 p->u.ofdm.code_rate_LP = FEC_3_4;
919 break;
920 case 3:
921 p->u.ofdm.code_rate_LP = FEC_5_6;
922 break;
923 case 4:
924 p->u.ofdm.code_rate_LP = FEC_7_8;
925 break;
928 switch ((buf[1] >> 2) & 3) {
929 case 0:
930 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
931 break;
932 case 1:
933 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
934 break;
935 case 2:
936 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
937 break;
940 p->inversion = INVERSION_AUTO;
941 p->frequency = state->frequency;
943 error:
944 return ret;
947 static int af9013_update_ber_unc(struct dvb_frontend *fe)
949 struct af9013_state *state = fe->demodulator_priv;
950 int ret;
951 u8 buf[3], i;
952 u32 error_bit_count = 0;
953 u32 total_bit_count = 0;
954 u32 abort_packet_count = 0;
956 state->ber = 0;
958 /* check if error bit count is ready */
959 ret = af9013_read_reg_bits(state, 0xd391, 4, 1, &buf[0]);
960 if (ret)
961 goto error;
962 if (!buf[0])
963 goto exit;
965 /* get RSD packet abort count */
966 for (i = 0; i < 2; i++) {
967 ret = af9013_read_reg(state, 0xd38a + i, &buf[i]);
968 if (ret)
969 goto error;
971 abort_packet_count = (buf[1] << 8) + buf[0];
973 /* get error bit count */
974 for (i = 0; i < 3; i++) {
975 ret = af9013_read_reg(state, 0xd387 + i, &buf[i]);
976 if (ret)
977 goto error;
979 error_bit_count = (buf[2] << 16) + (buf[1] << 8) + buf[0];
980 error_bit_count = error_bit_count - abort_packet_count * 8 * 8;
982 /* get used RSD counting period (10000 RSD packets used) */
983 for (i = 0; i < 2; i++) {
984 ret = af9013_read_reg(state, 0xd385 + i, &buf[i]);
985 if (ret)
986 goto error;
988 total_bit_count = (buf[1] << 8) + buf[0];
989 total_bit_count = total_bit_count - abort_packet_count;
990 total_bit_count = total_bit_count * 204 * 8;
992 if (total_bit_count)
993 state->ber = error_bit_count * 1000000000 / total_bit_count;
995 state->ucblocks += abort_packet_count;
997 deb_info("%s: err bits:%d total bits:%d abort count:%d\n", __func__,
998 error_bit_count, total_bit_count, abort_packet_count);
1000 /* set BER counting range */
1001 ret = af9013_write_reg(state, 0xd385, 10000 & 0xff);
1002 if (ret)
1003 goto error;
1004 ret = af9013_write_reg(state, 0xd386, 10000 >> 8);
1005 if (ret)
1006 goto error;
1007 /* reset and start BER counter */
1008 ret = af9013_write_reg_bits(state, 0xd391, 4, 1, 1);
1009 if (ret)
1010 goto error;
1012 exit:
1013 error:
1014 return ret;
1017 static int af9013_update_snr(struct dvb_frontend *fe)
1019 struct af9013_state *state = fe->demodulator_priv;
1020 int ret;
1021 u8 buf[3], i, len;
1022 u32 quant = 0;
1023 struct snr_table *uninitialized_var(snr_table);
1025 /* check if quantizer ready (for snr) */
1026 ret = af9013_read_reg_bits(state, 0xd2e1, 3, 1, &buf[0]);
1027 if (ret)
1028 goto error;
1029 if (buf[0]) {
1030 /* quantizer ready - read it */
1031 for (i = 0; i < 3; i++) {
1032 ret = af9013_read_reg(state, 0xd2e3 + i, &buf[i]);
1033 if (ret)
1034 goto error;
1036 quant = (buf[2] << 16) + (buf[1] << 8) + buf[0];
1038 /* read current constellation */
1039 ret = af9013_read_reg(state, 0xd3c1, &buf[0]);
1040 if (ret)
1041 goto error;
1043 switch ((buf[0] >> 6) & 3) {
1044 case 0:
1045 len = ARRAY_SIZE(qpsk_snr_table);
1046 snr_table = qpsk_snr_table;
1047 break;
1048 case 1:
1049 len = ARRAY_SIZE(qam16_snr_table);
1050 snr_table = qam16_snr_table;
1051 break;
1052 case 2:
1053 len = ARRAY_SIZE(qam64_snr_table);
1054 snr_table = qam64_snr_table;
1055 break;
1056 default:
1057 len = 0;
1058 break;
1061 if (len) {
1062 for (i = 0; i < len; i++) {
1063 if (quant < snr_table[i].val) {
1064 state->snr = snr_table[i].snr * 10;
1065 break;
1070 /* set quantizer super frame count */
1071 ret = af9013_write_reg(state, 0xd2e2, 1);
1072 if (ret)
1073 goto error;
1075 /* check quantizer availability */
1076 for (i = 0; i < 10; i++) {
1077 msleep(10);
1078 ret = af9013_read_reg_bits(state, 0xd2e6, 0, 1,
1079 &buf[0]);
1080 if (ret)
1081 goto error;
1082 if (!buf[0])
1083 break;
1086 /* reset quantizer */
1087 ret = af9013_write_reg_bits(state, 0xd2e1, 3, 1, 1);
1088 if (ret)
1089 goto error;
1092 error:
1093 return ret;
1096 static int af9013_update_signal_strength(struct dvb_frontend *fe)
1098 struct af9013_state *state = fe->demodulator_priv;
1099 int ret;
1100 u8 tmp0;
1101 u8 rf_gain, rf_50, rf_80, if_gain, if_50, if_80;
1102 int signal_strength;
1104 deb_info("%s\n", __func__);
1106 state->signal_strength = 0;
1108 ret = af9013_read_reg_bits(state, 0x9bee, 0, 1, &tmp0);
1109 if (ret)
1110 goto error;
1111 if (tmp0) {
1112 ret = af9013_read_reg(state, 0x9bbd, &rf_50);
1113 if (ret)
1114 goto error;
1115 ret = af9013_read_reg(state, 0x9bd0, &rf_80);
1116 if (ret)
1117 goto error;
1118 ret = af9013_read_reg(state, 0x9be2, &if_50);
1119 if (ret)
1120 goto error;
1121 ret = af9013_read_reg(state, 0x9be4, &if_80);
1122 if (ret)
1123 goto error;
1124 ret = af9013_read_reg(state, 0xd07c, &rf_gain);
1125 if (ret)
1126 goto error;
1127 ret = af9013_read_reg(state, 0xd07d, &if_gain);
1128 if (ret)
1129 goto error;
1130 signal_strength = (0xffff / (9 * (rf_50 + if_50) - \
1131 11 * (rf_80 + if_80))) * (10 * (rf_gain + if_gain) - \
1132 11 * (rf_80 + if_80));
1133 if (signal_strength < 0)
1134 signal_strength = 0;
1135 else if (signal_strength > 0xffff)
1136 signal_strength = 0xffff;
1138 state->signal_strength = signal_strength;
1141 error:
1142 return ret;
1145 static int af9013_update_statistics(struct dvb_frontend *fe)
1147 struct af9013_state *state = fe->demodulator_priv;
1148 int ret;
1150 if (time_before(jiffies, state->next_statistics_check))
1151 return 0;
1153 /* set minimum statistic update interval */
1154 state->next_statistics_check = jiffies + msecs_to_jiffies(1200);
1156 ret = af9013_update_signal_strength(fe);
1157 if (ret)
1158 goto error;
1159 ret = af9013_update_snr(fe);
1160 if (ret)
1161 goto error;
1162 ret = af9013_update_ber_unc(fe);
1163 if (ret)
1164 goto error;
1166 error:
1167 return ret;
1170 static int af9013_get_tune_settings(struct dvb_frontend *fe,
1171 struct dvb_frontend_tune_settings *fesettings)
1173 fesettings->min_delay_ms = 800;
1174 fesettings->step_size = 0;
1175 fesettings->max_drift = 0;
1177 return 0;
1180 static int af9013_read_status(struct dvb_frontend *fe, fe_status_t *status)
1182 struct af9013_state *state = fe->demodulator_priv;
1183 int ret = 0;
1184 u8 tmp;
1185 *status = 0;
1187 /* TPS lock */
1188 ret = af9013_read_reg_bits(state, 0xd330, 3, 1, &tmp);
1189 if (ret)
1190 goto error;
1191 if (tmp)
1192 *status |= FE_HAS_VITERBI | FE_HAS_CARRIER | FE_HAS_SIGNAL;
1194 /* MPEG2 lock */
1195 ret = af9013_read_reg_bits(state, 0xd507, 6, 1, &tmp);
1196 if (ret)
1197 goto error;
1198 if (tmp)
1199 *status |= FE_HAS_SYNC | FE_HAS_LOCK;
1201 if (!(*status & FE_HAS_SIGNAL)) {
1202 /* AGC lock */
1203 ret = af9013_read_reg_bits(state, 0xd1a0, 6, 1, &tmp);
1204 if (ret)
1205 goto error;
1206 if (tmp)
1207 *status |= FE_HAS_SIGNAL;
1210 if (!(*status & FE_HAS_CARRIER)) {
1211 /* CFO lock */
1212 ret = af9013_read_reg_bits(state, 0xd333, 7, 1, &tmp);
1213 if (ret)
1214 goto error;
1215 if (tmp)
1216 *status |= FE_HAS_CARRIER;
1219 if (!(*status & FE_HAS_CARRIER)) {
1220 /* SFOE lock */
1221 ret = af9013_read_reg_bits(state, 0xd334, 6, 1, &tmp);
1222 if (ret)
1223 goto error;
1224 if (tmp)
1225 *status |= FE_HAS_CARRIER;
1228 ret = af9013_update_statistics(fe);
1230 error:
1231 return ret;
1235 static int af9013_read_ber(struct dvb_frontend *fe, u32 *ber)
1237 struct af9013_state *state = fe->demodulator_priv;
1238 int ret;
1239 ret = af9013_update_statistics(fe);
1240 *ber = state->ber;
1241 return ret;
1244 static int af9013_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
1246 struct af9013_state *state = fe->demodulator_priv;
1247 int ret;
1248 ret = af9013_update_statistics(fe);
1249 *strength = state->signal_strength;
1250 return ret;
1253 static int af9013_read_snr(struct dvb_frontend *fe, u16 *snr)
1255 struct af9013_state *state = fe->demodulator_priv;
1256 int ret;
1257 ret = af9013_update_statistics(fe);
1258 *snr = state->snr;
1259 return ret;
1262 static int af9013_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
1264 struct af9013_state *state = fe->demodulator_priv;
1265 int ret;
1266 ret = af9013_update_statistics(fe);
1267 *ucblocks = state->ucblocks;
1268 return ret;
1271 static int af9013_sleep(struct dvb_frontend *fe)
1273 struct af9013_state *state = fe->demodulator_priv;
1274 int ret;
1275 deb_info("%s\n", __func__);
1277 ret = af9013_lock_led(state, 0);
1278 if (ret)
1279 goto error;
1281 ret = af9013_power_ctrl(state, 0);
1282 error:
1283 return ret;
1286 static int af9013_init(struct dvb_frontend *fe)
1288 struct af9013_state *state = fe->demodulator_priv;
1289 int ret, i, len;
1290 u8 tmp0, tmp1;
1291 struct regdesc *init;
1292 deb_info("%s\n", __func__);
1294 /* reset OFDM */
1295 ret = af9013_reset(state, 0);
1296 if (ret)
1297 goto error;
1299 /* power on */
1300 ret = af9013_power_ctrl(state, 1);
1301 if (ret)
1302 goto error;
1304 /* enable ADC */
1305 ret = af9013_write_reg(state, 0xd73a, 0xa4);
1306 if (ret)
1307 goto error;
1309 /* write API version to firmware */
1310 for (i = 0; i < sizeof(state->config.api_version); i++) {
1311 ret = af9013_write_reg(state, 0x9bf2 + i,
1312 state->config.api_version[i]);
1313 if (ret)
1314 goto error;
1317 /* program ADC control */
1318 ret = af9013_set_adc_ctrl(state);
1319 if (ret)
1320 goto error;
1322 /* set I2C master clock */
1323 ret = af9013_write_reg(state, 0xd416, 0x14);
1324 if (ret)
1325 goto error;
1327 /* set 16 embx */
1328 ret = af9013_write_reg_bits(state, 0xd700, 1, 1, 1);
1329 if (ret)
1330 goto error;
1332 /* set no trigger */
1333 ret = af9013_write_reg_bits(state, 0xd700, 2, 1, 0);
1334 if (ret)
1335 goto error;
1337 /* set read-update bit for constellation */
1338 ret = af9013_write_reg_bits(state, 0xd371, 1, 1, 1);
1339 if (ret)
1340 goto error;
1342 /* enable FEC monitor */
1343 ret = af9013_write_reg_bits(state, 0xd392, 1, 1, 1);
1344 if (ret)
1345 goto error;
1347 /* load OFSM settings */
1348 deb_info("%s: load ofsm settings\n", __func__);
1349 len = ARRAY_SIZE(ofsm_init);
1350 init = ofsm_init;
1351 for (i = 0; i < len; i++) {
1352 ret = af9013_write_reg_bits(state, init[i].addr, init[i].pos,
1353 init[i].len, init[i].val);
1354 if (ret)
1355 goto error;
1358 /* load tuner specific settings */
1359 deb_info("%s: load tuner specific settings\n", __func__);
1360 switch (state->config.tuner) {
1361 case AF9013_TUNER_MXL5003D:
1362 len = ARRAY_SIZE(tuner_init_mxl5003d);
1363 init = tuner_init_mxl5003d;
1364 break;
1365 case AF9013_TUNER_MXL5005D:
1366 case AF9013_TUNER_MXL5005R:
1367 len = ARRAY_SIZE(tuner_init_mxl5005);
1368 init = tuner_init_mxl5005;
1369 break;
1370 case AF9013_TUNER_ENV77H11D5:
1371 len = ARRAY_SIZE(tuner_init_env77h11d5);
1372 init = tuner_init_env77h11d5;
1373 break;
1374 case AF9013_TUNER_MT2060:
1375 len = ARRAY_SIZE(tuner_init_mt2060);
1376 init = tuner_init_mt2060;
1377 break;
1378 case AF9013_TUNER_MC44S803:
1379 len = ARRAY_SIZE(tuner_init_mc44s803);
1380 init = tuner_init_mc44s803;
1381 break;
1382 case AF9013_TUNER_QT1010:
1383 case AF9013_TUNER_QT1010A:
1384 len = ARRAY_SIZE(tuner_init_qt1010);
1385 init = tuner_init_qt1010;
1386 break;
1387 case AF9013_TUNER_MT2060_2:
1388 len = ARRAY_SIZE(tuner_init_mt2060_2);
1389 init = tuner_init_mt2060_2;
1390 break;
1391 case AF9013_TUNER_TDA18271:
1392 len = ARRAY_SIZE(tuner_init_tda18271);
1393 init = tuner_init_tda18271;
1394 break;
1395 case AF9013_TUNER_UNKNOWN:
1396 default:
1397 len = ARRAY_SIZE(tuner_init_unknown);
1398 init = tuner_init_unknown;
1399 break;
1402 for (i = 0; i < len; i++) {
1403 ret = af9013_write_reg_bits(state, init[i].addr, init[i].pos,
1404 init[i].len, init[i].val);
1405 if (ret)
1406 goto error;
1409 /* set TS mode */
1410 deb_info("%s: setting ts mode\n", __func__);
1411 tmp0 = 0; /* parallel mode */
1412 tmp1 = 0; /* serial mode */
1413 switch (state->config.output_mode) {
1414 case AF9013_OUTPUT_MODE_PARALLEL:
1415 tmp0 = 1;
1416 break;
1417 case AF9013_OUTPUT_MODE_SERIAL:
1418 tmp1 = 1;
1419 break;
1420 case AF9013_OUTPUT_MODE_USB:
1421 /* usb mode for AF9015 */
1422 default:
1423 break;
1425 ret = af9013_write_reg_bits(state, 0xd500, 1, 1, tmp0); /* parallel */
1426 if (ret)
1427 goto error;
1428 ret = af9013_write_reg_bits(state, 0xd500, 2, 1, tmp1); /* serial */
1429 if (ret)
1430 goto error;
1432 /* enable lock led */
1433 ret = af9013_lock_led(state, 1);
1434 if (ret)
1435 goto error;
1437 error:
1438 return ret;
1441 static struct dvb_frontend_ops af9013_ops;
1443 static int af9013_download_firmware(struct af9013_state *state)
1445 int i, len, packets, remainder, ret;
1446 const struct firmware *fw;
1447 u16 addr = 0x5100; /* firmware start address */
1448 u16 checksum = 0;
1449 u8 val;
1450 u8 fw_params[4];
1451 u8 *data;
1452 u8 *fw_file = AF9013_DEFAULT_FIRMWARE;
1454 msleep(100);
1455 /* check whether firmware is already running */
1456 ret = af9013_read_reg(state, 0x98be, &val);
1457 if (ret)
1458 goto error;
1459 else
1460 deb_info("%s: firmware status:%02x\n", __func__, val);
1462 if (val == 0x0c) /* fw is running, no need for download */
1463 goto exit;
1465 info("found a '%s' in cold state, will try to load a firmware",
1466 af9013_ops.info.name);
1468 /* request the firmware, this will block and timeout */
1469 ret = request_firmware(&fw, fw_file, state->i2c->dev.parent);
1470 if (ret) {
1471 err("did not find the firmware file. (%s) "
1472 "Please see linux/Documentation/dvb/ for more details" \
1473 " on firmware-problems. (%d)",
1474 fw_file, ret);
1475 goto error;
1478 info("downloading firmware from file '%s'", fw_file);
1480 /* calc checksum */
1481 for (i = 0; i < fw->size; i++)
1482 checksum += fw->data[i];
1484 fw_params[0] = checksum >> 8;
1485 fw_params[1] = checksum & 0xff;
1486 fw_params[2] = fw->size >> 8;
1487 fw_params[3] = fw->size & 0xff;
1489 /* write fw checksum & size */
1490 ret = af9013_write_ofsm_regs(state, 0x50fc,
1491 fw_params, sizeof(fw_params));
1492 if (ret)
1493 goto error_release;
1495 #define FW_PACKET_MAX_DATA 16
1497 packets = fw->size / FW_PACKET_MAX_DATA;
1498 remainder = fw->size % FW_PACKET_MAX_DATA;
1499 len = FW_PACKET_MAX_DATA;
1500 for (i = 0; i <= packets; i++) {
1501 if (i == packets) /* set size of the last packet */
1502 len = remainder;
1504 data = (u8 *)(fw->data + i * FW_PACKET_MAX_DATA);
1505 ret = af9013_write_ofsm_regs(state, addr, data, len);
1506 addr += FW_PACKET_MAX_DATA;
1508 if (ret) {
1509 err("firmware download failed at %d with %d", i, ret);
1510 goto error_release;
1514 /* request boot firmware */
1515 ret = af9013_write_reg(state, 0xe205, 1);
1516 if (ret)
1517 goto error_release;
1519 for (i = 0; i < 15; i++) {
1520 msleep(100);
1522 /* check firmware status */
1523 ret = af9013_read_reg(state, 0x98be, &val);
1524 if (ret)
1525 goto error_release;
1527 deb_info("%s: firmware status:%02x\n", __func__, val);
1529 if (val == 0x0c || val == 0x04) /* success or fail */
1530 break;
1533 if (val == 0x04) {
1534 err("firmware did not run");
1535 ret = -1;
1536 } else if (val != 0x0c) {
1537 err("firmware boot timeout");
1538 ret = -1;
1541 error_release:
1542 release_firmware(fw);
1543 error:
1544 exit:
1545 if (!ret)
1546 info("found a '%s' in warm state.", af9013_ops.info.name);
1547 return ret;
1550 static int af9013_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
1552 int ret;
1553 struct af9013_state *state = fe->demodulator_priv;
1554 deb_info("%s: enable:%d\n", __func__, enable);
1556 if (state->config.output_mode == AF9013_OUTPUT_MODE_USB)
1557 ret = af9013_write_reg_bits(state, 0xd417, 3, 1, enable);
1558 else
1559 ret = af9013_write_reg_bits(state, 0xd607, 2, 1, enable);
1561 return ret;
1564 static void af9013_release(struct dvb_frontend *fe)
1566 struct af9013_state *state = fe->demodulator_priv;
1567 kfree(state);
1570 static struct dvb_frontend_ops af9013_ops;
1572 struct dvb_frontend *af9013_attach(const struct af9013_config *config,
1573 struct i2c_adapter *i2c)
1575 int ret;
1576 struct af9013_state *state = NULL;
1577 u8 buf[3], i;
1579 /* allocate memory for the internal state */
1580 state = kzalloc(sizeof(struct af9013_state), GFP_KERNEL);
1581 if (state == NULL)
1582 goto error;
1584 /* setup the state */
1585 state->i2c = i2c;
1586 memcpy(&state->config, config, sizeof(struct af9013_config));
1588 /* chip version */
1589 ret = af9013_read_reg_bits(state, 0xd733, 4, 4, &buf[2]);
1590 if (ret)
1591 goto error;
1593 /* ROM version */
1594 for (i = 0; i < 2; i++) {
1595 ret = af9013_read_reg(state, 0x116b + i, &buf[i]);
1596 if (ret)
1597 goto error;
1599 deb_info("%s: chip version:%d ROM version:%d.%d\n", __func__,
1600 buf[2], buf[0], buf[1]);
1602 /* download firmware */
1603 if (state->config.output_mode != AF9013_OUTPUT_MODE_USB) {
1604 ret = af9013_download_firmware(state);
1605 if (ret)
1606 goto error;
1609 /* firmware version */
1610 for (i = 0; i < 3; i++) {
1611 ret = af9013_read_reg(state, 0x5103 + i, &buf[i]);
1612 if (ret)
1613 goto error;
1615 info("firmware version:%d.%d.%d", buf[0], buf[1], buf[2]);
1617 /* settings for mp2if */
1618 if (state->config.output_mode == AF9013_OUTPUT_MODE_USB) {
1619 /* AF9015 split PSB to 1.5k + 0.5k */
1620 ret = af9013_write_reg_bits(state, 0xd50b, 2, 1, 1);
1621 } else {
1622 /* AF9013 change the output bit to data7 */
1623 ret = af9013_write_reg_bits(state, 0xd500, 3, 1, 1);
1624 if (ret)
1625 goto error;
1626 /* AF9013 set mpeg to full speed */
1627 ret = af9013_write_reg_bits(state, 0xd502, 4, 1, 1);
1629 if (ret)
1630 goto error;
1631 ret = af9013_write_reg_bits(state, 0xd520, 4, 1, 1);
1632 if (ret)
1633 goto error;
1635 /* set GPIOs */
1636 for (i = 0; i < sizeof(state->config.gpio); i++) {
1637 ret = af9013_set_gpio(state, i, state->config.gpio[i]);
1638 if (ret)
1639 goto error;
1642 /* create dvb_frontend */
1643 memcpy(&state->frontend.ops, &af9013_ops,
1644 sizeof(struct dvb_frontend_ops));
1645 state->frontend.demodulator_priv = state;
1647 return &state->frontend;
1648 error:
1649 kfree(state);
1650 return NULL;
1652 EXPORT_SYMBOL(af9013_attach);
1654 static struct dvb_frontend_ops af9013_ops = {
1655 .info = {
1656 .name = "Afatech AF9013 DVB-T",
1657 .type = FE_OFDM,
1658 .frequency_min = 174000000,
1659 .frequency_max = 862000000,
1660 .frequency_stepsize = 250000,
1661 .frequency_tolerance = 0,
1662 .caps =
1663 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1664 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1665 FE_CAN_QPSK | FE_CAN_QAM_16 |
1666 FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
1667 FE_CAN_TRANSMISSION_MODE_AUTO |
1668 FE_CAN_GUARD_INTERVAL_AUTO |
1669 FE_CAN_HIERARCHY_AUTO |
1670 FE_CAN_RECOVER |
1671 FE_CAN_MUTE_TS
1674 .release = af9013_release,
1675 .init = af9013_init,
1676 .sleep = af9013_sleep,
1677 .i2c_gate_ctrl = af9013_i2c_gate_ctrl,
1679 .set_frontend = af9013_set_frontend,
1680 .get_frontend = af9013_get_frontend,
1682 .get_tune_settings = af9013_get_tune_settings,
1684 .read_status = af9013_read_status,
1685 .read_ber = af9013_read_ber,
1686 .read_signal_strength = af9013_read_signal_strength,
1687 .read_snr = af9013_read_snr,
1688 .read_ucblocks = af9013_read_ucblocks,
1691 module_param_named(debug, af9013_debug, int, 0644);
1692 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
1694 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1695 MODULE_DESCRIPTION("Afatech AF9013 DVB-T demodulator driver");
1696 MODULE_LICENSE("GPL");