blob: 1f478c8c0d4a10c5a537da0f72b27b3dde84f47a [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;
Matthias Schwarzott111221f2008-04-12 15:04:48 -030046 unsigned long xtal;
47 u8 freq_mult;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49
50static int debug;
51#define dprintk(args...) \
52 do { \
Matthias Schwarzott89f64752007-12-21 08:56:44 -030053 if (debug) \
54 printk(KERN_DEBUG "mt312: " args); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 } while (0)
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define MT312_PLL_CLK 10000000UL /* 10 MHz */
58
Matthias Schwarzott89f64752007-12-21 08:56:44 -030059static int mt312_read(struct mt312_state *state, const enum mt312_reg_addr reg,
Matthias Schwarzott1881ee82008-04-12 15:04:46 -030060 u8 *buf, const size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 int ret;
63 struct i2c_msg msg[2];
64 u8 regbuf[1] = { reg };
65
66 msg[0].addr = state->config->demod_address;
67 msg[0].flags = 0;
68 msg[0].buf = regbuf;
69 msg[0].len = 1;
70 msg[1].addr = state->config->demod_address;
71 msg[1].flags = I2C_M_RD;
72 msg[1].buf = buf;
73 msg[1].len = count;
74
75 ret = i2c_transfer(state->i2c, msg, 2);
76
77 if (ret != 2) {
Harvey Harrison271ddbf2008-04-08 23:20:00 -030078 printk(KERN_ERR "%s: ret == %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return -EREMOTEIO;
80 }
81
Matthias Schwarzott89f64752007-12-21 08:56:44 -030082 if (debug) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 int i;
84 dprintk("R(%d):", reg & 0x7f);
85 for (i = 0; i < count; i++)
Matthias Schwarzott1881ee82008-04-12 15:04:46 -030086 printk(" %02x", buf[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 printk("\n");
88 }
89
90 return 0;
91}
92
Matthias Schwarzott89f64752007-12-21 08:56:44 -030093static int mt312_write(struct mt312_state *state, const enum mt312_reg_addr reg,
Matthias Schwarzott1881ee82008-04-12 15:04:46 -030094 const u8 *src, const size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 int ret;
97 u8 buf[count + 1];
98 struct i2c_msg msg;
99
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300100 if (debug) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 int i;
102 dprintk("W(%d):", reg & 0x7f);
103 for (i = 0; i < count; i++)
Matthias Schwarzott1881ee82008-04-12 15:04:46 -0300104 printk(" %02x", src[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 printk("\n");
106 }
107
108 buf[0] = reg;
109 memcpy(&buf[1], src, count);
110
111 msg.addr = state->config->demod_address;
112 msg.flags = 0;
113 msg.buf = buf;
114 msg.len = count + 1;
115
116 ret = i2c_transfer(state->i2c, &msg, 1);
117
118 if (ret != 1) {
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300119 dprintk("%s: ret == %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return -EREMOTEIO;
121 }
122
123 return 0;
124}
125
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300126static inline int mt312_readreg(struct mt312_state *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 const enum mt312_reg_addr reg, u8 *val)
128{
129 return mt312_read(state, reg, val, 1);
130}
131
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300132static inline int mt312_writereg(struct mt312_state *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 const enum mt312_reg_addr reg, const u8 val)
134{
135 return mt312_write(state, reg, &val, 1);
136}
137
138static inline u32 mt312_div(u32 a, u32 b)
139{
140 return (a + (b / 2)) / b;
141}
142
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300143static int mt312_reset(struct mt312_state *state, const u8 full)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 return mt312_writereg(state, RESET, full ? 0x80 : 0x40);
146}
147
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300148static int mt312_get_inversion(struct mt312_state *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 fe_spectral_inversion_t *i)
150{
151 int ret;
152 u8 vit_mode;
153
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300154 ret = mt312_readreg(state, VIT_MODE, &vit_mode);
155 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 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
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300173 ret = mt312_readreg(state, SYM_RATE_H, &sym_rate_h);
174 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return ret;
176
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300177 if (sym_rate_h & 0x80) {
178 /* symbol rate search was used */
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300179 ret = mt312_writereg(state, MON_CTRL, 0x03);
180 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return ret;
182
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300183 ret = mt312_read(state, MONITOR_H, buf, sizeof(buf));
184 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return ret;
186
187 monitor = (buf[0] << 8) | buf[1];
188
Matthias Schwarzott0b6a3342007-12-21 08:58:09 -0300189 dprintk("sr(auto) = %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 mt312_div(monitor * 15625, 4));
191 } else {
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300192 ret = mt312_writereg(state, MON_CTRL, 0x05);
193 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return ret;
195
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300196 ret = mt312_read(state, MONITOR_H, buf, sizeof(buf));
197 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return ret;
199
200 dec_ratio = ((buf[0] >> 5) & 0x07) * 32;
201
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300202 ret = mt312_read(state, SYM_RAT_OP_H, buf, sizeof(buf));
203 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return ret;
205
206 sym_rat_op = (buf[0] << 8) | buf[1];
207
Matthias Schwarzott0b6a3342007-12-21 08:58:09 -0300208 dprintk("sym_rat_op=%d dec_ratio=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 sym_rat_op, dec_ratio);
Matthias Schwarzott0b6a3342007-12-21 08:58:09 -0300210 dprintk("*sr(manual) = %lu\n",
Matthias Schwarzott111221f2008-04-12 15:04:48 -0300211 (((state->xtal * 8192) / (sym_rat_op + 8192)) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 2) - dec_ratio);
213 }
214
215 return 0;
216}
217
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300218static int mt312_get_code_rate(struct mt312_state *state, fe_code_rate_t *cr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 const fe_code_rate_t fec_tab[8] =
221 { FEC_1_2, FEC_2_3, FEC_3_4, FEC_5_6, FEC_6_7, FEC_7_8,
222 FEC_AUTO, FEC_AUTO };
223
224 int ret;
225 u8 fec_status;
226
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300227 ret = mt312_readreg(state, FEC_STATUS, &fec_status);
228 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return ret;
230
231 *cr = fec_tab[(fec_status >> 4) & 0x07];
232
233 return 0;
234}
235
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300236static int mt312_initfe(struct dvb_frontend *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700238 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 int ret;
240 u8 buf[2];
241
242 /* wake up */
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300243 ret = mt312_writereg(state, CONFIG,
Matthias Schwarzott111221f2008-04-12 15:04:48 -0300244 (state->freq_mult == 6 ? 0x88 : 0x8c));
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300245 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return ret;
247
248 /* wait at least 150 usec */
249 udelay(150);
250
251 /* full reset */
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300252 ret = mt312_reset(state, 1);
253 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return ret;
255
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300256/* Per datasheet, write correct values. 09/28/03 ACCJr.
257 * If we don't do this, we won't get FE_HAS_VITERBI in the VP310. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 {
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300259 u8 buf_def[8] = { 0x14, 0x12, 0x03, 0x02,
260 0x01, 0x00, 0x00, 0x00 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300262 ret = mt312_write(state, VIT_SETUP, buf_def, sizeof(buf_def));
263 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return ret;
265 }
266
267 /* SYS_CLK */
Matthias Schwarzott111221f2008-04-12 15:04:48 -0300268 buf[0] = mt312_div(state->xtal * state->freq_mult * 2, 1000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 /* DISEQC_RATIO */
Matthias Schwarzott111221f2008-04-12 15:04:48 -0300271 buf[1] = mt312_div(state->xtal, 22000 * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300273 ret = mt312_write(state, SYS_CLK, buf, sizeof(buf));
274 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return ret;
276
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300277 ret = mt312_writereg(state, SNR_THS_HIGH, 0x32);
278 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return ret;
280
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300281 ret = mt312_writereg(state, OP_CTRL, 0x53);
282 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return ret;
284
285 /* TS_SW_LIM */
286 buf[0] = 0x8c;
287 buf[1] = 0x98;
288
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300289 ret = mt312_write(state, TS_SW_LIM_L, buf, sizeof(buf));
290 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return ret;
292
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300293 ret = mt312_writereg(state, CS_SW_LIM, 0x69);
294 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return ret;
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return 0;
298}
299
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300300static int mt312_send_master_cmd(struct dvb_frontend *fe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 struct dvb_diseqc_master_cmd *c)
302{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700303 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 int ret;
305 u8 diseqc_mode;
306
307 if ((c->msg_len == 0) || (c->msg_len > sizeof(c->msg)))
308 return -EINVAL;
309
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300310 ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode);
311 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return ret;
313
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300314 ret = mt312_write(state, (0x80 | DISEQC_INSTR), c->msg, c->msg_len);
315 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return ret;
317
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300318 ret = mt312_writereg(state, DISEQC_MODE,
319 (diseqc_mode & 0x40) | ((c->msg_len - 1) << 3)
320 | 0x04);
321 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return ret;
323
Matthias Schwarzott82cd2df2008-04-12 15:04:47 -0300324 /* is there a better way to wait for message to be transmitted */
325 msleep(100);
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /* set DISEQC_MODE[2:0] to zero if a return message is expected */
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300328 if (c->msg[0] & 0x02) {
329 ret = mt312_writereg(state, DISEQC_MODE, (diseqc_mode & 0x40));
330 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return ret;
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 return 0;
335}
336
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300337static int mt312_send_burst(struct dvb_frontend *fe, const fe_sec_mini_cmd_t c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700339 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 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
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300348 ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode);
349 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return ret;
351
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300352 ret = mt312_writereg(state, DISEQC_MODE,
353 (diseqc_mode & 0x40) | mini_tab[c]);
354 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return ret;
356
357 return 0;
358}
359
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300360static int mt312_set_tone(struct dvb_frontend *fe, const fe_sec_tone_mode_t t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700362 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 const u8 tone_tab[2] = { 0x01, 0x00 };
364
365 int ret;
366 u8 diseqc_mode;
367
368 if (t > SEC_TONE_OFF)
369 return -EINVAL;
370
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300371 ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode);
372 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return ret;
374
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300375 ret = mt312_writereg(state, DISEQC_MODE,
376 (diseqc_mode & 0x40) | tone_tab[t]);
377 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return ret;
379
380 return 0;
381}
382
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300383static int mt312_set_voltage(struct dvb_frontend *fe, const fe_sec_voltage_t v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700385 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 const u8 volt_tab[3] = { 0x00, 0x40, 0x00 };
387
388 if (v > SEC_VOLTAGE_OFF)
389 return -EINVAL;
390
391 return mt312_writereg(state, DISEQC_MODE, volt_tab[v]);
392}
393
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300394static int mt312_read_status(struct dvb_frontend *fe, fe_status_t *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700396 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 int ret;
398 u8 status[3];
399
400 *s = 0;
401
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300402 ret = mt312_read(state, QPSK_STAT_H, status, sizeof(status));
403 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return ret;
405
Matthias Schwarzott0b6a3342007-12-21 08:58:09 -0300406 dprintk("QPSK_STAT_H: 0x%02x, QPSK_STAT_L: 0x%02x,"
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300407 " FEC_STATUS: 0x%02x\n", status[0], status[1], status[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 if (status[0] & 0xc0)
410 *s |= FE_HAS_SIGNAL; /* signal noise ratio */
411 if (status[0] & 0x04)
412 *s |= FE_HAS_CARRIER; /* qpsk carrier lock */
413 if (status[2] & 0x02)
414 *s |= FE_HAS_VITERBI; /* viterbi lock */
415 if (status[2] & 0x04)
416 *s |= FE_HAS_SYNC; /* byte align lock */
417 if (status[0] & 0x01)
418 *s |= FE_HAS_LOCK; /* qpsk lock */
419
420 return 0;
421}
422
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300423static int mt312_read_ber(struct dvb_frontend *fe, u32 *ber)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700425 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int ret;
427 u8 buf[3];
428
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300429 ret = mt312_read(state, RS_BERCNT_H, buf, 3);
430 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return ret;
432
433 *ber = ((buf[0] << 16) | (buf[1] << 8) | buf[2]) * 64;
434
435 return 0;
436}
437
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300438static int mt312_read_signal_strength(struct dvb_frontend *fe,
439 u16 *signal_strength)
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[3];
444 u16 agc;
445 s16 err_db;
446
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300447 ret = mt312_read(state, AGC_H, buf, sizeof(buf));
448 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return ret;
450
451 agc = (buf[0] << 6) | (buf[1] >> 2);
452 err_db = (s16) (((buf[1] & 0x03) << 14) | buf[2] << 6) >> 6;
453
454 *signal_strength = agc;
455
Matthias Schwarzott0b6a3342007-12-21 08:58:09 -0300456 dprintk("agc=%08x err_db=%hd\n", agc, err_db);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 return 0;
459}
460
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300461static int mt312_read_snr(struct dvb_frontend *fe, u16 *snr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700463 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 int ret;
465 u8 buf[2];
466
Matthias Schwarzott1881ee82008-04-12 15:04:46 -0300467 ret = mt312_read(state, M_SNR_H, buf, sizeof(buf));
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300468 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return ret;
470
471 *snr = 0xFFFF - ((((buf[0] & 0x7f) << 8) | buf[1]) << 1);
472
473 return 0;
474}
475
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300476static int mt312_read_ucblocks(struct dvb_frontend *fe, u32 *ubc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700478 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 int ret;
480 u8 buf[2];
481
Matthias Schwarzott1881ee82008-04-12 15:04:46 -0300482 ret = mt312_read(state, RS_UBC_H, buf, sizeof(buf));
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300483 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return ret;
485
486 *ubc = (buf[0] << 8) | buf[1];
487
488 return 0;
489}
490
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300491static int mt312_set_frontend(struct dvb_frontend *fe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 struct dvb_frontend_parameters *p)
493{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700494 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 int ret;
496 u8 buf[5], config_val;
497 u16 sr;
498
499 const u8 fec_tab[10] =
500 { 0x00, 0x01, 0x02, 0x04, 0x3f, 0x08, 0x10, 0x20, 0x3f, 0x3f };
501 const u8 inv_tab[3] = { 0x00, 0x40, 0x80 };
502
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300503 dprintk("%s: Freq %d\n", __func__, p->frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Patrick Boettcherdea74862006-05-14 05:01:31 -0300505 if ((p->frequency < fe->ops.info.frequency_min)
506 || (p->frequency > fe->ops.info.frequency_max))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return -EINVAL;
508
509 if ((p->inversion < INVERSION_OFF)
510 || (p->inversion > INVERSION_ON))
511 return -EINVAL;
512
Patrick Boettcherdea74862006-05-14 05:01:31 -0300513 if ((p->u.qpsk.symbol_rate < fe->ops.info.symbol_rate_min)
514 || (p->u.qpsk.symbol_rate > fe->ops.info.symbol_rate_max))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return -EINVAL;
516
517 if ((p->u.qpsk.fec_inner < FEC_NONE)
518 || (p->u.qpsk.fec_inner > FEC_AUTO))
519 return -EINVAL;
520
521 if ((p->u.qpsk.fec_inner == FEC_4_5)
522 || (p->u.qpsk.fec_inner == FEC_8_9))
523 return -EINVAL;
524
525 switch (state->id) {
526 case ID_VP310:
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300527 /* For now we will do this only for the VP310.
528 * It should be better for the mt312 as well,
529 * but tuning will be slower. ACCJr 09/29/03
530 */
Alexey Dobriyan682e8522006-01-10 00:09:16 +0300531 ret = mt312_readreg(state, CONFIG, &config_val);
532 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return ret;
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300534 if (p->u.qpsk.symbol_rate >= 30000000) {
535 /* Note that 30MS/s should use 90MHz */
Matthias Schwarzott111221f2008-04-12 15:04:48 -0300536 if (state->freq_mult == 6) {
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300537 /* We are running 60MHz */
Matthias Schwarzott111221f2008-04-12 15:04:48 -0300538 state->freq_mult = 9;
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300539 ret = mt312_initfe(fe);
540 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return ret;
542 }
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300543 } else {
Matthias Schwarzott111221f2008-04-12 15:04:48 -0300544 if (state->freq_mult == 9) {
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300545 /* We are running 90MHz */
Matthias Schwarzott111221f2008-04-12 15:04:48 -0300546 state->freq_mult = 6;
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300547 ret = mt312_initfe(fe);
548 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return ret;
550 }
551 }
552 break;
553
554 case ID_MT312:
555 break;
556
557 default:
558 return -EINVAL;
559 }
560
Patrick Boettcherdea74862006-05-14 05:01:31 -0300561 if (fe->ops.tuner_ops.set_params) {
562 fe->ops.tuner_ops.set_params(fe, p);
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300563 if (fe->ops.i2c_gate_ctrl)
564 fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quinceya81870e2006-04-18 17:47:09 -0300565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 /* sr = (u16)(sr * 256.0 / 1000000.0) */
568 sr = mt312_div(p->u.qpsk.symbol_rate * 4, 15625);
569
570 /* SYM_RATE */
571 buf[0] = (sr >> 8) & 0x3f;
572 buf[1] = (sr >> 0) & 0xff;
573
574 /* VIT_MODE */
575 buf[2] = inv_tab[p->inversion] | fec_tab[p->u.qpsk.fec_inner];
576
577 /* QPSK_CTRL */
578 buf[3] = 0x40; /* swap I and Q before QPSK demodulation */
579
580 if (p->u.qpsk.symbol_rate < 10000000)
581 buf[3] |= 0x04; /* use afc mode */
582
583 /* GO */
584 buf[4] = 0x01;
585
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300586 ret = mt312_write(state, SYM_RATE_H, buf, sizeof(buf));
587 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return ret;
589
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800590 mt312_reset(state, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 return 0;
593}
594
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300595static int mt312_get_frontend(struct dvb_frontend *fe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 struct dvb_frontend_parameters *p)
597{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700598 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 int ret;
600
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300601 ret = mt312_get_inversion(state, &p->inversion);
602 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return ret;
604
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300605 ret = mt312_get_symbol_rate(state, &p->u.qpsk.symbol_rate);
606 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return ret;
608
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300609 ret = mt312_get_code_rate(state, &p->u.qpsk.fec_inner);
610 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return ret;
612
613 return 0;
614}
615
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300616static int mt312_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
Andrew de Quinceya81870e2006-04-18 17:47:09 -0300617{
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300618 struct mt312_state *state = fe->demodulator_priv;
Andrew de Quinceya81870e2006-04-18 17:47:09 -0300619
620 if (enable) {
621 return mt312_writereg(state, GPP_CTRL, 0x40);
622 } else {
623 return mt312_writereg(state, GPP_CTRL, 0x00);
624 }
625}
626
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300627static int mt312_sleep(struct dvb_frontend *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700629 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 int ret;
631 u8 config;
632
633 /* reset all registers to defaults */
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300634 ret = mt312_reset(state, 1);
635 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return ret;
637
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300638 ret = mt312_readreg(state, CONFIG, &config);
639 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return ret;
641
642 /* enter standby */
Matthias Schwarzott994fc28b2007-12-24 07:12:55 -0300643 ret = mt312_writereg(state, CONFIG, config & 0x7f);
644 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return ret;
646
647 return 0;
648}
649
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300650static int mt312_get_tune_settings(struct dvb_frontend *fe,
651 struct dvb_frontend_tune_settings *fesettings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 fesettings->min_delay_ms = 50;
654 fesettings->step_size = 0;
655 fesettings->max_drift = 0;
656 return 0;
657}
658
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300659static void mt312_release(struct dvb_frontend *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300661 struct mt312_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 kfree(state);
663}
664
Matthias Schwarzott111221f2008-04-12 15:04:48 -0300665#define MT312_SYS_CLK 90000000UL /* 90 MHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666static struct dvb_frontend_ops vp310_mt312_ops = {
667
668 .info = {
669 .name = "Zarlink ???? DVB-S",
670 .type = FE_QPSK,
671 .frequency_min = 950000,
672 .frequency_max = 2150000,
Matthias Schwarzott111221f2008-04-12 15:04:48 -0300673 .frequency_stepsize = (MT312_PLL_CLK / 1000) / 128, /* FIXME: adjust freq to real used xtal */
674 .symbol_rate_min = MT312_SYS_CLK / 128, /* FIXME as above */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 .symbol_rate_max = MT312_SYS_CLK / 2,
676 .caps =
677 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
678 FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
679 FE_CAN_FEC_AUTO | FE_CAN_QPSK | FE_CAN_MUTE_TS |
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800680 FE_CAN_RECOVER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 },
682
683 .release = mt312_release,
684
685 .init = mt312_initfe,
686 .sleep = mt312_sleep,
Andrew de Quinceya81870e2006-04-18 17:47:09 -0300687 .i2c_gate_ctrl = mt312_i2c_gate_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 .set_frontend = mt312_set_frontend,
690 .get_frontend = mt312_get_frontend,
691 .get_tune_settings = mt312_get_tune_settings,
692
693 .read_status = mt312_read_status,
694 .read_ber = mt312_read_ber,
695 .read_signal_strength = mt312_read_signal_strength,
696 .read_snr = mt312_read_snr,
697 .read_ucblocks = mt312_read_ucblocks,
698
699 .diseqc_send_master_cmd = mt312_send_master_cmd,
700 .diseqc_send_burst = mt312_send_burst,
701 .set_tone = mt312_set_tone,
702 .set_voltage = mt312_set_voltage,
703};
704
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300705struct dvb_frontend *vp310_mt312_attach(const struct mt312_config *config,
706 struct i2c_adapter *i2c)
Adrian Bunk805e6602006-02-27 00:07:49 -0300707{
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300708 struct mt312_state *state = NULL;
Adrian Bunk805e6602006-02-27 00:07:49 -0300709
710 /* allocate memory for the internal state */
711 state = kmalloc(sizeof(struct mt312_state), GFP_KERNEL);
712 if (state == NULL)
713 goto error;
714
715 /* setup the state */
716 state->config = config;
717 state->i2c = i2c;
Adrian Bunk805e6602006-02-27 00:07:49 -0300718
719 /* check if the demod is there */
720 if (mt312_readreg(state, ID, &state->id) < 0)
721 goto error;
722
Patrick Boettcherdea74862006-05-14 05:01:31 -0300723 /* create dvb_frontend */
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300724 memcpy(&state->frontend.ops, &vp310_mt312_ops,
725 sizeof(struct dvb_frontend_ops));
Patrick Boettcherdea74862006-05-14 05:01:31 -0300726 state->frontend.demodulator_priv = state;
727
Adrian Bunk805e6602006-02-27 00:07:49 -0300728 switch (state->id) {
729 case ID_VP310:
Patrick Boettcherdea74862006-05-14 05:01:31 -0300730 strcpy(state->frontend.ops.info.name, "Zarlink VP310 DVB-S");
Matthias Schwarzott111221f2008-04-12 15:04:48 -0300731 state->xtal = MT312_PLL_CLK;
732 state->freq_mult = 9;
Adrian Bunk805e6602006-02-27 00:07:49 -0300733 break;
734 case ID_MT312:
Patrick Boettcherdea74862006-05-14 05:01:31 -0300735 strcpy(state->frontend.ops.info.name, "Zarlink MT312 DVB-S");
Matthias Schwarzott111221f2008-04-12 15:04:48 -0300736 state->xtal = MT312_PLL_CLK;
737 state->freq_mult = 6;
Adrian Bunk805e6602006-02-27 00:07:49 -0300738 break;
739 default:
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300740 printk(KERN_WARNING "Only Zarlink VP310/MT312"
741 " are supported chips.\n");
Adrian Bunk805e6602006-02-27 00:07:49 -0300742 goto error;
743 }
744
Adrian Bunk805e6602006-02-27 00:07:49 -0300745 return &state->frontend;
746
747error:
748 kfree(state);
749 return NULL;
750}
Matthias Schwarzott89f64752007-12-21 08:56:44 -0300751EXPORT_SYMBOL(vp310_mt312_attach);
Adrian Bunk805e6602006-02-27 00:07:49 -0300752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753module_param(debug, int, 0644);
754MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
755
756MODULE_DESCRIPTION("Zarlink VP310/MT312 DVB-S Demodulator driver");
757MODULE_AUTHOR("Andreas Oberritter <obi@linuxtv.org>");
758MODULE_LICENSE("GPL");
759