Merge branch 'upstream-linus' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[sfrench/cifs-2.6.git] / drivers / media / dvb / frontends / s5h1409.c
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
32 struct 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         u32 is_qam_locked;
47         u32 qam_state;
48 };
49
50 static int debug = 0;
51 #define dprintk if (debug) printk
52
53 /* Register values to initialise the demod, this will set VSB by default */
54 static struct init_tab {
55         u8      reg;
56         u16     data;
57 } init_tab[] = {
58         { 0x00, 0x0071, },
59         { 0x01, 0x3213, },
60         { 0x09, 0x0025, },
61         { 0x1c, 0x001d, },
62         { 0x1f, 0x002d, },
63         { 0x20, 0x001d, },
64         { 0x22, 0x0022, },
65         { 0x23, 0x0020, },
66         { 0x29, 0x110f, },
67         { 0x2a, 0x10b4, },
68         { 0x2b, 0x10ae, },
69         { 0x2c, 0x0031, },
70         { 0x31, 0x010d, },
71         { 0x32, 0x0100, },
72         { 0x44, 0x0510, },
73         { 0x54, 0x0104, },
74         { 0x58, 0x2222, },
75         { 0x59, 0x1162, },
76         { 0x5a, 0x3211, },
77         { 0x5d, 0x0370, },
78         { 0x5e, 0x0296, },
79         { 0x61, 0x0010, },
80         { 0x63, 0x4a00, },
81         { 0x65, 0x0800, },
82         { 0x71, 0x0003, },
83         { 0x72, 0x0470, },
84         { 0x81, 0x0002, },
85         { 0x82, 0x0600, },
86         { 0x86, 0x0002, },
87         { 0x8a, 0x2c38, },
88         { 0x8b, 0x2a37, },
89         { 0x92, 0x302f, },
90         { 0x93, 0x3332, },
91         { 0x96, 0x000c, },
92         { 0x99, 0x0101, },
93         { 0x9c, 0x2e37, },
94         { 0x9d, 0x2c37, },
95         { 0x9e, 0x2c37, },
96         { 0xab, 0x0100, },
97         { 0xac, 0x1003, },
98         { 0xad, 0x103f, },
99         { 0xe2, 0x0100, },
100         { 0xe3, 0x0000, },
101         { 0x28, 0x1010, },
102         { 0xb1, 0x000e, },
103 };
104
105 /* VSB SNR lookup table */
106 static struct vsb_snr_tab {
107         u16     val;
108         u16     data;
109 } vsb_snr_tab[] = {
110         { 1023, 770, },
111         {  923, 300, },
112         {  918, 295, },
113         {  915, 290, },
114         {  911, 285, },
115         {  906, 280, },
116         {  901, 275, },
117         {  896, 270, },
118         {  891, 265, },
119         {  885, 260, },
120         {  879, 255, },
121         {  873, 250, },
122         {  864, 245, },
123         {  858, 240, },
124         {  850, 235, },
125         {  841, 230, },
126         {  832, 225, },
127         {  823, 220, },
128         {  812, 215, },
129         {  802, 210, },
130         {  788, 205, },
131         {  778, 200, },
132         {  767, 195, },
133         {  753, 190, },
134         {  740, 185, },
135         {  725, 180, },
136         {  707, 175, },
137         {  689, 170, },
138         {  671, 165, },
139         {  656, 160, },
140         {  637, 155, },
141         {  616, 150, },
142         {  542, 145, },
143         {  519, 140, },
144         {  507, 135, },
145         {  497, 130, },
146         {  492, 125, },
147         {  474, 120, },
148         {  300, 111, },
149         {    0,   0, },
150 };
151
152 /* QAM64 SNR lookup table */
153 static struct qam64_snr_tab {
154         u16     val;
155         u16     data;
156 } qam64_snr_tab[] = {
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 };
221
222 /* QAM256 SNR lookup table */
223 static struct qam256_snr_tab {
224         u16     val;
225         u16     data;
226 } qam256_snr_tab[] = {
227         {   12, 400, },
228         {   13, 390, },
229         {   15, 380, },
230         {   17, 360, },
231         {   19, 350, },
232         {   22, 348, },
233         {   23, 346, },
234         {   24, 344, },
235         {   25, 342, },
236         {   26, 340, },
237         {   27, 336, },
238         {   28, 334, },
239         {   29, 332, },
240         {   30, 330, },
241         {   31, 328, },
242         {   32, 326, },
243         {   33, 325, },
244         {   34, 322, },
245         {   35, 320, },
246         {   37, 318, },
247         {   39, 316, },
248         {   40, 314, },
249         {   41, 312, },
250         {   42, 310, },
251         {   43, 308, },
252         {   46, 306, },
253         {   47, 304, },
254         {   49, 302, },
255         {   51, 300, },
256         {   53, 298, },
257         {   54, 297, },
258         {   55, 296, },
259         {   56, 295, },
260         {   57, 294, },
261         {   59, 293, },
262         {   60, 292, },
263         {   61, 291, },
264         {   63, 290, },
265         {   64, 289, },
266         {   65, 288, },
267         {   66, 287, },
268         {   68, 286, },
269         {   69, 285, },
270         {   71, 284, },
271         {   72, 283, },
272         {   74, 282, },
273         {   75, 281, },
274         {   76, 280, },
275         {   77, 279, },
276         {   78, 278, },
277         {   81, 277, },
278         {   83, 276, },
279         {   84, 275, },
280         {   86, 274, },
281         {   87, 273, },
282         {   89, 272, },
283         {   90, 271, },
284         {   92, 270, },
285         {   93, 269, },
286         {   95, 268, },
287         {   96, 267, },
288         {   98, 266, },
289         {  100, 265, },
290         {  102, 264, },
291         {  104, 263, },
292         {  105, 262, },
293         {  106, 261, },
294         {  110, 260, },
295 };
296
297 /* 8 bit registers, 16 bit values */
298 static int s5h1409_writereg(struct s5h1409_state* state, u8 reg, u16 data)
299 {
300         int ret;
301         u8 buf [] = { reg, data >> 8,  data & 0xff };
302
303         struct i2c_msg msg = { .addr = state->config->demod_address,
304                                .flags = 0, .buf = buf, .len = 3 };
305
306         ret = i2c_transfer(state->i2c, &msg, 1);
307
308         if (ret != 1)
309                 printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
310                        "ret == %i)\n", __FUNCTION__, reg, data, ret);
311
312         return (ret != 1) ? -1 : 0;
313 }
314
315 static u16 s5h1409_readreg(struct s5h1409_state* state, u8 reg)
316 {
317         int ret;
318         u8 b0 [] = { reg };
319         u8 b1 [] = { 0, 0 };
320
321         struct i2c_msg msg [] = {
322                 { .addr = state->config->demod_address, .flags = 0,
323                   .buf = b0, .len = 1 },
324                 { .addr = state->config->demod_address, .flags = I2C_M_RD,
325                   .buf = b1, .len = 2 } };
326
327         ret = i2c_transfer(state->i2c, msg, 2);
328
329         if (ret != 2)
330                 printk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret);
331         return (b1[0] << 8) | b1[1];
332 }
333
334 static int s5h1409_softreset(struct dvb_frontend* fe)
335 {
336         struct s5h1409_state* state = fe->demodulator_priv;
337
338         dprintk("%s()\n", __FUNCTION__);
339
340         s5h1409_writereg(state, 0xf5, 0);
341         s5h1409_writereg(state, 0xf5, 1);
342         state->is_qam_locked = 0;
343         state->qam_state = 0;
344         return 0;
345 }
346
347 static int s5h1409_set_if_freq(struct dvb_frontend* fe, int KHz)
348 {
349         struct s5h1409_state* state = fe->demodulator_priv;
350         int ret = 0;
351
352         dprintk("%s(%d KHz)\n", __FUNCTION__, KHz);
353
354         if( (KHz == 44000) || (KHz == 5380) ) {
355                 s5h1409_writereg(state, 0x87, 0x01be);
356                 s5h1409_writereg(state, 0x88, 0x0436);
357                 s5h1409_writereg(state, 0x89, 0x054d);
358         } else
359         if (KHz == 4000) {
360                 s5h1409_writereg(state, 0x87, 0x014b);
361                 s5h1409_writereg(state, 0x88, 0x0cb5);
362                 s5h1409_writereg(state, 0x89, 0x03e2);
363         } else {
364                 printk("%s() Invalid arg = %d KHz\n", __FUNCTION__, KHz);
365                 ret = -1;
366         }
367
368         return ret;
369 }
370
371 static int s5h1409_set_spectralinversion(struct dvb_frontend* fe, int inverted)
372 {
373         struct s5h1409_state* state = fe->demodulator_priv;
374
375         dprintk("%s(%d)\n", __FUNCTION__, inverted);
376
377         if(inverted == 1)
378                 return s5h1409_writereg(state, 0x1b, 0x1101); /* Inverted */
379         else
380                 return s5h1409_writereg(state, 0x1b, 0x0110); /* Normal */
381 }
382
383 static int s5h1409_enable_modulation(struct dvb_frontend* fe,
384                                      fe_modulation_t m)
385 {
386         struct s5h1409_state* state = fe->demodulator_priv;
387
388         dprintk("%s(0x%08x)\n", __FUNCTION__, m);
389
390         switch(m) {
391         case VSB_8:
392                 dprintk("%s() VSB_8\n", __FUNCTION__);
393                 s5h1409_writereg(state, 0xf4, 0);
394                 break;
395         case QAM_64:
396         case QAM_256:
397                 dprintk("%s() QAM_AUTO (64/256)\n", __FUNCTION__);
398                 s5h1409_writereg(state, 0xf4, 1);
399                 s5h1409_writereg(state, 0x85, 0x110);
400                 break;
401         default:
402                 dprintk("%s() Invalid modulation\n", __FUNCTION__);
403                 return -EINVAL;
404         }
405
406         state->current_modulation = m;
407         s5h1409_softreset(fe);
408
409         return 0;
410 }
411
412 static int s5h1409_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
413 {
414         struct s5h1409_state* state = fe->demodulator_priv;
415
416         dprintk("%s(%d)\n", __FUNCTION__, enable);
417
418         if (enable)
419                 return s5h1409_writereg(state, 0xf3, 1);
420         else
421                 return s5h1409_writereg(state, 0xf3, 0);
422 }
423
424 static int s5h1409_set_gpio(struct dvb_frontend* fe, int enable)
425 {
426         struct s5h1409_state* state = fe->demodulator_priv;
427
428         dprintk("%s(%d)\n", __FUNCTION__, enable);
429
430         if (enable)
431                 return s5h1409_writereg(state, 0xe3, 0x1100);
432         else
433                 return s5h1409_writereg(state, 0xe3, 0x1000);
434 }
435
436 static int s5h1409_sleep(struct dvb_frontend* fe, int enable)
437 {
438         struct s5h1409_state* state = fe->demodulator_priv;
439
440         dprintk("%s(%d)\n", __FUNCTION__, enable);
441
442         return s5h1409_writereg(state, 0xf2, enable);
443 }
444
445 static int s5h1409_register_reset(struct dvb_frontend* fe)
446 {
447         struct s5h1409_state* state = fe->demodulator_priv;
448
449         dprintk("%s()\n", __FUNCTION__);
450
451         return s5h1409_writereg(state, 0xfa, 0);
452 }
453
454 static void s5h1409_set_qam_amhum_mode(struct dvb_frontend *fe)
455 {
456         struct s5h1409_state *state = fe->demodulator_priv;
457         u16 reg;
458
459         if (state->is_qam_locked)
460                 return;
461
462         /* QAM EQ lock check */
463         reg = s5h1409_readreg(state, 0xf0);
464
465         if ((reg >> 13) & 0x1) {
466
467                 state->is_qam_locked = 1;
468                 reg &= 0xff;
469
470                 s5h1409_writereg(state, 0x96, 0x00c);
471                 if ((reg < 0x38) || (reg > 0x68) ) {
472                         s5h1409_writereg(state, 0x93, 0x3332);
473                         s5h1409_writereg(state, 0x9e, 0x2c37);
474                 } else {
475                         s5h1409_writereg(state, 0x93, 0x3130);
476                         s5h1409_writereg(state, 0x9e, 0x2836);
477                 }
478
479         } else {
480                 s5h1409_writereg(state, 0x96, 0x0008);
481                 s5h1409_writereg(state, 0x93, 0x3332);
482                 s5h1409_writereg(state, 0x9e, 0x2c37);
483         }
484 }
485
486 static void s5h1409_set_qam_interleave_mode(struct dvb_frontend *fe)
487 {
488         struct s5h1409_state *state = fe->demodulator_priv;
489         u16 reg, reg1, reg2;
490
491         reg = s5h1409_readreg(state, 0xf1);
492
493         /* Master lock */
494         if ((reg >> 15) & 0x1) {
495                 if (state->qam_state != 2) {
496                         state->qam_state = 2;
497                         reg1 = s5h1409_readreg(state, 0xb2);
498                         reg2 = s5h1409_readreg(state, 0xad);
499
500                         s5h1409_writereg(state, 0x96, 0x20);
501                         s5h1409_writereg(state, 0xad,
502                                 ( ((reg1 & 0xf000) >> 4) | (reg2 & 0xf0ff)) );
503                         s5h1409_writereg(state, 0xab, 0x1100);
504                 }
505         } else {
506                 if (state->qam_state != 1) {
507                         state->qam_state = 1;
508                         s5h1409_writereg(state, 0x96, 0x08);
509                         s5h1409_writereg(state, 0xab, 0x1101);
510                 }
511         }
512 }
513
514 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
515 static int s5h1409_set_frontend (struct dvb_frontend* fe,
516                                  struct dvb_frontend_parameters *p)
517 {
518         struct s5h1409_state* state = fe->demodulator_priv;
519
520         dprintk("%s(frequency=%d)\n", __FUNCTION__, p->frequency);
521
522         s5h1409_softreset(fe);
523
524         state->current_frequency = p->frequency;
525
526         s5h1409_enable_modulation(fe, p->u.vsb.modulation);
527
528         /* Allow the demod to settle */
529         msleep(100);
530
531         if (fe->ops.tuner_ops.set_params) {
532                 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 1);
533                 fe->ops.tuner_ops.set_params(fe, p);
534                 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
535         }
536
537         /* Optimize the demod for QAM */
538         if (p->u.vsb.modulation != VSB_8) {
539                 s5h1409_set_qam_amhum_mode(fe);
540                 s5h1409_set_qam_interleave_mode(fe);
541         }
542
543         return 0;
544 }
545
546 /* Reset the demod hardware and reset all of the configuration registers
547    to a default state. */
548 static int s5h1409_init (struct dvb_frontend* fe)
549 {
550         int i;
551
552         struct s5h1409_state* state = fe->demodulator_priv;
553         dprintk("%s()\n", __FUNCTION__);
554
555         s5h1409_sleep(fe, 0);
556         s5h1409_register_reset(fe);
557
558         for (i=0; i < ARRAY_SIZE(init_tab); i++)
559                 s5h1409_writereg(state, init_tab[i].reg, init_tab[i].data);
560
561         /* The datasheet says that after initialisation, VSB is default */
562         state->current_modulation = VSB_8;
563
564         if (state->config->output_mode == S5H1409_SERIAL_OUTPUT)
565                 s5h1409_writereg(state, 0xab, 0x100); /* Serial */
566         else
567                 s5h1409_writereg(state, 0xab, 0x0); /* Parallel */
568
569         s5h1409_set_spectralinversion(fe, state->config->inversion);
570         s5h1409_set_if_freq(fe, state->config->if_freq);
571         s5h1409_set_gpio(fe, state->config->gpio);
572         s5h1409_softreset(fe);
573
574         /* Note: Leaving the I2C gate closed. */
575         s5h1409_i2c_gate_ctrl(fe, 0);
576
577         return 0;
578 }
579
580 static int s5h1409_read_status(struct dvb_frontend* fe, fe_status_t* status)
581 {
582         struct s5h1409_state* state = fe->demodulator_priv;
583         u16 reg;
584         u32 tuner_status = 0;
585
586         *status = 0;
587
588         /* Get the demodulator status */
589         reg = s5h1409_readreg(state, 0xf1);
590         if(reg & 0x1000)
591                 *status |= FE_HAS_VITERBI;
592         if(reg & 0x8000)
593                 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
594
595         switch(state->config->status_mode) {
596         case S5H1409_DEMODLOCKING:
597                 if (*status & FE_HAS_VITERBI)
598                         *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
599                 break;
600         case S5H1409_TUNERLOCKING:
601                 /* Get the tuner status */
602                 if (fe->ops.tuner_ops.get_status) {
603                         if (fe->ops.i2c_gate_ctrl)
604                                 fe->ops.i2c_gate_ctrl(fe, 1);
605
606                         fe->ops.tuner_ops.get_status(fe, &tuner_status);
607
608                         if (fe->ops.i2c_gate_ctrl)
609                                 fe->ops.i2c_gate_ctrl(fe, 0);
610                 }
611                 if (tuner_status)
612                         *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
613                 break;
614         }
615
616         dprintk("%s() status 0x%08x\n", __FUNCTION__, *status);
617
618         return 0;
619 }
620
621 static int s5h1409_qam256_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
622 {
623         int i, ret = -EINVAL;
624         dprintk("%s()\n", __FUNCTION__);
625
626         for (i=0; i < ARRAY_SIZE(qam256_snr_tab); i++) {
627                 if (v < qam256_snr_tab[i].val) {
628                         *snr = qam256_snr_tab[i].data;
629                         ret = 0;
630                         break;
631                 }
632         }
633         return ret;
634 }
635
636 static int s5h1409_qam64_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
637 {
638         int i, ret = -EINVAL;
639         dprintk("%s()\n", __FUNCTION__);
640
641         for (i=0; i < ARRAY_SIZE(qam64_snr_tab); i++) {
642                 if (v < qam64_snr_tab[i].val) {
643                         *snr = qam64_snr_tab[i].data;
644                         ret = 0;
645                         break;
646                 }
647         }
648         return ret;
649 }
650
651 static int s5h1409_vsb_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
652 {
653         int i, ret = -EINVAL;
654         dprintk("%s()\n", __FUNCTION__);
655
656         for (i=0; i < ARRAY_SIZE(vsb_snr_tab); i++) {
657                 if (v > vsb_snr_tab[i].val) {
658                         *snr = vsb_snr_tab[i].data;
659                         ret = 0;
660                         break;
661                 }
662         }
663         dprintk("%s() snr=%d\n", __FUNCTION__, *snr);
664         return ret;
665 }
666
667 static int s5h1409_read_snr(struct dvb_frontend* fe, u16* snr)
668 {
669         struct s5h1409_state* state = fe->demodulator_priv;
670         u16 reg;
671         dprintk("%s()\n", __FUNCTION__);
672
673         reg = s5h1409_readreg(state, 0xf1) & 0x1ff;
674
675         switch(state->current_modulation) {
676         case QAM_64:
677                 return s5h1409_qam64_lookup_snr(fe, snr, reg);
678         case QAM_256:
679                 return s5h1409_qam256_lookup_snr(fe, snr, reg);
680         case VSB_8:
681                 return s5h1409_vsb_lookup_snr(fe, snr, reg);
682         default:
683                 break;
684         }
685
686         return -EINVAL;
687 }
688
689 static int s5h1409_read_signal_strength(struct dvb_frontend* fe,
690                                         u16* signal_strength)
691 {
692         return s5h1409_read_snr(fe, signal_strength);
693 }
694
695 static int s5h1409_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
696 {
697         struct s5h1409_state* state = fe->demodulator_priv;
698
699         *ucblocks = s5h1409_readreg(state, 0xb5);
700
701         return 0;
702 }
703
704 static int s5h1409_read_ber(struct dvb_frontend* fe, u32* ber)
705 {
706         return s5h1409_read_ucblocks(fe, ber);
707 }
708
709 static int s5h1409_get_frontend(struct dvb_frontend* fe,
710                                 struct dvb_frontend_parameters *p)
711 {
712         struct s5h1409_state* state = fe->demodulator_priv;
713
714         p->frequency = state->current_frequency;
715         p->u.vsb.modulation = state->current_modulation;
716
717         return 0;
718 }
719
720 static int s5h1409_get_tune_settings(struct dvb_frontend* fe,
721                                      struct dvb_frontend_tune_settings *tune)
722 {
723         tune->min_delay_ms = 1000;
724         return 0;
725 }
726
727 static void s5h1409_release(struct dvb_frontend* fe)
728 {
729         struct s5h1409_state* state = fe->demodulator_priv;
730         kfree(state);
731 }
732
733 static struct dvb_frontend_ops s5h1409_ops;
734
735 struct dvb_frontend* s5h1409_attach(const struct s5h1409_config* config,
736                                     struct i2c_adapter* i2c)
737 {
738         struct s5h1409_state* state = NULL;
739
740         /* allocate memory for the internal state */
741         state = kmalloc(sizeof(struct s5h1409_state), GFP_KERNEL);
742         if (state == NULL)
743                 goto error;
744
745         /* setup the state */
746         state->config = config;
747         state->i2c = i2c;
748         state->current_modulation = 0;
749
750         /* check if the demod exists */
751         if (s5h1409_readreg(state, 0x04) != 0x0066)
752                 goto error;
753
754         /* create dvb_frontend */
755         memcpy(&state->frontend.ops, &s5h1409_ops,
756                sizeof(struct dvb_frontend_ops));
757         state->frontend.demodulator_priv = state;
758
759         /* Note: Leaving the I2C gate open here. */
760         s5h1409_writereg(state, 0xf3, 1);
761
762         return &state->frontend;
763
764 error:
765         kfree(state);
766         return NULL;
767 }
768
769 static struct dvb_frontend_ops s5h1409_ops = {
770
771         .info = {
772                 .name                   = "Samsung S5H1409 QAM/8VSB Frontend",
773                 .type                   = FE_ATSC,
774                 .frequency_min          = 54000000,
775                 .frequency_max          = 858000000,
776                 .frequency_stepsize     = 62500,
777                 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
778         },
779
780         .init                 = s5h1409_init,
781         .i2c_gate_ctrl        = s5h1409_i2c_gate_ctrl,
782         .set_frontend         = s5h1409_set_frontend,
783         .get_frontend         = s5h1409_get_frontend,
784         .get_tune_settings    = s5h1409_get_tune_settings,
785         .read_status          = s5h1409_read_status,
786         .read_ber             = s5h1409_read_ber,
787         .read_signal_strength = s5h1409_read_signal_strength,
788         .read_snr             = s5h1409_read_snr,
789         .read_ucblocks        = s5h1409_read_ucblocks,
790         .release              = s5h1409_release,
791 };
792
793 module_param(debug, int, 0644);
794 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
795
796 MODULE_DESCRIPTION("Samsung S5H1409 QAM-B/ATSC Demodulator driver");
797 MODULE_AUTHOR("Steven Toth");
798 MODULE_LICENSE("GPL");
799
800 EXPORT_SYMBOL(s5h1409_attach);
801
802 /*
803  * Local variables:
804  * c-basic-offset: 8
805  */