Merge /pub/scm/linux/kernel/git/torvalds/linux-2.6
[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, 0);
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, 0);
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                 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI); /* Vcc off */
221                 return -ETIMEDOUT;
222         }
223
224         return 0;
225 }
226
227 static int ciintf_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
228 {
229         struct budget_av *budget_av = (struct budget_av *) ca->data;
230         struct saa7146_dev *saa = budget_av->budget.dev;
231
232         if (slot != 0)
233                 return -EINVAL;
234
235         dprintk(1, "ciintf_slot_shutdown\n");
236
237         ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTB);
238         budget_av->slot_status = 0;
239         return 0;
240 }
241
242 static int ciintf_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
243 {
244         struct budget_av *budget_av = (struct budget_av *) ca->data;
245         struct saa7146_dev *saa = budget_av->budget.dev;
246
247         if (slot != 0)
248                 return -EINVAL;
249
250         dprintk(1, "ciintf_slot_ts_enable: %d\n", budget_av->slot_status);
251
252         ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTA);
253         return 0;
254 }
255
256 static int ciintf_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
257 {
258         struct budget_av *budget_av = (struct budget_av *) ca->data;
259         struct saa7146_dev *saa = budget_av->budget.dev;
260
261         if (slot != 0)
262                 return -EINVAL;
263
264         if (!budget_av->slot_status) {
265                 saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
266                 udelay(1);
267                 if (saa7146_read(saa, PSR) & MASK_06)
268                 {
269                         printk(KERN_INFO "budget-av: cam inserted\n");
270                         budget_av->slot_status = 1;
271                 }
272                 saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
273         } else if (!open) {
274                 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
275                 if (ttpci_budget_debiread(&budget_av->budget, DEBICICAM, 0, 1, 0, 1) == -ETIMEDOUT)
276                 {
277                         printk(KERN_INFO "budget-av: cam ejected\n");
278                         saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
279                         saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI); /* Vcc off */
280                         budget_av->slot_status = 0;
281                 }
282         }
283
284         if (budget_av->slot_status == 1)
285                 return DVB_CA_EN50221_POLL_CAM_PRESENT | DVB_CA_EN50221_POLL_CAM_READY;
286
287         return 0;
288 }
289
290 static int ciintf_init(struct budget_av *budget_av)
291 {
292         struct saa7146_dev *saa = budget_av->budget.dev;
293         int result;
294
295         memset(&budget_av->ca, 0, sizeof(struct dvb_ca_en50221));
296
297         saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO);
298         saa7146_setgpio(saa, 1, SAA7146_GPIO_OUTLO);
299         saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTLO);
300         saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
301
302         /* Enable DEBI pins */
303         saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16) | 0x800);
304
305         /* register CI interface */
306         budget_av->ca.owner = THIS_MODULE;
307         budget_av->ca.read_attribute_mem = ciintf_read_attribute_mem;
308         budget_av->ca.write_attribute_mem = ciintf_write_attribute_mem;
309         budget_av->ca.read_cam_control = ciintf_read_cam_control;
310         budget_av->ca.write_cam_control = ciintf_write_cam_control;
311         budget_av->ca.slot_reset = ciintf_slot_reset;
312         budget_av->ca.slot_shutdown = ciintf_slot_shutdown;
313         budget_av->ca.slot_ts_enable = ciintf_slot_ts_enable;
314         budget_av->ca.poll_slot_status = ciintf_poll_slot_status;
315         budget_av->ca.data = budget_av;
316
317         if ((result = dvb_ca_en50221_init(&budget_av->budget.dvb_adapter,
318                                           &budget_av->ca, 0, 1)) != 0) {
319                 printk(KERN_ERR "budget-av: ci initialisation failed.\n");
320                 goto error;
321         }
322
323         printk(KERN_INFO "budget-av: ci interface initialised.\n");
324         budget_av->budget.ci_present = 1;
325         return 0;
326
327 error:
328         saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16));
329         return result;
330 }
331
332 static void ciintf_deinit(struct budget_av *budget_av)
333 {
334         struct saa7146_dev *saa = budget_av->budget.dev;
335
336         saa7146_setgpio(saa, 0, SAA7146_GPIO_INPUT);
337         saa7146_setgpio(saa, 1, SAA7146_GPIO_INPUT);
338         saa7146_setgpio(saa, 2, SAA7146_GPIO_INPUT);
339         saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
340
341         /* release the CA device */
342         dvb_ca_en50221_release(&budget_av->ca);
343
344         /* disable DEBI pins */
345         saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16));
346 }
347
348
349 static const u8 saa7113_tab[] = {
350         0x01, 0x08,
351         0x02, 0xc0,
352         0x03, 0x33,
353         0x04, 0x00,
354         0x05, 0x00,
355         0x06, 0xeb,
356         0x07, 0xe0,
357         0x08, 0x28,
358         0x09, 0x00,
359         0x0a, 0x80,
360         0x0b, 0x47,
361         0x0c, 0x40,
362         0x0d, 0x00,
363         0x0e, 0x01,
364         0x0f, 0x44,
365
366         0x10, 0x08,
367         0x11, 0x0c,
368         0x12, 0x7b,
369         0x13, 0x00,
370         0x15, 0x00, 0x16, 0x00, 0x17, 0x00,
371
372         0x57, 0xff,
373         0x40, 0x82, 0x58, 0x00, 0x59, 0x54, 0x5a, 0x07,
374         0x5b, 0x83, 0x5e, 0x00,
375         0xff
376 };
377
378 static int saa7113_init(struct budget_av *budget_av)
379 {
380         struct budget *budget = &budget_av->budget;
381         struct saa7146_dev *saa = budget->dev;
382         const u8 *data = saa7113_tab;
383
384         saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI);
385         msleep(200);
386
387         if (i2c_writereg(&budget->i2c_adap, 0x4a, 0x01, 0x08) != 1) {
388                 dprintk(1, "saa7113 not found on KNC card\n");
389                 return -ENODEV;
390         }
391
392         dprintk(1, "saa7113 detected and initializing\n");
393
394         while (*data != 0xff) {
395                 i2c_writereg(&budget->i2c_adap, 0x4a, *data, *(data + 1));
396                 data += 2;
397         }
398
399         dprintk(1, "saa7113  status=%02x\n", i2c_readreg(&budget->i2c_adap, 0x4a, 0x1f));
400
401         return 0;
402 }
403
404 static int saa7113_setinput(struct budget_av *budget_av, int input)
405 {
406         struct budget *budget = &budget_av->budget;
407
408         if (1 != budget_av->has_saa7113)
409                 return -ENODEV;
410
411         if (input == 1) {
412                 i2c_writereg(&budget->i2c_adap, 0x4a, 0x02, 0xc7);
413                 i2c_writereg(&budget->i2c_adap, 0x4a, 0x09, 0x80);
414         } else if (input == 0) {
415                 i2c_writereg(&budget->i2c_adap, 0x4a, 0x02, 0xc0);
416                 i2c_writereg(&budget->i2c_adap, 0x4a, 0x09, 0x00);
417         } else
418                 return -EINVAL;
419
420         budget_av->cur_input = input;
421         return 0;
422 }
423
424
425 static int philips_su1278_ty_ci_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
426 {
427         u8 aclk = 0;
428         u8 bclk = 0;
429         u8 m1;
430
431         aclk = 0xb5;
432         if (srate < 2000000)
433                 bclk = 0x86;
434         else if (srate < 5000000)
435                 bclk = 0x89;
436         else if (srate < 15000000)
437                 bclk = 0x8f;
438         else if (srate < 45000000)
439                 bclk = 0x95;
440
441         m1 = 0x14;
442         if (srate < 4000000)
443                 m1 = 0x10;
444
445         stv0299_writereg(fe, 0x13, aclk);
446         stv0299_writereg(fe, 0x14, bclk);
447         stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
448         stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
449         stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
450         stv0299_writereg(fe, 0x0f, 0x80 | m1);
451
452         return 0;
453 }
454
455 static int philips_su1278_ty_ci_pll_set(struct dvb_frontend *fe,
456                                         struct dvb_frontend_parameters *params)
457 {
458         struct budget_av *budget_av = (struct budget_av *) fe->dvb->priv;
459         u32 div;
460         u8 buf[4];
461         struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
462
463         if ((params->frequency < 950000) || (params->frequency > 2150000))
464                 return -EINVAL;
465
466         div = (params->frequency + (125 - 1)) / 125;    // round correctly
467         buf[0] = (div >> 8) & 0x7f;
468         buf[1] = div & 0xff;
469         buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
470         buf[3] = 0x20;
471
472         if (params->u.qpsk.symbol_rate < 4000000)
473                 buf[3] |= 1;
474
475         if (params->frequency < 1250000)
476                 buf[3] |= 0;
477         else if (params->frequency < 1550000)
478                 buf[3] |= 0x40;
479         else if (params->frequency < 2050000)
480                 buf[3] |= 0x80;
481         else if (params->frequency < 2150000)
482                 buf[3] |= 0xC0;
483
484         if (i2c_transfer(&budget_av->budget.i2c_adap, &msg, 1) != 1)
485                 return -EIO;
486         return 0;
487 }
488
489 static u8 typhoon_cinergy1200s_inittab[] = {
490         0x01, 0x15,
491         0x02, 0x30,
492         0x03, 0x00,
493         0x04, 0x7d,             /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
494         0x05, 0x35,             /* I2CT = 0, SCLT = 1, SDAT = 1 */
495         0x06, 0x40,             /* DAC not used, set to high impendance mode */
496         0x07, 0x00,             /* DAC LSB */
497         0x08, 0x40,             /* DiSEqC off */
498         0x09, 0x00,             /* FIFO */
499         0x0c, 0x51,             /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
500         0x0d, 0x82,             /* DC offset compensation = ON, beta_agc1 = 2 */
501         0x0e, 0x23,             /* alpha_tmg = 2, beta_tmg = 3 */
502         0x10, 0x3f,             // AGC2  0x3d
503         0x11, 0x84,
504         0x12, 0xb5,             // Lock detect: -64  Carrier freq detect:on
505         0x15, 0xc9,             // lock detector threshold
506         0x16, 0x00,
507         0x17, 0x00,
508         0x18, 0x00,
509         0x19, 0x00,
510         0x1a, 0x00,
511         0x1f, 0x50,
512         0x20, 0x00,
513         0x21, 0x00,
514         0x22, 0x00,
515         0x23, 0x00,
516         0x28, 0x00,             // out imp: normal  out type: parallel FEC mode:0
517         0x29, 0x1e,             // 1/2 threshold
518         0x2a, 0x14,             // 2/3 threshold
519         0x2b, 0x0f,             // 3/4 threshold
520         0x2c, 0x09,             // 5/6 threshold
521         0x2d, 0x05,             // 7/8 threshold
522         0x2e, 0x01,
523         0x31, 0x1f,             // test all FECs
524         0x32, 0x19,             // viterbi and synchro search
525         0x33, 0xfc,             // rs control
526         0x34, 0x93,             // error control
527         0x0f, 0x92,
528         0xff, 0xff
529 };
530
531 static struct stv0299_config typhoon_config = {
532         .demod_address = 0x68,
533         .inittab = typhoon_cinergy1200s_inittab,
534         .mclk = 88000000UL,
535         .invert = 0,
536         .enhanced_tuning = 0,
537         .skip_reinit = 0,
538         .lock_output = STV0229_LOCKOUTPUT_1,
539         .volt13_op0_op1 = STV0299_VOLT13_OP0,
540         .min_delay_ms = 100,
541         .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
542         .pll_set = philips_su1278_ty_ci_pll_set,
543 };
544
545
546 static struct stv0299_config cinergy_1200s_config = {
547         .demod_address = 0x68,
548         .inittab = typhoon_cinergy1200s_inittab,
549         .mclk = 88000000UL,
550         .invert = 0,
551         .enhanced_tuning = 0,
552         .skip_reinit = 0,
553         .lock_output = STV0229_LOCKOUTPUT_0,
554         .volt13_op0_op1 = STV0299_VOLT13_OP0,
555         .min_delay_ms = 100,
556         .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
557         .pll_set = philips_su1278_ty_ci_pll_set,
558 };
559
560
561 static int philips_cu1216_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
562 {
563         struct budget *budget = (struct budget *) fe->dvb->priv;
564         u8 buf[4];
565         struct i2c_msg msg = {.addr = 0x60,.flags = 0,.buf = buf,.len = sizeof(buf) };
566
567 #define TUNER_MUL 62500
568
569         u32 div = (params->frequency + 36125000 + TUNER_MUL / 2) / TUNER_MUL;
570
571         buf[0] = (div >> 8) & 0x7f;
572         buf[1] = div & 0xff;
573         buf[2] = 0x86;
574         buf[3] = (params->frequency < 150000000 ? 0x01 :
575                   params->frequency < 445000000 ? 0x02 : 0x04);
576
577         if (i2c_transfer(&budget->i2c_adap, &msg, 1) != 1)
578                 return -EIO;
579         return 0;
580 }
581
582 static struct tda10021_config philips_cu1216_config = {
583         .demod_address = 0x0c,
584         .pll_set = philips_cu1216_pll_set,
585 };
586
587
588
589
590 static int philips_tu1216_pll_init(struct dvb_frontend *fe)
591 {
592         struct budget *budget = (struct budget *) fe->dvb->priv;
593         static u8 tu1216_init[] = { 0x0b, 0xf5, 0x85, 0xab };
594         struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tu1216_init,.len = sizeof(tu1216_init) };
595
596         // setup PLL configuration
597         if (i2c_transfer(&budget->i2c_adap, &tuner_msg, 1) != 1)
598                 return -EIO;
599         msleep(1);
600
601         return 0;
602 }
603
604 static int philips_tu1216_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
605 {
606         struct budget *budget = (struct budget *) fe->dvb->priv;
607         u8 tuner_buf[4];
608         struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tuner_buf,.len =
609                         sizeof(tuner_buf) };
610         int tuner_frequency = 0;
611         u8 band, cp, filter;
612
613         // determine charge pump
614         tuner_frequency = params->frequency + 36166000;
615         if (tuner_frequency < 87000000)
616                 return -EINVAL;
617         else if (tuner_frequency < 130000000)
618                 cp = 3;
619         else if (tuner_frequency < 160000000)
620                 cp = 5;
621         else if (tuner_frequency < 200000000)
622                 cp = 6;
623         else if (tuner_frequency < 290000000)
624                 cp = 3;
625         else if (tuner_frequency < 420000000)
626                 cp = 5;
627         else if (tuner_frequency < 480000000)
628                 cp = 6;
629         else if (tuner_frequency < 620000000)
630                 cp = 3;
631         else if (tuner_frequency < 830000000)
632                 cp = 5;
633         else if (tuner_frequency < 895000000)
634                 cp = 7;
635         else
636                 return -EINVAL;
637
638         // determine band
639         if (params->frequency < 49000000)
640                 return -EINVAL;
641         else if (params->frequency < 161000000)
642                 band = 1;
643         else if (params->frequency < 444000000)
644                 band = 2;
645         else if (params->frequency < 861000000)
646                 band = 4;
647         else
648                 return -EINVAL;
649
650         // setup PLL filter
651         switch (params->u.ofdm.bandwidth) {
652         case BANDWIDTH_6_MHZ:
653                 filter = 0;
654                 break;
655
656         case BANDWIDTH_7_MHZ:
657                 filter = 0;
658                 break;
659
660         case BANDWIDTH_8_MHZ:
661                 filter = 1;
662                 break;
663
664         default:
665                 return -EINVAL;
666         }
667
668         // calculate divisor
669         // ((36166000+((1000000/6)/2)) + Finput)/(1000000/6)
670         tuner_frequency = (((params->frequency / 1000) * 6) + 217496) / 1000;
671
672         // setup tuner buffer
673         tuner_buf[0] = (tuner_frequency >> 8) & 0x7f;
674         tuner_buf[1] = tuner_frequency & 0xff;
675         tuner_buf[2] = 0xca;
676         tuner_buf[3] = (cp << 5) | (filter << 3) | band;
677
678         if (i2c_transfer(&budget->i2c_adap, &tuner_msg, 1) != 1)
679                 return -EIO;
680
681         msleep(1);
682         return 0;
683 }
684
685 static int philips_tu1216_request_firmware(struct dvb_frontend *fe,
686                                            const struct firmware **fw, char *name)
687 {
688         struct budget *budget = (struct budget *) fe->dvb->priv;
689
690         return request_firmware(fw, name, &budget->dev->pci->dev);
691 }
692
693 static struct tda1004x_config philips_tu1216_config = {
694
695         .demod_address = 0x8,
696         .invert = 1,
697         .invert_oclk = 1,
698         .xtal_freq = TDA10046_XTAL_4M,
699         .agc_config = TDA10046_AGC_DEFAULT,
700         .if_freq = TDA10046_FREQ_3617,
701         .pll_init = philips_tu1216_pll_init,
702         .pll_set = philips_tu1216_pll_set,
703         .pll_sleep = NULL,
704         .request_firmware = philips_tu1216_request_firmware,
705 };
706
707
708
709
710 static u8 read_pwm(struct budget_av *budget_av)
711 {
712         u8 b = 0xff;
713         u8 pwm;
714         struct i2c_msg msg[] = { {.addr = 0x50,.flags = 0,.buf = &b,.len = 1},
715         {.addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1}
716         };
717
718         if ((i2c_transfer(&budget_av->budget.i2c_adap, msg, 2) != 2)
719             || (pwm == 0xff))
720                 pwm = 0x48;
721
722         return pwm;
723 }
724
725 #define SUBID_DVBS_KNC1         0x0010
726 #define SUBID_DVBS_KNC1_PLUS    0x0011
727 #define SUBID_DVBS_TYPHOON      0x4f56
728 #define SUBID_DVBS_CINERGY1200  0x1154
729
730 #define SUBID_DVBC_KNC1         0x0020
731 #define SUBID_DVBC_KNC1_PLUS    0x0021
732 #define SUBID_DVBC_CINERGY1200  0x1156
733
734 #define SUBID_DVBT_KNC1_PLUS    0x0031
735 #define SUBID_DVBT_KNC1         0x0030
736 #define SUBID_DVBT_CINERGY1200  0x1157
737
738 static void frontend_init(struct budget_av *budget_av)
739 {
740         struct saa7146_dev * saa = budget_av->budget.dev;
741         struct dvb_frontend * fe = NULL;
742
743         switch (saa->pci->subsystem_device) {
744                 case SUBID_DVBS_KNC1_PLUS:
745                 case SUBID_DVBC_KNC1_PLUS:
746                 case SUBID_DVBT_KNC1_PLUS:
747                         // Enable / PowerON Frontend
748                         saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTHI);
749                         break;
750         }
751
752         switch (saa->pci->subsystem_device) {
753
754         case SUBID_DVBS_KNC1:
755         case SUBID_DVBS_KNC1_PLUS:
756         case SUBID_DVBS_TYPHOON:
757                 fe = stv0299_attach(&typhoon_config,
758                                     &budget_av->budget.i2c_adap);
759                 break;
760
761         case SUBID_DVBS_CINERGY1200:
762                 fe = stv0299_attach(&cinergy_1200s_config,
763                                     &budget_av->budget.i2c_adap);
764                 break;
765
766         case SUBID_DVBC_KNC1:
767         case SUBID_DVBC_KNC1_PLUS:
768                 fe = tda10021_attach(&philips_cu1216_config,
769                                      &budget_av->budget.i2c_adap,
770                                      read_pwm(budget_av));
771                 break;
772
773         case SUBID_DVBT_KNC1:
774         case SUBID_DVBT_KNC1_PLUS:
775                 fe = tda10046_attach(&philips_tu1216_config,
776                                      &budget_av->budget.i2c_adap);
777                 break;
778
779         case SUBID_DVBC_CINERGY1200:
780                 fe = tda10021_attach(&philips_cu1216_config,
781                                      &budget_av->budget.i2c_adap,
782                                      read_pwm(budget_av));
783                 break;
784
785         case SUBID_DVBT_CINERGY1200:
786                 fe = tda10046_attach(&philips_tu1216_config,
787                                      &budget_av->budget.i2c_adap);
788                 break;
789         }
790
791         if (fe == NULL) {
792                 printk(KERN_ERR "budget-av: A frontend driver was not found "
793                                 "for device %04x/%04x subsystem %04x/%04x\n",
794                        saa->pci->vendor,
795                        saa->pci->device,
796                        saa->pci->subsystem_vendor,
797                        saa->pci->subsystem_device);
798                 return;
799         }
800
801         budget_av->budget.dvb_frontend = fe;
802
803         if (dvb_register_frontend(&budget_av->budget.dvb_adapter,
804                                   budget_av->budget.dvb_frontend)) {
805                 printk(KERN_ERR "budget-av: Frontend registration failed!\n");
806                 if (budget_av->budget.dvb_frontend->ops->release)
807                         budget_av->budget.dvb_frontend->ops->release(budget_av->budget.dvb_frontend);
808                 budget_av->budget.dvb_frontend = NULL;
809         }
810 }
811
812
813 static void budget_av_irq(struct saa7146_dev *dev, u32 * isr)
814 {
815         struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
816
817         dprintk(8, "dev: %p, budget_av: %p\n", dev, budget_av);
818
819         if (*isr & MASK_10)
820                 ttpci_budget_irq10_handler(dev, isr);
821 }
822
823 static int budget_av_detach(struct saa7146_dev *dev)
824 {
825         struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
826         int err;
827
828         dprintk(2, "dev: %p\n", dev);
829
830         if (1 == budget_av->has_saa7113) {
831                 saa7146_setgpio(dev, 0, SAA7146_GPIO_OUTLO);
832
833                 msleep(200);
834
835                 saa7146_unregister_device(&budget_av->vd, dev);
836         }
837
838         if (budget_av->budget.ci_present)
839                 ciintf_deinit(budget_av);
840
841         if (budget_av->budget.dvb_frontend != NULL)
842                 dvb_unregister_frontend(budget_av->budget.dvb_frontend);
843         err = ttpci_budget_deinit(&budget_av->budget);
844
845         kfree(budget_av);
846
847         return err;
848 }
849
850 static struct saa7146_ext_vv vv_data;
851
852 static int budget_av_attach(struct saa7146_dev *dev, struct saa7146_pci_extension_data *info)
853 {
854         struct budget_av *budget_av;
855         u8 *mac;
856         int err;
857
858         dprintk(2, "dev: %p\n", dev);
859
860         if (!(budget_av = kmalloc(sizeof(struct budget_av), GFP_KERNEL)))
861                 return -ENOMEM;
862
863         memset(budget_av, 0, sizeof(struct budget_av));
864
865         budget_av->has_saa7113 = 0;
866         budget_av->budget.ci_present = 0;
867
868         dev->ext_priv = budget_av;
869
870         if ((err = ttpci_budget_init(&budget_av->budget, dev, info, THIS_MODULE))) {
871                 kfree(budget_av);
872                 return err;
873         }
874
875         /* knc1 initialization */
876         saa7146_write(dev, DD1_STREAM_B, 0x04000000);
877         saa7146_write(dev, DD1_INIT, 0x07000600);
878         saa7146_write(dev, MC2, MASK_09 | MASK_25 | MASK_10 | MASK_26);
879
880         if (saa7113_init(budget_av) == 0) {
881                 budget_av->has_saa7113 = 1;
882
883                 if (0 != saa7146_vv_init(dev, &vv_data)) {
884                         /* fixme: proper cleanup here */
885                         ERR(("cannot init vv subsystem.\n"));
886                         return err;
887                 }
888
889                 if ((err = saa7146_register_device(&budget_av->vd, dev, "knc1", VFL_TYPE_GRABBER))) {
890                         /* fixme: proper cleanup here */
891                         ERR(("cannot register capture v4l2 device.\n"));
892                         return err;
893                 }
894
895                 /* beware: this modifies dev->vv ... */
896                 saa7146_set_hps_source_and_sync(dev, SAA7146_HPS_SOURCE_PORT_A,
897                                                 SAA7146_HPS_SYNC_PORT_A);
898
899                 saa7113_setinput(budget_av, 0);
900         } else {
901                 ciintf_init(budget_av);
902         }
903
904         /* fixme: find some sane values here... */
905         saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
906
907         mac = budget_av->budget.dvb_adapter.proposed_mac;
908         if (i2c_readregs(&budget_av->budget.i2c_adap, 0xa0, 0x30, mac, 6)) {
909                 printk(KERN_ERR "KNC1-%d: Could not read MAC from KNC1 card\n",
910                        budget_av->budget.dvb_adapter.num);
911                 memset(mac, 0, 6);
912         } else {
913                 printk(KERN_INFO "KNC1-%d: MAC addr = %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
914                        budget_av->budget.dvb_adapter.num,
915                        mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
916         }
917
918         budget_av->budget.dvb_adapter.priv = budget_av;
919         frontend_init(budget_av);
920
921         return 0;
922 }
923
924 #define KNC1_INPUTS 2
925 static struct v4l2_input knc1_inputs[KNC1_INPUTS] = {
926         {0, "Composite", V4L2_INPUT_TYPE_TUNER, 1, 0, V4L2_STD_PAL_BG | V4L2_STD_NTSC_M, 0},
927         {1, "S-Video", V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG | V4L2_STD_NTSC_M, 0},
928 };
929
930 static struct saa7146_extension_ioctls ioctls[] = {
931         {VIDIOC_ENUMINPUT, SAA7146_EXCLUSIVE},
932         {VIDIOC_G_INPUT, SAA7146_EXCLUSIVE},
933         {VIDIOC_S_INPUT, SAA7146_EXCLUSIVE},
934         {0, 0}
935 };
936
937 static int av_ioctl(struct saa7146_fh *fh, unsigned int cmd, void *arg)
938 {
939         struct saa7146_dev *dev = fh->dev;
940         struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
941
942         switch (cmd) {
943         case VIDIOC_ENUMINPUT:{
944                 struct v4l2_input *i = arg;
945
946                 dprintk(1, "VIDIOC_ENUMINPUT %d.\n", i->index);
947                 if (i->index < 0 || i->index >= KNC1_INPUTS) {
948                         return -EINVAL;
949                 }
950                 memcpy(i, &knc1_inputs[i->index], sizeof(struct v4l2_input));
951                 return 0;
952         }
953         case VIDIOC_G_INPUT:{
954                 int *input = (int *) arg;
955
956                 *input = budget_av->cur_input;
957
958                 dprintk(1, "VIDIOC_G_INPUT %d.\n", *input);
959                 return 0;
960         }
961         case VIDIOC_S_INPUT:{
962                 int input = *(int *) arg;
963                 dprintk(1, "VIDIOC_S_INPUT %d.\n", input);
964                 return saa7113_setinput(budget_av, input);
965         }
966         default:
967                 return -ENOIOCTLCMD;
968         }
969         return 0;
970 }
971
972 static struct saa7146_standard standard[] = {
973         {.name = "PAL",.id = V4L2_STD_PAL,
974          .v_offset = 0x17,.v_field = 288,
975          .h_offset = 0x14,.h_pixels = 680,
976          .v_max_out = 576,.h_max_out = 768 },
977
978         {.name = "NTSC",.id = V4L2_STD_NTSC,
979          .v_offset = 0x16,.v_field = 240,
980          .h_offset = 0x06,.h_pixels = 708,
981          .v_max_out = 480,.h_max_out = 640, },
982 };
983
984 static struct saa7146_ext_vv vv_data = {
985         .inputs = 2,
986         .capabilities = 0,      // perhaps later: V4L2_CAP_VBI_CAPTURE, but that need tweaking with the saa7113
987         .flags = 0,
988         .stds = &standard[0],
989         .num_stds = sizeof(standard) / sizeof(struct saa7146_standard),
990         .ioctls = &ioctls[0],
991         .ioctl = av_ioctl,
992 };
993
994 static struct saa7146_extension budget_extension;
995
996 MAKE_BUDGET_INFO(knc1s, "KNC1 DVB-S", BUDGET_KNC1S);
997 MAKE_BUDGET_INFO(knc1c, "KNC1 DVB-C", BUDGET_KNC1C);
998 MAKE_BUDGET_INFO(knc1t, "KNC1 DVB-T", BUDGET_KNC1T);
999 MAKE_BUDGET_INFO(knc1sp, "KNC1 DVB-S Plus", BUDGET_KNC1SP);
1000 MAKE_BUDGET_INFO(knc1cp, "KNC1 DVB-C Plus", BUDGET_KNC1CP);
1001 MAKE_BUDGET_INFO(knc1tp, "KNC1 DVB-T Plus", BUDGET_KNC1TP);
1002 MAKE_BUDGET_INFO(cin1200s, "TerraTec Cinergy 1200 DVB-S", BUDGET_CIN1200S);
1003 MAKE_BUDGET_INFO(cin1200c, "Terratec Cinergy 1200 DVB-C", BUDGET_CIN1200C);
1004 MAKE_BUDGET_INFO(cin1200t, "Terratec Cinergy 1200 DVB-T", BUDGET_CIN1200T);
1005
1006 static struct pci_device_id pci_tbl[] = {
1007         MAKE_EXTENSION_PCI(knc1s, 0x1131, 0x4f56),
1008         MAKE_EXTENSION_PCI(knc1s, 0x1131, 0x0010),
1009         MAKE_EXTENSION_PCI(knc1sp, 0x1131, 0x0011),
1010         MAKE_EXTENSION_PCI(knc1c, 0x1894, 0x0020),
1011         MAKE_EXTENSION_PCI(knc1cp, 0x1894, 0x0021),
1012         MAKE_EXTENSION_PCI(knc1t, 0x1894, 0x0030),
1013         MAKE_EXTENSION_PCI(knc1tp, 0x1894, 0x0031),
1014         MAKE_EXTENSION_PCI(cin1200s, 0x153b, 0x1154),
1015         MAKE_EXTENSION_PCI(cin1200c, 0x153b, 0x1156),
1016         MAKE_EXTENSION_PCI(cin1200t, 0x153b, 0x1157),
1017         {
1018          .vendor = 0,
1019         }
1020 };
1021
1022 MODULE_DEVICE_TABLE(pci, pci_tbl);
1023
1024 static struct saa7146_extension budget_extension = {
1025         .name = "budget_av",
1026         .pci_tbl = pci_tbl,
1027
1028         .module = THIS_MODULE,
1029         .attach = budget_av_attach,
1030         .detach = budget_av_detach,
1031
1032         .irq_mask = MASK_10,
1033         .irq_func = budget_av_irq,
1034 };
1035
1036 static int __init budget_av_init(void)
1037 {
1038         return saa7146_register_extension(&budget_extension);
1039 }
1040
1041 static void __exit budget_av_exit(void)
1042 {
1043         saa7146_unregister_extension(&budget_extension);
1044 }
1045
1046 module_init(budget_av_init);
1047 module_exit(budget_av_exit);
1048
1049 MODULE_LICENSE("GPL");
1050 MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, Michael Hunold, others");
1051 MODULE_DESCRIPTION("driver for the SAA7146 based so-called "
1052                    "budget PCI DVB w/ analog input and CI-module (e.g. the KNC cards)");