2 * budget-patch.c: driver for Budget Patch,
3 * hardware modification of DVB-S cards enabling full TS
5 * Written by Emard <emard@softhome.net>
7 * Original idea by Roberto Deza <rdeza@unav.es>
9 * Special thanks to Holger Waechtler, Michael Hunold, Marian Durkovic
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
30 * the project's page is at http://www.linuxtv.org/dvb/
34 #include "av7110_hw.h"
40 #define budget_patch budget
42 static struct saa7146_extension budget_extension;
44 MAKE_BUDGET_INFO(fs_1_3,"Siemens/Technotrend/Hauppauge PCI rev1.3+Budget_Patch", BUDGET_PATCH);
46 static struct pci_device_id pci_tbl[] = {
47 MAKE_EXTENSION_PCI(fs_1_3,0x13c2, 0x0000),
53 static int budget_wdebi(struct budget_patch *budget, u32 config, int addr, u32 val, int count)
55 struct saa7146_dev *dev=budget->dev;
57 dprintk(2, "budget: %p\n", budget);
59 if (count <= 0 || count > 4)
62 saa7146_write(dev, DEBI_CONFIG, config);
64 saa7146_write(dev, DEBI_AD, val );
65 saa7146_write(dev, DEBI_COMMAND, (count << 17) | (addr & 0xffff));
66 saa7146_write(dev, MC2, (2 << 16) | 2);
73 static int budget_av7110_send_fw_cmd(struct budget_patch *budget, u16* buf, int length)
77 dprintk(2, "budget: %p\n", budget);
79 for (i = 2; i < length; i++)
80 budget_wdebi(budget, DEBINOSWAP, COMMAND + 2*i, (u32) buf[i], 2);
83 budget_wdebi(budget, DEBINOSWAP, COMMAND + 2, (u32) buf[1], 2);
85 budget_wdebi(budget, DEBINOSWAP, COMMAND + 2, 0, 2);
87 budget_wdebi(budget, DEBINOSWAP, COMMAND, (u32) buf[0], 2);
92 static void av7110_set22k(struct budget_patch *budget, int state)
94 u16 buf[2] = {( COMTYPE_AUDIODAC << 8) | (state ? ON22K : OFF22K), 0};
96 dprintk(2, "budget: %p\n", budget);
97 budget_av7110_send_fw_cmd(budget, buf, 2);
101 static int av7110_send_diseqc_msg(struct budget_patch *budget, int len, u8 *msg, int burst)
104 u16 buf[18] = { ((COMTYPE_AUDIODAC << 8) | SendDiSEqC),
105 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
107 dprintk(2, "budget: %p\n", budget);
116 buf[3]=burst ? 0x01 : 0x00;
120 for (i=0; i<len; i++)
123 budget_av7110_send_fw_cmd(budget, buf, 18);
127 static int budget_patch_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
129 struct budget_patch* budget = (struct budget_patch*) fe->dvb->priv;
133 av7110_set22k (budget, 1);
136 av7110_set22k (budget, 0);
145 static int budget_patch_diseqc_send_master_cmd(struct dvb_frontend* fe, struct dvb_diseqc_master_cmd* cmd)
147 struct budget_patch* budget = (struct budget_patch*) fe->dvb->priv;
149 av7110_send_diseqc_msg (budget, cmd->msg_len, cmd->msg, 0);
154 static int budget_patch_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t minicmd)
156 struct budget_patch* budget = (struct budget_patch*) fe->dvb->priv;
158 av7110_send_diseqc_msg (budget, 0, NULL, minicmd);
163 static int alps_bsrv2_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
165 struct budget_patch* budget = (struct budget_patch*) fe->dvb->priv;
168 struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = buf, .len = sizeof(buf) };
169 u32 div = (params->frequency + 479500) / 125;
171 if (params->frequency > 2000000) pwr = 3;
172 else if (params->frequency > 1800000) pwr = 2;
173 else if (params->frequency > 1600000) pwr = 1;
174 else if (params->frequency > 1200000) pwr = 0;
175 else if (params->frequency >= 1100000) pwr = 1;
178 buf[0] = (div >> 8) & 0x7f;
180 buf[2] = ((div & 0x18000) >> 10) | 0x95;
181 buf[3] = (pwr << 6) | 0x30;
183 // NOTE: since we're using a prescaler of 2, we set the
184 // divisor frequency to 62.5kHz and divide by 125 above
186 if (i2c_transfer (&budget->i2c_adap, &msg, 1) != 1) return -EIO;
190 static struct ves1x93_config alps_bsrv2_config = {
191 .demod_address = 0x08,
194 .pll_set = alps_bsrv2_pll_set,
197 static u8 alps_bsru6_inittab[] = {
201 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
202 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
203 0x06, 0x40, /* DAC not used, set to high impendance mode */
204 0x07, 0x00, /* DAC LSB */
205 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
206 0x09, 0x00, /* FIFO */
207 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
208 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
209 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
210 0x10, 0x3f, // AGC2 0x3d
212 0x12, 0xb5, // Lock detect: -64 Carrier freq detect:on
213 0x15, 0xc9, // lock detector threshold
224 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
225 0x29, 0x1e, // 1/2 threshold
226 0x2a, 0x14, // 2/3 threshold
227 0x2b, 0x0f, // 3/4 threshold
228 0x2c, 0x09, // 5/6 threshold
229 0x2d, 0x05, // 7/8 threshold
231 0x31, 0x1f, // test all FECs
232 0x32, 0x19, // viterbi and synchro search
233 0x33, 0xfc, // rs control
234 0x34, 0x93, // error control
239 static int alps_bsru6_set_symbol_rate(struct dvb_frontend* fe, u32 srate, u32 ratio)
244 if (srate < 1500000) { aclk = 0xb7; bclk = 0x47; }
245 else if (srate < 3000000) { aclk = 0xb7; bclk = 0x4b; }
246 else if (srate < 7000000) { aclk = 0xb7; bclk = 0x4f; }
247 else if (srate < 14000000) { aclk = 0xb7; bclk = 0x53; }
248 else if (srate < 30000000) { aclk = 0xb6; bclk = 0x53; }
249 else if (srate < 45000000) { aclk = 0xb4; bclk = 0x51; }
251 stv0299_writereg (fe, 0x13, aclk);
252 stv0299_writereg (fe, 0x14, bclk);
253 stv0299_writereg (fe, 0x1f, (ratio >> 16) & 0xff);
254 stv0299_writereg (fe, 0x20, (ratio >> 8) & 0xff);
255 stv0299_writereg (fe, 0x21, (ratio ) & 0xf0);
260 static int alps_bsru6_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
262 struct budget_patch* budget = (struct budget_patch*) fe->dvb->priv;
265 struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = data, .len = sizeof(data) };
267 if ((params->frequency < 950000) || (params->frequency > 2150000)) return -EINVAL;
269 div = (params->frequency + (125 - 1)) / 125; // round correctly
270 data[0] = (div >> 8) & 0x7f;
271 data[1] = div & 0xff;
272 data[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
275 if (params->frequency > 1530000) data[3] = 0xc0;
277 if (i2c_transfer (&budget->i2c_adap, &msg, 1) != 1) return -EIO;
281 static struct stv0299_config alps_bsru6_config = {
283 .demod_address = 0x68,
284 .inittab = alps_bsru6_inittab,
287 .enhanced_tuning = 0,
289 .lock_output = STV0229_LOCKOUTPUT_1,
290 .volt13_op0_op1 = STV0299_VOLT13_OP1,
292 .set_symbol_rate = alps_bsru6_set_symbol_rate,
293 .pll_set = alps_bsru6_pll_set,
296 static int grundig_29504_451_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
298 struct budget_patch* budget = (struct budget_patch*) fe->dvb->priv;
301 struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = data, .len = sizeof(data) };
303 div = params->frequency / 125;
304 data[0] = (div >> 8) & 0x7f;
305 data[1] = div & 0xff;
309 if (i2c_transfer (&budget->i2c_adap, &msg, 1) != 1) return -EIO;
313 struct tda8083_config grundig_29504_451_config = {
314 .demod_address = 0x68,
315 .pll_set = grundig_29504_451_pll_set,
318 static void frontend_init(struct budget_patch* budget)
320 switch(budget->dev->pci->subsystem_device) {
321 case 0x0000: // Hauppauge/TT WinTV DVB-S rev1.X
323 // try the ALPS BSRV2 first of all
324 budget->dvb_frontend = ves1x93_attach(&alps_bsrv2_config, &budget->i2c_adap);
325 if (budget->dvb_frontend) {
326 budget->dvb_frontend->ops->diseqc_send_master_cmd = budget_patch_diseqc_send_master_cmd;
327 budget->dvb_frontend->ops->diseqc_send_burst = budget_patch_diseqc_send_burst;
328 budget->dvb_frontend->ops->set_tone = budget_patch_set_tone;
332 // try the ALPS BSRU6 now
333 budget->dvb_frontend = stv0299_attach(&alps_bsru6_config, &budget->i2c_adap);
334 if (budget->dvb_frontend) {
335 budget->dvb_frontend->ops->diseqc_send_master_cmd = budget_patch_diseqc_send_master_cmd;
336 budget->dvb_frontend->ops->diseqc_send_burst = budget_patch_diseqc_send_burst;
337 budget->dvb_frontend->ops->set_tone = budget_patch_set_tone;
341 // Try the grundig 29504-451
342 budget->dvb_frontend = tda8083_attach(&grundig_29504_451_config, &budget->i2c_adap);
343 if (budget->dvb_frontend) {
344 budget->dvb_frontend->ops->diseqc_send_master_cmd = budget_patch_diseqc_send_master_cmd;
345 budget->dvb_frontend->ops->diseqc_send_burst = budget_patch_diseqc_send_burst;
346 budget->dvb_frontend->ops->set_tone = budget_patch_set_tone;
352 if (budget->dvb_frontend == NULL) {
353 printk("dvb-ttpci: A frontend driver was not found for device %04x/%04x subsystem %04x/%04x\n",
354 budget->dev->pci->vendor,
355 budget->dev->pci->device,
356 budget->dev->pci->subsystem_vendor,
357 budget->dev->pci->subsystem_device);
359 if (dvb_register_frontend(budget->dvb_adapter, budget->dvb_frontend)) {
360 printk("budget-av: Frontend registration failed!\n");
361 if (budget->dvb_frontend->ops->release)
362 budget->dvb_frontend->ops->release(budget->dvb_frontend);
363 budget->dvb_frontend = NULL;
368 static int budget_patch_attach (struct saa7146_dev* dev, struct saa7146_pci_extension_data *info)
370 struct budget_patch *budget;
374 if (!(budget = kmalloc (sizeof(struct budget_patch), GFP_KERNEL)))
377 dprintk(2, "budget: %p\n", budget);
379 if ((err = ttpci_budget_init (budget, dev, info, THIS_MODULE))) {
385 ** This code will setup the SAA7146_RPS1 to generate a square
386 ** wave on GPIO3, changing when a field (TS_HEIGHT/2 "lines" of
387 ** TS_WIDTH packets) has been acquired on SAA7146_D1B video port;
388 ** then, this GPIO3 output which is connected to the D1B_VSYNC
389 ** input, will trigger the acquisition of the alternate field
391 ** Currently, the TT_budget / WinTV_Nova cards have two ICs
392 ** (74HCT4040, LVC74) for the generation of this VSYNC signal,
393 ** which seems that can be done perfectly without this :-)).
396 // Setup RPS1 "program" (p35)
398 // Wait reset Source Line Counter Threshold (p36)
399 WRITE_RPS1(cpu_to_le32(CMD_PAUSE | RPS_INV | EVT_HS));
400 // Wait Source Line Counter Threshold (p36)
401 WRITE_RPS1(cpu_to_le32(CMD_PAUSE | EVT_HS));
403 WRITE_RPS1(cpu_to_le32(CMD_WR_REG_MASK | (GPIO_CTRL>>2)));
404 WRITE_RPS1(cpu_to_le32(GPIO3_MSK));
405 WRITE_RPS1(cpu_to_le32(SAA7146_GPIO_OUTHI<<24));
406 // Wait reset Source Line Counter Threshold (p36)
407 WRITE_RPS1(cpu_to_le32(CMD_PAUSE | RPS_INV | EVT_HS));
408 // Wait Source Line Counter Threshold
409 WRITE_RPS1(cpu_to_le32(CMD_PAUSE | EVT_HS));
411 WRITE_RPS1(cpu_to_le32(CMD_WR_REG_MASK | (GPIO_CTRL>>2)));
412 WRITE_RPS1(cpu_to_le32(GPIO3_MSK));
413 WRITE_RPS1(cpu_to_le32(SAA7146_GPIO_OUTLO<<24));
414 // Jump to begin of RPS program (p37)
415 WRITE_RPS1(cpu_to_le32(CMD_JUMP));
416 WRITE_RPS1(cpu_to_le32(dev->d_rps1.dma_handle));
419 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
420 // Set RPS1 Address register to point to RPS code (r108 p42)
421 saa7146_write(dev, RPS_ADDR1, dev->d_rps1.dma_handle);
422 // Set Source Line Counter Threshold, using BRS (rCC p43)
423 saa7146_write(dev, RPS_THRESH1, ((TS_HEIGHT/2) | MASK_12));
424 // Enable RPS1 (rFC p33)
425 saa7146_write(dev, MC1, (MASK_13 | MASK_29));
427 dev->ext_priv = budget;
429 budget->dvb_adapter->priv = budget;
430 frontend_init(budget);
436 static int budget_patch_detach (struct saa7146_dev* dev)
438 struct budget_patch *budget = (struct budget_patch*) dev->ext_priv;
441 if (budget->dvb_frontend) dvb_unregister_frontend(budget->dvb_frontend);
443 err = ttpci_budget_deinit (budget);
451 static int __init budget_patch_init(void)
453 return saa7146_register_extension(&budget_extension);
456 static void __exit budget_patch_exit(void)
458 saa7146_unregister_extension(&budget_extension);
462 static struct saa7146_extension budget_extension = {
463 .name = "budget_patch dvb\0",
466 .module = THIS_MODULE,
468 .attach = budget_patch_attach,
469 .detach = budget_patch_detach,
472 .irq_func = ttpci_budget_irq10_handler,
476 module_init(budget_patch_init);
477 module_exit(budget_patch_exit);
479 MODULE_LICENSE("GPL");
480 MODULE_AUTHOR("Emard, Roberto Deza, Holger Waechtler, Michael Hunold, others");
481 MODULE_DESCRIPTION("Driver for full TS modified DVB-S SAA7146+AV7110 "
482 "based so-called Budget Patch cards");