aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/dvb/frontends/s5h1409.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/dvb/frontends/s5h1409.c')
-rw-r--r--drivers/media/dvb/frontends/s5h1409.c729
1 files changed, 729 insertions, 0 deletions
diff --git a/drivers/media/dvb/frontends/s5h1409.c b/drivers/media/dvb/frontends/s5h1409.c
new file mode 100644
index 000000000000..30e8a705fad4
--- /dev/null
+++ b/drivers/media/dvb/frontends/s5h1409.c
@@ -0,0 +1,729 @@
1/*
2 Samsung S5H1409 VSB/QAM demodulator driver
3
4 Copyright (C) 2006 Steven Toth <stoth@hauppauge.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20*/
21
22#include <linux/kernel.h>
23#include <linux/init.h>
24#include <linux/module.h>
25#include <linux/string.h>
26#include <linux/slab.h>
27#include <linux/delay.h>
28#include "dvb_frontend.h"
29#include "dvb-pll.h"
30#include "s5h1409.h"
31
32struct s5h1409_state {
33
34 struct i2c_adapter* i2c;
35
36 /* configuration settings */
37 const struct s5h1409_config* config;
38
39 struct dvb_frontend frontend;
40
41 /* previous uncorrected block counter */
42 fe_modulation_t current_modulation;
43
44 u32 current_frequency;
45};
46
47static int debug = 0;
48#define dprintk if (debug) printk
49
50/* Register values to initialise the demod, this will set VSB by default */
51static struct init_tab {
52 u8 reg;
53 u16 data;
54} init_tab[] = {
55 { 0x00, 0x0071, },
56 { 0x01, 0x3213, },
57 { 0x09, 0x0025, },
58 { 0x1c, 0x001d, },
59 { 0x1f, 0x002d, },
60 { 0x20, 0x001d, },
61 { 0x22, 0x0022, },
62 { 0x23, 0x0020, },
63 { 0x29, 0x110f, },
64 { 0x2a, 0x10b4, },
65 { 0x2b, 0x10ae, },
66 { 0x2c, 0x0031, },
67 { 0x31, 0x010d, },
68 { 0x32, 0x0100, },
69 { 0x44, 0x0510, },
70 { 0x54, 0x0104, },
71 { 0x58, 0x2222, },
72 { 0x59, 0x1162, },
73 { 0x5a, 0x3211, },
74 { 0x5d, 0x0370, },
75 { 0x5e, 0x0296, },
76 { 0x61, 0x0010, },
77 { 0x63, 0x4a00, },
78 { 0x65, 0x0800, },
79 { 0x71, 0x0003, },
80 { 0x72, 0x0470, },
81 { 0x81, 0x0002, },
82 { 0x82, 0x0600, },
83 { 0x86, 0x0002, },
84 { 0x8a, 0x2c38, },
85 { 0x8b, 0x2a37, },
86 { 0x92, 0x302f, },
87 { 0x93, 0x3332, },
88 { 0x96, 0x000c, },
89 { 0x99, 0x0101, },
90 { 0x9c, 0x2e37, },
91 { 0x9d, 0x2c37, },
92 { 0x9e, 0x2c37, },
93 { 0xab, 0x0100, },
94 { 0xac, 0x1003, },
95 { 0xad, 0x103f, },
96 { 0xe2, 0x0100, },
97 { 0x28, 0x1010, },
98 { 0xb1, 0x000e, },
99};
100
101/* VSB SNR lookup table */
102static struct vsb_snr_tab {
103 u16 val;
104 u16 data;
105} vsb_snr_tab[] = {
106 { 1023, 770, },
107 { 923, 300, },
108 { 918, 295, },
109 { 915, 290, },
110 { 911, 285, },
111 { 906, 280, },
112 { 901, 275, },
113 { 896, 270, },
114 { 891, 265, },
115 { 885, 260, },
116 { 879, 255, },
117 { 873, 250, },
118 { 864, 245, },
119 { 858, 240, },
120 { 850, 235, },
121 { 841, 230, },
122 { 832, 225, },
123 { 823, 220, },
124 { 812, 215, },
125 { 802, 210, },
126 { 788, 205, },
127 { 778, 200, },
128 { 767, 195, },
129 { 753, 190, },
130 { 740, 185, },
131 { 725, 180, },
132 { 707, 175, },
133 { 689, 170, },
134 { 671, 165, },
135 { 656, 160, },
136 { 637, 155, },
137 { 616, 150, },
138 { 542, 145, },
139 { 519, 140, },
140 { 507, 135, },
141 { 497, 130, },
142 { 492, 125, },
143 { 474, 120, },
144 { 300, 111, },
145 { 0, 0, },
146};
147
148/* QAM64 SNR lookup table */
149static struct qam64_snr_tab {
150 u16 val;
151 u16 data;
152} qam64_snr_tab[] = {
153 { 12, 300, },
154 { 15, 290, },
155 { 18, 280, },
156 { 22, 270, },
157 { 23, 268, },
158 { 24, 266, },
159 { 25, 264, },
160 { 27, 262, },
161 { 28, 260, },
162 { 29, 258, },
163 { 30, 256, },
164 { 32, 254, },
165 { 33, 252, },
166 { 34, 250, },
167 { 35, 249, },
168 { 36, 248, },
169 { 37, 247, },
170 { 38, 246, },
171 { 39, 245, },
172 { 40, 244, },
173 { 41, 243, },
174 { 42, 241, },
175 { 43, 240, },
176 { 44, 239, },
177 { 45, 238, },
178 { 46, 237, },
179 { 47, 236, },
180 { 48, 235, },
181 { 49, 234, },
182 { 50, 233, },
183 { 51, 232, },
184 { 52, 231, },
185 { 53, 230, },
186 { 55, 229, },
187 { 56, 228, },
188 { 57, 227, },
189 { 58, 226, },
190 { 59, 225, },
191 { 60, 224, },
192 { 62, 223, },
193 { 63, 222, },
194 { 65, 221, },
195 { 66, 220, },
196 { 68, 219, },
197 { 69, 218, },
198 { 70, 217, },
199 { 72, 216, },
200 { 73, 215, },
201 { 75, 214, },
202 { 76, 213, },
203 { 78, 212, },
204 { 80, 211, },
205 { 81, 210, },
206 { 83, 209, },
207 { 84, 208, },
208 { 85, 207, },
209 { 87, 206, },
210 { 89, 205, },
211 { 91, 204, },
212 { 93, 203, },
213 { 95, 202, },
214 { 96, 201, },
215 { 104, 200, },
216};
217
218/* QAM256 SNR lookup table */
219static struct qam256_snr_tab {
220 u16 val;
221 u16 data;
222} qam256_snr_tab[] = {
223 { 12, 400, },
224 { 13, 390, },
225 { 15, 380, },
226 { 17, 360, },
227 { 19, 350, },
228 { 22, 348, },
229 { 23, 346, },
230 { 24, 344, },
231 { 25, 342, },
232 { 26, 340, },
233 { 27, 336, },
234 { 28, 334, },
235 { 29, 332, },
236 { 30, 330, },
237 { 31, 328, },
238 { 32, 326, },
239 { 33, 325, },
240 { 34, 322, },
241 { 35, 320, },
242 { 37, 318, },
243 { 39, 316, },
244 { 40, 314, },
245 { 41, 312, },
246 { 42, 310, },
247 { 43, 308, },
248 { 46, 306, },
249 { 47, 304, },
250 { 49, 302, },
251 { 51, 300, },
252 { 53, 298, },
253 { 54, 297, },
254 { 55, 296, },
255 { 56, 295, },
256 { 57, 294, },
257 { 59, 293, },
258 { 60, 292, },
259 { 61, 291, },
260 { 63, 290, },
261 { 64, 289, },
262 { 65, 288, },
263 { 66, 287, },
264 { 68, 286, },
265 { 69, 285, },
266 { 71, 284, },
267 { 72, 283, },
268 { 74, 282, },
269 { 75, 281, },
270 { 76, 280, },
271 { 77, 279, },
272 { 78, 278, },
273 { 81, 277, },
274 { 83, 276, },
275 { 84, 275, },
276 { 86, 274, },
277 { 87, 273, },
278 { 89, 272, },
279 { 90, 271, },
280 { 92, 270, },
281 { 93, 269, },
282 { 95, 268, },
283 { 96, 267, },
284 { 98, 266, },
285 { 100, 265, },
286 { 102, 264, },
287 { 104, 263, },
288 { 105, 262, },
289 { 106, 261, },
290 { 110, 260, },
291};
292
293/* 8 bit registers, 16 bit values */
294static int s5h1409_writereg(struct s5h1409_state* state, u8 reg, u16 data)
295{
296 int ret;
297 u8 buf [] = { reg, data >> 8, data & 0xff };
298
299 struct i2c_msg msg = { .addr = state->config->demod_address,
300 .flags = 0, .buf = buf, .len = 3 };
301
302 ret = i2c_transfer(state->i2c, &msg, 1);
303
304 if (ret != 1)
305 printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
306 "ret == %i)\n", __FUNCTION__, reg, data, ret);
307
308 return (ret != 1) ? -1 : 0;
309}
310
311static u16 s5h1409_readreg(struct s5h1409_state* state, u8 reg)
312{
313 int ret;
314 u8 b0 [] = { reg };
315 u8 b1 [] = { 0, 0 };
316
317 struct i2c_msg msg [] = {
318 { .addr = state->config->demod_address, .flags = 0,
319 .buf = b0, .len = 1 },
320 { .addr = state->config->demod_address, .flags = I2C_M_RD,
321 .buf = b1, .len = 2 } };
322
323 ret = i2c_transfer(state->i2c, msg, 2);
324
325 if (ret != 2)
326 printk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret);
327 return (b1[0] << 8) | b1[1];
328}
329
330static int s5h1409_softreset(struct dvb_frontend* fe)
331{
332 struct s5h1409_state* state = fe->demodulator_priv;
333
334 dprintk("%s()\n", __FUNCTION__);
335
336 s5h1409_writereg(state, 0xf5, 0);
337 s5h1409_writereg(state, 0xf5, 1);
338 return 0;
339}
340
341static int s5h1409_set_if_freq(struct dvb_frontend* fe, int KHz)
342{
343 struct s5h1409_state* state = fe->demodulator_priv;
344 int ret = 0;
345
346 dprintk("%s(%d KHz)\n", __FUNCTION__, KHz);
347
348 if( (KHz == 44000) || (KHz == 5380) ) {
349 s5h1409_writereg(state, 0x87, 0x01be);
350 s5h1409_writereg(state, 0x88, 0x0436);
351 s5h1409_writereg(state, 0x89, 0x054d);
352 } else {
353 printk("%s() Invalid arg = %d KHz\n", __FUNCTION__, KHz);
354 ret = -1;
355 }
356
357 return ret;
358}
359
360static int s5h1409_set_spectralinversion(struct dvb_frontend* fe, int inverted)
361{
362 struct s5h1409_state* state = fe->demodulator_priv;
363
364 dprintk("%s()\n", __FUNCTION__);
365
366 if(inverted == 1)
367 return s5h1409_writereg(state, 0x1b, 0x1101); /* Inverted */
368 else
369 return s5h1409_writereg(state, 0x1b, 0x0110); /* Normal */
370}
371
372static int s5h1409_enable_modulation(struct dvb_frontend* fe,
373 fe_modulation_t m)
374{
375 struct s5h1409_state* state = fe->demodulator_priv;
376
377 dprintk("%s(0x%08x)\n", __FUNCTION__, m);
378
379 switch(m) {
380 case VSB_8:
381 dprintk("%s() VSB_8\n", __FUNCTION__);
382 s5h1409_writereg(state, 0xf4, 0);
383 break;
384 case QAM_64:
385 dprintk("%s() QAM_64\n", __FUNCTION__);
386 s5h1409_writereg(state, 0xf4, 1);
387 s5h1409_writereg(state, 0x85, 0x100);
388 break;
389 case QAM_256:
390 dprintk("%s() QAM_256\n", __FUNCTION__);
391 s5h1409_writereg(state, 0xf4, 1);
392 s5h1409_writereg(state, 0x85, 0x101);
393 break;
394 default:
395 dprintk("%s() Invalid modulation\n", __FUNCTION__);
396 return -EINVAL;
397 }
398
399 state->current_modulation = m;
400 s5h1409_softreset(fe);
401
402 return 0;
403}
404
405static int s5h1409_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
406{
407 struct s5h1409_state* state = fe->demodulator_priv;
408
409 dprintk("%s(%d)\n", __FUNCTION__, enable);
410
411 if (enable)
412 return s5h1409_writereg(state, 0xf3, 1);
413 else
414 return s5h1409_writereg(state, 0xf3, 0);
415}
416
417static int s5h1409_set_gpio(struct dvb_frontend* fe, int enable)
418{
419 struct s5h1409_state* state = fe->demodulator_priv;
420
421 dprintk("%s(%d)\n", __FUNCTION__, enable);
422
423 if (enable)
424 return s5h1409_writereg(state, 0xe3, 0x1100);
425 else
426 return s5h1409_writereg(state, 0xe3, 0);
427}
428
429static int s5h1409_sleep(struct dvb_frontend* fe, int enable)
430{
431 struct s5h1409_state* state = fe->demodulator_priv;
432
433 dprintk("%s(%d)\n", __FUNCTION__, enable);
434
435 return s5h1409_writereg(state, 0xf2, enable);
436}
437
438static int s5h1409_register_reset(struct dvb_frontend* fe)
439{
440 struct s5h1409_state* state = fe->demodulator_priv;
441
442 dprintk("%s()\n", __FUNCTION__);
443
444 return s5h1409_writereg(state, 0xfa, 0);
445}
446
447/* Talk to the demod, set the FEC, GUARD, QAM settings etc */
448static int s5h1409_set_frontend (struct dvb_frontend* fe,
449 struct dvb_frontend_parameters *p)
450{
451 struct s5h1409_state* state = fe->demodulator_priv;
452
453 dprintk("%s(frequency=%d)\n", __FUNCTION__, p->frequency);
454
455 s5h1409_softreset(fe);
456
457 state->current_frequency = p->frequency;
458
459 s5h1409_enable_modulation(fe, p->u.vsb.modulation);
460
461 if (fe->ops.tuner_ops.set_params) {
462 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 1);
463 fe->ops.tuner_ops.set_params(fe, p);
464 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
465 }
466
467 return 0;
468}
469
470/* Reset the demod hardware and reset all of the configuration registers
471 to a default state. */
472static int s5h1409_init (struct dvb_frontend* fe)
473{
474 int i;
475
476 struct s5h1409_state* state = fe->demodulator_priv;
477 dprintk("%s()\n", __FUNCTION__);
478
479 s5h1409_sleep(fe, 0);
480 s5h1409_register_reset(fe);
481
482 for (i=0; i < ARRAY_SIZE(init_tab); i++)
483 s5h1409_writereg(state, init_tab[i].reg, init_tab[i].data);
484
485 /* The datasheet says that after initialisation, VSB is default */
486 state->current_modulation = VSB_8;
487
488 if (state->config->output_mode == S5H1409_SERIAL_OUTPUT)
489 s5h1409_writereg(state, 0xab, 0x100); /* Serial */
490 else
491 s5h1409_writereg(state, 0xab, 0x0); /* Parallel */
492
493 s5h1409_set_spectralinversion(fe, state->config->inversion);
494 s5h1409_set_if_freq(fe, state->config->if_freq);
495 s5h1409_set_gpio(fe, state->config->gpio);
496 s5h1409_softreset(fe);
497
498 /* Note: Leaving the I2C gate open here. */
499 s5h1409_i2c_gate_ctrl(fe, 1);
500
501 return 0;
502}
503
504static int s5h1409_read_status(struct dvb_frontend* fe, fe_status_t* status)
505{
506 struct s5h1409_state* state = fe->demodulator_priv;
507 u16 reg;
508 u32 tuner_status = 0;
509
510 *status = 0;
511
512 /* Get the demodulator status */
513 reg = s5h1409_readreg(state, 0xf1);
514 if(reg & 0x1000)
515 *status |= FE_HAS_VITERBI;
516 if(reg & 0x8000)
517 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
518
519 switch(state->config->status_mode) {
520 case S5H1409_DEMODLOCKING:
521 if (*status & FE_HAS_VITERBI)
522 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
523 break;
524 case S5H1409_TUNERLOCKING:
525 /* Get the tuner status */
526 if (fe->ops.tuner_ops.get_status) {
527 if (fe->ops.i2c_gate_ctrl)
528 fe->ops.i2c_gate_ctrl(fe, 1);
529
530 fe->ops.tuner_ops.get_status(fe, &tuner_status);
531
532 if (fe->ops.i2c_gate_ctrl)
533 fe->ops.i2c_gate_ctrl(fe, 0);
534 }
535 if (tuner_status)
536 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
537 break;
538 }
539
540 dprintk("%s() status 0x%08x\n", __FUNCTION__, *status);
541
542 return 0;
543}
544
545static int s5h1409_qam256_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
546{
547 int i, ret = -EINVAL;
548 dprintk("%s()\n", __FUNCTION__);
549
550 for (i=0; i < ARRAY_SIZE(qam256_snr_tab); i++) {
551 if (v < qam256_snr_tab[i].val) {
552 *snr = qam256_snr_tab[i].data;
553 ret = 0;
554 break;
555 }
556 }
557 return ret;
558}
559
560static int s5h1409_qam64_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
561{
562 int i, ret = -EINVAL;
563 dprintk("%s()\n", __FUNCTION__);
564
565 for (i=0; i < ARRAY_SIZE(qam64_snr_tab); i++) {
566 if (v < qam64_snr_tab[i].val) {
567 *snr = qam64_snr_tab[i].data;
568 ret = 0;
569 break;
570 }
571 }
572 return ret;
573}
574
575static int s5h1409_vsb_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
576{
577 int i, ret = -EINVAL;
578 dprintk("%s()\n", __FUNCTION__);
579
580 for (i=0; i < ARRAY_SIZE(vsb_snr_tab); i++) {
581 if (v > vsb_snr_tab[i].val) {
582 *snr = vsb_snr_tab[i].data;
583 ret = 0;
584 break;
585 }
586 }
587 dprintk("%s() snr=%d\n", __FUNCTION__, *snr);
588 return ret;
589}
590
591static int s5h1409_read_snr(struct dvb_frontend* fe, u16* snr)
592{
593 struct s5h1409_state* state = fe->demodulator_priv;
594 u16 reg;
595 dprintk("%s()\n", __FUNCTION__);
596
597 reg = s5h1409_readreg(state, 0xf1) & 0x1ff;
598
599 switch(state->current_modulation) {
600 case QAM_64:
601 return s5h1409_qam64_lookup_snr(fe, snr, reg);
602 case QAM_256:
603 return s5h1409_qam256_lookup_snr(fe, snr, reg);
604 case VSB_8:
605 return s5h1409_vsb_lookup_snr(fe, snr, reg);
606 default:
607 break;
608 }
609
610 return -EINVAL;
611}
612
613static int s5h1409_read_signal_strength(struct dvb_frontend* fe,
614 u16* signal_strength)
615{
616 return s5h1409_read_snr(fe, signal_strength);
617}
618
619static int s5h1409_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
620{
621 struct s5h1409_state* state = fe->demodulator_priv;
622
623 *ucblocks = s5h1409_readreg(state, 0xb5);
624
625 return 0;
626}
627
628static int s5h1409_read_ber(struct dvb_frontend* fe, u32* ber)
629{
630 return s5h1409_read_ucblocks(fe, ber);
631}
632
633static int s5h1409_get_frontend(struct dvb_frontend* fe,
634 struct dvb_frontend_parameters *p)
635{
636 struct s5h1409_state* state = fe->demodulator_priv;
637
638 p->frequency = state->current_frequency;
639 p->u.vsb.modulation = state->current_modulation;
640
641 return 0;
642}
643
644static int s5h1409_get_tune_settings(struct dvb_frontend* fe,
645 struct dvb_frontend_tune_settings *tune)
646{
647 tune->min_delay_ms = 1000;
648 return 0;
649}
650
651static void s5h1409_release(struct dvb_frontend* fe)
652{
653 struct s5h1409_state* state = fe->demodulator_priv;
654 kfree(state);
655}
656
657static struct dvb_frontend_ops s5h1409_ops;
658
659struct dvb_frontend* s5h1409_attach(const struct s5h1409_config* config,
660 struct i2c_adapter* i2c)
661{
662 struct s5h1409_state* state = NULL;
663
664 /* allocate memory for the internal state */
665 state = kmalloc(sizeof(struct s5h1409_state), GFP_KERNEL);
666 if (state == NULL)
667 goto error;
668
669 /* setup the state */
670 state->config = config;
671 state->i2c = i2c;
672 state->current_modulation = 0;
673
674 /* check if the demod exists */
675 if (s5h1409_readreg(state, 0x04) != 0x0066)
676 goto error;
677
678 /* create dvb_frontend */
679 memcpy(&state->frontend.ops, &s5h1409_ops,
680 sizeof(struct dvb_frontend_ops));
681 state->frontend.demodulator_priv = state;
682
683 /* Note: Leaving the I2C gate open here. */
684 s5h1409_writereg(state, 0xf3, 1);
685
686 return &state->frontend;
687
688error:
689 kfree(state);
690 return NULL;
691}
692
693static struct dvb_frontend_ops s5h1409_ops = {
694
695 .info = {
696 .name = "Samsung S5H1409 QAM/8VSB Frontend",
697 .type = FE_ATSC,
698 .frequency_min = 54000000,
699 .frequency_max = 858000000,
700 .frequency_stepsize = 62500,
701 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
702 },
703
704 .init = s5h1409_init,
705 .i2c_gate_ctrl = s5h1409_i2c_gate_ctrl,
706 .set_frontend = s5h1409_set_frontend,
707 .get_frontend = s5h1409_get_frontend,
708 .get_tune_settings = s5h1409_get_tune_settings,
709 .read_status = s5h1409_read_status,
710 .read_ber = s5h1409_read_ber,
711 .read_signal_strength = s5h1409_read_signal_strength,
712 .read_snr = s5h1409_read_snr,
713 .read_ucblocks = s5h1409_read_ucblocks,
714 .release = s5h1409_release,
715};
716
717module_param(debug, int, 0644);
718MODULE_PARM_DESC(debug, "Enable verbose debug messages");
719
720MODULE_DESCRIPTION("Samsung S5H1409 QAM-B/ATSC Demodulator driver");
721MODULE_AUTHOR("Steven Toth");
722MODULE_LICENSE("GPL");
723
724EXPORT_SYMBOL(s5h1409_attach);
725
726/*
727 * Local variables:
728 * c-basic-offset: 8
729 */