blob: 2d68fafc0ef8f341da520590db3f74bf723f53f0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 Driver for Zarlink VP310/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>
Tim Schmielau4e57b682005-10-30 15:03:48 -080031#include <linux/string.h>
32#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include "dvb_frontend.h"
35#include "mt312_priv.h"
36#include "mt312.h"
37
38
39struct mt312_state {
Matthias Schwarzott89f64752007-12-21 08:56:44 -030040 struct i2c_adapter *i2c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 /* configuration settings */
Matthias Schwarzott89f64752007-12-21 08:56:44 -030042 const struct mt312_config *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 struct dvb_frontend frontend;
44
45 u8 id;
46 u8 frequency;
47};
48
49static int debug;
50#define dprintk(args...) \
51 do { \
Matthias Schwarzott89f64752007-12-21 08:56:44 -030052 if (debug) \
53 printk(KERN_DEBUG "mt312: " args); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 } while (0)
55
56#define MT312_SYS_CLK 90000000UL /* 90 MHz */
57#define MT312_LPOWER_SYS_CLK 60000000UL /* 60 MHz */
58#define MT312_PLL_CLK 10000000UL /* 10 MHz */
59
Matthias Schwarzott89f64752007-12-21 08:56:44 -030060static int mt312_read(struct mt312_state *state, const enum mt312_reg_addr reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 void *buf, const size_t count)
62{
63 int ret;
64 struct i2c_msg msg[2];
65 u8 regbuf[1] = { reg };
66
67 msg[0].addr = state->config->demod_address;
68 msg[0].flags = 0;
69 msg[0].buf = regbuf;
70 msg[0].len = 1;
71 msg[1].addr = state->config->demod_address;
72 msg[1].flags = I2C_M_RD;
73 msg[1].buf = buf;
74 msg[1].len = count;
75
76 ret = i2c_transfer(state->i2c, msg, 2);
77
78 if (ret != 2) {
79 printk(KERN_ERR "%s: ret == %d\n", __FUNCTION__, ret);
80 return -EREMOTEIO;
81 }
82
Matthias Schwarzott89f64752007-12-21 08:56:44 -030083 if (debug) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 int i;
85 dprintk("R(%d):", reg & 0x7f);
86 for (i = 0; i < count; i++)
87 printk(" %02x", ((const u8 *) buf)[i]);
88 printk("\n");
89 }
90
91 return 0;
92}
93
Matthias Schwarzott89f64752007-12-21 08:56:44 -030094static int mt312_write(struct mt312_state *state, const enum mt312_reg_addr reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 const void *src, const size_t count)
96{
97 int ret;
98 u8 buf[count + 1];
99 struct i2c_msg msg;
100
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300101 if (debug) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 int i;
103 dprintk("W(%d):", reg & 0x7f);
104 for (i = 0; i < count; i++)
105 printk(" %02x", ((const u8 *) src)[i]);
106 printk("\n");
107 }
108
109 buf[0] = reg;
110 memcpy(&buf[1], src, count);
111
112 msg.addr = state->config->demod_address;
113 msg.flags = 0;
114 msg.buf = buf;
115 msg.len = count + 1;
116
117 ret = i2c_transfer(state->i2c, &msg, 1);
118
119 if (ret != 1) {
120 dprintk("%s: ret == %d\n", __FUNCTION__, ret);
121 return -EREMOTEIO;
122 }
123
124 return 0;
125}
126
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300127static inline int mt312_readreg(struct mt312_state *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 const enum mt312_reg_addr reg, u8 *val)
129{
130 return mt312_read(state, reg, val, 1);
131}
132
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300133static inline int mt312_writereg(struct mt312_state *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 const enum mt312_reg_addr reg, const u8 val)
135{
136 return mt312_write(state, reg, &val, 1);
137}
138
139static inline u32 mt312_div(u32 a, u32 b)
140{
141 return (a + (b / 2)) / b;
142}
143
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300144static int mt312_reset(struct mt312_state *state, const u8 full)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 return mt312_writereg(state, RESET, full ? 0x80 : 0x40);
147}
148
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300149static int mt312_get_inversion(struct mt312_state *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 fe_spectral_inversion_t *i)
151{
152 int ret;
153 u8 vit_mode;
154
155 if ((ret = mt312_readreg(state, VIT_MODE, &vit_mode)) < 0)
156 return ret;
157
158 if (vit_mode & 0x80) /* auto inversion was used */
159 *i = (vit_mode & 0x40) ? INVERSION_ON : INVERSION_OFF;
160
161 return 0;
162}
163
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300164static int mt312_get_symbol_rate(struct mt312_state *state, u32 *sr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 int ret;
167 u8 sym_rate_h;
168 u8 dec_ratio;
169 u16 sym_rat_op;
170 u16 monitor;
171 u8 buf[2];
172
173 if ((ret = mt312_readreg(state, SYM_RATE_H, &sym_rate_h)) < 0)
174 return ret;
175
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300176 if (sym_rate_h & 0x80) {
177 /* symbol rate search was used */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if ((ret = mt312_writereg(state, MON_CTRL, 0x03)) < 0)
179 return ret;
180
181 if ((ret = mt312_read(state, MONITOR_H, buf, sizeof(buf))) < 0)
182 return ret;
183
184 monitor = (buf[0] << 8) | buf[1];
185
Matthias Schwarzott0b6a3342007-12-21 08:58:09 -0300186 dprintk("sr(auto) = %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 mt312_div(monitor * 15625, 4));
188 } else {
189 if ((ret = mt312_writereg(state, MON_CTRL, 0x05)) < 0)
190 return ret;
191
192 if ((ret = mt312_read(state, MONITOR_H, buf, sizeof(buf))) < 0)
193 return ret;
194
195 dec_ratio = ((buf[0] >> 5) & 0x07) * 32;
196
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300197 if ((ret = mt312_read(state, SYM_RAT_OP_H, buf,
198 sizeof(buf))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return ret;
200
201 sym_rat_op = (buf[0] << 8) | buf[1];
202
Matthias Schwarzott0b6a3342007-12-21 08:58:09 -0300203 dprintk("sym_rat_op=%d dec_ratio=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 sym_rat_op, dec_ratio);
Matthias Schwarzott0b6a3342007-12-21 08:58:09 -0300205 dprintk("*sr(manual) = %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 (((MT312_PLL_CLK * 8192) / (sym_rat_op + 8192)) *
207 2) - dec_ratio);
208 }
209
210 return 0;
211}
212
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300213static int mt312_get_code_rate(struct mt312_state *state, fe_code_rate_t *cr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 const fe_code_rate_t fec_tab[8] =
216 { FEC_1_2, FEC_2_3, FEC_3_4, FEC_5_6, FEC_6_7, FEC_7_8,
217 FEC_AUTO, FEC_AUTO };
218
219 int ret;
220 u8 fec_status;
221
222 if ((ret = mt312_readreg(state, FEC_STATUS, &fec_status)) < 0)
223 return ret;
224
225 *cr = fec_tab[(fec_status >> 4) & 0x07];
226
227 return 0;
228}
229
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300230static int mt312_initfe(struct dvb_frontend *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700232 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 int ret;
234 u8 buf[2];
235
236 /* wake up */
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300237 if ((ret = mt312_writereg(state, CONFIG,
238 (state->frequency == 60 ? 0x88 : 0x8c))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return ret;
240
241 /* wait at least 150 usec */
242 udelay(150);
243
244 /* full reset */
245 if ((ret = mt312_reset(state, 1)) < 0)
246 return ret;
247
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300248/* Per datasheet, write correct values. 09/28/03 ACCJr.
249 * If we don't do this, we won't get FE_HAS_VITERBI in the VP310. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 {
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300251 u8 buf_def[8] = { 0x14, 0x12, 0x03, 0x02,
252 0x01, 0x00, 0x00, 0x00 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300254 if ((ret = mt312_write(state, VIT_SETUP, buf_def,
255 sizeof(buf_def))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return ret;
257 }
258
259 /* SYS_CLK */
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300260 buf[0] = mt312_div((state->frequency == 60 ? MT312_LPOWER_SYS_CLK :
261 MT312_SYS_CLK) * 2, 1000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 /* DISEQC_RATIO */
264 buf[1] = mt312_div(MT312_PLL_CLK, 15000 * 4);
265
266 if ((ret = mt312_write(state, SYS_CLK, buf, sizeof(buf))) < 0)
267 return ret;
268
269 if ((ret = mt312_writereg(state, SNR_THS_HIGH, 0x32)) < 0)
270 return ret;
271
272 if ((ret = mt312_writereg(state, OP_CTRL, 0x53)) < 0)
273 return ret;
274
275 /* TS_SW_LIM */
276 buf[0] = 0x8c;
277 buf[1] = 0x98;
278
279 if ((ret = mt312_write(state, TS_SW_LIM_L, buf, sizeof(buf))) < 0)
280 return ret;
281
282 if ((ret = mt312_writereg(state, CS_SW_LIM, 0x69)) < 0)
283 return ret;
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return 0;
286}
287
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300288static int mt312_send_master_cmd(struct dvb_frontend *fe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 struct dvb_diseqc_master_cmd *c)
290{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700291 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 int ret;
293 u8 diseqc_mode;
294
295 if ((c->msg_len == 0) || (c->msg_len > sizeof(c->msg)))
296 return -EINVAL;
297
298 if ((ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode)) < 0)
299 return ret;
300
301 if ((ret =
302 mt312_write(state, (0x80 | DISEQC_INSTR), c->msg, c->msg_len)) < 0)
303 return ret;
304
305 if ((ret =
306 mt312_writereg(state, DISEQC_MODE,
307 (diseqc_mode & 0x40) | ((c->msg_len - 1) << 3)
308 | 0x04)) < 0)
309 return ret;
310
311 /* set DISEQC_MODE[2:0] to zero if a return message is expected */
312 if (c->msg[0] & 0x02)
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300313 if ((ret = mt312_writereg(state, DISEQC_MODE,
314 (diseqc_mode & 0x40))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return ret;
316
317 return 0;
318}
319
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300320static int mt312_send_burst(struct dvb_frontend *fe, const fe_sec_mini_cmd_t c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700322 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 const u8 mini_tab[2] = { 0x02, 0x03 };
324
325 int ret;
326 u8 diseqc_mode;
327
328 if (c > SEC_MINI_B)
329 return -EINVAL;
330
331 if ((ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode)) < 0)
332 return ret;
333
334 if ((ret =
335 mt312_writereg(state, DISEQC_MODE,
336 (diseqc_mode & 0x40) | mini_tab[c])) < 0)
337 return ret;
338
339 return 0;
340}
341
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300342static int mt312_set_tone(struct dvb_frontend *fe, const fe_sec_tone_mode_t t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700344 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 const u8 tone_tab[2] = { 0x01, 0x00 };
346
347 int ret;
348 u8 diseqc_mode;
349
350 if (t > SEC_TONE_OFF)
351 return -EINVAL;
352
353 if ((ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode)) < 0)
354 return ret;
355
356 if ((ret =
357 mt312_writereg(state, DISEQC_MODE,
358 (diseqc_mode & 0x40) | tone_tab[t])) < 0)
359 return ret;
360
361 return 0;
362}
363
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300364static int mt312_set_voltage(struct dvb_frontend *fe, const fe_sec_voltage_t v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700366 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 const u8 volt_tab[3] = { 0x00, 0x40, 0x00 };
368
369 if (v > SEC_VOLTAGE_OFF)
370 return -EINVAL;
371
372 return mt312_writereg(state, DISEQC_MODE, volt_tab[v]);
373}
374
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300375static int mt312_read_status(struct dvb_frontend *fe, fe_status_t *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700377 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 int ret;
379 u8 status[3];
380
381 *s = 0;
382
383 if ((ret = mt312_read(state, QPSK_STAT_H, status, sizeof(status))) < 0)
384 return ret;
385
Matthias Schwarzott0b6a3342007-12-21 08:58:09 -0300386 dprintk("QPSK_STAT_H: 0x%02x, QPSK_STAT_L: 0x%02x,"
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300387 " FEC_STATUS: 0x%02x\n", status[0], status[1], status[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 if (status[0] & 0xc0)
390 *s |= FE_HAS_SIGNAL; /* signal noise ratio */
391 if (status[0] & 0x04)
392 *s |= FE_HAS_CARRIER; /* qpsk carrier lock */
393 if (status[2] & 0x02)
394 *s |= FE_HAS_VITERBI; /* viterbi lock */
395 if (status[2] & 0x04)
396 *s |= FE_HAS_SYNC; /* byte align lock */
397 if (status[0] & 0x01)
398 *s |= FE_HAS_LOCK; /* qpsk lock */
399
400 return 0;
401}
402
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300403static int mt312_read_ber(struct dvb_frontend *fe, u32 *ber)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700405 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 int ret;
407 u8 buf[3];
408
409 if ((ret = mt312_read(state, RS_BERCNT_H, buf, 3)) < 0)
410 return ret;
411
412 *ber = ((buf[0] << 16) | (buf[1] << 8) | buf[2]) * 64;
413
414 return 0;
415}
416
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300417static int mt312_read_signal_strength(struct dvb_frontend *fe,
418 u16 *signal_strength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700420 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 int ret;
422 u8 buf[3];
423 u16 agc;
424 s16 err_db;
425
426 if ((ret = mt312_read(state, AGC_H, buf, sizeof(buf))) < 0)
427 return ret;
428
429 agc = (buf[0] << 6) | (buf[1] >> 2);
430 err_db = (s16) (((buf[1] & 0x03) << 14) | buf[2] << 6) >> 6;
431
432 *signal_strength = agc;
433
Matthias Schwarzott0b6a3342007-12-21 08:58:09 -0300434 dprintk("agc=%08x err_db=%hd\n", agc, err_db);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 return 0;
437}
438
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300439static int mt312_read_snr(struct dvb_frontend *fe, u16 *snr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700441 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int ret;
443 u8 buf[2];
444
445 if ((ret = mt312_read(state, M_SNR_H, &buf, sizeof(buf))) < 0)
446 return ret;
447
448 *snr = 0xFFFF - ((((buf[0] & 0x7f) << 8) | buf[1]) << 1);
449
450 return 0;
451}
452
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300453static int mt312_read_ucblocks(struct dvb_frontend *fe, u32 *ubc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700455 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 int ret;
457 u8 buf[2];
458
459 if ((ret = mt312_read(state, RS_UBC_H, &buf, sizeof(buf))) < 0)
460 return ret;
461
462 *ubc = (buf[0] << 8) | buf[1];
463
464 return 0;
465}
466
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300467static int mt312_set_frontend(struct dvb_frontend *fe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 struct dvb_frontend_parameters *p)
469{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700470 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 int ret;
472 u8 buf[5], config_val;
473 u16 sr;
474
475 const u8 fec_tab[10] =
476 { 0x00, 0x01, 0x02, 0x04, 0x3f, 0x08, 0x10, 0x20, 0x3f, 0x3f };
477 const u8 inv_tab[3] = { 0x00, 0x40, 0x80 };
478
479 dprintk("%s: Freq %d\n", __FUNCTION__, p->frequency);
480
Patrick Boettcherdea74862006-05-14 05:01:31 -0300481 if ((p->frequency < fe->ops.info.frequency_min)
482 || (p->frequency > fe->ops.info.frequency_max))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return -EINVAL;
484
485 if ((p->inversion < INVERSION_OFF)
486 || (p->inversion > INVERSION_ON))
487 return -EINVAL;
488
Patrick Boettcherdea74862006-05-14 05:01:31 -0300489 if ((p->u.qpsk.symbol_rate < fe->ops.info.symbol_rate_min)
490 || (p->u.qpsk.symbol_rate > fe->ops.info.symbol_rate_max))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return -EINVAL;
492
493 if ((p->u.qpsk.fec_inner < FEC_NONE)
494 || (p->u.qpsk.fec_inner > FEC_AUTO))
495 return -EINVAL;
496
497 if ((p->u.qpsk.fec_inner == FEC_4_5)
498 || (p->u.qpsk.fec_inner == FEC_8_9))
499 return -EINVAL;
500
501 switch (state->id) {
502 case ID_VP310:
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300503 /* For now we will do this only for the VP310.
504 * It should be better for the mt312 as well,
505 * but tuning will be slower. ACCJr 09/29/03
506 */
Alexey Dobriyan682e8522006-01-10 00:09:16 +0300507 ret = mt312_readreg(state, CONFIG, &config_val);
508 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return ret;
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300510 if (p->u.qpsk.symbol_rate >= 30000000) {
511 /* Note that 30MS/s should use 90MHz */
512 if ((config_val & 0x0c) == 0x08) {
513 /* We are running 60MHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 state->frequency = 90;
515 if ((ret = mt312_initfe(fe)) < 0)
516 return ret;
517 }
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300518 } else {
519 if ((config_val & 0x0c) == 0x0C) {
520 /* We are running 90MHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 state->frequency = 60;
522 if ((ret = mt312_initfe(fe)) < 0)
523 return ret;
524 }
525 }
526 break;
527
528 case ID_MT312:
529 break;
530
531 default:
532 return -EINVAL;
533 }
534
Patrick Boettcherdea74862006-05-14 05:01:31 -0300535 if (fe->ops.tuner_ops.set_params) {
536 fe->ops.tuner_ops.set_params(fe, p);
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300537 if (fe->ops.i2c_gate_ctrl)
538 fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quinceya81870e2006-04-18 17:47:09 -0300539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 /* sr = (u16)(sr * 256.0 / 1000000.0) */
542 sr = mt312_div(p->u.qpsk.symbol_rate * 4, 15625);
543
544 /* SYM_RATE */
545 buf[0] = (sr >> 8) & 0x3f;
546 buf[1] = (sr >> 0) & 0xff;
547
548 /* VIT_MODE */
549 buf[2] = inv_tab[p->inversion] | fec_tab[p->u.qpsk.fec_inner];
550
551 /* QPSK_CTRL */
552 buf[3] = 0x40; /* swap I and Q before QPSK demodulation */
553
554 if (p->u.qpsk.symbol_rate < 10000000)
555 buf[3] |= 0x04; /* use afc mode */
556
557 /* GO */
558 buf[4] = 0x01;
559
560 if ((ret = mt312_write(state, SYM_RATE_H, buf, sizeof(buf))) < 0)
561 return ret;
562
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800563 mt312_reset(state, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 return 0;
566}
567
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300568static int mt312_get_frontend(struct dvb_frontend *fe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct dvb_frontend_parameters *p)
570{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700571 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 int ret;
573
574 if ((ret = mt312_get_inversion(state, &p->inversion)) < 0)
575 return ret;
576
577 if ((ret = mt312_get_symbol_rate(state, &p->u.qpsk.symbol_rate)) < 0)
578 return ret;
579
580 if ((ret = mt312_get_code_rate(state, &p->u.qpsk.fec_inner)) < 0)
581 return ret;
582
583 return 0;
584}
585
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300586static int mt312_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
Andrew de Quinceya81870e2006-04-18 17:47:09 -0300587{
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300588 struct mt312_state *state = fe->demodulator_priv;
Andrew de Quinceya81870e2006-04-18 17:47:09 -0300589
590 if (enable) {
591 return mt312_writereg(state, GPP_CTRL, 0x40);
592 } else {
593 return mt312_writereg(state, GPP_CTRL, 0x00);
594 }
595}
596
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300597static int mt312_sleep(struct dvb_frontend *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700599 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 int ret;
601 u8 config;
602
603 /* reset all registers to defaults */
604 if ((ret = mt312_reset(state, 1)) < 0)
605 return ret;
606
607 if ((ret = mt312_readreg(state, CONFIG, &config)) < 0)
608 return ret;
609
610 /* enter standby */
611 if ((ret = mt312_writereg(state, CONFIG, config & 0x7f)) < 0)
612 return ret;
613
614 return 0;
615}
616
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300617static int mt312_get_tune_settings(struct dvb_frontend *fe,
618 struct dvb_frontend_tune_settings *fesettings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 fesettings->min_delay_ms = 50;
621 fesettings->step_size = 0;
622 fesettings->max_drift = 0;
623 return 0;
624}
625
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300626static void mt312_release(struct dvb_frontend *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300628 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 kfree(state);
630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632static struct dvb_frontend_ops vp310_mt312_ops = {
633
634 .info = {
635 .name = "Zarlink ???? DVB-S",
636 .type = FE_QPSK,
637 .frequency_min = 950000,
638 .frequency_max = 2150000,
639 .frequency_stepsize = (MT312_PLL_CLK / 1000) / 128,
640 .symbol_rate_min = MT312_SYS_CLK / 128,
641 .symbol_rate_max = MT312_SYS_CLK / 2,
642 .caps =
643 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
644 FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
645 FE_CAN_FEC_AUTO | FE_CAN_QPSK | FE_CAN_MUTE_TS |
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800646 FE_CAN_RECOVER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 },
648
649 .release = mt312_release,
650
651 .init = mt312_initfe,
652 .sleep = mt312_sleep,
Andrew de Quinceya81870e2006-04-18 17:47:09 -0300653 .i2c_gate_ctrl = mt312_i2c_gate_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 .set_frontend = mt312_set_frontend,
656 .get_frontend = mt312_get_frontend,
657 .get_tune_settings = mt312_get_tune_settings,
658
659 .read_status = mt312_read_status,
660 .read_ber = mt312_read_ber,
661 .read_signal_strength = mt312_read_signal_strength,
662 .read_snr = mt312_read_snr,
663 .read_ucblocks = mt312_read_ucblocks,
664
665 .diseqc_send_master_cmd = mt312_send_master_cmd,
666 .diseqc_send_burst = mt312_send_burst,
667 .set_tone = mt312_set_tone,
668 .set_voltage = mt312_set_voltage,
669};
670
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300671struct dvb_frontend *vp310_mt312_attach(const struct mt312_config *config,
672 struct i2c_adapter *i2c)
Adrian Bunk805e6602006-02-27 00:07:49 -0300673{
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300674 struct mt312_state *state = NULL;
Adrian Bunk805e6602006-02-27 00:07:49 -0300675
676 /* allocate memory for the internal state */
677 state = kmalloc(sizeof(struct mt312_state), GFP_KERNEL);
678 if (state == NULL)
679 goto error;
680
681 /* setup the state */
682 state->config = config;
683 state->i2c = i2c;
Adrian Bunk805e6602006-02-27 00:07:49 -0300684
685 /* check if the demod is there */
686 if (mt312_readreg(state, ID, &state->id) < 0)
687 goto error;
688
Patrick Boettcherdea74862006-05-14 05:01:31 -0300689 /* create dvb_frontend */
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300690 memcpy(&state->frontend.ops, &vp310_mt312_ops,
691 sizeof(struct dvb_frontend_ops));
Patrick Boettcherdea74862006-05-14 05:01:31 -0300692 state->frontend.demodulator_priv = state;
693
Adrian Bunk805e6602006-02-27 00:07:49 -0300694 switch (state->id) {
695 case ID_VP310:
Patrick Boettcherdea74862006-05-14 05:01:31 -0300696 strcpy(state->frontend.ops.info.name, "Zarlink VP310 DVB-S");
Adrian Bunk805e6602006-02-27 00:07:49 -0300697 state->frequency = 90;
698 break;
699 case ID_MT312:
Patrick Boettcherdea74862006-05-14 05:01:31 -0300700 strcpy(state->frontend.ops.info.name, "Zarlink MT312 DVB-S");
Adrian Bunk805e6602006-02-27 00:07:49 -0300701 state->frequency = 60;
702 break;
703 default:
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300704 printk(KERN_WARNING "Only Zarlink VP310/MT312"
705 " are supported chips.\n");
Adrian Bunk805e6602006-02-27 00:07:49 -0300706 goto error;
707 }
708
Adrian Bunk805e6602006-02-27 00:07:49 -0300709 return &state->frontend;
710
711error:
712 kfree(state);
713 return NULL;
714}
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300715EXPORT_SYMBOL(vp310_mt312_attach);
Adrian Bunk805e6602006-02-27 00:07:49 -0300716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717module_param(debug, int, 0644);
718MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
719
720MODULE_DESCRIPTION("Zarlink VP310/MT312 DVB-S Demodulator driver");
721MODULE_AUTHOR("Andreas Oberritter <obi@linuxtv.org>");
722MODULE_LICENSE("GPL");
723