budget-patch.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /*
  2. * budget-patch.c: driver for Budget Patch,
  3. * hardware modification of DVB-S cards enabling full TS
  4. *
  5. * Written by Emard <emard@softhome.net>
  6. *
  7. * Original idea by Roberto Deza <rdeza@unav.es>
  8. *
  9. * Special thanks to Holger Waechtler, Michael Hunold, Marian Durkovic
  10. * and Metzlerbros
  11. *
  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.
  16. *
  17. *
  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.
  22. *
  23. *
  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
  28. *
  29. *
  30. * the project's page is at http://www.linuxtv.org/dvb/
  31. */
  32. #include "av7110.h"
  33. #include "av7110_hw.h"
  34. #include "budget.h"
  35. #include "stv0299.h"
  36. #include "ves1x93.h"
  37. #include "tda8083.h"
  38. #define budget_patch budget
  39. static struct saa7146_extension budget_extension;
  40. MAKE_BUDGET_INFO(ttbp, "TT-Budget/Patch DVB-S 1.x PCI", BUDGET_PATCH);
  41. //MAKE_BUDGET_INFO(satel,"TT-Budget/Patch SATELCO PCI", BUDGET_TT_HW_DISEQC);
  42. static struct pci_device_id pci_tbl[] = {
  43. MAKE_EXTENSION_PCI(ttbp,0x13c2, 0x0000),
  44. // MAKE_EXTENSION_PCI(satel, 0x13c2, 0x1013),
  45. {
  46. .vendor = 0,
  47. }
  48. };
  49. /* those lines are for budget-patch to be tried
  50. ** on a true budget card and observe the
  51. ** behaviour of VSYNC generated by rps1.
  52. ** this code was shamelessly copy/pasted from budget.c
  53. */
  54. static void gpio_Set22K (struct budget *budget, int state)
  55. {
  56. struct saa7146_dev *dev=budget->dev;
  57. dprintk(2, "budget: %p\n", budget);
  58. saa7146_setgpio(dev, 3, (state ? SAA7146_GPIO_OUTHI : SAA7146_GPIO_OUTLO));
  59. }
  60. /* Diseqc functions only for TT Budget card */
  61. /* taken from the Skyvision DVB driver by
  62. Ralph Metzler <rjkm@metzlerbros.de> */
  63. static void DiseqcSendBit (struct budget *budget, int data)
  64. {
  65. struct saa7146_dev *dev=budget->dev;
  66. dprintk(2, "budget: %p\n", budget);
  67. saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTHI);
  68. udelay(data ? 500 : 1000);
  69. saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
  70. udelay(data ? 1000 : 500);
  71. }
  72. static void DiseqcSendByte (struct budget *budget, int data)
  73. {
  74. int i, par=1, d;
  75. dprintk(2, "budget: %p\n", budget);
  76. for (i=7; i>=0; i--) {
  77. d = (data>>i)&1;
  78. par ^= d;
  79. DiseqcSendBit(budget, d);
  80. }
  81. DiseqcSendBit(budget, par);
  82. }
  83. static int SendDiSEqCMsg (struct budget *budget, int len, u8 *msg, unsigned long burst)
  84. {
  85. struct saa7146_dev *dev=budget->dev;
  86. int i;
  87. dprintk(2, "budget: %p\n", budget);
  88. saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
  89. mdelay(16);
  90. for (i=0; i<len; i++)
  91. DiseqcSendByte(budget, msg[i]);
  92. mdelay(16);
  93. if (burst!=-1) {
  94. if (burst)
  95. DiseqcSendByte(budget, 0xff);
  96. else {
  97. saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTHI);
  98. udelay(12500);
  99. saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
  100. }
  101. msleep(20);
  102. }
  103. return 0;
  104. }
  105. /* shamelessly copy/pasted from budget.c
  106. */
  107. static int budget_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
  108. {
  109. struct budget* budget = (struct budget*) fe->dvb->priv;
  110. switch (tone) {
  111. case SEC_TONE_ON:
  112. gpio_Set22K (budget, 1);
  113. break;
  114. case SEC_TONE_OFF:
  115. gpio_Set22K (budget, 0);
  116. break;
  117. default:
  118. return -EINVAL;
  119. }
  120. return 0;
  121. }
  122. static int budget_diseqc_send_master_cmd(struct dvb_frontend* fe, struct dvb_diseqc_master_cmd* cmd)
  123. {
  124. struct budget* budget = (struct budget*) fe->dvb->priv;
  125. SendDiSEqCMsg (budget, cmd->msg_len, cmd->msg, 0);
  126. return 0;
  127. }
  128. static int budget_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t minicmd)
  129. {
  130. struct budget* budget = (struct budget*) fe->dvb->priv;
  131. SendDiSEqCMsg (budget, 0, NULL, minicmd);
  132. return 0;
  133. }
  134. static int budget_av7110_send_fw_cmd(struct budget_patch *budget, u16* buf, int length)
  135. {
  136. int i;
  137. dprintk(2, "budget: %p\n", budget);
  138. for (i = 2; i < length; i++)
  139. {
  140. ttpci_budget_debiwrite(budget, DEBINOSWAP, COMMAND + 2*i, 2, (u32) buf[i], 0,0);
  141. msleep(5);
  142. }
  143. if (length)
  144. ttpci_budget_debiwrite(budget, DEBINOSWAP, COMMAND + 2, 2, (u32) buf[1], 0,0);
  145. else
  146. ttpci_budget_debiwrite(budget, DEBINOSWAP, COMMAND + 2, 2, 0, 0,0);
  147. msleep(5);
  148. ttpci_budget_debiwrite(budget, DEBINOSWAP, COMMAND, 2, (u32) buf[0], 0,0);
  149. msleep(5);
  150. return 0;
  151. }
  152. static void av7110_set22k(struct budget_patch *budget, int state)
  153. {
  154. u16 buf[2] = {( COMTYPE_AUDIODAC << 8) | (state ? ON22K : OFF22K), 0};
  155. dprintk(2, "budget: %p\n", budget);
  156. budget_av7110_send_fw_cmd(budget, buf, 2);
  157. }
  158. static int av7110_send_diseqc_msg(struct budget_patch *budget, int len, u8 *msg, int burst)
  159. {
  160. int i;
  161. u16 buf[18] = { ((COMTYPE_AUDIODAC << 8) | SendDiSEqC),
  162. 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  163. dprintk(2, "budget: %p\n", budget);
  164. if (len>10)
  165. len=10;
  166. buf[1] = len+2;
  167. buf[2] = len;
  168. if (burst != -1)
  169. buf[3]=burst ? 0x01 : 0x00;
  170. else
  171. buf[3]=0xffff;
  172. for (i=0; i<len; i++)
  173. buf[i+4]=msg[i];
  174. budget_av7110_send_fw_cmd(budget, buf, 18);
  175. return 0;
  176. }
  177. static int budget_patch_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
  178. {
  179. struct budget_patch* budget = (struct budget_patch*) fe->dvb->priv;
  180. switch (tone) {
  181. case SEC_TONE_ON:
  182. av7110_set22k (budget, 1);
  183. break;
  184. case SEC_TONE_OFF:
  185. av7110_set22k (budget, 0);
  186. break;
  187. default:
  188. return -EINVAL;
  189. }
  190. return 0;
  191. }
  192. static int budget_patch_diseqc_send_master_cmd(struct dvb_frontend* fe, struct dvb_diseqc_master_cmd* cmd)
  193. {
  194. struct budget_patch* budget = (struct budget_patch*) fe->dvb->priv;
  195. av7110_send_diseqc_msg (budget, cmd->msg_len, cmd->msg, 0);
  196. return 0;
  197. }
  198. static int budget_patch_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t minicmd)
  199. {
  200. struct budget_patch* budget = (struct budget_patch*) fe->dvb->priv;
  201. av7110_send_diseqc_msg (budget, 0, NULL, minicmd);
  202. return 0;
  203. }
  204. static int alps_bsrv2_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
  205. {
  206. struct budget_patch* budget = (struct budget_patch*) fe->dvb->priv;
  207. u8 pwr = 0;
  208. u8 buf[4];
  209. struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = buf, .len = sizeof(buf) };
  210. u32 div = (params->frequency + 479500) / 125;
  211. if (params->frequency > 2000000) pwr = 3;
  212. else if (params->frequency > 1800000) pwr = 2;
  213. else if (params->frequency > 1600000) pwr = 1;
  214. else if (params->frequency > 1200000) pwr = 0;
  215. else if (params->frequency >= 1100000) pwr = 1;
  216. else pwr = 2;
  217. buf[0] = (div >> 8) & 0x7f;
  218. buf[1] = div & 0xff;
  219. buf[2] = ((div & 0x18000) >> 10) | 0x95;
  220. buf[3] = (pwr << 6) | 0x30;
  221. // NOTE: since we're using a prescaler of 2, we set the
  222. // divisor frequency to 62.5kHz and divide by 125 above
  223. if (i2c_transfer (&budget->i2c_adap, &msg, 1) != 1) return -EIO;
  224. return 0;
  225. }
  226. static struct ves1x93_config alps_bsrv2_config = {
  227. .demod_address = 0x08,
  228. .xin = 90100000UL,
  229. .invert_pwm = 0,
  230. .pll_set = alps_bsrv2_pll_set,
  231. };
  232. static u8 alps_bsru6_inittab[] = {
  233. 0x01, 0x15,
  234. 0x02, 0x00,
  235. 0x03, 0x00,
  236. 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
  237. 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
  238. 0x06, 0x40, /* DAC not used, set to high impendance mode */
  239. 0x07, 0x00, /* DAC LSB */
  240. 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
  241. 0x09, 0x00, /* FIFO */
  242. 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
  243. 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
  244. 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
  245. 0x10, 0x3f, // AGC2 0x3d
  246. 0x11, 0x84,
  247. 0x12, 0xb5, // Lock detect: -64 Carrier freq detect:on
  248. 0x15, 0xc9, // lock detector threshold
  249. 0x16, 0x00,
  250. 0x17, 0x00,
  251. 0x18, 0x00,
  252. 0x19, 0x00,
  253. 0x1a, 0x00,
  254. 0x1f, 0x50,
  255. 0x20, 0x00,
  256. 0x21, 0x00,
  257. 0x22, 0x00,
  258. 0x23, 0x00,
  259. 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
  260. 0x29, 0x1e, // 1/2 threshold
  261. 0x2a, 0x14, // 2/3 threshold
  262. 0x2b, 0x0f, // 3/4 threshold
  263. 0x2c, 0x09, // 5/6 threshold
  264. 0x2d, 0x05, // 7/8 threshold
  265. 0x2e, 0x01,
  266. 0x31, 0x1f, // test all FECs
  267. 0x32, 0x19, // viterbi and synchro search
  268. 0x33, 0xfc, // rs control
  269. 0x34, 0x93, // error control
  270. 0x0f, 0x52,
  271. 0xff, 0xff
  272. };
  273. static int alps_bsru6_set_symbol_rate(struct dvb_frontend* fe, u32 srate, u32 ratio)
  274. {
  275. u8 aclk = 0;
  276. u8 bclk = 0;
  277. if (srate < 1500000) { aclk = 0xb7; bclk = 0x47; }
  278. else if (srate < 3000000) { aclk = 0xb7; bclk = 0x4b; }
  279. else if (srate < 7000000) { aclk = 0xb7; bclk = 0x4f; }
  280. else if (srate < 14000000) { aclk = 0xb7; bclk = 0x53; }
  281. else if (srate < 30000000) { aclk = 0xb6; bclk = 0x53; }
  282. else if (srate < 45000000) { aclk = 0xb4; bclk = 0x51; }
  283. stv0299_writereg (fe, 0x13, aclk);
  284. stv0299_writereg (fe, 0x14, bclk);
  285. stv0299_writereg (fe, 0x1f, (ratio >> 16) & 0xff);
  286. stv0299_writereg (fe, 0x20, (ratio >> 8) & 0xff);
  287. stv0299_writereg (fe, 0x21, (ratio ) & 0xf0);
  288. return 0;
  289. }
  290. static int alps_bsru6_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
  291. {
  292. struct budget_patch* budget = (struct budget_patch*) fe->dvb->priv;
  293. u8 data[4];
  294. u32 div;
  295. struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = data, .len = sizeof(data) };
  296. if ((params->frequency < 950000) || (params->frequency > 2150000)) return -EINVAL;
  297. div = (params->frequency + (125 - 1)) / 125; // round correctly
  298. data[0] = (div >> 8) & 0x7f;
  299. data[1] = div & 0xff;
  300. data[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
  301. data[3] = 0xC4;
  302. if (params->frequency > 1530000) data[3] = 0xc0;
  303. if (i2c_transfer (&budget->i2c_adap, &msg, 1) != 1) return -EIO;
  304. return 0;
  305. }
  306. static struct stv0299_config alps_bsru6_config = {
  307. .demod_address = 0x68,
  308. .inittab = alps_bsru6_inittab,
  309. .mclk = 88000000UL,
  310. .invert = 1,
  311. .enhanced_tuning = 0,
  312. .skip_reinit = 0,
  313. .lock_output = STV0229_LOCKOUTPUT_1,
  314. .volt13_op0_op1 = STV0299_VOLT13_OP1,
  315. .min_delay_ms = 100,
  316. .set_symbol_rate = alps_bsru6_set_symbol_rate,
  317. .pll_set = alps_bsru6_pll_set,
  318. };
  319. static int grundig_29504_451_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
  320. {
  321. struct budget_patch* budget = (struct budget_patch*) fe->dvb->priv;
  322. u32 div;
  323. u8 data[4];
  324. struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = data, .len = sizeof(data) };
  325. div = params->frequency / 125;
  326. data[0] = (div >> 8) & 0x7f;
  327. data[1] = div & 0xff;
  328. data[2] = 0x8e;
  329. data[3] = 0x00;
  330. if (i2c_transfer (&budget->i2c_adap, &msg, 1) != 1) return -EIO;
  331. return 0;
  332. }
  333. static struct tda8083_config grundig_29504_451_config = {
  334. .demod_address = 0x68,
  335. .pll_set = grundig_29504_451_pll_set,
  336. };
  337. static void frontend_init(struct budget_patch* budget)
  338. {
  339. switch(budget->dev->pci->subsystem_device) {
  340. case 0x0000: // Hauppauge/TT WinTV DVB-S rev1.X
  341. case 0x1013: // SATELCO Multimedia PCI
  342. // try the ALPS BSRV2 first of all
  343. budget->dvb_frontend = ves1x93_attach(&alps_bsrv2_config, &budget->i2c_adap);
  344. if (budget->dvb_frontend) {
  345. budget->dvb_frontend->ops->diseqc_send_master_cmd = budget_patch_diseqc_send_master_cmd;
  346. budget->dvb_frontend->ops->diseqc_send_burst = budget_patch_diseqc_send_burst;
  347. budget->dvb_frontend->ops->set_tone = budget_patch_set_tone;
  348. break;
  349. }
  350. // try the ALPS BSRU6 now
  351. budget->dvb_frontend = stv0299_attach(&alps_bsru6_config, &budget->i2c_adap);
  352. if (budget->dvb_frontend) {
  353. budget->dvb_frontend->ops->diseqc_send_master_cmd = budget_diseqc_send_master_cmd;
  354. budget->dvb_frontend->ops->diseqc_send_burst = budget_diseqc_send_burst;
  355. budget->dvb_frontend->ops->set_tone = budget_set_tone;
  356. break;
  357. }
  358. // Try the grundig 29504-451
  359. budget->dvb_frontend = tda8083_attach(&grundig_29504_451_config, &budget->i2c_adap);
  360. if (budget->dvb_frontend) {
  361. budget->dvb_frontend->ops->diseqc_send_master_cmd = budget_diseqc_send_master_cmd;
  362. budget->dvb_frontend->ops->diseqc_send_burst = budget_diseqc_send_burst;
  363. budget->dvb_frontend->ops->set_tone = budget_set_tone;
  364. break;
  365. }
  366. break;
  367. }
  368. if (budget->dvb_frontend == NULL) {
  369. printk("dvb-ttpci: A frontend driver was not found for device %04x/%04x subsystem %04x/%04x\n",
  370. budget->dev->pci->vendor,
  371. budget->dev->pci->device,
  372. budget->dev->pci->subsystem_vendor,
  373. budget->dev->pci->subsystem_device);
  374. } else {
  375. if (dvb_register_frontend(&budget->dvb_adapter, budget->dvb_frontend)) {
  376. printk("budget-av: Frontend registration failed!\n");
  377. if (budget->dvb_frontend->ops->release)
  378. budget->dvb_frontend->ops->release(budget->dvb_frontend);
  379. budget->dvb_frontend = NULL;
  380. }
  381. }
  382. }
  383. /* written by Emard */
  384. static int budget_patch_attach (struct saa7146_dev* dev, struct saa7146_pci_extension_data *info)
  385. {
  386. struct budget_patch *budget;
  387. int err;
  388. int count = 0;
  389. int detected = 0;
  390. #define PATCH_RESET 0
  391. #define RPS_IRQ 0
  392. #define HPS_SETUP 0
  393. #if PATCH_RESET
  394. saa7146_write(dev, MC1, MASK_31);
  395. msleep(40);
  396. #endif
  397. #if HPS_SETUP
  398. // initialize registers. Better to have it like this
  399. // than leaving something unconfigured
  400. saa7146_write(dev, DD1_STREAM_B, 0);
  401. // port B VSYNC at rising edge
  402. saa7146_write(dev, DD1_INIT, 0x00000200); // have this in budget-core too!
  403. saa7146_write(dev, BRS_CTRL, 0x00000000); // VBI
  404. // debi config
  405. // saa7146_write(dev, DEBI_CONFIG, MASK_30|MASK_28|MASK_18);
  406. // zero all HPS registers
  407. saa7146_write(dev, HPS_H_PRESCALE, 0); // r68
  408. saa7146_write(dev, HPS_H_SCALE, 0); // r6c
  409. saa7146_write(dev, BCS_CTRL, 0); // r70
  410. saa7146_write(dev, HPS_V_SCALE, 0); // r60
  411. saa7146_write(dev, HPS_V_GAIN, 0); // r64
  412. saa7146_write(dev, CHROMA_KEY_RANGE, 0); // r74
  413. saa7146_write(dev, CLIP_FORMAT_CTRL, 0); // r78
  414. // Set HPS prescaler for port B input
  415. saa7146_write(dev, HPS_CTRL, (1<<30) | (0<<29) | (1<<28) | (0<<12) );
  416. saa7146_write(dev, MC2,
  417. 0 * (MASK_08 | MASK_24) | // BRS control
  418. 0 * (MASK_09 | MASK_25) | // a
  419. 0 * (MASK_10 | MASK_26) | // b
  420. 1 * (MASK_06 | MASK_22) | // HPS_CTRL1
  421. 1 * (MASK_05 | MASK_21) | // HPS_CTRL2
  422. 0 * (MASK_01 | MASK_15) // DEBI
  423. );
  424. #endif
  425. // Disable RPS1 and RPS0
  426. saa7146_write(dev, MC1, ( MASK_29 | MASK_28));
  427. // RPS1 timeout disable
  428. saa7146_write(dev, RPS_TOV1, 0);
  429. // code for autodetection
  430. // will wait for VBI_B event (vertical blank at port B)
  431. // and will reset GPIO3 after VBI_B is detected.
  432. // (GPIO3 should be raised high by CPU to
  433. // test if GPIO3 will generate vertical blank signal
  434. // in budget patch GPIO3 is connected to VSYNC_B
  435. count = 0;
  436. #if 0
  437. WRITE_RPS1(cpu_to_le32(CMD_UPLOAD |
  438. MASK_10 | MASK_09 | MASK_08 | MASK_06 | MASK_05 | MASK_04 | MASK_03 | MASK_02 ));
  439. #endif
  440. WRITE_RPS1(cpu_to_le32(CMD_PAUSE | EVT_VBI_B));
  441. WRITE_RPS1(cpu_to_le32(CMD_WR_REG_MASK | (GPIO_CTRL>>2)));
  442. WRITE_RPS1(cpu_to_le32(GPIO3_MSK));
  443. WRITE_RPS1(cpu_to_le32(SAA7146_GPIO_OUTLO<<24));
  444. #if RPS_IRQ
  445. // issue RPS1 interrupt to increment counter
  446. WRITE_RPS1(cpu_to_le32(CMD_INTERRUPT));
  447. // at least a NOP is neede between two interrupts
  448. WRITE_RPS1(cpu_to_le32(CMD_NOP));
  449. // interrupt again
  450. WRITE_RPS1(cpu_to_le32(CMD_INTERRUPT));
  451. #endif
  452. WRITE_RPS1(cpu_to_le32(CMD_STOP));
  453. #if RPS_IRQ
  454. // set event counter 1 source as RPS1 interrupt (0x03) (rE4 p53)
  455. // use 0x03 to track RPS1 interrupts - increase by 1 every gpio3 is toggled
  456. // use 0x15 to track VPE interrupts - increase by 1 every vpeirq() is called
  457. saa7146_write(dev, EC1SSR, (0x03<<2) | 3 );
  458. // set event counter 1 treshold to maximum allowed value (rEC p55)
  459. saa7146_write(dev, ECT1R, 0x3fff );
  460. #endif
  461. // Fix VSYNC level
  462. saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
  463. // Set RPS1 Address register to point to RPS code (r108 p42)
  464. saa7146_write(dev, RPS_ADDR1, dev->d_rps1.dma_handle);
  465. // Enable RPS1, (rFC p33)
  466. saa7146_write(dev, MC1, (MASK_13 | MASK_29 ));
  467. mdelay(50);
  468. saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTHI);
  469. mdelay(150);
  470. if( (saa7146_read(dev, GPIO_CTRL) & 0x10000000) == 0)
  471. detected = 1;
  472. #if RPS_IRQ
  473. printk("Event Counter 1 0x%04x\n", saa7146_read(dev, EC1R) & 0x3fff );
  474. #endif
  475. // Disable RPS1
  476. saa7146_write(dev, MC1, ( MASK_29 ));
  477. if(detected == 0)
  478. printk("budget-patch not detected or saa7146 in non-default state.\n"
  479. "try enabling ressetting of 7146 with MASK_31 in MC1 register\n");
  480. else
  481. printk("BUDGET-PATCH DETECTED.\n");
  482. /* OLD (Original design by Roberto Deza):
  483. ** This code will setup the SAA7146_RPS1 to generate a square
  484. ** wave on GPIO3, changing when a field (TS_HEIGHT/2 "lines" of
  485. ** TS_WIDTH packets) has been acquired on SAA7146_D1B video port;
  486. ** then, this GPIO3 output which is connected to the D1B_VSYNC
  487. ** input, will trigger the acquisition of the alternate field
  488. ** and so on.
  489. ** Currently, the TT_budget / WinTV_Nova cards have two ICs
  490. ** (74HCT4040, LVC74) for the generation of this VSYNC signal,
  491. ** which seems that can be done perfectly without this :-)).
  492. */
  493. /* New design (By Emard)
  494. ** this rps1 code will copy internal HS event to GPIO3 pin.
  495. ** GPIO3 is in budget-patch hardware connectd to port B VSYNC
  496. ** HS is an internal event of 7146, accessible with RPS
  497. ** and temporarily raised high every n lines
  498. ** (n in defined in the RPS_THRESH1 counter threshold)
  499. ** I think HS is raised high on the beginning of the n-th line
  500. ** and remains high until this n-th line that triggered
  501. ** it is completely received. When the receiption of n-th line
  502. ** ends, HS is lowered.
  503. ** To transmit data over DMA, 7146 needs changing state at
  504. ** port B VSYNC pin. Any changing of port B VSYNC will
  505. ** cause some DMA data transfer, with more or less packets loss.
  506. ** It depends on the phase and frequency of VSYNC and
  507. ** the way of 7146 is instructed to trigger on port B (defined
  508. ** in DD1_INIT register, 3rd nibble from the right valid
  509. ** numbers are 0-7, see datasheet)
  510. **
  511. ** The correct triggering can minimize packet loss,
  512. ** dvbtraffic should give this stable bandwidths:
  513. ** 22k transponder = 33814 kbit/s
  514. ** 27.5k transponder = 38045 kbit/s
  515. ** by experiment it is found that the best results
  516. ** (stable bandwidths and almost no packet loss)
  517. ** are obtained using DD1_INIT triggering number 2
  518. ** (Va at rising edge of VS Fa = HS x VS-failing forced toggle)
  519. ** and a VSYNC phase that occurs in the middle of DMA transfer
  520. ** (about byte 188*512=96256 in the DMA window).
  521. **
  522. ** Phase of HS is still not clear to me how to control,
  523. ** It just happens to be so. It can be seen if one enables
  524. ** RPS_IRQ and print Event Counter 1 in vpeirq(). Every
  525. ** time RPS_INTERRUPT is called, the Event Counter 1 will
  526. ** increment. That's how the 7146 is programmed to do event
  527. ** counting in this budget-patch.c
  528. ** I *think* HPS setting has something to do with the phase
  529. ** of HS but I cant be 100% sure in that.
  530. ** hardware debug note: a working budget card (including budget patch)
  531. ** with vpeirq() interrupt setup in mode "0x90" (every 64K) will
  532. ** generate 3 interrupts per 25-Hz DMA frame of 2*188*512 bytes
  533. ** and that means 3*25=75 Hz of interrupt freqency, as seen by
  534. ** watch cat /proc/interrupts
  535. **
  536. ** If this frequency is 3x lower (and data received in the DMA
  537. ** buffer don't start with 0x47, but in the middle of packets,
  538. ** whose lengths appear to be like 188 292 188 104 etc.
  539. ** this means VSYNC line is not connected in the hardware.
  540. ** (check soldering pcb and pins)
  541. ** The same behaviour of missing VSYNC can be duplicated on budget
  542. ** cards, by seting DD1_INIT trigger mode 7 in 3rd nibble.
  543. */
  544. // Setup RPS1 "program" (p35)
  545. count = 0;
  546. // Wait Source Line Counter Threshold (p36)
  547. WRITE_RPS1(cpu_to_le32(CMD_PAUSE | EVT_HS));
  548. // Set GPIO3=1 (p42)
  549. WRITE_RPS1(cpu_to_le32(CMD_WR_REG_MASK | (GPIO_CTRL>>2)));
  550. WRITE_RPS1(cpu_to_le32(GPIO3_MSK));
  551. WRITE_RPS1(cpu_to_le32(SAA7146_GPIO_OUTHI<<24));
  552. #if RPS_IRQ
  553. // issue RPS1 interrupt
  554. WRITE_RPS1(cpu_to_le32(CMD_INTERRUPT));
  555. #endif
  556. // Wait reset Source Line Counter Threshold (p36)
  557. WRITE_RPS1(cpu_to_le32(CMD_PAUSE | RPS_INV | EVT_HS));
  558. // Set GPIO3=0 (p42)
  559. WRITE_RPS1(cpu_to_le32(CMD_WR_REG_MASK | (GPIO_CTRL>>2)));
  560. WRITE_RPS1(cpu_to_le32(GPIO3_MSK));
  561. WRITE_RPS1(cpu_to_le32(SAA7146_GPIO_OUTLO<<24));
  562. #if RPS_IRQ
  563. // issue RPS1 interrupt
  564. WRITE_RPS1(cpu_to_le32(CMD_INTERRUPT));
  565. #endif
  566. // Jump to begin of RPS program (p37)
  567. WRITE_RPS1(cpu_to_le32(CMD_JUMP));
  568. WRITE_RPS1(cpu_to_le32(dev->d_rps1.dma_handle));
  569. // Fix VSYNC level
  570. saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
  571. // Set RPS1 Address register to point to RPS code (r108 p42)
  572. saa7146_write(dev, RPS_ADDR1, dev->d_rps1.dma_handle);
  573. // Set Source Line Counter Threshold, using BRS (rCC p43)
  574. // It generates HS event every TS_HEIGHT lines
  575. // this is related to TS_WIDTH set in register
  576. // NUM_LINE_BYTE3 in budget-core.c. If NUM_LINE_BYTE
  577. // low 16 bits are set to TS_WIDTH bytes (TS_WIDTH=2*188
  578. //,then RPS_THRESH1
  579. // should be set to trigger every TS_HEIGHT (512) lines.
  580. //
  581. saa7146_write(dev, RPS_THRESH1, (TS_HEIGHT*1) | MASK_12 );
  582. // saa7146_write(dev, RPS_THRESH0, ((TS_HEIGHT/2)<<16) |MASK_28| (TS_HEIGHT/2) |MASK_12 );
  583. // Enable RPS1 (rFC p33)
  584. saa7146_write(dev, MC1, (MASK_13 | MASK_29));
  585. if (!(budget = kmalloc (sizeof(struct budget_patch), GFP_KERNEL)))
  586. return -ENOMEM;
  587. dprintk(2, "budget: %p\n", budget);
  588. if ((err = ttpci_budget_init (budget, dev, info, THIS_MODULE))) {
  589. kfree (budget);
  590. return err;
  591. }
  592. dev->ext_priv = budget;
  593. budget->dvb_adapter.priv = budget;
  594. frontend_init(budget);
  595. return 0;
  596. }
  597. static int budget_patch_detach (struct saa7146_dev* dev)
  598. {
  599. struct budget_patch *budget = (struct budget_patch*) dev->ext_priv;
  600. int err;
  601. if (budget->dvb_frontend) dvb_unregister_frontend(budget->dvb_frontend);
  602. err = ttpci_budget_deinit (budget);
  603. kfree (budget);
  604. return err;
  605. }
  606. static int __init budget_patch_init(void)
  607. {
  608. return saa7146_register_extension(&budget_extension);
  609. }
  610. static void __exit budget_patch_exit(void)
  611. {
  612. saa7146_unregister_extension(&budget_extension);
  613. }
  614. static struct saa7146_extension budget_extension = {
  615. .name = "budget_patch dvb\0",
  616. .flags = 0,
  617. .module = THIS_MODULE,
  618. .pci_tbl = pci_tbl,
  619. .attach = budget_patch_attach,
  620. .detach = budget_patch_detach,
  621. .irq_mask = MASK_10,
  622. .irq_func = ttpci_budget_irq10_handler,
  623. };
  624. module_init(budget_patch_init);
  625. module_exit(budget_patch_exit);
  626. MODULE_LICENSE("GPL");
  627. MODULE_AUTHOR("Emard, Roberto Deza, Holger Waechtler, Michael Hunold, others");
  628. MODULE_DESCRIPTION("Driver for full TS modified DVB-S SAA7146+AV7110 "
  629. "based so-called Budget Patch cards");