]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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> | |
1da177e4 LT |
48 | #include <linux/string.h> |
49 | #include <linux/slab.h> | |
4e57b681 | 50 | #include <linux/jiffies.h> |
1da177e4 LT |
51 | #include <asm/div64.h> |
52 | ||
53 | #include "dvb_frontend.h" | |
54 | #include "stv0299.h" | |
55 | ||
56 | struct stv0299_state { | |
57 | struct i2c_adapter* i2c; | |
1da177e4 LT |
58 | 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; | |
37650221 | 65 | int errmode; |
7876ad75 | 66 | u32 ucblocks; |
24fb0604 | 67 | u8 mcr_reg; |
1da177e4 LT |
68 | }; |
69 | ||
37650221 AQ |
70 | #define STATUS_BER 0 |
71 | #define STATUS_UCBLOCKS 1 | |
72 | ||
1da177e4 | 73 | static int debug; |
591ad98d | 74 | static int debug_legacy_dish_switch; |
1da177e4 LT |
75 | #define dprintk(args...) \ |
76 | do { \ | |
77 | if (debug) printk(KERN_DEBUG "stv0299: " args); \ | |
78 | } while (0) | |
79 | ||
80 | ||
81 | static 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, " | |
271ddbf7 | 91 | "ret == %i)\n", __func__, reg, data, ret); |
1da177e4 LT |
92 | |
93 | return (ret != 1) ? -EREMOTEIO : 0; | |
94 | } | |
95 | ||
2e4e98e7 | 96 | static int stv0299_write(struct dvb_frontend* fe, const u8 buf[], int len) |
1da177e4 | 97 | { |
9101e622 | 98 | struct stv0299_state* state = fe->demodulator_priv; |
1da177e4 | 99 | |
c10d14d6 AQ |
100 | if (len != 2) |
101 | return -EINVAL; | |
102 | ||
103 | return stv0299_writeregI(state, buf[0], buf[1]); | |
1da177e4 LT |
104 | } |
105 | ||
106 | static 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", | |
271ddbf7 | 118 | __func__, reg, ret); |
1da177e4 LT |
119 | |
120 | return b1[0]; | |
121 | } | |
122 | ||
123 | static 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 = ®1, .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) | |
271ddbf7 | 132 | dprintk("%s: readreg error (ret == %i)\n", __func__, ret); |
1da177e4 LT |
133 | |
134 | return ret == 2 ? 0 : ret; | |
135 | } | |
136 | ||
137 | static int stv0299_set_FEC (struct stv0299_state* state, fe_code_rate_t fec) | |
138 | { | |
271ddbf7 | 139 | dprintk ("%s\n", __func__); |
1da177e4 LT |
140 | |
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 | ||
173 | static fe_code_rate_t stv0299_get_fec (struct stv0299_state* state) | |
174 | { | |
175 | static fe_code_rate_t fec_tab [] = { FEC_2_3, FEC_3_4, FEC_5_6, | |
176 | FEC_7_8, FEC_1_2 }; | |
177 | u8 index; | |
178 | ||
271ddbf7 | 179 | dprintk ("%s\n", __func__); |
1da177e4 LT |
180 | |
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 | ||
190 | static int stv0299_wait_diseqc_fifo (struct stv0299_state* state, int timeout) | |
191 | { | |
192 | unsigned long start = jiffies; | |
193 | ||
271ddbf7 | 194 | dprintk ("%s\n", __func__); |
1da177e4 LT |
195 | |
196 | while (stv0299_readreg(state, 0x0a) & 1) { | |
197 | if (jiffies - start > timeout) { | |
271ddbf7 | 198 | dprintk ("%s: timeout!!\n", __func__); |
1da177e4 LT |
199 | return -ETIMEDOUT; |
200 | } | |
201 | msleep(10); | |
202 | }; | |
203 | ||
204 | return 0; | |
205 | } | |
206 | ||
207 | static int stv0299_wait_diseqc_idle (struct stv0299_state* state, int timeout) | |
208 | { | |
209 | unsigned long start = jiffies; | |
210 | ||
271ddbf7 | 211 | dprintk ("%s\n", __func__); |
1da177e4 LT |
212 | |
213 | while ((stv0299_readreg(state, 0x0a) & 3) != 2 ) { | |
214 | if (jiffies - start > timeout) { | |
271ddbf7 | 215 | dprintk ("%s: timeout!!\n", __func__); |
1da177e4 LT |
216 | return -ETIMEDOUT; |
217 | } | |
218 | msleep(10); | |
219 | }; | |
220 | ||
221 | return 0; | |
222 | } | |
223 | ||
224 | static int stv0299_set_symbolrate (struct dvb_frontend* fe, u32 srate) | |
225 | { | |
9101e622 | 226 | struct stv0299_state* state = fe->demodulator_priv; |
1da177e4 LT |
227 | 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 | ||
242 | static 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 | ||
271ddbf7 | 250 | dprintk ("%s\n", __func__); |
1da177e4 LT |
251 | |
252 | stv0299_readregs (state, 0x1f, sfr, 3); | |
0402a6c2 | 253 | stv0299_readregs (state, 0x1a, (u8 *)&rtf, 1); |
1da177e4 LT |
254 | |
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 | ||
271ddbf7 HH |
262 | dprintk ("%s : srate = %i\n", __func__, srate); |
263 | dprintk ("%s : ofset = %i\n", __func__, offset); | |
1da177e4 LT |
264 | |
265 | srate += offset; | |
266 | ||
267 | srate += 1000; | |
268 | srate /= 2000; | |
269 | srate *= 2000; | |
270 | ||
271 | return srate; | |
272 | } | |
273 | ||
274 | static int stv0299_send_diseqc_msg (struct dvb_frontend* fe, | |
275 | struct dvb_diseqc_master_cmd *m) | |
276 | { | |
9101e622 | 277 | struct stv0299_state* state = fe->demodulator_priv; |
1da177e4 LT |
278 | u8 val; |
279 | int i; | |
280 | ||
271ddbf7 | 281 | dprintk ("%s\n", __func__); |
1da177e4 LT |
282 | |
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 | ||
305 | static int stv0299_send_diseqc_burst (struct dvb_frontend* fe, fe_sec_mini_cmd_t burst) | |
306 | { | |
9101e622 | 307 | struct stv0299_state* state = fe->demodulator_priv; |
1da177e4 LT |
308 | u8 val; |
309 | ||
271ddbf7 | 310 | dprintk ("%s\n", __func__); |
1da177e4 LT |
311 | |
312 | if (stv0299_wait_diseqc_idle (state, 100) < 0) | |
313 | return -ETIMEDOUT; | |
314 | ||
315 | val = stv0299_readreg (state, 0x08); | |
316 | ||
317 | if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x2)) /* burst mode */ | |
318 | return -EREMOTEIO; | |
319 | ||
320 | if (stv0299_writeregI (state, 0x09, burst == SEC_MINI_A ? 0x00 : 0xff)) | |
321 | return -EREMOTEIO; | |
322 | ||
323 | if (stv0299_wait_diseqc_idle (state, 100) < 0) | |
324 | return -ETIMEDOUT; | |
325 | ||
326 | if (stv0299_writeregI (state, 0x08, val)) | |
327 | return -EREMOTEIO; | |
328 | ||
329 | return 0; | |
330 | } | |
331 | ||
332 | static int stv0299_set_tone (struct dvb_frontend* fe, fe_sec_tone_mode_t tone) | |
333 | { | |
9101e622 | 334 | struct stv0299_state* state = fe->demodulator_priv; |
1da177e4 LT |
335 | u8 val; |
336 | ||
337 | if (stv0299_wait_diseqc_idle (state, 100) < 0) | |
338 | return -ETIMEDOUT; | |
339 | ||
340 | val = stv0299_readreg (state, 0x08); | |
341 | ||
342 | switch (tone) { | |
343 | case SEC_TONE_ON: | |
344 | return stv0299_writeregI (state, 0x08, val | 0x3); | |
345 | ||
346 | case SEC_TONE_OFF: | |
347 | return stv0299_writeregI (state, 0x08, (val & ~0x3) | 0x02); | |
348 | ||
349 | default: | |
350 | return -EINVAL; | |
351 | } | |
352 | } | |
353 | ||
354 | static int stv0299_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t voltage) | |
355 | { | |
9101e622 | 356 | struct stv0299_state* state = fe->demodulator_priv; |
1da177e4 LT |
357 | u8 reg0x08; |
358 | u8 reg0x0c; | |
359 | ||
271ddbf7 | 360 | dprintk("%s: %s\n", __func__, |
1da177e4 LT |
361 | voltage == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" : |
362 | voltage == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??"); | |
363 | ||
364 | reg0x08 = stv0299_readreg (state, 0x08); | |
365 | reg0x0c = stv0299_readreg (state, 0x0c); | |
366 | ||
367 | /** | |
368 | * H/V switching over OP0, OP1 and OP2 are LNB power enable bits | |
369 | */ | |
370 | reg0x0c &= 0x0f; | |
e84b133e | 371 | reg0x08 = (reg0x08 & 0x3f) | (state->config->lock_output << 6); |
1da177e4 LT |
372 | |
373 | switch (voltage) { | |
374 | case SEC_VOLTAGE_13: | |
e84b133e OE |
375 | if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0) |
376 | reg0x0c |= 0x10; /* OP1 off, OP0 on */ | |
377 | else | |
378 | reg0x0c |= 0x40; /* OP1 on, OP0 off */ | |
379 | break; | |
1da177e4 | 380 | case SEC_VOLTAGE_18: |
e84b133e OE |
381 | reg0x0c |= 0x50; /* OP1 on, OP0 on */ |
382 | break; | |
383 | case SEC_VOLTAGE_OFF: | |
384 | /* LNB power off! */ | |
385 | reg0x08 = 0x00; | |
386 | reg0x0c = 0x00; | |
387 | break; | |
1da177e4 LT |
388 | default: |
389 | return -EINVAL; | |
390 | }; | |
e84b133e OE |
391 | |
392 | if (state->config->op0_off) | |
393 | reg0x0c &= ~0x10; | |
394 | ||
395 | stv0299_writeregI(state, 0x08, reg0x08); | |
396 | return stv0299_writeregI(state, 0x0c, reg0x0c); | |
1da177e4 LT |
397 | } |
398 | ||
400b7083 | 399 | static int stv0299_send_legacy_dish_cmd (struct dvb_frontend* fe, unsigned long cmd) |
591ad98d JS |
400 | { |
401 | struct stv0299_state* state = fe->demodulator_priv; | |
402 | u8 reg0x08; | |
403 | u8 reg0x0c; | |
404 | u8 lv_mask = 0x40; | |
1da177e4 LT |
405 | u8 last = 1; |
406 | int i; | |
591ad98d JS |
407 | struct timeval nexttime; |
408 | struct timeval tv[10]; | |
1da177e4 | 409 | |
591ad98d JS |
410 | reg0x08 = stv0299_readreg (state, 0x08); |
411 | reg0x0c = stv0299_readreg (state, 0x0c); | |
412 | reg0x0c &= 0x0f; | |
413 | stv0299_writeregI (state, 0x08, (reg0x08 & 0x3f) | (state->config->lock_output << 6)); | |
414 | if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0) | |
415 | lv_mask = 0x10; | |
1da177e4 LT |
416 | |
417 | cmd = cmd << 1; | |
591ad98d | 418 | if (debug_legacy_dish_switch) |
271ddbf7 | 419 | printk ("%s switch command: 0x%04lx\n",__func__, cmd); |
591ad98d JS |
420 | |
421 | do_gettimeofday (&nexttime); | |
422 | if (debug_legacy_dish_switch) | |
423 | memcpy (&tv[0], &nexttime, sizeof (struct timeval)); | |
424 | stv0299_writeregI (state, 0x0c, reg0x0c | 0x50); /* set LNB to 18V */ | |
1da177e4 | 425 | |
83b75b04 | 426 | dvb_frontend_sleep_until(&nexttime, 32000); |
1da177e4 LT |
427 | |
428 | for (i=0; i<9; i++) { | |
591ad98d JS |
429 | if (debug_legacy_dish_switch) |
430 | do_gettimeofday (&tv[i+1]); | |
1da177e4 | 431 | if((cmd & 0x01) != last) { |
591ad98d JS |
432 | /* set voltage to (last ? 13V : 18V) */ |
433 | stv0299_writeregI (state, 0x0c, reg0x0c | (last ? lv_mask : 0x50)); | |
1da177e4 LT |
434 | last = (last) ? 0 : 1; |
435 | } | |
436 | ||
437 | cmd = cmd >> 1; | |
438 | ||
439 | if (i != 8) | |
83b75b04 | 440 | dvb_frontend_sleep_until(&nexttime, 8000); |
591ad98d JS |
441 | } |
442 | if (debug_legacy_dish_switch) { | |
443 | printk ("%s(%d): switch delay (should be 32k followed by all 8k\n", | |
271ddbf7 | 444 | __func__, fe->dvb->num); |
83b75b04 N |
445 | for (i = 1; i < 10; i++) |
446 | printk ("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i])); | |
1da177e4 LT |
447 | } |
448 | ||
449 | return 0; | |
450 | } | |
451 | ||
452 | static int stv0299_init (struct dvb_frontend* fe) | |
453 | { | |
9101e622 | 454 | struct stv0299_state* state = fe->demodulator_priv; |
1da177e4 | 455 | int i; |
e84b133e OE |
456 | u8 reg; |
457 | u8 val; | |
1da177e4 LT |
458 | |
459 | dprintk("stv0299: init chip\n"); | |
460 | ||
24fb0604 MP |
461 | stv0299_writeregI(state, 0x02, 0x30 | state->mcr_reg); |
462 | msleep(50); | |
463 | ||
e84b133e OE |
464 | for (i = 0; ; i += 2) { |
465 | reg = state->config->inittab[i]; | |
466 | val = state->config->inittab[i+1]; | |
467 | if (reg == 0xff && val == 0xff) | |
468 | break; | |
469 | if (reg == 0x0c && state->config->op0_off) | |
470 | val &= ~0x10; | |
24fb0604 MP |
471 | if (reg == 0x2) |
472 | state->mcr_reg = val & 0xf; | |
e84b133e OE |
473 | stv0299_writeregI(state, reg, val); |
474 | } | |
1da177e4 | 475 | |
1da177e4 LT |
476 | return 0; |
477 | } | |
478 | ||
479 | static int stv0299_read_status(struct dvb_frontend* fe, fe_status_t* status) | |
480 | { | |
9101e622 | 481 | struct stv0299_state* state = fe->demodulator_priv; |
1da177e4 LT |
482 | |
483 | u8 signal = 0xff - stv0299_readreg (state, 0x18); | |
484 | u8 sync = stv0299_readreg (state, 0x1b); | |
485 | ||
271ddbf7 | 486 | dprintk ("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __func__, sync); |
1da177e4 LT |
487 | *status = 0; |
488 | ||
489 | if (signal > 10) | |
490 | *status |= FE_HAS_SIGNAL; | |
491 | ||
492 | if (sync & 0x80) | |
493 | *status |= FE_HAS_CARRIER; | |
494 | ||
495 | if (sync & 0x10) | |
496 | *status |= FE_HAS_VITERBI; | |
497 | ||
498 | if (sync & 0x08) | |
499 | *status |= FE_HAS_SYNC; | |
500 | ||
501 | if ((sync & 0x98) == 0x98) | |
502 | *status |= FE_HAS_LOCK; | |
503 | ||
504 | return 0; | |
505 | } | |
506 | ||
507 | static int stv0299_read_ber(struct dvb_frontend* fe, u32* ber) | |
508 | { | |
9101e622 | 509 | struct stv0299_state* state = fe->demodulator_priv; |
1da177e4 | 510 | |
7876ad75 OE |
511 | if (state->errmode != STATUS_BER) |
512 | return -ENOSYS; | |
513 | ||
514 | *ber = stv0299_readreg(state, 0x1e) | (stv0299_readreg(state, 0x1d) << 8); | |
1da177e4 LT |
515 | |
516 | return 0; | |
517 | } | |
518 | ||
519 | static int stv0299_read_signal_strength(struct dvb_frontend* fe, u16* strength) | |
520 | { | |
9101e622 | 521 | struct stv0299_state* state = fe->demodulator_priv; |
1da177e4 LT |
522 | |
523 | s32 signal = 0xffff - ((stv0299_readreg (state, 0x18) << 8) | |
524 | | stv0299_readreg (state, 0x19)); | |
525 | ||
271ddbf7 | 526 | dprintk ("%s : FE_READ_SIGNAL_STRENGTH : AGC2I: 0x%02x%02x, signal=0x%04x\n", __func__, |
1da177e4 LT |
527 | stv0299_readreg (state, 0x18), |
528 | stv0299_readreg (state, 0x19), (int) signal); | |
529 | ||
530 | signal = signal * 5 / 4; | |
531 | *strength = (signal > 0xffff) ? 0xffff : (signal < 0) ? 0 : signal; | |
532 | ||
533 | return 0; | |
534 | } | |
535 | ||
536 | static int stv0299_read_snr(struct dvb_frontend* fe, u16* snr) | |
537 | { | |
9101e622 | 538 | struct stv0299_state* state = fe->demodulator_priv; |
1da177e4 LT |
539 | |
540 | s32 xsnr = 0xffff - ((stv0299_readreg (state, 0x24) << 8) | |
541 | | stv0299_readreg (state, 0x25)); | |
542 | xsnr = 3 * (xsnr - 0xa100); | |
543 | *snr = (xsnr > 0xffff) ? 0xffff : (xsnr < 0) ? 0 : xsnr; | |
544 | ||
545 | return 0; | |
546 | } | |
547 | ||
548 | static int stv0299_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks) | |
549 | { | |
9101e622 | 550 | struct stv0299_state* state = fe->demodulator_priv; |
1da177e4 | 551 | |
7876ad75 OE |
552 | if (state->errmode != STATUS_UCBLOCKS) |
553 | return -ENOSYS; | |
554 | ||
555 | state->ucblocks += stv0299_readreg(state, 0x1e); | |
556 | state->ucblocks += (stv0299_readreg(state, 0x1d) << 8); | |
557 | *ucblocks = state->ucblocks; | |
1da177e4 LT |
558 | |
559 | return 0; | |
560 | } | |
561 | ||
562 | static int stv0299_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters * p) | |
563 | { | |
9101e622 | 564 | struct stv0299_state* state = fe->demodulator_priv; |
1da177e4 LT |
565 | int invval = 0; |
566 | ||
271ddbf7 | 567 | dprintk ("%s : FE_SET_FRONTEND\n", __func__); |
e4aab64c IL |
568 | if (state->config->set_ts_params) |
569 | state->config->set_ts_params(fe, 0); | |
1da177e4 LT |
570 | |
571 | // set the inversion | |
572 | if (p->inversion == INVERSION_OFF) invval = 0; | |
573 | else if (p->inversion == INVERSION_ON) invval = 1; | |
574 | else { | |
575 | printk("stv0299 does not support auto-inversion\n"); | |
576 | return -EINVAL; | |
577 | } | |
578 | if (state->config->invert) invval = (~invval) & 1; | |
579 | stv0299_writeregI(state, 0x0c, (stv0299_readreg(state, 0x0c) & 0xfe) | invval); | |
580 | ||
dea74869 PB |
581 | if (fe->ops.tuner_ops.set_params) { |
582 | fe->ops.tuner_ops.set_params(fe, p); | |
583 | if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0); | |
53a8ee3e | 584 | } |
3528cc4e AQ |
585 | |
586 | stv0299_set_FEC (state, p->u.qpsk.fec_inner); | |
587 | stv0299_set_symbolrate (fe, p->u.qpsk.symbol_rate); | |
588 | stv0299_writeregI(state, 0x22, 0x00); | |
589 | stv0299_writeregI(state, 0x23, 0x00); | |
1da177e4 LT |
590 | |
591 | state->tuner_frequency = p->frequency; | |
592 | state->fec_inner = p->u.qpsk.fec_inner; | |
593 | state->symbol_rate = p->u.qpsk.symbol_rate; | |
594 | ||
595 | return 0; | |
596 | } | |
597 | ||
598 | static int stv0299_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters * p) | |
599 | { | |
9101e622 | 600 | struct stv0299_state* state = fe->demodulator_priv; |
1da177e4 LT |
601 | s32 derot_freq; |
602 | int invval; | |
603 | ||
604 | derot_freq = (s32)(s16) ((stv0299_readreg (state, 0x22) << 8) | |
605 | | stv0299_readreg (state, 0x23)); | |
606 | ||
607 | derot_freq *= (state->config->mclk >> 16); | |
608 | derot_freq += 500; | |
609 | derot_freq /= 1000; | |
610 | ||
611 | p->frequency += derot_freq; | |
612 | ||
613 | invval = stv0299_readreg (state, 0x0c) & 1; | |
614 | if (state->config->invert) invval = (~invval) & 1; | |
615 | p->inversion = invval ? INVERSION_ON : INVERSION_OFF; | |
616 | ||
617 | p->u.qpsk.fec_inner = stv0299_get_fec (state); | |
618 | p->u.qpsk.symbol_rate = stv0299_get_symbolrate (state); | |
619 | ||
620 | return 0; | |
621 | } | |
622 | ||
623 | static int stv0299_sleep(struct dvb_frontend* fe) | |
624 | { | |
9101e622 | 625 | struct stv0299_state* state = fe->demodulator_priv; |
1da177e4 | 626 | |
24fb0604 | 627 | stv0299_writeregI(state, 0x02, 0xb0 | state->mcr_reg); |
1da177e4 LT |
628 | state->initialised = 0; |
629 | ||
630 | return 0; | |
631 | } | |
632 | ||
53a8ee3e AQ |
633 | static int stv0299_i2c_gate_ctrl(struct dvb_frontend* fe, int enable) |
634 | { | |
635 | struct stv0299_state* state = fe->demodulator_priv; | |
636 | ||
637 | if (enable) { | |
a9686e0d | 638 | stv0299_writeregI(state, 0x05, 0xb5); |
53a8ee3e | 639 | } else { |
a9686e0d | 640 | stv0299_writeregI(state, 0x05, 0x35); |
53a8ee3e | 641 | } |
a9686e0d AQ |
642 | udelay(1); |
643 | return 0; | |
53a8ee3e AQ |
644 | } |
645 | ||
1da177e4 LT |
646 | static int stv0299_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings) |
647 | { | |
9101e622 | 648 | struct stv0299_state* state = fe->demodulator_priv; |
1da177e4 LT |
649 | |
650 | fesettings->min_delay_ms = state->config->min_delay_ms; | |
651 | if (fesettings->parameters.u.qpsk.symbol_rate < 10000000) { | |
652 | fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 32000; | |
653 | fesettings->max_drift = 5000; | |
654 | } else { | |
655 | fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 16000; | |
656 | fesettings->max_drift = fesettings->parameters.u.qpsk.symbol_rate / 2000; | |
657 | } | |
658 | return 0; | |
659 | } | |
660 | ||
661 | static void stv0299_release(struct dvb_frontend* fe) | |
662 | { | |
b8742700 | 663 | struct stv0299_state* state = fe->demodulator_priv; |
1da177e4 LT |
664 | kfree(state); |
665 | } | |
666 | ||
667 | static struct dvb_frontend_ops stv0299_ops; | |
668 | ||
669 | struct dvb_frontend* stv0299_attach(const struct stv0299_config* config, | |
670 | struct i2c_adapter* i2c) | |
671 | { | |
672 | struct stv0299_state* state = NULL; | |
673 | int id; | |
674 | ||
675 | /* allocate memory for the internal state */ | |
084e24ac | 676 | state = kzalloc(sizeof(struct stv0299_state), GFP_KERNEL); |
1da177e4 LT |
677 | if (state == NULL) goto error; |
678 | ||
679 | /* setup the state */ | |
680 | state->config = config; | |
681 | state->i2c = i2c; | |
1da177e4 LT |
682 | state->initialised = 0; |
683 | state->tuner_frequency = 0; | |
684 | state->symbol_rate = 0; | |
685 | state->fec_inner = 0; | |
37650221 | 686 | state->errmode = STATUS_BER; |
1da177e4 LT |
687 | |
688 | /* check if the demod is there */ | |
24fb0604 | 689 | stv0299_writeregI(state, 0x02, 0x30); /* standby off */ |
1da177e4 LT |
690 | msleep(200); |
691 | id = stv0299_readreg(state, 0x00); | |
692 | ||
693 | /* register 0x00 contains 0xa1 for STV0299 and STV0299B */ | |
694 | /* register 0x00 might contain 0x80 when returning from standby */ | |
695 | if (id != 0xa1 && id != 0x80) goto error; | |
696 | ||
697 | /* create dvb_frontend */ | |
dea74869 | 698 | memcpy(&state->frontend.ops, &stv0299_ops, sizeof(struct dvb_frontend_ops)); |
9101e622 | 699 | state->frontend.demodulator_priv = state; |
1da177e4 LT |
700 | return &state->frontend; |
701 | ||
702 | error: | |
703 | kfree(state); | |
704 | return NULL; | |
705 | } | |
706 | ||
707 | static struct dvb_frontend_ops stv0299_ops = { | |
708 | ||
709 | .info = { | |
710 | .name = "ST STV0299 DVB-S", | |
711 | .type = FE_QPSK, | |
712 | .frequency_min = 950000, | |
713 | .frequency_max = 2150000, | |
714 | .frequency_stepsize = 125, /* kHz for QPSK frontends */ | |
715 | .frequency_tolerance = 0, | |
716 | .symbol_rate_min = 1000000, | |
717 | .symbol_rate_max = 45000000, | |
718 | .symbol_rate_tolerance = 500, /* ppm */ | |
719 | .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 | | |
720 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | | |
721 | FE_CAN_QPSK | | |
722 | FE_CAN_FEC_AUTO | |
723 | }, | |
724 | ||
725 | .release = stv0299_release, | |
726 | ||
727 | .init = stv0299_init, | |
728 | .sleep = stv0299_sleep, | |
c10d14d6 | 729 | .write = stv0299_write, |
53a8ee3e | 730 | .i2c_gate_ctrl = stv0299_i2c_gate_ctrl, |
1da177e4 LT |
731 | |
732 | .set_frontend = stv0299_set_frontend, | |
733 | .get_frontend = stv0299_get_frontend, | |
734 | .get_tune_settings = stv0299_get_tune_settings, | |
735 | ||
736 | .read_status = stv0299_read_status, | |
737 | .read_ber = stv0299_read_ber, | |
738 | .read_signal_strength = stv0299_read_signal_strength, | |
739 | .read_snr = stv0299_read_snr, | |
740 | .read_ucblocks = stv0299_read_ucblocks, | |
741 | ||
742 | .diseqc_send_master_cmd = stv0299_send_diseqc_msg, | |
743 | .diseqc_send_burst = stv0299_send_diseqc_burst, | |
744 | .set_tone = stv0299_set_tone, | |
745 | .set_voltage = stv0299_set_voltage, | |
746 | .dishnetwork_send_legacy_command = stv0299_send_legacy_dish_cmd, | |
747 | }; | |
748 | ||
591ad98d JS |
749 | module_param(debug_legacy_dish_switch, int, 0444); |
750 | MODULE_PARM_DESC(debug_legacy_dish_switch, "Enable timing analysis for Dish Network legacy switches"); | |
751 | ||
1da177e4 LT |
752 | module_param(debug, int, 0644); |
753 | MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off)."); | |
754 | ||
755 | MODULE_DESCRIPTION("ST STV0299 DVB Demodulator driver"); | |
756 | MODULE_AUTHOR("Ralph Metzler, Holger Waechtler, Peter Schildmann, Felix Domke, " | |
53a8ee3e | 757 | "Andreas Oberritter, Andrew de Quincey, Kenneth Aafly"); |
1da177e4 LT |
758 | MODULE_LICENSE("GPL"); |
759 | ||
1da177e4 | 760 | EXPORT_SYMBOL(stv0299_attach); |