blob: 8bc784ab1972dd122897a8a5367ca954af467967 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
52struct 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
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -070062/* 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/****************************************************************************
70 * INITIALIZATION
71 ****************************************************************************/
72
73static 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
93static 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
107static 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
119static 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
Andrew de Quincey2d0235d2006-01-09 15:25:05 -0200130 result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, address & 0xfff, 1, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 if (result == -ETIMEDOUT)
133 budget_av->slot_status = 0;
134 return result;
135}
136
137static 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
Andrew de Quincey2d0235d2006-01-09 15:25:05 -0200148 result = ttpci_budget_debiwrite(&budget_av->budget, DEBICICAM, address & 0xfff, 1, value, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 if (result == -ETIMEDOUT)
151 budget_av->slot_status = 0;
152 return result;
153}
154
155static 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
173static 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
191static 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;
Andrew de Quincey2d0235d2006-01-09 15:25:05 -0200195 int timeout = 50; // 5 seconds (4.4.6 Ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 if (slot != 0)
198 return -EINVAL;
199
200 dprintk(1, "ciintf_slot_reset\n");
201
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700202 saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700204 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 msleep(100);
215
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700216 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 */
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700220 return -ETIMEDOUT;
221 }
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return 0;
224}
225
226static 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
241static 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
255static 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;
Andrew de Quincey2d0235d2006-01-09 15:25:05 -0200259 int cam_present = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 if (slot != 0)
262 return -EINVAL;
263
Andrew de Quincey2d0235d2006-01-09 15:25:05 -0200264 if (!budget_av->slot_status)
265 {
266 // first of all test the card detect line
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
268 udelay(1);
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700269 if (saa7146_read(saa, PSR) & MASK_06)
270 {
Andrew de Quincey2d0235d2006-01-09 15:25:05 -0200271 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) {
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700287 printk(KERN_INFO "budget-av: cam inserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 budget_av->slot_status = 1;
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 } 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)
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700293 {
294 printk(KERN_INFO "budget-av: cam ejected\n");
295 saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 budget_av->slot_status = 0;
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
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
306static 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
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700313 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO);
314 saa7146_setgpio(saa, 1, SAA7146_GPIO_OUTLO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTLO);
316 saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /* 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;
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700332
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700333 if ((result = dvb_ca_en50221_init(&budget_av->budget.dvb_adapter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 &budget_av->ca, 0, 1)) != 0) {
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700335 printk(KERN_ERR "budget-av: ci initialisation failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 goto error;
337 }
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700338
339 printk(KERN_INFO "budget-av: ci interface initialised.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 budget_av->budget.ci_present = 1;
341 return 0;
342
343error:
344 saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16));
345 return result;
346}
347
348static 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
365static 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
394static int saa7113_init(struct budget_av *budget_av)
395{
396 struct budget *budget = &budget_av->budget;
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700397 struct saa7146_dev *saa = budget->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 const u8 *data = saa7113_tab;
399
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700400 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI);
401 msleep(200);
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 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
420static 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
441static 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
471static int philips_su1278_ty_ci_pll_set(struct dvb_frontend *fe,
Andreas Oberrittercfbfce12005-09-09 13:02:30 -0700472 struct i2c_adapter *i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 struct dvb_frontend_parameters *params)
474{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 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
Andreas Oberrittercfbfce12005-09-09 13:02:30 -0700500 if (i2c_transfer(i2c, &msg, 1) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return -EIO;
502 return 0;
503}
504
505static u8 typhoon_cinergy1200s_inittab[] = {
506 0x01, 0x15,
507 0x02, 0x30,
508 0x03, 0x00,
509 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
510 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
511 0x06, 0x40, /* DAC not used, set to high impendance mode */
512 0x07, 0x00, /* DAC LSB */
513 0x08, 0x40, /* DiSEqC off */
514 0x09, 0x00, /* FIFO */
515 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
516 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
517 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
518 0x10, 0x3f, // AGC2 0x3d
519 0x11, 0x84,
Oliver Endrissff29d062005-11-08 21:35:43 -0800520 0x12, 0xb9,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 0x15, 0xc9, // lock detector threshold
522 0x16, 0x00,
523 0x17, 0x00,
524 0x18, 0x00,
525 0x19, 0x00,
526 0x1a, 0x00,
527 0x1f, 0x50,
528 0x20, 0x00,
529 0x21, 0x00,
530 0x22, 0x00,
531 0x23, 0x00,
532 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
533 0x29, 0x1e, // 1/2 threshold
534 0x2a, 0x14, // 2/3 threshold
535 0x2b, 0x0f, // 3/4 threshold
536 0x2c, 0x09, // 5/6 threshold
537 0x2d, 0x05, // 7/8 threshold
538 0x2e, 0x01,
539 0x31, 0x1f, // test all FECs
540 0x32, 0x19, // viterbi and synchro search
541 0x33, 0xfc, // rs control
542 0x34, 0x93, // error control
543 0x0f, 0x92,
544 0xff, 0xff
545};
546
547static struct stv0299_config typhoon_config = {
548 .demod_address = 0x68,
549 .inittab = typhoon_cinergy1200s_inittab,
550 .mclk = 88000000UL,
551 .invert = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 .skip_reinit = 0,
553 .lock_output = STV0229_LOCKOUTPUT_1,
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
561static struct stv0299_config cinergy_1200s_config = {
562 .demod_address = 0x68,
563 .inittab = typhoon_cinergy1200s_inittab,
564 .mclk = 88000000UL,
565 .invert = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 .skip_reinit = 0,
567 .lock_output = STV0229_LOCKOUTPUT_0,
568 .volt13_op0_op1 = STV0299_VOLT13_OP0,
569 .min_delay_ms = 100,
570 .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
571 .pll_set = philips_su1278_ty_ci_pll_set,
572};
573
574
575static int philips_cu1216_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
576{
577 struct budget *budget = (struct budget *) fe->dvb->priv;
578 u8 buf[4];
579 struct i2c_msg msg = {.addr = 0x60,.flags = 0,.buf = buf,.len = sizeof(buf) };
580
581#define TUNER_MUL 62500
582
583 u32 div = (params->frequency + 36125000 + TUNER_MUL / 2) / TUNER_MUL;
584
585 buf[0] = (div >> 8) & 0x7f;
586 buf[1] = div & 0xff;
Johannes Stezenbacheef57642005-07-07 17:57:58 -0700587 buf[2] = 0x86;
588 buf[3] = (params->frequency < 150000000 ? 0x01 :
589 params->frequency < 445000000 ? 0x02 : 0x04);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 if (i2c_transfer(&budget->i2c_adap, &msg, 1) != 1)
592 return -EIO;
593 return 0;
594}
595
596static struct tda10021_config philips_cu1216_config = {
597 .demod_address = 0x0c,
598 .pll_set = philips_cu1216_pll_set,
599};
600
601
602
603
604static int philips_tu1216_pll_init(struct dvb_frontend *fe)
605{
606 struct budget *budget = (struct budget *) fe->dvb->priv;
607 static u8 tu1216_init[] = { 0x0b, 0xf5, 0x85, 0xab };
608 struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tu1216_init,.len = sizeof(tu1216_init) };
609
610 // setup PLL configuration
611 if (i2c_transfer(&budget->i2c_adap, &tuner_msg, 1) != 1)
612 return -EIO;
613 msleep(1);
614
615 return 0;
616}
617
618static int philips_tu1216_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
619{
620 struct budget *budget = (struct budget *) fe->dvb->priv;
621 u8 tuner_buf[4];
622 struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tuner_buf,.len =
623 sizeof(tuner_buf) };
624 int tuner_frequency = 0;
625 u8 band, cp, filter;
626
627 // determine charge pump
628 tuner_frequency = params->frequency + 36166000;
629 if (tuner_frequency < 87000000)
630 return -EINVAL;
631 else if (tuner_frequency < 130000000)
632 cp = 3;
633 else if (tuner_frequency < 160000000)
634 cp = 5;
635 else if (tuner_frequency < 200000000)
636 cp = 6;
637 else if (tuner_frequency < 290000000)
638 cp = 3;
639 else if (tuner_frequency < 420000000)
640 cp = 5;
641 else if (tuner_frequency < 480000000)
642 cp = 6;
643 else if (tuner_frequency < 620000000)
644 cp = 3;
645 else if (tuner_frequency < 830000000)
646 cp = 5;
647 else if (tuner_frequency < 895000000)
648 cp = 7;
649 else
650 return -EINVAL;
651
652 // determine band
653 if (params->frequency < 49000000)
654 return -EINVAL;
655 else if (params->frequency < 161000000)
656 band = 1;
657 else if (params->frequency < 444000000)
658 band = 2;
659 else if (params->frequency < 861000000)
660 band = 4;
661 else
662 return -EINVAL;
663
664 // setup PLL filter
665 switch (params->u.ofdm.bandwidth) {
666 case BANDWIDTH_6_MHZ:
667 filter = 0;
668 break;
669
670 case BANDWIDTH_7_MHZ:
671 filter = 0;
672 break;
673
674 case BANDWIDTH_8_MHZ:
675 filter = 1;
676 break;
677
678 default:
679 return -EINVAL;
680 }
681
682 // calculate divisor
683 // ((36166000+((1000000/6)/2)) + Finput)/(1000000/6)
684 tuner_frequency = (((params->frequency / 1000) * 6) + 217496) / 1000;
685
686 // setup tuner buffer
687 tuner_buf[0] = (tuner_frequency >> 8) & 0x7f;
688 tuner_buf[1] = tuner_frequency & 0xff;
689 tuner_buf[2] = 0xca;
690 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
691
692 if (i2c_transfer(&budget->i2c_adap, &tuner_msg, 1) != 1)
693 return -EIO;
694
695 msleep(1);
696 return 0;
697}
698
699static int philips_tu1216_request_firmware(struct dvb_frontend *fe,
700 const struct firmware **fw, char *name)
701{
702 struct budget *budget = (struct budget *) fe->dvb->priv;
703
704 return request_firmware(fw, name, &budget->dev->pci->dev);
705}
706
707static struct tda1004x_config philips_tu1216_config = {
708
709 .demod_address = 0x8,
710 .invert = 1,
711 .invert_oclk = 1,
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700712 .xtal_freq = TDA10046_XTAL_4M,
713 .agc_config = TDA10046_AGC_DEFAULT,
714 .if_freq = TDA10046_FREQ_3617,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 .pll_init = philips_tu1216_pll_init,
716 .pll_set = philips_tu1216_pll_set,
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700717 .pll_sleep = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 .request_firmware = philips_tu1216_request_firmware,
719};
720
721
722
723
724static u8 read_pwm(struct budget_av *budget_av)
725{
726 u8 b = 0xff;
727 u8 pwm;
728 struct i2c_msg msg[] = { {.addr = 0x50,.flags = 0,.buf = &b,.len = 1},
729 {.addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1}
730 };
731
732 if ((i2c_transfer(&budget_av->budget.i2c_adap, msg, 2) != 2)
733 || (pwm == 0xff))
734 pwm = 0x48;
735
736 return pwm;
737}
738
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700739#define SUBID_DVBS_KNC1 0x0010
740#define SUBID_DVBS_KNC1_PLUS 0x0011
741#define SUBID_DVBS_TYPHOON 0x4f56
742#define SUBID_DVBS_CINERGY1200 0x1154
743
744#define SUBID_DVBC_KNC1 0x0020
745#define SUBID_DVBC_KNC1_PLUS 0x0021
746#define SUBID_DVBC_CINERGY1200 0x1156
747
748#define SUBID_DVBT_KNC1_PLUS 0x0031
749#define SUBID_DVBT_KNC1 0x0030
750#define SUBID_DVBT_CINERGY1200 0x1157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752static void frontend_init(struct budget_av *budget_av)
753{
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700754 struct saa7146_dev * saa = budget_av->budget.dev;
755 struct dvb_frontend * fe = NULL;
756
757 switch (saa->pci->subsystem_device) {
758 case SUBID_DVBS_KNC1_PLUS:
759 case SUBID_DVBC_KNC1_PLUS:
760 case SUBID_DVBT_KNC1_PLUS:
761 // Enable / PowerON Frontend
Andrew de Quincey87b2eca2005-09-09 13:03:09 -0700762 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO);
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700763 saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTHI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 break;
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700765 }
766
767 switch (saa->pci->subsystem_device) {
768
769 case SUBID_DVBS_KNC1:
770 case SUBID_DVBS_KNC1_PLUS:
771 case SUBID_DVBS_TYPHOON:
772 fe = stv0299_attach(&typhoon_config,
773 &budget_av->budget.i2c_adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 break;
775
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700776 case SUBID_DVBS_CINERGY1200:
777 fe = stv0299_attach(&cinergy_1200s_config,
778 &budget_av->budget.i2c_adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 break;
780
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700781 case SUBID_DVBC_KNC1:
782 case SUBID_DVBC_KNC1_PLUS:
783 fe = tda10021_attach(&philips_cu1216_config,
784 &budget_av->budget.i2c_adap,
785 read_pwm(budget_av));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 break;
787
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700788 case SUBID_DVBT_KNC1:
789 case SUBID_DVBT_KNC1_PLUS:
790 fe = tda10046_attach(&philips_tu1216_config,
791 &budget_av->budget.i2c_adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 break;
793
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700794 case SUBID_DVBC_CINERGY1200:
795 fe = tda10021_attach(&philips_cu1216_config,
796 &budget_av->budget.i2c_adap,
797 read_pwm(budget_av));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 break;
799
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700800 case SUBID_DVBT_CINERGY1200:
801 fe = tda10046_attach(&philips_tu1216_config,
802 &budget_av->budget.i2c_adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 break;
804 }
805
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700806 if (fe == NULL) {
807 printk(KERN_ERR "budget-av: A frontend driver was not found "
808 "for device %04x/%04x subsystem %04x/%04x\n",
809 saa->pci->vendor,
810 saa->pci->device,
811 saa->pci->subsystem_vendor,
812 saa->pci->subsystem_device);
813 return;
814 }
815
816 budget_av->budget.dvb_frontend = fe;
817
818 if (dvb_register_frontend(&budget_av->budget.dvb_adapter,
819 budget_av->budget.dvb_frontend)) {
820 printk(KERN_ERR "budget-av: Frontend registration failed!\n");
821 if (budget_av->budget.dvb_frontend->ops->release)
822 budget_av->budget.dvb_frontend->ops->release(budget_av->budget.dvb_frontend);
823 budget_av->budget.dvb_frontend = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825}
826
827
828static void budget_av_irq(struct saa7146_dev *dev, u32 * isr)
829{
830 struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
831
832 dprintk(8, "dev: %p, budget_av: %p\n", dev, budget_av);
833
834 if (*isr & MASK_10)
835 ttpci_budget_irq10_handler(dev, isr);
836}
837
838static int budget_av_detach(struct saa7146_dev *dev)
839{
840 struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
841 int err;
842
843 dprintk(2, "dev: %p\n", dev);
844
845 if (1 == budget_av->has_saa7113) {
846 saa7146_setgpio(dev, 0, SAA7146_GPIO_OUTLO);
847
848 msleep(200);
849
850 saa7146_unregister_device(&budget_av->vd, dev);
851 }
852
853 if (budget_av->budget.ci_present)
854 ciintf_deinit(budget_av);
855
856 if (budget_av->budget.dvb_frontend != NULL)
857 dvb_unregister_frontend(budget_av->budget.dvb_frontend);
858 err = ttpci_budget_deinit(&budget_av->budget);
859
860 kfree(budget_av);
861
862 return err;
863}
864
865static struct saa7146_ext_vv vv_data;
866
867static int budget_av_attach(struct saa7146_dev *dev, struct saa7146_pci_extension_data *info)
868{
869 struct budget_av *budget_av;
870 u8 *mac;
871 int err;
872
873 dprintk(2, "dev: %p\n", dev);
874
875 if (!(budget_av = kmalloc(sizeof(struct budget_av), GFP_KERNEL)))
876 return -ENOMEM;
877
878 memset(budget_av, 0, sizeof(struct budget_av));
879
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700880 budget_av->has_saa7113 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 budget_av->budget.ci_present = 0;
882
883 dev->ext_priv = budget_av;
884
885 if ((err = ttpci_budget_init(&budget_av->budget, dev, info, THIS_MODULE))) {
886 kfree(budget_av);
887 return err;
888 }
889
890 /* knc1 initialization */
891 saa7146_write(dev, DD1_STREAM_B, 0x04000000);
892 saa7146_write(dev, DD1_INIT, 0x07000600);
893 saa7146_write(dev, MC2, MASK_09 | MASK_25 | MASK_10 | MASK_26);
894
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700895 if (saa7113_init(budget_av) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 budget_av->has_saa7113 = 1;
897
898 if (0 != saa7146_vv_init(dev, &vv_data)) {
899 /* fixme: proper cleanup here */
900 ERR(("cannot init vv subsystem.\n"));
901 return err;
902 }
903
904 if ((err = saa7146_register_device(&budget_av->vd, dev, "knc1", VFL_TYPE_GRABBER))) {
905 /* fixme: proper cleanup here */
906 ERR(("cannot register capture v4l2 device.\n"));
907 return err;
908 }
909
910 /* beware: this modifies dev->vv ... */
911 saa7146_set_hps_source_and_sync(dev, SAA7146_HPS_SOURCE_PORT_A,
912 SAA7146_HPS_SYNC_PORT_A);
913
914 saa7113_setinput(budget_av, 0);
915 } else {
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700916 ciintf_init(budget_av);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918
919 /* fixme: find some sane values here... */
920 saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
921
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700922 mac = budget_av->budget.dvb_adapter.proposed_mac;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (i2c_readregs(&budget_av->budget.i2c_adap, 0xa0, 0x30, mac, 6)) {
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700924 printk(KERN_ERR "KNC1-%d: Could not read MAC from KNC1 card\n",
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700925 budget_av->budget.dvb_adapter.num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 memset(mac, 0, 6);
927 } else {
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700928 printk(KERN_INFO "KNC1-%d: MAC addr = %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700929 budget_av->budget.dvb_adapter.num,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
931 }
932
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700933 budget_av->budget.dvb_adapter.priv = budget_av;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 frontend_init(budget_av);
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return 0;
937}
938
939#define KNC1_INPUTS 2
940static struct v4l2_input knc1_inputs[KNC1_INPUTS] = {
941 {0, "Composite", V4L2_INPUT_TYPE_TUNER, 1, 0, V4L2_STD_PAL_BG | V4L2_STD_NTSC_M, 0},
942 {1, "S-Video", V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG | V4L2_STD_NTSC_M, 0},
943};
944
945static struct saa7146_extension_ioctls ioctls[] = {
946 {VIDIOC_ENUMINPUT, SAA7146_EXCLUSIVE},
947 {VIDIOC_G_INPUT, SAA7146_EXCLUSIVE},
948 {VIDIOC_S_INPUT, SAA7146_EXCLUSIVE},
949 {0, 0}
950};
951
952static int av_ioctl(struct saa7146_fh *fh, unsigned int cmd, void *arg)
953{
954 struct saa7146_dev *dev = fh->dev;
955 struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
956
957 switch (cmd) {
958 case VIDIOC_ENUMINPUT:{
959 struct v4l2_input *i = arg;
960
961 dprintk(1, "VIDIOC_ENUMINPUT %d.\n", i->index);
962 if (i->index < 0 || i->index >= KNC1_INPUTS) {
963 return -EINVAL;
964 }
965 memcpy(i, &knc1_inputs[i->index], sizeof(struct v4l2_input));
966 return 0;
967 }
968 case VIDIOC_G_INPUT:{
969 int *input = (int *) arg;
970
971 *input = budget_av->cur_input;
972
973 dprintk(1, "VIDIOC_G_INPUT %d.\n", *input);
974 return 0;
975 }
976 case VIDIOC_S_INPUT:{
977 int input = *(int *) arg;
978 dprintk(1, "VIDIOC_S_INPUT %d.\n", input);
979 return saa7113_setinput(budget_av, input);
980 }
981 default:
982 return -ENOIOCTLCMD;
983 }
984 return 0;
985}
986
987static struct saa7146_standard standard[] = {
988 {.name = "PAL",.id = V4L2_STD_PAL,
989 .v_offset = 0x17,.v_field = 288,
990 .h_offset = 0x14,.h_pixels = 680,
991 .v_max_out = 576,.h_max_out = 768 },
992
993 {.name = "NTSC",.id = V4L2_STD_NTSC,
994 .v_offset = 0x16,.v_field = 240,
995 .h_offset = 0x06,.h_pixels = 708,
996 .v_max_out = 480,.h_max_out = 640, },
997};
998
999static struct saa7146_ext_vv vv_data = {
1000 .inputs = 2,
1001 .capabilities = 0, // perhaps later: V4L2_CAP_VBI_CAPTURE, but that need tweaking with the saa7113
1002 .flags = 0,
1003 .stds = &standard[0],
1004 .num_stds = sizeof(standard) / sizeof(struct saa7146_standard),
1005 .ioctls = &ioctls[0],
1006 .ioctl = av_ioctl,
1007};
1008
1009static struct saa7146_extension budget_extension;
1010
1011MAKE_BUDGET_INFO(knc1s, "KNC1 DVB-S", BUDGET_KNC1S);
1012MAKE_BUDGET_INFO(knc1c, "KNC1 DVB-C", BUDGET_KNC1C);
1013MAKE_BUDGET_INFO(knc1t, "KNC1 DVB-T", BUDGET_KNC1T);
Johannes Stezenbach2d4f2c22005-05-16 21:54:23 -07001014MAKE_BUDGET_INFO(knc1sp, "KNC1 DVB-S Plus", BUDGET_KNC1SP);
1015MAKE_BUDGET_INFO(knc1cp, "KNC1 DVB-C Plus", BUDGET_KNC1CP);
1016MAKE_BUDGET_INFO(knc1tp, "KNC1 DVB-T Plus", BUDGET_KNC1TP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017MAKE_BUDGET_INFO(cin1200s, "TerraTec Cinergy 1200 DVB-S", BUDGET_CIN1200S);
1018MAKE_BUDGET_INFO(cin1200c, "Terratec Cinergy 1200 DVB-C", BUDGET_CIN1200C);
1019MAKE_BUDGET_INFO(cin1200t, "Terratec Cinergy 1200 DVB-T", BUDGET_CIN1200T);
1020
1021static struct pci_device_id pci_tbl[] = {
1022 MAKE_EXTENSION_PCI(knc1s, 0x1131, 0x4f56),
Johannes Stezenbach2d4f2c22005-05-16 21:54:23 -07001023 MAKE_EXTENSION_PCI(knc1s, 0x1131, 0x0010),
1024 MAKE_EXTENSION_PCI(knc1sp, 0x1131, 0x0011),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 MAKE_EXTENSION_PCI(knc1c, 0x1894, 0x0020),
Johannes Stezenbach2d4f2c22005-05-16 21:54:23 -07001026 MAKE_EXTENSION_PCI(knc1cp, 0x1894, 0x0021),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 MAKE_EXTENSION_PCI(knc1t, 0x1894, 0x0030),
Johannes Stezenbach2d4f2c22005-05-16 21:54:23 -07001028 MAKE_EXTENSION_PCI(knc1tp, 0x1894, 0x0031),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 MAKE_EXTENSION_PCI(cin1200s, 0x153b, 0x1154),
1030 MAKE_EXTENSION_PCI(cin1200c, 0x153b, 0x1156),
1031 MAKE_EXTENSION_PCI(cin1200t, 0x153b, 0x1157),
1032 {
1033 .vendor = 0,
1034 }
1035};
1036
1037MODULE_DEVICE_TABLE(pci, pci_tbl);
1038
1039static struct saa7146_extension budget_extension = {
Julian Scheel27b05fd2005-07-12 13:58:39 -07001040 .name = "budget_av",
Oliver Endriss69459f32005-12-01 00:51:48 -08001041 .flags = SAA7146_I2C_SHORT_DELAY,
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 .pci_tbl = pci_tbl,
1044
1045 .module = THIS_MODULE,
1046 .attach = budget_av_attach,
1047 .detach = budget_av_detach,
1048
1049 .irq_mask = MASK_10,
1050 .irq_func = budget_av_irq,
1051};
1052
1053static int __init budget_av_init(void)
1054{
1055 return saa7146_register_extension(&budget_extension);
1056}
1057
1058static void __exit budget_av_exit(void)
1059{
1060 saa7146_unregister_extension(&budget_extension);
1061}
1062
1063module_init(budget_av_init);
1064module_exit(budget_av_exit);
1065
1066MODULE_LICENSE("GPL");
1067MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, Michael Hunold, others");
1068MODULE_DESCRIPTION("driver for the SAA7146 based so-called "
1069 "budget PCI DVB w/ analog input and CI-module (e.g. the KNC cards)");