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