blob: 6c1cb1973c6e09c90010a7ec0167a9e50ae5b23e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 Driver for ST STV0299 demodulator
3
4 Copyright (C) 2001-2002 Convergence Integrated Media GmbH
5 <ralph@convergence.de>,
6 <holger@convergence.de>,
7 <js@convergence.de>
8
9
10 Philips SU1278/SH
11
12 Copyright (C) 2002 by Peter Schildmann <peter.schildmann@web.de>
13
14
15 LG TDQF-S001F
16
17 Copyright (C) 2002 Felix Domke <tmbinc@elitedvb.net>
18 & Andreas Oberritter <obi@linuxtv.org>
19
20
21 Support for Samsung TBMU24112IMB used on Technisat SkyStar2 rev. 2.6B
22
23 Copyright (C) 2003 Vadim Catana <skystar@moldova.cc>:
24
25 Support for Philips SU1278 on Technotrend hardware
26
27 Copyright (C) 2004 Andrew de Quincey <adq_dvb@lidskialf.net>
28
29 This program is free software; you can redistribute it and/or modify
30 it under the terms of the GNU General Public License as published by
31 the Free Software Foundation; either version 2 of the License, or
32 (at your option) any later version.
33
34 This program is distributed in the hope that it will be useful,
35 but WITHOUT ANY WARRANTY; without even the implied warranty of
36 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 GNU General Public License for more details.
38
39 You should have received a copy of the GNU General Public License
40 along with this program; if not, write to the Free Software
41 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42
43*/
44
45#include <linux/init.h>
46#include <linux/kernel.h>
47#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/string.h>
49#include <linux/slab.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080050#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/div64.h>
52
53#include "dvb_frontend.h"
54#include "stv0299.h"
55
56struct stv0299_state {
57 struct i2c_adapter* i2c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 const struct stv0299_config* config;
59 struct dvb_frontend frontend;
60
61 u8 initialised:1;
62 u32 tuner_frequency;
63 u32 symbol_rate;
64 fe_code_rate_t fec_inner;
Andrew de Quincey37650222005-11-08 21:35:11 -080065 int errmode;
Oliver Endriss7876ad72008-06-19 23:25:04 -030066 u32 ucblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
Andrew de Quincey37650222005-11-08 21:35:11 -080069#define STATUS_BER 0
70#define STATUS_UCBLOCKS 1
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int debug;
Johannes Stezenbach591ad982005-05-16 21:54:31 -070073static int debug_legacy_dish_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define dprintk(args...) \
75 do { \
76 if (debug) printk(KERN_DEBUG "stv0299: " args); \
77 } while (0)
78
79
80static int stv0299_writeregI (struct stv0299_state* state, u8 reg, u8 data)
81{
82 int ret;
83 u8 buf [] = { reg, data };
84 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
85
86 ret = i2c_transfer (state->i2c, &msg, 1);
87
88 if (ret != 1)
89 dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, "
Harvey Harrison271ddbf2008-04-08 23:20:00 -030090 "ret == %i)\n", __func__, reg, data, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 return (ret != 1) ? -EREMOTEIO : 0;
93}
94
Adrian Bunk34630402007-02-06 21:50:36 -030095static int stv0299_write(struct dvb_frontend* fe, u8 *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080097 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Andrew de Quinceyc10d14d2006-08-08 09:10:08 -030099 if (len != 2)
100 return -EINVAL;
101
102 return stv0299_writeregI(state, buf[0], buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
105static u8 stv0299_readreg (struct stv0299_state* state, u8 reg)
106{
107 int ret;
108 u8 b0 [] = { reg };
109 u8 b1 [] = { 0 };
110 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
111 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
112
113 ret = i2c_transfer (state->i2c, msg, 2);
114
115 if (ret != 2)
116 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n",
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300117 __func__, reg, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 return b1[0];
120}
121
122static int stv0299_readregs (struct stv0299_state* state, u8 reg1, u8 *b, u8 len)
123{
124 int ret;
125 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = &reg1, .len = 1 },
126 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = len } };
127
128 ret = i2c_transfer (state->i2c, msg, 2);
129
130 if (ret != 2)
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300131 dprintk("%s: readreg error (ret == %i)\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 return ret == 2 ? 0 : ret;
134}
135
136static int stv0299_set_FEC (struct stv0299_state* state, fe_code_rate_t fec)
137{
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300138 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 switch (fec) {
141 case FEC_AUTO:
142 {
143 return stv0299_writeregI (state, 0x31, 0x1f);
144 }
145 case FEC_1_2:
146 {
147 return stv0299_writeregI (state, 0x31, 0x01);
148 }
149 case FEC_2_3:
150 {
151 return stv0299_writeregI (state, 0x31, 0x02);
152 }
153 case FEC_3_4:
154 {
155 return stv0299_writeregI (state, 0x31, 0x04);
156 }
157 case FEC_5_6:
158 {
159 return stv0299_writeregI (state, 0x31, 0x08);
160 }
161 case FEC_7_8:
162 {
163 return stv0299_writeregI (state, 0x31, 0x10);
164 }
165 default:
166 {
167 return -EINVAL;
168 }
169 }
170}
171
172static fe_code_rate_t stv0299_get_fec (struct stv0299_state* state)
173{
174 static fe_code_rate_t fec_tab [] = { FEC_2_3, FEC_3_4, FEC_5_6,
175 FEC_7_8, FEC_1_2 };
176 u8 index;
177
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300178 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 index = stv0299_readreg (state, 0x1b);
181 index &= 0x7;
182
183 if (index > 4)
184 return FEC_AUTO;
185
186 return fec_tab [index];
187}
188
189static int stv0299_wait_diseqc_fifo (struct stv0299_state* state, int timeout)
190{
191 unsigned long start = jiffies;
192
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300193 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 while (stv0299_readreg(state, 0x0a) & 1) {
196 if (jiffies - start > timeout) {
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300197 dprintk ("%s: timeout!!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return -ETIMEDOUT;
199 }
200 msleep(10);
201 };
202
203 return 0;
204}
205
206static int stv0299_wait_diseqc_idle (struct stv0299_state* state, int timeout)
207{
208 unsigned long start = jiffies;
209
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300210 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 while ((stv0299_readreg(state, 0x0a) & 3) != 2 ) {
213 if (jiffies - start > timeout) {
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300214 dprintk ("%s: timeout!!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return -ETIMEDOUT;
216 }
217 msleep(10);
218 };
219
220 return 0;
221}
222
223static int stv0299_set_symbolrate (struct dvb_frontend* fe, u32 srate)
224{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800225 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 u64 big = srate;
227 u32 ratio;
228
229 // check rate is within limits
230 if ((srate < 1000000) || (srate > 45000000)) return -EINVAL;
231
232 // calculate value to program
233 big = big << 20;
234 big += (state->config->mclk-1); // round correctly
235 do_div(big, state->config->mclk);
236 ratio = big << 4;
237
238 return state->config->set_symbol_rate(fe, srate, ratio);
239}
240
241static int stv0299_get_symbolrate (struct stv0299_state* state)
242{
243 u32 Mclk = state->config->mclk / 4096L;
244 u32 srate;
245 s32 offset;
246 u8 sfr[3];
247 s8 rtf;
248
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300249 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 stv0299_readregs (state, 0x1f, sfr, 3);
Oliver Endriss0402a6c2007-07-12 23:22:59 -0300252 stv0299_readregs (state, 0x1a, (u8 *)&rtf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 srate = (sfr[0] << 8) | sfr[1];
255 srate *= Mclk;
256 srate /= 16;
257 srate += (sfr[2] >> 4) * Mclk / 256;
258 offset = (s32) rtf * (srate / 4096L);
259 offset /= 128;
260
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300261 dprintk ("%s : srate = %i\n", __func__, srate);
262 dprintk ("%s : ofset = %i\n", __func__, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 srate += offset;
265
266 srate += 1000;
267 srate /= 2000;
268 srate *= 2000;
269
270 return srate;
271}
272
273static int stv0299_send_diseqc_msg (struct dvb_frontend* fe,
274 struct dvb_diseqc_master_cmd *m)
275{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800276 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 u8 val;
278 int i;
279
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300280 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 if (stv0299_wait_diseqc_idle (state, 100) < 0)
283 return -ETIMEDOUT;
284
285 val = stv0299_readreg (state, 0x08);
286
287 if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x6)) /* DiSEqC mode */
288 return -EREMOTEIO;
289
290 for (i=0; i<m->msg_len; i++) {
291 if (stv0299_wait_diseqc_fifo (state, 100) < 0)
292 return -ETIMEDOUT;
293
294 if (stv0299_writeregI (state, 0x09, m->msg[i]))
295 return -EREMOTEIO;
296 }
297
298 if (stv0299_wait_diseqc_idle (state, 100) < 0)
299 return -ETIMEDOUT;
300
301 return 0;
302}
303
304static int stv0299_send_diseqc_burst (struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
305{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800306 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 u8 val;
308
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300309 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 if (stv0299_wait_diseqc_idle (state, 100) < 0)
312 return -ETIMEDOUT;
313
314 val = stv0299_readreg (state, 0x08);
315
316 if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x2)) /* burst mode */
317 return -EREMOTEIO;
318
319 if (stv0299_writeregI (state, 0x09, burst == SEC_MINI_A ? 0x00 : 0xff))
320 return -EREMOTEIO;
321
322 if (stv0299_wait_diseqc_idle (state, 100) < 0)
323 return -ETIMEDOUT;
324
325 if (stv0299_writeregI (state, 0x08, val))
326 return -EREMOTEIO;
327
328 return 0;
329}
330
331static int stv0299_set_tone (struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
332{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800333 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 u8 val;
335
336 if (stv0299_wait_diseqc_idle (state, 100) < 0)
337 return -ETIMEDOUT;
338
339 val = stv0299_readreg (state, 0x08);
340
341 switch (tone) {
342 case SEC_TONE_ON:
343 return stv0299_writeregI (state, 0x08, val | 0x3);
344
345 case SEC_TONE_OFF:
346 return stv0299_writeregI (state, 0x08, (val & ~0x3) | 0x02);
347
348 default:
349 return -EINVAL;
350 }
351}
352
353static int stv0299_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t voltage)
354{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800355 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 u8 reg0x08;
357 u8 reg0x0c;
358
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300359 dprintk("%s: %s\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 voltage == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" :
361 voltage == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??");
362
363 reg0x08 = stv0299_readreg (state, 0x08);
364 reg0x0c = stv0299_readreg (state, 0x0c);
365
366 /**
367 * H/V switching over OP0, OP1 and OP2 are LNB power enable bits
368 */
369 reg0x0c &= 0x0f;
Oliver Endrisse84b1332008-04-20 21:57:34 -0300370 reg0x08 = (reg0x08 & 0x3f) | (state->config->lock_output << 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 switch (voltage) {
373 case SEC_VOLTAGE_13:
Oliver Endrisse84b1332008-04-20 21:57:34 -0300374 if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0)
375 reg0x0c |= 0x10; /* OP1 off, OP0 on */
376 else
377 reg0x0c |= 0x40; /* OP1 on, OP0 off */
378 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 case SEC_VOLTAGE_18:
Oliver Endrisse84b1332008-04-20 21:57:34 -0300380 reg0x0c |= 0x50; /* OP1 on, OP0 on */
381 break;
382 case SEC_VOLTAGE_OFF:
383 /* LNB power off! */
384 reg0x08 = 0x00;
385 reg0x0c = 0x00;
386 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 default:
388 return -EINVAL;
389 };
Oliver Endrisse84b1332008-04-20 21:57:34 -0300390
391 if (state->config->op0_off)
392 reg0x0c &= ~0x10;
393
394 stv0299_writeregI(state, 0x08, reg0x08);
395 return stv0299_writeregI(state, 0x0c, reg0x0c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Peter Beutner400b7082006-01-09 15:32:43 -0200398static int stv0299_send_legacy_dish_cmd (struct dvb_frontend* fe, unsigned long cmd)
Johannes Stezenbach591ad982005-05-16 21:54:31 -0700399{
400 struct stv0299_state* state = fe->demodulator_priv;
401 u8 reg0x08;
402 u8 reg0x0c;
403 u8 lv_mask = 0x40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 u8 last = 1;
405 int i;
Johannes Stezenbach591ad982005-05-16 21:54:31 -0700406 struct timeval nexttime;
407 struct timeval tv[10];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Johannes Stezenbach591ad982005-05-16 21:54:31 -0700409 reg0x08 = stv0299_readreg (state, 0x08);
410 reg0x0c = stv0299_readreg (state, 0x0c);
411 reg0x0c &= 0x0f;
412 stv0299_writeregI (state, 0x08, (reg0x08 & 0x3f) | (state->config->lock_output << 6));
413 if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0)
414 lv_mask = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 cmd = cmd << 1;
Johannes Stezenbach591ad982005-05-16 21:54:31 -0700417 if (debug_legacy_dish_switch)
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300418 printk ("%s switch command: 0x%04lx\n",__func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Johannes Stezenbach591ad982005-05-16 21:54:31 -0700420 do_gettimeofday (&nexttime);
421 if (debug_legacy_dish_switch)
422 memcpy (&tv[0], &nexttime, sizeof (struct timeval));
423 stv0299_writeregI (state, 0x0c, reg0x0c | 0x50); /* set LNB to 18V */
424
NooneImportant83b75b02005-11-08 21:35:27 -0800425 dvb_frontend_sleep_until(&nexttime, 32000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 for (i=0; i<9; i++) {
Johannes Stezenbach591ad982005-05-16 21:54:31 -0700428 if (debug_legacy_dish_switch)
429 do_gettimeofday (&tv[i+1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if((cmd & 0x01) != last) {
Johannes Stezenbach591ad982005-05-16 21:54:31 -0700431 /* set voltage to (last ? 13V : 18V) */
432 stv0299_writeregI (state, 0x0c, reg0x0c | (last ? lv_mask : 0x50));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 last = (last) ? 0 : 1;
434 }
435
436 cmd = cmd >> 1;
437
438 if (i != 8)
NooneImportant83b75b02005-11-08 21:35:27 -0800439 dvb_frontend_sleep_until(&nexttime, 8000);
Johannes Stezenbach591ad982005-05-16 21:54:31 -0700440 }
441 if (debug_legacy_dish_switch) {
442 printk ("%s(%d): switch delay (should be 32k followed by all 8k\n",
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300443 __func__, fe->dvb->num);
NooneImportant83b75b02005-11-08 21:35:27 -0800444 for (i = 1; i < 10; i++)
445 printk ("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447
448 return 0;
449}
450
451static int stv0299_init (struct dvb_frontend* fe)
452{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800453 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 int i;
Oliver Endrisse84b1332008-04-20 21:57:34 -0300455 u8 reg;
456 u8 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 dprintk("stv0299: init chip\n");
459
Oliver Endrisse84b1332008-04-20 21:57:34 -0300460 for (i = 0; ; i += 2) {
461 reg = state->config->inittab[i];
462 val = state->config->inittab[i+1];
463 if (reg == 0xff && val == 0xff)
464 break;
465 if (reg == 0x0c && state->config->op0_off)
466 val &= ~0x10;
467 stv0299_writeregI(state, reg, val);
468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return 0;
471}
472
473static int stv0299_read_status(struct dvb_frontend* fe, fe_status_t* status)
474{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800475 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 u8 signal = 0xff - stv0299_readreg (state, 0x18);
478 u8 sync = stv0299_readreg (state, 0x1b);
479
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300480 dprintk ("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __func__, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 *status = 0;
482
483 if (signal > 10)
484 *status |= FE_HAS_SIGNAL;
485
486 if (sync & 0x80)
487 *status |= FE_HAS_CARRIER;
488
489 if (sync & 0x10)
490 *status |= FE_HAS_VITERBI;
491
492 if (sync & 0x08)
493 *status |= FE_HAS_SYNC;
494
495 if ((sync & 0x98) == 0x98)
496 *status |= FE_HAS_LOCK;
497
498 return 0;
499}
500
501static int stv0299_read_ber(struct dvb_frontend* fe, u32* ber)
502{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800503 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Oliver Endriss7876ad72008-06-19 23:25:04 -0300505 if (state->errmode != STATUS_BER)
506 return -ENOSYS;
507
508 *ber = stv0299_readreg(state, 0x1e) | (stv0299_readreg(state, 0x1d) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 return 0;
511}
512
513static int stv0299_read_signal_strength(struct dvb_frontend* fe, u16* strength)
514{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800515 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 s32 signal = 0xffff - ((stv0299_readreg (state, 0x18) << 8)
518 | stv0299_readreg (state, 0x19));
519
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300520 dprintk ("%s : FE_READ_SIGNAL_STRENGTH : AGC2I: 0x%02x%02x, signal=0x%04x\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 stv0299_readreg (state, 0x18),
522 stv0299_readreg (state, 0x19), (int) signal);
523
524 signal = signal * 5 / 4;
525 *strength = (signal > 0xffff) ? 0xffff : (signal < 0) ? 0 : signal;
526
527 return 0;
528}
529
530static int stv0299_read_snr(struct dvb_frontend* fe, u16* snr)
531{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800532 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 s32 xsnr = 0xffff - ((stv0299_readreg (state, 0x24) << 8)
535 | stv0299_readreg (state, 0x25));
536 xsnr = 3 * (xsnr - 0xa100);
537 *snr = (xsnr > 0xffff) ? 0xffff : (xsnr < 0) ? 0 : xsnr;
538
539 return 0;
540}
541
542static int stv0299_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
543{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800544 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Oliver Endriss7876ad72008-06-19 23:25:04 -0300546 if (state->errmode != STATUS_UCBLOCKS)
547 return -ENOSYS;
548
549 state->ucblocks += stv0299_readreg(state, 0x1e);
550 state->ucblocks += (stv0299_readreg(state, 0x1d) << 8);
551 *ucblocks = state->ucblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 return 0;
554}
555
556static int stv0299_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters * p)
557{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800558 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 int invval = 0;
560
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300561 dprintk ("%s : FE_SET_FRONTEND\n", __func__);
Igor M. Liplianine4aab642008-09-23 15:43:57 -0300562 if (state->config->set_ts_params)
563 state->config->set_ts_params(fe, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 // set the inversion
566 if (p->inversion == INVERSION_OFF) invval = 0;
567 else if (p->inversion == INVERSION_ON) invval = 1;
568 else {
569 printk("stv0299 does not support auto-inversion\n");
570 return -EINVAL;
571 }
572 if (state->config->invert) invval = (~invval) & 1;
573 stv0299_writeregI(state, 0x0c, (stv0299_readreg(state, 0x0c) & 0xfe) | invval);
574
Patrick Boettcherdea74862006-05-14 05:01:31 -0300575 if (fe->ops.tuner_ops.set_params) {
576 fe->ops.tuner_ops.set_params(fe, p);
577 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quincey53a8ee32006-04-18 17:47:10 -0300578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Andrew de Quincy3528cc4e2005-11-08 21:35:28 -0800580 stv0299_set_FEC (state, p->u.qpsk.fec_inner);
581 stv0299_set_symbolrate (fe, p->u.qpsk.symbol_rate);
582 stv0299_writeregI(state, 0x22, 0x00);
583 stv0299_writeregI(state, 0x23, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 state->tuner_frequency = p->frequency;
586 state->fec_inner = p->u.qpsk.fec_inner;
587 state->symbol_rate = p->u.qpsk.symbol_rate;
588
589 return 0;
590}
591
592static int stv0299_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters * p)
593{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800594 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 s32 derot_freq;
596 int invval;
597
598 derot_freq = (s32)(s16) ((stv0299_readreg (state, 0x22) << 8)
599 | stv0299_readreg (state, 0x23));
600
601 derot_freq *= (state->config->mclk >> 16);
602 derot_freq += 500;
603 derot_freq /= 1000;
604
605 p->frequency += derot_freq;
606
607 invval = stv0299_readreg (state, 0x0c) & 1;
608 if (state->config->invert) invval = (~invval) & 1;
609 p->inversion = invval ? INVERSION_ON : INVERSION_OFF;
610
611 p->u.qpsk.fec_inner = stv0299_get_fec (state);
612 p->u.qpsk.symbol_rate = stv0299_get_symbolrate (state);
613
614 return 0;
615}
616
617static int stv0299_sleep(struct dvb_frontend* fe)
618{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800619 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 stv0299_writeregI(state, 0x02, 0x80);
622 state->initialised = 0;
623
624 return 0;
625}
626
Andrew de Quincey53a8ee32006-04-18 17:47:10 -0300627static int stv0299_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
628{
629 struct stv0299_state* state = fe->demodulator_priv;
630
631 if (enable) {
Andrew de Quinceya9686e02006-05-22 10:31:40 -0300632 stv0299_writeregI(state, 0x05, 0xb5);
Andrew de Quincey53a8ee32006-04-18 17:47:10 -0300633 } else {
Andrew de Quinceya9686e02006-05-22 10:31:40 -0300634 stv0299_writeregI(state, 0x05, 0x35);
Andrew de Quincey53a8ee32006-04-18 17:47:10 -0300635 }
Andrew de Quinceya9686e02006-05-22 10:31:40 -0300636 udelay(1);
637 return 0;
Andrew de Quincey53a8ee32006-04-18 17:47:10 -0300638}
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640static int stv0299_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
641{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800642 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 fesettings->min_delay_ms = state->config->min_delay_ms;
645 if (fesettings->parameters.u.qpsk.symbol_rate < 10000000) {
646 fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 32000;
647 fesettings->max_drift = 5000;
648 } else {
649 fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 16000;
650 fesettings->max_drift = fesettings->parameters.u.qpsk.symbol_rate / 2000;
651 }
652 return 0;
653}
654
655static void stv0299_release(struct dvb_frontend* fe)
656{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700657 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 kfree(state);
659}
660
661static struct dvb_frontend_ops stv0299_ops;
662
663struct dvb_frontend* stv0299_attach(const struct stv0299_config* config,
664 struct i2c_adapter* i2c)
665{
666 struct stv0299_state* state = NULL;
667 int id;
668
669 /* allocate memory for the internal state */
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700670 state = kmalloc(sizeof(struct stv0299_state), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (state == NULL) goto error;
672
673 /* setup the state */
674 state->config = config;
675 state->i2c = i2c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 state->initialised = 0;
677 state->tuner_frequency = 0;
678 state->symbol_rate = 0;
679 state->fec_inner = 0;
Andrew de Quincey37650222005-11-08 21:35:11 -0800680 state->errmode = STATUS_BER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 /* check if the demod is there */
683 stv0299_writeregI(state, 0x02, 0x34); /* standby off */
684 msleep(200);
685 id = stv0299_readreg(state, 0x00);
686
687 /* register 0x00 contains 0xa1 for STV0299 and STV0299B */
688 /* register 0x00 might contain 0x80 when returning from standby */
689 if (id != 0xa1 && id != 0x80) goto error;
690
691 /* create dvb_frontend */
Patrick Boettcherdea74862006-05-14 05:01:31 -0300692 memcpy(&state->frontend.ops, &stv0299_ops, sizeof(struct dvb_frontend_ops));
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800693 state->frontend.demodulator_priv = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return &state->frontend;
695
696error:
697 kfree(state);
698 return NULL;
699}
700
701static struct dvb_frontend_ops stv0299_ops = {
702
703 .info = {
704 .name = "ST STV0299 DVB-S",
705 .type = FE_QPSK,
706 .frequency_min = 950000,
707 .frequency_max = 2150000,
708 .frequency_stepsize = 125, /* kHz for QPSK frontends */
709 .frequency_tolerance = 0,
710 .symbol_rate_min = 1000000,
711 .symbol_rate_max = 45000000,
712 .symbol_rate_tolerance = 500, /* ppm */
713 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
714 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
715 FE_CAN_QPSK |
716 FE_CAN_FEC_AUTO
717 },
718
719 .release = stv0299_release,
720
721 .init = stv0299_init,
722 .sleep = stv0299_sleep,
Andrew de Quinceyc10d14d2006-08-08 09:10:08 -0300723 .write = stv0299_write,
Andrew de Quincey53a8ee32006-04-18 17:47:10 -0300724 .i2c_gate_ctrl = stv0299_i2c_gate_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 .set_frontend = stv0299_set_frontend,
727 .get_frontend = stv0299_get_frontend,
728 .get_tune_settings = stv0299_get_tune_settings,
729
730 .read_status = stv0299_read_status,
731 .read_ber = stv0299_read_ber,
732 .read_signal_strength = stv0299_read_signal_strength,
733 .read_snr = stv0299_read_snr,
734 .read_ucblocks = stv0299_read_ucblocks,
735
736 .diseqc_send_master_cmd = stv0299_send_diseqc_msg,
737 .diseqc_send_burst = stv0299_send_diseqc_burst,
738 .set_tone = stv0299_set_tone,
739 .set_voltage = stv0299_set_voltage,
740 .dishnetwork_send_legacy_command = stv0299_send_legacy_dish_cmd,
741};
742
Johannes Stezenbach591ad982005-05-16 21:54:31 -0700743module_param(debug_legacy_dish_switch, int, 0444);
744MODULE_PARM_DESC(debug_legacy_dish_switch, "Enable timing analysis for Dish Network legacy switches");
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746module_param(debug, int, 0644);
747MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
748
749MODULE_DESCRIPTION("ST STV0299 DVB Demodulator driver");
750MODULE_AUTHOR("Ralph Metzler, Holger Waechtler, Peter Schildmann, Felix Domke, "
Andrew de Quincey53a8ee32006-04-18 17:47:10 -0300751 "Andreas Oberritter, Andrew de Quincey, Kenneth Aafly");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752MODULE_LICENSE("GPL");
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754EXPORT_SYMBOL(stv0299_attach);