]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/media/dvb/frontends/s5h1409.c
V4L/DVB (13331): mxl5005s: provide ability to override QAM gain for HVR-1600
[mirror_ubuntu-bionic-kernel.git] / drivers / media / dvb / frontends / s5h1409.c
CommitLineData
89885558
ST
1/*
2 Samsung S5H1409 VSB/QAM demodulator driver
3
6d897616 4 Copyright (C) 2006 Steven Toth <stoth@linuxtv.org>
89885558
ST
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"
89885558
ST
29#include "s5h1409.h"
30
31struct s5h1409_state {
32
b7709c0d 33 struct i2c_adapter *i2c;
89885558
ST
34
35 /* configuration settings */
b7709c0d 36 const struct s5h1409_config *config;
89885558
ST
37
38 struct dvb_frontend frontend;
39
40 /* previous uncorrected block counter */
41 fe_modulation_t current_modulation;
42
43 u32 current_frequency;
2b03238a 44 int if_freq;
dd7d5013
ST
45
46 u32 is_qam_locked;
f0cd44b4
DH
47
48 /* QAM tuning state goes through the following state transitions */
49#define QAM_STATE_UNTUNED 0
50#define QAM_STATE_TUNING_STARTED 1
51#define QAM_STATE_INTERLEAVE_SET 2
52#define QAM_STATE_QAM_OPTIMIZED_L1 3
53#define QAM_STATE_QAM_OPTIMIZED_L2 4
54#define QAM_STATE_QAM_OPTIMIZED_L3 5
55 u8 qam_state;
89885558
ST
56};
57
ff699e6b 58static int debug;
b7709c0d
ST
59module_param(debug, int, 0644);
60MODULE_PARM_DESC(debug, "Enable verbose debug messages");
61
89885558
ST
62#define dprintk if (debug) printk
63
64/* Register values to initialise the demod, this will set VSB by default */
65static struct init_tab {
66 u8 reg;
67 u16 data;
68} init_tab[] = {
69 { 0x00, 0x0071, },
70 { 0x01, 0x3213, },
71 { 0x09, 0x0025, },
72 { 0x1c, 0x001d, },
73 { 0x1f, 0x002d, },
74 { 0x20, 0x001d, },
75 { 0x22, 0x0022, },
76 { 0x23, 0x0020, },
77 { 0x29, 0x110f, },
78 { 0x2a, 0x10b4, },
79 { 0x2b, 0x10ae, },
80 { 0x2c, 0x0031, },
81 { 0x31, 0x010d, },
82 { 0x32, 0x0100, },
83 { 0x44, 0x0510, },
84 { 0x54, 0x0104, },
85 { 0x58, 0x2222, },
86 { 0x59, 0x1162, },
87 { 0x5a, 0x3211, },
88 { 0x5d, 0x0370, },
89 { 0x5e, 0x0296, },
90 { 0x61, 0x0010, },
91 { 0x63, 0x4a00, },
92 { 0x65, 0x0800, },
93 { 0x71, 0x0003, },
94 { 0x72, 0x0470, },
95 { 0x81, 0x0002, },
96 { 0x82, 0x0600, },
97 { 0x86, 0x0002, },
98 { 0x8a, 0x2c38, },
99 { 0x8b, 0x2a37, },
100 { 0x92, 0x302f, },
101 { 0x93, 0x3332, },
102 { 0x96, 0x000c, },
103 { 0x99, 0x0101, },
104 { 0x9c, 0x2e37, },
105 { 0x9d, 0x2c37, },
106 { 0x9e, 0x2c37, },
107 { 0xab, 0x0100, },
108 { 0xac, 0x1003, },
109 { 0xad, 0x103f, },
110 { 0xe2, 0x0100, },
dfc1c08a 111 { 0xe3, 0x1000, },
89885558
ST
112 { 0x28, 0x1010, },
113 { 0xb1, 0x000e, },
114};
115
116/* VSB SNR lookup table */
117static struct vsb_snr_tab {
118 u16 val;
119 u16 data;
120} vsb_snr_tab[] = {
2300317f 121 { 924, 300, },
89885558
ST
122 { 923, 300, },
123 { 918, 295, },
124 { 915, 290, },
125 { 911, 285, },
126 { 906, 280, },
127 { 901, 275, },
128 { 896, 270, },
129 { 891, 265, },
130 { 885, 260, },
131 { 879, 255, },
132 { 873, 250, },
133 { 864, 245, },
134 { 858, 240, },
135 { 850, 235, },
136 { 841, 230, },
137 { 832, 225, },
138 { 823, 220, },
139 { 812, 215, },
140 { 802, 210, },
141 { 788, 205, },
142 { 778, 200, },
143 { 767, 195, },
144 { 753, 190, },
145 { 740, 185, },
146 { 725, 180, },
147 { 707, 175, },
148 { 689, 170, },
149 { 671, 165, },
150 { 656, 160, },
151 { 637, 155, },
152 { 616, 150, },
153 { 542, 145, },
154 { 519, 140, },
155 { 507, 135, },
156 { 497, 130, },
157 { 492, 125, },
158 { 474, 120, },
159 { 300, 111, },
160 { 0, 0, },
161};
162
163/* QAM64 SNR lookup table */
164static struct qam64_snr_tab {
165 u16 val;
166 u16 data;
167} qam64_snr_tab[] = {
2300317f 168 { 1, 0, },
89885558
ST
169 { 12, 300, },
170 { 15, 290, },
171 { 18, 280, },
172 { 22, 270, },
173 { 23, 268, },
174 { 24, 266, },
175 { 25, 264, },
176 { 27, 262, },
177 { 28, 260, },
178 { 29, 258, },
179 { 30, 256, },
180 { 32, 254, },
181 { 33, 252, },
182 { 34, 250, },
183 { 35, 249, },
184 { 36, 248, },
185 { 37, 247, },
186 { 38, 246, },
187 { 39, 245, },
188 { 40, 244, },
189 { 41, 243, },
190 { 42, 241, },
191 { 43, 240, },
192 { 44, 239, },
193 { 45, 238, },
194 { 46, 237, },
195 { 47, 236, },
196 { 48, 235, },
197 { 49, 234, },
198 { 50, 233, },
199 { 51, 232, },
200 { 52, 231, },
201 { 53, 230, },
202 { 55, 229, },
203 { 56, 228, },
204 { 57, 227, },
205 { 58, 226, },
206 { 59, 225, },
207 { 60, 224, },
208 { 62, 223, },
209 { 63, 222, },
210 { 65, 221, },
211 { 66, 220, },
212 { 68, 219, },
213 { 69, 218, },
214 { 70, 217, },
215 { 72, 216, },
216 { 73, 215, },
217 { 75, 214, },
218 { 76, 213, },
219 { 78, 212, },
220 { 80, 211, },
221 { 81, 210, },
222 { 83, 209, },
223 { 84, 208, },
224 { 85, 207, },
225 { 87, 206, },
226 { 89, 205, },
227 { 91, 204, },
228 { 93, 203, },
229 { 95, 202, },
230 { 96, 201, },
231 { 104, 200, },
2300317f 232 { 255, 0, },
89885558
ST
233};
234
235/* QAM256 SNR lookup table */
236static struct qam256_snr_tab {
237 u16 val;
238 u16 data;
239} qam256_snr_tab[] = {
2300317f 240 { 1, 0, },
89885558
ST
241 { 12, 400, },
242 { 13, 390, },
243 { 15, 380, },
244 { 17, 360, },
245 { 19, 350, },
246 { 22, 348, },
247 { 23, 346, },
248 { 24, 344, },
249 { 25, 342, },
250 { 26, 340, },
251 { 27, 336, },
252 { 28, 334, },
253 { 29, 332, },
254 { 30, 330, },
255 { 31, 328, },
256 { 32, 326, },
257 { 33, 325, },
258 { 34, 322, },
259 { 35, 320, },
260 { 37, 318, },
261 { 39, 316, },
262 { 40, 314, },
263 { 41, 312, },
264 { 42, 310, },
265 { 43, 308, },
266 { 46, 306, },
267 { 47, 304, },
268 { 49, 302, },
269 { 51, 300, },
270 { 53, 298, },
271 { 54, 297, },
272 { 55, 296, },
273 { 56, 295, },
274 { 57, 294, },
275 { 59, 293, },
276 { 60, 292, },
277 { 61, 291, },
278 { 63, 290, },
279 { 64, 289, },
280 { 65, 288, },
281 { 66, 287, },
282 { 68, 286, },
283 { 69, 285, },
284 { 71, 284, },
285 { 72, 283, },
286 { 74, 282, },
287 { 75, 281, },
288 { 76, 280, },
289 { 77, 279, },
290 { 78, 278, },
291 { 81, 277, },
292 { 83, 276, },
293 { 84, 275, },
294 { 86, 274, },
295 { 87, 273, },
296 { 89, 272, },
297 { 90, 271, },
298 { 92, 270, },
299 { 93, 269, },
300 { 95, 268, },
301 { 96, 267, },
302 { 98, 266, },
303 { 100, 265, },
304 { 102, 264, },
305 { 104, 263, },
306 { 105, 262, },
307 { 106, 261, },
308 { 110, 260, },
2300317f 309 { 255, 0, },
89885558
ST
310};
311
312/* 8 bit registers, 16 bit values */
b7709c0d 313static int s5h1409_writereg(struct s5h1409_state *state, u8 reg, u16 data)
89885558
ST
314{
315 int ret;
b7709c0d 316 u8 buf[] = { reg, data >> 8, data & 0xff };
89885558 317
3873dd04
MK
318 struct i2c_msg msg = { .addr = state->config->demod_address,
319 .flags = 0, .buf = buf, .len = 3 };
89885558
ST
320
321 ret = i2c_transfer(state->i2c, &msg, 1);
322
323 if (ret != 1)
b7709c0d 324 printk(KERN_ERR "%s: error (reg == 0x%02x, val == 0x%04x, "
271ddbf7 325 "ret == %i)\n", __func__, reg, data, ret);
89885558
ST
326
327 return (ret != 1) ? -1 : 0;
328}
329
b7709c0d 330static u16 s5h1409_readreg(struct s5h1409_state *state, u8 reg)
89885558
ST
331{
332 int ret;
b7709c0d
ST
333 u8 b0[] = { reg };
334 u8 b1[] = { 0, 0 };
89885558 335
b7709c0d 336 struct i2c_msg msg[] = {
3873dd04
MK
337 { .addr = state->config->demod_address, .flags = 0,
338 .buf = b0, .len = 1 },
339 { .addr = state->config->demod_address, .flags = I2C_M_RD,
340 .buf = b1, .len = 2 } };
89885558
ST
341
342 ret = i2c_transfer(state->i2c, msg, 2);
343
344 if (ret != 2)
271ddbf7 345 printk("%s: readreg error (ret == %i)\n", __func__, ret);
89885558
ST
346 return (b1[0] << 8) | b1[1];
347}
348
b7709c0d 349static int s5h1409_softreset(struct dvb_frontend *fe)
89885558 350{
b7709c0d 351 struct s5h1409_state *state = fe->demodulator_priv;
89885558 352
271ddbf7 353 dprintk("%s()\n", __func__);
89885558
ST
354
355 s5h1409_writereg(state, 0xf5, 0);
356 s5h1409_writereg(state, 0xf5, 1);
dd7d5013 357 state->is_qam_locked = 0;
f0cd44b4 358 state->qam_state = QAM_STATE_UNTUNED;
89885558
ST
359 return 0;
360}
361
2b03238a 362#define S5H1409_VSB_IF_FREQ 5380
b7709c0d 363#define S5H1409_QAM_IF_FREQ (state->config->qam_if)
2b03238a 364
b7709c0d 365static int s5h1409_set_if_freq(struct dvb_frontend *fe, int KHz)
89885558 366{
b7709c0d 367 struct s5h1409_state *state = fe->demodulator_priv;
89885558 368
271ddbf7 369 dprintk("%s(%d KHz)\n", __func__, KHz);
89885558 370
6b7daa88
MK
371 switch (KHz) {
372 case 4000:
dd7d5013
ST
373 s5h1409_writereg(state, 0x87, 0x014b);
374 s5h1409_writereg(state, 0x88, 0x0cb5);
375 s5h1409_writereg(state, 0x89, 0x03e2);
6b7daa88
MK
376 break;
377 case 5380:
378 case 44000:
379 default:
380 s5h1409_writereg(state, 0x87, 0x01be);
381 s5h1409_writereg(state, 0x88, 0x0436);
382 s5h1409_writereg(state, 0x89, 0x054d);
383 break;
89885558 384 }
6b7daa88 385 state->if_freq = KHz;
89885558 386
6b7daa88 387 return 0;
89885558
ST
388}
389
b7709c0d 390static int s5h1409_set_spectralinversion(struct dvb_frontend *fe, int inverted)
89885558 391{
b7709c0d 392 struct s5h1409_state *state = fe->demodulator_priv;
89885558 393
271ddbf7 394 dprintk("%s(%d)\n", __func__, inverted);
89885558 395
b7709c0d 396 if (inverted == 1)
89885558
ST
397 return s5h1409_writereg(state, 0x1b, 0x1101); /* Inverted */
398 else
399 return s5h1409_writereg(state, 0x1b, 0x0110); /* Normal */
400}
401
b7709c0d 402static int s5h1409_enable_modulation(struct dvb_frontend *fe,
3873dd04 403 fe_modulation_t m)
89885558 404{
b7709c0d 405 struct s5h1409_state *state = fe->demodulator_priv;
89885558 406
271ddbf7 407 dprintk("%s(0x%08x)\n", __func__, m);
89885558 408
b7709c0d 409 switch (m) {
89885558 410 case VSB_8:
271ddbf7 411 dprintk("%s() VSB_8\n", __func__);
2b03238a
MK
412 if (state->if_freq != S5H1409_VSB_IF_FREQ)
413 s5h1409_set_if_freq(fe, S5H1409_VSB_IF_FREQ);
89885558
ST
414 s5h1409_writereg(state, 0xf4, 0);
415 break;
416 case QAM_64:
89885558 417 case QAM_256:
4fc85c74 418 case QAM_AUTO:
271ddbf7 419 dprintk("%s() QAM_AUTO (64/256)\n", __func__);
2b03238a
MK
420 if (state->if_freq != S5H1409_QAM_IF_FREQ)
421 s5h1409_set_if_freq(fe, S5H1409_QAM_IF_FREQ);
89885558 422 s5h1409_writereg(state, 0xf4, 1);
dd7d5013 423 s5h1409_writereg(state, 0x85, 0x110);
89885558
ST
424 break;
425 default:
271ddbf7 426 dprintk("%s() Invalid modulation\n", __func__);
89885558
ST
427 return -EINVAL;
428 }
429
430 state->current_modulation = m;
431 s5h1409_softreset(fe);
432
433 return 0;
434}
435
b7709c0d 436static int s5h1409_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
89885558 437{
b7709c0d 438 struct s5h1409_state *state = fe->demodulator_priv;
89885558 439
271ddbf7 440 dprintk("%s(%d)\n", __func__, enable);
89885558
ST
441
442 if (enable)
443 return s5h1409_writereg(state, 0xf3, 1);
444 else
445 return s5h1409_writereg(state, 0xf3, 0);
446}
447
b7709c0d 448static int s5h1409_set_gpio(struct dvb_frontend *fe, int enable)
89885558 449{
b7709c0d 450 struct s5h1409_state *state = fe->demodulator_priv;
89885558 451
271ddbf7 452 dprintk("%s(%d)\n", __func__, enable);
89885558
ST
453
454 if (enable)
dfc1c08a
ST
455 return s5h1409_writereg(state, 0xe3,
456 s5h1409_readreg(state, 0xe3) | 0x1100);
89885558 457 else
dfc1c08a 458 return s5h1409_writereg(state, 0xe3,
8e08af3c 459 s5h1409_readreg(state, 0xe3) & 0xfeff);
89885558
ST
460}
461
b7709c0d 462static int s5h1409_sleep(struct dvb_frontend *fe, int enable)
89885558 463{
b7709c0d 464 struct s5h1409_state *state = fe->demodulator_priv;
89885558 465
271ddbf7 466 dprintk("%s(%d)\n", __func__, enable);
89885558
ST
467
468 return s5h1409_writereg(state, 0xf2, enable);
469}
470
b7709c0d 471static int s5h1409_register_reset(struct dvb_frontend *fe)
89885558 472{
b7709c0d 473 struct s5h1409_state *state = fe->demodulator_priv;
89885558 474
271ddbf7 475 dprintk("%s()\n", __func__);
89885558
ST
476
477 return s5h1409_writereg(state, 0xfa, 0);
478}
479
dd7d5013
ST
480static void s5h1409_set_qam_amhum_mode(struct dvb_frontend *fe)
481{
482 struct s5h1409_state *state = fe->demodulator_priv;
483 u16 reg;
484
f0cd44b4
DH
485 if (state->qam_state < QAM_STATE_INTERLEAVE_SET) {
486 /* We should not perform amhum optimization until
487 the interleave mode has been configured */
488 return;
489 }
490
491 if (state->qam_state == QAM_STATE_QAM_OPTIMIZED_L3) {
492 /* We've already reached the maximum optimization level, so
493 dont bother banging on the status registers */
494 return;
495 }
496
497 /* QAM EQ lock check */
498 reg = s5h1409_readreg(state, 0xf0);
499
500 if ((reg >> 13) & 0x1) {
501 reg &= 0xff;
502
503 s5h1409_writereg(state, 0x96, 0x000c);
504 if (reg < 0x68) {
505 if (state->qam_state < QAM_STATE_QAM_OPTIMIZED_L3) {
506 dprintk("%s() setting QAM state to OPT_L3\n",
507 __func__);
508 s5h1409_writereg(state, 0x93, 0x3130);
509 s5h1409_writereg(state, 0x9e, 0x2836);
510 state->qam_state = QAM_STATE_QAM_OPTIMIZED_L3;
511 }
512 } else {
513 if (state->qam_state < QAM_STATE_QAM_OPTIMIZED_L2) {
514 dprintk("%s() setting QAM state to OPT_L2\n",
515 __func__);
516 s5h1409_writereg(state, 0x93, 0x3332);
517 s5h1409_writereg(state, 0x9e, 0x2c37);
518 state->qam_state = QAM_STATE_QAM_OPTIMIZED_L2;
519 }
520 }
521
522 } else {
523 if (state->qam_state < QAM_STATE_QAM_OPTIMIZED_L1) {
524 dprintk("%s() setting QAM state to OPT_L1\n", __func__);
525 s5h1409_writereg(state, 0x96, 0x0008);
526 s5h1409_writereg(state, 0x93, 0x3332);
527 s5h1409_writereg(state, 0x9e, 0x2c37);
528 state->qam_state = QAM_STATE_QAM_OPTIMIZED_L1;
529 }
530 }
531}
532
533static void s5h1409_set_qam_amhum_mode_legacy(struct dvb_frontend *fe)
534{
535 struct s5h1409_state *state = fe->demodulator_priv;
536 u16 reg;
537
dd7d5013
ST
538 if (state->is_qam_locked)
539 return;
540
541 /* QAM EQ lock check */
542 reg = s5h1409_readreg(state, 0xf0);
543
544 if ((reg >> 13) & 0x1) {
545
546 state->is_qam_locked = 1;
547 reg &= 0xff;
548
549 s5h1409_writereg(state, 0x96, 0x00c);
b7709c0d 550 if ((reg < 0x38) || (reg > 0x68)) {
dd7d5013
ST
551 s5h1409_writereg(state, 0x93, 0x3332);
552 s5h1409_writereg(state, 0x9e, 0x2c37);
553 } else {
554 s5h1409_writereg(state, 0x93, 0x3130);
555 s5h1409_writereg(state, 0x9e, 0x2836);
556 }
557
558 } else {
559 s5h1409_writereg(state, 0x96, 0x0008);
560 s5h1409_writereg(state, 0x93, 0x3332);
561 s5h1409_writereg(state, 0x9e, 0x2c37);
562 }
563}
564
565static void s5h1409_set_qam_interleave_mode(struct dvb_frontend *fe)
566{
567 struct s5h1409_state *state = fe->demodulator_priv;
568 u16 reg, reg1, reg2;
569
f0cd44b4
DH
570 if (state->qam_state >= QAM_STATE_INTERLEAVE_SET) {
571 /* We've done the optimization already */
572 return;
573 }
574
575 reg = s5h1409_readreg(state, 0xf1);
576
577 /* Master lock */
578 if ((reg >> 15) & 0x1) {
579 if (state->qam_state == QAM_STATE_UNTUNED ||
580 state->qam_state == QAM_STATE_TUNING_STARTED) {
581 dprintk("%s() setting QAM state to INTERLEAVE_SET\n",
582 __func__);
583 reg1 = s5h1409_readreg(state, 0xb2);
584 reg2 = s5h1409_readreg(state, 0xad);
585
586 s5h1409_writereg(state, 0x96, 0x0020);
587 s5h1409_writereg(state, 0xad,
588 (((reg1 & 0xf000) >> 4) | (reg2 & 0xf0ff)));
589 s5h1409_writereg(state, 0xab,
590 s5h1409_readreg(state, 0xab) & 0xeffe);
591 state->qam_state = QAM_STATE_INTERLEAVE_SET;
592 }
593 } else {
594 if (state->qam_state == QAM_STATE_UNTUNED) {
595 dprintk("%s() setting QAM state to TUNING_STARTED\n",
596 __func__);
597 s5h1409_writereg(state, 0x96, 0x08);
598 s5h1409_writereg(state, 0xab,
599 s5h1409_readreg(state, 0xab) | 0x1001);
600 state->qam_state = QAM_STATE_TUNING_STARTED;
601 }
602 }
603}
604
605static void s5h1409_set_qam_interleave_mode_legacy(struct dvb_frontend *fe)
606{
607 struct s5h1409_state *state = fe->demodulator_priv;
608 u16 reg, reg1, reg2;
609
dd7d5013
ST
610 reg = s5h1409_readreg(state, 0xf1);
611
612 /* Master lock */
613 if ((reg >> 15) & 0x1) {
614 if (state->qam_state != 2) {
615 state->qam_state = 2;
616 reg1 = s5h1409_readreg(state, 0xb2);
617 reg2 = s5h1409_readreg(state, 0xad);
618
619 s5h1409_writereg(state, 0x96, 0x20);
620 s5h1409_writereg(state, 0xad,
b7709c0d 621 (((reg1 & 0xf000) >> 4) | (reg2 & 0xf0ff)));
dfc1c08a
ST
622 s5h1409_writereg(state, 0xab,
623 s5h1409_readreg(state, 0xab) & 0xeffe);
dd7d5013
ST
624 }
625 } else {
626 if (state->qam_state != 1) {
627 state->qam_state = 1;
628 s5h1409_writereg(state, 0x96, 0x08);
dfc1c08a
ST
629 s5h1409_writereg(state, 0xab,
630 s5h1409_readreg(state, 0xab) | 0x1001);
dd7d5013
ST
631 }
632 }
633}
634
89885558 635/* Talk to the demod, set the FEC, GUARD, QAM settings etc */
b7709c0d 636static int s5h1409_set_frontend(struct dvb_frontend *fe,
3873dd04 637 struct dvb_frontend_parameters *p)
89885558 638{
b7709c0d 639 struct s5h1409_state *state = fe->demodulator_priv;
89885558 640
271ddbf7 641 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
89885558
ST
642
643 s5h1409_softreset(fe);
644
645 state->current_frequency = p->frequency;
646
647 s5h1409_enable_modulation(fe, p->u.vsb.modulation);
648
649 if (fe->ops.tuner_ops.set_params) {
b7709c0d
ST
650 if (fe->ops.i2c_gate_ctrl)
651 fe->ops.i2c_gate_ctrl(fe, 1);
89885558 652 fe->ops.tuner_ops.set_params(fe, p);
b7709c0d
ST
653 if (fe->ops.i2c_gate_ctrl)
654 fe->ops.i2c_gate_ctrl(fe, 0);
89885558
ST
655 }
656
67e70baf
DH
657 /* Issue a reset to the demod so it knows to resync against the
658 newly tuned frequency */
659 s5h1409_softreset(fe);
660
f0cd44b4
DH
661 /* Optimize the demod for QAM */
662 if (state->current_modulation != VSB_8) {
663 /* This almost certainly applies to all boards, but for now
664 only do it for the HVR-1600. Once the other boards are
665 tested, the "legacy" versions can just go away */
666 if (state->config->hvr1600_opt == S5H1409_HVR1600_OPTIMIZE) {
667 s5h1409_set_qam_amhum_mode(fe);
668 s5h1409_set_qam_interleave_mode(fe);
669 } else {
670 s5h1409_set_qam_amhum_mode_legacy(fe);
671 s5h1409_set_qam_interleave_mode_legacy(fe);
672 }
673 }
674
89885558
ST
675 return 0;
676}
677
dfc1c08a
ST
678static int s5h1409_set_mpeg_timing(struct dvb_frontend *fe, int mode)
679{
680 struct s5h1409_state *state = fe->demodulator_priv;
681 u16 val;
682
271ddbf7 683 dprintk("%s(%d)\n", __func__, mode);
dfc1c08a
ST
684
685 val = s5h1409_readreg(state, 0xac) & 0xcfff;
686 switch (mode) {
687 case S5H1409_MPEGTIMING_CONTINOUS_INVERTING_CLOCK:
688 val |= 0x0000;
689 break;
690 case S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK:
271ddbf7 691 dprintk("%s(%d) Mode1 or Defaulting\n", __func__, mode);
dfc1c08a
ST
692 val |= 0x1000;
693 break;
694 case S5H1409_MPEGTIMING_NONCONTINOUS_INVERTING_CLOCK:
695 val |= 0x2000;
696 break;
697 case S5H1409_MPEGTIMING_NONCONTINOUS_NONINVERTING_CLOCK:
698 val |= 0x3000;
699 break;
700 default:
701 return -EINVAL;
702 }
703
704 /* Configure MPEG Signal Timing charactistics */
705 return s5h1409_writereg(state, 0xac, val);
706}
707
89885558
ST
708/* Reset the demod hardware and reset all of the configuration registers
709 to a default state. */
b7709c0d 710static int s5h1409_init(struct dvb_frontend *fe)
89885558
ST
711{
712 int i;
713
b7709c0d 714 struct s5h1409_state *state = fe->demodulator_priv;
271ddbf7 715 dprintk("%s()\n", __func__);
89885558
ST
716
717 s5h1409_sleep(fe, 0);
718 s5h1409_register_reset(fe);
719
b7709c0d 720 for (i = 0; i < ARRAY_SIZE(init_tab); i++)
89885558
ST
721 s5h1409_writereg(state, init_tab[i].reg, init_tab[i].data);
722
723 /* The datasheet says that after initialisation, VSB is default */
724 state->current_modulation = VSB_8;
725
af5c8e15
DH
726 /* Optimize for the HVR-1600 if appropriate. Note that some of these
727 may get folded into the generic case after testing with other
728 devices */
729 if (state->config->hvr1600_opt == S5H1409_HVR1600_OPTIMIZE) {
730 /* VSB AGC REF */
731 s5h1409_writereg(state, 0x09, 0x0050);
732
733 /* Unknown but Windows driver does it... */
734 s5h1409_writereg(state, 0x21, 0x0001);
735 s5h1409_writereg(state, 0x50, 0x030e);
736
737 /* QAM AGC REF */
738 s5h1409_writereg(state, 0x82, 0x0800);
739 }
740
89885558 741 if (state->config->output_mode == S5H1409_SERIAL_OUTPUT)
dfc1c08a
ST
742 s5h1409_writereg(state, 0xab,
743 s5h1409_readreg(state, 0xab) | 0x100); /* Serial */
89885558 744 else
dfc1c08a
ST
745 s5h1409_writereg(state, 0xab,
746 s5h1409_readreg(state, 0xab) & 0xfeff); /* Parallel */
89885558
ST
747
748 s5h1409_set_spectralinversion(fe, state->config->inversion);
2b03238a 749 s5h1409_set_if_freq(fe, state->if_freq);
89885558 750 s5h1409_set_gpio(fe, state->config->gpio);
dfc1c08a 751 s5h1409_set_mpeg_timing(fe, state->config->mpeg_timing);
89885558
ST
752 s5h1409_softreset(fe);
753
dd7d5013
ST
754 /* Note: Leaving the I2C gate closed. */
755 s5h1409_i2c_gate_ctrl(fe, 0);
89885558
ST
756
757 return 0;
758}
759
b7709c0d 760static int s5h1409_read_status(struct dvb_frontend *fe, fe_status_t *status)
89885558 761{
b7709c0d 762 struct s5h1409_state *state = fe->demodulator_priv;
89885558
ST
763 u16 reg;
764 u32 tuner_status = 0;
765
766 *status = 0;
767
f0cd44b4
DH
768 /* Optimize the demod for QAM */
769 if (state->current_modulation != VSB_8) {
770 /* This almost certainly applies to all boards, but for now
771 only do it for the HVR-1600. Once the other boards are
772 tested, the "legacy" versions can just go away */
773 if (state->config->hvr1600_opt == S5H1409_HVR1600_OPTIMIZE) {
774 s5h1409_set_qam_amhum_mode(fe);
775 s5h1409_set_qam_interleave_mode(fe);
776 }
777 }
778
89885558
ST
779 /* Get the demodulator status */
780 reg = s5h1409_readreg(state, 0xf1);
b7709c0d 781 if (reg & 0x1000)
89885558 782 *status |= FE_HAS_VITERBI;
b7709c0d 783 if (reg & 0x8000)
89885558
ST
784 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
785
b7709c0d 786 switch (state->config->status_mode) {
89885558
ST
787 case S5H1409_DEMODLOCKING:
788 if (*status & FE_HAS_VITERBI)
789 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
790 break;
791 case S5H1409_TUNERLOCKING:
792 /* Get the tuner status */
793 if (fe->ops.tuner_ops.get_status) {
3873dd04
MK
794 if (fe->ops.i2c_gate_ctrl)
795 fe->ops.i2c_gate_ctrl(fe, 1);
89885558
ST
796
797 fe->ops.tuner_ops.get_status(fe, &tuner_status);
798
3873dd04
MK
799 if (fe->ops.i2c_gate_ctrl)
800 fe->ops.i2c_gate_ctrl(fe, 0);
89885558
ST
801 }
802 if (tuner_status)
803 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
804 break;
805 }
806
271ddbf7 807 dprintk("%s() status 0x%08x\n", __func__, *status);
89885558
ST
808
809 return 0;
810}
811
b7709c0d 812static int s5h1409_qam256_lookup_snr(struct dvb_frontend *fe, u16 *snr, u16 v)
89885558
ST
813{
814 int i, ret = -EINVAL;
271ddbf7 815 dprintk("%s()\n", __func__);
89885558 816
b7709c0d 817 for (i = 0; i < ARRAY_SIZE(qam256_snr_tab); i++) {
89885558
ST
818 if (v < qam256_snr_tab[i].val) {
819 *snr = qam256_snr_tab[i].data;
820 ret = 0;
821 break;
822 }
823 }
824 return ret;
825}
826
b7709c0d 827static int s5h1409_qam64_lookup_snr(struct dvb_frontend *fe, u16 *snr, u16 v)
89885558
ST
828{
829 int i, ret = -EINVAL;
271ddbf7 830 dprintk("%s()\n", __func__);
89885558 831
b7709c0d 832 for (i = 0; i < ARRAY_SIZE(qam64_snr_tab); i++) {
89885558
ST
833 if (v < qam64_snr_tab[i].val) {
834 *snr = qam64_snr_tab[i].data;
835 ret = 0;
836 break;
837 }
838 }
839 return ret;
840}
841
b7709c0d 842static int s5h1409_vsb_lookup_snr(struct dvb_frontend *fe, u16 *snr, u16 v)
89885558
ST
843{
844 int i, ret = -EINVAL;
271ddbf7 845 dprintk("%s()\n", __func__);
89885558 846
b7709c0d 847 for (i = 0; i < ARRAY_SIZE(vsb_snr_tab); i++) {
89885558
ST
848 if (v > vsb_snr_tab[i].val) {
849 *snr = vsb_snr_tab[i].data;
850 ret = 0;
851 break;
852 }
853 }
271ddbf7 854 dprintk("%s() snr=%d\n", __func__, *snr);
89885558
ST
855 return ret;
856}
857
b7709c0d 858static int s5h1409_read_snr(struct dvb_frontend *fe, u16 *snr)
89885558 859{
b7709c0d 860 struct s5h1409_state *state = fe->demodulator_priv;
89885558 861 u16 reg;
271ddbf7 862 dprintk("%s()\n", __func__);
89885558 863
b7709c0d 864 switch (state->current_modulation) {
89885558 865 case QAM_64:
2300317f 866 reg = s5h1409_readreg(state, 0xf0) & 0xff;
89885558
ST
867 return s5h1409_qam64_lookup_snr(fe, snr, reg);
868 case QAM_256:
2300317f 869 reg = s5h1409_readreg(state, 0xf0) & 0xff;
89885558
ST
870 return s5h1409_qam256_lookup_snr(fe, snr, reg);
871 case VSB_8:
2300317f 872 reg = s5h1409_readreg(state, 0xf1) & 0x3ff;
89885558
ST
873 return s5h1409_vsb_lookup_snr(fe, snr, reg);
874 default:
875 break;
876 }
877
878 return -EINVAL;
879}
880
b7709c0d
ST
881static int s5h1409_read_signal_strength(struct dvb_frontend *fe,
882 u16 *signal_strength)
89885558
ST
883{
884 return s5h1409_read_snr(fe, signal_strength);
885}
886
b7709c0d 887static int s5h1409_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
89885558 888{
b7709c0d 889 struct s5h1409_state *state = fe->demodulator_priv;
89885558
ST
890
891 *ucblocks = s5h1409_readreg(state, 0xb5);
892
893 return 0;
894}
895
b7709c0d 896static int s5h1409_read_ber(struct dvb_frontend *fe, u32 *ber)
89885558
ST
897{
898 return s5h1409_read_ucblocks(fe, ber);
899}
900
b7709c0d 901static int s5h1409_get_frontend(struct dvb_frontend *fe,
3873dd04 902 struct dvb_frontend_parameters *p)
89885558 903{
b7709c0d 904 struct s5h1409_state *state = fe->demodulator_priv;
89885558
ST
905
906 p->frequency = state->current_frequency;
907 p->u.vsb.modulation = state->current_modulation;
908
909 return 0;
910}
911
b7709c0d 912static int s5h1409_get_tune_settings(struct dvb_frontend *fe,
3873dd04 913 struct dvb_frontend_tune_settings *tune)
89885558
ST
914{
915 tune->min_delay_ms = 1000;
916 return 0;
917}
918
b7709c0d 919static void s5h1409_release(struct dvb_frontend *fe)
89885558 920{
b7709c0d 921 struct s5h1409_state *state = fe->demodulator_priv;
89885558
ST
922 kfree(state);
923}
924
925static struct dvb_frontend_ops s5h1409_ops;
926
b7709c0d
ST
927struct dvb_frontend *s5h1409_attach(const struct s5h1409_config *config,
928 struct i2c_adapter *i2c)
89885558 929{
b7709c0d 930 struct s5h1409_state *state = NULL;
a57ed8a1 931 u16 reg;
89885558
ST
932
933 /* allocate memory for the internal state */
084e24ac 934 state = kzalloc(sizeof(struct s5h1409_state), GFP_KERNEL);
89885558
ST
935 if (state == NULL)
936 goto error;
937
938 /* setup the state */
939 state->config = config;
940 state->i2c = i2c;
941 state->current_modulation = 0;
2b03238a 942 state->if_freq = S5H1409_VSB_IF_FREQ;
89885558
ST
943
944 /* check if the demod exists */
a57ed8a1
ST
945 reg = s5h1409_readreg(state, 0x04);
946 if ((reg != 0x0066) && (reg != 0x007f))
89885558
ST
947 goto error;
948
949 /* create dvb_frontend */
3873dd04
MK
950 memcpy(&state->frontend.ops, &s5h1409_ops,
951 sizeof(struct dvb_frontend_ops));
89885558
ST
952 state->frontend.demodulator_priv = state;
953
a57ed8a1
ST
954 if (s5h1409_init(&state->frontend) != 0) {
955 printk(KERN_ERR "%s: Failed to initialize correctly\n",
271ddbf7 956 __func__);
a57ed8a1
ST
957 goto error;
958 }
959
89885558 960 /* Note: Leaving the I2C gate open here. */
a57ed8a1 961 s5h1409_i2c_gate_ctrl(&state->frontend, 1);
89885558
ST
962
963 return &state->frontend;
964
965error:
966 kfree(state);
967 return NULL;
968}
b7709c0d 969EXPORT_SYMBOL(s5h1409_attach);
89885558
ST
970
971static struct dvb_frontend_ops s5h1409_ops = {
972
973 .info = {
974 .name = "Samsung S5H1409 QAM/8VSB Frontend",
975 .type = FE_ATSC,
976 .frequency_min = 54000000,
977 .frequency_max = 858000000,
978 .frequency_stepsize = 62500,
979 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
980 },
981
982 .init = s5h1409_init,
983 .i2c_gate_ctrl = s5h1409_i2c_gate_ctrl,
984 .set_frontend = s5h1409_set_frontend,
985 .get_frontend = s5h1409_get_frontend,
986 .get_tune_settings = s5h1409_get_tune_settings,
987 .read_status = s5h1409_read_status,
988 .read_ber = s5h1409_read_ber,
989 .read_signal_strength = s5h1409_read_signal_strength,
990 .read_snr = s5h1409_read_snr,
991 .read_ucblocks = s5h1409_read_ucblocks,
992 .release = s5h1409_release,
993};
994
89885558
ST
995MODULE_DESCRIPTION("Samsung S5H1409 QAM-B/ATSC Demodulator driver");
996MODULE_AUTHOR("Steven Toth");
997MODULE_LICENSE("GPL");
998
3873dd04
MK
999
1000/*
1001 * Local variables:
1002 * c-basic-offset: 8
1003 */