Merge master.kernel.org:/home/rmk/linux-2.6-serial
[linux-2.6] / drivers / media / dvb / ttpci / budget-av.c
1 /*
2  * budget-av.c: driver for the SAA7146 based Budget DVB cards
3  *              with analog video in
4  *
5  * Compiled from various sources by Michael Hunold <michael@mihu.de>
6  *
7  * CI interface support (c) 2004 Olivier Gournet <ogournet@anevia.com> &
8  *                               Andrew de Quincey <adq_dvb@lidskialf.net>
9  *
10  * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
11  *
12  * Copyright (C) 1999-2002 Ralph  Metzler
13  *                       & Marcus Metzler for convergence integrated media GmbH
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
31  *
32  *
33  * the project's page is at http://www.linuxtv.org/dvb/
34  */
35
36 #include "budget.h"
37 #include "stv0299.h"
38 #include "tda10021.h"
39 #include "tda1004x.h"
40 #include <media/saa7146_vv.h>
41 #include <linux/module.h>
42 #include <linux/errno.h>
43 #include <linux/slab.h>
44 #include <linux/interrupt.h>
45 #include <linux/input.h>
46 #include <linux/spinlock.h>
47
48 #include "dvb_ca_en50221.h"
49
50 #define DEBICICAM               0x02420000
51
52 struct budget_av {
53         struct budget budget;
54         struct video_device *vd;
55         int cur_input;
56         int has_saa7113;
57         struct tasklet_struct ciintf_irq_tasklet;
58         int slot_status;
59         struct dvb_ca_en50221 ca;
60 };
61
62 /* GPIO CI Connections:
63  * 0 - Vcc/Reset (Reset is controlled by capacitor)
64  * 1 - Attribute Memory
65  * 2 - Card Enable (Active Low)
66  * 3 - Card Detect
67  */
68
69 /****************************************************************************
70  * INITIALIZATION
71  ****************************************************************************/
72
73 static u8 i2c_readreg(struct i2c_adapter *i2c, u8 id, u8 reg)
74 {
75         u8 mm1[] = { 0x00 };
76         u8 mm2[] = { 0x00 };
77         struct i2c_msg msgs[2];
78
79         msgs[0].flags = 0;
80         msgs[1].flags = I2C_M_RD;
81         msgs[0].addr = msgs[1].addr = id / 2;
82         mm1[0] = reg;
83         msgs[0].len = 1;
84         msgs[1].len = 1;
85         msgs[0].buf = mm1;
86         msgs[1].buf = mm2;
87
88         i2c_transfer(i2c, msgs, 2);
89
90         return mm2[0];
91 }
92
93 static int i2c_readregs(struct i2c_adapter *i2c, u8 id, u8 reg, u8 * buf, u8 len)
94 {
95         u8 mm1[] = { reg };
96         struct i2c_msg msgs[2] = {
97                 {.addr = id / 2,.flags = 0,.buf = mm1,.len = 1},
98                 {.addr = id / 2,.flags = I2C_M_RD,.buf = buf,.len = len}
99         };
100
101         if (i2c_transfer(i2c, msgs, 2) != 2)
102                 return -EIO;
103
104         return 0;
105 }
106
107 static int i2c_writereg(struct i2c_adapter *i2c, u8 id, u8 reg, u8 val)
108 {
109         u8 msg[2] = { reg, val };
110         struct i2c_msg msgs;
111
112         msgs.flags = 0;
113         msgs.addr = id / 2;
114         msgs.len = 2;
115         msgs.buf = msg;
116         return i2c_transfer(i2c, &msgs, 1);
117 }
118
119 static int ciintf_read_attribute_mem(struct dvb_ca_en50221 *ca, int slot, int address)
120 {
121         struct budget_av *budget_av = (struct budget_av *) ca->data;
122         int result;
123
124         if (slot != 0)
125                 return -EINVAL;
126
127         saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTHI);
128         udelay(1);
129
130         result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, address & 0xfff, 1, 0, 1);
131
132         if (result == -ETIMEDOUT)
133                 budget_av->slot_status = 0;
134         return result;
135 }
136
137 static int ciintf_write_attribute_mem(struct dvb_ca_en50221 *ca, int slot, int address, u8 value)
138 {
139         struct budget_av *budget_av = (struct budget_av *) ca->data;
140         int result;
141
142         if (slot != 0)
143                 return -EINVAL;
144
145         saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTHI);
146         udelay(1);
147
148         result = ttpci_budget_debiwrite(&budget_av->budget, DEBICICAM, address & 0xfff, 1, value, 0, 1);
149
150         if (result == -ETIMEDOUT)
151                 budget_av->slot_status = 0;
152         return result;
153 }
154
155 static int ciintf_read_cam_control(struct dvb_ca_en50221 *ca, int slot, u8 address)
156 {
157         struct budget_av *budget_av = (struct budget_av *) ca->data;
158         int result;
159
160         if (slot != 0)
161                 return -EINVAL;
162
163         saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
164         udelay(1);
165
166         result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, address & 3, 1, 0, 0);
167
168         if (result == -ETIMEDOUT)
169                 budget_av->slot_status = 0;
170         return result;
171 }
172
173 static int ciintf_write_cam_control(struct dvb_ca_en50221 *ca, int slot, u8 address, u8 value)
174 {
175         struct budget_av *budget_av = (struct budget_av *) ca->data;
176         int result;
177
178         if (slot != 0)
179                 return -EINVAL;
180
181         saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
182         udelay(1);
183
184         result = ttpci_budget_debiwrite(&budget_av->budget, DEBICICAM, address & 3, 1, value, 0, 0);
185
186         if (result == -ETIMEDOUT)
187                 budget_av->slot_status = 0;
188         return result;
189 }
190
191 static int ciintf_slot_reset(struct dvb_ca_en50221 *ca, int slot)
192 {
193         struct budget_av *budget_av = (struct budget_av *) ca->data;
194         struct saa7146_dev *saa = budget_av->budget.dev;
195         int timeout = 50; // 5 seconds (4.4.6 Ready)
196
197         if (slot != 0)
198                 return -EINVAL;
199
200         dprintk(1, "ciintf_slot_reset\n");
201
202         saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
203
204         saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI); /* Vcc off */
205         msleep(2);
206         saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO); /* Vcc on */
207         msleep(20); /* 20 ms Vcc settling time */
208
209         saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTLO); /* enable card */
210
211         /* This should have been based on pin 16 READY of the pcmcia port,
212          * but AFAICS it is not routed to the saa7146 */
213         while (--timeout > 0 && ciintf_read_attribute_mem(ca, slot, 0) != 0x1d)
214                 msleep(100);
215
216         if (timeout <= 0)
217         {
218                 printk(KERN_ERR "budget-av: cam reset failed (timeout).\n");
219                 saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
220                 return -ETIMEDOUT;
221         }
222
223         return 0;
224 }
225
226 static int ciintf_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
227 {
228         struct budget_av *budget_av = (struct budget_av *) ca->data;
229         struct saa7146_dev *saa = budget_av->budget.dev;
230
231         if (slot != 0)
232                 return -EINVAL;
233
234         dprintk(1, "ciintf_slot_shutdown\n");
235
236         ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTB);
237         budget_av->slot_status = 0;
238         return 0;
239 }
240
241 static int ciintf_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
242 {
243         struct budget_av *budget_av = (struct budget_av *) ca->data;
244         struct saa7146_dev *saa = budget_av->budget.dev;
245
246         if (slot != 0)
247                 return -EINVAL;
248
249         dprintk(1, "ciintf_slot_ts_enable: %d\n", budget_av->slot_status);
250
251         ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTA);
252         return 0;
253 }
254
255 static int ciintf_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
256 {
257         struct budget_av *budget_av = (struct budget_av *) ca->data;
258         struct saa7146_dev *saa = budget_av->budget.dev;
259         int cam_present = 0;
260
261         if (slot != 0)
262                 return -EINVAL;
263
264         if (!budget_av->slot_status)
265         {
266                 // first of all test the card detect line
267                 saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
268                 udelay(1);
269                 if (saa7146_read(saa, PSR) & MASK_06)
270                 {
271                         cam_present = 1;
272                 }
273                 saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
274
275                 // that is unreliable however, so try and read from IO memory
276                 if (!cam_present)
277                 {
278                         saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
279                         if (ttpci_budget_debiread(&budget_av->budget, DEBICICAM, 0, 1, 0, 1) != -ETIMEDOUT)
280                         {
281                                 cam_present = 1;
282                         }
283                 }
284
285                 // did we find something?
286                 if (cam_present) {
287                         printk(KERN_INFO "budget-av: cam inserted\n");
288                         budget_av->slot_status = 1;
289                 }
290         } else if (!open) {
291                 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
292                 if (ttpci_budget_debiread(&budget_av->budget, DEBICICAM, 0, 1, 0, 1) == -ETIMEDOUT)
293                 {
294                         printk(KERN_INFO "budget-av: cam ejected\n");
295                         saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
296                         budget_av->slot_status = 0;
297                 }
298         }
299
300         if (budget_av->slot_status == 1)
301                 return DVB_CA_EN50221_POLL_CAM_PRESENT | DVB_CA_EN50221_POLL_CAM_READY;
302
303         return 0;
304 }
305
306 static int ciintf_init(struct budget_av *budget_av)
307 {
308         struct saa7146_dev *saa = budget_av->budget.dev;
309         int result;
310
311         memset(&budget_av->ca, 0, sizeof(struct dvb_ca_en50221));
312
313         saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO);
314         saa7146_setgpio(saa, 1, SAA7146_GPIO_OUTLO);
315         saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTLO);
316         saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
317
318         /* Enable DEBI pins */
319         saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16) | 0x800);
320
321         /* register CI interface */
322         budget_av->ca.owner = THIS_MODULE;
323         budget_av->ca.read_attribute_mem = ciintf_read_attribute_mem;
324         budget_av->ca.write_attribute_mem = ciintf_write_attribute_mem;
325         budget_av->ca.read_cam_control = ciintf_read_cam_control;
326         budget_av->ca.write_cam_control = ciintf_write_cam_control;
327         budget_av->ca.slot_reset = ciintf_slot_reset;
328         budget_av->ca.slot_shutdown = ciintf_slot_shutdown;
329         budget_av->ca.slot_ts_enable = ciintf_slot_ts_enable;
330         budget_av->ca.poll_slot_status = ciintf_poll_slot_status;
331         budget_av->ca.data = budget_av;
332
333         if ((result = dvb_ca_en50221_init(&budget_av->budget.dvb_adapter,
334                                           &budget_av->ca, 0, 1)) != 0) {
335                 printk(KERN_ERR "budget-av: ci initialisation failed.\n");
336                 goto error;
337         }
338
339         printk(KERN_INFO "budget-av: ci interface initialised.\n");
340         budget_av->budget.ci_present = 1;
341         return 0;
342
343 error:
344         saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16));
345         return result;
346 }
347
348 static void ciintf_deinit(struct budget_av *budget_av)
349 {
350         struct saa7146_dev *saa = budget_av->budget.dev;
351
352         saa7146_setgpio(saa, 0, SAA7146_GPIO_INPUT);
353         saa7146_setgpio(saa, 1, SAA7146_GPIO_INPUT);
354         saa7146_setgpio(saa, 2, SAA7146_GPIO_INPUT);
355         saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
356
357         /* release the CA device */
358         dvb_ca_en50221_release(&budget_av->ca);
359
360         /* disable DEBI pins */
361         saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16));
362 }
363
364
365 static const u8 saa7113_tab[] = {
366         0x01, 0x08,
367         0x02, 0xc0,
368         0x03, 0x33,
369         0x04, 0x00,
370         0x05, 0x00,
371         0x06, 0xeb,
372         0x07, 0xe0,
373         0x08, 0x28,
374         0x09, 0x00,
375         0x0a, 0x80,
376         0x0b, 0x47,
377         0x0c, 0x40,
378         0x0d, 0x00,
379         0x0e, 0x01,
380         0x0f, 0x44,
381
382         0x10, 0x08,
383         0x11, 0x0c,
384         0x12, 0x7b,
385         0x13, 0x00,
386         0x15, 0x00, 0x16, 0x00, 0x17, 0x00,
387
388         0x57, 0xff,
389         0x40, 0x82, 0x58, 0x00, 0x59, 0x54, 0x5a, 0x07,
390         0x5b, 0x83, 0x5e, 0x00,
391         0xff
392 };
393
394 static int saa7113_init(struct budget_av *budget_av)
395 {
396         struct budget *budget = &budget_av->budget;
397         struct saa7146_dev *saa = budget->dev;
398         const u8 *data = saa7113_tab;
399
400         saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI);
401         msleep(200);
402
403         if (i2c_writereg(&budget->i2c_adap, 0x4a, 0x01, 0x08) != 1) {
404                 dprintk(1, "saa7113 not found on KNC card\n");
405                 return -ENODEV;
406         }
407
408         dprintk(1, "saa7113 detected and initializing\n");
409
410         while (*data != 0xff) {
411                 i2c_writereg(&budget->i2c_adap, 0x4a, *data, *(data + 1));
412                 data += 2;
413         }
414
415         dprintk(1, "saa7113  status=%02x\n", i2c_readreg(&budget->i2c_adap, 0x4a, 0x1f));
416
417         return 0;
418 }
419
420 static int saa7113_setinput(struct budget_av *budget_av, int input)
421 {
422         struct budget *budget = &budget_av->budget;
423
424         if (1 != budget_av->has_saa7113)
425                 return -ENODEV;
426
427         if (input == 1) {
428                 i2c_writereg(&budget->i2c_adap, 0x4a, 0x02, 0xc7);
429                 i2c_writereg(&budget->i2c_adap, 0x4a, 0x09, 0x80);
430         } else if (input == 0) {
431                 i2c_writereg(&budget->i2c_adap, 0x4a, 0x02, 0xc0);
432                 i2c_writereg(&budget->i2c_adap, 0x4a, 0x09, 0x00);
433         } else
434                 return -EINVAL;
435
436         budget_av->cur_input = input;
437         return 0;
438 }
439
440
441 static int philips_su1278_ty_ci_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
442 {
443         u8 aclk = 0;
444         u8 bclk = 0;
445         u8 m1;
446
447         aclk = 0xb5;
448         if (srate < 2000000)
449                 bclk = 0x86;
450         else if (srate < 5000000)
451                 bclk = 0x89;
452         else if (srate < 15000000)
453                 bclk = 0x8f;
454         else if (srate < 45000000)
455                 bclk = 0x95;
456
457         m1 = 0x14;
458         if (srate < 4000000)
459                 m1 = 0x10;
460
461         stv0299_writereg(fe, 0x13, aclk);
462         stv0299_writereg(fe, 0x14, bclk);
463         stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
464         stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
465         stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
466         stv0299_writereg(fe, 0x0f, 0x80 | m1);
467
468         return 0;
469 }
470
471 static int philips_su1278_ty_ci_pll_set(struct dvb_frontend *fe,
472                                         struct i2c_adapter *i2c,
473                                         struct dvb_frontend_parameters *params)
474 {
475         u32 div;
476         u8 buf[4];
477         struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
478
479         if ((params->frequency < 950000) || (params->frequency > 2150000))
480                 return -EINVAL;
481
482         div = (params->frequency + (125 - 1)) / 125;    // round correctly
483         buf[0] = (div >> 8) & 0x7f;
484         buf[1] = div & 0xff;
485         buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
486         buf[3] = 0x20;
487
488         if (params->u.qpsk.symbol_rate < 4000000)
489                 buf[3] |= 1;
490
491         if (params->frequency < 1250000)
492                 buf[3] |= 0;
493         else if (params->frequency < 1550000)
494                 buf[3] |= 0x40;
495         else if (params->frequency < 2050000)
496                 buf[3] |= 0x80;
497         else if (params->frequency < 2150000)
498                 buf[3] |= 0xC0;
499
500         if (i2c_transfer(i2c, &msg, 1) != 1)
501                 return -EIO;
502         return 0;
503 }
504
505 #define MIN2(a,b) ((a) < (b) ? (a) : (b))
506 #define MIN3(a,b,c) MIN2(MIN2(a,b),c)
507
508 static int philips_su1278sh2_tua6100_pll_set(struct dvb_frontend *fe,
509                                         struct i2c_adapter *i2c,
510                                         struct dvb_frontend_parameters *params)
511 {
512         u8 reg0 [2] = { 0x00, 0x00 };
513         u8 reg1 [4] = { 0x01, 0x00, 0x00, 0x00 };
514         u8 reg2 [3] = { 0x02, 0x00, 0x00 };
515         int _fband;
516         int first_ZF;
517         int R, A, N, P, M;
518         struct i2c_msg msg = {.addr = 0x60,.flags = 0,.buf = NULL,.len = 0 };
519         int freq = params->frequency;
520
521         first_ZF = (freq) / 1000;
522
523         if (abs(MIN2(abs(first_ZF-1190),abs(first_ZF-1790))) <
524                    abs(MIN3(abs(first_ZF-1202),abs(first_ZF-1542),abs(first_ZF-1890))))
525                 _fband = 2;
526         else
527                 _fband = 3;
528
529         if (_fband == 2) {
530                 if (((first_ZF >= 950) && (first_ZF < 1350)) ||
531                                     ((first_ZF >= 1430) && (first_ZF < 1950)))
532                         reg0[1] = 0x07;
533                 else if (((first_ZF >= 1350) && (first_ZF < 1430)) ||
534                                          ((first_ZF >= 1950) && (first_ZF < 2150)))
535                         reg0[1] = 0x0B;
536         }
537
538         if(_fband == 3) {
539                 if (((first_ZF >= 950) && (first_ZF < 1350)) ||
540                                     ((first_ZF >= 1455) && (first_ZF < 1950)))
541                         reg0[1] = 0x07;
542                 else if (((first_ZF >= 1350) && (first_ZF < 1420)) ||
543                                          ((first_ZF >= 1950) && (first_ZF < 2150)))
544                         reg0[1] = 0x0B;
545                 else if ((first_ZF >= 1420) && (first_ZF < 1455))
546                         reg0[1] = 0x0F;
547         }
548
549         if (first_ZF > 1525)
550                 reg1[1] |= 0x80;
551         else
552                 reg1[1] &= 0x7F;
553
554         if (_fband == 2) {
555                 if (first_ZF > 1430) { /* 1430MHZ */
556                         reg1[1] &= 0xCF; /* N2 */
557                         reg2[1] &= 0xCF; /* R2 */
558                         reg2[1] |= 0x10;
559                 } else {
560                         reg1[1] &= 0xCF; /* N2 */
561                         reg1[1] |= 0x20;
562                         reg2[1] &= 0xCF; /* R2 */
563                         reg2[1] |= 0x10;
564                 }
565         }
566
567         if (_fband == 3) {
568                 if ((first_ZF >= 1455) &&
569                                    (first_ZF < 1630)) {
570                         reg1[1] &= 0xCF; /* N2 */
571                         reg1[1] |= 0x20;
572                         reg2[1] &= 0xCF; /* R2 */
573                                    } else {
574                                            if (first_ZF < 1455) {
575                                                    reg1[1] &= 0xCF; /* N2 */
576                                                    reg1[1] |= 0x20;
577                                                    reg2[1] &= 0xCF; /* R2 */
578                                                    reg2[1] |= 0x10;
579                                            } else {
580                                                    if (first_ZF >= 1630) {
581                                                            reg1[1] &= 0xCF; /* N2 */
582                                                            reg2[1] &= 0xCF; /* R2 */
583                                                            reg2[1] |= 0x10;
584                                                    }
585                                            }
586                                    }
587         }
588
589         /* set ports, enable P0 for symbol rates > 4Ms/s */
590         if (params->u.qpsk.symbol_rate >= 4000000)
591                 reg1[1] |= 0x0c;
592         else
593                 reg1[1] |= 0x04;
594
595         reg2[1] |= 0x0c;
596
597         R = 64;
598         A = 64;
599         P = 64;  //32
600
601         M = (freq * R) / 4;             /* in Mhz */
602         N = (M - A * 1000) / (P * 1000);
603
604         reg1[1] |= (N >> 9) & 0x03;
605         reg1[2]  = (N >> 1) & 0xff;
606         reg1[3]  = (N << 7) & 0x80;
607
608         reg2[1] |= (R >> 8) & 0x03;
609         reg2[2]  = R & 0xFF;    /* R */
610
611         reg1[3] |= A & 0x7f;    /* A */
612
613         if (P == 64)
614                 reg1[1] |= 0x40; /* Prescaler 64/65 */
615
616         reg0[1] |= 0x03;
617
618         /* already enabled - do not reenable i2c repeater or TX fails */
619         msg.buf = reg0;
620         msg.len = sizeof(reg0);
621         if (i2c_transfer(i2c, &msg, 1) != 1)
622                 return -EIO;
623
624         stv0299_enable_plli2c(fe);
625         msg.buf = reg1;
626         msg.len = sizeof(reg1);
627         if (i2c_transfer(i2c, &msg, 1) != 1)
628                 return -EIO;
629
630         stv0299_enable_plli2c(fe);
631         msg.buf = reg2;
632         msg.len = sizeof(reg2);
633         if (i2c_transfer(i2c, &msg, 1) != 1)
634                 return -EIO;
635
636         return 0;
637 }
638
639 static u8 typhoon_cinergy1200s_inittab[] = {
640         0x01, 0x15,
641         0x02, 0x30,
642         0x03, 0x00,
643         0x04, 0x7d,             /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
644         0x05, 0x35,             /* I2CT = 0, SCLT = 1, SDAT = 1 */
645         0x06, 0x40,             /* DAC not used, set to high impendance mode */
646         0x07, 0x00,             /* DAC LSB */
647         0x08, 0x40,             /* DiSEqC off */
648         0x09, 0x00,             /* FIFO */
649         0x0c, 0x51,             /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
650         0x0d, 0x82,             /* DC offset compensation = ON, beta_agc1 = 2 */
651         0x0e, 0x23,             /* alpha_tmg = 2, beta_tmg = 3 */
652         0x10, 0x3f,             // AGC2  0x3d
653         0x11, 0x84,
654         0x12, 0xb9,
655         0x15, 0xc9,             // lock detector threshold
656         0x16, 0x00,
657         0x17, 0x00,
658         0x18, 0x00,
659         0x19, 0x00,
660         0x1a, 0x00,
661         0x1f, 0x50,
662         0x20, 0x00,
663         0x21, 0x00,
664         0x22, 0x00,
665         0x23, 0x00,
666         0x28, 0x00,             // out imp: normal  out type: parallel FEC mode:0
667         0x29, 0x1e,             // 1/2 threshold
668         0x2a, 0x14,             // 2/3 threshold
669         0x2b, 0x0f,             // 3/4 threshold
670         0x2c, 0x09,             // 5/6 threshold
671         0x2d, 0x05,             // 7/8 threshold
672         0x2e, 0x01,
673         0x31, 0x1f,             // test all FECs
674         0x32, 0x19,             // viterbi and synchro search
675         0x33, 0xfc,             // rs control
676         0x34, 0x93,             // error control
677         0x0f, 0x92,
678         0xff, 0xff
679 };
680
681 static struct stv0299_config typhoon_config = {
682         .demod_address = 0x68,
683         .inittab = typhoon_cinergy1200s_inittab,
684         .mclk = 88000000UL,
685         .invert = 0,
686         .skip_reinit = 0,
687         .lock_output = STV0229_LOCKOUTPUT_1,
688         .volt13_op0_op1 = STV0299_VOLT13_OP0,
689         .min_delay_ms = 100,
690         .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
691         .pll_set = philips_su1278_ty_ci_pll_set,
692 };
693
694
695 static struct stv0299_config cinergy_1200s_config = {
696         .demod_address = 0x68,
697         .inittab = typhoon_cinergy1200s_inittab,
698         .mclk = 88000000UL,
699         .invert = 0,
700         .skip_reinit = 0,
701         .lock_output = STV0229_LOCKOUTPUT_0,
702         .volt13_op0_op1 = STV0299_VOLT13_OP0,
703         .min_delay_ms = 100,
704         .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
705         .pll_set = philips_su1278_ty_ci_pll_set,
706 };
707
708 static struct stv0299_config cinergy_1200s_1894_0010_config = {
709         .demod_address = 0x68,
710         .inittab = typhoon_cinergy1200s_inittab,
711         .mclk = 88000000UL,
712         .invert = 1,
713         .skip_reinit = 0,
714         .lock_output = STV0229_LOCKOUTPUT_1,
715         .volt13_op0_op1 = STV0299_VOLT13_OP0,
716         .min_delay_ms = 100,
717         .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
718         .pll_set = philips_su1278sh2_tua6100_pll_set,
719 };
720
721 static int philips_cu1216_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
722 {
723         struct budget *budget = (struct budget *) fe->dvb->priv;
724         u8 buf[4];
725         struct i2c_msg msg = {.addr = 0x60,.flags = 0,.buf = buf,.len = sizeof(buf) };
726
727 #define TUNER_MUL 62500
728
729         u32 div = (params->frequency + 36125000 + TUNER_MUL / 2) / TUNER_MUL;
730
731         buf[0] = (div >> 8) & 0x7f;
732         buf[1] = div & 0xff;
733         buf[2] = 0x86;
734         buf[3] = (params->frequency < 150000000 ? 0x01 :
735                   params->frequency < 445000000 ? 0x02 : 0x04);
736
737         if (i2c_transfer(&budget->i2c_adap, &msg, 1) != 1)
738                 return -EIO;
739         return 0;
740 }
741
742 static struct tda10021_config philips_cu1216_config = {
743         .demod_address = 0x0c,
744         .pll_set = philips_cu1216_pll_set,
745 };
746
747
748
749
750 static int philips_tu1216_pll_init(struct dvb_frontend *fe)
751 {
752         struct budget *budget = (struct budget *) fe->dvb->priv;
753         static u8 tu1216_init[] = { 0x0b, 0xf5, 0x85, 0xab };
754         struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tu1216_init,.len = sizeof(tu1216_init) };
755
756         // setup PLL configuration
757         if (i2c_transfer(&budget->i2c_adap, &tuner_msg, 1) != 1)
758                 return -EIO;
759         msleep(1);
760
761         return 0;
762 }
763
764 static int philips_tu1216_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
765 {
766         struct budget *budget = (struct budget *) fe->dvb->priv;
767         u8 tuner_buf[4];
768         struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tuner_buf,.len =
769                         sizeof(tuner_buf) };
770         int tuner_frequency = 0;
771         u8 band, cp, filter;
772
773         // determine charge pump
774         tuner_frequency = params->frequency + 36166000;
775         if (tuner_frequency < 87000000)
776                 return -EINVAL;
777         else if (tuner_frequency < 130000000)
778                 cp = 3;
779         else if (tuner_frequency < 160000000)
780                 cp = 5;
781         else if (tuner_frequency < 200000000)
782                 cp = 6;
783         else if (tuner_frequency < 290000000)
784                 cp = 3;
785         else if (tuner_frequency < 420000000)
786                 cp = 5;
787         else if (tuner_frequency < 480000000)
788                 cp = 6;
789         else if (tuner_frequency < 620000000)
790                 cp = 3;
791         else if (tuner_frequency < 830000000)
792                 cp = 5;
793         else if (tuner_frequency < 895000000)
794                 cp = 7;
795         else
796                 return -EINVAL;
797
798         // determine band
799         if (params->frequency < 49000000)
800                 return -EINVAL;
801         else if (params->frequency < 161000000)
802                 band = 1;
803         else if (params->frequency < 444000000)
804                 band = 2;
805         else if (params->frequency < 861000000)
806                 band = 4;
807         else
808                 return -EINVAL;
809
810         // setup PLL filter
811         switch (params->u.ofdm.bandwidth) {
812         case BANDWIDTH_6_MHZ:
813                 filter = 0;
814                 break;
815
816         case BANDWIDTH_7_MHZ:
817                 filter = 0;
818                 break;
819
820         case BANDWIDTH_8_MHZ:
821                 filter = 1;
822                 break;
823
824         default:
825                 return -EINVAL;
826         }
827
828         // calculate divisor
829         // ((36166000+((1000000/6)/2)) + Finput)/(1000000/6)
830         tuner_frequency = (((params->frequency / 1000) * 6) + 217496) / 1000;
831
832         // setup tuner buffer
833         tuner_buf[0] = (tuner_frequency >> 8) & 0x7f;
834         tuner_buf[1] = tuner_frequency & 0xff;
835         tuner_buf[2] = 0xca;
836         tuner_buf[3] = (cp << 5) | (filter << 3) | band;
837
838         if (i2c_transfer(&budget->i2c_adap, &tuner_msg, 1) != 1)
839                 return -EIO;
840
841         msleep(1);
842         return 0;
843 }
844
845 static int philips_tu1216_request_firmware(struct dvb_frontend *fe,
846                                            const struct firmware **fw, char *name)
847 {
848         struct budget *budget = (struct budget *) fe->dvb->priv;
849
850         return request_firmware(fw, name, &budget->dev->pci->dev);
851 }
852
853 static struct tda1004x_config philips_tu1216_config = {
854
855         .demod_address = 0x8,
856         .invert = 1,
857         .invert_oclk = 1,
858         .xtal_freq = TDA10046_XTAL_4M,
859         .agc_config = TDA10046_AGC_DEFAULT,
860         .if_freq = TDA10046_FREQ_3617,
861         .pll_init = philips_tu1216_pll_init,
862         .pll_set = philips_tu1216_pll_set,
863         .pll_sleep = NULL,
864         .request_firmware = philips_tu1216_request_firmware,
865 };
866
867
868
869
870 static u8 read_pwm(struct budget_av *budget_av)
871 {
872         u8 b = 0xff;
873         u8 pwm;
874         struct i2c_msg msg[] = { {.addr = 0x50,.flags = 0,.buf = &b,.len = 1},
875         {.addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1}
876         };
877
878         if ((i2c_transfer(&budget_av->budget.i2c_adap, msg, 2) != 2)
879             || (pwm == 0xff))
880                 pwm = 0x48;
881
882         return pwm;
883 }
884
885 #define SUBID_DVBS_KNC1         0x0010
886 #define SUBID_DVBS_KNC1_PLUS    0x0011
887 #define SUBID_DVBS_TYPHOON      0x4f56
888 #define SUBID_DVBS_CINERGY1200  0x1154
889
890 #define SUBID_DVBC_KNC1         0x0020
891 #define SUBID_DVBC_KNC1_PLUS    0x0021
892 #define SUBID_DVBC_CINERGY1200  0x1156
893
894 #define SUBID_DVBT_KNC1_PLUS    0x0031
895 #define SUBID_DVBT_KNC1         0x0030
896 #define SUBID_DVBT_CINERGY1200  0x1157
897
898 static void frontend_init(struct budget_av *budget_av)
899 {
900         struct saa7146_dev * saa = budget_av->budget.dev;
901         struct dvb_frontend * fe = NULL;
902
903         switch (saa->pci->subsystem_device) {
904                 case SUBID_DVBS_KNC1_PLUS:
905                 case SUBID_DVBC_KNC1_PLUS:
906                 case SUBID_DVBT_KNC1_PLUS:
907                         // Enable / PowerON Frontend
908                         saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO);
909                         saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTHI);
910                         break;
911         }
912
913         switch (saa->pci->subsystem_device) {
914
915         case SUBID_DVBS_KNC1:
916                 if (saa->pci->subsystem_vendor == 0x1894) {
917                         fe = stv0299_attach(&cinergy_1200s_1894_0010_config,
918                                              &budget_av->budget.i2c_adap);
919                 } else {
920                         fe = stv0299_attach(&typhoon_config,
921                                              &budget_av->budget.i2c_adap);
922                 }
923                 break;
924
925         case SUBID_DVBS_KNC1_PLUS:
926         case SUBID_DVBS_TYPHOON:
927                 fe = stv0299_attach(&typhoon_config,
928                                     &budget_av->budget.i2c_adap);
929                 break;
930
931         case SUBID_DVBS_CINERGY1200:
932                 fe = stv0299_attach(&cinergy_1200s_config,
933                                     &budget_av->budget.i2c_adap);
934                 break;
935
936         case SUBID_DVBC_KNC1:
937         case SUBID_DVBC_KNC1_PLUS:
938                 fe = tda10021_attach(&philips_cu1216_config,
939                                      &budget_av->budget.i2c_adap,
940                                      read_pwm(budget_av));
941                 break;
942
943         case SUBID_DVBT_KNC1:
944         case SUBID_DVBT_KNC1_PLUS:
945                 fe = tda10046_attach(&philips_tu1216_config,
946                                      &budget_av->budget.i2c_adap);
947                 break;
948
949         case SUBID_DVBC_CINERGY1200:
950                 fe = tda10021_attach(&philips_cu1216_config,
951                                      &budget_av->budget.i2c_adap,
952                                      read_pwm(budget_av));
953                 break;
954
955         case SUBID_DVBT_CINERGY1200:
956                 fe = tda10046_attach(&philips_tu1216_config,
957                                      &budget_av->budget.i2c_adap);
958                 break;
959         }
960
961         if (fe == NULL) {
962                 printk(KERN_ERR "budget-av: A frontend driver was not found "
963                                 "for device %04x/%04x subsystem %04x/%04x\n",
964                        saa->pci->vendor,
965                        saa->pci->device,
966                        saa->pci->subsystem_vendor,
967                        saa->pci->subsystem_device);
968                 return;
969         }
970
971         budget_av->budget.dvb_frontend = fe;
972
973         if (dvb_register_frontend(&budget_av->budget.dvb_adapter,
974                                   budget_av->budget.dvb_frontend)) {
975                 printk(KERN_ERR "budget-av: Frontend registration failed!\n");
976                 if (budget_av->budget.dvb_frontend->ops->release)
977                         budget_av->budget.dvb_frontend->ops->release(budget_av->budget.dvb_frontend);
978                 budget_av->budget.dvb_frontend = NULL;
979         }
980 }
981
982
983 static void budget_av_irq(struct saa7146_dev *dev, u32 * isr)
984 {
985         struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
986
987         dprintk(8, "dev: %p, budget_av: %p\n", dev, budget_av);
988
989         if (*isr & MASK_10)
990                 ttpci_budget_irq10_handler(dev, isr);
991 }
992
993 static int budget_av_detach(struct saa7146_dev *dev)
994 {
995         struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
996         int err;
997
998         dprintk(2, "dev: %p\n", dev);
999
1000         if (1 == budget_av->has_saa7113) {
1001                 saa7146_setgpio(dev, 0, SAA7146_GPIO_OUTLO);
1002
1003                 msleep(200);
1004
1005                 saa7146_unregister_device(&budget_av->vd, dev);
1006         }
1007
1008         if (budget_av->budget.ci_present)
1009                 ciintf_deinit(budget_av);
1010
1011         if (budget_av->budget.dvb_frontend != NULL)
1012                 dvb_unregister_frontend(budget_av->budget.dvb_frontend);
1013         err = ttpci_budget_deinit(&budget_av->budget);
1014
1015         kfree(budget_av);
1016
1017         return err;
1018 }
1019
1020 static struct saa7146_ext_vv vv_data;
1021
1022 static int budget_av_attach(struct saa7146_dev *dev, struct saa7146_pci_extension_data *info)
1023 {
1024         struct budget_av *budget_av;
1025         u8 *mac;
1026         int err;
1027
1028         dprintk(2, "dev: %p\n", dev);
1029
1030         if (!(budget_av = kmalloc(sizeof(struct budget_av), GFP_KERNEL)))
1031                 return -ENOMEM;
1032
1033         memset(budget_av, 0, sizeof(struct budget_av));
1034
1035         budget_av->has_saa7113 = 0;
1036         budget_av->budget.ci_present = 0;
1037
1038         dev->ext_priv = budget_av;
1039
1040         if ((err = ttpci_budget_init(&budget_av->budget, dev, info, THIS_MODULE))) {
1041                 kfree(budget_av);
1042                 return err;
1043         }
1044
1045         /* knc1 initialization */
1046         saa7146_write(dev, DD1_STREAM_B, 0x04000000);
1047         saa7146_write(dev, DD1_INIT, 0x07000600);
1048         saa7146_write(dev, MC2, MASK_09 | MASK_25 | MASK_10 | MASK_26);
1049
1050         if (saa7113_init(budget_av) == 0) {
1051                 budget_av->has_saa7113 = 1;
1052
1053                 if (0 != saa7146_vv_init(dev, &vv_data)) {
1054                         /* fixme: proper cleanup here */
1055                         ERR(("cannot init vv subsystem.\n"));
1056                         return err;
1057                 }
1058
1059                 if ((err = saa7146_register_device(&budget_av->vd, dev, "knc1", VFL_TYPE_GRABBER))) {
1060                         /* fixme: proper cleanup here */
1061                         ERR(("cannot register capture v4l2 device.\n"));
1062                         return err;
1063                 }
1064
1065                 /* beware: this modifies dev->vv ... */
1066                 saa7146_set_hps_source_and_sync(dev, SAA7146_HPS_SOURCE_PORT_A,
1067                                                 SAA7146_HPS_SYNC_PORT_A);
1068
1069                 saa7113_setinput(budget_av, 0);
1070         } else {
1071                 ciintf_init(budget_av);
1072         }
1073
1074         /* fixme: find some sane values here... */
1075         saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
1076
1077         mac = budget_av->budget.dvb_adapter.proposed_mac;
1078         if (i2c_readregs(&budget_av->budget.i2c_adap, 0xa0, 0x30, mac, 6)) {
1079                 printk(KERN_ERR "KNC1-%d: Could not read MAC from KNC1 card\n",
1080                        budget_av->budget.dvb_adapter.num);
1081                 memset(mac, 0, 6);
1082         } else {
1083                 printk(KERN_INFO "KNC1-%d: MAC addr = %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
1084                        budget_av->budget.dvb_adapter.num,
1085                        mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
1086         }
1087
1088         budget_av->budget.dvb_adapter.priv = budget_av;
1089         frontend_init(budget_av);
1090
1091         return 0;
1092 }
1093
1094 #define KNC1_INPUTS 2
1095 static struct v4l2_input knc1_inputs[KNC1_INPUTS] = {
1096         {0, "Composite", V4L2_INPUT_TYPE_TUNER, 1, 0, V4L2_STD_PAL_BG | V4L2_STD_NTSC_M, 0},
1097         {1, "S-Video", V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG | V4L2_STD_NTSC_M, 0},
1098 };
1099
1100 static struct saa7146_extension_ioctls ioctls[] = {
1101         {VIDIOC_ENUMINPUT, SAA7146_EXCLUSIVE},
1102         {VIDIOC_G_INPUT, SAA7146_EXCLUSIVE},
1103         {VIDIOC_S_INPUT, SAA7146_EXCLUSIVE},
1104         {0, 0}
1105 };
1106
1107 static int av_ioctl(struct saa7146_fh *fh, unsigned int cmd, void *arg)
1108 {
1109         struct saa7146_dev *dev = fh->dev;
1110         struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
1111
1112         switch (cmd) {
1113         case VIDIOC_ENUMINPUT:{
1114                 struct v4l2_input *i = arg;
1115
1116                 dprintk(1, "VIDIOC_ENUMINPUT %d.\n", i->index);
1117                 if (i->index < 0 || i->index >= KNC1_INPUTS) {
1118                         return -EINVAL;
1119                 }
1120                 memcpy(i, &knc1_inputs[i->index], sizeof(struct v4l2_input));
1121                 return 0;
1122         }
1123         case VIDIOC_G_INPUT:{
1124                 int *input = (int *) arg;
1125
1126                 *input = budget_av->cur_input;
1127
1128                 dprintk(1, "VIDIOC_G_INPUT %d.\n", *input);
1129                 return 0;
1130         }
1131         case VIDIOC_S_INPUT:{
1132                 int input = *(int *) arg;
1133                 dprintk(1, "VIDIOC_S_INPUT %d.\n", input);
1134                 return saa7113_setinput(budget_av, input);
1135         }
1136         default:
1137                 return -ENOIOCTLCMD;
1138         }
1139         return 0;
1140 }
1141
1142 static struct saa7146_standard standard[] = {
1143         {.name = "PAL",.id = V4L2_STD_PAL,
1144          .v_offset = 0x17,.v_field = 288,
1145          .h_offset = 0x14,.h_pixels = 680,
1146          .v_max_out = 576,.h_max_out = 768 },
1147
1148         {.name = "NTSC",.id = V4L2_STD_NTSC,
1149          .v_offset = 0x16,.v_field = 240,
1150          .h_offset = 0x06,.h_pixels = 708,
1151          .v_max_out = 480,.h_max_out = 640, },
1152 };
1153
1154 static struct saa7146_ext_vv vv_data = {
1155         .inputs = 2,
1156         .capabilities = 0,      // perhaps later: V4L2_CAP_VBI_CAPTURE, but that need tweaking with the saa7113
1157         .flags = 0,
1158         .stds = &standard[0],
1159         .num_stds = sizeof(standard) / sizeof(struct saa7146_standard),
1160         .ioctls = &ioctls[0],
1161         .ioctl = av_ioctl,
1162 };
1163
1164 static struct saa7146_extension budget_extension;
1165
1166 MAKE_BUDGET_INFO(knc1s, "KNC1 DVB-S", BUDGET_KNC1S);
1167 MAKE_BUDGET_INFO(knc1c, "KNC1 DVB-C", BUDGET_KNC1C);
1168 MAKE_BUDGET_INFO(knc1t, "KNC1 DVB-T", BUDGET_KNC1T);
1169 MAKE_BUDGET_INFO(knc1sp, "KNC1 DVB-S Plus", BUDGET_KNC1SP);
1170 MAKE_BUDGET_INFO(knc1cp, "KNC1 DVB-C Plus", BUDGET_KNC1CP);
1171 MAKE_BUDGET_INFO(knc1tp, "KNC1 DVB-T Plus", BUDGET_KNC1TP);
1172 MAKE_BUDGET_INFO(cin1200s, "TerraTec Cinergy 1200 DVB-S", BUDGET_CIN1200S);
1173 MAKE_BUDGET_INFO(cin1200c, "Terratec Cinergy 1200 DVB-C", BUDGET_CIN1200C);
1174 MAKE_BUDGET_INFO(cin1200t, "Terratec Cinergy 1200 DVB-T", BUDGET_CIN1200T);
1175
1176 static struct pci_device_id pci_tbl[] = {
1177         MAKE_EXTENSION_PCI(knc1s, 0x1131, 0x4f56),
1178         MAKE_EXTENSION_PCI(knc1s, 0x1131, 0x0010),
1179         MAKE_EXTENSION_PCI(knc1s, 0x1894, 0x0010),
1180         MAKE_EXTENSION_PCI(knc1sp, 0x1131, 0x0011),
1181         MAKE_EXTENSION_PCI(knc1c, 0x1894, 0x0020),
1182         MAKE_EXTENSION_PCI(knc1cp, 0x1894, 0x0021),
1183         MAKE_EXTENSION_PCI(knc1t, 0x1894, 0x0030),
1184         MAKE_EXTENSION_PCI(knc1tp, 0x1894, 0x0031),
1185         MAKE_EXTENSION_PCI(cin1200s, 0x153b, 0x1154),
1186         MAKE_EXTENSION_PCI(cin1200c, 0x153b, 0x1156),
1187         MAKE_EXTENSION_PCI(cin1200t, 0x153b, 0x1157),
1188         {
1189          .vendor = 0,
1190         }
1191 };
1192
1193 MODULE_DEVICE_TABLE(pci, pci_tbl);
1194
1195 static struct saa7146_extension budget_extension = {
1196         .name = "budget_av",
1197         .flags = SAA7146_I2C_SHORT_DELAY,
1198
1199         .pci_tbl = pci_tbl,
1200
1201         .module = THIS_MODULE,
1202         .attach = budget_av_attach,
1203         .detach = budget_av_detach,
1204
1205         .irq_mask = MASK_10,
1206         .irq_func = budget_av_irq,
1207 };
1208
1209 static int __init budget_av_init(void)
1210 {
1211         return saa7146_register_extension(&budget_extension);
1212 }
1213
1214 static void __exit budget_av_exit(void)
1215 {
1216         saa7146_unregister_extension(&budget_extension);
1217 }
1218
1219 module_init(budget_av_init);
1220 module_exit(budget_av_exit);
1221
1222 MODULE_LICENSE("GPL");
1223 MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, Michael Hunold, others");
1224 MODULE_DESCRIPTION("driver for the SAA7146 based so-called "
1225                    "budget PCI DVB w/ analog input and CI-module (e.g. the KNC cards)");