blob: 0ca5d9f0d851bc6c0eb24408ff5f644b8aad9f34 [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;
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -030064 enum fe_code_rate fec_inner;
Andrew de Quincey37650222005-11-08 21:35:11 -080065 int errmode;
Oliver Endriss7876ad72008-06-19 23:25:04 -030066 u32 ucblocks;
Malcolm Priestley24fb0602011-03-26 21:57:33 -030067 u8 mcr_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
Andrew de Quincey37650222005-11-08 21:35:11 -080070#define STATUS_BER 0
71#define STATUS_UCBLOCKS 1
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static int debug;
Johannes Stezenbach591ad982005-05-16 21:54:31 -070074static int debug_legacy_dish_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define dprintk(args...) \
76 do { \
77 if (debug) printk(KERN_DEBUG "stv0299: " args); \
78 } while (0)
79
80
81static int stv0299_writeregI (struct stv0299_state* state, u8 reg, u8 data)
82{
83 int ret;
84 u8 buf [] = { reg, data };
85 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
86
87 ret = i2c_transfer (state->i2c, &msg, 1);
88
89 if (ret != 1)
90 dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, "
Harvey Harrison271ddbf2008-04-08 23:20:00 -030091 "ret == %i)\n", __func__, reg, data, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 return (ret != 1) ? -EREMOTEIO : 0;
94}
95
lawrence rust2e4e98e2010-08-25 09:50:20 -030096static int stv0299_write(struct dvb_frontend* fe, const u8 buf[], int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080098 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Andrew de Quinceyc10d14d2006-08-08 09:10:08 -0300100 if (len != 2)
101 return -EINVAL;
102
103 return stv0299_writeregI(state, buf[0], buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106static u8 stv0299_readreg (struct stv0299_state* state, u8 reg)
107{
108 int ret;
109 u8 b0 [] = { reg };
110 u8 b1 [] = { 0 };
111 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
112 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
113
114 ret = i2c_transfer (state->i2c, msg, 2);
115
116 if (ret != 2)
117 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n",
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300118 __func__, reg, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 return b1[0];
121}
122
123static int stv0299_readregs (struct stv0299_state* state, u8 reg1, u8 *b, u8 len)
124{
125 int ret;
126 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = &reg1, .len = 1 },
127 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = len } };
128
129 ret = i2c_transfer (state->i2c, msg, 2);
130
131 if (ret != 2)
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300132 dprintk("%s: readreg error (ret == %i)\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 return ret == 2 ? 0 : ret;
135}
136
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300137static int stv0299_set_FEC(struct stv0299_state *state, enum fe_code_rate fec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300139 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 switch (fec) {
142 case FEC_AUTO:
143 {
144 return stv0299_writeregI (state, 0x31, 0x1f);
145 }
146 case FEC_1_2:
147 {
148 return stv0299_writeregI (state, 0x31, 0x01);
149 }
150 case FEC_2_3:
151 {
152 return stv0299_writeregI (state, 0x31, 0x02);
153 }
154 case FEC_3_4:
155 {
156 return stv0299_writeregI (state, 0x31, 0x04);
157 }
158 case FEC_5_6:
159 {
160 return stv0299_writeregI (state, 0x31, 0x08);
161 }
162 case FEC_7_8:
163 {
164 return stv0299_writeregI (state, 0x31, 0x10);
165 }
166 default:
167 {
168 return -EINVAL;
169 }
170 }
171}
172
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300173static enum fe_code_rate stv0299_get_fec(struct stv0299_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300175 static enum fe_code_rate fec_tab[] = { FEC_2_3, FEC_3_4, FEC_5_6,
176 FEC_7_8, FEC_1_2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 u8 index;
178
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300179 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 index = stv0299_readreg (state, 0x1b);
182 index &= 0x7;
183
184 if (index > 4)
185 return FEC_AUTO;
186
187 return fec_tab [index];
188}
189
190static int stv0299_wait_diseqc_fifo (struct stv0299_state* state, int timeout)
191{
192 unsigned long start = jiffies;
193
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300194 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 while (stv0299_readreg(state, 0x0a) & 1) {
197 if (jiffies - start > timeout) {
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300198 dprintk ("%s: timeout!!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return -ETIMEDOUT;
200 }
201 msleep(10);
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 return 0;
205}
206
207static int stv0299_wait_diseqc_idle (struct stv0299_state* state, int timeout)
208{
209 unsigned long start = jiffies;
210
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300211 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 while ((stv0299_readreg(state, 0x0a) & 3) != 2 ) {
214 if (jiffies - start > timeout) {
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300215 dprintk ("%s: timeout!!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return -ETIMEDOUT;
217 }
218 msleep(10);
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 return 0;
222}
223
224static int stv0299_set_symbolrate (struct dvb_frontend* fe, u32 srate)
225{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800226 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 u64 big = srate;
228 u32 ratio;
229
230 // check rate is within limits
231 if ((srate < 1000000) || (srate > 45000000)) return -EINVAL;
232
233 // calculate value to program
234 big = big << 20;
235 big += (state->config->mclk-1); // round correctly
236 do_div(big, state->config->mclk);
237 ratio = big << 4;
238
239 return state->config->set_symbol_rate(fe, srate, ratio);
240}
241
242static int stv0299_get_symbolrate (struct stv0299_state* state)
243{
244 u32 Mclk = state->config->mclk / 4096L;
245 u32 srate;
246 s32 offset;
247 u8 sfr[3];
248 s8 rtf;
249
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300250 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 stv0299_readregs (state, 0x1f, sfr, 3);
Oliver Endriss0402a6c2007-07-12 23:22:59 -0300253 stv0299_readregs (state, 0x1a, (u8 *)&rtf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 srate = (sfr[0] << 8) | sfr[1];
256 srate *= Mclk;
257 srate /= 16;
258 srate += (sfr[2] >> 4) * Mclk / 256;
259 offset = (s32) rtf * (srate / 4096L);
260 offset /= 128;
261
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300262 dprintk ("%s : srate = %i\n", __func__, srate);
263 dprintk ("%s : ofset = %i\n", __func__, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 srate += offset;
266
267 srate += 1000;
268 srate /= 2000;
269 srate *= 2000;
270
271 return srate;
272}
273
274static int stv0299_send_diseqc_msg (struct dvb_frontend* fe,
275 struct dvb_diseqc_master_cmd *m)
276{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800277 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 u8 val;
279 int i;
280
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300281 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 if (stv0299_wait_diseqc_idle (state, 100) < 0)
284 return -ETIMEDOUT;
285
286 val = stv0299_readreg (state, 0x08);
287
288 if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x6)) /* DiSEqC mode */
289 return -EREMOTEIO;
290
291 for (i=0; i<m->msg_len; i++) {
292 if (stv0299_wait_diseqc_fifo (state, 100) < 0)
293 return -ETIMEDOUT;
294
295 if (stv0299_writeregI (state, 0x09, m->msg[i]))
296 return -EREMOTEIO;
297 }
298
299 if (stv0299_wait_diseqc_idle (state, 100) < 0)
300 return -ETIMEDOUT;
301
302 return 0;
303}
304
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300305static int stv0299_send_diseqc_burst(struct dvb_frontend *fe,
306 enum fe_sec_mini_cmd burst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800308 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 u8 val;
310
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300311 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 if (stv0299_wait_diseqc_idle (state, 100) < 0)
314 return -ETIMEDOUT;
315
316 val = stv0299_readreg (state, 0x08);
317
318 if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x2)) /* burst mode */
319 return -EREMOTEIO;
320
321 if (stv0299_writeregI (state, 0x09, burst == SEC_MINI_A ? 0x00 : 0xff))
322 return -EREMOTEIO;
323
324 if (stv0299_wait_diseqc_idle (state, 100) < 0)
325 return -ETIMEDOUT;
326
327 if (stv0299_writeregI (state, 0x08, val))
328 return -EREMOTEIO;
329
330 return 0;
331}
332
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300333static int stv0299_set_tone(struct dvb_frontend *fe,
334 enum fe_sec_tone_mode tone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800336 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 u8 val;
338
339 if (stv0299_wait_diseqc_idle (state, 100) < 0)
340 return -ETIMEDOUT;
341
342 val = stv0299_readreg (state, 0x08);
343
344 switch (tone) {
345 case SEC_TONE_ON:
346 return stv0299_writeregI (state, 0x08, val | 0x3);
347
348 case SEC_TONE_OFF:
349 return stv0299_writeregI (state, 0x08, (val & ~0x3) | 0x02);
350
351 default:
352 return -EINVAL;
353 }
354}
355
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300356static int stv0299_set_voltage(struct dvb_frontend *fe,
357 enum fe_sec_voltage voltage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800359 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 u8 reg0x08;
361 u8 reg0x0c;
362
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300363 dprintk("%s: %s\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 voltage == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" :
365 voltage == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??");
366
367 reg0x08 = stv0299_readreg (state, 0x08);
368 reg0x0c = stv0299_readreg (state, 0x0c);
369
370 /**
371 * H/V switching over OP0, OP1 and OP2 are LNB power enable bits
372 */
373 reg0x0c &= 0x0f;
Oliver Endrisse84b1332008-04-20 21:57:34 -0300374 reg0x08 = (reg0x08 & 0x3f) | (state->config->lock_output << 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 switch (voltage) {
377 case SEC_VOLTAGE_13:
Oliver Endrisse84b1332008-04-20 21:57:34 -0300378 if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0)
379 reg0x0c |= 0x10; /* OP1 off, OP0 on */
380 else
381 reg0x0c |= 0x40; /* OP1 on, OP0 off */
382 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 case SEC_VOLTAGE_18:
Oliver Endrisse84b1332008-04-20 21:57:34 -0300384 reg0x0c |= 0x50; /* OP1 on, OP0 on */
385 break;
386 case SEC_VOLTAGE_OFF:
387 /* LNB power off! */
388 reg0x08 = 0x00;
389 reg0x0c = 0x00;
390 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 default:
392 return -EINVAL;
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300393 }
Oliver Endrisse84b1332008-04-20 21:57:34 -0300394
395 if (state->config->op0_off)
396 reg0x0c &= ~0x10;
397
398 stv0299_writeregI(state, 0x08, reg0x08);
399 return stv0299_writeregI(state, 0x0c, reg0x0c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Peter Beutner400b7082006-01-09 15:32:43 -0200402static int stv0299_send_legacy_dish_cmd (struct dvb_frontend* fe, unsigned long cmd)
Johannes Stezenbach591ad982005-05-16 21:54:31 -0700403{
404 struct stv0299_state* state = fe->demodulator_priv;
405 u8 reg0x08;
406 u8 reg0x0c;
407 u8 lv_mask = 0x40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 u8 last = 1;
409 int i;
Johannes Stezenbach591ad982005-05-16 21:54:31 -0700410 struct timeval nexttime;
411 struct timeval tv[10];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Johannes Stezenbach591ad982005-05-16 21:54:31 -0700413 reg0x08 = stv0299_readreg (state, 0x08);
414 reg0x0c = stv0299_readreg (state, 0x0c);
415 reg0x0c &= 0x0f;
416 stv0299_writeregI (state, 0x08, (reg0x08 & 0x3f) | (state->config->lock_output << 6));
417 if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0)
418 lv_mask = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 cmd = cmd << 1;
Johannes Stezenbach591ad982005-05-16 21:54:31 -0700421 if (debug_legacy_dish_switch)
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300422 printk ("%s switch command: 0x%04lx\n",__func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Johannes Stezenbach591ad982005-05-16 21:54:31 -0700424 do_gettimeofday (&nexttime);
425 if (debug_legacy_dish_switch)
Ezequiel Garciaee45ddc2012-10-23 15:57:24 -0300426 tv[0] = nexttime;
Johannes Stezenbach591ad982005-05-16 21:54:31 -0700427 stv0299_writeregI (state, 0x0c, reg0x0c | 0x50); /* set LNB to 18V */
428
NooneImportant83b75b02005-11-08 21:35:27 -0800429 dvb_frontend_sleep_until(&nexttime, 32000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 for (i=0; i<9; i++) {
Johannes Stezenbach591ad982005-05-16 21:54:31 -0700432 if (debug_legacy_dish_switch)
433 do_gettimeofday (&tv[i+1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if((cmd & 0x01) != last) {
Johannes Stezenbach591ad982005-05-16 21:54:31 -0700435 /* set voltage to (last ? 13V : 18V) */
436 stv0299_writeregI (state, 0x0c, reg0x0c | (last ? lv_mask : 0x50));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 last = (last) ? 0 : 1;
438 }
439
440 cmd = cmd >> 1;
441
442 if (i != 8)
NooneImportant83b75b02005-11-08 21:35:27 -0800443 dvb_frontend_sleep_until(&nexttime, 8000);
Johannes Stezenbach591ad982005-05-16 21:54:31 -0700444 }
445 if (debug_legacy_dish_switch) {
446 printk ("%s(%d): switch delay (should be 32k followed by all 8k\n",
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300447 __func__, fe->dvb->num);
NooneImportant83b75b02005-11-08 21:35:27 -0800448 for (i = 1; i < 10; i++)
449 printk ("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451
452 return 0;
453}
454
455static int stv0299_init (struct dvb_frontend* fe)
456{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800457 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 int i;
Oliver Endrisse84b1332008-04-20 21:57:34 -0300459 u8 reg;
460 u8 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 dprintk("stv0299: init chip\n");
463
Malcolm Priestley24fb0602011-03-26 21:57:33 -0300464 stv0299_writeregI(state, 0x02, 0x30 | state->mcr_reg);
465 msleep(50);
466
Oliver Endrisse84b1332008-04-20 21:57:34 -0300467 for (i = 0; ; i += 2) {
468 reg = state->config->inittab[i];
469 val = state->config->inittab[i+1];
470 if (reg == 0xff && val == 0xff)
471 break;
472 if (reg == 0x0c && state->config->op0_off)
473 val &= ~0x10;
Malcolm Priestley24fb0602011-03-26 21:57:33 -0300474 if (reg == 0x2)
475 state->mcr_reg = val & 0xf;
Oliver Endrisse84b1332008-04-20 21:57:34 -0300476 stv0299_writeregI(state, reg, val);
477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return 0;
480}
481
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300482static int stv0299_read_status(struct dvb_frontend *fe,
483 enum fe_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800485 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 u8 signal = 0xff - stv0299_readreg (state, 0x18);
488 u8 sync = stv0299_readreg (state, 0x1b);
489
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300490 dprintk ("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __func__, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 *status = 0;
492
493 if (signal > 10)
494 *status |= FE_HAS_SIGNAL;
495
496 if (sync & 0x80)
497 *status |= FE_HAS_CARRIER;
498
499 if (sync & 0x10)
500 *status |= FE_HAS_VITERBI;
501
502 if (sync & 0x08)
503 *status |= FE_HAS_SYNC;
504
505 if ((sync & 0x98) == 0x98)
506 *status |= FE_HAS_LOCK;
507
508 return 0;
509}
510
511static int stv0299_read_ber(struct dvb_frontend* fe, u32* ber)
512{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800513 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Oliver Endriss7876ad72008-06-19 23:25:04 -0300515 if (state->errmode != STATUS_BER)
516 return -ENOSYS;
517
518 *ber = stv0299_readreg(state, 0x1e) | (stv0299_readreg(state, 0x1d) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 return 0;
521}
522
523static int stv0299_read_signal_strength(struct dvb_frontend* fe, u16* strength)
524{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800525 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 s32 signal = 0xffff - ((stv0299_readreg (state, 0x18) << 8)
528 | stv0299_readreg (state, 0x19));
529
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300530 dprintk ("%s : FE_READ_SIGNAL_STRENGTH : AGC2I: 0x%02x%02x, signal=0x%04x\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 stv0299_readreg (state, 0x18),
532 stv0299_readreg (state, 0x19), (int) signal);
533
534 signal = signal * 5 / 4;
535 *strength = (signal > 0xffff) ? 0xffff : (signal < 0) ? 0 : signal;
536
537 return 0;
538}
539
540static int stv0299_read_snr(struct dvb_frontend* fe, u16* snr)
541{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800542 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 s32 xsnr = 0xffff - ((stv0299_readreg (state, 0x24) << 8)
545 | stv0299_readreg (state, 0x25));
546 xsnr = 3 * (xsnr - 0xa100);
547 *snr = (xsnr > 0xffff) ? 0xffff : (xsnr < 0) ? 0 : xsnr;
548
549 return 0;
550}
551
552static int stv0299_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
553{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800554 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Oliver Endriss7876ad72008-06-19 23:25:04 -0300556 if (state->errmode != STATUS_UCBLOCKS)
557 return -ENOSYS;
558
559 state->ucblocks += stv0299_readreg(state, 0x1e);
560 state->ucblocks += (stv0299_readreg(state, 0x1d) << 8);
561 *ucblocks = state->ucblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 return 0;
564}
565
Mauro Carvalho Chehab45f4a8e2011-12-26 14:29:52 -0300566static int stv0299_set_frontend(struct dvb_frontend *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Mauro Carvalho Chehab45f4a8e2011-12-26 14:29:52 -0300568 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800569 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 int invval = 0;
571
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300572 dprintk ("%s : FE_SET_FRONTEND\n", __func__);
Igor M. Liplianine4aab642008-09-23 15:43:57 -0300573 if (state->config->set_ts_params)
574 state->config->set_ts_params(fe, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 // set the inversion
577 if (p->inversion == INVERSION_OFF) invval = 0;
578 else if (p->inversion == INVERSION_ON) invval = 1;
579 else {
580 printk("stv0299 does not support auto-inversion\n");
581 return -EINVAL;
582 }
583 if (state->config->invert) invval = (~invval) & 1;
584 stv0299_writeregI(state, 0x0c, (stv0299_readreg(state, 0x0c) & 0xfe) | invval);
585
Patrick Boettcherdea74862006-05-14 05:01:31 -0300586 if (fe->ops.tuner_ops.set_params) {
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -0300587 fe->ops.tuner_ops.set_params(fe);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300588 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quincey53a8ee32006-04-18 17:47:10 -0300589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Mauro Carvalho Chehab45f4a8e2011-12-26 14:29:52 -0300591 stv0299_set_FEC(state, p->fec_inner);
592 stv0299_set_symbolrate(fe, p->symbol_rate);
Andrew de Quincy3528cc4e2005-11-08 21:35:28 -0800593 stv0299_writeregI(state, 0x22, 0x00);
594 stv0299_writeregI(state, 0x23, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 state->tuner_frequency = p->frequency;
Mauro Carvalho Chehab45f4a8e2011-12-26 14:29:52 -0300597 state->fec_inner = p->fec_inner;
598 state->symbol_rate = p->symbol_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 return 0;
601}
602
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300603static int stv0299_get_frontend(struct dvb_frontend *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300605 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800606 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 s32 derot_freq;
608 int invval;
609
610 derot_freq = (s32)(s16) ((stv0299_readreg (state, 0x22) << 8)
611 | stv0299_readreg (state, 0x23));
612
613 derot_freq *= (state->config->mclk >> 16);
614 derot_freq += 500;
615 derot_freq /= 1000;
616
617 p->frequency += derot_freq;
618
619 invval = stv0299_readreg (state, 0x0c) & 1;
620 if (state->config->invert) invval = (~invval) & 1;
621 p->inversion = invval ? INVERSION_ON : INVERSION_OFF;
622
Mauro Carvalho Chehab45f4a8e2011-12-26 14:29:52 -0300623 p->fec_inner = stv0299_get_fec(state);
624 p->symbol_rate = stv0299_get_symbolrate(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 return 0;
627}
628
629static int stv0299_sleep(struct dvb_frontend* fe)
630{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800631 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Malcolm Priestley24fb0602011-03-26 21:57:33 -0300633 stv0299_writeregI(state, 0x02, 0xb0 | state->mcr_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 state->initialised = 0;
635
636 return 0;
637}
638
Andrew de Quincey53a8ee32006-04-18 17:47:10 -0300639static int stv0299_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
640{
641 struct stv0299_state* state = fe->demodulator_priv;
642
643 if (enable) {
Andrew de Quinceya9686e02006-05-22 10:31:40 -0300644 stv0299_writeregI(state, 0x05, 0xb5);
Andrew de Quincey53a8ee32006-04-18 17:47:10 -0300645 } else {
Andrew de Quinceya9686e02006-05-22 10:31:40 -0300646 stv0299_writeregI(state, 0x05, 0x35);
Andrew de Quincey53a8ee32006-04-18 17:47:10 -0300647 }
Andrew de Quinceya9686e02006-05-22 10:31:40 -0300648 udelay(1);
649 return 0;
Andrew de Quincey53a8ee32006-04-18 17:47:10 -0300650}
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652static int stv0299_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
653{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800654 struct stv0299_state* state = fe->demodulator_priv;
Mauro Carvalho Chehab5581e132011-12-26 16:59:09 -0300655 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 fesettings->min_delay_ms = state->config->min_delay_ms;
Mauro Carvalho Chehab5581e132011-12-26 16:59:09 -0300658 if (p->symbol_rate < 10000000) {
659 fesettings->step_size = p->symbol_rate / 32000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 fesettings->max_drift = 5000;
661 } else {
Mauro Carvalho Chehab5581e132011-12-26 16:59:09 -0300662 fesettings->step_size = p->symbol_rate / 16000;
663 fesettings->max_drift = p->symbol_rate / 2000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665 return 0;
666}
667
668static void stv0299_release(struct dvb_frontend* fe)
669{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700670 struct stv0299_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 kfree(state);
672}
673
674static struct dvb_frontend_ops stv0299_ops;
675
676struct dvb_frontend* stv0299_attach(const struct stv0299_config* config,
677 struct i2c_adapter* i2c)
678{
679 struct stv0299_state* state = NULL;
680 int id;
681
682 /* allocate memory for the internal state */
Matthias Schwarzott084e24a2009-08-10 22:51:01 -0300683 state = kzalloc(sizeof(struct stv0299_state), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (state == NULL) goto error;
685
686 /* setup the state */
687 state->config = config;
688 state->i2c = i2c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 state->initialised = 0;
690 state->tuner_frequency = 0;
691 state->symbol_rate = 0;
692 state->fec_inner = 0;
Andrew de Quincey37650222005-11-08 21:35:11 -0800693 state->errmode = STATUS_BER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 /* check if the demod is there */
Malcolm Priestley24fb0602011-03-26 21:57:33 -0300696 stv0299_writeregI(state, 0x02, 0x30); /* standby off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 msleep(200);
698 id = stv0299_readreg(state, 0x00);
699
700 /* register 0x00 contains 0xa1 for STV0299 and STV0299B */
701 /* register 0x00 might contain 0x80 when returning from standby */
702 if (id != 0xa1 && id != 0x80) goto error;
703
704 /* create dvb_frontend */
Patrick Boettcherdea74862006-05-14 05:01:31 -0300705 memcpy(&state->frontend.ops, &stv0299_ops, sizeof(struct dvb_frontend_ops));
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800706 state->frontend.demodulator_priv = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return &state->frontend;
708
709error:
710 kfree(state);
711 return NULL;
712}
713
714static struct dvb_frontend_ops stv0299_ops = {
Mauro Carvalho Chehab45f4a8e2011-12-26 14:29:52 -0300715 .delsys = { SYS_DVBS },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 .info = {
717 .name = "ST STV0299 DVB-S",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 .frequency_min = 950000,
719 .frequency_max = 2150000,
720 .frequency_stepsize = 125, /* kHz for QPSK frontends */
721 .frequency_tolerance = 0,
722 .symbol_rate_min = 1000000,
723 .symbol_rate_max = 45000000,
724 .symbol_rate_tolerance = 500, /* ppm */
725 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
726 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
727 FE_CAN_QPSK |
728 FE_CAN_FEC_AUTO
729 },
730
731 .release = stv0299_release,
732
733 .init = stv0299_init,
734 .sleep = stv0299_sleep,
Andrew de Quinceyc10d14d2006-08-08 09:10:08 -0300735 .write = stv0299_write,
Andrew de Quincey53a8ee32006-04-18 17:47:10 -0300736 .i2c_gate_ctrl = stv0299_i2c_gate_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Mauro Carvalho Chehab45f4a8e2011-12-26 14:29:52 -0300738 .set_frontend = stv0299_set_frontend,
739 .get_frontend = stv0299_get_frontend,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 .get_tune_settings = stv0299_get_tune_settings,
741
742 .read_status = stv0299_read_status,
743 .read_ber = stv0299_read_ber,
744 .read_signal_strength = stv0299_read_signal_strength,
745 .read_snr = stv0299_read_snr,
746 .read_ucblocks = stv0299_read_ucblocks,
747
748 .diseqc_send_master_cmd = stv0299_send_diseqc_msg,
749 .diseqc_send_burst = stv0299_send_diseqc_burst,
750 .set_tone = stv0299_set_tone,
751 .set_voltage = stv0299_set_voltage,
752 .dishnetwork_send_legacy_command = stv0299_send_legacy_dish_cmd,
753};
754
Johannes Stezenbach591ad982005-05-16 21:54:31 -0700755module_param(debug_legacy_dish_switch, int, 0444);
756MODULE_PARM_DESC(debug_legacy_dish_switch, "Enable timing analysis for Dish Network legacy switches");
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758module_param(debug, int, 0644);
759MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
760
761MODULE_DESCRIPTION("ST STV0299 DVB Demodulator driver");
762MODULE_AUTHOR("Ralph Metzler, Holger Waechtler, Peter Schildmann, Felix Domke, "
Andrew de Quincey53a8ee32006-04-18 17:47:10 -0300763 "Andreas Oberritter, Andrew de Quincey, Kenneth Aafly");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764MODULE_LICENSE("GPL");
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766EXPORT_SYMBOL(stv0299_attach);