blob: 882be672f5d2fc697f904c4bc80b7243312d1805 [file] [log] [blame]
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001/*
2 Montage Technology DS3000/TS2020 - DVBS/S2 Demodulator/Tuner driver
3 Copyright (C) 2009 Konstantin Dimitrov <kosio.dimitrov@gmail.com>
4
5 Copyright (C) 2009 TurboSight.com
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/slab.h>
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/init.h>
27#include <linux/firmware.h>
28
29#include "dvb_frontend.h"
30#include "ds3000.h"
31
32static int debug;
33
34#define dprintk(args...) \
35 do { \
36 if (debug) \
37 printk(args); \
38 } while (0)
39
40/* as of March 2009 current DS3000 firmware version is 1.78 */
41/* DS3000 FW v1.78 MD5: a32d17910c4f370073f9346e71d34b80 */
42#define DS3000_DEFAULT_FIRMWARE "dvb-fe-ds3000.fw"
43
44#define DS3000_SAMPLE_RATE 96000 /* in kHz */
45#define DS3000_XTAL_FREQ 27000 /* in kHz */
46
47/* Register values to initialise the demod in DVB-S mode */
48static u8 ds3000_dvbs_init_tab[] = {
49 0x23, 0x05,
50 0x08, 0x03,
51 0x0c, 0x00,
52 0x21, 0x54,
53 0x25, 0x82,
54 0x27, 0x31,
55 0x30, 0x08,
56 0x31, 0x40,
57 0x32, 0x32,
58 0x33, 0x35,
59 0x35, 0xff,
60 0x3a, 0x00,
61 0x37, 0x10,
62 0x38, 0x10,
63 0x39, 0x02,
64 0x42, 0x60,
65 0x4a, 0x40,
66 0x4b, 0x04,
67 0x4d, 0x91,
68 0x5d, 0xc8,
69 0x50, 0x77,
70 0x51, 0x77,
71 0x52, 0x36,
72 0x53, 0x36,
73 0x56, 0x01,
74 0x63, 0x43,
75 0x64, 0x30,
76 0x65, 0x40,
77 0x68, 0x26,
78 0x69, 0x4c,
79 0x70, 0x20,
80 0x71, 0x70,
81 0x72, 0x04,
82 0x73, 0x00,
83 0x70, 0x40,
84 0x71, 0x70,
85 0x72, 0x04,
86 0x73, 0x00,
87 0x70, 0x60,
88 0x71, 0x70,
89 0x72, 0x04,
90 0x73, 0x00,
91 0x70, 0x80,
92 0x71, 0x70,
93 0x72, 0x04,
94 0x73, 0x00,
95 0x70, 0xa0,
96 0x71, 0x70,
97 0x72, 0x04,
98 0x73, 0x00,
99 0x70, 0x1f,
100 0x76, 0x00,
101 0x77, 0xd1,
102 0x78, 0x0c,
103 0x79, 0x80,
104 0x7f, 0x04,
105 0x7c, 0x00,
106 0x80, 0x86,
107 0x81, 0xa6,
108 0x85, 0x04,
109 0xcd, 0xf4,
110 0x90, 0x33,
111 0xa0, 0x44,
112 0xc0, 0x18,
113 0xc3, 0x10,
114 0xc4, 0x08,
115 0xc5, 0x80,
116 0xc6, 0x80,
117 0xc7, 0x0a,
118 0xc8, 0x1a,
119 0xc9, 0x80,
120 0xfe, 0x92,
121 0xe0, 0xf8,
122 0xe6, 0x8b,
123 0xd0, 0x40,
124 0xf8, 0x20,
125 0xfa, 0x0f,
126 0xfd, 0x20,
127 0xad, 0x20,
128 0xae, 0x07,
129 0xb8, 0x00,
130};
131
132/* Register values to initialise the demod in DVB-S2 mode */
133static u8 ds3000_dvbs2_init_tab[] = {
134 0x23, 0x0f,
135 0x08, 0x07,
136 0x0c, 0x00,
137 0x21, 0x54,
138 0x25, 0x82,
139 0x27, 0x31,
140 0x30, 0x08,
141 0x31, 0x32,
142 0x32, 0x32,
143 0x33, 0x35,
144 0x35, 0xff,
145 0x3a, 0x00,
146 0x37, 0x10,
147 0x38, 0x10,
148 0x39, 0x02,
149 0x42, 0x60,
150 0x4a, 0x80,
151 0x4b, 0x04,
152 0x4d, 0x81,
153 0x5d, 0x88,
154 0x50, 0x36,
155 0x51, 0x36,
156 0x52, 0x36,
157 0x53, 0x36,
158 0x63, 0x60,
159 0x64, 0x10,
160 0x65, 0x10,
161 0x68, 0x04,
162 0x69, 0x29,
163 0x70, 0x20,
164 0x71, 0x70,
165 0x72, 0x04,
166 0x73, 0x00,
167 0x70, 0x40,
168 0x71, 0x70,
169 0x72, 0x04,
170 0x73, 0x00,
171 0x70, 0x60,
172 0x71, 0x70,
173 0x72, 0x04,
174 0x73, 0x00,
175 0x70, 0x80,
176 0x71, 0x70,
177 0x72, 0x04,
178 0x73, 0x00,
179 0x70, 0xa0,
180 0x71, 0x70,
181 0x72, 0x04,
182 0x73, 0x00,
183 0x70, 0x1f,
184 0xa0, 0x44,
185 0xc0, 0x08,
186 0xc1, 0x10,
187 0xc2, 0x08,
188 0xc3, 0x10,
189 0xc4, 0x08,
190 0xc5, 0xf0,
191 0xc6, 0xf0,
192 0xc7, 0x0a,
193 0xc8, 0x1a,
194 0xc9, 0x80,
195 0xca, 0x23,
196 0xcb, 0x24,
197 0xce, 0x74,
198 0x90, 0x03,
199 0x76, 0x80,
200 0x77, 0x42,
201 0x78, 0x0a,
202 0x79, 0x80,
203 0xad, 0x40,
204 0xae, 0x07,
205 0x7f, 0xd4,
206 0x7c, 0x00,
207 0x80, 0xa8,
208 0x81, 0xda,
209 0x7c, 0x01,
210 0x80, 0xda,
211 0x81, 0xec,
212 0x7c, 0x02,
213 0x80, 0xca,
214 0x81, 0xeb,
215 0x7c, 0x03,
216 0x80, 0xba,
217 0x81, 0xdb,
218 0x85, 0x08,
219 0x86, 0x00,
220 0x87, 0x02,
221 0x89, 0x80,
222 0x8b, 0x44,
223 0x8c, 0xaa,
224 0x8a, 0x10,
225 0xba, 0x00,
226 0xf5, 0x04,
227 0xfe, 0x44,
228 0xd2, 0x32,
229 0xb8, 0x00,
230};
231
232/* DS3000 doesn't need some parameters as input and auto-detects them */
233/* save input from the application of those parameters */
234struct ds3000_tuning {
235 u32 frequency;
236 u32 symbol_rate;
237 fe_spectral_inversion_t inversion;
238 enum fe_code_rate fec;
239
240 /* input values */
241 u8 inversion_val;
242 fe_modulation_t delivery;
243 u8 rolloff;
244};
245
246struct ds3000_state {
247 struct i2c_adapter *i2c;
248 const struct ds3000_config *config;
249
250 struct dvb_frontend frontend;
251
252 struct ds3000_tuning dcur;
253 struct ds3000_tuning dnxt;
254
255 u8 skip_fw_load;
256
257 /* previous uncorrected block counter for DVB-S2 */
258 u16 prevUCBS2;
259};
260
261static int ds3000_writereg(struct ds3000_state *state, int reg, int data)
262{
263 u8 buf[] = { reg, data };
264 struct i2c_msg msg = { .addr = state->config->demod_address,
265 .flags = 0, .buf = buf, .len = 2 };
266 int err;
267
268 dprintk("%s: write reg 0x%02x, value 0x%02x\n", __func__, reg, data);
269
270 err = i2c_transfer(state->i2c, &msg, 1);
271 if (err != 1) {
272 printk(KERN_ERR "%s: writereg error(err == %i, reg == 0x%02x,"
273 " value == 0x%02x)\n", __func__, err, reg, data);
274 return -EREMOTEIO;
275 }
276
277 return 0;
278}
279
280static int ds3000_tuner_writereg(struct ds3000_state *state, int reg, int data)
281{
282 u8 buf[] = { reg, data };
283 struct i2c_msg msg = { .addr = 0x60,
284 .flags = 0, .buf = buf, .len = 2 };
285 int err;
286
287 dprintk("%s: write reg 0x%02x, value 0x%02x\n", __func__, reg, data);
288
289 ds3000_writereg(state, 0x03, 0x11);
290 err = i2c_transfer(state->i2c, &msg, 1);
291 if (err != 1) {
292 printk("%s: writereg error(err == %i, reg == 0x%02x,"
293 " value == 0x%02x)\n", __func__, err, reg, data);
294 return -EREMOTEIO;
295 }
296
297 return 0;
298}
299
300/* I2C write for 8k firmware load */
301static int ds3000_writeFW(struct ds3000_state *state, int reg,
302 const u8 *data, u16 len)
303{
304 int i, ret = -EREMOTEIO;
305 struct i2c_msg msg;
306 u8 *buf;
307
Igor M. Liplianincaa687c2011-02-01 19:40:25 -0300308 buf = kmalloc(33, GFP_KERNEL);
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -0300309 if (buf == NULL) {
310 printk(KERN_ERR "Unable to kmalloc\n");
311 ret = -ENOMEM;
312 goto error;
313 }
314
315 *(buf) = reg;
316
317 msg.addr = state->config->demod_address;
318 msg.flags = 0;
319 msg.buf = buf;
Igor M. Liplianincaa687c2011-02-01 19:40:25 -0300320 msg.len = 33;
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -0300321
Igor M. Liplianincaa687c2011-02-01 19:40:25 -0300322 for (i = 0; i < len; i += 32) {
323 memcpy(buf + 1, data + i, 32);
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -0300324
325 dprintk("%s: write reg 0x%02x, len = %d\n", __func__, reg, len);
326
327 ret = i2c_transfer(state->i2c, &msg, 1);
328 if (ret != 1) {
329 printk(KERN_ERR "%s: write error(err == %i, "
330 "reg == 0x%02x\n", __func__, ret, reg);
331 ret = -EREMOTEIO;
332 }
333 }
334
335error:
336 kfree(buf);
337
338 return ret;
339}
340
341static int ds3000_readreg(struct ds3000_state *state, u8 reg)
342{
343 int ret;
344 u8 b0[] = { reg };
345 u8 b1[] = { 0 };
346 struct i2c_msg msg[] = {
347 {
348 .addr = state->config->demod_address,
349 .flags = 0,
350 .buf = b0,
351 .len = 1
352 }, {
353 .addr = state->config->demod_address,
354 .flags = I2C_M_RD,
355 .buf = b1,
356 .len = 1
357 }
358 };
359
360 ret = i2c_transfer(state->i2c, msg, 2);
361
362 if (ret != 2) {
363 printk(KERN_ERR "%s: reg=0x%x(error=%d)\n", __func__, reg, ret);
364 return ret;
365 }
366
367 dprintk("%s: read reg 0x%02x, value 0x%02x\n", __func__, reg, b1[0]);
368
369 return b1[0];
370}
371
372static int ds3000_tuner_readreg(struct ds3000_state *state, u8 reg)
373{
374 int ret;
375 u8 b0[] = { reg };
376 u8 b1[] = { 0 };
377 struct i2c_msg msg[] = {
378 {
379 .addr = 0x60,
380 .flags = 0,
381 .buf = b0,
382 .len = 1
383 }, {
384 .addr = 0x60,
385 .flags = I2C_M_RD,
386 .buf = b1,
387 .len = 1
388 }
389 };
390
391 ds3000_writereg(state, 0x03, 0x12);
392 ret = i2c_transfer(state->i2c, msg, 2);
393
394 if (ret != 2) {
395 printk(KERN_ERR "%s: reg=0x%x(error=%d)\n", __func__, reg, ret);
396 return ret;
397 }
398
399 dprintk("%s: read reg 0x%02x, value 0x%02x\n", __func__, reg, b1[0]);
400
401 return b1[0];
402}
403
404static int ds3000_set_inversion(struct ds3000_state *state,
405 fe_spectral_inversion_t inversion)
406{
407 dprintk("%s(%d)\n", __func__, inversion);
408
409 switch (inversion) {
410 case INVERSION_OFF:
411 case INVERSION_ON:
412 case INVERSION_AUTO:
413 break;
414 default:
415 return -EINVAL;
416 }
417
418 state->dnxt.inversion = inversion;
419
420 return 0;
421}
422
423static int ds3000_set_symbolrate(struct ds3000_state *state, u32 rate)
424{
425 int ret = 0;
426
427 dprintk("%s()\n", __func__);
428
429 dprintk("%s() symbol_rate = %d\n", __func__, state->dnxt.symbol_rate);
430
431 /* check if symbol rate is within limits */
432 if ((state->dnxt.symbol_rate >
433 state->frontend.ops.info.symbol_rate_max) ||
434 (state->dnxt.symbol_rate <
435 state->frontend.ops.info.symbol_rate_min))
436 ret = -EOPNOTSUPP;
437
438 state->dnxt.symbol_rate = rate;
439
440 return ret;
441}
442
443static int ds3000_load_firmware(struct dvb_frontend *fe,
444 const struct firmware *fw);
445
446static int ds3000_firmware_ondemand(struct dvb_frontend *fe)
447{
448 struct ds3000_state *state = fe->demodulator_priv;
449 const struct firmware *fw;
450 int ret = 0;
451
452 dprintk("%s()\n", __func__);
453
454 if (ds3000_readreg(state, 0xb2) <= 0)
455 return ret;
456
457 if (state->skip_fw_load)
458 return 0;
459 /* Load firmware */
460 /* request the firmware, this will block until someone uploads it */
461 printk(KERN_INFO "%s: Waiting for firmware upload (%s)...\n", __func__,
462 DS3000_DEFAULT_FIRMWARE);
463 ret = request_firmware(&fw, DS3000_DEFAULT_FIRMWARE,
464 state->i2c->dev.parent);
465 printk(KERN_INFO "%s: Waiting for firmware upload(2)...\n", __func__);
466 if (ret) {
467 printk(KERN_ERR "%s: No firmware uploaded (timeout or file not "
468 "found?)\n", __func__);
469 return ret;
470 }
471
472 /* Make sure we don't recurse back through here during loading */
473 state->skip_fw_load = 1;
474
475 ret = ds3000_load_firmware(fe, fw);
476 if (ret)
477 printk("%s: Writing firmware to device failed\n", __func__);
478
479 release_firmware(fw);
480
481 dprintk("%s: Firmware upload %s\n", __func__,
482 ret == 0 ? "complete" : "failed");
483
484 /* Ensure firmware is always loaded if required */
485 state->skip_fw_load = 0;
486
487 return ret;
488}
489
490static int ds3000_load_firmware(struct dvb_frontend *fe,
491 const struct firmware *fw)
492{
493 struct ds3000_state *state = fe->demodulator_priv;
494
495 dprintk("%s\n", __func__);
496 dprintk("Firmware is %zu bytes (%02x %02x .. %02x %02x)\n",
497 fw->size,
498 fw->data[0],
499 fw->data[1],
500 fw->data[fw->size - 2],
501 fw->data[fw->size - 1]);
502
503 /* Begin the firmware load process */
504 ds3000_writereg(state, 0xb2, 0x01);
505 /* write the entire firmware */
506 ds3000_writeFW(state, 0xb0, fw->data, fw->size);
507 ds3000_writereg(state, 0xb2, 0x00);
508
509 return 0;
510}
511
Igor M. Liplianind2ffc442011-02-25 18:41:22 -0300512static int ds3000_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
513{
514 struct ds3000_state *state = fe->demodulator_priv;
515 u8 data;
516
517 dprintk("%s(%d)\n", __func__, voltage);
518
519 data = ds3000_readreg(state, 0xa2);
520 data |= 0x03; /* bit0 V/H, bit1 off/on */
521
522 switch (voltage) {
523 case SEC_VOLTAGE_18:
524 data &= ~0x03;
525 break;
526 case SEC_VOLTAGE_13:
527 data &= ~0x03;
528 data |= 0x01;
529 break;
530 case SEC_VOLTAGE_OFF:
531 break;
532 }
533
534 ds3000_writereg(state, 0xa2, data);
535
536 return 0;
537}
538
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -0300539static int ds3000_read_status(struct dvb_frontend *fe, fe_status_t* status)
540{
541 struct ds3000_state *state = fe->demodulator_priv;
542 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
543 int lock;
544
545 *status = 0;
546
547 switch (c->delivery_system) {
548 case SYS_DVBS:
549 lock = ds3000_readreg(state, 0xd1);
550 if ((lock & 0x07) == 0x07)
551 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
552 FE_HAS_VITERBI | FE_HAS_SYNC |
553 FE_HAS_LOCK;
554
555 break;
556 case SYS_DVBS2:
557 lock = ds3000_readreg(state, 0x0d);
558 if ((lock & 0x8f) == 0x8f)
559 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
560 FE_HAS_VITERBI | FE_HAS_SYNC |
561 FE_HAS_LOCK;
562
563 break;
564 default:
565 return 1;
566 }
567
568 dprintk("%s: status = 0x%02x\n", __func__, lock);
569
570 return 0;
571}
572
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -0300573/* read DS3000 BER value */
574static int ds3000_read_ber(struct dvb_frontend *fe, u32* ber)
575{
576 struct ds3000_state *state = fe->demodulator_priv;
577 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
578 u8 data;
579 u32 ber_reading, lpdc_frames;
580
581 dprintk("%s()\n", __func__);
582
583 switch (c->delivery_system) {
584 case SYS_DVBS:
585 /* set the number of bytes checked during
586 BER estimation */
587 ds3000_writereg(state, 0xf9, 0x04);
588 /* read BER estimation status */
589 data = ds3000_readreg(state, 0xf8);
590 /* check if BER estimation is ready */
591 if ((data & 0x10) == 0) {
592 /* this is the number of error bits,
593 to calculate the bit error rate
594 divide to 8388608 */
595 *ber = (ds3000_readreg(state, 0xf7) << 8) |
596 ds3000_readreg(state, 0xf6);
597 /* start counting error bits */
598 /* need to be set twice
599 otherwise it fails sometimes */
600 data |= 0x10;
601 ds3000_writereg(state, 0xf8, data);
602 ds3000_writereg(state, 0xf8, data);
603 } else
604 /* used to indicate that BER estimation
605 is not ready, i.e. BER is unknown */
606 *ber = 0xffffffff;
607 break;
608 case SYS_DVBS2:
609 /* read the number of LPDC decoded frames */
610 lpdc_frames = (ds3000_readreg(state, 0xd7) << 16) |
611 (ds3000_readreg(state, 0xd6) << 8) |
612 ds3000_readreg(state, 0xd5);
613 /* read the number of packets with bad CRC */
614 ber_reading = (ds3000_readreg(state, 0xf8) << 8) |
615 ds3000_readreg(state, 0xf7);
616 if (lpdc_frames > 750) {
617 /* clear LPDC frame counters */
618 ds3000_writereg(state, 0xd1, 0x01);
619 /* clear bad packets counter */
620 ds3000_writereg(state, 0xf9, 0x01);
621 /* enable bad packets counter */
622 ds3000_writereg(state, 0xf9, 0x00);
623 /* enable LPDC frame counters */
624 ds3000_writereg(state, 0xd1, 0x00);
625 *ber = ber_reading;
626 } else
627 /* used to indicate that BER estimation is not ready,
628 i.e. BER is unknown */
629 *ber = 0xffffffff;
630 break;
631 default:
632 return 1;
633 }
634
635 return 0;
636}
637
638/* read TS2020 signal strength */
639static int ds3000_read_signal_strength(struct dvb_frontend *fe,
640 u16 *signal_strength)
641{
642 struct ds3000_state *state = fe->demodulator_priv;
643 u16 sig_reading, sig_strength;
644 u8 rfgain, bbgain;
645
646 dprintk("%s()\n", __func__);
647
648 rfgain = ds3000_tuner_readreg(state, 0x3d) & 0x1f;
649 bbgain = ds3000_tuner_readreg(state, 0x21) & 0x1f;
650
651 if (rfgain > 15)
652 rfgain = 15;
653 if (bbgain > 13)
654 bbgain = 13;
655
656 sig_reading = rfgain * 2 + bbgain * 3;
657
658 sig_strength = 40 + (64 - sig_reading) * 50 / 64 ;
659
660 /* cook the value to be suitable for szap-s2 human readable output */
661 *signal_strength = sig_strength * 1000;
662
663 dprintk("%s: raw / cooked = 0x%04x / 0x%04x\n", __func__,
664 sig_reading, *signal_strength);
665
666 return 0;
667}
668
669/* calculate DS3000 snr value in dB */
670static int ds3000_read_snr(struct dvb_frontend *fe, u16 *snr)
671{
672 struct ds3000_state *state = fe->demodulator_priv;
673 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
674 u8 snr_reading, snr_value;
675 u32 dvbs2_signal_reading, dvbs2_noise_reading, tmp;
676 static const u16 dvbs_snr_tab[] = { /* 20 x Table (rounded up) */
677 0x0000, 0x1b13, 0x2aea, 0x3627, 0x3ede, 0x45fe, 0x4c03,
678 0x513a, 0x55d4, 0x59f2, 0x5dab, 0x6111, 0x6431, 0x6717,
679 0x69c9, 0x6c4e, 0x6eac, 0x70e8, 0x7304, 0x7505
680 };
681 static const u16 dvbs2_snr_tab[] = { /* 80 x Table (rounded up) */
682 0x0000, 0x0bc2, 0x12a3, 0x1785, 0x1b4e, 0x1e65, 0x2103,
683 0x2347, 0x2546, 0x2710, 0x28ae, 0x2a28, 0x2b83, 0x2cc5,
684 0x2df1, 0x2f09, 0x3010, 0x3109, 0x31f4, 0x32d2, 0x33a6,
685 0x3470, 0x3531, 0x35ea, 0x369b, 0x3746, 0x37ea, 0x3888,
686 0x3920, 0x39b3, 0x3a42, 0x3acc, 0x3b51, 0x3bd3, 0x3c51,
687 0x3ccb, 0x3d42, 0x3db6, 0x3e27, 0x3e95, 0x3f00, 0x3f68,
688 0x3fcf, 0x4033, 0x4094, 0x40f4, 0x4151, 0x41ac, 0x4206,
689 0x425e, 0x42b4, 0x4308, 0x435b, 0x43ac, 0x43fc, 0x444a,
690 0x4497, 0x44e2, 0x452d, 0x4576, 0x45bd, 0x4604, 0x4649,
691 0x468e, 0x46d1, 0x4713, 0x4755, 0x4795, 0x47d4, 0x4813,
692 0x4851, 0x488d, 0x48c9, 0x4904, 0x493f, 0x4978, 0x49b1,
693 0x49e9, 0x4a20, 0x4a57
694 };
695
696 dprintk("%s()\n", __func__);
697
698 switch (c->delivery_system) {
699 case SYS_DVBS:
700 snr_reading = ds3000_readreg(state, 0xff);
701 snr_reading /= 8;
702 if (snr_reading == 0)
703 *snr = 0x0000;
704 else {
705 if (snr_reading > 20)
706 snr_reading = 20;
707 snr_value = dvbs_snr_tab[snr_reading - 1] * 10 / 23026;
708 /* cook the value to be suitable for szap-s2
709 human readable output */
710 *snr = snr_value * 8 * 655;
711 }
712 dprintk("%s: raw / cooked = 0x%02x / 0x%04x\n", __func__,
713 snr_reading, *snr);
714 break;
715 case SYS_DVBS2:
716 dvbs2_noise_reading = (ds3000_readreg(state, 0x8c) & 0x3f) +
717 (ds3000_readreg(state, 0x8d) << 4);
718 dvbs2_signal_reading = ds3000_readreg(state, 0x8e);
719 tmp = dvbs2_signal_reading * dvbs2_signal_reading >> 1;
Nicolas Noirbent450df222010-03-22 14:54:43 -0300720 if (tmp == 0) {
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -0300721 *snr = 0x0000;
722 return 0;
723 }
724 if (dvbs2_noise_reading == 0) {
725 snr_value = 0x0013;
726 /* cook the value to be suitable for szap-s2
727 human readable output */
728 *snr = 0xffff;
729 return 0;
730 }
731 if (tmp > dvbs2_noise_reading) {
732 snr_reading = tmp / dvbs2_noise_reading;
733 if (snr_reading > 80)
734 snr_reading = 80;
735 snr_value = dvbs2_snr_tab[snr_reading - 1] / 1000;
736 /* cook the value to be suitable for szap-s2
737 human readable output */
738 *snr = snr_value * 5 * 655;
739 } else {
740 snr_reading = dvbs2_noise_reading / tmp;
741 if (snr_reading > 80)
742 snr_reading = 80;
743 *snr = -(dvbs2_snr_tab[snr_reading] / 1000);
744 }
745 dprintk("%s: raw / cooked = 0x%02x / 0x%04x\n", __func__,
746 snr_reading, *snr);
747 break;
748 default:
749 return 1;
750 }
751
752 return 0;
753}
754
755/* read DS3000 uncorrected blocks */
756static int ds3000_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
757{
758 struct ds3000_state *state = fe->demodulator_priv;
759 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
760 u8 data;
761 u16 _ucblocks;
762
763 dprintk("%s()\n", __func__);
764
765 switch (c->delivery_system) {
766 case SYS_DVBS:
767 *ucblocks = (ds3000_readreg(state, 0xf5) << 8) |
768 ds3000_readreg(state, 0xf4);
769 data = ds3000_readreg(state, 0xf8);
770 /* clear packet counters */
771 data &= ~0x20;
772 ds3000_writereg(state, 0xf8, data);
773 /* enable packet counters */
774 data |= 0x20;
775 ds3000_writereg(state, 0xf8, data);
776 break;
777 case SYS_DVBS2:
778 _ucblocks = (ds3000_readreg(state, 0xe2) << 8) |
779 ds3000_readreg(state, 0xe1);
780 if (_ucblocks > state->prevUCBS2)
781 *ucblocks = _ucblocks - state->prevUCBS2;
782 else
783 *ucblocks = state->prevUCBS2 - _ucblocks;
784 state->prevUCBS2 = _ucblocks;
785 break;
786 default:
787 return 1;
788 }
789
790 return 0;
791}
792
793/* Overwrite the current tuning params, we are about to tune */
794static void ds3000_clone_params(struct dvb_frontend *fe)
795{
796 struct ds3000_state *state = fe->demodulator_priv;
797 memcpy(&state->dcur, &state->dnxt, sizeof(state->dcur));
798}
799
800static int ds3000_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
801{
802 struct ds3000_state *state = fe->demodulator_priv;
803 u8 data;
804
805 dprintk("%s(%d)\n", __func__, tone);
806 if ((tone != SEC_TONE_ON) && (tone != SEC_TONE_OFF)) {
807 printk(KERN_ERR "%s: Invalid, tone=%d\n", __func__, tone);
808 return -EINVAL;
809 }
810
811 data = ds3000_readreg(state, 0xa2);
812 data &= ~0xc0;
813 ds3000_writereg(state, 0xa2, data);
814
815 switch (tone) {
816 case SEC_TONE_ON:
817 dprintk("%s: setting tone on\n", __func__);
818 data = ds3000_readreg(state, 0xa1);
819 data &= ~0x43;
820 data |= 0x04;
821 ds3000_writereg(state, 0xa1, data);
822 break;
823 case SEC_TONE_OFF:
824 dprintk("%s: setting tone off\n", __func__);
825 data = ds3000_readreg(state, 0xa2);
826 data |= 0x80;
827 ds3000_writereg(state, 0xa2, data);
828 break;
829 }
830
831 return 0;
832}
833
834static int ds3000_send_diseqc_msg(struct dvb_frontend *fe,
835 struct dvb_diseqc_master_cmd *d)
836{
837 struct ds3000_state *state = fe->demodulator_priv;
838 int i;
839 u8 data;
840
841 /* Dump DiSEqC message */
842 dprintk("%s(", __func__);
843 for (i = 0 ; i < d->msg_len;) {
844 dprintk("0x%02x", d->msg[i]);
845 if (++i < d->msg_len)
846 dprintk(", ");
847 }
848
849 /* enable DiSEqC message send pin */
850 data = ds3000_readreg(state, 0xa2);
851 data &= ~0xc0;
852 ds3000_writereg(state, 0xa2, data);
853
854 /* DiSEqC message */
855 for (i = 0; i < d->msg_len; i++)
856 ds3000_writereg(state, 0xa3 + i, d->msg[i]);
857
858 data = ds3000_readreg(state, 0xa1);
859 /* clear DiSEqC message length and status,
860 enable DiSEqC message send */
861 data &= ~0xf8;
862 /* set DiSEqC mode, modulation active during 33 pulses,
863 set DiSEqC message length */
864 data |= ((d->msg_len - 1) << 3) | 0x07;
865 ds3000_writereg(state, 0xa1, data);
866
867 /* wait up to 150ms for DiSEqC transmission to complete */
868 for (i = 0; i < 15; i++) {
869 data = ds3000_readreg(state, 0xa1);
870 if ((data & 0x40) == 0)
871 break;
872 msleep(10);
873 }
874
875 /* DiSEqC timeout after 150ms */
876 if (i == 15) {
877 data = ds3000_readreg(state, 0xa1);
878 data &= ~0x80;
879 data |= 0x40;
880 ds3000_writereg(state, 0xa1, data);
881
882 data = ds3000_readreg(state, 0xa2);
883 data &= ~0xc0;
884 data |= 0x80;
885 ds3000_writereg(state, 0xa2, data);
886
887 return 1;
888 }
889
890 data = ds3000_readreg(state, 0xa2);
891 data &= ~0xc0;
892 data |= 0x80;
893 ds3000_writereg(state, 0xa2, data);
894
895 return 0;
896}
897
898/* Send DiSEqC burst */
899static int ds3000_diseqc_send_burst(struct dvb_frontend *fe,
900 fe_sec_mini_cmd_t burst)
901{
902 struct ds3000_state *state = fe->demodulator_priv;
903 int i;
904 u8 data;
905
906 dprintk("%s()\n", __func__);
907
908 data = ds3000_readreg(state, 0xa2);
909 data &= ~0xc0;
910 ds3000_writereg(state, 0xa2, data);
911
912 /* DiSEqC burst */
913 if (burst == SEC_MINI_A)
914 /* Unmodulated tone burst */
915 ds3000_writereg(state, 0xa1, 0x02);
916 else if (burst == SEC_MINI_B)
917 /* Modulated tone burst */
918 ds3000_writereg(state, 0xa1, 0x01);
919 else
920 return -EINVAL;
921
922 msleep(13);
923 for (i = 0; i < 5; i++) {
924 data = ds3000_readreg(state, 0xa1);
925 if ((data & 0x40) == 0)
926 break;
927 msleep(1);
928 }
929
930 if (i == 5) {
931 data = ds3000_readreg(state, 0xa1);
932 data &= ~0x80;
933 data |= 0x40;
934 ds3000_writereg(state, 0xa1, data);
935
936 data = ds3000_readreg(state, 0xa2);
937 data &= ~0xc0;
938 data |= 0x80;
939 ds3000_writereg(state, 0xa2, data);
940
941 return 1;
942 }
943
944 data = ds3000_readreg(state, 0xa2);
945 data &= ~0xc0;
946 data |= 0x80;
947 ds3000_writereg(state, 0xa2, data);
948
949 return 0;
950}
951
952static void ds3000_release(struct dvb_frontend *fe)
953{
954 struct ds3000_state *state = fe->demodulator_priv;
955 dprintk("%s\n", __func__);
956 kfree(state);
957}
958
959static struct dvb_frontend_ops ds3000_ops;
960
961struct dvb_frontend *ds3000_attach(const struct ds3000_config *config,
962 struct i2c_adapter *i2c)
963{
964 struct ds3000_state *state = NULL;
965 int ret;
966
967 dprintk("%s\n", __func__);
968
969 /* allocate memory for the internal state */
Julia Lawall2ef17c92010-05-13 16:59:15 -0300970 state = kzalloc(sizeof(struct ds3000_state), GFP_KERNEL);
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -0300971 if (state == NULL) {
972 printk(KERN_ERR "Unable to kmalloc\n");
973 goto error2;
974 }
975
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -0300976 state->config = config;
977 state->i2c = i2c;
978 state->prevUCBS2 = 0;
979
980 /* check if the demod is present */
981 ret = ds3000_readreg(state, 0x00) & 0xfe;
982 if (ret != 0xe0) {
983 printk(KERN_ERR "Invalid probe, probably not a DS3000\n");
984 goto error3;
985 }
986
987 printk(KERN_INFO "DS3000 chip version: %d.%d attached.\n",
988 ds3000_readreg(state, 0x02),
989 ds3000_readreg(state, 0x01));
990
991 memcpy(&state->frontend.ops, &ds3000_ops,
992 sizeof(struct dvb_frontend_ops));
993 state->frontend.demodulator_priv = state;
994 return &state->frontend;
995
996error3:
997 kfree(state);
998error2:
999 return NULL;
1000}
1001EXPORT_SYMBOL(ds3000_attach);
1002
1003static int ds3000_set_property(struct dvb_frontend *fe,
1004 struct dtv_property *tvp)
1005{
1006 dprintk("%s(..)\n", __func__);
1007 return 0;
1008}
1009
1010static int ds3000_get_property(struct dvb_frontend *fe,
1011 struct dtv_property *tvp)
1012{
1013 dprintk("%s(..)\n", __func__);
1014 return 0;
1015}
1016
1017static int ds3000_tune(struct dvb_frontend *fe,
1018 struct dvb_frontend_parameters *p)
1019{
1020 struct ds3000_state *state = fe->demodulator_priv;
1021 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1022
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001023 int i;
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001024 u8 status, mlpf, mlpf_new, mlpf_max, mlpf_min, nlpf;
1025 u16 value, ndiv;
1026 u32 f3db;
1027
1028 dprintk("%s() ", __func__);
1029
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001030 state->dnxt.delivery = c->modulation;
1031 state->dnxt.frequency = c->frequency;
1032 state->dnxt.rolloff = 2; /* fixme */
1033 state->dnxt.fec = c->fec_inner;
1034
1035 ret = ds3000_set_inversion(state, p->inversion);
1036 if (ret != 0)
1037 return ret;
1038
1039 ret = ds3000_set_symbolrate(state, c->symbol_rate);
1040 if (ret != 0)
1041 return ret;
1042
1043 /* discard the 'current' tuning parameters and prepare to tune */
1044 ds3000_clone_params(fe);
1045
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001046 /* Reset status register */
1047 status = 0;
1048 /* Tune */
1049 /* unknown */
1050 ds3000_tuner_writereg(state, 0x07, 0x02);
1051 ds3000_tuner_writereg(state, 0x10, 0x00);
1052 ds3000_tuner_writereg(state, 0x60, 0x79);
1053 ds3000_tuner_writereg(state, 0x08, 0x01);
1054 ds3000_tuner_writereg(state, 0x00, 0x01);
1055 /* calculate and set freq divider */
1056 if (state->dcur.frequency < 1146000) {
1057 ds3000_tuner_writereg(state, 0x10, 0x11);
1058 ndiv = ((state->dcur.frequency * (6 + 8) * 4) +
1059 (DS3000_XTAL_FREQ / 2)) /
1060 DS3000_XTAL_FREQ - 1024;
1061 } else {
1062 ds3000_tuner_writereg(state, 0x10, 0x01);
1063 ndiv = ((state->dcur.frequency * (6 + 8) * 2) +
1064 (DS3000_XTAL_FREQ / 2)) /
1065 DS3000_XTAL_FREQ - 1024;
1066 }
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001067
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001068 ds3000_tuner_writereg(state, 0x01, (ndiv & 0x0f00) >> 8);
1069 ds3000_tuner_writereg(state, 0x02, ndiv & 0x00ff);
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001070
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001071 /* set pll */
1072 ds3000_tuner_writereg(state, 0x03, 0x06);
1073 ds3000_tuner_writereg(state, 0x51, 0x0f);
1074 ds3000_tuner_writereg(state, 0x51, 0x1f);
1075 ds3000_tuner_writereg(state, 0x50, 0x10);
1076 ds3000_tuner_writereg(state, 0x50, 0x00);
1077 msleep(5);
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001078
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001079 /* unknown */
1080 ds3000_tuner_writereg(state, 0x51, 0x17);
1081 ds3000_tuner_writereg(state, 0x51, 0x1f);
1082 ds3000_tuner_writereg(state, 0x50, 0x08);
1083 ds3000_tuner_writereg(state, 0x50, 0x00);
1084 msleep(5);
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001085
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001086 value = ds3000_tuner_readreg(state, 0x3d);
1087 value &= 0x0f;
1088 if ((value > 4) && (value < 15)) {
1089 value -= 3;
1090 if (value < 4)
1091 value = 4;
1092 value = ((value << 3) | 0x01) & 0x79;
1093 }
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001094
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001095 ds3000_tuner_writereg(state, 0x60, value);
1096 ds3000_tuner_writereg(state, 0x51, 0x17);
1097 ds3000_tuner_writereg(state, 0x51, 0x1f);
1098 ds3000_tuner_writereg(state, 0x50, 0x08);
1099 ds3000_tuner_writereg(state, 0x50, 0x00);
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001100
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001101 /* set low-pass filter period */
1102 ds3000_tuner_writereg(state, 0x04, 0x2e);
1103 ds3000_tuner_writereg(state, 0x51, 0x1b);
1104 ds3000_tuner_writereg(state, 0x51, 0x1f);
1105 ds3000_tuner_writereg(state, 0x50, 0x04);
1106 ds3000_tuner_writereg(state, 0x50, 0x00);
1107 msleep(5);
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001108
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001109 f3db = ((state->dcur.symbol_rate / 1000) << 2) / 5 + 2000;
1110 if ((state->dcur.symbol_rate / 1000) < 5000)
1111 f3db += 3000;
1112 if (f3db < 7000)
1113 f3db = 7000;
1114 if (f3db > 40000)
1115 f3db = 40000;
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001116
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001117 /* set low-pass filter baseband */
1118 value = ds3000_tuner_readreg(state, 0x26);
1119 mlpf = 0x2e * 207 / ((value << 1) + 151);
1120 mlpf_max = mlpf * 135 / 100;
1121 mlpf_min = mlpf * 78 / 100;
1122 if (mlpf_max > 63)
1123 mlpf_max = 63;
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001124
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001125 /* rounded to the closest integer */
1126 nlpf = ((mlpf * f3db * 1000) + (2766 * DS3000_XTAL_FREQ / 2))
1127 / (2766 * DS3000_XTAL_FREQ);
1128 if (nlpf > 23)
1129 nlpf = 23;
1130 if (nlpf < 1)
1131 nlpf = 1;
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001132
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001133 /* rounded to the closest integer */
1134 mlpf_new = ((DS3000_XTAL_FREQ * nlpf * 2766) +
1135 (1000 * f3db / 2)) / (1000 * f3db);
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001136
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001137 if (mlpf_new < mlpf_min) {
1138 nlpf++;
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001139 mlpf_new = ((DS3000_XTAL_FREQ * nlpf * 2766) +
1140 (1000 * f3db / 2)) / (1000 * f3db);
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001141 }
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001142
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001143 if (mlpf_new > mlpf_max)
1144 mlpf_new = mlpf_max;
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001145
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001146 ds3000_tuner_writereg(state, 0x04, mlpf_new);
1147 ds3000_tuner_writereg(state, 0x06, nlpf);
1148 ds3000_tuner_writereg(state, 0x51, 0x1b);
1149 ds3000_tuner_writereg(state, 0x51, 0x1f);
1150 ds3000_tuner_writereg(state, 0x50, 0x04);
1151 ds3000_tuner_writereg(state, 0x50, 0x00);
1152 msleep(5);
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001153
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001154 /* unknown */
1155 ds3000_tuner_writereg(state, 0x51, 0x1e);
1156 ds3000_tuner_writereg(state, 0x51, 0x1f);
1157 ds3000_tuner_writereg(state, 0x50, 0x01);
1158 ds3000_tuner_writereg(state, 0x50, 0x00);
1159 msleep(60);
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001160
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001161 /* ds3000 global reset */
1162 ds3000_writereg(state, 0x07, 0x80);
1163 ds3000_writereg(state, 0x07, 0x00);
1164 /* ds3000 build-in uC reset */
1165 ds3000_writereg(state, 0xb2, 0x01);
1166 /* ds3000 software reset */
1167 ds3000_writereg(state, 0x00, 0x01);
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001168
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001169 switch (c->delivery_system) {
1170 case SYS_DVBS:
1171 /* initialise the demod in DVB-S mode */
1172 for (i = 0; i < sizeof(ds3000_dvbs_init_tab); i += 2)
1173 ds3000_writereg(state,
1174 ds3000_dvbs_init_tab[i],
1175 ds3000_dvbs_init_tab[i + 1]);
1176 value = ds3000_readreg(state, 0xfe);
1177 value &= 0xc0;
1178 value |= 0x1b;
1179 ds3000_writereg(state, 0xfe, value);
1180 break;
1181 case SYS_DVBS2:
1182 /* initialise the demod in DVB-S2 mode */
1183 for (i = 0; i < sizeof(ds3000_dvbs2_init_tab); i += 2)
1184 ds3000_writereg(state,
1185 ds3000_dvbs2_init_tab[i],
1186 ds3000_dvbs2_init_tab[i + 1]);
1187 ds3000_writereg(state, 0xfe, 0x98);
1188 break;
1189 default:
1190 return 1;
1191 }
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001192
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001193 /* enable 27MHz clock output */
1194 ds3000_writereg(state, 0x29, 0x80);
1195 /* enable ac coupling */
1196 ds3000_writereg(state, 0x25, 0x8a);
1197
1198 /* enhance symbol rate performance */
1199 if ((state->dcur.symbol_rate / 1000) <= 5000) {
1200 value = 29777 / (state->dcur.symbol_rate / 1000) + 1;
1201 if (value % 2 != 0)
1202 value++;
1203 ds3000_writereg(state, 0xc3, 0x0d);
1204 ds3000_writereg(state, 0xc8, value);
1205 ds3000_writereg(state, 0xc4, 0x10);
1206 ds3000_writereg(state, 0xc7, 0x0e);
1207 } else if ((state->dcur.symbol_rate / 1000) <= 10000) {
1208 value = 92166 / (state->dcur.symbol_rate / 1000) + 1;
1209 if (value % 2 != 0)
1210 value++;
1211 ds3000_writereg(state, 0xc3, 0x07);
1212 ds3000_writereg(state, 0xc8, value);
1213 ds3000_writereg(state, 0xc4, 0x09);
1214 ds3000_writereg(state, 0xc7, 0x12);
1215 } else if ((state->dcur.symbol_rate / 1000) <= 20000) {
1216 value = 64516 / (state->dcur.symbol_rate / 1000) + 1;
1217 ds3000_writereg(state, 0xc3, value);
1218 ds3000_writereg(state, 0xc8, 0x0e);
1219 ds3000_writereg(state, 0xc4, 0x07);
1220 ds3000_writereg(state, 0xc7, 0x18);
1221 } else {
1222 value = 129032 / (state->dcur.symbol_rate / 1000) + 1;
1223 ds3000_writereg(state, 0xc3, value);
1224 ds3000_writereg(state, 0xc8, 0x0a);
1225 ds3000_writereg(state, 0xc4, 0x05);
1226 ds3000_writereg(state, 0xc7, 0x24);
1227 }
1228
1229 /* normalized symbol rate rounded to the closest integer */
1230 value = (((state->dcur.symbol_rate / 1000) << 16) +
1231 (DS3000_SAMPLE_RATE / 2)) / DS3000_SAMPLE_RATE;
1232 ds3000_writereg(state, 0x61, value & 0x00ff);
1233 ds3000_writereg(state, 0x62, (value & 0xff00) >> 8);
1234
1235 /* co-channel interference cancellation disabled */
1236 ds3000_writereg(state, 0x56, 0x00);
1237
1238 /* equalizer disabled */
1239 ds3000_writereg(state, 0x76, 0x00);
1240
1241 /*ds3000_writereg(state, 0x08, 0x03);
1242 ds3000_writereg(state, 0xfd, 0x22);
1243 ds3000_writereg(state, 0x08, 0x07);
1244 ds3000_writereg(state, 0xfd, 0x42);
1245 ds3000_writereg(state, 0x08, 0x07);*/
1246
1247 if (state->config->ci_mode) {
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001248 switch (c->delivery_system) {
1249 case SYS_DVBS:
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001250 default:
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001251 ds3000_writereg(state, 0xfd, 0x80);
1252 break;
1253 case SYS_DVBS2:
1254 ds3000_writereg(state, 0xfd, 0x01);
Igor M. Liplianind2ffc442011-02-25 18:41:22 -03001255 break;
Igor M. Liplianind2ffc442011-02-25 18:41:22 -03001256 }
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001257 }
Igor M. Liplianind2ffc442011-02-25 18:41:22 -03001258
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001259 /* ds3000 out of software reset */
1260 ds3000_writereg(state, 0x00, 0x00);
1261 /* start ds3000 build-in uC */
1262 ds3000_writereg(state, 0xb2, 0x00);
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001263
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001264 /* TODO: calculate and set carrier offset */
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001265
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001266 for (i = 0; i < 30 ; i++) {
1267 ds3000_read_status(fe, &status);
1268 if (status && FE_HAS_LOCK)
1269 break;
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001270
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001271 msleep(10);
1272 }
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001273
Igor M. Liplianin18a73f32011-02-25 18:41:23 -03001274 return 0;
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001275}
1276
1277static enum dvbfe_algo ds3000_get_algo(struct dvb_frontend *fe)
1278{
1279 dprintk("%s()\n", __func__);
1280 return DVBFE_ALGO_SW;
1281}
1282
1283/*
1284 * Initialise or wake up device
1285 *
1286 * Power config will reset and load initial firmware if required
1287 */
1288static int ds3000_initfe(struct dvb_frontend *fe)
1289{
Igor M. Liplianina0ea298d52011-02-01 19:40:03 -03001290 struct ds3000_state *state = fe->demodulator_priv;
1291 int ret;
1292
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001293 dprintk("%s()\n", __func__);
Igor M. Liplianina0ea298d52011-02-01 19:40:03 -03001294 /* hard reset */
1295 ds3000_writereg(state, 0x08, 0x01 | ds3000_readreg(state, 0x08));
1296 msleep(1);
1297
1298 /* TS2020 init */
1299 ds3000_tuner_writereg(state, 0x42, 0x73);
1300 ds3000_tuner_writereg(state, 0x05, 0x01);
1301 ds3000_tuner_writereg(state, 0x62, 0xf5);
Igor M. Liplianinb9bf2ea2011-02-01 19:40:36 -03001302 /* Load the firmware if required */
1303 ret = ds3000_firmware_ondemand(fe);
1304 if (ret != 0) {
1305 printk(KERN_ERR "%s: Unable initialize firmware\n", __func__);
1306 return ret;
1307 }
Igor M. Liplianina0ea298d52011-02-01 19:40:03 -03001308
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001309 return 0;
1310}
1311
1312/* Put device to sleep */
1313static int ds3000_sleep(struct dvb_frontend *fe)
1314{
1315 dprintk("%s()\n", __func__);
1316 return 0;
1317}
1318
1319static struct dvb_frontend_ops ds3000_ops = {
1320
1321 .info = {
1322 .name = "Montage Technology DS3000/TS2020",
1323 .type = FE_QPSK,
1324 .frequency_min = 950000,
1325 .frequency_max = 2150000,
1326 .frequency_stepsize = 1011, /* kHz for QPSK frontends */
1327 .frequency_tolerance = 5000,
1328 .symbol_rate_min = 1000000,
1329 .symbol_rate_max = 45000000,
1330 .caps = FE_CAN_INVERSION_AUTO |
1331 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1332 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
1333 FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1334 FE_CAN_2G_MODULATION |
1335 FE_CAN_QPSK | FE_CAN_RECOVER
1336 },
1337
1338 .release = ds3000_release,
1339
1340 .init = ds3000_initfe,
1341 .sleep = ds3000_sleep,
1342 .read_status = ds3000_read_status,
1343 .read_ber = ds3000_read_ber,
1344 .read_signal_strength = ds3000_read_signal_strength,
1345 .read_snr = ds3000_read_snr,
1346 .read_ucblocks = ds3000_read_ucblocks,
Igor M. Liplianind2ffc442011-02-25 18:41:22 -03001347 .set_voltage = ds3000_set_voltage,
Igor M. Liplianin09ea33e2009-11-24 20:16:04 -03001348 .set_tone = ds3000_set_tone,
1349 .diseqc_send_master_cmd = ds3000_send_diseqc_msg,
1350 .diseqc_send_burst = ds3000_diseqc_send_burst,
1351 .get_frontend_algo = ds3000_get_algo,
1352
1353 .set_property = ds3000_set_property,
1354 .get_property = ds3000_get_property,
1355 .set_frontend = ds3000_tune,
1356};
1357
1358module_param(debug, int, 0644);
1359MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
1360
1361MODULE_DESCRIPTION("DVB Frontend module for Montage Technology "
1362 "DS3000/TS2020 hardware");
1363MODULE_AUTHOR("Konstantin Dimitrov");
1364MODULE_LICENSE("GPL");