blob: a7a347a3cad0ec26ecae4b5c7d030e6f480cce6a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for Zarlink DVB-T MT352 demodulator
3 *
4 * Written by Holger Waechtler <holger@qanu.de>
5 * and Daniel Mack <daniel@qanu.de>
6 *
7 * AVerMedia AVerTV DVB-T 771 support by
8 * Wolfram Joost <dbox2@frokaschwei.de>
9 *
10 * Support for Samsung TDTC9251DH01C(M) tuner
11 * Copyright (C) 2004 Antonio Mancuso <antonio.mancuso@digitaltelevision.it>
12 * Amauri Celani <acelani@essegi.net>
13 *
14 * DVICO FusionHDTV DVB-T1 and DVICO FusionHDTV DVB-T Lite support by
15 * Christopher Pascoe <c.pascoe@itee.uq.edu.au>
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 *
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.=
31 */
32
33#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/moduleparam.h>
36#include <linux/init.h>
37#include <linux/delay.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080038#include <linux/string.h>
39#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include "dvb_frontend.h"
42#include "mt352_priv.h"
43#include "mt352.h"
44
45struct mt352_state {
46 struct i2c_adapter* i2c;
47 struct dvb_frontend frontend;
48 struct dvb_frontend_ops ops;
49
50 /* configuration settings */
Johannes Stezenbach77b3bd02005-05-16 21:54:34 -070051 struct mt352_config config;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052};
53
54static int debug;
55#define dprintk(args...) \
56 do { \
57 if (debug) printk(KERN_DEBUG "mt352: " args); \
58 } while (0)
59
60static int mt352_single_write(struct dvb_frontend *fe, u8 reg, u8 val)
61{
62 struct mt352_state* state = fe->demodulator_priv;
63 u8 buf[2] = { reg, val };
Johannes Stezenbach77b3bd02005-05-16 21:54:34 -070064 struct i2c_msg msg = { .addr = state->config.demod_address, .flags = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 .buf = buf, .len = 2 };
66 int err = i2c_transfer(state->i2c, &msg, 1);
67 if (err != 1) {
68 printk("mt352_write() to reg %x failed (err = %d)!\n", reg, err);
69 return err;
70 }
71 return 0;
72}
73
74int mt352_write(struct dvb_frontend* fe, u8* ibuf, int ilen)
75{
76 int err,i;
77 for (i=0; i < ilen-1; i++)
78 if ((err = mt352_single_write(fe,ibuf[0]+i,ibuf[i+1])))
79 return err;
80
81 return 0;
82}
83
84static int mt352_read_register(struct mt352_state* state, u8 reg)
85{
86 int ret;
87 u8 b0 [] = { reg };
88 u8 b1 [] = { 0 };
Johannes Stezenbach77b3bd02005-05-16 21:54:34 -070089 struct i2c_msg msg [] = { { .addr = state->config.demod_address,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 .flags = 0,
91 .buf = b0, .len = 1 },
Johannes Stezenbach77b3bd02005-05-16 21:54:34 -070092 { .addr = state->config.demod_address,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 .flags = I2C_M_RD,
94 .buf = b1, .len = 1 } };
95
96 ret = i2c_transfer(state->i2c, msg, 2);
97
98 if (ret != 2) {
99 printk("%s: readreg error (reg=%d, ret==%i)\n",
100 __FUNCTION__, reg, ret);
101 return ret;
102 }
103
104 return b1[0];
105}
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static int mt352_sleep(struct dvb_frontend* fe)
108{
109 static u8 mt352_softdown[] = { CLOCK_CTL, 0x20, 0x08 };
110
111 mt352_write(fe, mt352_softdown, sizeof(mt352_softdown));
112 return 0;
113}
114
115static void mt352_calc_nominal_rate(struct mt352_state* state,
116 enum fe_bandwidth bandwidth,
117 unsigned char *buf)
118{
119 u32 adc_clock = 20480; /* 20.340 MHz */
120 u32 bw,value;
121
122 switch (bandwidth) {
123 case BANDWIDTH_6_MHZ:
124 bw = 6;
125 break;
126 case BANDWIDTH_7_MHZ:
127 bw = 7;
128 break;
129 case BANDWIDTH_8_MHZ:
130 default:
131 bw = 8;
132 break;
133 }
Johannes Stezenbach77b3bd02005-05-16 21:54:34 -0700134 if (state->config.adc_clock)
135 adc_clock = state->config.adc_clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 value = 64 * bw * (1<<16) / (7 * 8);
138 value = value * 1000 / adc_clock;
139 dprintk("%s: bw %d, adc_clock %d => 0x%x\n",
140 __FUNCTION__, bw, adc_clock, value);
141 buf[0] = msb(value);
142 buf[1] = lsb(value);
143}
144
145static void mt352_calc_input_freq(struct mt352_state* state,
146 unsigned char *buf)
147{
148 int adc_clock = 20480; /* 20.480000 MHz */
149 int if2 = 36167; /* 36.166667 MHz */
150 int ife,value;
151
Johannes Stezenbach77b3bd02005-05-16 21:54:34 -0700152 if (state->config.adc_clock)
153 adc_clock = state->config.adc_clock;
154 if (state->config.if2)
155 if2 = state->config.if2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 ife = (2*adc_clock - if2);
158 value = -16374 * ife / adc_clock;
159 dprintk("%s: if2 %d, ife %d, adc_clock %d => %d / 0x%x\n",
160 __FUNCTION__, if2, ife, adc_clock, value, value & 0x3fff);
161 buf[0] = msb(value);
162 buf[1] = lsb(value);
163}
164
165static int mt352_set_parameters(struct dvb_frontend* fe,
166 struct dvb_frontend_parameters *param)
167{
168 struct mt352_state* state = fe->demodulator_priv;
169 unsigned char buf[13];
170 static unsigned char tuner_go[] = { 0x5d, 0x01 };
171 static unsigned char fsm_go[] = { 0x5e, 0x01 };
172 unsigned int tps = 0;
173 struct dvb_ofdm_parameters *op = &param->u.ofdm;
174
175 switch (op->code_rate_HP) {
176 case FEC_2_3:
177 tps |= (1 << 7);
178 break;
179 case FEC_3_4:
180 tps |= (2 << 7);
181 break;
182 case FEC_5_6:
183 tps |= (3 << 7);
184 break;
185 case FEC_7_8:
186 tps |= (4 << 7);
187 break;
188 case FEC_1_2:
189 case FEC_AUTO:
190 break;
191 default:
192 return -EINVAL;
193 }
194
195 switch (op->code_rate_LP) {
196 case FEC_2_3:
197 tps |= (1 << 4);
198 break;
199 case FEC_3_4:
200 tps |= (2 << 4);
201 break;
202 case FEC_5_6:
203 tps |= (3 << 4);
204 break;
205 case FEC_7_8:
206 tps |= (4 << 4);
207 break;
208 case FEC_1_2:
209 case FEC_AUTO:
210 break;
211 case FEC_NONE:
212 if (op->hierarchy_information == HIERARCHY_AUTO ||
213 op->hierarchy_information == HIERARCHY_NONE)
214 break;
215 default:
216 return -EINVAL;
217 }
218
219 switch (op->constellation) {
220 case QPSK:
221 break;
222 case QAM_AUTO:
223 case QAM_16:
224 tps |= (1 << 13);
225 break;
226 case QAM_64:
227 tps |= (2 << 13);
228 break;
229 default:
230 return -EINVAL;
231 }
232
233 switch (op->transmission_mode) {
234 case TRANSMISSION_MODE_2K:
235 case TRANSMISSION_MODE_AUTO:
236 break;
237 case TRANSMISSION_MODE_8K:
238 tps |= (1 << 0);
239 break;
240 default:
241 return -EINVAL;
242 }
243
244 switch (op->guard_interval) {
245 case GUARD_INTERVAL_1_32:
246 case GUARD_INTERVAL_AUTO:
247 break;
248 case GUARD_INTERVAL_1_16:
249 tps |= (1 << 2);
250 break;
251 case GUARD_INTERVAL_1_8:
252 tps |= (2 << 2);
253 break;
254 case GUARD_INTERVAL_1_4:
255 tps |= (3 << 2);
256 break;
257 default:
258 return -EINVAL;
259 }
260
261 switch (op->hierarchy_information) {
262 case HIERARCHY_AUTO:
263 case HIERARCHY_NONE:
264 break;
265 case HIERARCHY_1:
266 tps |= (1 << 10);
267 break;
268 case HIERARCHY_2:
269 tps |= (2 << 10);
270 break;
271 case HIERARCHY_4:
272 tps |= (3 << 10);
273 break;
274 default:
275 return -EINVAL;
276 }
277
278
279 buf[0] = TPS_GIVEN_1; /* TPS_GIVEN_1 and following registers */
280
281 buf[1] = msb(tps); /* TPS_GIVEN_(1|0) */
282 buf[2] = lsb(tps);
283
284 buf[3] = 0x50; // old
285// buf[3] = 0xf4; // pinnacle
286
287 mt352_calc_nominal_rate(state, op->bandwidth, buf+4);
288 mt352_calc_input_freq(state, buf+6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Andrew de Quinceyd1544ec2006-04-18 17:47:10 -0300290 if (fe->ops->tuner_ops.pllbuf) {
291 fe->ops->tuner_ops.pllbuf(fe, param, buf+8, 5);
292 buf[8] <<= 1;
293 mt352_write(fe, buf, sizeof(buf));
294 }
Johannes Stezenbach77b3bd02005-05-16 21:54:34 -0700295 if (state->config.no_tuner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 /* start decoding */
297 mt352_write(fe, fsm_go, 2);
298 } else {
299 /* start tuning */
300 mt352_write(fe, tuner_go, 2);
301 }
302 return 0;
303}
304
305static int mt352_get_parameters(struct dvb_frontend* fe,
306 struct dvb_frontend_parameters *param)
307{
308 struct mt352_state* state = fe->demodulator_priv;
309 u16 tps;
310 u16 div;
311 u8 trl;
312 struct dvb_ofdm_parameters *op = &param->u.ofdm;
313 static const u8 tps_fec_to_api[8] =
314 {
315 FEC_1_2,
316 FEC_2_3,
317 FEC_3_4,
318 FEC_5_6,
319 FEC_7_8,
320 FEC_AUTO,
321 FEC_AUTO,
322 FEC_AUTO
323 };
324
325 if ( (mt352_read_register(state,0x00) & 0xC0) != 0xC0 )
326 return -EINVAL;
327
328 /* Use TPS_RECEIVED-registers, not the TPS_CURRENT-registers because
329 * the mt352 sometimes works with the wrong parameters
330 */
331 tps = (mt352_read_register(state, TPS_RECEIVED_1) << 8) | mt352_read_register(state, TPS_RECEIVED_0);
332 div = (mt352_read_register(state, CHAN_START_1) << 8) | mt352_read_register(state, CHAN_START_0);
333 trl = mt352_read_register(state, TRL_NOMINAL_RATE_1);
334
335 op->code_rate_HP = tps_fec_to_api[(tps >> 7) & 7];
336 op->code_rate_LP = tps_fec_to_api[(tps >> 4) & 7];
337
338 switch ( (tps >> 13) & 3)
339 {
340 case 0:
341 op->constellation = QPSK;
342 break;
343 case 1:
344 op->constellation = QAM_16;
345 break;
346 case 2:
347 op->constellation = QAM_64;
348 break;
349 default:
350 op->constellation = QAM_AUTO;
351 break;
352 }
353
354 op->transmission_mode = (tps & 0x01) ? TRANSMISSION_MODE_8K : TRANSMISSION_MODE_2K;
355
356 switch ( (tps >> 2) & 3)
357 {
358 case 0:
359 op->guard_interval = GUARD_INTERVAL_1_32;
360 break;
361 case 1:
362 op->guard_interval = GUARD_INTERVAL_1_16;
363 break;
364 case 2:
365 op->guard_interval = GUARD_INTERVAL_1_8;
366 break;
367 case 3:
368 op->guard_interval = GUARD_INTERVAL_1_4;
369 break;
370 default:
371 op->guard_interval = GUARD_INTERVAL_AUTO;
372 break;
373 }
374
375 switch ( (tps >> 10) & 7)
376 {
377 case 0:
378 op->hierarchy_information = HIERARCHY_NONE;
379 break;
380 case 1:
381 op->hierarchy_information = HIERARCHY_1;
382 break;
383 case 2:
384 op->hierarchy_information = HIERARCHY_2;
385 break;
386 case 3:
387 op->hierarchy_information = HIERARCHY_4;
388 break;
389 default:
390 op->hierarchy_information = HIERARCHY_AUTO;
391 break;
392 }
393
394 param->frequency = ( 500 * (div - IF_FREQUENCYx6) ) / 3 * 1000;
395
396 if (trl == 0x72)
397 op->bandwidth = BANDWIDTH_8_MHZ;
398 else if (trl == 0x64)
399 op->bandwidth = BANDWIDTH_7_MHZ;
400 else
401 op->bandwidth = BANDWIDTH_6_MHZ;
402
403
404 if (mt352_read_register(state, STATUS_2) & 0x02)
405 param->inversion = INVERSION_OFF;
406 else
407 param->inversion = INVERSION_ON;
408
409 return 0;
410}
411
412static int mt352_read_status(struct dvb_frontend* fe, fe_status_t* status)
413{
414 struct mt352_state* state = fe->demodulator_priv;
415 int s0, s1, s3;
416
417 /* FIXME:
418 *
419 * The MT352 design manual from Zarlink states (page 46-47):
420 *
421 * Notes about the TUNER_GO register:
422 *
423 * If the Read_Tuner_Byte (bit-1) is activated, then the tuner status
424 * byte is copied from the tuner to the STATUS_3 register and
425 * completion of the read operation is indicated by bit-5 of the
426 * INTERRUPT_3 register.
427 */
428
429 if ((s0 = mt352_read_register(state, STATUS_0)) < 0)
430 return -EREMOTEIO;
431 if ((s1 = mt352_read_register(state, STATUS_1)) < 0)
432 return -EREMOTEIO;
433 if ((s3 = mt352_read_register(state, STATUS_3)) < 0)
434 return -EREMOTEIO;
435
436 *status = 0;
437 if (s0 & (1 << 4))
438 *status |= FE_HAS_CARRIER;
439 if (s0 & (1 << 1))
440 *status |= FE_HAS_VITERBI;
441 if (s0 & (1 << 5))
442 *status |= FE_HAS_LOCK;
443 if (s1 & (1 << 1))
444 *status |= FE_HAS_SYNC;
445 if (s3 & (1 << 6))
446 *status |= FE_HAS_SIGNAL;
447
448 if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
449 (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
450 *status &= ~FE_HAS_LOCK;
451
452 return 0;
453}
454
455static int mt352_read_ber(struct dvb_frontend* fe, u32* ber)
456{
457 struct mt352_state* state = fe->demodulator_priv;
458
459 *ber = (mt352_read_register (state, RS_ERR_CNT_2) << 16) |
460 (mt352_read_register (state, RS_ERR_CNT_1) << 8) |
461 (mt352_read_register (state, RS_ERR_CNT_0));
462
463 return 0;
464}
465
466static int mt352_read_signal_strength(struct dvb_frontend* fe, u16* strength)
467{
468 struct mt352_state* state = fe->demodulator_priv;
469
Barry Scott4ff4ac12005-09-09 13:02:29 -0700470 /* align the 12 bit AGC gain with the most significant bits */
471 u16 signal = ((mt352_read_register(state, AGC_GAIN_1) & 0x0f) << 12) |
472 (mt352_read_register(state, AGC_GAIN_0) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Barry Scott4ff4ac12005-09-09 13:02:29 -0700474 /* inverse of gain is signal strength */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 *strength = ~signal;
476 return 0;
477}
478
479static int mt352_read_snr(struct dvb_frontend* fe, u16* snr)
480{
481 struct mt352_state* state = fe->demodulator_priv;
482
483 u8 _snr = mt352_read_register (state, SNR);
484 *snr = (_snr << 8) | _snr;
485
486 return 0;
487}
488
489static int mt352_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
490{
491 struct mt352_state* state = fe->demodulator_priv;
492
493 *ucblocks = (mt352_read_register (state, RS_UBC_1) << 8) |
494 (mt352_read_register (state, RS_UBC_0));
495
496 return 0;
497}
498
499static int mt352_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fe_tune_settings)
500{
501 fe_tune_settings->min_delay_ms = 800;
502 fe_tune_settings->step_size = 0;
503 fe_tune_settings->max_drift = 0;
504
505 return 0;
506}
507
508static int mt352_init(struct dvb_frontend* fe)
509{
510 struct mt352_state* state = fe->demodulator_priv;
511
512 static u8 mt352_reset_attach [] = { RESET, 0xC0 };
513
514 dprintk("%s: hello\n",__FUNCTION__);
515
516 if ((mt352_read_register(state, CLOCK_CTL) & 0x10) == 0 ||
517 (mt352_read_register(state, CONFIG) & 0x20) == 0) {
518
519 /* Do a "hard" reset */
520 mt352_write(fe, mt352_reset_attach, sizeof(mt352_reset_attach));
Johannes Stezenbach77b3bd02005-05-16 21:54:34 -0700521 return state->config.demod_init(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523
524 return 0;
525}
526
527static void mt352_release(struct dvb_frontend* fe)
528{
529 struct mt352_state* state = fe->demodulator_priv;
530 kfree(state);
531}
532
533static struct dvb_frontend_ops mt352_ops;
534
535struct dvb_frontend* mt352_attach(const struct mt352_config* config,
536 struct i2c_adapter* i2c)
537{
538 struct mt352_state* state = NULL;
539
540 /* allocate memory for the internal state */
Panagiotis Issaris74081872006-01-11 19:40:56 -0200541 state = kzalloc(sizeof(struct mt352_state), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (state == NULL) goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 /* setup the state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 state->i2c = i2c;
Johannes Stezenbach77b3bd02005-05-16 21:54:34 -0700546 memcpy(&state->config,config,sizeof(struct mt352_config));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 memcpy(&state->ops, &mt352_ops, sizeof(struct dvb_frontend_ops));
548
549 /* check if the demod is there */
550 if (mt352_read_register(state, CHIP_ID) != ID_MT352) goto error;
551
552 /* create dvb_frontend */
553 state->frontend.ops = &state->ops;
554 state->frontend.demodulator_priv = state;
555 return &state->frontend;
556
557error:
558 kfree(state);
559 return NULL;
560}
561
562static struct dvb_frontend_ops mt352_ops = {
563
564 .info = {
565 .name = "Zarlink MT352 DVB-T",
566 .type = FE_OFDM,
567 .frequency_min = 174000000,
568 .frequency_max = 862000000,
569 .frequency_stepsize = 166667,
570 .frequency_tolerance = 0,
571 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
572 FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
573 FE_CAN_FEC_AUTO |
574 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
575 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
576 FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER |
577 FE_CAN_MUTE_TS
578 },
579
580 .release = mt352_release,
581
582 .init = mt352_init,
583 .sleep = mt352_sleep,
584
585 .set_frontend = mt352_set_parameters,
586 .get_frontend = mt352_get_parameters,
587 .get_tune_settings = mt352_get_tune_settings,
588
589 .read_status = mt352_read_status,
590 .read_ber = mt352_read_ber,
591 .read_signal_strength = mt352_read_signal_strength,
592 .read_snr = mt352_read_snr,
593 .read_ucblocks = mt352_read_ucblocks,
594};
595
596module_param(debug, int, 0644);
597MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
598
599MODULE_DESCRIPTION("Zarlink MT352 DVB-T Demodulator driver");
600MODULE_AUTHOR("Holger Waechtler, Daniel Mack, Antonio Mancuso");
601MODULE_LICENSE("GPL");
602
603EXPORT_SYMBOL(mt352_attach);
604EXPORT_SYMBOL(mt352_write);