vserver 1.9.5.x5
[linux-2.6.git] / drivers / media / dvb / frontends / mt352.c
1 /*
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>
38
39 #include "dvb_frontend.h"
40 #include "mt352_priv.h"
41 #include "mt352.h"
42
43 struct mt352_state {
44
45         struct i2c_adapter* i2c;
46
47         struct dvb_frontend_ops ops;
48
49         /* configuration settings */
50         const struct mt352_config* config;
51
52         struct dvb_frontend frontend;
53 };
54
55 static int debug;
56 #define dprintk(args...) \
57 do {                                                                    \
58                 if (debug) printk(KERN_DEBUG "mt352: " args); \
59 } while (0)
60
61 static int mt352_single_write(struct dvb_frontend *fe, u8 reg, u8 val)
62 {
63         struct mt352_state* state = (struct mt352_state*) fe->demodulator_priv;
64         u8 buf[2] = { reg, val };
65         struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0,
66                                .buf = buf, .len = 2 };
67         int err = i2c_transfer(state->i2c, &msg, 1);
68         if (err != 1) {
69                 dprintk("mt352_write() to reg %x failed (err = %d)!\n", reg, err);
70                 return err;
71 }
72         return 0; 
73 }
74
75 int mt352_write(struct dvb_frontend* fe, u8* ibuf, int ilen)
76 {
77         int err,i;
78         for (i=0; i < ilen-1; i++)
79                 if ((err = mt352_single_write(fe,ibuf[0]+i,ibuf[i+1]))) 
80                         return err;
81
82         return 0;
83 }
84
85 static u8 mt352_read_register(struct mt352_state* state, u8 reg)
86 {
87         int ret;
88         u8 b0 [] = { reg };
89         u8 b1 [] = { 0 };
90         struct i2c_msg msg [] = { { .addr = state->config->demod_address,
91                                     .flags = 0,
92                                     .buf = b0, .len = 1 },
93                                   { .addr = state->config->demod_address,
94                                     .flags = I2C_M_RD,
95                                     .buf = b1, .len = 1 } };
96
97         ret = i2c_transfer(state->i2c, msg, 2);
98
99         if (ret != 2)
100                 dprintk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret);
101
102         return b1[0];
103 }
104
105 u8 mt352_read(struct dvb_frontend *fe, u8 reg)
106 {
107         return mt352_read_register(fe->demodulator_priv,reg);
108 }
109
110
111
112
113
114
115
116
117 static int mt352_sleep(struct dvb_frontend* fe)
118 {
119         static u8 mt352_softdown[] = { CLOCK_CTL, 0x20, 0x08 };
120
121         mt352_write(fe, mt352_softdown, sizeof(mt352_softdown));
122
123         return 0;
124 }
125
126 static int mt352_set_parameters(struct dvb_frontend* fe,
127                                 struct dvb_frontend_parameters *param)
128 {
129         struct mt352_state* state = (struct mt352_state*) fe->demodulator_priv;
130         unsigned char buf[14];
131         unsigned int tps = 0;
132         struct dvb_ofdm_parameters *op = &param->u.ofdm;
133         int i;
134
135         switch (op->code_rate_HP) {
136                 case FEC_2_3:
137                         tps |= (1 << 7);
138                         break;
139                 case FEC_3_4:
140                         tps |= (2 << 7);
141                         break;
142                 case FEC_5_6:
143                         tps |= (3 << 7);
144                         break;
145                 case FEC_7_8:
146                         tps |= (4 << 7);
147                         break;
148                 case FEC_1_2:
149                 case FEC_AUTO:
150                         break;
151                 default:
152                         return -EINVAL;
153         }
154
155         switch (op->code_rate_LP) {
156                 case FEC_2_3:
157                         tps |= (1 << 4);
158                         break;
159                 case FEC_3_4:
160                         tps |= (2 << 4);
161                         break;
162                 case FEC_5_6:
163                         tps |= (3 << 4);
164                         break;
165                 case FEC_7_8:
166                         tps |= (4 << 4);
167                         break;
168                 case FEC_1_2:
169                 case FEC_AUTO:
170                         break;
171                 case FEC_NONE:
172                         if (op->hierarchy_information == HIERARCHY_AUTO ||
173                             op->hierarchy_information == HIERARCHY_NONE)
174                                 break;
175                 default:
176                         return -EINVAL;
177         }
178
179         switch (op->constellation) {
180                 case QPSK:
181                         break;
182                 case QAM_AUTO:
183                 case QAM_16:
184                         tps |= (1 << 13);
185                         break;
186                 case QAM_64:
187                         tps |= (2 << 13);
188                         break;
189                 default:
190                         return -EINVAL;
191         }
192
193         switch (op->transmission_mode) {
194                 case TRANSMISSION_MODE_2K:
195                 case TRANSMISSION_MODE_AUTO:
196                         break;
197                 case TRANSMISSION_MODE_8K:
198                         tps |= (1 << 0);
199                         break;
200                 default:
201                         return -EINVAL;
202         }
203
204         switch (op->guard_interval) {
205                 case GUARD_INTERVAL_1_32:
206                 case GUARD_INTERVAL_AUTO:
207                         break;
208                 case GUARD_INTERVAL_1_16:
209                         tps |= (1 << 2);
210                         break;
211                 case GUARD_INTERVAL_1_8:
212                         tps |= (2 << 2);
213                         break;
214                 case GUARD_INTERVAL_1_4:
215                         tps |= (3 << 2);
216                         break;
217                 default:
218                         return -EINVAL;
219         }
220
221         switch (op->hierarchy_information) {
222                 case HIERARCHY_AUTO:
223                 case HIERARCHY_NONE:
224                         break;
225                 case HIERARCHY_1:
226                         tps |= (1 << 10);
227                         break;
228                 case HIERARCHY_2:
229                         tps |= (2 << 10);
230                         break;
231                 case HIERARCHY_4:
232                         tps |= (3 << 10);
233                         break;
234                 default:
235                         return -EINVAL;
236         }
237
238
239         buf[0] = TPS_GIVEN_1; /* TPS_GIVEN_1 and following registers */
240
241         buf[1] = msb(tps);      /* TPS_GIVEN_(1|0) */
242         buf[2] = lsb(tps);
243
244         buf[3] = 0x50;
245
246         /**
247          *  these settings assume 20.48MHz f_ADC, for other tuners you might
248          *  need other values. See p. 33 in the MT352 Design Manual.
249          */
250         if (op->bandwidth == BANDWIDTH_8_MHZ) {
251                 buf[4] = 0x72;  /* TRL_NOMINAL_RATE_(1|0) */
252                 buf[5] = 0x49;
253         } else if (op->bandwidth == BANDWIDTH_7_MHZ) {
254                 buf[4] = 0x64;
255                 buf[5] = 0x00;
256         } else {                /* 6MHz */
257                 buf[4] = 0x55;
258                 buf[5] = 0xb7;
259         }
260
261         buf[6] = 0x31;  /* INPUT_FREQ_(1|0), 20.48MHz clock, 36.166667MHz IF */
262         buf[7] = 0x05;  /* see MT352 Design Manual page 32 for details */
263
264         state->config->pll_set(fe, param, buf+8);
265
266         buf[13] = 0x01; /* TUNER_GO!! */
267
268         /* Only send the tuning request if the tuner doesn't have the requested
269          * parameters already set.  Enhances tuning time and prevents stream
270          * breakup when retuning the same transponder. */
271         for (i = 1; i < 13; i++)
272                 if (buf[i] != mt352_read_register(state, i + 0x50)) {
273                         mt352_write(fe, buf, sizeof(buf));
274                         break;
275                 }
276
277         return 0;
278 }
279
280 static int mt352_get_parameters(struct dvb_frontend* fe,
281                                 struct dvb_frontend_parameters *param)
282 {
283         struct mt352_state* state = (struct mt352_state*) fe->demodulator_priv;
284         u16 tps;
285         u16 div;
286         u8 trl;
287         struct dvb_ofdm_parameters *op = &param->u.ofdm;
288         static const u8 tps_fec_to_api[8] =
289         {
290                 FEC_1_2,
291                 FEC_2_3,
292                 FEC_3_4,
293                 FEC_5_6,
294                 FEC_7_8,
295                 FEC_AUTO,
296                 FEC_AUTO,
297                 FEC_AUTO
298         };
299
300         if ( (mt352_read_register(state,0x00) & 0xC0) != 0xC0 )
301         {
302                 return -EINVAL;
303         }
304
305         /* Use TPS_RECEIVED-registers, not the TPS_CURRENT-registers because
306          * the mt352 sometimes works with the wrong parameters
307          */
308         tps = (mt352_read_register(state, TPS_RECEIVED_1) << 8) | mt352_read_register(state, TPS_RECEIVED_0);
309         div = (mt352_read_register(state, CHAN_START_1) << 8) | mt352_read_register(state, CHAN_START_0);
310         trl = mt352_read_register(state, TRL_NOMINAL_RATE_1);
311
312         op->code_rate_HP = tps_fec_to_api[(tps >> 7) & 7];
313         op->code_rate_LP = tps_fec_to_api[(tps >> 4) & 7];
314
315         switch ( (tps >> 13) & 3)
316         {
317                 case 0:
318                         op->constellation = QPSK;
319                         break;
320                 case 1:
321                         op->constellation = QAM_16;
322                         break;
323                 case 2:
324                         op->constellation = QAM_64;
325                         break;
326                 default:
327                         op->constellation = QAM_AUTO;
328                         break;
329         }
330
331         op->transmission_mode = (tps & 0x01) ? TRANSMISSION_MODE_8K : TRANSMISSION_MODE_2K;
332
333         switch ( (tps >> 2) & 3)
334         {
335                 case 0:
336                         op->guard_interval = GUARD_INTERVAL_1_32;
337                         break;
338                 case 1:
339                         op->guard_interval = GUARD_INTERVAL_1_16;
340                         break;
341                 case 2:
342                         op->guard_interval = GUARD_INTERVAL_1_8;
343                         break;
344                 case 3:
345                         op->guard_interval = GUARD_INTERVAL_1_4;
346                         break;
347                 default:
348                         op->guard_interval = GUARD_INTERVAL_AUTO;
349                         break;
350         }
351
352         switch ( (tps >> 10) & 7)
353         {
354                 case 0:
355                         op->hierarchy_information = HIERARCHY_NONE;
356                         break;
357                 case 1:
358                         op->hierarchy_information = HIERARCHY_1;
359                         break;
360                 case 2:
361                         op->hierarchy_information = HIERARCHY_2;
362                         break;
363                 case 3:
364                         op->hierarchy_information = HIERARCHY_4;
365                         break;
366                 default:
367                         op->hierarchy_information = HIERARCHY_AUTO;
368                         break;
369         }
370
371         param->frequency = ( 500 * (div - IF_FREQUENCYx6) ) / 3 * 1000;
372
373         if (trl == 0x72)
374         {
375                 op->bandwidth = BANDWIDTH_8_MHZ;
376         }
377         else if (trl == 0x64)
378         {
379                 op->bandwidth = BANDWIDTH_7_MHZ;
380         }
381         else
382         {
383                 op->bandwidth = BANDWIDTH_6_MHZ;
384         }
385
386
387         if (mt352_read_register(state, STATUS_2) & 0x02)
388                 param->inversion = INVERSION_OFF;
389         else
390                 param->inversion = INVERSION_ON;
391
392         return 0;
393 }
394
395 static int mt352_read_status(struct dvb_frontend* fe, fe_status_t* status)
396 {
397         struct mt352_state* state = (struct mt352_state*) fe->demodulator_priv;
398         u8 r;
399
400                 *status = 0;
401         r = mt352_read_register (state, STATUS_0);
402                 if (r & (1 << 4))
403                         *status = FE_HAS_CARRIER;
404                 if (r & (1 << 1))
405                         *status |= FE_HAS_VITERBI;
406                 if (r & (1 << 5))
407                         *status |= FE_HAS_LOCK;
408
409         r = mt352_read_register (state, STATUS_1);
410                 if (r & (1 << 1))
411                         *status |= FE_HAS_SYNC;
412
413         r = mt352_read_register (state, STATUS_3);
414                 if (r & (1 << 6))
415                         *status |= FE_HAS_SIGNAL;
416
417         if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
418                       (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
419                 *status &= ~FE_HAS_LOCK;
420
421         return 0;
422 }
423
424 static int mt352_read_ber(struct dvb_frontend* fe, u32* ber)
425 {
426         struct mt352_state* state = (struct mt352_state*) fe->demodulator_priv;
427
428         *ber = (mt352_read_register (state, RS_ERR_CNT_2) << 16) |
429                (mt352_read_register (state, RS_ERR_CNT_1) << 8) |
430                (mt352_read_register (state, RS_ERR_CNT_0));
431
432                         return 0;
433         }
434
435 static int mt352_read_signal_strength(struct dvb_frontend* fe, u16* strength)
436 {
437         struct mt352_state* state = (struct mt352_state*) fe->demodulator_priv;
438
439         u16 signal = (mt352_read_register (state, AGC_GAIN_3) << 8) |
440                      (mt352_read_register (state, AGC_GAIN_2));
441
442         *strength = ~signal;
443         return 0;
444 }
445
446 static int mt352_read_snr(struct dvb_frontend* fe, u16* snr)
447 {
448         struct mt352_state* state = (struct mt352_state*) fe->demodulator_priv;
449
450         u8 _snr = mt352_read_register (state, SNR);
451         *snr = (_snr << 8) | _snr;
452
453         return 0;
454 }
455
456 static int mt352_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
457 {
458         struct mt352_state* state = (struct mt352_state*) fe->demodulator_priv;
459
460         *ucblocks = (mt352_read_register (state,  RS_UBC_1) << 8) |
461                     (mt352_read_register (state,  RS_UBC_0));
462
463         return 0;
464         }
465
466 static int mt352_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fe_tune_settings)
467 {
468         fe_tune_settings->min_delay_ms = 800;
469         fe_tune_settings->step_size = 0;
470         fe_tune_settings->max_drift = 0;
471
472         return 0;
473         }
474
475 static int mt352_init(struct dvb_frontend* fe)
476 {
477         struct mt352_state* state = (struct mt352_state*) fe->demodulator_priv;
478
479         static u8 mt352_reset_attach [] = { RESET, 0xC0 };
480
481         if ((mt352_read_register(state, CLOCK_CTL) & 0x10) == 0 ||
482             (mt352_read_register(state, CONFIG) & 0x20) == 0) {
483
484         /* Do a "hard" reset */
485                 mt352_write(fe, mt352_reset_attach, sizeof(mt352_reset_attach));
486                 return state->config->demod_init(fe);
487         }
488
489         return 0;
490         }
491
492 static void mt352_release(struct dvb_frontend* fe)
493 {
494         struct mt352_state* state = (struct mt352_state*) fe->demodulator_priv;
495                 kfree(state);
496         }
497
498 static struct dvb_frontend_ops mt352_ops;
499
500 struct dvb_frontend* mt352_attach(const struct mt352_config* config,
501                                   struct i2c_adapter* i2c)
502 {
503         struct mt352_state* state = NULL;
504
505         /* allocate memory for the internal state */
506         state = (struct mt352_state*) kmalloc(sizeof(struct mt352_state), GFP_KERNEL);
507         if (state == NULL) goto error;
508
509         /* setup the state */
510         state->config = config;
511         state->i2c = i2c;
512         memcpy(&state->ops, &mt352_ops, sizeof(struct dvb_frontend_ops));
513
514         /* check if the demod is there */
515         if (mt352_read_register(state, CHIP_ID) != ID_MT352) goto error;
516
517         /* create dvb_frontend */
518         state->frontend.ops = &state->ops;
519         state->frontend.demodulator_priv = state;
520         return &state->frontend;
521
522 error:
523         if (state) kfree(state);
524         return NULL;
525 }
526
527 static struct dvb_frontend_ops mt352_ops = {
528
529         .info = {
530                 .name                   = "Zarlink MT352 DVB-T",
531                 .type                   = FE_OFDM,
532                 .frequency_min          = 174000000,
533                 .frequency_max          = 862000000,
534                 .frequency_stepsize     = 166667,
535                 .frequency_tolerance    = 0,
536                 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
537                         FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
538                         FE_CAN_FEC_AUTO |
539                         FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
540                         FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
541                         FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER |
542                         FE_CAN_MUTE_TS
543         },
544
545         .release = mt352_release,
546
547         .init = mt352_init,
548         .sleep = mt352_sleep,
549
550         .set_frontend = mt352_set_parameters,
551         .get_frontend = mt352_get_parameters,
552         .get_tune_settings = mt352_get_tune_settings,
553
554         .read_status = mt352_read_status,
555         .read_ber = mt352_read_ber,
556         .read_signal_strength = mt352_read_signal_strength,
557         .read_snr = mt352_read_snr,
558         .read_ucblocks = mt352_read_ucblocks,
559 };
560
561 module_param(debug, int, 0644);
562 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
563
564 MODULE_DESCRIPTION("Zarlink MT352 DVB-T Demodulator driver");
565 MODULE_AUTHOR("Holger Waechtler, Daniel Mack, Antonio Mancuso");
566 MODULE_LICENSE("GPL");
567
568 EXPORT_SYMBOL(mt352_attach);
569 EXPORT_SYMBOL(mt352_write);
570 EXPORT_SYMBOL(mt352_read);