Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / media / dvb-frontends / s5h1409.c
blob3089cc174a6f5bd75a1b468da60735ed602036ee
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 Samsung S5H1409 VSB/QAM demodulator driver
5 Copyright (C) 2006 Steven Toth <stoth@linuxtv.org>
8 */
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/string.h>
14 #include <linux/slab.h>
15 #include <linux/delay.h>
16 #include <media/dvb_frontend.h>
17 #include "s5h1409.h"
19 struct s5h1409_state {
21 struct i2c_adapter *i2c;
23 /* configuration settings */
24 const struct s5h1409_config *config;
26 struct dvb_frontend frontend;
28 /* previous uncorrected block counter */
29 enum fe_modulation current_modulation;
31 u32 current_frequency;
32 int if_freq;
34 u32 is_qam_locked;
36 /* QAM tuning state goes through the following state transitions */
37 #define QAM_STATE_UNTUNED 0
38 #define QAM_STATE_TUNING_STARTED 1
39 #define QAM_STATE_INTERLEAVE_SET 2
40 #define QAM_STATE_QAM_OPTIMIZED_L1 3
41 #define QAM_STATE_QAM_OPTIMIZED_L2 4
42 #define QAM_STATE_QAM_OPTIMIZED_L3 5
43 u8 qam_state;
46 static int debug;
47 module_param(debug, int, 0644);
48 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
50 #define dprintk if (debug) printk
52 /* Register values to initialise the demod, this will set VSB by default */
53 static struct init_tab {
54 u8 reg;
55 u16 data;
56 } init_tab[] = {
57 { 0x00, 0x0071, },
58 { 0x01, 0x3213, },
59 { 0x09, 0x0025, },
60 { 0x1c, 0x001d, },
61 { 0x1f, 0x002d, },
62 { 0x20, 0x001d, },
63 { 0x22, 0x0022, },
64 { 0x23, 0x0020, },
65 { 0x29, 0x110f, },
66 { 0x2a, 0x10b4, },
67 { 0x2b, 0x10ae, },
68 { 0x2c, 0x0031, },
69 { 0x31, 0x010d, },
70 { 0x32, 0x0100, },
71 { 0x44, 0x0510, },
72 { 0x54, 0x0104, },
73 { 0x58, 0x2222, },
74 { 0x59, 0x1162, },
75 { 0x5a, 0x3211, },
76 { 0x5d, 0x0370, },
77 { 0x5e, 0x0296, },
78 { 0x61, 0x0010, },
79 { 0x63, 0x4a00, },
80 { 0x65, 0x0800, },
81 { 0x71, 0x0003, },
82 { 0x72, 0x0470, },
83 { 0x81, 0x0002, },
84 { 0x82, 0x0600, },
85 { 0x86, 0x0002, },
86 { 0x8a, 0x2c38, },
87 { 0x8b, 0x2a37, },
88 { 0x92, 0x302f, },
89 { 0x93, 0x3332, },
90 { 0x96, 0x000c, },
91 { 0x99, 0x0101, },
92 { 0x9c, 0x2e37, },
93 { 0x9d, 0x2c37, },
94 { 0x9e, 0x2c37, },
95 { 0xab, 0x0100, },
96 { 0xac, 0x1003, },
97 { 0xad, 0x103f, },
98 { 0xe2, 0x0100, },
99 { 0xe3, 0x1000, },
100 { 0x28, 0x1010, },
101 { 0xb1, 0x000e, },
104 /* VSB SNR lookup table */
105 static struct vsb_snr_tab {
106 u16 val;
107 u16 data;
108 } vsb_snr_tab[] = {
109 { 924, 300, },
110 { 923, 300, },
111 { 918, 295, },
112 { 915, 290, },
113 { 911, 285, },
114 { 906, 280, },
115 { 901, 275, },
116 { 896, 270, },
117 { 891, 265, },
118 { 885, 260, },
119 { 879, 255, },
120 { 873, 250, },
121 { 864, 245, },
122 { 858, 240, },
123 { 850, 235, },
124 { 841, 230, },
125 { 832, 225, },
126 { 823, 220, },
127 { 812, 215, },
128 { 802, 210, },
129 { 788, 205, },
130 { 778, 200, },
131 { 767, 195, },
132 { 753, 190, },
133 { 740, 185, },
134 { 725, 180, },
135 { 707, 175, },
136 { 689, 170, },
137 { 671, 165, },
138 { 656, 160, },
139 { 637, 155, },
140 { 616, 150, },
141 { 542, 145, },
142 { 519, 140, },
143 { 507, 135, },
144 { 497, 130, },
145 { 492, 125, },
146 { 474, 120, },
147 { 300, 111, },
148 { 0, 0, },
151 /* QAM64 SNR lookup table */
152 static struct qam64_snr_tab {
153 u16 val;
154 u16 data;
155 } qam64_snr_tab[] = {
156 { 1, 0, },
157 { 12, 300, },
158 { 15, 290, },
159 { 18, 280, },
160 { 22, 270, },
161 { 23, 268, },
162 { 24, 266, },
163 { 25, 264, },
164 { 27, 262, },
165 { 28, 260, },
166 { 29, 258, },
167 { 30, 256, },
168 { 32, 254, },
169 { 33, 252, },
170 { 34, 250, },
171 { 35, 249, },
172 { 36, 248, },
173 { 37, 247, },
174 { 38, 246, },
175 { 39, 245, },
176 { 40, 244, },
177 { 41, 243, },
178 { 42, 241, },
179 { 43, 240, },
180 { 44, 239, },
181 { 45, 238, },
182 { 46, 237, },
183 { 47, 236, },
184 { 48, 235, },
185 { 49, 234, },
186 { 50, 233, },
187 { 51, 232, },
188 { 52, 231, },
189 { 53, 230, },
190 { 55, 229, },
191 { 56, 228, },
192 { 57, 227, },
193 { 58, 226, },
194 { 59, 225, },
195 { 60, 224, },
196 { 62, 223, },
197 { 63, 222, },
198 { 65, 221, },
199 { 66, 220, },
200 { 68, 219, },
201 { 69, 218, },
202 { 70, 217, },
203 { 72, 216, },
204 { 73, 215, },
205 { 75, 214, },
206 { 76, 213, },
207 { 78, 212, },
208 { 80, 211, },
209 { 81, 210, },
210 { 83, 209, },
211 { 84, 208, },
212 { 85, 207, },
213 { 87, 206, },
214 { 89, 205, },
215 { 91, 204, },
216 { 93, 203, },
217 { 95, 202, },
218 { 96, 201, },
219 { 104, 200, },
220 { 255, 0, },
223 /* QAM256 SNR lookup table */
224 static struct qam256_snr_tab {
225 u16 val;
226 u16 data;
227 } qam256_snr_tab[] = {
228 { 1, 0, },
229 { 12, 400, },
230 { 13, 390, },
231 { 15, 380, },
232 { 17, 360, },
233 { 19, 350, },
234 { 22, 348, },
235 { 23, 346, },
236 { 24, 344, },
237 { 25, 342, },
238 { 26, 340, },
239 { 27, 336, },
240 { 28, 334, },
241 { 29, 332, },
242 { 30, 330, },
243 { 31, 328, },
244 { 32, 326, },
245 { 33, 325, },
246 { 34, 322, },
247 { 35, 320, },
248 { 37, 318, },
249 { 39, 316, },
250 { 40, 314, },
251 { 41, 312, },
252 { 42, 310, },
253 { 43, 308, },
254 { 46, 306, },
255 { 47, 304, },
256 { 49, 302, },
257 { 51, 300, },
258 { 53, 298, },
259 { 54, 297, },
260 { 55, 296, },
261 { 56, 295, },
262 { 57, 294, },
263 { 59, 293, },
264 { 60, 292, },
265 { 61, 291, },
266 { 63, 290, },
267 { 64, 289, },
268 { 65, 288, },
269 { 66, 287, },
270 { 68, 286, },
271 { 69, 285, },
272 { 71, 284, },
273 { 72, 283, },
274 { 74, 282, },
275 { 75, 281, },
276 { 76, 280, },
277 { 77, 279, },
278 { 78, 278, },
279 { 81, 277, },
280 { 83, 276, },
281 { 84, 275, },
282 { 86, 274, },
283 { 87, 273, },
284 { 89, 272, },
285 { 90, 271, },
286 { 92, 270, },
287 { 93, 269, },
288 { 95, 268, },
289 { 96, 267, },
290 { 98, 266, },
291 { 100, 265, },
292 { 102, 264, },
293 { 104, 263, },
294 { 105, 262, },
295 { 106, 261, },
296 { 110, 260, },
297 { 255, 0, },
300 /* 8 bit registers, 16 bit values */
301 static int s5h1409_writereg(struct s5h1409_state *state, u8 reg, u16 data)
303 int ret;
304 u8 buf[] = { reg, data >> 8, data & 0xff };
306 struct i2c_msg msg = { .addr = state->config->demod_address,
307 .flags = 0, .buf = buf, .len = 3 };
309 ret = i2c_transfer(state->i2c, &msg, 1);
311 if (ret != 1)
312 printk(KERN_ERR "%s: error (reg == 0x%02x, val == 0x%04x, ret == %i)\n",
313 __func__, reg, data, ret);
315 return (ret != 1) ? -1 : 0;
318 static u16 s5h1409_readreg(struct s5h1409_state *state, u8 reg)
320 int ret;
321 u8 b0[] = { reg };
322 u8 b1[] = { 0, 0 };
324 struct i2c_msg msg[] = {
325 { .addr = state->config->demod_address, .flags = 0,
326 .buf = b0, .len = 1 },
327 { .addr = state->config->demod_address, .flags = I2C_M_RD,
328 .buf = b1, .len = 2 } };
330 ret = i2c_transfer(state->i2c, msg, 2);
332 if (ret != 2)
333 printk("%s: readreg error (ret == %i)\n", __func__, ret);
334 return (b1[0] << 8) | b1[1];
337 static int s5h1409_softreset(struct dvb_frontend *fe)
339 struct s5h1409_state *state = fe->demodulator_priv;
341 dprintk("%s()\n", __func__);
343 s5h1409_writereg(state, 0xf5, 0);
344 s5h1409_writereg(state, 0xf5, 1);
345 state->is_qam_locked = 0;
346 state->qam_state = QAM_STATE_UNTUNED;
347 return 0;
350 #define S5H1409_VSB_IF_FREQ 5380
351 #define S5H1409_QAM_IF_FREQ (state->config->qam_if)
353 static int s5h1409_set_if_freq(struct dvb_frontend *fe, int KHz)
355 struct s5h1409_state *state = fe->demodulator_priv;
357 dprintk("%s(%d KHz)\n", __func__, KHz);
359 switch (KHz) {
360 case 4000:
361 s5h1409_writereg(state, 0x87, 0x014b);
362 s5h1409_writereg(state, 0x88, 0x0cb5);
363 s5h1409_writereg(state, 0x89, 0x03e2);
364 break;
365 case 5380:
366 case 44000:
367 default:
368 s5h1409_writereg(state, 0x87, 0x01be);
369 s5h1409_writereg(state, 0x88, 0x0436);
370 s5h1409_writereg(state, 0x89, 0x054d);
371 break;
373 state->if_freq = KHz;
375 return 0;
378 static int s5h1409_set_spectralinversion(struct dvb_frontend *fe, int inverted)
380 struct s5h1409_state *state = fe->demodulator_priv;
382 dprintk("%s(%d)\n", __func__, inverted);
384 if (inverted == 1)
385 return s5h1409_writereg(state, 0x1b, 0x1101); /* Inverted */
386 else
387 return s5h1409_writereg(state, 0x1b, 0x0110); /* Normal */
390 static int s5h1409_enable_modulation(struct dvb_frontend *fe,
391 enum fe_modulation m)
393 struct s5h1409_state *state = fe->demodulator_priv;
395 dprintk("%s(0x%08x)\n", __func__, m);
397 switch (m) {
398 case VSB_8:
399 dprintk("%s() VSB_8\n", __func__);
400 if (state->if_freq != S5H1409_VSB_IF_FREQ)
401 s5h1409_set_if_freq(fe, S5H1409_VSB_IF_FREQ);
402 s5h1409_writereg(state, 0xf4, 0);
403 break;
404 case QAM_64:
405 case QAM_256:
406 case QAM_AUTO:
407 dprintk("%s() QAM_AUTO (64/256)\n", __func__);
408 if (state->if_freq != S5H1409_QAM_IF_FREQ)
409 s5h1409_set_if_freq(fe, S5H1409_QAM_IF_FREQ);
410 s5h1409_writereg(state, 0xf4, 1);
411 s5h1409_writereg(state, 0x85, 0x110);
412 break;
413 default:
414 dprintk("%s() Invalid modulation\n", __func__);
415 return -EINVAL;
418 state->current_modulation = m;
419 s5h1409_softreset(fe);
421 return 0;
424 static int s5h1409_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
426 struct s5h1409_state *state = fe->demodulator_priv;
428 dprintk("%s(%d)\n", __func__, enable);
430 if (enable)
431 return s5h1409_writereg(state, 0xf3, 1);
432 else
433 return s5h1409_writereg(state, 0xf3, 0);
436 static int s5h1409_set_gpio(struct dvb_frontend *fe, int enable)
438 struct s5h1409_state *state = fe->demodulator_priv;
440 dprintk("%s(%d)\n", __func__, enable);
442 if (enable)
443 return s5h1409_writereg(state, 0xe3,
444 s5h1409_readreg(state, 0xe3) | 0x1100);
445 else
446 return s5h1409_writereg(state, 0xe3,
447 s5h1409_readreg(state, 0xe3) & 0xfeff);
450 static int s5h1409_sleep(struct dvb_frontend *fe, int enable)
452 struct s5h1409_state *state = fe->demodulator_priv;
454 dprintk("%s(%d)\n", __func__, enable);
456 return s5h1409_writereg(state, 0xf2, enable);
459 static int s5h1409_register_reset(struct dvb_frontend *fe)
461 struct s5h1409_state *state = fe->demodulator_priv;
463 dprintk("%s()\n", __func__);
465 return s5h1409_writereg(state, 0xfa, 0);
468 static void s5h1409_set_qam_amhum_mode(struct dvb_frontend *fe)
470 struct s5h1409_state *state = fe->demodulator_priv;
471 u16 reg;
473 if (state->qam_state < QAM_STATE_INTERLEAVE_SET) {
474 /* We should not perform amhum optimization until
475 the interleave mode has been configured */
476 return;
479 if (state->qam_state == QAM_STATE_QAM_OPTIMIZED_L3) {
480 /* We've already reached the maximum optimization level, so
481 don't bother banging on the status registers */
482 return;
485 /* QAM EQ lock check */
486 reg = s5h1409_readreg(state, 0xf0);
488 if ((reg >> 13) & 0x1) {
489 reg &= 0xff;
491 s5h1409_writereg(state, 0x96, 0x000c);
492 if (reg < 0x68) {
493 if (state->qam_state < QAM_STATE_QAM_OPTIMIZED_L3) {
494 dprintk("%s() setting QAM state to OPT_L3\n",
495 __func__);
496 s5h1409_writereg(state, 0x93, 0x3130);
497 s5h1409_writereg(state, 0x9e, 0x2836);
498 state->qam_state = QAM_STATE_QAM_OPTIMIZED_L3;
500 } else {
501 if (state->qam_state < QAM_STATE_QAM_OPTIMIZED_L2) {
502 dprintk("%s() setting QAM state to OPT_L2\n",
503 __func__);
504 s5h1409_writereg(state, 0x93, 0x3332);
505 s5h1409_writereg(state, 0x9e, 0x2c37);
506 state->qam_state = QAM_STATE_QAM_OPTIMIZED_L2;
510 } else {
511 if (state->qam_state < QAM_STATE_QAM_OPTIMIZED_L1) {
512 dprintk("%s() setting QAM state to OPT_L1\n", __func__);
513 s5h1409_writereg(state, 0x96, 0x0008);
514 s5h1409_writereg(state, 0x93, 0x3332);
515 s5h1409_writereg(state, 0x9e, 0x2c37);
516 state->qam_state = QAM_STATE_QAM_OPTIMIZED_L1;
521 static void s5h1409_set_qam_amhum_mode_legacy(struct dvb_frontend *fe)
523 struct s5h1409_state *state = fe->demodulator_priv;
524 u16 reg;
526 if (state->is_qam_locked)
527 return;
529 /* QAM EQ lock check */
530 reg = s5h1409_readreg(state, 0xf0);
532 if ((reg >> 13) & 0x1) {
534 state->is_qam_locked = 1;
535 reg &= 0xff;
537 s5h1409_writereg(state, 0x96, 0x00c);
538 if ((reg < 0x38) || (reg > 0x68)) {
539 s5h1409_writereg(state, 0x93, 0x3332);
540 s5h1409_writereg(state, 0x9e, 0x2c37);
541 } else {
542 s5h1409_writereg(state, 0x93, 0x3130);
543 s5h1409_writereg(state, 0x9e, 0x2836);
546 } else {
547 s5h1409_writereg(state, 0x96, 0x0008);
548 s5h1409_writereg(state, 0x93, 0x3332);
549 s5h1409_writereg(state, 0x9e, 0x2c37);
553 static void s5h1409_set_qam_interleave_mode(struct dvb_frontend *fe)
555 struct s5h1409_state *state = fe->demodulator_priv;
556 u16 reg, reg1, reg2;
558 if (state->qam_state >= QAM_STATE_INTERLEAVE_SET) {
559 /* We've done the optimization already */
560 return;
563 reg = s5h1409_readreg(state, 0xf1);
565 /* Master lock */
566 if ((reg >> 15) & 0x1) {
567 if (state->qam_state == QAM_STATE_UNTUNED ||
568 state->qam_state == QAM_STATE_TUNING_STARTED) {
569 dprintk("%s() setting QAM state to INTERLEAVE_SET\n",
570 __func__);
571 reg1 = s5h1409_readreg(state, 0xb2);
572 reg2 = s5h1409_readreg(state, 0xad);
574 s5h1409_writereg(state, 0x96, 0x0020);
575 s5h1409_writereg(state, 0xad,
576 (((reg1 & 0xf000) >> 4) | (reg2 & 0xf0ff)));
577 state->qam_state = QAM_STATE_INTERLEAVE_SET;
579 } else {
580 if (state->qam_state == QAM_STATE_UNTUNED) {
581 dprintk("%s() setting QAM state to TUNING_STARTED\n",
582 __func__);
583 s5h1409_writereg(state, 0x96, 0x08);
584 s5h1409_writereg(state, 0xab,
585 s5h1409_readreg(state, 0xab) | 0x1001);
586 state->qam_state = QAM_STATE_TUNING_STARTED;
591 static void s5h1409_set_qam_interleave_mode_legacy(struct dvb_frontend *fe)
593 struct s5h1409_state *state = fe->demodulator_priv;
594 u16 reg, reg1, reg2;
596 reg = s5h1409_readreg(state, 0xf1);
598 /* Master lock */
599 if ((reg >> 15) & 0x1) {
600 if (state->qam_state != 2) {
601 state->qam_state = 2;
602 reg1 = s5h1409_readreg(state, 0xb2);
603 reg2 = s5h1409_readreg(state, 0xad);
605 s5h1409_writereg(state, 0x96, 0x20);
606 s5h1409_writereg(state, 0xad,
607 (((reg1 & 0xf000) >> 4) | (reg2 & 0xf0ff)));
608 s5h1409_writereg(state, 0xab,
609 s5h1409_readreg(state, 0xab) & 0xeffe);
611 } else {
612 if (state->qam_state != 1) {
613 state->qam_state = 1;
614 s5h1409_writereg(state, 0x96, 0x08);
615 s5h1409_writereg(state, 0xab,
616 s5h1409_readreg(state, 0xab) | 0x1001);
621 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
622 static int s5h1409_set_frontend(struct dvb_frontend *fe)
624 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
625 struct s5h1409_state *state = fe->demodulator_priv;
627 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
629 s5h1409_softreset(fe);
631 state->current_frequency = p->frequency;
633 s5h1409_enable_modulation(fe, p->modulation);
635 if (fe->ops.tuner_ops.set_params) {
636 if (fe->ops.i2c_gate_ctrl)
637 fe->ops.i2c_gate_ctrl(fe, 1);
638 fe->ops.tuner_ops.set_params(fe);
639 if (fe->ops.i2c_gate_ctrl)
640 fe->ops.i2c_gate_ctrl(fe, 0);
643 /* Issue a reset to the demod so it knows to resync against the
644 newly tuned frequency */
645 s5h1409_softreset(fe);
647 /* Optimize the demod for QAM */
648 if (state->current_modulation != VSB_8) {
649 /* This almost certainly applies to all boards, but for now
650 only do it for the HVR-1600. Once the other boards are
651 tested, the "legacy" versions can just go away */
652 if (state->config->hvr1600_opt == S5H1409_HVR1600_OPTIMIZE) {
653 s5h1409_set_qam_interleave_mode(fe);
654 s5h1409_set_qam_amhum_mode(fe);
655 } else {
656 s5h1409_set_qam_amhum_mode_legacy(fe);
657 s5h1409_set_qam_interleave_mode_legacy(fe);
661 return 0;
664 static int s5h1409_set_mpeg_timing(struct dvb_frontend *fe, int mode)
666 struct s5h1409_state *state = fe->demodulator_priv;
667 u16 val;
669 dprintk("%s(%d)\n", __func__, mode);
671 val = s5h1409_readreg(state, 0xac) & 0xcfff;
672 switch (mode) {
673 case S5H1409_MPEGTIMING_CONTINUOUS_INVERTING_CLOCK:
674 val |= 0x0000;
675 break;
676 case S5H1409_MPEGTIMING_CONTINUOUS_NONINVERTING_CLOCK:
677 dprintk("%s(%d) Mode1 or Defaulting\n", __func__, mode);
678 val |= 0x1000;
679 break;
680 case S5H1409_MPEGTIMING_NONCONTINUOUS_INVERTING_CLOCK:
681 val |= 0x2000;
682 break;
683 case S5H1409_MPEGTIMING_NONCONTINUOUS_NONINVERTING_CLOCK:
684 val |= 0x3000;
685 break;
686 default:
687 return -EINVAL;
690 /* Configure MPEG Signal Timing charactistics */
691 return s5h1409_writereg(state, 0xac, val);
694 /* Reset the demod hardware and reset all of the configuration registers
695 to a default state. */
696 static int s5h1409_init(struct dvb_frontend *fe)
698 int i;
700 struct s5h1409_state *state = fe->demodulator_priv;
701 dprintk("%s()\n", __func__);
703 s5h1409_sleep(fe, 0);
704 s5h1409_register_reset(fe);
706 for (i = 0; i < ARRAY_SIZE(init_tab); i++)
707 s5h1409_writereg(state, init_tab[i].reg, init_tab[i].data);
709 /* The datasheet says that after initialisation, VSB is default */
710 state->current_modulation = VSB_8;
712 /* Optimize for the HVR-1600 if appropriate. Note that some of these
713 may get folded into the generic case after testing with other
714 devices */
715 if (state->config->hvr1600_opt == S5H1409_HVR1600_OPTIMIZE) {
716 /* VSB AGC REF */
717 s5h1409_writereg(state, 0x09, 0x0050);
719 /* Unknown but Windows driver does it... */
720 s5h1409_writereg(state, 0x21, 0x0001);
721 s5h1409_writereg(state, 0x50, 0x030e);
723 /* QAM AGC REF */
724 s5h1409_writereg(state, 0x82, 0x0800);
727 if (state->config->output_mode == S5H1409_SERIAL_OUTPUT)
728 s5h1409_writereg(state, 0xab,
729 s5h1409_readreg(state, 0xab) | 0x100); /* Serial */
730 else
731 s5h1409_writereg(state, 0xab,
732 s5h1409_readreg(state, 0xab) & 0xfeff); /* Parallel */
734 s5h1409_set_spectralinversion(fe, state->config->inversion);
735 s5h1409_set_if_freq(fe, state->if_freq);
736 s5h1409_set_gpio(fe, state->config->gpio);
737 s5h1409_set_mpeg_timing(fe, state->config->mpeg_timing);
738 s5h1409_softreset(fe);
740 /* Note: Leaving the I2C gate closed. */
741 s5h1409_i2c_gate_ctrl(fe, 0);
743 return 0;
746 static int s5h1409_read_status(struct dvb_frontend *fe, enum fe_status *status)
748 struct s5h1409_state *state = fe->demodulator_priv;
749 u16 reg;
750 u32 tuner_status = 0;
752 *status = 0;
754 /* Optimize the demod for QAM */
755 if (state->current_modulation != VSB_8) {
756 /* This almost certainly applies to all boards, but for now
757 only do it for the HVR-1600. Once the other boards are
758 tested, the "legacy" versions can just go away */
759 if (state->config->hvr1600_opt == S5H1409_HVR1600_OPTIMIZE) {
760 s5h1409_set_qam_interleave_mode(fe);
761 s5h1409_set_qam_amhum_mode(fe);
765 /* Get the demodulator status */
766 reg = s5h1409_readreg(state, 0xf1);
767 if (reg & 0x1000)
768 *status |= FE_HAS_VITERBI;
769 if (reg & 0x8000)
770 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
772 switch (state->config->status_mode) {
773 case S5H1409_DEMODLOCKING:
774 if (*status & FE_HAS_VITERBI)
775 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
776 break;
777 case S5H1409_TUNERLOCKING:
778 /* Get the tuner status */
779 if (fe->ops.tuner_ops.get_status) {
780 if (fe->ops.i2c_gate_ctrl)
781 fe->ops.i2c_gate_ctrl(fe, 1);
783 fe->ops.tuner_ops.get_status(fe, &tuner_status);
785 if (fe->ops.i2c_gate_ctrl)
786 fe->ops.i2c_gate_ctrl(fe, 0);
788 if (tuner_status)
789 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
790 break;
793 dprintk("%s() status 0x%08x\n", __func__, *status);
795 return 0;
798 static int s5h1409_qam256_lookup_snr(struct dvb_frontend *fe, u16 *snr, u16 v)
800 int i, ret = -EINVAL;
801 dprintk("%s()\n", __func__);
803 for (i = 0; i < ARRAY_SIZE(qam256_snr_tab); i++) {
804 if (v < qam256_snr_tab[i].val) {
805 *snr = qam256_snr_tab[i].data;
806 ret = 0;
807 break;
810 return ret;
813 static int s5h1409_qam64_lookup_snr(struct dvb_frontend *fe, u16 *snr, u16 v)
815 int i, ret = -EINVAL;
816 dprintk("%s()\n", __func__);
818 for (i = 0; i < ARRAY_SIZE(qam64_snr_tab); i++) {
819 if (v < qam64_snr_tab[i].val) {
820 *snr = qam64_snr_tab[i].data;
821 ret = 0;
822 break;
825 return ret;
828 static int s5h1409_vsb_lookup_snr(struct dvb_frontend *fe, u16 *snr, u16 v)
830 int i, ret = -EINVAL;
831 dprintk("%s()\n", __func__);
833 for (i = 0; i < ARRAY_SIZE(vsb_snr_tab); i++) {
834 if (v > vsb_snr_tab[i].val) {
835 *snr = vsb_snr_tab[i].data;
836 ret = 0;
837 break;
840 dprintk("%s() snr=%d\n", __func__, *snr);
841 return ret;
844 static int s5h1409_read_snr(struct dvb_frontend *fe, u16 *snr)
846 struct s5h1409_state *state = fe->demodulator_priv;
847 u16 reg;
848 dprintk("%s()\n", __func__);
850 switch (state->current_modulation) {
851 case QAM_64:
852 reg = s5h1409_readreg(state, 0xf0) & 0xff;
853 return s5h1409_qam64_lookup_snr(fe, snr, reg);
854 case QAM_256:
855 reg = s5h1409_readreg(state, 0xf0) & 0xff;
856 return s5h1409_qam256_lookup_snr(fe, snr, reg);
857 case VSB_8:
858 reg = s5h1409_readreg(state, 0xf1) & 0x3ff;
859 return s5h1409_vsb_lookup_snr(fe, snr, reg);
860 default:
861 break;
864 return -EINVAL;
867 static int s5h1409_read_signal_strength(struct dvb_frontend *fe,
868 u16 *signal_strength)
870 /* borrowed from lgdt330x.c
872 * Calculate strength from SNR up to 35dB
873 * Even though the SNR can go higher than 35dB,
874 * there is some comfort factor in having a range of
875 * strong signals that can show at 100%
877 u16 snr;
878 u32 tmp;
879 int ret = s5h1409_read_snr(fe, &snr);
881 *signal_strength = 0;
883 if (0 == ret) {
884 /* The following calculation method was chosen
885 * purely for the sake of code re-use from the
886 * other demod drivers that use this method */
888 /* Convert from SNR in dB * 10 to 8.24 fixed-point */
889 tmp = (snr * ((1 << 24) / 10));
891 /* Convert from 8.24 fixed-point to
892 * scale the range 0 - 35*2^24 into 0 - 65535*/
893 if (tmp >= 8960 * 0x10000)
894 *signal_strength = 0xffff;
895 else
896 *signal_strength = tmp / 8960;
899 return ret;
902 static int s5h1409_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
904 struct s5h1409_state *state = fe->demodulator_priv;
906 *ucblocks = s5h1409_readreg(state, 0xb5);
908 return 0;
911 static int s5h1409_read_ber(struct dvb_frontend *fe, u32 *ber)
913 return s5h1409_read_ucblocks(fe, ber);
916 static int s5h1409_get_frontend(struct dvb_frontend *fe,
917 struct dtv_frontend_properties *p)
919 struct s5h1409_state *state = fe->demodulator_priv;
921 p->frequency = state->current_frequency;
922 p->modulation = state->current_modulation;
924 return 0;
927 static int s5h1409_get_tune_settings(struct dvb_frontend *fe,
928 struct dvb_frontend_tune_settings *tune)
930 tune->min_delay_ms = 1000;
931 return 0;
934 static void s5h1409_release(struct dvb_frontend *fe)
936 struct s5h1409_state *state = fe->demodulator_priv;
937 kfree(state);
940 static const struct dvb_frontend_ops s5h1409_ops;
942 struct dvb_frontend *s5h1409_attach(const struct s5h1409_config *config,
943 struct i2c_adapter *i2c)
945 struct s5h1409_state *state = NULL;
946 u16 reg;
948 /* allocate memory for the internal state */
949 state = kzalloc(sizeof(struct s5h1409_state), GFP_KERNEL);
950 if (state == NULL)
951 goto error;
953 /* setup the state */
954 state->config = config;
955 state->i2c = i2c;
956 state->current_modulation = 0;
957 state->if_freq = S5H1409_VSB_IF_FREQ;
959 /* check if the demod exists */
960 reg = s5h1409_readreg(state, 0x04);
961 if ((reg != 0x0066) && (reg != 0x007f))
962 goto error;
964 /* create dvb_frontend */
965 memcpy(&state->frontend.ops, &s5h1409_ops,
966 sizeof(struct dvb_frontend_ops));
967 state->frontend.demodulator_priv = state;
969 if (s5h1409_init(&state->frontend) != 0) {
970 printk(KERN_ERR "%s: Failed to initialize correctly\n",
971 __func__);
972 goto error;
975 /* Note: Leaving the I2C gate open here. */
976 s5h1409_i2c_gate_ctrl(&state->frontend, 1);
978 return &state->frontend;
980 error:
981 kfree(state);
982 return NULL;
984 EXPORT_SYMBOL(s5h1409_attach);
986 static const struct dvb_frontend_ops s5h1409_ops = {
987 .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
988 .info = {
989 .name = "Samsung S5H1409 QAM/8VSB Frontend",
990 .frequency_min_hz = 54 * MHz,
991 .frequency_max_hz = 858 * MHz,
992 .frequency_stepsize_hz = 62500,
993 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
996 .init = s5h1409_init,
997 .i2c_gate_ctrl = s5h1409_i2c_gate_ctrl,
998 .set_frontend = s5h1409_set_frontend,
999 .get_frontend = s5h1409_get_frontend,
1000 .get_tune_settings = s5h1409_get_tune_settings,
1001 .read_status = s5h1409_read_status,
1002 .read_ber = s5h1409_read_ber,
1003 .read_signal_strength = s5h1409_read_signal_strength,
1004 .read_snr = s5h1409_read_snr,
1005 .read_ucblocks = s5h1409_read_ucblocks,
1006 .release = s5h1409_release,
1009 MODULE_DESCRIPTION("Samsung S5H1409 QAM-B/ATSC Demodulator driver");
1010 MODULE_AUTHOR("Steven Toth");
1011 MODULE_LICENSE("GPL");