blob: 4d1954072bb4be048bb948306c8bfcd95e8a5e40 [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"
Andrew de Quincey1c72cfdc2006-09-05 17:58:20 -030040#include "tua6100.h"
Regis Prevotf8bf1342006-01-11 23:31:53 -020041#include "dvb-pll.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <media/saa7146_vv.h>
43#include <linux/module.h>
44#include <linux/errno.h>
45#include <linux/slab.h>
46#include <linux/interrupt.h>
47#include <linux/input.h>
48#include <linux/spinlock.h>
49
50#include "dvb_ca_en50221.h"
51
52#define DEBICICAM 0x02420000
53
Andrew de Quincey5c1208b2006-05-22 10:32:02 -030054#define SLOTSTATUS_NONE 1
55#define SLOTSTATUS_PRESENT 2
56#define SLOTSTATUS_RESET 4
57#define SLOTSTATUS_READY 8
58#define SLOTSTATUS_OCCUPIED (SLOTSTATUS_PRESENT|SLOTSTATUS_RESET|SLOTSTATUS_READY)
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060struct budget_av {
61 struct budget budget;
62 struct video_device *vd;
63 int cur_input;
64 int has_saa7113;
65 struct tasklet_struct ciintf_irq_tasklet;
66 int slot_status;
67 struct dvb_ca_en50221 ca;
Andrew de Quincey5c1208b2006-05-22 10:32:02 -030068 u8 reinitialise_demod:1;
69 u8 tda10021_poclkp:1;
70 u8 tda10021_ts_enabled;
71 int (*tda10021_set_frontend)(struct dvb_frontend *fe, struct dvb_frontend_parameters *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
Andrew de Quincey5c1208b2006-05-22 10:32:02 -030074static int ciintf_slot_shutdown(struct dvb_ca_en50221 *ca, int slot);
75
76
Andrew de Quincey86f40cc2006-03-30 15:53:35 -030077/* GPIO Connections:
78 * 0 - Vcc/Reset (Reset is controlled by capacitor). Resets the frontend *AS WELL*!
79 * 1 - CI memory select 0=>IO memory, 1=>Attribute Memory
80 * 2 - CI Card Enable (Active Low)
81 * 3 - CI Card Detect
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -070082 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/****************************************************************************
85 * INITIALIZATION
86 ****************************************************************************/
87
88static u8 i2c_readreg(struct i2c_adapter *i2c, u8 id, u8 reg)
89{
90 u8 mm1[] = { 0x00 };
91 u8 mm2[] = { 0x00 };
92 struct i2c_msg msgs[2];
93
94 msgs[0].flags = 0;
95 msgs[1].flags = I2C_M_RD;
96 msgs[0].addr = msgs[1].addr = id / 2;
97 mm1[0] = reg;
98 msgs[0].len = 1;
99 msgs[1].len = 1;
100 msgs[0].buf = mm1;
101 msgs[1].buf = mm2;
102
103 i2c_transfer(i2c, msgs, 2);
104
105 return mm2[0];
106}
107
108static int i2c_readregs(struct i2c_adapter *i2c, u8 id, u8 reg, u8 * buf, u8 len)
109{
110 u8 mm1[] = { reg };
111 struct i2c_msg msgs[2] = {
112 {.addr = id / 2,.flags = 0,.buf = mm1,.len = 1},
113 {.addr = id / 2,.flags = I2C_M_RD,.buf = buf,.len = len}
114 };
115
116 if (i2c_transfer(i2c, msgs, 2) != 2)
117 return -EIO;
118
119 return 0;
120}
121
122static int i2c_writereg(struct i2c_adapter *i2c, u8 id, u8 reg, u8 val)
123{
124 u8 msg[2] = { reg, val };
125 struct i2c_msg msgs;
126
127 msgs.flags = 0;
128 msgs.addr = id / 2;
129 msgs.len = 2;
130 msgs.buf = msg;
131 return i2c_transfer(i2c, &msgs, 1);
132}
133
134static int ciintf_read_attribute_mem(struct dvb_ca_en50221 *ca, int slot, int address)
135{
136 struct budget_av *budget_av = (struct budget_av *) ca->data;
137 int result;
138
139 if (slot != 0)
140 return -EINVAL;
141
142 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTHI);
143 udelay(1);
144
Andrew de Quincey2d0235d2006-01-09 15:25:05 -0200145 result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, address & 0xfff, 1, 0, 1);
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300146 if (result == -ETIMEDOUT) {
147 ciintf_slot_shutdown(ca, slot);
148 printk(KERN_INFO "budget-av: cam ejected 1\n");
149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return result;
151}
152
153static int ciintf_write_attribute_mem(struct dvb_ca_en50221 *ca, int slot, int address, u8 value)
154{
155 struct budget_av *budget_av = (struct budget_av *) ca->data;
156 int result;
157
158 if (slot != 0)
159 return -EINVAL;
160
161 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTHI);
162 udelay(1);
163
Andrew de Quincey2d0235d2006-01-09 15:25:05 -0200164 result = ttpci_budget_debiwrite(&budget_av->budget, DEBICICAM, address & 0xfff, 1, value, 0, 1);
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300165 if (result == -ETIMEDOUT) {
166 ciintf_slot_shutdown(ca, slot);
167 printk(KERN_INFO "budget-av: cam ejected 2\n");
168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return result;
170}
171
172static int ciintf_read_cam_control(struct dvb_ca_en50221 *ca, int slot, u8 address)
173{
174 struct budget_av *budget_av = (struct budget_av *) ca->data;
175 int result;
176
177 if (slot != 0)
178 return -EINVAL;
179
180 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
181 udelay(1);
182
183 result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, address & 3, 1, 0, 0);
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300184 if ((result == -ETIMEDOUT) || ((result == 0xff) && ((address & 3) < 2))) {
185 ciintf_slot_shutdown(ca, slot);
186 printk(KERN_INFO "budget-av: cam ejected 3\n");
187 return -ETIMEDOUT;
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return result;
190}
191
192static int ciintf_write_cam_control(struct dvb_ca_en50221 *ca, int slot, u8 address, u8 value)
193{
194 struct budget_av *budget_av = (struct budget_av *) ca->data;
195 int result;
196
197 if (slot != 0)
198 return -EINVAL;
199
200 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
201 udelay(1);
202
203 result = ttpci_budget_debiwrite(&budget_av->budget, DEBICICAM, address & 3, 1, value, 0, 0);
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300204 if (result == -ETIMEDOUT) {
205 ciintf_slot_shutdown(ca, slot);
206 printk(KERN_INFO "budget-av: cam ejected 5\n");
207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return result;
209}
210
211static int ciintf_slot_reset(struct dvb_ca_en50221 *ca, int slot)
212{
213 struct budget_av *budget_av = (struct budget_av *) ca->data;
214 struct saa7146_dev *saa = budget_av->budget.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 if (slot != 0)
217 return -EINVAL;
218
219 dprintk(1, "ciintf_slot_reset\n");
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300220 budget_av->slot_status = SLOTSTATUS_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700222 saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700224 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI); /* Vcc off */
225 msleep(2);
226 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO); /* Vcc on */
227 msleep(20); /* 20 ms Vcc settling time */
228
229 saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTLO); /* enable card */
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300230 ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTB);
231 msleep(20);
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700232
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300233 /* reinitialise the frontend if necessary */
234 if (budget_av->reinitialise_demod)
235 dvb_frontend_reinitialise(budget_av->budget.dvb_frontend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300237 /* set tda10021 back to original clock configuration on reset */
238 if (budget_av->tda10021_poclkp) {
Andrew de Quincey2bfe0312006-08-08 09:10:08 -0300239 tda10021_writereg(budget_av->budget.dvb_frontend, 0x12, 0xa0);
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300240 budget_av->tda10021_ts_enabled = 0;
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700241 }
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return 0;
244}
245
246static int ciintf_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
247{
248 struct budget_av *budget_av = (struct budget_av *) ca->data;
249 struct saa7146_dev *saa = budget_av->budget.dev;
250
251 if (slot != 0)
252 return -EINVAL;
253
254 dprintk(1, "ciintf_slot_shutdown\n");
255
256 ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTB);
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300257 budget_av->slot_status = SLOTSTATUS_NONE;
258
259 /* set tda10021 back to original clock configuration when cam removed */
260 if (budget_av->tda10021_poclkp) {
Andrew de Quincey2bfe0312006-08-08 09:10:08 -0300261 tda10021_writereg(budget_av->budget.dvb_frontend, 0x12, 0xa0);
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300262 budget_av->tda10021_ts_enabled = 0;
263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return 0;
265}
266
267static int ciintf_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
268{
269 struct budget_av *budget_av = (struct budget_av *) ca->data;
270 struct saa7146_dev *saa = budget_av->budget.dev;
271
272 if (slot != 0)
273 return -EINVAL;
274
275 dprintk(1, "ciintf_slot_ts_enable: %d\n", budget_av->slot_status);
276
277 ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTA);
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300278
279 /* tda10021 seems to need a different TS clock config when data is routed to the CAM */
280 if (budget_av->tda10021_poclkp) {
Andrew de Quincey2bfe0312006-08-08 09:10:08 -0300281 tda10021_writereg(budget_av->budget.dvb_frontend, 0x12, 0xa1);
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300282 budget_av->tda10021_ts_enabled = 1;
283 }
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return 0;
286}
287
288static int ciintf_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
289{
290 struct budget_av *budget_av = (struct budget_av *) ca->data;
291 struct saa7146_dev *saa = budget_av->budget.dev;
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300292 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 if (slot != 0)
295 return -EINVAL;
296
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300297 /* test the card detect line - needs to be done carefully
298 * since it never goes high for some CAMs on this interface (e.g. topuptv) */
299 if (budget_av->slot_status == SLOTSTATUS_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
301 udelay(1);
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300302 if (saa7146_read(saa, PSR) & MASK_06) {
303 if (budget_av->slot_status == SLOTSTATUS_NONE) {
304 budget_av->slot_status = SLOTSTATUS_PRESENT;
305 printk(KERN_INFO "budget-av: cam inserted A\n");
Andrew de Quincey2d0235d2006-01-09 15:25:05 -0200306 }
307 }
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300308 saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
309 }
Andrew de Quincey2d0235d2006-01-09 15:25:05 -0200310
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300311 /* We also try and read from IO memory to work round the above detection bug. If
312 * there is no CAM, we will get a timeout. Only done if there is no cam
313 * present, since this test actually breaks some cams :(
314 *
315 * if the CI interface is not open, we also do the above test since we
316 * don't care if the cam has problems - we'll be resetting it on open() anyway */
317 if ((budget_av->slot_status == SLOTSTATUS_NONE) || (!open)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300319 result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, 0, 1, 0, 1);
320 if ((result >= 0) && (budget_av->slot_status == SLOTSTATUS_NONE)) {
321 budget_av->slot_status = SLOTSTATUS_PRESENT;
322 printk(KERN_INFO "budget-av: cam inserted B\n");
323 } else if (result < 0) {
324 if (budget_av->slot_status != SLOTSTATUS_NONE) {
325 ciintf_slot_shutdown(ca, slot);
326 printk(KERN_INFO "budget-av: cam ejected 5\n");
327 return 0;
328 }
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300332 /* read from attribute memory in reset/ready state to know when the CAM is ready */
333 if (budget_av->slot_status == SLOTSTATUS_RESET) {
334 result = ciintf_read_attribute_mem(ca, slot, 0);
335 if (result == 0x1d) {
336 budget_av->slot_status = SLOTSTATUS_READY;
337 }
338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300340 /* work out correct return code */
341 if (budget_av->slot_status != SLOTSTATUS_NONE) {
342 if (budget_av->slot_status & SLOTSTATUS_READY) {
343 return DVB_CA_EN50221_POLL_CAM_PRESENT | DVB_CA_EN50221_POLL_CAM_READY;
344 }
345 return DVB_CA_EN50221_POLL_CAM_PRESENT;
346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return 0;
348}
349
350static int ciintf_init(struct budget_av *budget_av)
351{
352 struct saa7146_dev *saa = budget_av->budget.dev;
353 int result;
354
355 memset(&budget_av->ca, 0, sizeof(struct dvb_ca_en50221));
356
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700357 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO);
358 saa7146_setgpio(saa, 1, SAA7146_GPIO_OUTLO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTLO);
360 saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /* Enable DEBI pins */
363 saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16) | 0x800);
364
365 /* register CI interface */
366 budget_av->ca.owner = THIS_MODULE;
367 budget_av->ca.read_attribute_mem = ciintf_read_attribute_mem;
368 budget_av->ca.write_attribute_mem = ciintf_write_attribute_mem;
369 budget_av->ca.read_cam_control = ciintf_read_cam_control;
370 budget_av->ca.write_cam_control = ciintf_write_cam_control;
371 budget_av->ca.slot_reset = ciintf_slot_reset;
372 budget_av->ca.slot_shutdown = ciintf_slot_shutdown;
373 budget_av->ca.slot_ts_enable = ciintf_slot_ts_enable;
374 budget_av->ca.poll_slot_status = ciintf_poll_slot_status;
375 budget_av->ca.data = budget_av;
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300376 budget_av->budget.ci_present = 1;
377 budget_av->slot_status = SLOTSTATUS_NONE;
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700378
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700379 if ((result = dvb_ca_en50221_init(&budget_av->budget.dvb_adapter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 &budget_av->ca, 0, 1)) != 0) {
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700381 printk(KERN_ERR "budget-av: ci initialisation failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 goto error;
383 }
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700384
385 printk(KERN_INFO "budget-av: ci interface initialised.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return 0;
387
388error:
389 saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16));
390 return result;
391}
392
393static void ciintf_deinit(struct budget_av *budget_av)
394{
395 struct saa7146_dev *saa = budget_av->budget.dev;
396
397 saa7146_setgpio(saa, 0, SAA7146_GPIO_INPUT);
398 saa7146_setgpio(saa, 1, SAA7146_GPIO_INPUT);
399 saa7146_setgpio(saa, 2, SAA7146_GPIO_INPUT);
400 saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
401
402 /* release the CA device */
403 dvb_ca_en50221_release(&budget_av->ca);
404
405 /* disable DEBI pins */
406 saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16));
407}
408
409
410static const u8 saa7113_tab[] = {
411 0x01, 0x08,
412 0x02, 0xc0,
413 0x03, 0x33,
414 0x04, 0x00,
415 0x05, 0x00,
416 0x06, 0xeb,
417 0x07, 0xe0,
418 0x08, 0x28,
419 0x09, 0x00,
420 0x0a, 0x80,
421 0x0b, 0x47,
422 0x0c, 0x40,
423 0x0d, 0x00,
424 0x0e, 0x01,
425 0x0f, 0x44,
426
427 0x10, 0x08,
428 0x11, 0x0c,
429 0x12, 0x7b,
430 0x13, 0x00,
431 0x15, 0x00, 0x16, 0x00, 0x17, 0x00,
432
433 0x57, 0xff,
434 0x40, 0x82, 0x58, 0x00, 0x59, 0x54, 0x5a, 0x07,
435 0x5b, 0x83, 0x5e, 0x00,
436 0xff
437};
438
439static int saa7113_init(struct budget_av *budget_av)
440{
441 struct budget *budget = &budget_av->budget;
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700442 struct saa7146_dev *saa = budget->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 const u8 *data = saa7113_tab;
444
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700445 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI);
446 msleep(200);
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (i2c_writereg(&budget->i2c_adap, 0x4a, 0x01, 0x08) != 1) {
449 dprintk(1, "saa7113 not found on KNC card\n");
450 return -ENODEV;
451 }
452
453 dprintk(1, "saa7113 detected and initializing\n");
454
455 while (*data != 0xff) {
456 i2c_writereg(&budget->i2c_adap, 0x4a, *data, *(data + 1));
457 data += 2;
458 }
459
460 dprintk(1, "saa7113 status=%02x\n", i2c_readreg(&budget->i2c_adap, 0x4a, 0x1f));
461
462 return 0;
463}
464
465static int saa7113_setinput(struct budget_av *budget_av, int input)
466{
467 struct budget *budget = &budget_av->budget;
468
469 if (1 != budget_av->has_saa7113)
470 return -ENODEV;
471
472 if (input == 1) {
473 i2c_writereg(&budget->i2c_adap, 0x4a, 0x02, 0xc7);
474 i2c_writereg(&budget->i2c_adap, 0x4a, 0x09, 0x80);
475 } else if (input == 0) {
476 i2c_writereg(&budget->i2c_adap, 0x4a, 0x02, 0xc0);
477 i2c_writereg(&budget->i2c_adap, 0x4a, 0x09, 0x00);
478 } else
479 return -EINVAL;
480
481 budget_av->cur_input = input;
482 return 0;
483}
484
485
486static int philips_su1278_ty_ci_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
487{
488 u8 aclk = 0;
489 u8 bclk = 0;
490 u8 m1;
491
492 aclk = 0xb5;
493 if (srate < 2000000)
494 bclk = 0x86;
495 else if (srate < 5000000)
496 bclk = 0x89;
497 else if (srate < 15000000)
498 bclk = 0x8f;
499 else if (srate < 45000000)
500 bclk = 0x95;
501
502 m1 = 0x14;
503 if (srate < 4000000)
504 m1 = 0x10;
505
506 stv0299_writereg(fe, 0x13, aclk);
507 stv0299_writereg(fe, 0x14, bclk);
508 stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
509 stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
510 stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
511 stv0299_writereg(fe, 0x0f, 0x80 | m1);
512
513 return 0;
514}
515
Andrew de Quinceye87d41c2006-04-18 17:47:11 -0300516static int philips_su1278_ty_ci_tuner_set_params(struct dvb_frontend *fe,
517 struct dvb_frontend_parameters *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 u32 div;
520 u8 buf[4];
Andrew de Quinceye87d41c2006-04-18 17:47:11 -0300521 struct budget *budget = (struct budget *) fe->dvb->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
523
524 if ((params->frequency < 950000) || (params->frequency > 2150000))
525 return -EINVAL;
526
527 div = (params->frequency + (125 - 1)) / 125; // round correctly
528 buf[0] = (div >> 8) & 0x7f;
529 buf[1] = div & 0xff;
530 buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
531 buf[3] = 0x20;
532
533 if (params->u.qpsk.symbol_rate < 4000000)
534 buf[3] |= 1;
535
536 if (params->frequency < 1250000)
537 buf[3] |= 0;
538 else if (params->frequency < 1550000)
539 buf[3] |= 0x40;
540 else if (params->frequency < 2050000)
541 buf[3] |= 0x80;
542 else if (params->frequency < 2150000)
543 buf[3] |= 0xC0;
544
Patrick Boettcherdea74862006-05-14 05:01:31 -0300545 if (fe->ops.i2c_gate_ctrl)
546 fe->ops.i2c_gate_ctrl(fe, 1);
Andrew de Quinceye87d41c2006-04-18 17:47:11 -0300547 if (i2c_transfer(&budget->i2c_adap, &msg, 1) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return -EIO;
549 return 0;
550}
551
552static u8 typhoon_cinergy1200s_inittab[] = {
553 0x01, 0x15,
554 0x02, 0x30,
555 0x03, 0x00,
556 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
557 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
558 0x06, 0x40, /* DAC not used, set to high impendance mode */
559 0x07, 0x00, /* DAC LSB */
560 0x08, 0x40, /* DiSEqC off */
561 0x09, 0x00, /* FIFO */
562 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
563 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
564 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
565 0x10, 0x3f, // AGC2 0x3d
566 0x11, 0x84,
Oliver Endrissff29d062005-11-08 21:35:43 -0800567 0x12, 0xb9,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 0x15, 0xc9, // lock detector threshold
569 0x16, 0x00,
570 0x17, 0x00,
571 0x18, 0x00,
572 0x19, 0x00,
573 0x1a, 0x00,
574 0x1f, 0x50,
575 0x20, 0x00,
576 0x21, 0x00,
577 0x22, 0x00,
578 0x23, 0x00,
579 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
580 0x29, 0x1e, // 1/2 threshold
581 0x2a, 0x14, // 2/3 threshold
582 0x2b, 0x0f, // 3/4 threshold
583 0x2c, 0x09, // 5/6 threshold
584 0x2d, 0x05, // 7/8 threshold
585 0x2e, 0x01,
586 0x31, 0x1f, // test all FECs
587 0x32, 0x19, // viterbi and synchro search
588 0x33, 0xfc, // rs control
589 0x34, 0x93, // error control
590 0x0f, 0x92,
591 0xff, 0xff
592};
593
594static struct stv0299_config typhoon_config = {
595 .demod_address = 0x68,
596 .inittab = typhoon_cinergy1200s_inittab,
597 .mclk = 88000000UL,
598 .invert = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 .skip_reinit = 0,
600 .lock_output = STV0229_LOCKOUTPUT_1,
601 .volt13_op0_op1 = STV0299_VOLT13_OP0,
602 .min_delay_ms = 100,
603 .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604};
605
606
607static struct stv0299_config cinergy_1200s_config = {
608 .demod_address = 0x68,
609 .inittab = typhoon_cinergy1200s_inittab,
610 .mclk = 88000000UL,
611 .invert = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 .skip_reinit = 0,
613 .lock_output = STV0229_LOCKOUTPUT_0,
614 .volt13_op0_op1 = STV0299_VOLT13_OP0,
615 .min_delay_ms = 100,
616 .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617};
618
Andrew de Quinceyeffa7912006-01-09 15:25:09 -0200619static struct stv0299_config cinergy_1200s_1894_0010_config = {
620 .demod_address = 0x68,
621 .inittab = typhoon_cinergy1200s_inittab,
622 .mclk = 88000000UL,
623 .invert = 1,
624 .skip_reinit = 0,
625 .lock_output = STV0229_LOCKOUTPUT_1,
626 .volt13_op0_op1 = STV0299_VOLT13_OP0,
627 .min_delay_ms = 100,
628 .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
Andrew de Quinceyeffa7912006-01-09 15:25:09 -0200629};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Andrew de Quinceye87d41c2006-04-18 17:47:11 -0300631static int philips_cu1216_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 struct budget *budget = (struct budget *) fe->dvb->priv;
634 u8 buf[4];
635 struct i2c_msg msg = {.addr = 0x60,.flags = 0,.buf = buf,.len = sizeof(buf) };
636
637#define TUNER_MUL 62500
638
639 u32 div = (params->frequency + 36125000 + TUNER_MUL / 2) / TUNER_MUL;
640
641 buf[0] = (div >> 8) & 0x7f;
642 buf[1] = div & 0xff;
Johannes Stezenbacheef57642005-07-07 17:57:58 -0700643 buf[2] = 0x86;
644 buf[3] = (params->frequency < 150000000 ? 0x01 :
645 params->frequency < 445000000 ? 0x02 : 0x04);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Patrick Boettcherdea74862006-05-14 05:01:31 -0300647 if (fe->ops.i2c_gate_ctrl)
648 fe->ops.i2c_gate_ctrl(fe, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (i2c_transfer(&budget->i2c_adap, &msg, 1) != 1)
650 return -EIO;
651 return 0;
652}
653
654static struct tda10021_config philips_cu1216_config = {
655 .demod_address = 0x0c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656};
657
Andrew de Quincey61cebe92006-11-19 14:10:59 -0300658static struct tda10021_config philips_cu1216_config_altaddress = {
659 .demod_address = 0x0d,
660};
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663
664
Andrew de Quinceye87d41c2006-04-18 17:47:11 -0300665static int philips_tu1216_tuner_init(struct dvb_frontend *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 struct budget *budget = (struct budget *) fe->dvb->priv;
668 static u8 tu1216_init[] = { 0x0b, 0xf5, 0x85, 0xab };
669 struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tu1216_init,.len = sizeof(tu1216_init) };
670
671 // setup PLL configuration
Patrick Boettcherdea74862006-05-14 05:01:31 -0300672 if (fe->ops.i2c_gate_ctrl)
673 fe->ops.i2c_gate_ctrl(fe, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (i2c_transfer(&budget->i2c_adap, &tuner_msg, 1) != 1)
675 return -EIO;
676 msleep(1);
677
678 return 0;
679}
680
Andrew de Quinceye87d41c2006-04-18 17:47:11 -0300681static int philips_tu1216_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 struct budget *budget = (struct budget *) fe->dvb->priv;
684 u8 tuner_buf[4];
685 struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tuner_buf,.len =
686 sizeof(tuner_buf) };
687 int tuner_frequency = 0;
688 u8 band, cp, filter;
689
690 // determine charge pump
691 tuner_frequency = params->frequency + 36166000;
692 if (tuner_frequency < 87000000)
693 return -EINVAL;
694 else if (tuner_frequency < 130000000)
695 cp = 3;
696 else if (tuner_frequency < 160000000)
697 cp = 5;
698 else if (tuner_frequency < 200000000)
699 cp = 6;
700 else if (tuner_frequency < 290000000)
701 cp = 3;
702 else if (tuner_frequency < 420000000)
703 cp = 5;
704 else if (tuner_frequency < 480000000)
705 cp = 6;
706 else if (tuner_frequency < 620000000)
707 cp = 3;
708 else if (tuner_frequency < 830000000)
709 cp = 5;
710 else if (tuner_frequency < 895000000)
711 cp = 7;
712 else
713 return -EINVAL;
714
715 // determine band
716 if (params->frequency < 49000000)
717 return -EINVAL;
718 else if (params->frequency < 161000000)
719 band = 1;
720 else if (params->frequency < 444000000)
721 band = 2;
722 else if (params->frequency < 861000000)
723 band = 4;
724 else
725 return -EINVAL;
726
727 // setup PLL filter
728 switch (params->u.ofdm.bandwidth) {
729 case BANDWIDTH_6_MHZ:
730 filter = 0;
731 break;
732
733 case BANDWIDTH_7_MHZ:
734 filter = 0;
735 break;
736
737 case BANDWIDTH_8_MHZ:
738 filter = 1;
739 break;
740
741 default:
742 return -EINVAL;
743 }
744
745 // calculate divisor
746 // ((36166000+((1000000/6)/2)) + Finput)/(1000000/6)
747 tuner_frequency = (((params->frequency / 1000) * 6) + 217496) / 1000;
748
749 // setup tuner buffer
750 tuner_buf[0] = (tuner_frequency >> 8) & 0x7f;
751 tuner_buf[1] = tuner_frequency & 0xff;
752 tuner_buf[2] = 0xca;
753 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
754
Patrick Boettcherdea74862006-05-14 05:01:31 -0300755 if (fe->ops.i2c_gate_ctrl)
756 fe->ops.i2c_gate_ctrl(fe, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (i2c_transfer(&budget->i2c_adap, &tuner_msg, 1) != 1)
758 return -EIO;
759
760 msleep(1);
761 return 0;
762}
763
764static int philips_tu1216_request_firmware(struct dvb_frontend *fe,
765 const struct firmware **fw, char *name)
766{
767 struct budget *budget = (struct budget *) fe->dvb->priv;
768
769 return request_firmware(fw, name, &budget->dev->pci->dev);
770}
771
772static struct tda1004x_config philips_tu1216_config = {
773
774 .demod_address = 0x8,
775 .invert = 1,
776 .invert_oclk = 1,
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700777 .xtal_freq = TDA10046_XTAL_4M,
778 .agc_config = TDA10046_AGC_DEFAULT,
779 .if_freq = TDA10046_FREQ_3617,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 .request_firmware = philips_tu1216_request_firmware,
781};
782
Regis Prevotf8bf1342006-01-11 23:31:53 -0200783static u8 philips_sd1878_inittab[] = {
784 0x01, 0x15,
785 0x02, 0x30,
786 0x03, 0x00,
787 0x04, 0x7d,
788 0x05, 0x35,
789 0x06, 0x40,
790 0x07, 0x00,
791 0x08, 0x43,
792 0x09, 0x02,
793 0x0C, 0x51,
794 0x0D, 0x82,
795 0x0E, 0x23,
796 0x10, 0x3f,
797 0x11, 0x84,
798 0x12, 0xb9,
799 0x15, 0xc9,
800 0x16, 0x19,
801 0x17, 0x8c,
802 0x18, 0x59,
803 0x19, 0xf8,
804 0x1a, 0xfe,
805 0x1c, 0x7f,
806 0x1d, 0x00,
807 0x1e, 0x00,
808 0x1f, 0x50,
809 0x20, 0x00,
810 0x21, 0x00,
811 0x22, 0x00,
812 0x23, 0x00,
813 0x28, 0x00,
814 0x29, 0x28,
815 0x2a, 0x14,
816 0x2b, 0x0f,
817 0x2c, 0x09,
818 0x2d, 0x09,
819 0x31, 0x1f,
820 0x32, 0x19,
821 0x33, 0xfc,
822 0x34, 0x93,
823 0xff, 0xff
824};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Andrew de Quinceye87d41c2006-04-18 17:47:11 -0300826static int philips_sd1878_tda8261_tuner_set_params(struct dvb_frontend *fe,
827 struct dvb_frontend_parameters *params)
Regis Prevotf8bf1342006-01-11 23:31:53 -0200828{
829 u8 buf[4];
830 int rc;
831 struct i2c_msg tuner_msg = {.addr=0x60,.flags=0,.buf=buf,.len=sizeof(buf)};
Andrew de Quinceye87d41c2006-04-18 17:47:11 -0300832 struct budget *budget = (struct budget *) fe->dvb->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Regis Prevotf8bf1342006-01-11 23:31:53 -0200834 if((params->frequency < 950000) || (params->frequency > 2150000))
835 return -EINVAL;
836
837 rc=dvb_pll_configure(&dvb_pll_philips_sd1878_tda8261, buf,
Michael Krufky47ae9ae2006-11-20 16:38:42 -0300838 params->frequency, 0);
Regis Prevotf8bf1342006-01-11 23:31:53 -0200839 if(rc < 0) return rc;
840
Patrick Boettcherdea74862006-05-14 05:01:31 -0300841 if (fe->ops.i2c_gate_ctrl)
842 fe->ops.i2c_gate_ctrl(fe, 1);
Andrew de Quinceye87d41c2006-04-18 17:47:11 -0300843 if(i2c_transfer(&budget->i2c_adap, &tuner_msg, 1) != 1)
Regis Prevotf8bf1342006-01-11 23:31:53 -0200844 return -EIO;
845
846 return 0;
847}
848
849static int philips_sd1878_ci_set_symbol_rate(struct dvb_frontend *fe,
850 u32 srate, u32 ratio)
851{
852 u8 aclk = 0;
853 u8 bclk = 0;
854 u8 m1;
855
856 aclk = 0xb5;
857 if (srate < 2000000)
858 bclk = 0x86;
859 else if (srate < 5000000)
860 bclk = 0x89;
861 else if (srate < 15000000)
862 bclk = 0x8f;
863 else if (srate < 45000000)
864 bclk = 0x95;
865
866 m1 = 0x14;
867 if (srate < 4000000)
868 m1 = 0x10;
869
870 stv0299_writereg(fe, 0x0e, 0x23);
871 stv0299_writereg(fe, 0x0f, 0x94);
872 stv0299_writereg(fe, 0x10, 0x39);
873 stv0299_writereg(fe, 0x13, aclk);
874 stv0299_writereg(fe, 0x14, bclk);
875 stv0299_writereg(fe, 0x15, 0xc9);
876 stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
877 stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
878 stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
879 stv0299_writereg(fe, 0x0f, 0x80 | m1);
880
881 return 0;
882}
883
884static struct stv0299_config philips_sd1878_config = {
885 .demod_address = 0x68,
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300886 .inittab = philips_sd1878_inittab,
Regis Prevotf8bf1342006-01-11 23:31:53 -0200887 .mclk = 88000000UL,
888 .invert = 0,
889 .skip_reinit = 0,
890 .lock_output = STV0229_LOCKOUTPUT_1,
891 .volt13_op0_op1 = STV0299_VOLT13_OP0,
892 .min_delay_ms = 100,
893 .set_symbol_rate = philips_sd1878_ci_set_symbol_rate,
Regis Prevotf8bf1342006-01-11 23:31:53 -0200894};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896static u8 read_pwm(struct budget_av *budget_av)
897{
898 u8 b = 0xff;
899 u8 pwm;
900 struct i2c_msg msg[] = { {.addr = 0x50,.flags = 0,.buf = &b,.len = 1},
901 {.addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1}
902 };
903
904 if ((i2c_transfer(&budget_av->budget.i2c_adap, msg, 2) != 2)
905 || (pwm == 0xff))
906 pwm = 0x48;
907
908 return pwm;
909}
910
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700911#define SUBID_DVBS_KNC1 0x0010
912#define SUBID_DVBS_KNC1_PLUS 0x0011
913#define SUBID_DVBS_TYPHOON 0x4f56
914#define SUBID_DVBS_CINERGY1200 0x1154
Regis Prevotf8bf1342006-01-11 23:31:53 -0200915#define SUBID_DVBS_CYNERGY1200N 0x1155
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700916
Regis Prevotf8bf1342006-01-11 23:31:53 -0200917#define SUBID_DVBS_TV_STAR 0x0014
918#define SUBID_DVBS_TV_STAR_CI 0x0016
Lothar Englisch60110ce2006-06-06 16:13:46 -0300919#define SUBID_DVBS_EASYWATCH_1 0x001a
Thilo Berger36f4f332006-02-27 00:09:08 -0300920#define SUBID_DVBS_EASYWATCH 0x001e
Thomas Hammc01d1e42006-11-17 07:12:58 -0300921#define SUBID_DVBC_EASYWATCH 0x002a
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700922#define SUBID_DVBC_KNC1 0x0020
923#define SUBID_DVBC_KNC1_PLUS 0x0021
924#define SUBID_DVBC_CINERGY1200 0x1156
925
926#define SUBID_DVBT_KNC1_PLUS 0x0031
927#define SUBID_DVBT_KNC1 0x0030
928#define SUBID_DVBT_CINERGY1200 0x1157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300930
931static int tda10021_set_frontend(struct dvb_frontend *fe,
932 struct dvb_frontend_parameters *p)
933{
934 struct budget_av* budget_av = fe->dvb->priv;
935 int result;
936
937 result = budget_av->tda10021_set_frontend(fe, p);
938 if (budget_av->tda10021_ts_enabled) {
Andrew de Quincey2bfe0312006-08-08 09:10:08 -0300939 tda10021_writereg(budget_av->budget.dvb_frontend, 0x12, 0xa1);
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300940 } else {
Andrew de Quincey2bfe0312006-08-08 09:10:08 -0300941 tda10021_writereg(budget_av->budget.dvb_frontend, 0x12, 0xa0);
Andrew de Quincey5c1208b2006-05-22 10:32:02 -0300942 }
943
944 return result;
945}
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947static void frontend_init(struct budget_av *budget_av)
948{
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700949 struct saa7146_dev * saa = budget_av->budget.dev;
950 struct dvb_frontend * fe = NULL;
951
Andrew de Quincey473f5422006-04-13 17:29:07 -0300952 /* Enable / PowerON Frontend */
953 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO);
954
Hartmut Birr740cf9e2006-11-03 15:34:18 -0300955 /* Wait for PowerON */
956 msleep(100);
957
Andrew de Quincey473f5422006-04-13 17:29:07 -0300958 /* additional setup necessary for the PLUS cards */
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700959 switch (saa->pci->subsystem_device) {
960 case SUBID_DVBS_KNC1_PLUS:
961 case SUBID_DVBC_KNC1_PLUS:
962 case SUBID_DVBT_KNC1_PLUS:
Thomas Hammc01d1e42006-11-17 07:12:58 -0300963 case SUBID_DVBC_EASYWATCH:
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700964 saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTHI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 break;
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700966 }
967
968 switch (saa->pci->subsystem_device) {
969
970 case SUBID_DVBS_KNC1:
Christoph Pfister4e318be2006-08-12 09:13:35 -0300971 case SUBID_DVBS_KNC1_PLUS:
Lothar Englisch60110ce2006-06-06 16:13:46 -0300972 case SUBID_DVBS_EASYWATCH_1:
Andrew de Quinceyeffa7912006-01-09 15:25:09 -0200973 if (saa->pci->subsystem_vendor == 0x1894) {
Andrew de Quincey2bfe0312006-08-08 09:10:08 -0300974 fe = dvb_attach(stv0299_attach, &cinergy_1200s_1894_0010_config,
Andrew de Quinceyeffa7912006-01-09 15:25:09 -0200975 &budget_av->budget.i2c_adap);
Andrew de Quinceye87d41c2006-04-18 17:47:11 -0300976 if (fe) {
Andrew de Quincey1c72cfdc2006-09-05 17:58:20 -0300977 dvb_attach(tua6100_attach, fe, 0x60, &budget_av->budget.i2c_adap);
Andrew de Quinceye87d41c2006-04-18 17:47:11 -0300978 }
Andrew de Quinceyeffa7912006-01-09 15:25:09 -0200979 } else {
Andrew de Quincey2bfe0312006-08-08 09:10:08 -0300980 fe = dvb_attach(stv0299_attach, &typhoon_config,
Andrew de Quinceyeffa7912006-01-09 15:25:09 -0200981 &budget_av->budget.i2c_adap);
Andrew de Quinceye87d41c2006-04-18 17:47:11 -0300982 if (fe) {
Patrick Boettcherdea74862006-05-14 05:01:31 -0300983 fe->ops.tuner_ops.set_params = philips_su1278_ty_ci_tuner_set_params;
Andrew de Quinceye87d41c2006-04-18 17:47:11 -0300984 }
Andrew de Quinceyeffa7912006-01-09 15:25:09 -0200985 }
986 break;
987
Regis Prevotf8bf1342006-01-11 23:31:53 -0200988 case SUBID_DVBS_TV_STAR:
989 case SUBID_DVBS_TV_STAR_CI:
990 case SUBID_DVBS_CYNERGY1200N:
Thilo Berger36f4f332006-02-27 00:09:08 -0300991 case SUBID_DVBS_EASYWATCH:
Andrew de Quincey2bfe0312006-08-08 09:10:08 -0300992 fe = dvb_attach(stv0299_attach, &philips_sd1878_config,
Regis Prevotf8bf1342006-01-11 23:31:53 -0200993 &budget_av->budget.i2c_adap);
Andrew de Quinceye87d41c2006-04-18 17:47:11 -0300994 if (fe) {
Patrick Boettcherdea74862006-05-14 05:01:31 -0300995 fe->ops.tuner_ops.set_params = philips_sd1878_tda8261_tuner_set_params;
Andrew de Quinceye87d41c2006-04-18 17:47:11 -0300996 }
Regis Prevotf8bf1342006-01-11 23:31:53 -0200997 break;
998
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -0700999 case SUBID_DVBS_TYPHOON:
Andrew de Quincey2bfe0312006-08-08 09:10:08 -03001000 fe = dvb_attach(stv0299_attach, &typhoon_config,
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -07001001 &budget_av->budget.i2c_adap);
Andrew de Quinceye87d41c2006-04-18 17:47:11 -03001002 if (fe) {
Patrick Boettcherdea74862006-05-14 05:01:31 -03001003 fe->ops.tuner_ops.set_params = philips_su1278_ty_ci_tuner_set_params;
Andrew de Quinceye87d41c2006-04-18 17:47:11 -03001004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 break;
1006
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -07001007 case SUBID_DVBS_CINERGY1200:
Andrew de Quincey2bfe0312006-08-08 09:10:08 -03001008 fe = dvb_attach(stv0299_attach, &cinergy_1200s_config,
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -07001009 &budget_av->budget.i2c_adap);
Andrew de Quinceye87d41c2006-04-18 17:47:11 -03001010 if (fe) {
Patrick Boettcherdea74862006-05-14 05:01:31 -03001011 fe->ops.tuner_ops.set_params = philips_su1278_ty_ci_tuner_set_params;
Andrew de Quinceye87d41c2006-04-18 17:47:11 -03001012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 break;
1014
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -07001015 case SUBID_DVBC_KNC1:
1016 case SUBID_DVBC_KNC1_PLUS:
Andrew de Quincey5c1208b2006-05-22 10:32:02 -03001017 case SUBID_DVBC_CINERGY1200:
Thomas Hammc01d1e42006-11-17 07:12:58 -03001018 case SUBID_DVBC_EASYWATCH:
Andrew de Quincey5c1208b2006-05-22 10:32:02 -03001019 budget_av->reinitialise_demod = 1;
Andrew de Quincey2bfe0312006-08-08 09:10:08 -03001020 fe = dvb_attach(tda10021_attach, &philips_cu1216_config,
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -07001021 &budget_av->budget.i2c_adap,
1022 read_pwm(budget_av));
Andrew de Quincey61cebe92006-11-19 14:10:59 -03001023 if (fe == NULL)
1024 fe = dvb_attach(tda10021_attach, &philips_cu1216_config_altaddress,
1025 &budget_av->budget.i2c_adap,
1026 read_pwm(budget_av));
Andrew de Quinceye87d41c2006-04-18 17:47:11 -03001027 if (fe) {
Andrew de Quincey5c1208b2006-05-22 10:32:02 -03001028 budget_av->tda10021_poclkp = 1;
Patrick Boettcherdea74862006-05-14 05:01:31 -03001029 budget_av->tda10021_set_frontend = fe->ops.set_frontend;
1030 fe->ops.set_frontend = tda10021_set_frontend;
1031 fe->ops.tuner_ops.set_params = philips_cu1216_tuner_set_params;
Andrew de Quinceye87d41c2006-04-18 17:47:11 -03001032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 break;
1034
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -07001035 case SUBID_DVBT_KNC1:
1036 case SUBID_DVBT_KNC1_PLUS:
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -07001037 case SUBID_DVBT_CINERGY1200:
Andrew de Quincey5c1208b2006-05-22 10:32:02 -03001038 budget_av->reinitialise_demod = 1;
Andrew de Quincey2bfe0312006-08-08 09:10:08 -03001039 fe = dvb_attach(tda10046_attach, &philips_tu1216_config,
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -07001040 &budget_av->budget.i2c_adap);
Andrew de Quincey6b3ccab2006-04-20 12:01:47 -03001041 if (fe) {
Patrick Boettcherdea74862006-05-14 05:01:31 -03001042 fe->ops.tuner_ops.init = philips_tu1216_tuner_init;
1043 fe->ops.tuner_ops.set_params = philips_tu1216_tuner_set_params;
Andrew de Quincey6b3ccab2006-04-20 12:01:47 -03001044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 break;
1046 }
1047
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -07001048 if (fe == NULL) {
1049 printk(KERN_ERR "budget-av: A frontend driver was not found "
1050 "for device %04x/%04x subsystem %04x/%04x\n",
1051 saa->pci->vendor,
1052 saa->pci->device,
1053 saa->pci->subsystem_vendor,
1054 saa->pci->subsystem_device);
1055 return;
1056 }
1057
1058 budget_av->budget.dvb_frontend = fe;
1059
1060 if (dvb_register_frontend(&budget_av->budget.dvb_adapter,
1061 budget_av->budget.dvb_frontend)) {
1062 printk(KERN_ERR "budget-av: Frontend registration failed!\n");
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03001063 dvb_frontend_detach(budget_av->budget.dvb_frontend);
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -07001064 budget_av->budget.dvb_frontend = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066}
1067
1068
1069static void budget_av_irq(struct saa7146_dev *dev, u32 * isr)
1070{
1071 struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
1072
1073 dprintk(8, "dev: %p, budget_av: %p\n", dev, budget_av);
1074
1075 if (*isr & MASK_10)
1076 ttpci_budget_irq10_handler(dev, isr);
1077}
1078
1079static int budget_av_detach(struct saa7146_dev *dev)
1080{
1081 struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
1082 int err;
1083
1084 dprintk(2, "dev: %p\n", dev);
1085
1086 if (1 == budget_av->has_saa7113) {
1087 saa7146_setgpio(dev, 0, SAA7146_GPIO_OUTLO);
1088
1089 msleep(200);
1090
1091 saa7146_unregister_device(&budget_av->vd, dev);
1092 }
1093
1094 if (budget_av->budget.ci_present)
1095 ciintf_deinit(budget_av);
1096
Andrew de Quincey2bfe0312006-08-08 09:10:08 -03001097 if (budget_av->budget.dvb_frontend != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 dvb_unregister_frontend(budget_av->budget.dvb_frontend);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03001099 dvb_frontend_detach(budget_av->budget.dvb_frontend);
Andrew de Quincey2bfe0312006-08-08 09:10:08 -03001100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 err = ttpci_budget_deinit(&budget_av->budget);
1102
1103 kfree(budget_av);
1104
1105 return err;
1106}
1107
1108static struct saa7146_ext_vv vv_data;
1109
1110static int budget_av_attach(struct saa7146_dev *dev, struct saa7146_pci_extension_data *info)
1111{
1112 struct budget_av *budget_av;
1113 u8 *mac;
1114 int err;
1115
1116 dprintk(2, "dev: %p\n", dev);
1117
Panagiotis Issaris74081872006-01-11 19:40:56 -02001118 if (!(budget_av = kzalloc(sizeof(struct budget_av), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return -ENOMEM;
1120
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -07001121 budget_av->has_saa7113 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 budget_av->budget.ci_present = 0;
1123
1124 dev->ext_priv = budget_av;
1125
1126 if ((err = ttpci_budget_init(&budget_av->budget, dev, info, THIS_MODULE))) {
1127 kfree(budget_av);
1128 return err;
1129 }
1130
1131 /* knc1 initialization */
1132 saa7146_write(dev, DD1_STREAM_B, 0x04000000);
1133 saa7146_write(dev, DD1_INIT, 0x07000600);
1134 saa7146_write(dev, MC2, MASK_09 | MASK_25 | MASK_10 | MASK_26);
1135
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -07001136 if (saa7113_init(budget_av) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 budget_av->has_saa7113 = 1;
1138
1139 if (0 != saa7146_vv_init(dev, &vv_data)) {
1140 /* fixme: proper cleanup here */
1141 ERR(("cannot init vv subsystem.\n"));
1142 return err;
1143 }
1144
1145 if ((err = saa7146_register_device(&budget_av->vd, dev, "knc1", VFL_TYPE_GRABBER))) {
1146 /* fixme: proper cleanup here */
1147 ERR(("cannot register capture v4l2 device.\n"));
1148 return err;
1149 }
1150
1151 /* beware: this modifies dev->vv ... */
1152 saa7146_set_hps_source_and_sync(dev, SAA7146_HPS_SOURCE_PORT_A,
1153 SAA7146_HPS_SYNC_PORT_A);
1154
1155 saa7113_setinput(budget_av, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157
1158 /* fixme: find some sane values here... */
1159 saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
1160
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001161 mac = budget_av->budget.dvb_adapter.proposed_mac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (i2c_readregs(&budget_av->budget.i2c_adap, 0xa0, 0x30, mac, 6)) {
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -07001163 printk(KERN_ERR "KNC1-%d: Could not read MAC from KNC1 card\n",
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001164 budget_av->budget.dvb_adapter.num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 memset(mac, 0, 6);
1166 } else {
Johannes Stezenbachb82a96a2005-05-16 21:54:49 -07001167 printk(KERN_INFO "KNC1-%d: MAC addr = %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001168 budget_av->budget.dvb_adapter.num,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
1170 }
1171
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001172 budget_av->budget.dvb_adapter.priv = budget_av;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 frontend_init(budget_av);
Andrew de Quinceyc5e768a2006-06-29 13:16:11 -03001174 ciintf_init(budget_av);
Oliver Endriss32e4c3a2006-07-18 22:55:23 -03001175
1176 ttpci_budget_init_hooks(&budget_av->budget);
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return 0;
1179}
1180
1181#define KNC1_INPUTS 2
1182static struct v4l2_input knc1_inputs[KNC1_INPUTS] = {
1183 {0, "Composite", V4L2_INPUT_TYPE_TUNER, 1, 0, V4L2_STD_PAL_BG | V4L2_STD_NTSC_M, 0},
1184 {1, "S-Video", V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG | V4L2_STD_NTSC_M, 0},
1185};
1186
1187static struct saa7146_extension_ioctls ioctls[] = {
1188 {VIDIOC_ENUMINPUT, SAA7146_EXCLUSIVE},
1189 {VIDIOC_G_INPUT, SAA7146_EXCLUSIVE},
1190 {VIDIOC_S_INPUT, SAA7146_EXCLUSIVE},
1191 {0, 0}
1192};
1193
1194static int av_ioctl(struct saa7146_fh *fh, unsigned int cmd, void *arg)
1195{
1196 struct saa7146_dev *dev = fh->dev;
1197 struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
1198
1199 switch (cmd) {
1200 case VIDIOC_ENUMINPUT:{
1201 struct v4l2_input *i = arg;
1202
1203 dprintk(1, "VIDIOC_ENUMINPUT %d.\n", i->index);
1204 if (i->index < 0 || i->index >= KNC1_INPUTS) {
1205 return -EINVAL;
1206 }
1207 memcpy(i, &knc1_inputs[i->index], sizeof(struct v4l2_input));
1208 return 0;
1209 }
1210 case VIDIOC_G_INPUT:{
1211 int *input = (int *) arg;
1212
1213 *input = budget_av->cur_input;
1214
1215 dprintk(1, "VIDIOC_G_INPUT %d.\n", *input);
1216 return 0;
1217 }
1218 case VIDIOC_S_INPUT:{
1219 int input = *(int *) arg;
1220 dprintk(1, "VIDIOC_S_INPUT %d.\n", input);
1221 return saa7113_setinput(budget_av, input);
1222 }
1223 default:
1224 return -ENOIOCTLCMD;
1225 }
1226 return 0;
1227}
1228
1229static struct saa7146_standard standard[] = {
1230 {.name = "PAL",.id = V4L2_STD_PAL,
1231 .v_offset = 0x17,.v_field = 288,
1232 .h_offset = 0x14,.h_pixels = 680,
1233 .v_max_out = 576,.h_max_out = 768 },
1234
1235 {.name = "NTSC",.id = V4L2_STD_NTSC,
1236 .v_offset = 0x16,.v_field = 240,
1237 .h_offset = 0x06,.h_pixels = 708,
1238 .v_max_out = 480,.h_max_out = 640, },
1239};
1240
1241static struct saa7146_ext_vv vv_data = {
1242 .inputs = 2,
1243 .capabilities = 0, // perhaps later: V4L2_CAP_VBI_CAPTURE, but that need tweaking with the saa7113
1244 .flags = 0,
1245 .stds = &standard[0],
1246 .num_stds = sizeof(standard) / sizeof(struct saa7146_standard),
1247 .ioctls = &ioctls[0],
1248 .ioctl = av_ioctl,
1249};
1250
1251static struct saa7146_extension budget_extension;
1252
1253MAKE_BUDGET_INFO(knc1s, "KNC1 DVB-S", BUDGET_KNC1S);
1254MAKE_BUDGET_INFO(knc1c, "KNC1 DVB-C", BUDGET_KNC1C);
1255MAKE_BUDGET_INFO(knc1t, "KNC1 DVB-T", BUDGET_KNC1T);
Regis Prevotf8bf1342006-01-11 23:31:53 -02001256MAKE_BUDGET_INFO(kncxs, "KNC TV STAR DVB-S", BUDGET_TVSTAR);
Thilo Berger36f4f332006-02-27 00:09:08 -03001257MAKE_BUDGET_INFO(satewpls, "Satelco EasyWatch DVB-S light", BUDGET_TVSTAR);
Lothar Englisch60110ce2006-06-06 16:13:46 -03001258MAKE_BUDGET_INFO(satewpls1, "Satelco EasyWatch DVB-S light", BUDGET_KNC1S);
Thomas Hammc01d1e42006-11-17 07:12:58 -03001259MAKE_BUDGET_INFO(satewplc, "Satelco EasyWatch DVB-C", BUDGET_KNC1CP);
Johannes Stezenbach2d4f2c22005-05-16 21:54:23 -07001260MAKE_BUDGET_INFO(knc1sp, "KNC1 DVB-S Plus", BUDGET_KNC1SP);
1261MAKE_BUDGET_INFO(knc1cp, "KNC1 DVB-C Plus", BUDGET_KNC1CP);
1262MAKE_BUDGET_INFO(knc1tp, "KNC1 DVB-T Plus", BUDGET_KNC1TP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263MAKE_BUDGET_INFO(cin1200s, "TerraTec Cinergy 1200 DVB-S", BUDGET_CIN1200S);
Regis Prevotf8bf1342006-01-11 23:31:53 -02001264MAKE_BUDGET_INFO(cin1200sn, "TerraTec Cinergy 1200 DVB-S", BUDGET_CIN1200S);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265MAKE_BUDGET_INFO(cin1200c, "Terratec Cinergy 1200 DVB-C", BUDGET_CIN1200C);
1266MAKE_BUDGET_INFO(cin1200t, "Terratec Cinergy 1200 DVB-T", BUDGET_CIN1200T);
1267
1268static struct pci_device_id pci_tbl[] = {
1269 MAKE_EXTENSION_PCI(knc1s, 0x1131, 0x4f56),
Johannes Stezenbach2d4f2c22005-05-16 21:54:23 -07001270 MAKE_EXTENSION_PCI(knc1s, 0x1131, 0x0010),
Andrew de Quinceyeffa7912006-01-09 15:25:09 -02001271 MAKE_EXTENSION_PCI(knc1s, 0x1894, 0x0010),
Johannes Stezenbach2d4f2c22005-05-16 21:54:23 -07001272 MAKE_EXTENSION_PCI(knc1sp, 0x1131, 0x0011),
Christoph Pfister4e318be2006-08-12 09:13:35 -03001273 MAKE_EXTENSION_PCI(knc1sp, 0x1894, 0x0011),
Regis Prevotf8bf1342006-01-11 23:31:53 -02001274 MAKE_EXTENSION_PCI(kncxs, 0x1894, 0x0014),
1275 MAKE_EXTENSION_PCI(kncxs, 0x1894, 0x0016),
Thilo Berger36f4f332006-02-27 00:09:08 -03001276 MAKE_EXTENSION_PCI(satewpls, 0x1894, 0x001e),
Lothar Englisch60110ce2006-06-06 16:13:46 -03001277 MAKE_EXTENSION_PCI(satewpls1, 0x1894, 0x001a),
Thomas Hammc01d1e42006-11-17 07:12:58 -03001278 MAKE_EXTENSION_PCI(satewplc, 0x1894, 0x002a),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 MAKE_EXTENSION_PCI(knc1c, 0x1894, 0x0020),
Johannes Stezenbach2d4f2c22005-05-16 21:54:23 -07001280 MAKE_EXTENSION_PCI(knc1cp, 0x1894, 0x0021),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 MAKE_EXTENSION_PCI(knc1t, 0x1894, 0x0030),
Johannes Stezenbach2d4f2c22005-05-16 21:54:23 -07001282 MAKE_EXTENSION_PCI(knc1tp, 0x1894, 0x0031),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 MAKE_EXTENSION_PCI(cin1200s, 0x153b, 0x1154),
Regis Prevotf8bf1342006-01-11 23:31:53 -02001284 MAKE_EXTENSION_PCI(cin1200sn, 0x153b, 0x1155),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 MAKE_EXTENSION_PCI(cin1200c, 0x153b, 0x1156),
1286 MAKE_EXTENSION_PCI(cin1200t, 0x153b, 0x1157),
1287 {
1288 .vendor = 0,
1289 }
1290};
1291
1292MODULE_DEVICE_TABLE(pci, pci_tbl);
1293
1294static struct saa7146_extension budget_extension = {
Julian Scheel27b05fd2005-07-12 13:58:39 -07001295 .name = "budget_av",
Oliver Endriss00c4cc62006-11-01 13:09:51 -03001296 .flags = SAA7146_USE_I2C_IRQ,
Oliver Endriss69459f32005-12-01 00:51:48 -08001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 .pci_tbl = pci_tbl,
1299
1300 .module = THIS_MODULE,
1301 .attach = budget_av_attach,
1302 .detach = budget_av_detach,
1303
1304 .irq_mask = MASK_10,
1305 .irq_func = budget_av_irq,
1306};
1307
1308static int __init budget_av_init(void)
1309{
1310 return saa7146_register_extension(&budget_extension);
1311}
1312
1313static void __exit budget_av_exit(void)
1314{
1315 saa7146_unregister_extension(&budget_extension);
1316}
1317
1318module_init(budget_av_init);
1319module_exit(budget_av_exit);
1320
1321MODULE_LICENSE("GPL");
1322MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, Michael Hunold, others");
1323MODULE_DESCRIPTION("driver for the SAA7146 based so-called "
1324 "budget PCI DVB w/ analog input and CI-module (e.g. the KNC cards)");