blob: 3391777e0b2fa3b435280e973444da0b8b36f46b [file] [log] [blame]
Steven Toth89885552007-07-28 19:34:52 -03001/*
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;
Michael Krufky2b032382007-12-13 10:04:10 -030045 int if_freq;
Steven Tothdd7d5012007-10-24 21:05:51 -030046
47 u32 is_qam_locked;
48 u32 qam_state;
Steven Toth89885552007-07-28 19:34:52 -030049};
50
51static int debug = 0;
52#define dprintk if (debug) printk
53
54/* Register values to initialise the demod, this will set VSB by default */
55static struct init_tab {
56 u8 reg;
57 u16 data;
58} init_tab[] = {
59 { 0x00, 0x0071, },
60 { 0x01, 0x3213, },
61 { 0x09, 0x0025, },
62 { 0x1c, 0x001d, },
63 { 0x1f, 0x002d, },
64 { 0x20, 0x001d, },
65 { 0x22, 0x0022, },
66 { 0x23, 0x0020, },
67 { 0x29, 0x110f, },
68 { 0x2a, 0x10b4, },
69 { 0x2b, 0x10ae, },
70 { 0x2c, 0x0031, },
71 { 0x31, 0x010d, },
72 { 0x32, 0x0100, },
73 { 0x44, 0x0510, },
74 { 0x54, 0x0104, },
75 { 0x58, 0x2222, },
76 { 0x59, 0x1162, },
77 { 0x5a, 0x3211, },
78 { 0x5d, 0x0370, },
79 { 0x5e, 0x0296, },
80 { 0x61, 0x0010, },
81 { 0x63, 0x4a00, },
82 { 0x65, 0x0800, },
83 { 0x71, 0x0003, },
84 { 0x72, 0x0470, },
85 { 0x81, 0x0002, },
86 { 0x82, 0x0600, },
87 { 0x86, 0x0002, },
88 { 0x8a, 0x2c38, },
89 { 0x8b, 0x2a37, },
90 { 0x92, 0x302f, },
91 { 0x93, 0x3332, },
92 { 0x96, 0x000c, },
93 { 0x99, 0x0101, },
94 { 0x9c, 0x2e37, },
95 { 0x9d, 0x2c37, },
96 { 0x9e, 0x2c37, },
97 { 0xab, 0x0100, },
98 { 0xac, 0x1003, },
99 { 0xad, 0x103f, },
100 { 0xe2, 0x0100, },
Steven Tothdd7d5012007-10-24 21:05:51 -0300101 { 0xe3, 0x0000, },
Steven Toth89885552007-07-28 19:34:52 -0300102 { 0x28, 0x1010, },
103 { 0xb1, 0x000e, },
104};
105
106/* VSB SNR lookup table */
107static struct vsb_snr_tab {
108 u16 val;
109 u16 data;
110} vsb_snr_tab[] = {
Steven Toth23003172007-12-12 22:14:00 -0300111 { 924, 300, },
Steven Toth89885552007-07-28 19:34:52 -0300112 { 923, 300, },
113 { 918, 295, },
114 { 915, 290, },
115 { 911, 285, },
116 { 906, 280, },
117 { 901, 275, },
118 { 896, 270, },
119 { 891, 265, },
120 { 885, 260, },
121 { 879, 255, },
122 { 873, 250, },
123 { 864, 245, },
124 { 858, 240, },
125 { 850, 235, },
126 { 841, 230, },
127 { 832, 225, },
128 { 823, 220, },
129 { 812, 215, },
130 { 802, 210, },
131 { 788, 205, },
132 { 778, 200, },
133 { 767, 195, },
134 { 753, 190, },
135 { 740, 185, },
136 { 725, 180, },
137 { 707, 175, },
138 { 689, 170, },
139 { 671, 165, },
140 { 656, 160, },
141 { 637, 155, },
142 { 616, 150, },
143 { 542, 145, },
144 { 519, 140, },
145 { 507, 135, },
146 { 497, 130, },
147 { 492, 125, },
148 { 474, 120, },
149 { 300, 111, },
150 { 0, 0, },
151};
152
153/* QAM64 SNR lookup table */
154static struct qam64_snr_tab {
155 u16 val;
156 u16 data;
157} qam64_snr_tab[] = {
Steven Toth23003172007-12-12 22:14:00 -0300158 { 1, 0, },
Steven Toth89885552007-07-28 19:34:52 -0300159 { 12, 300, },
160 { 15, 290, },
161 { 18, 280, },
162 { 22, 270, },
163 { 23, 268, },
164 { 24, 266, },
165 { 25, 264, },
166 { 27, 262, },
167 { 28, 260, },
168 { 29, 258, },
169 { 30, 256, },
170 { 32, 254, },
171 { 33, 252, },
172 { 34, 250, },
173 { 35, 249, },
174 { 36, 248, },
175 { 37, 247, },
176 { 38, 246, },
177 { 39, 245, },
178 { 40, 244, },
179 { 41, 243, },
180 { 42, 241, },
181 { 43, 240, },
182 { 44, 239, },
183 { 45, 238, },
184 { 46, 237, },
185 { 47, 236, },
186 { 48, 235, },
187 { 49, 234, },
188 { 50, 233, },
189 { 51, 232, },
190 { 52, 231, },
191 { 53, 230, },
192 { 55, 229, },
193 { 56, 228, },
194 { 57, 227, },
195 { 58, 226, },
196 { 59, 225, },
197 { 60, 224, },
198 { 62, 223, },
199 { 63, 222, },
200 { 65, 221, },
201 { 66, 220, },
202 { 68, 219, },
203 { 69, 218, },
204 { 70, 217, },
205 { 72, 216, },
206 { 73, 215, },
207 { 75, 214, },
208 { 76, 213, },
209 { 78, 212, },
210 { 80, 211, },
211 { 81, 210, },
212 { 83, 209, },
213 { 84, 208, },
214 { 85, 207, },
215 { 87, 206, },
216 { 89, 205, },
217 { 91, 204, },
218 { 93, 203, },
219 { 95, 202, },
220 { 96, 201, },
221 { 104, 200, },
Steven Toth23003172007-12-12 22:14:00 -0300222 { 255, 0, },
Steven Toth89885552007-07-28 19:34:52 -0300223};
224
225/* QAM256 SNR lookup table */
226static struct qam256_snr_tab {
227 u16 val;
228 u16 data;
229} qam256_snr_tab[] = {
Steven Toth23003172007-12-12 22:14:00 -0300230 { 1, 0, },
Steven Toth89885552007-07-28 19:34:52 -0300231 { 12, 400, },
232 { 13, 390, },
233 { 15, 380, },
234 { 17, 360, },
235 { 19, 350, },
236 { 22, 348, },
237 { 23, 346, },
238 { 24, 344, },
239 { 25, 342, },
240 { 26, 340, },
241 { 27, 336, },
242 { 28, 334, },
243 { 29, 332, },
244 { 30, 330, },
245 { 31, 328, },
246 { 32, 326, },
247 { 33, 325, },
248 { 34, 322, },
249 { 35, 320, },
250 { 37, 318, },
251 { 39, 316, },
252 { 40, 314, },
253 { 41, 312, },
254 { 42, 310, },
255 { 43, 308, },
256 { 46, 306, },
257 { 47, 304, },
258 { 49, 302, },
259 { 51, 300, },
260 { 53, 298, },
261 { 54, 297, },
262 { 55, 296, },
263 { 56, 295, },
264 { 57, 294, },
265 { 59, 293, },
266 { 60, 292, },
267 { 61, 291, },
268 { 63, 290, },
269 { 64, 289, },
270 { 65, 288, },
271 { 66, 287, },
272 { 68, 286, },
273 { 69, 285, },
274 { 71, 284, },
275 { 72, 283, },
276 { 74, 282, },
277 { 75, 281, },
278 { 76, 280, },
279 { 77, 279, },
280 { 78, 278, },
281 { 81, 277, },
282 { 83, 276, },
283 { 84, 275, },
284 { 86, 274, },
285 { 87, 273, },
286 { 89, 272, },
287 { 90, 271, },
288 { 92, 270, },
289 { 93, 269, },
290 { 95, 268, },
291 { 96, 267, },
292 { 98, 266, },
293 { 100, 265, },
294 { 102, 264, },
295 { 104, 263, },
296 { 105, 262, },
297 { 106, 261, },
298 { 110, 260, },
Steven Toth23003172007-12-12 22:14:00 -0300299 { 255, 0, },
Steven Toth89885552007-07-28 19:34:52 -0300300};
301
302/* 8 bit registers, 16 bit values */
303static int s5h1409_writereg(struct s5h1409_state* state, u8 reg, u16 data)
304{
305 int ret;
306 u8 buf [] = { reg, data >> 8, data & 0xff };
307
Michael Krufky3873dd02007-07-28 20:02:55 -0300308 struct i2c_msg msg = { .addr = state->config->demod_address,
309 .flags = 0, .buf = buf, .len = 3 };
Steven Toth89885552007-07-28 19:34:52 -0300310
311 ret = i2c_transfer(state->i2c, &msg, 1);
312
313 if (ret != 1)
Michael Krufky3873dd02007-07-28 20:02:55 -0300314 printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
315 "ret == %i)\n", __FUNCTION__, reg, data, ret);
Steven Toth89885552007-07-28 19:34:52 -0300316
317 return (ret != 1) ? -1 : 0;
318}
319
320static u16 s5h1409_readreg(struct s5h1409_state* state, u8 reg)
321{
322 int ret;
323 u8 b0 [] = { reg };
324 u8 b1 [] = { 0, 0 };
325
326 struct i2c_msg msg [] = {
Michael Krufky3873dd02007-07-28 20:02:55 -0300327 { .addr = state->config->demod_address, .flags = 0,
328 .buf = b0, .len = 1 },
329 { .addr = state->config->demod_address, .flags = I2C_M_RD,
330 .buf = b1, .len = 2 } };
Steven Toth89885552007-07-28 19:34:52 -0300331
332 ret = i2c_transfer(state->i2c, msg, 2);
333
334 if (ret != 2)
Michael Krufky3873dd02007-07-28 20:02:55 -0300335 printk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret);
Steven Toth89885552007-07-28 19:34:52 -0300336 return (b1[0] << 8) | b1[1];
337}
338
339static int s5h1409_softreset(struct dvb_frontend* fe)
340{
341 struct s5h1409_state* state = fe->demodulator_priv;
342
343 dprintk("%s()\n", __FUNCTION__);
344
345 s5h1409_writereg(state, 0xf5, 0);
346 s5h1409_writereg(state, 0xf5, 1);
Steven Tothdd7d5012007-10-24 21:05:51 -0300347 state->is_qam_locked = 0;
348 state->qam_state = 0;
Steven Toth89885552007-07-28 19:34:52 -0300349 return 0;
350}
351
Michael Krufky2b032382007-12-13 10:04:10 -0300352#define S5H1409_VSB_IF_FREQ 5380
353#define S5H1409_QAM_IF_FREQ state->config->qam_if
354
Steven Toth89885552007-07-28 19:34:52 -0300355static int s5h1409_set_if_freq(struct dvb_frontend* fe, int KHz)
356{
357 struct s5h1409_state* state = fe->demodulator_priv;
Steven Toth89885552007-07-28 19:34:52 -0300358
359 dprintk("%s(%d KHz)\n", __FUNCTION__, KHz);
360
Michael Krufky6b7daa82007-12-13 10:11:49 -0300361 switch (KHz) {
362 case 4000:
Steven Tothdd7d5012007-10-24 21:05:51 -0300363 s5h1409_writereg(state, 0x87, 0x014b);
364 s5h1409_writereg(state, 0x88, 0x0cb5);
365 s5h1409_writereg(state, 0x89, 0x03e2);
Michael Krufky6b7daa82007-12-13 10:11:49 -0300366 break;
367 case 5380:
368 case 44000:
369 default:
370 s5h1409_writereg(state, 0x87, 0x01be);
371 s5h1409_writereg(state, 0x88, 0x0436);
372 s5h1409_writereg(state, 0x89, 0x054d);
373 break;
Steven Toth89885552007-07-28 19:34:52 -0300374 }
Michael Krufky6b7daa82007-12-13 10:11:49 -0300375 state->if_freq = KHz;
Steven Toth89885552007-07-28 19:34:52 -0300376
Michael Krufky6b7daa82007-12-13 10:11:49 -0300377 return 0;
Steven Toth89885552007-07-28 19:34:52 -0300378}
379
380static int s5h1409_set_spectralinversion(struct dvb_frontend* fe, int inverted)
381{
382 struct s5h1409_state* state = fe->demodulator_priv;
383
Steven Tothdd7d5012007-10-24 21:05:51 -0300384 dprintk("%s(%d)\n", __FUNCTION__, inverted);
Steven Toth89885552007-07-28 19:34:52 -0300385
386 if(inverted == 1)
387 return s5h1409_writereg(state, 0x1b, 0x1101); /* Inverted */
388 else
389 return s5h1409_writereg(state, 0x1b, 0x0110); /* Normal */
390}
391
Michael Krufky3873dd02007-07-28 20:02:55 -0300392static int s5h1409_enable_modulation(struct dvb_frontend* fe,
393 fe_modulation_t m)
Steven Toth89885552007-07-28 19:34:52 -0300394{
395 struct s5h1409_state* state = fe->demodulator_priv;
396
397 dprintk("%s(0x%08x)\n", __FUNCTION__, m);
398
399 switch(m) {
400 case VSB_8:
401 dprintk("%s() VSB_8\n", __FUNCTION__);
Michael Krufky2b032382007-12-13 10:04:10 -0300402 if (state->if_freq != S5H1409_VSB_IF_FREQ)
403 s5h1409_set_if_freq(fe, S5H1409_VSB_IF_FREQ);
Steven Toth89885552007-07-28 19:34:52 -0300404 s5h1409_writereg(state, 0xf4, 0);
405 break;
406 case QAM_64:
Steven Toth89885552007-07-28 19:34:52 -0300407 case QAM_256:
Steven Tothdd7d5012007-10-24 21:05:51 -0300408 dprintk("%s() QAM_AUTO (64/256)\n", __FUNCTION__);
Michael Krufky2b032382007-12-13 10:04:10 -0300409 if (state->if_freq != S5H1409_QAM_IF_FREQ)
410 s5h1409_set_if_freq(fe, S5H1409_QAM_IF_FREQ);
Steven Toth89885552007-07-28 19:34:52 -0300411 s5h1409_writereg(state, 0xf4, 1);
Steven Tothdd7d5012007-10-24 21:05:51 -0300412 s5h1409_writereg(state, 0x85, 0x110);
Steven Toth89885552007-07-28 19:34:52 -0300413 break;
414 default:
415 dprintk("%s() Invalid modulation\n", __FUNCTION__);
416 return -EINVAL;
417 }
418
419 state->current_modulation = m;
420 s5h1409_softreset(fe);
421
422 return 0;
423}
424
425static int s5h1409_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
426{
427 struct s5h1409_state* state = fe->demodulator_priv;
428
429 dprintk("%s(%d)\n", __FUNCTION__, enable);
430
431 if (enable)
432 return s5h1409_writereg(state, 0xf3, 1);
433 else
434 return s5h1409_writereg(state, 0xf3, 0);
435}
436
437static int s5h1409_set_gpio(struct dvb_frontend* fe, int enable)
438{
439 struct s5h1409_state* state = fe->demodulator_priv;
440
441 dprintk("%s(%d)\n", __FUNCTION__, enable);
442
443 if (enable)
444 return s5h1409_writereg(state, 0xe3, 0x1100);
445 else
Steven Tothdd7d5012007-10-24 21:05:51 -0300446 return s5h1409_writereg(state, 0xe3, 0x1000);
Steven Toth89885552007-07-28 19:34:52 -0300447}
448
449static int s5h1409_sleep(struct dvb_frontend* fe, int enable)
450{
451 struct s5h1409_state* state = fe->demodulator_priv;
452
453 dprintk("%s(%d)\n", __FUNCTION__, enable);
454
455 return s5h1409_writereg(state, 0xf2, enable);
456}
457
458static int s5h1409_register_reset(struct dvb_frontend* fe)
459{
460 struct s5h1409_state* state = fe->demodulator_priv;
461
462 dprintk("%s()\n", __FUNCTION__);
463
464 return s5h1409_writereg(state, 0xfa, 0);
465}
466
Steven Tothdd7d5012007-10-24 21:05:51 -0300467static void s5h1409_set_qam_amhum_mode(struct dvb_frontend *fe)
468{
469 struct s5h1409_state *state = fe->demodulator_priv;
470 u16 reg;
471
472 if (state->is_qam_locked)
473 return;
474
475 /* QAM EQ lock check */
476 reg = s5h1409_readreg(state, 0xf0);
477
478 if ((reg >> 13) & 0x1) {
479
480 state->is_qam_locked = 1;
481 reg &= 0xff;
482
483 s5h1409_writereg(state, 0x96, 0x00c);
484 if ((reg < 0x38) || (reg > 0x68) ) {
485 s5h1409_writereg(state, 0x93, 0x3332);
486 s5h1409_writereg(state, 0x9e, 0x2c37);
487 } else {
488 s5h1409_writereg(state, 0x93, 0x3130);
489 s5h1409_writereg(state, 0x9e, 0x2836);
490 }
491
492 } else {
493 s5h1409_writereg(state, 0x96, 0x0008);
494 s5h1409_writereg(state, 0x93, 0x3332);
495 s5h1409_writereg(state, 0x9e, 0x2c37);
496 }
497}
498
499static void s5h1409_set_qam_interleave_mode(struct dvb_frontend *fe)
500{
501 struct s5h1409_state *state = fe->demodulator_priv;
502 u16 reg, reg1, reg2;
503
504 reg = s5h1409_readreg(state, 0xf1);
505
506 /* Master lock */
507 if ((reg >> 15) & 0x1) {
508 if (state->qam_state != 2) {
509 state->qam_state = 2;
510 reg1 = s5h1409_readreg(state, 0xb2);
511 reg2 = s5h1409_readreg(state, 0xad);
512
513 s5h1409_writereg(state, 0x96, 0x20);
514 s5h1409_writereg(state, 0xad,
515 ( ((reg1 & 0xf000) >> 4) | (reg2 & 0xf0ff)) );
516 s5h1409_writereg(state, 0xab, 0x1100);
517 }
518 } else {
519 if (state->qam_state != 1) {
520 state->qam_state = 1;
521 s5h1409_writereg(state, 0x96, 0x08);
522 s5h1409_writereg(state, 0xab, 0x1101);
523 }
524 }
525}
526
Steven Toth89885552007-07-28 19:34:52 -0300527/* Talk to the demod, set the FEC, GUARD, QAM settings etc */
Michael Krufky3873dd02007-07-28 20:02:55 -0300528static int s5h1409_set_frontend (struct dvb_frontend* fe,
529 struct dvb_frontend_parameters *p)
Steven Toth89885552007-07-28 19:34:52 -0300530{
531 struct s5h1409_state* state = fe->demodulator_priv;
532
533 dprintk("%s(frequency=%d)\n", __FUNCTION__, p->frequency);
534
535 s5h1409_softreset(fe);
536
537 state->current_frequency = p->frequency;
538
539 s5h1409_enable_modulation(fe, p->u.vsb.modulation);
540
Steven Tothdd7d5012007-10-24 21:05:51 -0300541 /* Allow the demod to settle */
542 msleep(100);
543
Steven Toth89885552007-07-28 19:34:52 -0300544 if (fe->ops.tuner_ops.set_params) {
545 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 1);
546 fe->ops.tuner_ops.set_params(fe, p);
547 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
548 }
549
Steven Tothdd7d5012007-10-24 21:05:51 -0300550 /* Optimize the demod for QAM */
551 if (p->u.vsb.modulation != VSB_8) {
552 s5h1409_set_qam_amhum_mode(fe);
553 s5h1409_set_qam_interleave_mode(fe);
554 }
555
Steven Toth89885552007-07-28 19:34:52 -0300556 return 0;
557}
558
559/* Reset the demod hardware and reset all of the configuration registers
560 to a default state. */
561static int s5h1409_init (struct dvb_frontend* fe)
562{
563 int i;
564
565 struct s5h1409_state* state = fe->demodulator_priv;
566 dprintk("%s()\n", __FUNCTION__);
567
568 s5h1409_sleep(fe, 0);
569 s5h1409_register_reset(fe);
570
Michael Krufkya45c9272007-03-21 12:03:23 -0300571 for (i=0; i < ARRAY_SIZE(init_tab); i++)
Steven Toth89885552007-07-28 19:34:52 -0300572 s5h1409_writereg(state, init_tab[i].reg, init_tab[i].data);
573
574 /* The datasheet says that after initialisation, VSB is default */
575 state->current_modulation = VSB_8;
576
577 if (state->config->output_mode == S5H1409_SERIAL_OUTPUT)
578 s5h1409_writereg(state, 0xab, 0x100); /* Serial */
579 else
580 s5h1409_writereg(state, 0xab, 0x0); /* Parallel */
581
582 s5h1409_set_spectralinversion(fe, state->config->inversion);
Michael Krufky2b032382007-12-13 10:04:10 -0300583 s5h1409_set_if_freq(fe, state->if_freq);
Steven Toth89885552007-07-28 19:34:52 -0300584 s5h1409_set_gpio(fe, state->config->gpio);
585 s5h1409_softreset(fe);
586
Steven Tothdd7d5012007-10-24 21:05:51 -0300587 /* Note: Leaving the I2C gate closed. */
588 s5h1409_i2c_gate_ctrl(fe, 0);
Steven Toth89885552007-07-28 19:34:52 -0300589
590 return 0;
591}
592
593static int s5h1409_read_status(struct dvb_frontend* fe, fe_status_t* status)
594{
595 struct s5h1409_state* state = fe->demodulator_priv;
596 u16 reg;
597 u32 tuner_status = 0;
598
599 *status = 0;
600
601 /* Get the demodulator status */
602 reg = s5h1409_readreg(state, 0xf1);
603 if(reg & 0x1000)
604 *status |= FE_HAS_VITERBI;
605 if(reg & 0x8000)
606 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
607
608 switch(state->config->status_mode) {
609 case S5H1409_DEMODLOCKING:
610 if (*status & FE_HAS_VITERBI)
611 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
612 break;
613 case S5H1409_TUNERLOCKING:
614 /* Get the tuner status */
615 if (fe->ops.tuner_ops.get_status) {
Michael Krufky3873dd02007-07-28 20:02:55 -0300616 if (fe->ops.i2c_gate_ctrl)
617 fe->ops.i2c_gate_ctrl(fe, 1);
Steven Toth89885552007-07-28 19:34:52 -0300618
619 fe->ops.tuner_ops.get_status(fe, &tuner_status);
620
Michael Krufky3873dd02007-07-28 20:02:55 -0300621 if (fe->ops.i2c_gate_ctrl)
622 fe->ops.i2c_gate_ctrl(fe, 0);
Steven Toth89885552007-07-28 19:34:52 -0300623 }
624 if (tuner_status)
625 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
626 break;
627 }
628
629 dprintk("%s() status 0x%08x\n", __FUNCTION__, *status);
630
631 return 0;
632}
633
634static int s5h1409_qam256_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
635{
636 int i, ret = -EINVAL;
637 dprintk("%s()\n", __FUNCTION__);
638
Michael Krufkya45c9272007-03-21 12:03:23 -0300639 for (i=0; i < ARRAY_SIZE(qam256_snr_tab); i++) {
Steven Toth89885552007-07-28 19:34:52 -0300640 if (v < qam256_snr_tab[i].val) {
641 *snr = qam256_snr_tab[i].data;
642 ret = 0;
643 break;
644 }
645 }
646 return ret;
647}
648
649static int s5h1409_qam64_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
650{
651 int i, ret = -EINVAL;
652 dprintk("%s()\n", __FUNCTION__);
653
Michael Krufkya45c9272007-03-21 12:03:23 -0300654 for (i=0; i < ARRAY_SIZE(qam64_snr_tab); i++) {
Steven Toth89885552007-07-28 19:34:52 -0300655 if (v < qam64_snr_tab[i].val) {
656 *snr = qam64_snr_tab[i].data;
657 ret = 0;
658 break;
659 }
660 }
661 return ret;
662}
663
664static int s5h1409_vsb_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
665{
666 int i, ret = -EINVAL;
667 dprintk("%s()\n", __FUNCTION__);
668
Michael Krufkya45c9272007-03-21 12:03:23 -0300669 for (i=0; i < ARRAY_SIZE(vsb_snr_tab); i++) {
Steven Toth89885552007-07-28 19:34:52 -0300670 if (v > vsb_snr_tab[i].val) {
671 *snr = vsb_snr_tab[i].data;
672 ret = 0;
673 break;
674 }
675 }
676 dprintk("%s() snr=%d\n", __FUNCTION__, *snr);
677 return ret;
678}
679
680static int s5h1409_read_snr(struct dvb_frontend* fe, u16* snr)
681{
682 struct s5h1409_state* state = fe->demodulator_priv;
683 u16 reg;
684 dprintk("%s()\n", __FUNCTION__);
685
Steven Toth89885552007-07-28 19:34:52 -0300686 switch(state->current_modulation) {
687 case QAM_64:
Steven Toth23003172007-12-12 22:14:00 -0300688 reg = s5h1409_readreg(state, 0xf0) & 0xff;
Steven Toth89885552007-07-28 19:34:52 -0300689 return s5h1409_qam64_lookup_snr(fe, snr, reg);
690 case QAM_256:
Steven Toth23003172007-12-12 22:14:00 -0300691 reg = s5h1409_readreg(state, 0xf0) & 0xff;
Steven Toth89885552007-07-28 19:34:52 -0300692 return s5h1409_qam256_lookup_snr(fe, snr, reg);
693 case VSB_8:
Steven Toth23003172007-12-12 22:14:00 -0300694 reg = s5h1409_readreg(state, 0xf1) & 0x3ff;
Steven Toth89885552007-07-28 19:34:52 -0300695 return s5h1409_vsb_lookup_snr(fe, snr, reg);
696 default:
697 break;
698 }
699
700 return -EINVAL;
701}
702
Michael Krufky3873dd02007-07-28 20:02:55 -0300703static int s5h1409_read_signal_strength(struct dvb_frontend* fe,
704 u16* signal_strength)
Steven Toth89885552007-07-28 19:34:52 -0300705{
706 return s5h1409_read_snr(fe, signal_strength);
707}
708
709static int s5h1409_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
710{
711 struct s5h1409_state* state = fe->demodulator_priv;
712
713 *ucblocks = s5h1409_readreg(state, 0xb5);
714
715 return 0;
716}
717
718static int s5h1409_read_ber(struct dvb_frontend* fe, u32* ber)
719{
720 return s5h1409_read_ucblocks(fe, ber);
721}
722
Michael Krufky3873dd02007-07-28 20:02:55 -0300723static int s5h1409_get_frontend(struct dvb_frontend* fe,
724 struct dvb_frontend_parameters *p)
Steven Toth89885552007-07-28 19:34:52 -0300725{
726 struct s5h1409_state* state = fe->demodulator_priv;
727
728 p->frequency = state->current_frequency;
729 p->u.vsb.modulation = state->current_modulation;
730
731 return 0;
732}
733
Michael Krufky3873dd02007-07-28 20:02:55 -0300734static int s5h1409_get_tune_settings(struct dvb_frontend* fe,
735 struct dvb_frontend_tune_settings *tune)
Steven Toth89885552007-07-28 19:34:52 -0300736{
737 tune->min_delay_ms = 1000;
738 return 0;
739}
740
741static void s5h1409_release(struct dvb_frontend* fe)
742{
743 struct s5h1409_state* state = fe->demodulator_priv;
744 kfree(state);
745}
746
747static struct dvb_frontend_ops s5h1409_ops;
748
749struct dvb_frontend* s5h1409_attach(const struct s5h1409_config* config,
750 struct i2c_adapter* i2c)
751{
752 struct s5h1409_state* state = NULL;
Steven Totha57ed8a2008-01-10 03:43:11 -0300753 u16 reg;
Steven Toth89885552007-07-28 19:34:52 -0300754
755 /* allocate memory for the internal state */
756 state = kmalloc(sizeof(struct s5h1409_state), GFP_KERNEL);
757 if (state == NULL)
758 goto error;
759
760 /* setup the state */
761 state->config = config;
762 state->i2c = i2c;
763 state->current_modulation = 0;
Michael Krufky2b032382007-12-13 10:04:10 -0300764 state->if_freq = S5H1409_VSB_IF_FREQ;
Steven Toth89885552007-07-28 19:34:52 -0300765
766 /* check if the demod exists */
Steven Totha57ed8a2008-01-10 03:43:11 -0300767 reg = s5h1409_readreg(state, 0x04);
768 if ((reg != 0x0066) && (reg != 0x007f))
Steven Toth89885552007-07-28 19:34:52 -0300769 goto error;
770
771 /* create dvb_frontend */
Michael Krufky3873dd02007-07-28 20:02:55 -0300772 memcpy(&state->frontend.ops, &s5h1409_ops,
773 sizeof(struct dvb_frontend_ops));
Steven Toth89885552007-07-28 19:34:52 -0300774 state->frontend.demodulator_priv = state;
775
Steven Totha57ed8a2008-01-10 03:43:11 -0300776 if (s5h1409_init(&state->frontend) != 0) {
777 printk(KERN_ERR "%s: Failed to initialize correctly\n",
778 __FUNCTION__);
779 goto error;
780 }
781
Steven Toth89885552007-07-28 19:34:52 -0300782 /* Note: Leaving the I2C gate open here. */
Steven Totha57ed8a2008-01-10 03:43:11 -0300783 s5h1409_i2c_gate_ctrl(&state->frontend, 1);
Steven Toth89885552007-07-28 19:34:52 -0300784
785 return &state->frontend;
786
787error:
788 kfree(state);
789 return NULL;
790}
791
792static struct dvb_frontend_ops s5h1409_ops = {
793
794 .info = {
795 .name = "Samsung S5H1409 QAM/8VSB Frontend",
796 .type = FE_ATSC,
797 .frequency_min = 54000000,
798 .frequency_max = 858000000,
799 .frequency_stepsize = 62500,
800 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
801 },
802
803 .init = s5h1409_init,
804 .i2c_gate_ctrl = s5h1409_i2c_gate_ctrl,
805 .set_frontend = s5h1409_set_frontend,
806 .get_frontend = s5h1409_get_frontend,
807 .get_tune_settings = s5h1409_get_tune_settings,
808 .read_status = s5h1409_read_status,
809 .read_ber = s5h1409_read_ber,
810 .read_signal_strength = s5h1409_read_signal_strength,
811 .read_snr = s5h1409_read_snr,
812 .read_ucblocks = s5h1409_read_ucblocks,
813 .release = s5h1409_release,
814};
815
816module_param(debug, int, 0644);
817MODULE_PARM_DESC(debug, "Enable verbose debug messages");
818
819MODULE_DESCRIPTION("Samsung S5H1409 QAM-B/ATSC Demodulator driver");
820MODULE_AUTHOR("Steven Toth");
821MODULE_LICENSE("GPL");
822
823EXPORT_SYMBOL(s5h1409_attach);
Michael Krufky3873dd02007-07-28 20:02:55 -0300824
825/*
826 * Local variables:
827 * c-basic-offset: 8
828 */