]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/dvb/frontends/s5h1411.c
V4L/DVB (9311): s5h1411: bugfix: Setting serial or parallel mode could destroy bits
[linux-2.6-omap-h63xx.git] / drivers / media / dvb / frontends / s5h1411.c
1 /*
2     Samsung S5H1411 VSB/QAM demodulator driver
3
4     Copyright (C) 2008 Steven Toth <stoth@linuxtv.org>
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 "s5h1411.h"
30
31 struct s5h1411_state {
32
33         struct i2c_adapter *i2c;
34
35         /* configuration settings */
36         const struct s5h1411_config *config;
37
38         struct dvb_frontend frontend;
39
40         fe_modulation_t current_modulation;
41
42         u32 current_frequency;
43         int if_freq;
44
45         u8 inversion;
46 };
47
48 static int debug;
49
50 #define dprintk(arg...) do {    \
51         if (debug)              \
52                 printk(arg);    \
53         } while (0)
54
55 /* Register values to initialise the demod, defaults to VSB */
56 static struct init_tab {
57         u8      addr;
58         u8      reg;
59         u16     data;
60 } init_tab[] = {
61         { S5H1411_I2C_TOP_ADDR, 0x00, 0x0071, },
62         { S5H1411_I2C_TOP_ADDR, 0x08, 0x0047, },
63         { S5H1411_I2C_TOP_ADDR, 0x1c, 0x0400, },
64         { S5H1411_I2C_TOP_ADDR, 0x1e, 0x0370, },
65         { S5H1411_I2C_TOP_ADDR, 0x1f, 0x342c, },
66         { S5H1411_I2C_TOP_ADDR, 0x24, 0x0231, },
67         { S5H1411_I2C_TOP_ADDR, 0x25, 0x1011, },
68         { S5H1411_I2C_TOP_ADDR, 0x26, 0x0f07, },
69         { S5H1411_I2C_TOP_ADDR, 0x27, 0x0f04, },
70         { S5H1411_I2C_TOP_ADDR, 0x28, 0x070f, },
71         { S5H1411_I2C_TOP_ADDR, 0x29, 0x2820, },
72         { S5H1411_I2C_TOP_ADDR, 0x2a, 0x102e, },
73         { S5H1411_I2C_TOP_ADDR, 0x2b, 0x0220, },
74         { S5H1411_I2C_TOP_ADDR, 0x2e, 0x0d0e, },
75         { S5H1411_I2C_TOP_ADDR, 0x2f, 0x1013, },
76         { S5H1411_I2C_TOP_ADDR, 0x31, 0x171b, },
77         { S5H1411_I2C_TOP_ADDR, 0x32, 0x0e0f, },
78         { S5H1411_I2C_TOP_ADDR, 0x33, 0x0f10, },
79         { S5H1411_I2C_TOP_ADDR, 0x34, 0x170e, },
80         { S5H1411_I2C_TOP_ADDR, 0x35, 0x4b10, },
81         { S5H1411_I2C_TOP_ADDR, 0x36, 0x0f17, },
82         { S5H1411_I2C_TOP_ADDR, 0x3c, 0x1577, },
83         { S5H1411_I2C_TOP_ADDR, 0x3d, 0x081a, },
84         { S5H1411_I2C_TOP_ADDR, 0x3e, 0x77ee, },
85         { S5H1411_I2C_TOP_ADDR, 0x40, 0x1e09, },
86         { S5H1411_I2C_TOP_ADDR, 0x41, 0x0f0c, },
87         { S5H1411_I2C_TOP_ADDR, 0x42, 0x1f10, },
88         { S5H1411_I2C_TOP_ADDR, 0x4d, 0x0509, },
89         { S5H1411_I2C_TOP_ADDR, 0x4e, 0x0a00, },
90         { S5H1411_I2C_TOP_ADDR, 0x50, 0x0000, },
91         { S5H1411_I2C_TOP_ADDR, 0x5b, 0x0000, },
92         { S5H1411_I2C_TOP_ADDR, 0x5c, 0x0008, },
93         { S5H1411_I2C_TOP_ADDR, 0x57, 0x1101, },
94         { S5H1411_I2C_TOP_ADDR, 0x65, 0x007c, },
95         { S5H1411_I2C_TOP_ADDR, 0x68, 0x0512, },
96         { S5H1411_I2C_TOP_ADDR, 0x69, 0x0258, },
97         { S5H1411_I2C_TOP_ADDR, 0x70, 0x0004, },
98         { S5H1411_I2C_TOP_ADDR, 0x71, 0x0007, },
99         { S5H1411_I2C_TOP_ADDR, 0x76, 0x00a9, },
100         { S5H1411_I2C_TOP_ADDR, 0x78, 0x3141, },
101         { S5H1411_I2C_TOP_ADDR, 0x7a, 0x3141, },
102         { S5H1411_I2C_TOP_ADDR, 0xb3, 0x8003, },
103         { S5H1411_I2C_TOP_ADDR, 0xb5, 0xa6bb, },
104         { S5H1411_I2C_TOP_ADDR, 0xb6, 0x0609, },
105         { S5H1411_I2C_TOP_ADDR, 0xb7, 0x2f06, },
106         { S5H1411_I2C_TOP_ADDR, 0xb8, 0x003f, },
107         { S5H1411_I2C_TOP_ADDR, 0xb9, 0x2700, },
108         { S5H1411_I2C_TOP_ADDR, 0xba, 0xfac8, },
109         { S5H1411_I2C_TOP_ADDR, 0xbe, 0x1003, },
110         { S5H1411_I2C_TOP_ADDR, 0xbf, 0x103f, },
111         { S5H1411_I2C_TOP_ADDR, 0xce, 0x2000, },
112         { S5H1411_I2C_TOP_ADDR, 0xcf, 0x0800, },
113         { S5H1411_I2C_TOP_ADDR, 0xd0, 0x0800, },
114         { S5H1411_I2C_TOP_ADDR, 0xd1, 0x0400, },
115         { S5H1411_I2C_TOP_ADDR, 0xd2, 0x0800, },
116         { S5H1411_I2C_TOP_ADDR, 0xd3, 0x2000, },
117         { S5H1411_I2C_TOP_ADDR, 0xd4, 0x3000, },
118         { S5H1411_I2C_TOP_ADDR, 0xdb, 0x4a9b, },
119         { S5H1411_I2C_TOP_ADDR, 0xdc, 0x1000, },
120         { S5H1411_I2C_TOP_ADDR, 0xde, 0x0001, },
121         { S5H1411_I2C_TOP_ADDR, 0xdf, 0x0000, },
122         { S5H1411_I2C_TOP_ADDR, 0xe3, 0x0301, },
123         { S5H1411_I2C_QAM_ADDR, 0xf3, 0x0000, },
124         { S5H1411_I2C_QAM_ADDR, 0xf3, 0x0001, },
125         { S5H1411_I2C_QAM_ADDR, 0x08, 0x0600, },
126         { S5H1411_I2C_QAM_ADDR, 0x18, 0x4201, },
127         { S5H1411_I2C_QAM_ADDR, 0x1e, 0x6476, },
128         { S5H1411_I2C_QAM_ADDR, 0x21, 0x0830, },
129         { S5H1411_I2C_QAM_ADDR, 0x0c, 0x5679, },
130         { S5H1411_I2C_QAM_ADDR, 0x0d, 0x579b, },
131         { S5H1411_I2C_QAM_ADDR, 0x24, 0x0102, },
132         { S5H1411_I2C_QAM_ADDR, 0x31, 0x7488, },
133         { S5H1411_I2C_QAM_ADDR, 0x32, 0x0a08, },
134         { S5H1411_I2C_QAM_ADDR, 0x3d, 0x8689, },
135         { S5H1411_I2C_QAM_ADDR, 0x49, 0x0048, },
136         { S5H1411_I2C_QAM_ADDR, 0x57, 0x2012, },
137         { S5H1411_I2C_QAM_ADDR, 0x5d, 0x7676, },
138         { S5H1411_I2C_QAM_ADDR, 0x04, 0x0400, },
139         { S5H1411_I2C_QAM_ADDR, 0x58, 0x00c0, },
140         { S5H1411_I2C_QAM_ADDR, 0x5b, 0x0100, },
141 };
142
143 /* VSB SNR lookup table */
144 static struct vsb_snr_tab {
145         u16     val;
146         u16     data;
147 } vsb_snr_tab[] = {
148         {  0x39f, 300, },
149         {  0x39b, 295, },
150         {  0x397, 290, },
151         {  0x394, 285, },
152         {  0x38f, 280, },
153         {  0x38b, 275, },
154         {  0x387, 270, },
155         {  0x382, 265, },
156         {  0x37d, 260, },
157         {  0x377, 255, },
158         {  0x370, 250, },
159         {  0x36a, 245, },
160         {  0x364, 240, },
161         {  0x35b, 235, },
162         {  0x353, 230, },
163         {  0x349, 225, },
164         {  0x340, 320, },
165         {  0x337, 215, },
166         {  0x327, 210, },
167         {  0x31b, 205, },
168         {  0x310, 200, },
169         {  0x302, 195, },
170         {  0x2f3, 190, },
171         {  0x2e4, 185, },
172         {  0x2d7, 180, },
173         {  0x2cd, 175, },
174         {  0x2bb, 170, },
175         {  0x2a9, 165, },
176         {  0x29e, 160, },
177         {  0x284, 155, },
178         {  0x27a, 150, },
179         {  0x260, 145, },
180         {  0x23a, 140, },
181         {  0x224, 135, },
182         {  0x213, 130, },
183         {  0x204, 125, },
184         {  0x1fe, 120, },
185         {      0,   0, },
186 };
187
188 /* QAM64 SNR lookup table */
189 static struct qam64_snr_tab {
190         u16     val;
191         u16     data;
192 } qam64_snr_tab[] = {
193         {  0x0001,   0, },
194         {  0x0af0, 300, },
195         {  0x0d80, 290, },
196         {  0x10a0, 280, },
197         {  0x14b5, 270, },
198         {  0x1590, 268, },
199         {  0x1680, 266, },
200         {  0x17b0, 264, },
201         {  0x18c0, 262, },
202         {  0x19b0, 260, },
203         {  0x1ad0, 258, },
204         {  0x1d00, 256, },
205         {  0x1da0, 254, },
206         {  0x1ef0, 252, },
207         {  0x2050, 250, },
208         {  0x20f0, 249, },
209         {  0x21d0, 248, },
210         {  0x22b0, 247, },
211         {  0x23a0, 246, },
212         {  0x2470, 245, },
213         {  0x24f0, 244, },
214         {  0x25a0, 243, },
215         {  0x26c0, 242, },
216         {  0x27b0, 241, },
217         {  0x28d0, 240, },
218         {  0x29b0, 239, },
219         {  0x2ad0, 238, },
220         {  0x2ba0, 237, },
221         {  0x2c80, 236, },
222         {  0x2d20, 235, },
223         {  0x2e00, 234, },
224         {  0x2f10, 233, },
225         {  0x3050, 232, },
226         {  0x3190, 231, },
227         {  0x3300, 230, },
228         {  0x3340, 229, },
229         {  0x3200, 228, },
230         {  0x3550, 227, },
231         {  0x3610, 226, },
232         {  0x3600, 225, },
233         {  0x3700, 224, },
234         {  0x3800, 223, },
235         {  0x3920, 222, },
236         {  0x3a20, 221, },
237         {  0x3b30, 220, },
238         {  0x3d00, 219, },
239         {  0x3e00, 218, },
240         {  0x4000, 217, },
241         {  0x4100, 216, },
242         {  0x4300, 215, },
243         {  0x4400, 214, },
244         {  0x4600, 213, },
245         {  0x4700, 212, },
246         {  0x4800, 211, },
247         {  0x4a00, 210, },
248         {  0x4b00, 209, },
249         {  0x4d00, 208, },
250         {  0x4f00, 207, },
251         {  0x5050, 206, },
252         {  0x5200, 205, },
253         {  0x53c0, 204, },
254         {  0x5450, 203, },
255         {  0x5650, 202, },
256         {  0x5820, 201, },
257         {  0x6000, 200, },
258         {  0xffff,   0, },
259 };
260
261 /* QAM256 SNR lookup table */
262 static struct qam256_snr_tab {
263         u16     val;
264         u16     data;
265 } qam256_snr_tab[] = {
266         {  0x0001,   0, },
267         {  0x0970, 400, },
268         {  0x0a90, 390, },
269         {  0x0b90, 380, },
270         {  0x0d90, 370, },
271         {  0x0ff0, 360, },
272         {  0x1240, 350, },
273         {  0x1345, 348, },
274         {  0x13c0, 346, },
275         {  0x14c0, 344, },
276         {  0x1500, 342, },
277         {  0x1610, 340, },
278         {  0x1700, 338, },
279         {  0x1800, 336, },
280         {  0x18b0, 334, },
281         {  0x1900, 332, },
282         {  0x1ab0, 330, },
283         {  0x1bc0, 328, },
284         {  0x1cb0, 326, },
285         {  0x1db0, 324, },
286         {  0x1eb0, 322, },
287         {  0x2030, 320, },
288         {  0x2200, 318, },
289         {  0x2280, 316, },
290         {  0x2410, 314, },
291         {  0x25b0, 312, },
292         {  0x27a0, 310, },
293         {  0x2840, 308, },
294         {  0x29d0, 306, },
295         {  0x2b10, 304, },
296         {  0x2d30, 302, },
297         {  0x2f20, 300, },
298         {  0x30c0, 298, },
299         {  0x3260, 297, },
300         {  0x32c0, 296, },
301         {  0x3300, 295, },
302         {  0x33b0, 294, },
303         {  0x34b0, 293, },
304         {  0x35a0, 292, },
305         {  0x3650, 291, },
306         {  0x3800, 290, },
307         {  0x3900, 289, },
308         {  0x3a50, 288, },
309         {  0x3b30, 287, },
310         {  0x3cb0, 286, },
311         {  0x3e20, 285, },
312         {  0x3fa0, 284, },
313         {  0x40a0, 283, },
314         {  0x41c0, 282, },
315         {  0x42f0, 281, },
316         {  0x44a0, 280, },
317         {  0x4600, 279, },
318         {  0x47b0, 278, },
319         {  0x4900, 277, },
320         {  0x4a00, 276, },
321         {  0x4ba0, 275, },
322         {  0x4d00, 274, },
323         {  0x4f00, 273, },
324         {  0x5000, 272, },
325         {  0x51f0, 272, },
326         {  0x53a0, 270, },
327         {  0x5520, 269, },
328         {  0x5700, 268, },
329         {  0x5800, 267, },
330         {  0x5a00, 266, },
331         {  0x5c00, 265, },
332         {  0x5d00, 264, },
333         {  0x5f00, 263, },
334         {  0x6000, 262, },
335         {  0x6200, 261, },
336         {  0x6400, 260, },
337         {  0xffff,   0, },
338 };
339
340 /* 8 bit registers, 16 bit values */
341 static int s5h1411_writereg(struct s5h1411_state *state,
342         u8 addr, u8 reg, u16 data)
343 {
344         int ret;
345         u8 buf[] = { reg, data >> 8,  data & 0xff };
346
347         struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = buf, .len = 3 };
348
349         ret = i2c_transfer(state->i2c, &msg, 1);
350
351         if (ret != 1)
352                 printk(KERN_ERR "%s: writereg error 0x%02x 0x%02x 0x%04x, "
353                        "ret == %i)\n", __func__, addr, reg, data, ret);
354
355         return (ret != 1) ? -1 : 0;
356 }
357
358 static u16 s5h1411_readreg(struct s5h1411_state *state, u8 addr, u8 reg)
359 {
360         int ret;
361         u8 b0[] = { reg };
362         u8 b1[] = { 0, 0 };
363
364         struct i2c_msg msg[] = {
365                 { .addr = addr, .flags = 0, .buf = b0, .len = 1 },
366                 { .addr = addr, .flags = I2C_M_RD, .buf = b1, .len = 2 } };
367
368         ret = i2c_transfer(state->i2c, msg, 2);
369
370         if (ret != 2)
371                 printk(KERN_ERR "%s: readreg error (ret == %i)\n",
372                         __func__, ret);
373         return (b1[0] << 8) | b1[1];
374 }
375
376 static int s5h1411_softreset(struct dvb_frontend *fe)
377 {
378         struct s5h1411_state *state = fe->demodulator_priv;
379
380         dprintk("%s()\n", __func__);
381
382         s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf7, 0);
383         s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf7, 1);
384         return 0;
385 }
386
387 static int s5h1411_set_if_freq(struct dvb_frontend *fe, int KHz)
388 {
389         struct s5h1411_state *state = fe->demodulator_priv;
390
391         dprintk("%s(%d KHz)\n", __func__, KHz);
392
393         switch (KHz) {
394         case 3250:
395                 s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x38, 0x10d5);
396                 s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x39, 0x5342);
397                 s5h1411_writereg(state, S5H1411_I2C_QAM_ADDR, 0x2c, 0x10d9);
398                 break;
399         case 3500:
400                 s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x38, 0x1225);
401                 s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x39, 0x1e96);
402                 s5h1411_writereg(state, S5H1411_I2C_QAM_ADDR, 0x2c, 0x1225);
403                 break;
404         case 4000:
405                 s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x38, 0x14bc);
406                 s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x39, 0xb53e);
407                 s5h1411_writereg(state, S5H1411_I2C_QAM_ADDR, 0x2c, 0x14bd);
408                 break;
409         default:
410                 dprintk("%s(%d KHz) Invalid, defaulting to 5380\n",
411                         __func__, KHz);
412                 /* no break, need to continue */
413         case 5380:
414         case 44000:
415                 s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x38, 0x1be4);
416                 s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x39, 0x3655);
417                 s5h1411_writereg(state, S5H1411_I2C_QAM_ADDR, 0x2c, 0x1be4);
418                 break;
419         }
420
421         state->if_freq = KHz;
422
423         return 0;
424 }
425
426 static int s5h1411_set_mpeg_timing(struct dvb_frontend *fe, int mode)
427 {
428         struct s5h1411_state *state = fe->demodulator_priv;
429         u16 val;
430
431         dprintk("%s(%d)\n", __func__, mode);
432
433         val = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xbe) & 0xcfff;
434         switch (mode) {
435         case S5H1411_MPEGTIMING_CONTINOUS_INVERTING_CLOCK:
436                 val |= 0x0000;
437                 break;
438         case S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK:
439                 dprintk("%s(%d) Mode1 or Defaulting\n", __func__, mode);
440                 val |= 0x1000;
441                 break;
442         case S5H1411_MPEGTIMING_NONCONTINOUS_INVERTING_CLOCK:
443                 val |= 0x2000;
444                 break;
445         case S5H1411_MPEGTIMING_NONCONTINOUS_NONINVERTING_CLOCK:
446                 val |= 0x3000;
447                 break;
448         default:
449                 return -EINVAL;
450         }
451
452         /* Configure MPEG Signal Timing charactistics */
453         return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xbe, val);
454 }
455
456 static int s5h1411_set_spectralinversion(struct dvb_frontend *fe, int inversion)
457 {
458         struct s5h1411_state *state = fe->demodulator_priv;
459         u16 val;
460
461         dprintk("%s(%d)\n", __func__, inversion);
462         val = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0x24) & ~0x1000;
463
464         if (inversion == 1)
465                 val |= 0x1000; /* Inverted */
466         else
467                 val |= 0x0000;
468
469         state->inversion = inversion;
470         return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x24, val);
471 }
472
473 static int s5h1411_set_serialmode(struct dvb_frontend *fe, int serial)
474 {
475         struct s5h1411_state *state = fe->demodulator_priv;
476         u16 val;
477
478         dprintk("%s(%d)\n", __func__, serial);
479         val = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xbd) & ~0x100;
480
481         if (serial == 1)
482                 val |= 0x100;
483
484         return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xbd, val);
485 }
486
487 static int s5h1411_enable_modulation(struct dvb_frontend *fe,
488                                      fe_modulation_t m)
489 {
490         struct s5h1411_state *state = fe->demodulator_priv;
491
492         dprintk("%s(0x%08x)\n", __func__, m);
493
494         switch (m) {
495         case VSB_8:
496                 dprintk("%s() VSB_8\n", __func__);
497                 s5h1411_set_if_freq(fe, state->config->vsb_if);
498                 s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x00, 0x71);
499                 s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf6, 0x00);
500                 s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xcd, 0xf1);
501                 break;
502         case QAM_64:
503         case QAM_256:
504         case QAM_AUTO:
505                 dprintk("%s() QAM_AUTO (64/256)\n", __func__);
506                 s5h1411_set_if_freq(fe, state->config->qam_if);
507                 s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x00, 0x0171);
508                 s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf6, 0x0001);
509                 s5h1411_writereg(state, S5H1411_I2C_QAM_ADDR, 0x16, 0x1101);
510                 s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xcd, 0x00f0);
511                 break;
512         default:
513                 dprintk("%s() Invalid modulation\n", __func__);
514                 return -EINVAL;
515         }
516
517         state->current_modulation = m;
518         s5h1411_softreset(fe);
519
520         return 0;
521 }
522
523 static int s5h1411_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
524 {
525         struct s5h1411_state *state = fe->demodulator_priv;
526
527         dprintk("%s(%d)\n", __func__, enable);
528
529         if (enable)
530                 return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf5, 1);
531         else
532                 return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf5, 0);
533 }
534
535 static int s5h1411_set_gpio(struct dvb_frontend *fe, int enable)
536 {
537         struct s5h1411_state *state = fe->demodulator_priv;
538         u16 val;
539
540         dprintk("%s(%d)\n", __func__, enable);
541
542         val = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xe0) & ~0x02;
543
544         if (enable)
545                 return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xe0,
546                                 val | 0x02);
547         else
548                 return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xe0, val);
549 }
550
551 static int s5h1411_sleep(struct dvb_frontend *fe, int enable)
552 {
553         struct s5h1411_state *state = fe->demodulator_priv;
554
555         dprintk("%s(%d)\n", __func__, enable);
556
557         if (enable)
558                 s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf4, 1);
559         else {
560                 s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf4, 0);
561                 s5h1411_softreset(fe);
562         }
563
564         return 0;
565 }
566
567 static int s5h1411_register_reset(struct dvb_frontend *fe)
568 {
569         struct s5h1411_state *state = fe->demodulator_priv;
570
571         dprintk("%s()\n", __func__);
572
573         return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf3, 0);
574 }
575
576 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
577 static int s5h1411_set_frontend(struct dvb_frontend *fe,
578         struct dvb_frontend_parameters *p)
579 {
580         struct s5h1411_state *state = fe->demodulator_priv;
581
582         dprintk("%s(frequency=%d)\n", __func__, p->frequency);
583
584         s5h1411_softreset(fe);
585
586         state->current_frequency = p->frequency;
587
588         s5h1411_enable_modulation(fe, p->u.vsb.modulation);
589
590         /* Allow the demod to settle */
591         msleep(100);
592
593         if (fe->ops.tuner_ops.set_params) {
594                 if (fe->ops.i2c_gate_ctrl)
595                         fe->ops.i2c_gate_ctrl(fe, 1);
596
597                 fe->ops.tuner_ops.set_params(fe, p);
598
599                 if (fe->ops.i2c_gate_ctrl)
600                         fe->ops.i2c_gate_ctrl(fe, 0);
601         }
602
603         return 0;
604 }
605
606 /* Reset the demod hardware and reset all of the configuration registers
607    to a default state. */
608 static int s5h1411_init(struct dvb_frontend *fe)
609 {
610         struct s5h1411_state *state = fe->demodulator_priv;
611         int i;
612
613         dprintk("%s()\n", __func__);
614
615         s5h1411_sleep(fe, 0);
616         s5h1411_register_reset(fe);
617
618         for (i = 0; i < ARRAY_SIZE(init_tab); i++)
619                 s5h1411_writereg(state, init_tab[i].addr,
620                         init_tab[i].reg,
621                         init_tab[i].data);
622
623         /* The datasheet says that after initialisation, VSB is default */
624         state->current_modulation = VSB_8;
625
626         if (state->config->output_mode == S5H1411_SERIAL_OUTPUT)
627                 /* Serial */
628                 s5h1411_set_serialmode(fe, 1);
629         else
630                 /* Parallel */
631                 s5h1411_set_serialmode(fe, 0);
632
633         s5h1411_set_spectralinversion(fe, state->config->inversion);
634         s5h1411_set_if_freq(fe, state->config->vsb_if);
635         s5h1411_set_gpio(fe, state->config->gpio);
636         s5h1411_set_mpeg_timing(fe, state->config->mpeg_timing);
637         s5h1411_softreset(fe);
638
639         /* Note: Leaving the I2C gate closed. */
640         s5h1411_i2c_gate_ctrl(fe, 0);
641
642         return 0;
643 }
644
645 static int s5h1411_read_status(struct dvb_frontend *fe, fe_status_t *status)
646 {
647         struct s5h1411_state *state = fe->demodulator_priv;
648         u16 reg;
649         u32 tuner_status = 0;
650
651         *status = 0;
652
653         /* Register F2 bit 15 = Master Lock, removed */
654
655         switch (state->current_modulation) {
656         case QAM_64:
657         case QAM_256:
658                 reg = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xf0);
659                 if (reg & 0x10) /* QAM FEC Lock */
660                         *status |= FE_HAS_SYNC | FE_HAS_LOCK;
661                 if (reg & 0x100) /* QAM EQ Lock */
662                         *status |= FE_HAS_VITERBI | FE_HAS_CARRIER | FE_HAS_SIGNAL;
663
664                 break;
665         case VSB_8:
666                 reg = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xf2);
667                 if (reg & 0x1000) /* FEC Lock */
668                         *status |= FE_HAS_SYNC | FE_HAS_LOCK;
669                 if (reg & 0x2000) /* EQ Lock */
670                         *status |= FE_HAS_VITERBI | FE_HAS_CARRIER | FE_HAS_SIGNAL;
671
672                 reg = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0x53);
673                 if (reg & 0x1) /* AFC Lock */
674                         *status |= FE_HAS_SIGNAL;
675
676                 break;
677         default:
678                 return -EINVAL;
679         }
680
681         switch (state->config->status_mode) {
682         case S5H1411_DEMODLOCKING:
683                 if (*status & FE_HAS_VITERBI)
684                         *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
685                 break;
686         case S5H1411_TUNERLOCKING:
687                 /* Get the tuner status */
688                 if (fe->ops.tuner_ops.get_status) {
689                         if (fe->ops.i2c_gate_ctrl)
690                                 fe->ops.i2c_gate_ctrl(fe, 1);
691
692                         fe->ops.tuner_ops.get_status(fe, &tuner_status);
693
694                         if (fe->ops.i2c_gate_ctrl)
695                                 fe->ops.i2c_gate_ctrl(fe, 0);
696                 }
697                 if (tuner_status)
698                         *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
699                 break;
700         }
701
702         dprintk("%s() status 0x%08x\n", __func__, *status);
703
704         return 0;
705 }
706
707 static int s5h1411_qam256_lookup_snr(struct dvb_frontend *fe, u16 *snr, u16 v)
708 {
709         int i, ret = -EINVAL;
710         dprintk("%s()\n", __func__);
711
712         for (i = 0; i < ARRAY_SIZE(qam256_snr_tab); i++) {
713                 if (v < qam256_snr_tab[i].val) {
714                         *snr = qam256_snr_tab[i].data;
715                         ret = 0;
716                         break;
717                 }
718         }
719         return ret;
720 }
721
722 static int s5h1411_qam64_lookup_snr(struct dvb_frontend *fe, u16 *snr, u16 v)
723 {
724         int i, ret = -EINVAL;
725         dprintk("%s()\n", __func__);
726
727         for (i = 0; i < ARRAY_SIZE(qam64_snr_tab); i++) {
728                 if (v < qam64_snr_tab[i].val) {
729                         *snr = qam64_snr_tab[i].data;
730                         ret = 0;
731                         break;
732                 }
733         }
734         return ret;
735 }
736
737 static int s5h1411_vsb_lookup_snr(struct dvb_frontend *fe, u16 *snr, u16 v)
738 {
739         int i, ret = -EINVAL;
740         dprintk("%s()\n", __func__);
741
742         for (i = 0; i < ARRAY_SIZE(vsb_snr_tab); i++) {
743                 if (v > vsb_snr_tab[i].val) {
744                         *snr = vsb_snr_tab[i].data;
745                         ret = 0;
746                         break;
747                 }
748         }
749         dprintk("%s() snr=%d\n", __func__, *snr);
750         return ret;
751 }
752
753 static int s5h1411_read_snr(struct dvb_frontend *fe, u16 *snr)
754 {
755         struct s5h1411_state *state = fe->demodulator_priv;
756         u16 reg;
757         dprintk("%s()\n", __func__);
758
759         switch (state->current_modulation) {
760         case QAM_64:
761                 reg = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xf1);
762                 return s5h1411_qam64_lookup_snr(fe, snr, reg);
763         case QAM_256:
764                 reg = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xf1);
765                 return s5h1411_qam256_lookup_snr(fe, snr, reg);
766         case VSB_8:
767                 reg = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR,
768                         0xf2) & 0x3ff;
769                 return s5h1411_vsb_lookup_snr(fe, snr, reg);
770         default:
771                 break;
772         }
773
774         return -EINVAL;
775 }
776
777 static int s5h1411_read_signal_strength(struct dvb_frontend *fe,
778         u16 *signal_strength)
779 {
780         return s5h1411_read_snr(fe, signal_strength);
781 }
782
783 static int s5h1411_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
784 {
785         struct s5h1411_state *state = fe->demodulator_priv;
786
787         *ucblocks = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xc9);
788
789         return 0;
790 }
791
792 static int s5h1411_read_ber(struct dvb_frontend *fe, u32 *ber)
793 {
794         return s5h1411_read_ucblocks(fe, ber);
795 }
796
797 static int s5h1411_get_frontend(struct dvb_frontend *fe,
798                                 struct dvb_frontend_parameters *p)
799 {
800         struct s5h1411_state *state = fe->demodulator_priv;
801
802         p->frequency = state->current_frequency;
803         p->u.vsb.modulation = state->current_modulation;
804
805         return 0;
806 }
807
808 static int s5h1411_get_tune_settings(struct dvb_frontend *fe,
809                                      struct dvb_frontend_tune_settings *tune)
810 {
811         tune->min_delay_ms = 1000;
812         return 0;
813 }
814
815 static void s5h1411_release(struct dvb_frontend *fe)
816 {
817         struct s5h1411_state *state = fe->demodulator_priv;
818         kfree(state);
819 }
820
821 static struct dvb_frontend_ops s5h1411_ops;
822
823 struct dvb_frontend *s5h1411_attach(const struct s5h1411_config *config,
824                                     struct i2c_adapter *i2c)
825 {
826         struct s5h1411_state *state = NULL;
827         u16 reg;
828
829         /* allocate memory for the internal state */
830         state = kmalloc(sizeof(struct s5h1411_state), GFP_KERNEL);
831         if (state == NULL)
832                 goto error;
833
834         /* setup the state */
835         state->config = config;
836         state->i2c = i2c;
837         state->current_modulation = VSB_8;
838         state->inversion = state->config->inversion;
839
840         /* check if the demod exists */
841         reg = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0x05);
842         if (reg != 0x0066)
843                 goto error;
844
845         /* create dvb_frontend */
846         memcpy(&state->frontend.ops, &s5h1411_ops,
847                sizeof(struct dvb_frontend_ops));
848
849         state->frontend.demodulator_priv = state;
850
851         if (s5h1411_init(&state->frontend) != 0) {
852                 printk(KERN_ERR "%s: Failed to initialize correctly\n",
853                         __func__);
854                 goto error;
855         }
856
857         /* Note: Leaving the I2C gate open here. */
858         s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf5, 1);
859
860         return &state->frontend;
861
862 error:
863         kfree(state);
864         return NULL;
865 }
866 EXPORT_SYMBOL(s5h1411_attach);
867
868 static struct dvb_frontend_ops s5h1411_ops = {
869
870         .info = {
871                 .name                   = "Samsung S5H1411 QAM/8VSB Frontend",
872                 .type                   = FE_ATSC,
873                 .frequency_min          = 54000000,
874                 .frequency_max          = 858000000,
875                 .frequency_stepsize     = 62500,
876                 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
877         },
878
879         .init                 = s5h1411_init,
880         .i2c_gate_ctrl        = s5h1411_i2c_gate_ctrl,
881         .set_frontend         = s5h1411_set_frontend,
882         .get_frontend         = s5h1411_get_frontend,
883         .get_tune_settings    = s5h1411_get_tune_settings,
884         .read_status          = s5h1411_read_status,
885         .read_ber             = s5h1411_read_ber,
886         .read_signal_strength = s5h1411_read_signal_strength,
887         .read_snr             = s5h1411_read_snr,
888         .read_ucblocks        = s5h1411_read_ucblocks,
889         .release              = s5h1411_release,
890 };
891
892 module_param(debug, int, 0644);
893 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
894
895 MODULE_DESCRIPTION("Samsung S5H1411 QAM-B/ATSC Demodulator driver");
896 MODULE_AUTHOR("Steven Toth");
897 MODULE_LICENSE("GPL");
898
899 /*
900  * Local variables:
901  * c-basic-offset: 8
902  */