Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/cpufreq
[linux-2.6] / drivers / media / video / saa7134 / saa7134-dvb.c
1 /*
2  *
3  * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
4  *
5  *  Extended 3 / 2005 by Hartmut Hackmann to support various
6  *  cards with the tda10046 DVB-T channel decoder
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/delay.h>
29 #include <linux/kthread.h>
30 #include <linux/suspend.h>
31
32 #include "saa7134-reg.h"
33 #include "saa7134.h"
34 #include <media/v4l2-common.h>
35
36 #ifdef HAVE_MT352
37 # include "mt352.h"
38 # include "mt352_priv.h" /* FIXME */
39 #endif
40 #ifdef HAVE_TDA1004X
41 # include "tda1004x.h"
42 #endif
43 #ifdef HAVE_NXT200X
44 # include "nxt200x.h"
45 # include "dvb-pll.h"
46 #endif
47
48 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
49 MODULE_LICENSE("GPL");
50
51 static unsigned int antenna_pwr = 0;
52
53 module_param(antenna_pwr, int, 0444);
54 MODULE_PARM_DESC(antenna_pwr,"enable antenna power (Pinnacle 300i)");
55
56 /* ------------------------------------------------------------------ */
57
58 #ifdef HAVE_MT352
59 static int pinnacle_antenna_pwr(struct saa7134_dev *dev, int on)
60 {
61         u32 ok;
62
63         if (!on) {
64                 saa_setl(SAA7134_GPIO_GPMODE0 >> 2,     (1 << 26));
65                 saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 26));
66                 return 0;
67         }
68
69         saa_setl(SAA7134_GPIO_GPMODE0 >> 2,     (1 << 26));
70         saa_setl(SAA7134_GPIO_GPSTATUS0 >> 2,   (1 << 26));
71         udelay(10);
72
73         saa_setl(SAA7134_GPIO_GPMODE0 >> 2,     (1 << 28));
74         saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 28));
75         udelay(10);
76         saa_setl(SAA7134_GPIO_GPSTATUS0 >> 2,   (1 << 28));
77         udelay(10);
78         ok = saa_readl(SAA7134_GPIO_GPSTATUS0) & (1 << 27);
79         printk("%s: %s %s\n", dev->name, __FUNCTION__,
80                ok ? "on" : "off");
81
82         if (!ok)
83                 saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2,   (1 << 26));
84         return ok;
85 }
86
87 static int mt352_pinnacle_init(struct dvb_frontend* fe)
88 {
89         static u8 clock_config []  = { CLOCK_CTL,  0x3d, 0x28 };
90         static u8 reset []         = { RESET,      0x80 };
91         static u8 adc_ctl_1_cfg [] = { ADC_CTL_1,  0x40 };
92         static u8 agc_cfg []       = { AGC_TARGET, 0x28, 0xa0 };
93         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x31 };
94         static u8 fsm_ctl_cfg[]    = { 0x7b,       0x04 };
95         static u8 gpp_ctl_cfg []   = { GPP_CTL,    0x0f };
96         static u8 scan_ctl_cfg []  = { SCAN_CTL,   0x0d };
97         static u8 irq_cfg []       = { INTERRUPT_EN_0, 0x00, 0x00, 0x00, 0x00 };
98         struct saa7134_dev *dev= fe->dvb->priv;
99
100         printk("%s: %s called\n",dev->name,__FUNCTION__);
101
102         mt352_write(fe, clock_config,   sizeof(clock_config));
103         udelay(200);
104         mt352_write(fe, reset,          sizeof(reset));
105         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
106         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
107         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
108         mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
109
110         mt352_write(fe, fsm_ctl_cfg,    sizeof(fsm_ctl_cfg));
111         mt352_write(fe, scan_ctl_cfg,   sizeof(scan_ctl_cfg));
112         mt352_write(fe, irq_cfg,        sizeof(irq_cfg));
113         return 0;
114 }
115
116 static int mt352_pinnacle_pll_set(struct dvb_frontend* fe,
117                                   struct dvb_frontend_parameters* params,
118                                   u8* pllbuf)
119 {
120         static int on  = TDA9887_PRESENT | TDA9887_PORT2_INACTIVE;
121         static int off = TDA9887_PRESENT | TDA9887_PORT2_ACTIVE;
122         struct saa7134_dev *dev = fe->dvb->priv;
123         struct v4l2_frequency f;
124
125         /* set frequency (mt2050) */
126         f.tuner     = 0;
127         f.type      = V4L2_TUNER_DIGITAL_TV;
128         f.frequency = params->frequency / 1000 * 16 / 1000;
129         saa7134_i2c_call_clients(dev,TDA9887_SET_CONFIG,&on);
130         saa7134_i2c_call_clients(dev,VIDIOC_S_FREQUENCY,&f);
131         saa7134_i2c_call_clients(dev,TDA9887_SET_CONFIG,&off);
132
133         pinnacle_antenna_pwr(dev, antenna_pwr);
134
135         /* mt352 setup */
136         mt352_pinnacle_init(fe);
137         pllbuf[0] = 0xc2;
138         pllbuf[1] = 0x00;
139         pllbuf[2] = 0x00;
140         pllbuf[3] = 0x80;
141         pllbuf[4] = 0x00;
142         return 0;
143 }
144
145 static struct mt352_config pinnacle_300i = {
146         .demod_address = 0x3c >> 1,
147         .adc_clock     = 20333,
148         .if2           = 36150,
149         .no_tuner      = 1,
150         .demod_init    = mt352_pinnacle_init,
151         .pll_set       = mt352_pinnacle_pll_set,
152 };
153 #endif
154
155 /* ------------------------------------------------------------------ */
156
157 #ifdef HAVE_TDA1004X
158
159 static int philips_tda6651_pll_set(u8 addr, struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
160 {
161         struct saa7134_dev *dev = fe->dvb->priv;
162         u8 tuner_buf[4];
163         struct i2c_msg tuner_msg = {.addr = addr,.flags = 0,.buf = tuner_buf,.len =
164                         sizeof(tuner_buf) };
165         int tuner_frequency = 0;
166         u8 band, cp, filter;
167
168         /* determine charge pump */
169         tuner_frequency = params->frequency + 36166000;
170         if (tuner_frequency < 87000000)
171                 return -EINVAL;
172         else if (tuner_frequency < 130000000)
173                 cp = 3;
174         else if (tuner_frequency < 160000000)
175                 cp = 5;
176         else if (tuner_frequency < 200000000)
177                 cp = 6;
178         else if (tuner_frequency < 290000000)
179                 cp = 3;
180         else if (tuner_frequency < 420000000)
181                 cp = 5;
182         else if (tuner_frequency < 480000000)
183                 cp = 6;
184         else if (tuner_frequency < 620000000)
185                 cp = 3;
186         else if (tuner_frequency < 830000000)
187                 cp = 5;
188         else if (tuner_frequency < 895000000)
189                 cp = 7;
190         else
191                 return -EINVAL;
192
193         /* determine band */
194         if (params->frequency < 49000000)
195                 return -EINVAL;
196         else if (params->frequency < 161000000)
197                 band = 1;
198         else if (params->frequency < 444000000)
199                 band = 2;
200         else if (params->frequency < 861000000)
201                 band = 4;
202         else
203                 return -EINVAL;
204
205         /* setup PLL filter */
206         switch (params->u.ofdm.bandwidth) {
207         case BANDWIDTH_6_MHZ:
208                 filter = 0;
209                 break;
210
211         case BANDWIDTH_7_MHZ:
212                 filter = 0;
213                 break;
214
215         case BANDWIDTH_8_MHZ:
216                 filter = 1;
217                 break;
218
219         default:
220                 return -EINVAL;
221         }
222
223         /* calculate divisor
224          * ((36166000+((1000000/6)/2)) + Finput)/(1000000/6)
225          */
226         tuner_frequency = (((params->frequency / 1000) * 6) + 217496) / 1000;
227
228         /* setup tuner buffer */
229         tuner_buf[0] = (tuner_frequency >> 8) & 0x7f;
230         tuner_buf[1] = tuner_frequency & 0xff;
231         tuner_buf[2] = 0xca;
232         tuner_buf[3] = (cp << 5) | (filter << 3) | band;
233
234         if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
235                 return -EIO;
236         msleep(1);
237         return 0;
238 }
239
240 static int philips_tda6651_pll_init(u8 addr, struct dvb_frontend *fe)
241 {
242         struct saa7134_dev *dev = fe->dvb->priv;
243         static u8 tu1216_init[] = { 0x0b, 0xf5, 0x85, 0xab };
244         struct i2c_msg tuner_msg = {.addr = addr,.flags = 0,.buf = tu1216_init,.len = sizeof(tu1216_init) };
245
246         /* setup PLL configuration */
247         if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
248                 return -EIO;
249         msleep(1);
250
251         return 0;
252 }
253
254 /* ------------------------------------------------------------------ */
255
256 static int philips_tu1216_pll_60_init(struct dvb_frontend *fe)
257 {
258         return philips_tda6651_pll_init(0x60, fe);
259 }
260
261 static int philips_tu1216_pll_60_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
262 {
263         return philips_tda6651_pll_set(0x60, fe, params);
264 }
265
266 static int philips_tu1216_request_firmware(struct dvb_frontend *fe,
267                                            const struct firmware **fw, char *name)
268 {
269         struct saa7134_dev *dev = fe->dvb->priv;
270         return request_firmware(fw, name, &dev->pci->dev);
271 }
272
273 static struct tda1004x_config philips_tu1216_60_config = {
274
275         .demod_address = 0x8,
276         .invert        = 1,
277         .invert_oclk   = 0,
278         .xtal_freq     = TDA10046_XTAL_4M,
279         .agc_config    = TDA10046_AGC_DEFAULT,
280         .if_freq       = TDA10046_FREQ_3617,
281         .pll_init      = philips_tu1216_pll_60_init,
282         .pll_set       = philips_tu1216_pll_60_set,
283         .pll_sleep     = NULL,
284         .request_firmware = philips_tu1216_request_firmware,
285 };
286
287 /* ------------------------------------------------------------------ */
288
289 static int philips_tu1216_pll_61_init(struct dvb_frontend *fe)
290 {
291         return philips_tda6651_pll_init(0x61, fe);
292 }
293
294 static int philips_tu1216_pll_61_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
295 {
296         return philips_tda6651_pll_set(0x61, fe, params);
297 }
298
299 static struct tda1004x_config philips_tu1216_61_config = {
300
301         .demod_address = 0x8,
302         .invert        = 1,
303         .invert_oclk   = 0,
304         .xtal_freq     = TDA10046_XTAL_4M,
305         .agc_config    = TDA10046_AGC_DEFAULT,
306         .if_freq       = TDA10046_FREQ_3617,
307         .pll_init      = philips_tu1216_pll_61_init,
308         .pll_set       = philips_tu1216_pll_61_set,
309         .pll_sleep     = NULL,
310         .request_firmware = philips_tu1216_request_firmware,
311 };
312
313 /* ------------------------------------------------------------------ */
314
315 static int philips_europa_pll_init(struct dvb_frontend *fe)
316 {
317         struct saa7134_dev *dev = fe->dvb->priv;
318         static u8 msg[] = { 0x0b, 0xf5, 0x86, 0xab };
319         struct i2c_msg init_msg = {.addr = 0x61,.flags = 0,.buf = msg,.len = sizeof(msg) };
320
321         /* setup PLL configuration */
322         if (i2c_transfer(&dev->i2c_adap, &init_msg, 1) != 1)
323                 return -EIO;
324         msleep(1);
325
326         /* switch the board to dvb mode */
327         init_msg.addr = 0x43;
328         init_msg.len  = 0x02;
329         msg[0] = 0x00;
330         msg[1] = 0x40;
331         if (i2c_transfer(&dev->i2c_adap, &init_msg, 1) != 1)
332                 return -EIO;
333
334         return 0;
335 }
336
337 static int philips_td1316_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
338 {
339         return philips_tda6651_pll_set(0x61, fe, params);
340 }
341
342 static void philips_europa_analog(struct dvb_frontend *fe)
343 {
344         struct saa7134_dev *dev = fe->dvb->priv;
345         /* this message actually turns the tuner back to analog mode */
346         static u8 msg[] = { 0x0b, 0xdc, 0x86, 0xa4 };
347         struct i2c_msg analog_msg = {.addr = 0x61,.flags = 0,.buf = msg,.len = sizeof(msg) };
348
349         i2c_transfer(&dev->i2c_adap, &analog_msg, 1);
350         msleep(1);
351
352         /* switch the board to analog mode */
353         analog_msg.addr = 0x43;
354         analog_msg.len  = 0x02;
355         msg[0] = 0x00;
356         msg[1] = 0x14;
357         i2c_transfer(&dev->i2c_adap, &analog_msg, 1);
358 }
359
360 static struct tda1004x_config philips_europa_config = {
361
362         .demod_address = 0x8,
363         .invert        = 0,
364         .invert_oclk   = 0,
365         .xtal_freq     = TDA10046_XTAL_4M,
366         .agc_config    = TDA10046_AGC_IFO_AUTO_POS,
367         .if_freq       = TDA10046_FREQ_052,
368         .pll_init      = philips_europa_pll_init,
369         .pll_set       = philips_td1316_pll_set,
370         .pll_sleep     = philips_europa_analog,
371         .request_firmware = NULL,
372 };
373
374 /* ------------------------------------------------------------------ */
375
376 static int philips_fmd1216_pll_init(struct dvb_frontend *fe)
377 {
378         struct saa7134_dev *dev = fe->dvb->priv;
379         /* this message is to set up ATC and ALC */
380         static u8 fmd1216_init[] = { 0x0b, 0xdc, 0x9c, 0xa0 };
381         struct i2c_msg tuner_msg = {.addr = 0x61,.flags = 0,.buf = fmd1216_init,.len = sizeof(fmd1216_init) };
382
383         if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
384                 return -EIO;
385         msleep(1);
386
387         return 0;
388 }
389
390 static void philips_fmd1216_analog(struct dvb_frontend *fe)
391 {
392         struct saa7134_dev *dev = fe->dvb->priv;
393         /* this message actually turns the tuner back to analog mode */
394         static u8 fmd1216_init[] = { 0x0b, 0xdc, 0x9c, 0x60 };
395         struct i2c_msg tuner_msg = {.addr = 0x61,.flags = 0,.buf = fmd1216_init,.len = sizeof(fmd1216_init) };
396
397         i2c_transfer(&dev->i2c_adap, &tuner_msg, 1);
398         msleep(1);
399         fmd1216_init[2] = 0x86;
400         fmd1216_init[3] = 0x54;
401         i2c_transfer(&dev->i2c_adap, &tuner_msg, 1);
402         msleep(1);
403 }
404
405 static int philips_fmd1216_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
406 {
407         struct saa7134_dev *dev = fe->dvb->priv;
408         u8 tuner_buf[4];
409         struct i2c_msg tuner_msg = {.addr = 0x61,.flags = 0,.buf = tuner_buf,.len =
410                         sizeof(tuner_buf) };
411         int tuner_frequency = 0;
412         int divider = 0;
413         u8 band, mode, cp;
414
415         /* determine charge pump */
416         tuner_frequency = params->frequency + 36130000;
417         if (tuner_frequency < 87000000)
418                 return -EINVAL;
419         /* low band */
420         else if (tuner_frequency < 180000000) {
421                 band = 1;
422                 mode = 7;
423                 cp   = 0;
424         } else if (tuner_frequency < 195000000) {
425                 band = 1;
426                 mode = 6;
427                 cp   = 1;
428         /* mid band     */
429         } else if (tuner_frequency < 366000000) {
430                 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
431                         band = 10;
432                 } else {
433                         band = 2;
434                 }
435                 mode = 7;
436                 cp   = 0;
437         } else if (tuner_frequency < 478000000) {
438                 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
439                         band = 10;
440                 } else {
441                         band = 2;
442                 }
443                 mode = 6;
444                 cp   = 1;
445         /* high band */
446         } else if (tuner_frequency < 662000000) {
447                 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
448                         band = 12;
449                 } else {
450                         band = 4;
451                 }
452                 mode = 7;
453                 cp   = 0;
454         } else if (tuner_frequency < 840000000) {
455                 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
456                         band = 12;
457                 } else {
458                         band = 4;
459                 }
460                 mode = 6;
461                 cp   = 1;
462         } else {
463                 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
464                         band = 12;
465                 } else {
466                         band = 4;
467                 }
468                 mode = 7;
469                 cp   = 1;
470
471         }
472         /* calculate divisor */
473         /* ((36166000 + Finput) / 166666) rounded! */
474         divider = (tuner_frequency + 83333) / 166667;
475
476         /* setup tuner buffer */
477         tuner_buf[0] = (divider >> 8) & 0x7f;
478         tuner_buf[1] = divider & 0xff;
479         tuner_buf[2] = 0x80 | (cp << 6) | (mode  << 3) | 4;
480         tuner_buf[3] = 0x40 | band;
481
482         if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
483                 return -EIO;
484         return 0;
485 }
486
487 static struct tda1004x_config medion_cardbus = {
488         .demod_address = 0x08,
489         .invert        = 1,
490         .invert_oclk   = 0,
491         .xtal_freq     = TDA10046_XTAL_16M,
492         .agc_config    = TDA10046_AGC_IFO_AUTO_NEG,
493         .if_freq       = TDA10046_FREQ_3613,
494         .pll_init      = philips_fmd1216_pll_init,
495         .pll_set       = philips_fmd1216_pll_set,
496         .pll_sleep         = philips_fmd1216_analog,
497         .request_firmware = NULL,
498 };
499
500 /* ------------------------------------------------------------------ */
501
502 struct tda827x_data {
503         u32 lomax;
504         u8  spd;
505         u8  bs;
506         u8  bp;
507         u8  cp;
508         u8  gc3;
509         u8 div1p5;
510 };
511
512 static struct tda827x_data tda827x_dvbt[] = {
513         { .lomax =  62000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1},
514         { .lomax =  66000000, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1},
515         { .lomax =  76000000, .spd = 3, .bs = 1, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0},
516         { .lomax =  84000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0},
517         { .lomax =  93000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0},
518         { .lomax =  98000000, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0},
519         { .lomax = 109000000, .spd = 3, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
520         { .lomax = 123000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1},
521         { .lomax = 133000000, .spd = 2, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1},
522         { .lomax = 151000000, .spd = 2, .bs = 1, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
523         { .lomax = 154000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
524         { .lomax = 181000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 0, .div1p5 = 0},
525         { .lomax = 185000000, .spd = 2, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
526         { .lomax = 217000000, .spd = 2, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
527         { .lomax = 244000000, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1},
528         { .lomax = 265000000, .spd = 1, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1},
529         { .lomax = 302000000, .spd = 1, .bs = 1, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
530         { .lomax = 324000000, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
531         { .lomax = 370000000, .spd = 1, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
532         { .lomax = 454000000, .spd = 1, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
533         { .lomax = 493000000, .spd = 0, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1},
534         { .lomax = 530000000, .spd = 0, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1},
535         { .lomax = 554000000, .spd = 0, .bs = 1, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
536         { .lomax = 604000000, .spd = 0, .bs = 1, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
537         { .lomax = 696000000, .spd = 0, .bs = 2, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
538         { .lomax = 740000000, .spd = 0, .bs = 2, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0},
539         { .lomax = 820000000, .spd = 0, .bs = 3, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
540         { .lomax = 865000000, .spd = 0, .bs = 3, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0},
541         { .lomax =         0, .spd = 0, .bs = 0, .bp = 0, .cp = 0, .gc3 = 0, .div1p5 = 0}
542 };
543
544 static int philips_tda827x_pll_init(struct dvb_frontend *fe)
545 {
546         return 0;
547 }
548
549 static int philips_tda827x_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
550 {
551         struct saa7134_dev *dev = fe->dvb->priv;
552         u8 tuner_buf[14];
553
554         struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tuner_buf,
555                                         .len = sizeof(tuner_buf) };
556         int i, tuner_freq, if_freq;
557         u32 N;
558         switch (params->u.ofdm.bandwidth) {
559         case BANDWIDTH_6_MHZ:
560                 if_freq = 4000000;
561                 break;
562         case BANDWIDTH_7_MHZ:
563                 if_freq = 4500000;
564                 break;
565         default:                   /* 8 MHz or Auto */
566                 if_freq = 5000000;
567                 break;
568         }
569         tuner_freq = params->frequency + if_freq;
570
571         i = 0;
572         while (tda827x_dvbt[i].lomax < tuner_freq) {
573                 if(tda827x_dvbt[i + 1].lomax == 0)
574                         break;
575                 i++;
576         }
577
578         N = ((tuner_freq + 125000) / 250000) << (tda827x_dvbt[i].spd + 2);
579         tuner_buf[0] = 0;
580         tuner_buf[1] = (N>>8) | 0x40;
581         tuner_buf[2] = N & 0xff;
582         tuner_buf[3] = 0;
583         tuner_buf[4] = 0x52;
584         tuner_buf[5] = (tda827x_dvbt[i].spd << 6) + (tda827x_dvbt[i].div1p5 << 5) +
585                                    (tda827x_dvbt[i].bs << 3) + tda827x_dvbt[i].bp;
586         tuner_buf[6] = (tda827x_dvbt[i].gc3 << 4) + 0x8f;
587         tuner_buf[7] = 0xbf;
588         tuner_buf[8] = 0x2a;
589         tuner_buf[9] = 0x05;
590         tuner_buf[10] = 0xff;
591         tuner_buf[11] = 0x00;
592         tuner_buf[12] = 0x00;
593         tuner_buf[13] = 0x40;
594
595         tuner_msg.len = 14;
596         if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
597                 return -EIO;
598
599         msleep(500);
600         /* correct CP value */
601         tuner_buf[0] = 0x30;
602         tuner_buf[1] = 0x50 + tda827x_dvbt[i].cp;
603         tuner_msg.len = 2;
604         i2c_transfer(&dev->i2c_adap, &tuner_msg, 1);
605
606         return 0;
607 }
608
609 static void philips_tda827x_pll_sleep(struct dvb_frontend *fe)
610 {
611         struct saa7134_dev *dev = fe->dvb->priv;
612         static u8 tda827x_sleep[] = { 0x30, 0xd0};
613         struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tda827x_sleep,
614                                     .len = sizeof(tda827x_sleep) };
615         i2c_transfer(&dev->i2c_adap, &tuner_msg, 1);
616 }
617
618 static struct tda1004x_config tda827x_lifeview_config = {
619         .demod_address = 0x08,
620         .invert        = 1,
621         .invert_oclk   = 0,
622         .xtal_freq     = TDA10046_XTAL_16M,
623         .agc_config    = TDA10046_AGC_TDA827X,
624         .if_freq       = TDA10046_FREQ_045,
625         .pll_init      = philips_tda827x_pll_init,
626         .pll_set       = philips_tda827x_pll_set,
627         .pll_sleep         = philips_tda827x_pll_sleep,
628         .request_firmware = NULL,
629 };
630
631 /* ------------------------------------------------------------------ */
632
633 struct tda827xa_data {
634         u32 lomax;
635         u8  svco;
636         u8  spd;
637         u8  scr;
638         u8  sbs;
639         u8  gc3;
640 };
641
642 static struct tda827xa_data tda827xa_dvbt[] = {
643         { .lomax =  56875000, .svco = 3, .spd = 4, .scr = 0, .sbs = 0, .gc3 = 1},
644         { .lomax =  67250000, .svco = 0, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
645         { .lomax =  81250000, .svco = 1, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
646         { .lomax =  97500000, .svco = 2, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
647         { .lomax = 113750000, .svco = 3, .spd = 3, .scr = 0, .sbs = 1, .gc3 = 1},
648         { .lomax = 134500000, .svco = 0, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
649         { .lomax = 154000000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
650         { .lomax = 162500000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
651         { .lomax = 183000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
652         { .lomax = 195000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1},
653         { .lomax = 227500000, .svco = 3, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1},
654         { .lomax = 269000000, .svco = 0, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1},
655         { .lomax = 290000000, .svco = 1, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1},
656         { .lomax = 325000000, .svco = 1, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
657         { .lomax = 390000000, .svco = 2, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
658         { .lomax = 455000000, .svco = 3, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
659         { .lomax = 520000000, .svco = 0, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
660         { .lomax = 538000000, .svco = 0, .spd = 0, .scr = 1, .sbs = 3, .gc3 = 1},
661         { .lomax = 550000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
662         { .lomax = 620000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
663         { .lomax = 650000000, .svco = 1, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
664         { .lomax = 700000000, .svco = 2, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
665         { .lomax = 780000000, .svco = 2, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
666         { .lomax = 820000000, .svco = 3, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
667         { .lomax = 870000000, .svco = 3, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
668         { .lomax = 911000000, .svco = 3, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 0},
669         { .lomax =         0, .svco = 0, .spd = 0, .scr = 0, .sbs = 0, .gc3 = 0}};
670
671
672 static int philips_tda827xa_pll_set(u8 addr, struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
673 {
674         struct saa7134_dev *dev = fe->dvb->priv;
675         u8 tuner_buf[14];
676         unsigned char reg2[2];
677
678         struct i2c_msg msg = {.addr = addr,.flags = 0,.buf = tuner_buf};
679         int i, tuner_freq, if_freq;
680         u32 N;
681
682         switch (params->u.ofdm.bandwidth) {
683         case BANDWIDTH_6_MHZ:
684                 if_freq = 4000000;
685                 break;
686         case BANDWIDTH_7_MHZ:
687                 if_freq = 4500000;
688                 break;
689         default:                   /* 8 MHz or Auto */
690                 if_freq = 5000000;
691                 break;
692         }
693         tuner_freq = params->frequency + if_freq;
694
695         i = 0;
696         while (tda827xa_dvbt[i].lomax < tuner_freq) {
697                 if(tda827xa_dvbt[i + 1].lomax == 0)
698                         break;
699                 i++;
700         }
701
702         N = ((tuner_freq + 31250) / 62500) << tda827xa_dvbt[i].spd;
703         tuner_buf[0] = 0;            // subaddress
704         tuner_buf[1] = N >> 8;
705         tuner_buf[2] = N & 0xff;
706         tuner_buf[3] = 0;
707         tuner_buf[4] = 0x16;
708         tuner_buf[5] = (tda827xa_dvbt[i].spd << 5) + (tda827xa_dvbt[i].svco << 3) +
709                         tda827xa_dvbt[i].sbs;
710         tuner_buf[6] = 0x4b + (tda827xa_dvbt[i].gc3 << 4);
711         tuner_buf[7] = 0x0c;
712         tuner_buf[8] = 0x06;
713         tuner_buf[9] = 0x24;
714         tuner_buf[10] = 0xff;
715         tuner_buf[11] = 0x60;
716         tuner_buf[12] = 0x00;
717         tuner_buf[13] = 0x39;  // lpsel
718         msg.len = 14;
719         if (i2c_transfer(&dev->i2c_adap, &msg, 1) != 1)
720                 return -EIO;
721
722         msg.buf= reg2;
723         msg.len = 2;
724         reg2[0] = 0x60;
725         reg2[1] = 0x3c;
726         i2c_transfer(&dev->i2c_adap, &msg, 1);
727
728         reg2[0] = 0xa0;
729         reg2[1] = 0x40;
730         i2c_transfer(&dev->i2c_adap, &msg, 1);
731
732         msleep(2);
733         /* correct CP value */
734         reg2[0] = 0x30;
735         reg2[1] = 0x10 + tda827xa_dvbt[i].scr;
736         msg.len = 2;
737         i2c_transfer(&dev->i2c_adap, &msg, 1);
738
739         msleep(550);
740         reg2[0] = 0x50;
741         reg2[1] = 0x4f + (tda827xa_dvbt[i].gc3 << 4);
742         i2c_transfer(&dev->i2c_adap, &msg, 1);
743
744         return 0;
745
746 }
747
748 static void philips_tda827xa_pll_sleep(u8 addr, struct dvb_frontend *fe)
749 {
750         struct saa7134_dev *dev = fe->dvb->priv;
751         static u8 tda827xa_sleep[] = { 0x30, 0x90};
752         struct i2c_msg tuner_msg = {.addr = addr,.flags = 0,.buf = tda827xa_sleep,
753                                     .len = sizeof(tda827xa_sleep) };
754         i2c_transfer(&dev->i2c_adap, &tuner_msg, 1);
755
756 }
757
758 /* ------------------------------------------------------------------ */
759
760 static int philips_tiger_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
761 {
762         int ret;
763         struct saa7134_dev *dev = fe->dvb->priv;
764         static u8 tda8290_close[] = { 0x21, 0xc0};
765         static u8 tda8290_open[]  = { 0x21, 0x80};
766         struct i2c_msg tda8290_msg = {.addr = 0x4b,.flags = 0, .len = 2};
767         /* close tda8290 i2c bridge */
768         tda8290_msg.buf = tda8290_close;
769         ret = i2c_transfer(&dev->i2c_adap, &tda8290_msg, 1);
770         if (ret != 1)
771                 return -EIO;
772         msleep(20);
773         ret = philips_tda827xa_pll_set(0x61, fe, params);
774         if (ret != 0)
775                 return ret;
776         /* open tda8290 i2c bridge */
777         tda8290_msg.buf = tda8290_open;
778         i2c_transfer(&dev->i2c_adap, &tda8290_msg, 1);
779         return ret;
780 };
781
782 static int philips_tiger_dvb_mode(struct dvb_frontend *fe)
783 {
784         struct saa7134_dev *dev = fe->dvb->priv;
785         static u8 data[] = { 0x3c, 0x33, 0x6a};
786         struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
787
788         if (i2c_transfer(&dev->i2c_adap, &msg, 1) != 1)
789                 return -EIO;
790         return 0;
791 }
792
793 static void philips_tiger_analog_mode(struct dvb_frontend *fe)
794 {
795         struct saa7134_dev *dev = fe->dvb->priv;
796         static u8 data[] = { 0x3c, 0x33, 0x68};
797         struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
798
799         i2c_transfer(&dev->i2c_adap, &msg, 1);
800         philips_tda827xa_pll_sleep( 0x61, fe);
801 }
802
803 static struct tda1004x_config philips_tiger_config = {
804         .demod_address = 0x08,
805         .invert        = 1,
806         .invert_oclk   = 0,
807         .xtal_freq     = TDA10046_XTAL_16M,
808         .agc_config    = TDA10046_AGC_TDA827X,
809         .if_freq       = TDA10046_FREQ_045,
810         .pll_init      = philips_tiger_dvb_mode,
811         .pll_set       = philips_tiger_pll_set,
812         .pll_sleep     = philips_tiger_analog_mode,
813         .request_firmware = NULL,
814 };
815
816 #endif
817
818 /* ------------------------------------------------------------------ */
819
820 #ifdef HAVE_NXT200X
821 static struct nxt200x_config avertvhda180 = {
822         .demod_address    = 0x0a,
823         .pll_address      = 0x61,
824         .pll_desc         = &dvb_pll_tdhu2,
825 };
826 #endif
827
828 /* ------------------------------------------------------------------ */
829
830 static int dvb_init(struct saa7134_dev *dev)
831 {
832         /* init struct videobuf_dvb */
833         dev->ts.nr_bufs    = 32;
834         dev->ts.nr_packets = 32*4;
835         dev->dvb.name = dev->name;
836         videobuf_queue_init(&dev->dvb.dvbq, &saa7134_ts_qops,
837                             dev->pci, &dev->slock,
838                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
839                             V4L2_FIELD_ALTERNATE,
840                             sizeof(struct saa7134_buf),
841                             dev);
842
843         switch (dev->board) {
844 #ifdef HAVE_MT352
845         case SAA7134_BOARD_PINNACLE_300I_DVBT_PAL:
846                 printk("%s: pinnacle 300i dvb setup\n",dev->name);
847                 dev->dvb.frontend = mt352_attach(&pinnacle_300i,
848                                                  &dev->i2c_adap);
849                 break;
850 #endif
851 #ifdef HAVE_TDA1004X
852         case SAA7134_BOARD_MD7134:
853                 dev->dvb.frontend = tda10046_attach(&medion_cardbus,
854                                                     &dev->i2c_adap);
855                 break;
856         case SAA7134_BOARD_PHILIPS_TOUGH:
857                 dev->dvb.frontend = tda10046_attach(&philips_tu1216_60_config,
858                                                     &dev->i2c_adap);
859                 break;
860         case SAA7134_BOARD_FLYDVBTDUO:
861                 dev->dvb.frontend = tda10046_attach(&tda827x_lifeview_config,
862                                                     &dev->i2c_adap);
863                 break;
864         case SAA7134_BOARD_FLYDVBT_DUO_CARDBUS:
865                 dev->dvb.frontend = tda10046_attach(&tda827x_lifeview_config,
866                                                     &dev->i2c_adap);
867                 break;
868         case SAA7134_BOARD_PHILIPS_EUROPA:
869                 dev->dvb.frontend = tda10046_attach(&philips_europa_config,
870                                                     &dev->i2c_adap);
871                 break;
872         case SAA7134_BOARD_VIDEOMATE_DVBT_300:
873                 dev->dvb.frontend = tda10046_attach(&philips_europa_config,
874                                                     &dev->i2c_adap);
875                 break;
876         case SAA7134_BOARD_VIDEOMATE_DVBT_200:
877                 dev->dvb.frontend = tda10046_attach(&philips_tu1216_61_config,
878                                                     &dev->i2c_adap);
879                 break;
880         case SAA7134_BOARD_PHILIPS_TIGER:
881                 dev->dvb.frontend = tda10046_attach(&philips_tiger_config,
882                                                     &dev->i2c_adap);
883                 break;
884         case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
885                 dev->dvb.frontend = tda10046_attach(&philips_tiger_config,
886                                                     &dev->i2c_adap);
887                 break;
888 #endif
889 #ifdef HAVE_NXT200X
890         case SAA7134_BOARD_AVERMEDIA_AVERTVHD_A180:
891                 dev->dvb.frontend = nxt200x_attach(&avertvhda180, &dev->i2c_adap);
892                 break;
893 #endif
894         default:
895                 printk("%s: Huh? unknown DVB card?\n",dev->name);
896                 break;
897         }
898
899         if (NULL == dev->dvb.frontend) {
900                 printk("%s: frontend initialization failed\n",dev->name);
901                 return -1;
902         }
903
904         /* register everything else */
905         return videobuf_dvb_register(&dev->dvb, THIS_MODULE, dev);
906 }
907
908 static int dvb_fini(struct saa7134_dev *dev)
909 {
910         static int on  = TDA9887_PRESENT | TDA9887_PORT2_INACTIVE;
911
912         switch (dev->board) {
913         case SAA7134_BOARD_PINNACLE_300I_DVBT_PAL:
914                 /* otherwise we don't detect the tuner on next insmod */
915                 saa7134_i2c_call_clients(dev,TDA9887_SET_CONFIG,&on);
916                 break;
917         };
918         videobuf_dvb_unregister(&dev->dvb);
919         return 0;
920 }
921
922 static struct saa7134_mpeg_ops dvb_ops = {
923         .type          = SAA7134_MPEG_DVB,
924         .init          = dvb_init,
925         .fini          = dvb_fini,
926 };
927
928 static int __init dvb_register(void)
929 {
930         return saa7134_ts_register(&dvb_ops);
931 }
932
933 static void __exit dvb_unregister(void)
934 {
935         saa7134_ts_unregister(&dvb_ops);
936 }
937
938 module_init(dvb_register);
939 module_exit(dvb_unregister);
940
941 /* ------------------------------------------------------------------ */
942 /*
943  * Local variables:
944  * c-basic-offset: 8
945  * End:
946  */