ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / media / dvb / frontends / mt312.c
1 /* 
2     Driver for Zarlink MT312 Satellite Channel Decoder
3
4     Copyright (C) 2003 Andreas Oberritter <obi@linuxtv.org>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21     References:
22     http://products.zarlink.com/product_profiles/MT312.htm
23     http://products.zarlink.com/product_profiles/SL1935.htm
24 */
25
26 #include <linux/delay.h>
27 #include <linux/errno.h>
28 #include <linux/init.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31
32 #include "dvb_frontend.h"
33 #include "mt312.h"
34
35 #define I2C_ADDR_MT312          0x0e
36 #define I2C_ADDR_SL1935         0x61
37 #define I2C_ADDR_TSA5059        0x61
38
39 #define MT312_DEBUG             0
40
41 #define MT312_SYS_CLK           90000000UL      /* 90 MHz */
42 #define MT312_LPOWER_SYS_CLK    60000000UL      /* 60 MHz */
43 #define MT312_PLL_CLK           10000000UL      /* 10 MHz */
44
45 /* number of active frontends */
46 static int mt312_count = 0;
47
48 #if MT312_DEBUG == 0
49 #define dprintk(x...)
50 #else
51 static int debug = 0;
52 #define dprintk if(debug == 1) printk
53 #endif
54
55 static struct dvb_frontend_info mt312_info = {
56         .name = "Zarlink MT312",
57         .type = FE_QPSK,
58         .frequency_min = 950000,
59         .frequency_max = 2150000,
60         .frequency_stepsize = (MT312_PLL_CLK / 1000) / 128,
61         /*.frequency_tolerance = 29500,         FIXME: binary compatibility waste? */
62         .symbol_rate_min = MT312_SYS_CLK / 128,
63         .symbol_rate_max = MT312_SYS_CLK / 2,
64         /*.symbol_rate_tolerance = 500,         FIXME: binary compatibility waste? 2% */
65         .notifier_delay = 0,
66         .caps =
67             FE_CAN_INVERSION_AUTO | FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
68             FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
69             FE_CAN_FEC_AUTO | FE_CAN_QPSK | FE_CAN_MUTE_TS | 
70             FE_CAN_RECOVER
71 };
72
73 static int mt312_read(struct dvb_i2c_bus *i2c,
74                       const enum mt312_reg_addr reg, void *buf,
75                       const size_t count)
76 {
77         int ret;
78         struct i2c_msg msg[2];
79         u8 regbuf[1] = { reg };
80
81         msg[0].addr = I2C_ADDR_MT312;
82         msg[0].flags = 0;
83         msg[0].buf = regbuf;
84         msg[0].len = 1;
85         msg[1].addr = I2C_ADDR_MT312;
86         msg[1].flags = I2C_M_RD;
87         msg[1].buf = buf;
88         msg[1].len = count;
89
90         ret = i2c->xfer(i2c, msg, 2);
91
92         if ((ret != 2) && (mt312_count != 0)) {
93                 printk(KERN_ERR "%s: ret == %d\n", __FUNCTION__, ret);
94                 return -EREMOTEIO;
95         }
96 #if MT312_DEBUG
97         if(debug) {
98                 int i;
99                 printk(KERN_INFO "R(%d):", reg & 0x7f);
100                 for (i = 0; i < count; i++)
101                         printk(" %02x", ((const u8 *) buf)[i]);
102                 printk("\n");
103         }
104 #endif
105
106         return 0;
107 }
108
109 static int mt312_write(struct dvb_i2c_bus *i2c,
110                        const enum mt312_reg_addr reg, const void *src,
111                        const size_t count)
112 {
113         int ret;
114         u8 buf[count + 1];
115         struct i2c_msg msg;
116
117 #if MT312_DEBUG
118         if(debug) {
119                 int i;
120                 printk(KERN_INFO "W(%d):", reg & 0x7f);
121                 for (i = 0; i < count; i++)
122                         printk(" %02x", ((const u8 *) src)[i]);
123                 printk("\n");
124         }
125 #endif
126
127         buf[0] = reg;
128         memcpy(&buf[1], src, count);
129
130         msg.addr = I2C_ADDR_MT312;
131         msg.flags = 0;
132         msg.buf = buf;
133         msg.len = count + 1;
134
135         ret = i2c->xfer(i2c, &msg, 1);
136
137         if (ret != 1) {
138                 printk(KERN_ERR "%s: ret == %d\n", __FUNCTION__, ret);
139                 return -EREMOTEIO;
140         }
141
142         return 0;
143 }
144
145 static inline int mt312_readreg(struct dvb_i2c_bus *i2c,
146                                 const enum mt312_reg_addr reg, u8 * val)
147 {
148         return mt312_read(i2c, reg, val, 1);
149 }
150
151 static inline int mt312_writereg(struct dvb_i2c_bus *i2c,
152                                  const enum mt312_reg_addr reg, const u8 val)
153 {
154         return mt312_write(i2c, reg, &val, 1);
155 }
156
157 static int mt312_pll_write(struct dvb_i2c_bus *i2c, const u8 addr,
158                            u8 * buf, const u8 len)
159 {
160         int ret;
161         struct i2c_msg msg;
162
163         msg.addr = addr;
164         msg.flags = 0;
165         msg.buf = buf;
166         msg.len = len;
167
168         if ((ret = mt312_writereg(i2c, GPP_CTRL, 0x40)) < 0)
169                 return ret;
170
171         if ((ret = i2c->xfer(i2c, &msg, 1)) != 1)
172                 printk(KERN_ERR "%s: i/o error (ret == %d)\n", __FUNCTION__, ret);
173
174         if ((ret = mt312_writereg(i2c, GPP_CTRL, 0x00)) < 0)
175                 return ret;
176
177         return 0;
178 }
179
180 static inline u32 mt312_div(u32 a, u32 b)
181 {
182         return (a + (b / 2)) / b;
183 }
184
185 static int sl1935_set_tv_freq(struct dvb_i2c_bus *i2c, u32 freq, u32 sr)
186 {
187         /* 155 uA, Baseband Path B */
188         u8 buf[4] = { 0x00, 0x00, 0x80, 0x00 };
189
190         u8 exp;
191         u32 ref;
192         u32 div;
193
194         if (sr < 10000000) {    /* 1-10 MSym/s: ratio 2 ^ 3 */
195                 exp = 3;
196                 buf[2] |= 0x40; /* 690 uA */
197         } else if (sr < 15000000) {     /* 10-15 MSym/s: ratio 2 ^ 4 */
198                 exp = 4;
199                 buf[2] |= 0x20; /* 330 uA */
200         } else {                /* 15-45 MSym/s: ratio 2 ^ 7 */
201                 exp = 7;
202                 buf[3] |= 0x08; /* Baseband Path A */
203         }
204
205         div = mt312_div(MT312_PLL_CLK, 1 << exp);
206         ref = mt312_div(freq * 1000, div);
207         mt312_info.frequency_stepsize = mt312_div(div, 1000);
208
209         buf[0] = (ref >> 8) & 0x7f;
210         buf[1] = (ref >> 0) & 0xff;
211         buf[2] |= (exp - 1);
212
213         if (freq < 1550000)
214                 buf[3] |= 0x10;
215
216         dprintk(KERN_INFO "synth dword = %02x%02x%02x%02x\n", buf[0],
217                buf[1], buf[2], buf[3]);
218
219         return mt312_pll_write(i2c, I2C_ADDR_SL1935, buf, sizeof(buf));
220 }
221
222 static int tsa5059_set_tv_freq(struct dvb_i2c_bus *i2c, u32 freq, u32 sr)
223 {
224         u8 buf[4];
225
226         u32 ref = mt312_div(freq, 125);
227
228         buf[0] = (ref >> 8) & 0x7f;
229         buf[1] = (ref >> 0) & 0xff;
230         buf[2] = 0x84 | ((ref >> 10) & 0x60);
231         buf[3] = 0x80;
232         
233         if (freq < 1550000)
234                 buf[3] |= 0x02;
235
236         dprintk(KERN_INFO "synth dword = %02x%02x%02x%02x\n", buf[0],
237                buf[1], buf[2], buf[3]);
238
239         return mt312_pll_write(i2c, I2C_ADDR_TSA5059, buf, sizeof(buf));
240 }
241
242 static int mt312_reset(struct dvb_i2c_bus *i2c, const u8 full)
243 {
244         return mt312_writereg(i2c, RESET, full ? 0x80 : 0x40);
245 }
246
247 static int mt312_init(struct dvb_i2c_bus *i2c, const long id, u8 pll)
248 {
249         int ret;
250         u8 buf[2];
251
252         /* wake up */
253         if ((ret = mt312_writereg(i2c, CONFIG, (pll == 60 ? 0x88 : 0x8c))) < 0)
254                 return ret;
255
256         /* wait at least 150 usec */
257         udelay(150);
258
259         /* full reset */
260         if ((ret = mt312_reset(i2c, 1)) < 0)
261                 return ret;
262
263 // Per datasheet, write correct values. 09/28/03 ACCJr.
264 // If we don't do this, we won't get FE_HAS_VITERBI in the VP310.
265         {
266                 u8 buf_def[8]={0x14, 0x12, 0x03, 0x02, 0x01, 0x00, 0x00, 0x00};
267
268                 if ((ret = mt312_write(i2c, VIT_SETUP, buf_def, sizeof(buf_def))) < 0)
269                         return ret;
270         }
271
272         /* SYS_CLK */
273         buf[0] = mt312_div((pll == 60 ? MT312_LPOWER_SYS_CLK : MT312_SYS_CLK) * 2, 1000000);
274
275         /* DISEQC_RATIO */
276         buf[1] = mt312_div(MT312_PLL_CLK, 15000 * 4);
277
278         if ((ret = mt312_write(i2c, SYS_CLK, buf, sizeof(buf))) < 0)
279                 return ret;
280
281         if ((ret = mt312_writereg(i2c, SNR_THS_HIGH, 0x32)) < 0)
282                 return ret;
283
284         if ((ret = mt312_writereg(i2c, OP_CTRL, 0x53)) < 0)
285                 return ret;
286
287         /* TS_SW_LIM */
288         buf[0] = 0x8c;
289         buf[1] = 0x98;
290
291         if ((ret = mt312_write(i2c, TS_SW_LIM_L, buf, sizeof(buf))) < 0)
292                 return ret;
293
294         if ((ret = mt312_writereg(i2c, CS_SW_LIM, 0x69)) < 0)
295                 return ret;
296
297         return 0;
298 }
299
300 static int mt312_send_master_cmd(struct dvb_i2c_bus *i2c,
301                                  const struct dvb_diseqc_master_cmd *c)
302 {
303         int ret;
304         u8 diseqc_mode;
305
306         if ((c->msg_len == 0) || (c->msg_len > sizeof(c->msg)))
307                 return -EINVAL;
308
309         if ((ret = mt312_readreg(i2c, DISEQC_MODE, &diseqc_mode)) < 0)
310                 return ret;
311
312         if ((ret =
313              mt312_write(i2c, (0x80 | DISEQC_INSTR), c->msg, c->msg_len)) < 0)
314                 return ret;
315
316         if ((ret =
317              mt312_writereg(i2c, DISEQC_MODE,
318                             (diseqc_mode & 0x40) | ((c->msg_len - 1) << 3)
319                             | 0x04)) < 0)
320                 return ret;
321
322         /* set DISEQC_MODE[2:0] to zero if a return message is expected */
323         if (c->msg[0] & 0x02)
324                 if ((ret =
325                      mt312_writereg(i2c, DISEQC_MODE, (diseqc_mode & 0x40))) < 0)
326                         return ret;
327
328         return 0;
329 }
330
331 static int mt312_recv_slave_reply(struct dvb_i2c_bus *i2c,
332                                   struct dvb_diseqc_slave_reply *r)
333 {
334         /* TODO */
335         return -EOPNOTSUPP;
336 }
337
338 static int mt312_send_burst(struct dvb_i2c_bus *i2c, const fe_sec_mini_cmd_t c)
339 {
340         const u8 mini_tab[2] = { 0x02, 0x03 };
341
342         int ret;
343         u8 diseqc_mode;
344
345         if (c > SEC_MINI_B)
346                 return -EINVAL;
347
348         if ((ret = mt312_readreg(i2c, DISEQC_MODE, &diseqc_mode)) < 0)
349                 return ret;
350
351         if ((ret =
352              mt312_writereg(i2c, DISEQC_MODE,
353                             (diseqc_mode & 0x40) | mini_tab[c])) < 0)
354                 return ret;
355
356         return 0;
357 }
358
359 static int mt312_set_tone(struct dvb_i2c_bus *i2c, const fe_sec_tone_mode_t t)
360 {
361         const u8 tone_tab[2] = { 0x01, 0x00 };
362
363         int ret;
364         u8 diseqc_mode;
365
366         if (t > SEC_TONE_OFF)
367                 return -EINVAL;
368
369         if ((ret = mt312_readreg(i2c, DISEQC_MODE, &diseqc_mode)) < 0)
370                 return ret;
371
372         if ((ret =
373              mt312_writereg(i2c, DISEQC_MODE,
374                             (diseqc_mode & 0x40) | tone_tab[t])) < 0)
375                 return ret;
376
377         return 0;
378 }
379
380 static int mt312_set_voltage(struct dvb_i2c_bus *i2c, const fe_sec_voltage_t v)
381 {
382         const u8 volt_tab[3] = { 0x00, 0x40, 0x00 };
383
384         if (v > SEC_VOLTAGE_OFF)
385                 return -EINVAL;
386
387         return mt312_writereg(i2c, DISEQC_MODE, volt_tab[v]);
388 }
389
390 static int mt312_read_status(struct dvb_i2c_bus *i2c, fe_status_t *s, const long id)
391 {
392         int ret;
393         u8 status[3], vit_mode;
394
395         *s = 0;
396
397         if ((ret = mt312_read(i2c, QPSK_STAT_H, status, sizeof(status))) < 0)
398                 return ret;
399
400         dprintk(KERN_DEBUG "QPSK_STAT_H: 0x%02x, QPSK_STAT_L: 0x%02x, FEC_STATUS: 0x%02x\n", status[0], status[1], status[2]);
401
402         if (status[0] & 0xc0)
403                 *s |= FE_HAS_SIGNAL;    /* signal noise ratio */
404         if (status[0] & 0x04)
405                 *s |= FE_HAS_CARRIER;   /* qpsk carrier lock */
406         if (status[2] & 0x02)
407                 *s |= FE_HAS_VITERBI;   /* viterbi lock */
408         if (status[2] & 0x04)
409                 *s |= FE_HAS_SYNC;      /* byte align lock */
410         if (status[0] & 0x01)
411                 *s |= FE_HAS_LOCK;      /* qpsk lock */
412         // VP310 doesn't have AUTO, so we "implement it here" ACCJr
413         if ((id == ID_VP310) && !(status[0] & 0x01)) {
414                 if ((ret = mt312_readreg(i2c, VIT_MODE, &vit_mode)) < 0)
415                         return ret;
416                 vit_mode ^= 0x40;
417                 if ((ret = mt312_writereg(i2c, VIT_MODE, vit_mode)) < 0)
418                         return ret;
419                 if ((ret = mt312_writereg(i2c, GO, 0x01)) < 0)
420                         return ret;
421         }
422
423         return 0;
424 }
425
426 static int mt312_read_bercnt(struct dvb_i2c_bus *i2c, u32 * ber)
427 {
428         int ret;
429         u8 buf[3];
430
431         if ((ret = mt312_read(i2c, RS_BERCNT_H, buf, 3)) < 0)
432                 return ret;
433
434         *ber = ((buf[0] << 16) | (buf[1] << 8) | buf[2]) * 64;
435
436         return 0;
437 }
438
439 static int mt312_read_agc(struct dvb_i2c_bus *i2c, u16 * signal_strength)
440 {
441         int ret;
442         u8 buf[3];
443         u16 agc;
444         s16 err_db;
445
446         if ((ret = mt312_read(i2c, AGC_H, buf, sizeof(buf))) < 0)
447                 return ret;
448
449         agc = (buf[0] << 6) | (buf[1] >> 2);
450         err_db = (s16) (((buf[1] & 0x03) << 14) | buf[2] << 6) >> 6;
451
452         *signal_strength = agc;
453
454         dprintk(KERN_DEBUG "agc=%08x err_db=%hd\n", agc, err_db);
455
456         return 0;
457 }
458
459 static int mt312_read_snr(struct dvb_i2c_bus *i2c, u16 * snr)
460 {
461         int ret;
462         u8 buf[2];
463
464         if ((ret = mt312_read(i2c, M_SNR_H, &buf, sizeof(buf))) < 0)
465                 return ret;
466
467         *snr = 0xFFFF - ((((buf[0] & 0x7f) << 8) | buf[1]) << 1);
468
469         return 0;
470 }
471
472 static int mt312_read_ubc(struct dvb_i2c_bus *i2c, u32 * ubc)
473 {
474         int ret;
475         u8 buf[2];
476
477         if ((ret = mt312_read(i2c, RS_UBC_H, &buf, sizeof(buf))) < 0)
478                 return ret;
479
480         *ubc = (buf[0] << 8) | buf[1];
481
482         return 0;
483 }
484
485 static int mt312_set_frontend(struct dvb_i2c_bus *i2c,
486                               const struct dvb_frontend_parameters *p,
487                               const long id)
488 {
489         int ret;
490         u8 buf[5], config_val;
491         u16 sr;
492
493         const u8 fec_tab[10] =
494             { 0x00, 0x01, 0x02, 0x04, 0x3f, 0x08, 0x10, 0x20, 0x3f, 0x3f };
495         const u8 inv_tab[3] = { 0x00, 0x40, 0x80 };
496
497         int (*set_tv_freq)(struct dvb_i2c_bus *i2c, u32 freq, u32 sr);
498
499         dprintk("%s: Freq %d\n", __FUNCTION__, p->frequency);
500
501         if ((p->frequency < mt312_info.frequency_min)
502             || (p->frequency > mt312_info.frequency_max))
503                 return -EINVAL;
504
505         if ((p->inversion < INVERSION_OFF)
506             || (p->inversion > INVERSION_AUTO))
507                 return -EINVAL;
508
509         if ((p->u.qpsk.symbol_rate < mt312_info.symbol_rate_min)
510             || (p->u.qpsk.symbol_rate > mt312_info.symbol_rate_max))
511                 return -EINVAL;
512
513         if ((p->u.qpsk.fec_inner < FEC_NONE)
514             || (p->u.qpsk.fec_inner > FEC_AUTO))
515                 return -EINVAL;
516
517         if ((p->u.qpsk.fec_inner == FEC_4_5)
518             || (p->u.qpsk.fec_inner == FEC_8_9))
519                 return -EINVAL;
520
521         switch (id) {
522         case ID_VP310:
523         // For now we will do this only for the VP310.
524         // It should be better for the mt312 as well, but tunning will be slower. ACCJr 09/29/03
525                 if ((ret = mt312_readreg(i2c, CONFIG, &config_val) < 0))
526                         return ret;
527                 if (p->u.qpsk.symbol_rate >= 30000000) //Note that 30MS/s should use 90MHz
528                 {
529                         if ((config_val & 0x0c) == 0x08) //We are running 60MHz
530                                 if ((ret = mt312_init(i2c, id, (u8) 90)) < 0)
531                                         return ret;
532                 }
533                 else
534                 {
535                         if ((config_val & 0x0c) == 0x0C) //We are running 90MHz
536                                 if ((ret = mt312_init(i2c, id, (u8) 60)) < 0)
537                                         return ret;
538                 }
539                 set_tv_freq = tsa5059_set_tv_freq;
540                 break;
541         case ID_MT312:
542                 set_tv_freq = sl1935_set_tv_freq;
543                 break;
544         default:
545                 return -EINVAL;
546         }
547
548         if ((ret = set_tv_freq(i2c, p->frequency, p->u.qpsk.symbol_rate)) < 0)
549                 return ret;
550
551         /* sr = (u16)(sr * 256.0 / 1000000.0) */
552         sr = mt312_div(p->u.qpsk.symbol_rate * 4, 15625);
553
554         /* SYM_RATE */
555         buf[0] = (sr >> 8) & 0x3f;
556         buf[1] = (sr >> 0) & 0xff;
557
558         /* VIT_MODE */
559         buf[2] = inv_tab[p->inversion] | fec_tab[p->u.qpsk.fec_inner];
560
561         /* QPSK_CTRL */
562         buf[3] = 0x40;          /* swap I and Q before QPSK demodulation */
563
564         if (p->u.qpsk.symbol_rate < 10000000)
565                 buf[3] |= 0x04; /* use afc mode */
566
567         /* GO */
568         buf[4] = 0x01;
569
570         if ((ret = mt312_write(i2c, SYM_RATE_H, buf, sizeof(buf))) < 0)
571                 return ret;
572
573         mt312_reset(i2c, 0);
574
575         return 0;
576 }
577
578 static int mt312_get_inversion(struct dvb_i2c_bus *i2c,
579                                fe_spectral_inversion_t * i)
580 {
581         int ret;
582         u8 vit_mode;
583
584         if ((ret = mt312_readreg(i2c, VIT_MODE, &vit_mode)) < 0)
585                 return ret;
586
587         if (vit_mode & 0x80)    /* auto inversion was used */
588                 *i = (vit_mode & 0x40) ? INVERSION_ON : INVERSION_OFF;
589
590         return 0;
591 }
592
593 static int mt312_get_symbol_rate(struct dvb_i2c_bus *i2c, u32 * sr)
594 {
595         int ret;
596         u8 sym_rate_h;
597         u8 dec_ratio;
598         u16 sym_rat_op;
599         u16 monitor;
600         u8 buf[2];
601
602         if ((ret = mt312_readreg(i2c, SYM_RATE_H, &sym_rate_h)) < 0)
603                 return ret;
604
605         if (sym_rate_h & 0x80) {        /* symbol rate search was used */
606                 if ((ret = mt312_writereg(i2c, MON_CTRL, 0x03)) < 0)
607                         return ret;
608
609                 if ((ret = mt312_read(i2c, MONITOR_H, buf, sizeof(buf))) < 0)
610                         return ret;
611
612                 monitor = (buf[0] << 8) | buf[1];
613
614                 dprintk(KERN_DEBUG "sr(auto) = %u\n",
615                        mt312_div(monitor * 15625, 4));
616         } else {
617                 if ((ret = mt312_writereg(i2c, MON_CTRL, 0x05)) < 0)
618                         return ret;
619
620                 if ((ret = mt312_read(i2c, MONITOR_H, buf, sizeof(buf))) < 0)
621                         return ret;
622
623                 dec_ratio = ((buf[0] >> 5) & 0x07) * 32;
624
625                 if ((ret = mt312_read(i2c, SYM_RAT_OP_H, buf, sizeof(buf))) < 0)
626                         return ret;
627
628                 sym_rat_op = (buf[0] << 8) | buf[1];
629
630                 dprintk(KERN_DEBUG "sym_rat_op=%d dec_ratio=%d\n",
631                        sym_rat_op, dec_ratio);
632                 dprintk(KERN_DEBUG "*sr(manual) = %lu\n",
633                        (((MT312_PLL_CLK * 8192) / (sym_rat_op + 8192)) *
634                         2) - dec_ratio);
635         }
636
637         return 0;
638 }
639
640 static int mt312_get_code_rate(struct dvb_i2c_bus *i2c, fe_code_rate_t * cr)
641 {
642         const fe_code_rate_t fec_tab[8] =
643             { FEC_1_2, FEC_2_3, FEC_3_4, FEC_5_6, FEC_6_7, FEC_7_8,
644                 FEC_AUTO, FEC_AUTO };
645
646         int ret;
647         u8 fec_status;
648
649         if ((ret = mt312_readreg(i2c, FEC_STATUS, &fec_status)) < 0)
650                 return ret;
651
652         *cr = fec_tab[(fec_status >> 4) & 0x07];
653
654         return 0;
655 }
656
657 static int mt312_get_frontend(struct dvb_i2c_bus *i2c,
658                               struct dvb_frontend_parameters *p)
659 {
660         int ret;
661
662         if ((ret = mt312_get_inversion(i2c, &p->inversion)) < 0)
663                 return ret;
664
665         if ((ret = mt312_get_symbol_rate(i2c, &p->u.qpsk.symbol_rate)) < 0)
666                 return ret;
667
668         if ((ret = mt312_get_code_rate(i2c, &p->u.qpsk.fec_inner)) < 0)
669                 return ret;
670
671         return 0;
672 }
673
674 static int mt312_sleep(struct dvb_i2c_bus *i2c)
675 {
676         int ret;
677         u8 config;
678
679         /* reset all registers to defaults */
680         if ((ret = mt312_reset(i2c, 1)) < 0)
681                 return ret;
682
683         if ((ret = mt312_readreg(i2c, CONFIG, &config)) < 0)
684                 return ret;
685
686         /* enter standby */
687         if ((ret = mt312_writereg(i2c, CONFIG, config & 0x7f)) < 0)
688                 return ret;
689
690         return 0;
691 }
692
693 static int mt312_ioctl(struct dvb_frontend *fe, unsigned int cmd, void *arg)
694 {
695         struct dvb_i2c_bus *i2c = fe->i2c;
696
697         switch (cmd) {
698         case FE_GET_INFO:
699                 memcpy(arg, &mt312_info, sizeof(struct dvb_frontend_info));
700                 break;
701
702         case FE_DISEQC_RESET_OVERLOAD:
703                 return -EOPNOTSUPP;
704
705         case FE_DISEQC_SEND_MASTER_CMD:
706                 return mt312_send_master_cmd(i2c, arg);
707
708         case FE_DISEQC_RECV_SLAVE_REPLY:
709                 if ((long) fe->data == ID_MT312)
710                         return mt312_recv_slave_reply(i2c, arg);
711                 else
712                         return -EOPNOTSUPP;
713
714         case FE_DISEQC_SEND_BURST:
715                 return mt312_send_burst(i2c, (fe_sec_mini_cmd_t) arg);
716
717         case FE_SET_TONE:
718                 return mt312_set_tone(i2c, (fe_sec_tone_mode_t) arg);
719
720         case FE_SET_VOLTAGE:
721                 return mt312_set_voltage(i2c, (fe_sec_voltage_t) arg);
722
723         case FE_ENABLE_HIGH_LNB_VOLTAGE:
724                 return -EOPNOTSUPP;
725
726         case FE_READ_STATUS:
727                 return mt312_read_status(i2c, arg, (long) fe->data);
728
729         case FE_READ_BER:
730                 return mt312_read_bercnt(i2c, arg);
731
732         case FE_READ_SIGNAL_STRENGTH:
733                 return mt312_read_agc(i2c, arg);
734
735         case FE_READ_SNR:
736                 return mt312_read_snr(i2c, arg);
737
738         case FE_READ_UNCORRECTED_BLOCKS:
739                 return mt312_read_ubc(i2c, arg);
740
741         case FE_SET_FRONTEND:
742                 return mt312_set_frontend(i2c, arg, (long) fe->data);
743
744         case FE_GET_FRONTEND:
745                 return mt312_get_frontend(i2c, arg);
746
747         case FE_GET_EVENT:
748                 return -EOPNOTSUPP;
749
750         case FE_SLEEP:
751                 return mt312_sleep(i2c);
752
753         case FE_INIT:
754         //For the VP310 we should run at 60MHz when ever possible.
755         //It should be better to run the mt312 ar lower speed when ever possible, but tunning will be slower. ACCJr 09/29/03
756                 if ((long)fe->data == ID_MT312)
757                         return mt312_init(i2c, (long) fe->data, (u8) 90);
758                 else
759                         return mt312_init(i2c, (long) fe->data, (u8) 60);
760
761         case FE_GET_TUNE_SETTINGS:
762         {
763                 struct dvb_frontend_tune_settings* fesettings = (struct dvb_frontend_tune_settings*) arg;
764                 fesettings->min_delay_ms = 50;
765                 fesettings->step_size = 0;
766                 fesettings->max_drift = 0;
767                 return 0;
768         }           
769
770         default:
771                 return -ENOIOCTLCMD;
772         }
773
774         return 0;
775 }
776
777 static int mt312_attach(struct dvb_i2c_bus *i2c, void **data)
778 {
779         int ret;
780         u8 id;
781
782         if ((ret = mt312_readreg(i2c, ID, &id)) < 0)
783                 return ret;
784
785         if ((id != ID_VP310) && (id != ID_MT312))
786                 return -ENODEV;
787
788         if ((ret = dvb_register_frontend(mt312_ioctl, i2c,
789                                 (void *)(long)id, &mt312_info)) < 0)
790                 return ret;
791
792         mt312_count++;
793
794         return 0;
795 }
796
797 static void mt312_detach(struct dvb_i2c_bus *i2c, void *data)
798 {
799         dvb_unregister_frontend(mt312_ioctl, i2c);
800
801         if (mt312_count)
802                 mt312_count--;
803 }
804
805 static int __init mt312_module_init(void)
806 {
807         return dvb_register_i2c_device(THIS_MODULE, mt312_attach, mt312_detach);
808 }
809
810 static void __exit mt312_module_exit(void)
811 {
812         dvb_unregister_i2c_device(mt312_attach);
813 }
814
815 module_init(mt312_module_init);
816 module_exit(mt312_module_exit);
817
818 #if MT312_DEBUG != 0
819 MODULE_PARM(debug,"i");
820 MODULE_PARM_DESC(debug, "enable verbose debug messages");
821 #endif
822
823 MODULE_DESCRIPTION("MT312 Satellite Channel Decoder Driver");
824 MODULE_AUTHOR("Andreas Oberritter <obi@linuxtv.org>");
825 MODULE_LICENSE("GPL");