b25017827026383eefb45488ae5c8923dce69e3d
[linux-flexiantxendom0-3.2.10.git] / drivers / media / dvb / frontends / mt312.c
1 /* 
2     Driver for Zarlink MT312 Satellite Channel Decoder
3
4     Copyright (C) 2003 Andreas Oberritter <obi@saftware.de>
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_PLL_CLK           10000000UL      /* 10 MHz */
43
44 static struct dvb_frontend_info mt312_info = {
45         .name = "Zarlink MT312",
46         .type = FE_QPSK,
47         .frequency_min = 950000,
48         .frequency_max = 2150000,
49         .frequency_stepsize = (MT312_PLL_CLK / 1000) / 128,
50         /*.frequency_tolerance = 29500,         FIXME: binary compatibility waste? */
51         .symbol_rate_min = MT312_SYS_CLK / 128,
52         .symbol_rate_max = MT312_SYS_CLK / 2,
53         /*.symbol_rate_tolerance = 500,         FIXME: binary compatibility waste? 2% */
54         .notifier_delay = 0,
55         .caps =
56             FE_CAN_INVERSION_AUTO | FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
57             FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
58             FE_CAN_FEC_AUTO | FE_CAN_QPSK | FE_CAN_RECOVER |
59             FE_CAN_CLEAN_SETUP | FE_CAN_MUTE_TS
60 };
61
62 static int mt312_read(struct dvb_i2c_bus *i2c,
63                       const enum mt312_reg_addr reg, void *buf,
64                       const size_t count)
65 {
66         int ret;
67         struct i2c_msg msg[2];
68         u8 regbuf[1] = { reg };
69
70         msg[0].addr = I2C_ADDR_MT312;
71         msg[0].flags = 0;
72         msg[0].buf = regbuf;
73         msg[0].len = 1;
74         msg[1].addr = I2C_ADDR_MT312;
75         msg[1].flags = I2C_M_RD;
76         msg[1].buf = buf;
77         msg[1].len = count;
78
79         ret = i2c->xfer(i2c, msg, 2);
80
81         if (ret != 2) {
82                 printk(KERN_ERR "%s: ret == %d\n", __FUNCTION__, ret);
83                 return -EREMOTEIO;
84         }
85 #ifdef MT312_DEBUG
86         {
87                 int i;
88                 printk(KERN_INFO "R(%d):", reg & 0x7f);
89                 for (i = 0; i < count; i++)
90                         printk(" %02x", ((const u8 *) buf)[i]);
91                 printk("\n");
92         }
93 #endif
94
95         return 0;
96 }
97
98 static int mt312_write(struct dvb_i2c_bus *i2c,
99                        const enum mt312_reg_addr reg, const void *src,
100                        const size_t count)
101 {
102         int ret;
103         u8 buf[count + 1];
104         struct i2c_msg msg;
105
106 #ifdef MT312_DEBUG
107         {
108                 int i;
109                 printk(KERN_INFO "W(%d):", reg & 0x7f);
110                 for (i = 0; i < count; i++)
111                         printk(" %02x", ((const u8 *) src)[i]);
112                 printk("\n");
113         }
114 #endif
115
116         buf[0] = reg;
117         memcpy(&buf[1], src, count);
118
119         msg.addr = I2C_ADDR_MT312;
120         msg.flags = 0;
121         msg.buf = buf;
122         msg.len = count + 1;
123
124         ret = i2c->xfer(i2c, &msg, 1);
125
126         if (ret != 1) {
127                 printk(KERN_ERR "%s: ret == %d\n", __FUNCTION__, ret);
128                 return -EREMOTEIO;
129         }
130
131         return 0;
132 }
133
134 static inline int mt312_readreg(struct dvb_i2c_bus *i2c,
135                                 const enum mt312_reg_addr reg, u8 * val)
136 {
137         return mt312_read(i2c, reg, val, 1);
138 }
139
140 static inline int mt312_writereg(struct dvb_i2c_bus *i2c,
141                                  const enum mt312_reg_addr reg, const u8 val)
142 {
143         return mt312_write(i2c, reg, &val, 1);
144 }
145
146 static int mt312_pll_write(struct dvb_i2c_bus *i2c, const u8 addr,
147                            u8 * buf, const u8 len)
148 {
149         int ret;
150         struct i2c_msg msg;
151
152         msg.addr = addr;
153         msg.flags = 0;
154         msg.buf = buf;
155         msg.len = len;
156
157         if ((ret = mt312_writereg(i2c, GPP_CTRL, 0x40)) < 0)
158                 return ret;
159
160         if ((ret = i2c->xfer(i2c, &msg, 1)) != 1)
161                 printk(KERN_ERR "%s: i/o error (ret == %d)\n", __FUNCTION__, ret);
162
163         if ((ret = mt312_writereg(i2c, GPP_CTRL, 0x00)) < 0)
164                 return ret;
165
166         return 0;
167 }
168
169 static inline u32 mt312_div(u32 a, u32 b)
170 {
171         return (a + (b / 2)) / b;
172 }
173
174 static int sl1935_set_tv_freq(struct dvb_i2c_bus *i2c, u32 freq, u32 sr)
175 {
176         /* 155 uA, Baseband Path B */
177         u8 buf[4] = { 0x00, 0x00, 0x80, 0x00 };
178
179         u8 exp;
180         u32 ref;
181         u32 div;
182
183         if (sr < 10000000) {    /* 1-10 MSym/s: ratio 2 ^ 3 */
184                 exp = 3;
185                 buf[2] |= 0x40; /* 690 uA */
186         } else if (sr < 15000000) {     /* 10-15 MSym/s: ratio 2 ^ 4 */
187                 exp = 4;
188                 buf[2] |= 0x20; /* 330 uA */
189         } else {                /* 15-45 MSym/s: ratio 2 ^ 7 */
190                 exp = 7;
191                 buf[3] |= 0x08; /* Baseband Path A */
192         }
193
194         div = mt312_div(MT312_PLL_CLK, 1 << exp);
195         ref = mt312_div(freq * 1000, div);
196         mt312_info.frequency_stepsize = mt312_div(div, 1000);
197
198         buf[0] = (ref >> 8) & 0x7f;
199         buf[1] = (ref >> 0) & 0xff;
200         buf[2] |= (exp - 1);
201
202         if (freq < 1550000)
203                 buf[3] |= 0x10;
204
205         printk(KERN_INFO "synth dword = %02x%02x%02x%02x\n", buf[0],
206                buf[1], buf[2], buf[3]);
207
208         return mt312_pll_write(i2c, I2C_ADDR_SL1935, buf, sizeof(buf));
209 }
210
211 static int tsa5059_set_tv_freq(struct dvb_i2c_bus *i2c, u32 freq, u32 sr)
212 {
213         u8 buf[4];
214
215         u32 ref = mt312_div(freq, 125);
216
217         buf[0] = (ref >> 8) & 0x7f;
218         buf[1] = (ref >> 0) & 0xff;
219         buf[2] = 0x84 | ((ref >> 10) & 0x60);
220         buf[3] = 0x80;
221         
222         if (freq < 1550000)
223                 buf[3] |= 0x02;
224
225         printk(KERN_INFO "synth dword = %02x%02x%02x%02x\n", buf[0],
226                buf[1], buf[2], buf[3]);
227
228         return mt312_pll_write(i2c, I2C_ADDR_TSA5059, buf, sizeof(buf));
229 }
230
231 static int mt312_reset(struct dvb_i2c_bus *i2c, const u8 full)
232 {
233         return mt312_writereg(i2c, RESET, full ? 0x80 : 0x40);
234 }
235
236 static int mt312_init(struct dvb_i2c_bus *i2c, const long id)
237 {
238         int ret;
239         u8 buf[2];
240
241         /* wake up */
242         if ((ret = mt312_writereg(i2c, CONFIG, 0x8c)) < 0)
243                 return ret;
244
245         /* wait at least 150 usec */
246         udelay(150);
247
248         /* full reset */
249         if ((ret = mt312_reset(i2c, 1)) < 0)
250                 return ret;
251
252         /* SYS_CLK */
253         buf[0] = mt312_div(MT312_SYS_CLK * 2, 1000000);
254
255         /* DISEQC_RATIO */
256         buf[1] = mt312_div(MT312_PLL_CLK, 15000 * 4);
257
258         if ((ret = mt312_write(i2c, SYS_CLK, buf, sizeof(buf))) < 0)
259                 return ret;
260
261         if ((ret = mt312_writereg(i2c, SNR_THS_HIGH, 0x32)) < 0)
262                 return ret;
263
264         if ((ret = mt312_writereg(i2c, OP_CTRL, 0x53)) < 0)
265                 return ret;
266
267         /* TS_SW_LIM */
268         buf[0] = 0x8c;
269         buf[1] = 0x98;
270
271         if ((ret = mt312_write(i2c, TS_SW_LIM_L, buf, sizeof(buf))) < 0)
272                 return ret;
273
274         if ((ret = mt312_writereg(i2c, CS_SW_LIM, 0x69)) < 0)
275                 return ret;
276
277         return 0;
278 }
279
280 static int mt312_send_master_cmd(struct dvb_i2c_bus *i2c,
281                                  const struct dvb_diseqc_master_cmd *c)
282 {
283         int ret;
284         u8 diseqc_mode;
285
286         if ((c->msg_len == 0) || (c->msg_len > sizeof(c->msg)))
287                 return -EINVAL;
288
289         if ((ret = mt312_readreg(i2c, DISEQC_MODE, &diseqc_mode)) < 0)
290                 return ret;
291
292         if ((ret =
293              mt312_write(i2c, (0x80 | DISEQC_INSTR), c->msg, c->msg_len)) < 0)
294                 return ret;
295
296         if ((ret =
297              mt312_writereg(i2c, DISEQC_MODE,
298                             (diseqc_mode & 0x40) | ((c->msg_len - 1) << 3)
299                             | 0x04)) < 0)
300                 return ret;
301
302         /* set DISEQC_MODE[2:0] to zero if a return message is expected */
303         if (c->msg[0] & 0x02)
304                 if ((ret =
305                      mt312_writereg(i2c, DISEQC_MODE, (diseqc_mode & 0x40))) < 0)
306                         return ret;
307
308         return 0;
309 }
310
311 static int mt312_recv_slave_reply(struct dvb_i2c_bus *i2c,
312                                   struct dvb_diseqc_slave_reply *r)
313 {
314         /* TODO */
315         return -EOPNOTSUPP;
316 }
317
318 static int mt312_send_burst(struct dvb_i2c_bus *i2c, const fe_sec_mini_cmd_t c)
319 {
320         const u8 mini_tab[2] = { 0x02, 0x03 };
321
322         int ret;
323         u8 diseqc_mode;
324
325         if (c > SEC_MINI_B)
326                 return -EINVAL;
327
328         if ((ret = mt312_readreg(i2c, DISEQC_MODE, &diseqc_mode)) < 0)
329                 return ret;
330
331         if ((ret =
332              mt312_writereg(i2c, DISEQC_MODE,
333                             (diseqc_mode & 0x40) | mini_tab[c])) < 0)
334                 return ret;
335
336         return 0;
337 }
338
339 static int mt312_set_tone(struct dvb_i2c_bus *i2c, const fe_sec_tone_mode_t t)
340 {
341         const u8 tone_tab[2] = { 0x01, 0x00 };
342
343         int ret;
344         u8 diseqc_mode;
345
346         if (t > SEC_TONE_OFF)
347                 return -EINVAL;
348
349         if ((ret = mt312_readreg(i2c, DISEQC_MODE, &diseqc_mode)) < 0)
350                 return ret;
351
352         if ((ret =
353              mt312_writereg(i2c, DISEQC_MODE,
354                             (diseqc_mode & 0x40) | tone_tab[t])) < 0)
355                 return ret;
356
357         return 0;
358 }
359
360 static int mt312_set_voltage(struct dvb_i2c_bus *i2c, const fe_sec_voltage_t v)
361 {
362         const u8 volt_tab[3] = { 0x00, 0x40, 0x00 };
363
364         if (v > SEC_VOLTAGE_OFF)
365                 return -EINVAL;
366
367         return mt312_writereg(i2c, DISEQC_MODE, volt_tab[v]);
368 }
369
370 static int mt312_read_status(struct dvb_i2c_bus *i2c, fe_status_t * s)
371 {
372         int ret;
373         u8 status[3];
374
375         *s = 0;
376
377         if ((ret = mt312_read(i2c, QPSK_STAT_H, status, sizeof(status))) < 0)
378                 return ret;
379
380         if (status[0] & 0xc0)
381                 *s |= FE_HAS_SIGNAL;    /* signal noise ratio */
382         if (status[0] & 0x04)
383                 *s |= FE_HAS_CARRIER;   /* qpsk carrier lock */
384         if (status[2] & 0x02)
385                 *s |= FE_HAS_VITERBI;   /* viterbi lock */
386         if (status[2] & 0x04)
387                 *s |= FE_HAS_SYNC;      /* byte align lock */
388         if (status[0] & 0x01)
389                 *s |= FE_HAS_LOCK;      /* qpsk lock */
390
391         return 0;
392 }
393
394 static int mt312_read_bercnt(struct dvb_i2c_bus *i2c, u32 * ber)
395 {
396         int ret;
397         u8 buf[3];
398
399         if ((ret = mt312_read(i2c, RS_BERCNT_H, buf, 3)) < 0)
400                 return ret;
401
402         *ber = ((buf[0] << 16) | (buf[1] << 8) | buf[2]) * 64;
403
404         return 0;
405 }
406
407 static int mt312_read_agc(struct dvb_i2c_bus *i2c, u16 * signal_strength)
408 {
409         int ret;
410         u8 buf[3];
411         u16 agc;
412         s16 err_db;
413
414         if ((ret = mt312_read(i2c, AGC_H, buf, sizeof(buf))) < 0)
415                 return ret;
416
417         agc = (buf[0] << 6) | (buf[1] >> 2);
418         err_db = (s16) (((buf[1] & 0x03) << 14) | buf[2] << 6) >> 6;
419
420         *signal_strength = agc;
421
422         printk(KERN_DEBUG "agc=%08x err_db=%hd\n", agc, err_db);
423
424         return 0;
425 }
426
427 static int mt312_read_snr(struct dvb_i2c_bus *i2c, u16 * snr)
428 {
429         int ret;
430         u8 buf[2];
431
432         if ((ret = mt312_read(i2c, M_SNR_H, &buf, sizeof(buf))) < 0)
433                 return ret;
434
435         *snr = 0xFFFF - ((((buf[0] & 0x7f) << 8) | buf[1]) << 1);
436
437         return 0;
438 }
439
440 static int mt312_read_ubc(struct dvb_i2c_bus *i2c, u32 * ubc)
441 {
442         int ret;
443         u8 buf[2];
444
445         if ((ret = mt312_read(i2c, RS_UBC_H, &buf, sizeof(buf))) < 0)
446                 return ret;
447
448         *ubc = (buf[0] << 8) | buf[1];
449
450         return 0;
451 }
452
453 static int mt312_set_frontend(struct dvb_i2c_bus *i2c,
454                               const struct dvb_frontend_parameters *p,
455                               const long id)
456 {
457         int ret;
458         u8 buf[5];
459         u16 sr;
460
461         const u8 fec_tab[10] =
462             { 0x00, 0x01, 0x02, 0x04, 0x3f, 0x08, 0x10, 0x20, 0x3f, 0x3f };
463         const u8 inv_tab[3] = { 0x00, 0x40, 0x80 };
464
465         int (*set_tv_freq)(struct dvb_i2c_bus *i2c, u32 freq, u32 sr);
466
467         if ((p->frequency < mt312_info.frequency_min)
468             || (p->frequency > mt312_info.frequency_max))
469                 return -EINVAL;
470
471         if ((p->inversion < INVERSION_OFF)
472             || (p->inversion > INVERSION_AUTO))
473                 return -EINVAL;
474
475         if ((p->u.qpsk.symbol_rate < mt312_info.symbol_rate_min)
476             || (p->u.qpsk.symbol_rate > mt312_info.symbol_rate_max))
477                 return -EINVAL;
478
479         if ((p->u.qpsk.fec_inner < FEC_NONE)
480             || (p->u.qpsk.fec_inner > FEC_AUTO))
481                 return -EINVAL;
482
483         if ((p->u.qpsk.fec_inner == FEC_4_5)
484             || (p->u.qpsk.fec_inner == FEC_8_9))
485                 return -EINVAL;
486
487         switch (id) {
488         case ID_VP310:
489                 set_tv_freq = tsa5059_set_tv_freq;
490                 break;
491         case ID_MT312:
492                 set_tv_freq = sl1935_set_tv_freq;
493                 break;
494         default:
495                 return -EINVAL;
496         }
497
498         if ((ret = set_tv_freq(i2c, p->frequency, p->u.qpsk.symbol_rate)) < 0)
499                 return ret;
500
501         /* sr = (u16)(sr * 256.0 / 1000000.0) */
502         sr = mt312_div(p->u.qpsk.symbol_rate * 4, 15625);
503
504         /* SYM_RATE */
505         buf[0] = (sr >> 8) & 0x3f;
506         buf[1] = (sr >> 0) & 0xff;
507
508         /* VIT_MODE */
509         buf[2] = inv_tab[p->inversion] | fec_tab[p->u.qpsk.fec_inner];
510
511         /* QPSK_CTRL */
512         buf[3] = 0x40;          /* swap I and Q before QPSK demodulation */
513
514         if (p->u.qpsk.symbol_rate < 10000000)
515                 buf[3] |= 0x04; /* use afc mode */
516
517         /* GO */
518         buf[4] = 0x01;
519
520         if ((ret = mt312_write(i2c, SYM_RATE_H, buf, sizeof(buf))) < 0)
521                 return ret;
522
523         return 0;
524 }
525
526 static int mt312_get_inversion(struct dvb_i2c_bus *i2c,
527                                fe_spectral_inversion_t * i)
528 {
529         int ret;
530         u8 vit_mode;
531
532         if ((ret = mt312_readreg(i2c, VIT_MODE, &vit_mode)) < 0)
533                 return ret;
534
535         if (vit_mode & 0x80)    /* auto inversion was used */
536                 *i = (vit_mode & 0x40) ? INVERSION_ON : INVERSION_OFF;
537
538         return 0;
539 }
540
541 static int mt312_get_symbol_rate(struct dvb_i2c_bus *i2c, u32 * sr)
542 {
543         int ret;
544         u8 sym_rate_h;
545         u8 dec_ratio;
546         u16 sym_rat_op;
547         u16 monitor;
548         u8 buf[2];
549
550         if ((ret = mt312_readreg(i2c, SYM_RATE_H, &sym_rate_h)) < 0)
551                 return ret;
552
553         if (sym_rate_h & 0x80) {        /* symbol rate search was used */
554                 if ((ret = mt312_writereg(i2c, MON_CTRL, 0x03)) < 0)
555                         return ret;
556
557                 if ((ret = mt312_read(i2c, MONITOR_H, buf, sizeof(buf))) < 0)
558                         return ret;
559
560                 monitor = (buf[0] << 8) | buf[1];
561
562                 printk(KERN_DEBUG "sr(auto) = %u\n",
563                        mt312_div(monitor * 15625, 4));
564         } else {
565                 if ((ret = mt312_writereg(i2c, MON_CTRL, 0x05)) < 0)
566                         return ret;
567
568                 if ((ret = mt312_read(i2c, MONITOR_H, buf, sizeof(buf))) < 0)
569                         return ret;
570
571                 dec_ratio = ((buf[0] >> 5) & 0x07) * 32;
572
573                 if ((ret = mt312_read(i2c, SYM_RAT_OP_H, buf, sizeof(buf))) < 0)
574                         return ret;
575
576                 sym_rat_op = (buf[0] << 8) | buf[1];
577
578                 printk(KERN_DEBUG "sym_rat_op=%d dec_ratio=%d\n",
579                        sym_rat_op, dec_ratio);
580                 printk(KERN_DEBUG "*sr(manual) = %lu\n",
581                        (((MT312_PLL_CLK * 8192) / (sym_rat_op + 8192)) *
582                         2) - dec_ratio);
583         }
584
585         return 0;
586 }
587
588 static int mt312_get_code_rate(struct dvb_i2c_bus *i2c, fe_code_rate_t * cr)
589 {
590         const fe_code_rate_t fec_tab[8] =
591             { FEC_1_2, FEC_2_3, FEC_3_4, FEC_5_6, FEC_6_7, FEC_7_8,
592                 FEC_AUTO, FEC_AUTO };
593
594         int ret;
595         u8 fec_status;
596
597         if ((ret = mt312_readreg(i2c, FEC_STATUS, &fec_status)) < 0)
598                 return ret;
599
600         *cr = fec_tab[(fec_status >> 4) & 0x07];
601
602         return 0;
603 }
604
605 static int mt312_get_frontend(struct dvb_i2c_bus *i2c,
606                               struct dvb_frontend_parameters *p)
607 {
608         int ret;
609
610         if ((ret = mt312_get_inversion(i2c, &p->inversion)) < 0)
611                 return ret;
612
613         if ((ret = mt312_get_symbol_rate(i2c, &p->u.qpsk.symbol_rate)) < 0)
614                 return ret;
615
616         if ((ret = mt312_get_code_rate(i2c, &p->u.qpsk.fec_inner)) < 0)
617                 return ret;
618
619         return 0;
620 }
621
622 static int mt312_sleep(struct dvb_i2c_bus *i2c)
623 {
624         int ret;
625         u8 config;
626
627         /* reset all registers to defaults */
628         if ((ret = mt312_reset(i2c, 1)) < 0)
629                 return ret;
630
631         if ((ret = mt312_readreg(i2c, CONFIG, &config)) < 0)
632                 return ret;
633
634         /* enter standby */
635         if ((ret = mt312_writereg(i2c, CONFIG, config & 0x7f)) < 0)
636                 return ret;
637
638         return 0;
639 }
640
641 static int mt312_ioctl(struct dvb_frontend *fe, unsigned int cmd, void *arg)
642 {
643         struct dvb_i2c_bus *i2c = fe->i2c;
644
645         switch (cmd) {
646         case FE_GET_INFO:
647                 memcpy(arg, &mt312_info, sizeof(struct dvb_frontend_info));
648                 break;
649
650         case FE_DISEQC_RESET_OVERLOAD:
651                 return -EOPNOTSUPP;
652
653         case FE_DISEQC_SEND_MASTER_CMD:
654                 return mt312_send_master_cmd(i2c, arg);
655
656         case FE_DISEQC_RECV_SLAVE_REPLY:
657                 if ((long) fe->data == ID_MT312)
658                         return mt312_recv_slave_reply(i2c, arg);
659                 else
660                         return -EOPNOTSUPP;
661
662         case FE_DISEQC_SEND_BURST:
663                 return mt312_send_burst(i2c, (fe_sec_mini_cmd_t) arg);
664
665         case FE_SET_TONE:
666                 return mt312_set_tone(i2c, (fe_sec_tone_mode_t) arg);
667
668         case FE_SET_VOLTAGE:
669                 return mt312_set_voltage(i2c, (fe_sec_voltage_t) arg);
670
671         case FE_ENABLE_HIGH_LNB_VOLTAGE:
672                 return -EOPNOTSUPP;
673
674         case FE_READ_STATUS:
675                 return mt312_read_status(i2c, arg);
676
677         case FE_READ_BER:
678                 return mt312_read_bercnt(i2c, arg);
679
680         case FE_READ_SIGNAL_STRENGTH:
681                 return mt312_read_agc(i2c, arg);
682
683         case FE_READ_SNR:
684                 return mt312_read_snr(i2c, arg);
685
686         case FE_READ_UNCORRECTED_BLOCKS:
687                 return mt312_read_ubc(i2c, arg);
688
689         case FE_SET_FRONTEND:
690                 return mt312_set_frontend(i2c, arg, (long) fe->data);
691
692         case FE_GET_FRONTEND:
693                 return mt312_get_frontend(i2c, arg);
694
695         case FE_GET_EVENT:
696                 return -EOPNOTSUPP;
697
698         case FE_SLEEP:
699                 return mt312_sleep(i2c);
700
701         case FE_INIT:
702                 return mt312_init(i2c, (long) fe->data);
703
704         case FE_RESET:
705                 return mt312_reset(i2c, 0);
706
707         default:
708                 return -ENOIOCTLCMD;
709         }
710
711         return 0;
712 }
713
714 static int mt312_attach(struct dvb_i2c_bus *i2c)
715 {
716         int ret;
717         u8 id;
718
719         if ((ret = mt312_readreg(i2c, ID, &id)) < 0)
720                 return ret;
721
722         if ((id != ID_VP310) && (id != ID_MT312))
723                 return -ENODEV;
724
725         return dvb_register_frontend(mt312_ioctl, i2c, (void *) (long) id,
726                                      &mt312_info);
727 }
728
729 static void mt312_detach(struct dvb_i2c_bus *i2c)
730 {
731         dvb_unregister_frontend(mt312_ioctl, i2c);
732 }
733
734 static int __init mt312_module_init(void)
735 {
736         return dvb_register_i2c_device(THIS_MODULE, mt312_attach, mt312_detach);
737 }
738
739 static void __exit mt312_module_exit(void)
740 {
741         dvb_unregister_i2c_device(mt312_attach);
742 }
743
744 module_init(mt312_module_init);
745 module_exit(mt312_module_exit);
746
747 MODULE_DESCRIPTION("MT312 Satellite Channel Decoder Driver");
748 MODULE_AUTHOR("Andreas Oberritter <obi@saftware.de>");
749 MODULE_LICENSE("GPL");