dvb-bt8xx.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. /*
  2. * Bt8xx based DVB adapter driver
  3. *
  4. * Copyright (C) 2002,2003 Florian Schirmer <jolt@tuxbox.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. */
  21. #define pr_fmt(fmt) "dvb_bt8xx: " fmt
  22. #include <linux/bitops.h>
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/kernel.h>
  26. #include <linux/device.h>
  27. #include <linux/delay.h>
  28. #include <linux/slab.h>
  29. #include <linux/i2c.h>
  30. #include "dmxdev.h"
  31. #include "dvbdev.h"
  32. #include "dvb_demux.h"
  33. #include "dvb_frontend.h"
  34. #include "dvb-bt8xx.h"
  35. #include "bt878.h"
  36. static int debug;
  37. module_param(debug, int, 0644);
  38. MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
  39. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  40. #define dprintk( args... ) \
  41. do { \
  42. if (debug) printk(KERN_DEBUG args); \
  43. } while (0)
  44. #define IF_FREQUENCYx6 217 /* 6 * 36.16666666667MHz */
  45. static void dvb_bt8xx_task(unsigned long data)
  46. {
  47. struct dvb_bt8xx_card *card = (struct dvb_bt8xx_card *)data;
  48. //printk("%d ", card->bt->finished_block);
  49. while (card->bt->last_block != card->bt->finished_block) {
  50. (card->bt->TS_Size ? dvb_dmx_swfilter_204 : dvb_dmx_swfilter)
  51. (&card->demux,
  52. &card->bt->buf_cpu[card->bt->last_block *
  53. card->bt->block_bytes],
  54. card->bt->block_bytes);
  55. card->bt->last_block = (card->bt->last_block + 1) %
  56. card->bt->block_count;
  57. }
  58. }
  59. static int dvb_bt8xx_start_feed(struct dvb_demux_feed *dvbdmxfeed)
  60. {
  61. struct dvb_demux*dvbdmx = dvbdmxfeed->demux;
  62. struct dvb_bt8xx_card *card = dvbdmx->priv;
  63. int rc;
  64. dprintk("dvb_bt8xx: start_feed\n");
  65. if (!dvbdmx->dmx.frontend)
  66. return -EINVAL;
  67. mutex_lock(&card->lock);
  68. card->nfeeds++;
  69. rc = card->nfeeds;
  70. if (card->nfeeds == 1)
  71. bt878_start(card->bt, card->gpio_mode,
  72. card->op_sync_orin, card->irq_err_ignore);
  73. mutex_unlock(&card->lock);
  74. return rc;
  75. }
  76. static int dvb_bt8xx_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
  77. {
  78. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  79. struct dvb_bt8xx_card *card = dvbdmx->priv;
  80. dprintk("dvb_bt8xx: stop_feed\n");
  81. if (!dvbdmx->dmx.frontend)
  82. return -EINVAL;
  83. mutex_lock(&card->lock);
  84. card->nfeeds--;
  85. if (card->nfeeds == 0)
  86. bt878_stop(card->bt);
  87. mutex_unlock(&card->lock);
  88. return 0;
  89. }
  90. static int is_pci_slot_eq(struct pci_dev* adev, struct pci_dev* bdev)
  91. {
  92. if ((adev->subsystem_vendor == bdev->subsystem_vendor) &&
  93. (adev->subsystem_device == bdev->subsystem_device) &&
  94. (adev->bus->number == bdev->bus->number) &&
  95. (PCI_SLOT(adev->devfn) == PCI_SLOT(bdev->devfn)))
  96. return 1;
  97. return 0;
  98. }
  99. static struct bt878 *dvb_bt8xx_878_match(unsigned int bttv_nr,
  100. struct pci_dev* bttv_pci_dev)
  101. {
  102. unsigned int card_nr;
  103. /* Hmm, n squared. Hope n is small */
  104. for (card_nr = 0; card_nr < bt878_num; card_nr++)
  105. if (is_pci_slot_eq(bt878[card_nr].dev, bttv_pci_dev))
  106. return &bt878[card_nr];
  107. return NULL;
  108. }
  109. static int thomson_dtt7579_demod_init(struct dvb_frontend* fe)
  110. {
  111. static u8 mt352_clock_config [] = { 0x89, 0x38, 0x38 };
  112. static u8 mt352_reset [] = { 0x50, 0x80 };
  113. static u8 mt352_adc_ctl_1_cfg [] = { 0x8E, 0x40 };
  114. static u8 mt352_agc_cfg [] = { 0x67, 0x28, 0x20 };
  115. static u8 mt352_gpp_ctl_cfg [] = { 0x8C, 0x33 };
  116. static u8 mt352_capt_range_cfg[] = { 0x75, 0x32 };
  117. mt352_write(fe, mt352_clock_config, sizeof(mt352_clock_config));
  118. udelay(2000);
  119. mt352_write(fe, mt352_reset, sizeof(mt352_reset));
  120. mt352_write(fe, mt352_adc_ctl_1_cfg, sizeof(mt352_adc_ctl_1_cfg));
  121. mt352_write(fe, mt352_agc_cfg, sizeof(mt352_agc_cfg));
  122. mt352_write(fe, mt352_gpp_ctl_cfg, sizeof(mt352_gpp_ctl_cfg));
  123. mt352_write(fe, mt352_capt_range_cfg, sizeof(mt352_capt_range_cfg));
  124. return 0;
  125. }
  126. static int thomson_dtt7579_tuner_calc_regs(struct dvb_frontend *fe, u8* pllbuf, int buf_len)
  127. {
  128. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  129. u32 div;
  130. unsigned char bs = 0;
  131. unsigned char cp = 0;
  132. if (buf_len < 5)
  133. return -EINVAL;
  134. div = (((c->frequency + 83333) * 3) / 500000) + IF_FREQUENCYx6;
  135. if (c->frequency < 542000000)
  136. cp = 0xb4;
  137. else if (c->frequency < 771000000)
  138. cp = 0xbc;
  139. else
  140. cp = 0xf4;
  141. if (c->frequency == 0)
  142. bs = 0x03;
  143. else if (c->frequency < 443250000)
  144. bs = 0x02;
  145. else
  146. bs = 0x08;
  147. pllbuf[0] = 0x60;
  148. pllbuf[1] = div >> 8;
  149. pllbuf[2] = div & 0xff;
  150. pllbuf[3] = cp;
  151. pllbuf[4] = bs;
  152. return 5;
  153. }
  154. static struct mt352_config thomson_dtt7579_config = {
  155. .demod_address = 0x0f,
  156. .demod_init = thomson_dtt7579_demod_init,
  157. };
  158. static struct zl10353_config thomson_dtt7579_zl10353_config = {
  159. .demod_address = 0x0f,
  160. };
  161. static int cx24108_tuner_set_params(struct dvb_frontend *fe)
  162. {
  163. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  164. u32 freq = c->frequency;
  165. int i, a, n, pump;
  166. u32 band, pll;
  167. u32 osci[]={950000,1019000,1075000,1178000,1296000,1432000,
  168. 1576000,1718000,1856000,2036000,2150000};
  169. u32 bandsel[]={0,0x00020000,0x00040000,0x00100800,0x00101000,
  170. 0x00102000,0x00104000,0x00108000,0x00110000,
  171. 0x00120000,0x00140000};
  172. #define XTAL 1011100 /* Hz, really 1.0111 MHz and a /10 prescaler */
  173. dprintk("cx24108 debug: entering SetTunerFreq, freq=%d\n", freq);
  174. /* This is really the bit driving the tuner chip cx24108 */
  175. if (freq<950000)
  176. freq = 950000; /* kHz */
  177. else if (freq>2150000)
  178. freq = 2150000; /* satellite IF is 950..2150MHz */
  179. /* decide which VCO to use for the input frequency */
  180. for(i = 1; (i < ARRAY_SIZE(osci) - 1) && (osci[i] < freq); i++);
  181. dprintk("cx24108 debug: select vco #%d (f=%d)\n", i, freq);
  182. band=bandsel[i];
  183. /* the gain values must be set by SetSymbolrate */
  184. /* compute the pll divider needed, from Conexant data sheet,
  185. resolved for (n*32+a), remember f(vco) is f(receive) *2 or *4,
  186. depending on the divider bit. It is set to /4 on the 2 lowest
  187. bands */
  188. n=((i<=2?2:1)*freq*10L)/(XTAL/100);
  189. a=n%32; n/=32; if(a==0) n--;
  190. pump=(freq<(osci[i-1]+osci[i])/2);
  191. pll=0xf8000000|
  192. ((pump?1:2)<<(14+11))|
  193. ((n&0x1ff)<<(5+11))|
  194. ((a&0x1f)<<11);
  195. /* everything is shifted left 11 bits to left-align the bits in the
  196. 32bit word. Output to the tuner goes MSB-aligned, after all */
  197. dprintk("cx24108 debug: pump=%d, n=%d, a=%d\n", pump, n, a);
  198. cx24110_pll_write(fe,band);
  199. /* set vga and vca to their widest-band settings, as a precaution.
  200. SetSymbolrate might not be called to set this up */
  201. cx24110_pll_write(fe,0x500c0000);
  202. cx24110_pll_write(fe,0x83f1f800);
  203. cx24110_pll_write(fe,pll);
  204. //writereg(client,0x56,0x7f);
  205. return 0;
  206. }
  207. static int pinnsat_tuner_init(struct dvb_frontend* fe)
  208. {
  209. struct dvb_bt8xx_card *card = fe->dvb->priv;
  210. bttv_gpio_enable(card->bttv_nr, 1, 1); /* output */
  211. bttv_write_gpio(card->bttv_nr, 1, 1); /* relay on */
  212. return 0;
  213. }
  214. static int pinnsat_tuner_sleep(struct dvb_frontend* fe)
  215. {
  216. struct dvb_bt8xx_card *card = fe->dvb->priv;
  217. bttv_write_gpio(card->bttv_nr, 1, 0); /* relay off */
  218. return 0;
  219. }
  220. static struct cx24110_config pctvsat_config = {
  221. .demod_address = 0x55,
  222. };
  223. static int microtune_mt7202dtf_tuner_set_params(struct dvb_frontend *fe)
  224. {
  225. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  226. struct dvb_bt8xx_card *card = (struct dvb_bt8xx_card *) fe->dvb->priv;
  227. u8 cfg, cpump, band_select;
  228. u8 data[4];
  229. u32 div;
  230. struct i2c_msg msg = { .addr = 0x60, .flags = 0, .buf = data, .len = sizeof(data) };
  231. div = (36000000 + c->frequency + 83333) / 166666;
  232. cfg = 0x88;
  233. if (c->frequency < 175000000)
  234. cpump = 2;
  235. else if (c->frequency < 390000000)
  236. cpump = 1;
  237. else if (c->frequency < 470000000)
  238. cpump = 2;
  239. else if (c->frequency < 750000000)
  240. cpump = 2;
  241. else
  242. cpump = 3;
  243. if (c->frequency < 175000000)
  244. band_select = 0x0e;
  245. else if (c->frequency < 470000000)
  246. band_select = 0x05;
  247. else
  248. band_select = 0x03;
  249. data[0] = (div >> 8) & 0x7f;
  250. data[1] = div & 0xff;
  251. data[2] = ((div >> 10) & 0x60) | cfg;
  252. data[3] = (cpump << 6) | band_select;
  253. if (fe->ops.i2c_gate_ctrl)
  254. fe->ops.i2c_gate_ctrl(fe, 1);
  255. i2c_transfer(card->i2c_adapter, &msg, 1);
  256. return (div * 166666 - 36000000);
  257. }
  258. static int microtune_mt7202dtf_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
  259. {
  260. struct dvb_bt8xx_card* bt = (struct dvb_bt8xx_card*) fe->dvb->priv;
  261. return reject_firmware(fw, name, &bt->bt->dev->dev);
  262. }
  263. static const struct sp887x_config microtune_mt7202dtf_config = {
  264. .demod_address = 0x70,
  265. .request_firmware = microtune_mt7202dtf_request_firmware,
  266. };
  267. static int advbt771_samsung_tdtc9251dh0_demod_init(struct dvb_frontend* fe)
  268. {
  269. static u8 mt352_clock_config [] = { 0x89, 0x38, 0x2d };
  270. static u8 mt352_reset [] = { 0x50, 0x80 };
  271. static u8 mt352_adc_ctl_1_cfg [] = { 0x8E, 0x40 };
  272. static u8 mt352_agc_cfg [] = { 0x67, 0x10, 0x23, 0x00, 0xFF, 0xFF,
  273. 0x00, 0xFF, 0x00, 0x40, 0x40 };
  274. static u8 mt352_av771_extra[] = { 0xB5, 0x7A };
  275. static u8 mt352_capt_range_cfg[] = { 0x75, 0x32 };
  276. mt352_write(fe, mt352_clock_config, sizeof(mt352_clock_config));
  277. udelay(2000);
  278. mt352_write(fe, mt352_reset, sizeof(mt352_reset));
  279. mt352_write(fe, mt352_adc_ctl_1_cfg, sizeof(mt352_adc_ctl_1_cfg));
  280. mt352_write(fe, mt352_agc_cfg,sizeof(mt352_agc_cfg));
  281. udelay(2000);
  282. mt352_write(fe, mt352_av771_extra,sizeof(mt352_av771_extra));
  283. mt352_write(fe, mt352_capt_range_cfg, sizeof(mt352_capt_range_cfg));
  284. return 0;
  285. }
  286. static int advbt771_samsung_tdtc9251dh0_tuner_calc_regs(struct dvb_frontend *fe, u8 *pllbuf, int buf_len)
  287. {
  288. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  289. u32 div;
  290. unsigned char bs = 0;
  291. unsigned char cp = 0;
  292. if (buf_len < 5) return -EINVAL;
  293. div = (((c->frequency + 83333) * 3) / 500000) + IF_FREQUENCYx6;
  294. if (c->frequency < 150000000)
  295. cp = 0xB4;
  296. else if (c->frequency < 173000000)
  297. cp = 0xBC;
  298. else if (c->frequency < 250000000)
  299. cp = 0xB4;
  300. else if (c->frequency < 400000000)
  301. cp = 0xBC;
  302. else if (c->frequency < 420000000)
  303. cp = 0xF4;
  304. else if (c->frequency < 470000000)
  305. cp = 0xFC;
  306. else if (c->frequency < 600000000)
  307. cp = 0xBC;
  308. else if (c->frequency < 730000000)
  309. cp = 0xF4;
  310. else
  311. cp = 0xFC;
  312. if (c->frequency < 150000000)
  313. bs = 0x01;
  314. else if (c->frequency < 173000000)
  315. bs = 0x01;
  316. else if (c->frequency < 250000000)
  317. bs = 0x02;
  318. else if (c->frequency < 400000000)
  319. bs = 0x02;
  320. else if (c->frequency < 420000000)
  321. bs = 0x02;
  322. else if (c->frequency < 470000000)
  323. bs = 0x02;
  324. else if (c->frequency < 600000000)
  325. bs = 0x08;
  326. else if (c->frequency < 730000000)
  327. bs = 0x08;
  328. else
  329. bs = 0x08;
  330. pllbuf[0] = 0x61;
  331. pllbuf[1] = div >> 8;
  332. pllbuf[2] = div & 0xff;
  333. pllbuf[3] = cp;
  334. pllbuf[4] = bs;
  335. return 5;
  336. }
  337. static struct mt352_config advbt771_samsung_tdtc9251dh0_config = {
  338. .demod_address = 0x0f,
  339. .demod_init = advbt771_samsung_tdtc9251dh0_demod_init,
  340. };
  341. static struct dst_config dst_config = {
  342. .demod_address = 0x55,
  343. };
  344. static int or51211_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
  345. {
  346. struct dvb_bt8xx_card* bt = (struct dvb_bt8xx_card*) fe->dvb->priv;
  347. return reject_firmware(fw, name, &bt->bt->dev->dev);
  348. }
  349. static void or51211_setmode(struct dvb_frontend * fe, int mode)
  350. {
  351. struct dvb_bt8xx_card *bt = fe->dvb->priv;
  352. bttv_write_gpio(bt->bttv_nr, 0x0002, mode); /* Reset */
  353. msleep(20);
  354. }
  355. static void or51211_reset(struct dvb_frontend * fe)
  356. {
  357. struct dvb_bt8xx_card *bt = fe->dvb->priv;
  358. /* RESET DEVICE
  359. * reset is controlled by GPIO-0
  360. * when set to 0 causes reset and when to 1 for normal op
  361. * must remain reset for 128 clock cycles on a 50Mhz clock
  362. * also PRM1 PRM2 & PRM4 are controlled by GPIO-1,GPIO-2 & GPIO-4
  363. * We assume that the reset has be held low long enough or we
  364. * have been reset by a power on. When the driver is unloaded
  365. * reset set to 0 so if reloaded we have been reset.
  366. */
  367. /* reset & PRM1,2&4 are outputs */
  368. int ret = bttv_gpio_enable(bt->bttv_nr, 0x001F, 0x001F);
  369. if (ret != 0)
  370. printk(KERN_WARNING "or51211: Init Error - Can't Reset DVR (%i)\n", ret);
  371. bttv_write_gpio(bt->bttv_nr, 0x001F, 0x0000); /* Reset */
  372. msleep(20);
  373. /* Now set for normal operation */
  374. bttv_write_gpio(bt->bttv_nr, 0x0001F, 0x0001);
  375. /* wait for operation to begin */
  376. msleep(500);
  377. }
  378. static void or51211_sleep(struct dvb_frontend * fe)
  379. {
  380. struct dvb_bt8xx_card *bt = fe->dvb->priv;
  381. bttv_write_gpio(bt->bttv_nr, 0x0001, 0x0000);
  382. }
  383. static const struct or51211_config or51211_config = {
  384. .demod_address = 0x15,
  385. .request_firmware = or51211_request_firmware,
  386. .setmode = or51211_setmode,
  387. .reset = or51211_reset,
  388. .sleep = or51211_sleep,
  389. };
  390. static int vp3021_alps_tded4_tuner_set_params(struct dvb_frontend *fe)
  391. {
  392. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  393. struct dvb_bt8xx_card *card = (struct dvb_bt8xx_card *) fe->dvb->priv;
  394. u8 buf[4];
  395. u32 div;
  396. struct i2c_msg msg = { .addr = 0x60, .flags = 0, .buf = buf, .len = sizeof(buf) };
  397. div = (c->frequency + 36166667) / 166667;
  398. buf[0] = (div >> 8) & 0x7F;
  399. buf[1] = div & 0xFF;
  400. buf[2] = 0x85;
  401. if ((c->frequency >= 47000000) && (c->frequency < 153000000))
  402. buf[3] = 0x01;
  403. else if ((c->frequency >= 153000000) && (c->frequency < 430000000))
  404. buf[3] = 0x02;
  405. else if ((c->frequency >= 430000000) && (c->frequency < 824000000))
  406. buf[3] = 0x0C;
  407. else if ((c->frequency >= 824000000) && (c->frequency < 863000000))
  408. buf[3] = 0x8C;
  409. else
  410. return -EINVAL;
  411. if (fe->ops.i2c_gate_ctrl)
  412. fe->ops.i2c_gate_ctrl(fe, 1);
  413. i2c_transfer(card->i2c_adapter, &msg, 1);
  414. return 0;
  415. }
  416. static struct nxt6000_config vp3021_alps_tded4_config = {
  417. .demod_address = 0x0a,
  418. .clock_inversion = 1,
  419. };
  420. static int digitv_alps_tded4_demod_init(struct dvb_frontend* fe)
  421. {
  422. static u8 mt352_clock_config [] = { 0x89, 0x38, 0x2d };
  423. static u8 mt352_reset [] = { 0x50, 0x80 };
  424. static u8 mt352_adc_ctl_1_cfg [] = { 0x8E, 0x40 };
  425. static u8 mt352_agc_cfg [] = { 0x67, 0x20, 0xa0 };
  426. static u8 mt352_capt_range_cfg[] = { 0x75, 0x32 };
  427. mt352_write(fe, mt352_clock_config, sizeof(mt352_clock_config));
  428. udelay(2000);
  429. mt352_write(fe, mt352_reset, sizeof(mt352_reset));
  430. mt352_write(fe, mt352_adc_ctl_1_cfg, sizeof(mt352_adc_ctl_1_cfg));
  431. mt352_write(fe, mt352_agc_cfg,sizeof(mt352_agc_cfg));
  432. mt352_write(fe, mt352_capt_range_cfg, sizeof(mt352_capt_range_cfg));
  433. return 0;
  434. }
  435. static int digitv_alps_tded4_tuner_calc_regs(struct dvb_frontend *fe, u8 *pllbuf, int buf_len)
  436. {
  437. u32 div;
  438. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  439. if (buf_len < 5)
  440. return -EINVAL;
  441. div = (((c->frequency + 83333) * 3) / 500000) + IF_FREQUENCYx6;
  442. pllbuf[0] = 0x61;
  443. pllbuf[1] = (div >> 8) & 0x7F;
  444. pllbuf[2] = div & 0xFF;
  445. pllbuf[3] = 0x85;
  446. dprintk("frequency %u, div %u\n", c->frequency, div);
  447. if (c->frequency < 470000000)
  448. pllbuf[4] = 0x02;
  449. else if (c->frequency > 823000000)
  450. pllbuf[4] = 0x88;
  451. else
  452. pllbuf[4] = 0x08;
  453. if (c->bandwidth_hz == 8000000)
  454. pllbuf[4] |= 0x04;
  455. return 5;
  456. }
  457. static void digitv_alps_tded4_reset(struct dvb_bt8xx_card *bt)
  458. {
  459. /*
  460. * Reset the frontend, must be called before trying
  461. * to initialise the MT352 or mt352_attach
  462. * will fail. Same goes for the nxt6000 frontend.
  463. *
  464. */
  465. int ret = bttv_gpio_enable(bt->bttv_nr, 0x08, 0x08);
  466. if (ret != 0)
  467. printk(KERN_WARNING "digitv_alps_tded4: Init Error - Can't Reset DVR (%i)\n", ret);
  468. /* Pulse the reset line */
  469. bttv_write_gpio(bt->bttv_nr, 0x08, 0x08); /* High */
  470. bttv_write_gpio(bt->bttv_nr, 0x08, 0x00); /* Low */
  471. msleep(100);
  472. bttv_write_gpio(bt->bttv_nr, 0x08, 0x08); /* High */
  473. }
  474. static struct mt352_config digitv_alps_tded4_config = {
  475. .demod_address = 0x0a,
  476. .demod_init = digitv_alps_tded4_demod_init,
  477. };
  478. static struct lgdt330x_config tdvs_tua6034_config = {
  479. .demod_address = 0x0e,
  480. .demod_chip = LGDT3303,
  481. .serial_mpeg = 0x40, /* TPSERIAL for 3303 in TOP_CONTROL */
  482. };
  483. static void lgdt330x_reset(struct dvb_bt8xx_card *bt)
  484. {
  485. /* Set pin 27 of the lgdt3303 chip high to reset the frontend */
  486. /* Pulse the reset line */
  487. bttv_write_gpio(bt->bttv_nr, 0x00e00007, 0x00000001); /* High */
  488. bttv_write_gpio(bt->bttv_nr, 0x00e00007, 0x00000000); /* Low */
  489. msleep(100);
  490. bttv_write_gpio(bt->bttv_nr, 0x00e00007, 0x00000001); /* High */
  491. msleep(100);
  492. }
  493. static void frontend_init(struct dvb_bt8xx_card *card, u32 type)
  494. {
  495. struct dst_state* state = NULL;
  496. switch(type) {
  497. case BTTV_BOARD_DVICO_DVBT_LITE:
  498. card->fe = dvb_attach(mt352_attach, &thomson_dtt7579_config, card->i2c_adapter);
  499. if (card->fe == NULL)
  500. card->fe = dvb_attach(zl10353_attach, &thomson_dtt7579_zl10353_config,
  501. card->i2c_adapter);
  502. if (card->fe != NULL) {
  503. card->fe->ops.tuner_ops.calc_regs = thomson_dtt7579_tuner_calc_regs;
  504. card->fe->ops.info.frequency_min = 174000000;
  505. card->fe->ops.info.frequency_max = 862000000;
  506. }
  507. break;
  508. case BTTV_BOARD_DVICO_FUSIONHDTV_5_LITE:
  509. lgdt330x_reset(card);
  510. card->fe = dvb_attach(lgdt330x_attach, &tdvs_tua6034_config, card->i2c_adapter);
  511. if (card->fe != NULL) {
  512. dvb_attach(simple_tuner_attach, card->fe,
  513. card->i2c_adapter, 0x61,
  514. TUNER_LG_TDVS_H06XF);
  515. dprintk ("dvb_bt8xx: lgdt330x detected\n");
  516. }
  517. break;
  518. case BTTV_BOARD_NEBULA_DIGITV:
  519. /*
  520. * It is possible to determine the correct frontend using the I2C bus (see the Nebula SDK);
  521. * this would be a cleaner solution than trying each frontend in turn.
  522. */
  523. /* Old Nebula (marked (c)2003 on high profile pci card) has nxt6000 demod */
  524. digitv_alps_tded4_reset(card);
  525. card->fe = dvb_attach(nxt6000_attach, &vp3021_alps_tded4_config, card->i2c_adapter);
  526. if (card->fe != NULL) {
  527. card->fe->ops.tuner_ops.set_params = vp3021_alps_tded4_tuner_set_params;
  528. dprintk ("dvb_bt8xx: an nxt6000 was detected on your digitv card\n");
  529. break;
  530. }
  531. /* New Nebula (marked (c)2005 on low profile pci card) has mt352 demod */
  532. digitv_alps_tded4_reset(card);
  533. card->fe = dvb_attach(mt352_attach, &digitv_alps_tded4_config, card->i2c_adapter);
  534. if (card->fe != NULL) {
  535. card->fe->ops.tuner_ops.calc_regs = digitv_alps_tded4_tuner_calc_regs;
  536. dprintk ("dvb_bt8xx: an mt352 was detected on your digitv card\n");
  537. }
  538. break;
  539. case BTTV_BOARD_AVDVBT_761:
  540. card->fe = dvb_attach(sp887x_attach, &microtune_mt7202dtf_config, card->i2c_adapter);
  541. if (card->fe) {
  542. card->fe->ops.tuner_ops.set_params = microtune_mt7202dtf_tuner_set_params;
  543. }
  544. break;
  545. case BTTV_BOARD_AVDVBT_771:
  546. card->fe = dvb_attach(mt352_attach, &advbt771_samsung_tdtc9251dh0_config, card->i2c_adapter);
  547. if (card->fe != NULL) {
  548. card->fe->ops.tuner_ops.calc_regs = advbt771_samsung_tdtc9251dh0_tuner_calc_regs;
  549. card->fe->ops.info.frequency_min = 174000000;
  550. card->fe->ops.info.frequency_max = 862000000;
  551. }
  552. break;
  553. case BTTV_BOARD_TWINHAN_DST:
  554. /* DST is not a frontend driver !!! */
  555. state = kmalloc(sizeof (struct dst_state), GFP_KERNEL);
  556. if (!state) {
  557. pr_err("No memory\n");
  558. break;
  559. }
  560. /* Setup the Card */
  561. state->config = &dst_config;
  562. state->i2c = card->i2c_adapter;
  563. state->bt = card->bt;
  564. state->dst_ca = NULL;
  565. /* DST is not a frontend, attaching the ASIC */
  566. if (dvb_attach(dst_attach, state, &card->dvb_adapter) == NULL) {
  567. pr_err("%s: Could not find a Twinhan DST\n", __func__);
  568. kfree(state);
  569. break;
  570. }
  571. /* Attach other DST peripherals if any */
  572. /* Conditional Access device */
  573. card->fe = &state->frontend;
  574. if (state->dst_hw_cap & DST_TYPE_HAS_CA)
  575. dvb_attach(dst_ca_attach, state, &card->dvb_adapter);
  576. break;
  577. case BTTV_BOARD_PINNACLESAT:
  578. card->fe = dvb_attach(cx24110_attach, &pctvsat_config, card->i2c_adapter);
  579. if (card->fe) {
  580. card->fe->ops.tuner_ops.init = pinnsat_tuner_init;
  581. card->fe->ops.tuner_ops.sleep = pinnsat_tuner_sleep;
  582. card->fe->ops.tuner_ops.set_params = cx24108_tuner_set_params;
  583. }
  584. break;
  585. case BTTV_BOARD_PC_HDTV:
  586. card->fe = dvb_attach(or51211_attach, &or51211_config, card->i2c_adapter);
  587. if (card->fe != NULL)
  588. dvb_attach(simple_tuner_attach, card->fe,
  589. card->i2c_adapter, 0x61,
  590. TUNER_PHILIPS_FCV1236D);
  591. break;
  592. }
  593. if (card->fe == NULL)
  594. pr_err("A frontend driver was not found for device [%04x:%04x] subsystem [%04x:%04x]\n",
  595. card->bt->dev->vendor,
  596. card->bt->dev->device,
  597. card->bt->dev->subsystem_vendor,
  598. card->bt->dev->subsystem_device);
  599. else
  600. if (dvb_register_frontend(&card->dvb_adapter, card->fe)) {
  601. pr_err("Frontend registration failed!\n");
  602. dvb_frontend_detach(card->fe);
  603. card->fe = NULL;
  604. }
  605. }
  606. static int dvb_bt8xx_load_card(struct dvb_bt8xx_card *card, u32 type)
  607. {
  608. int result;
  609. result = dvb_register_adapter(&card->dvb_adapter, card->card_name,
  610. THIS_MODULE, &card->bt->dev->dev,
  611. adapter_nr);
  612. if (result < 0) {
  613. pr_err("dvb_register_adapter failed (errno = %d)\n", result);
  614. return result;
  615. }
  616. card->dvb_adapter.priv = card;
  617. card->bt->adapter = card->i2c_adapter;
  618. memset(&card->demux, 0, sizeof(struct dvb_demux));
  619. card->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING;
  620. card->demux.priv = card;
  621. card->demux.filternum = 256;
  622. card->demux.feednum = 256;
  623. card->demux.start_feed = dvb_bt8xx_start_feed;
  624. card->demux.stop_feed = dvb_bt8xx_stop_feed;
  625. card->demux.write_to_decoder = NULL;
  626. result = dvb_dmx_init(&card->demux);
  627. if (result < 0) {
  628. pr_err("dvb_dmx_init failed (errno = %d)\n", result);
  629. goto err_unregister_adaptor;
  630. }
  631. card->dmxdev.filternum = 256;
  632. card->dmxdev.demux = &card->demux.dmx;
  633. card->dmxdev.capabilities = 0;
  634. result = dvb_dmxdev_init(&card->dmxdev, &card->dvb_adapter);
  635. if (result < 0) {
  636. pr_err("dvb_dmxdev_init failed (errno = %d)\n", result);
  637. goto err_dmx_release;
  638. }
  639. card->fe_hw.source = DMX_FRONTEND_0;
  640. result = card->demux.dmx.add_frontend(&card->demux.dmx, &card->fe_hw);
  641. if (result < 0) {
  642. pr_err("dvb_dmx_init failed (errno = %d)\n", result);
  643. goto err_dmxdev_release;
  644. }
  645. card->fe_mem.source = DMX_MEMORY_FE;
  646. result = card->demux.dmx.add_frontend(&card->demux.dmx, &card->fe_mem);
  647. if (result < 0) {
  648. pr_err("dvb_dmx_init failed (errno = %d)\n", result);
  649. goto err_remove_hw_frontend;
  650. }
  651. result = card->demux.dmx.connect_frontend(&card->demux.dmx, &card->fe_hw);
  652. if (result < 0) {
  653. pr_err("dvb_dmx_init failed (errno = %d)\n", result);
  654. goto err_remove_mem_frontend;
  655. }
  656. result = dvb_net_init(&card->dvb_adapter, &card->dvbnet, &card->demux.dmx);
  657. if (result < 0) {
  658. pr_err("dvb_net_init failed (errno = %d)\n", result);
  659. goto err_disconnect_frontend;
  660. }
  661. tasklet_init(&card->bt->tasklet, dvb_bt8xx_task, (unsigned long) card);
  662. frontend_init(card, type);
  663. return 0;
  664. err_disconnect_frontend:
  665. card->demux.dmx.disconnect_frontend(&card->demux.dmx);
  666. err_remove_mem_frontend:
  667. card->demux.dmx.remove_frontend(&card->demux.dmx, &card->fe_mem);
  668. err_remove_hw_frontend:
  669. card->demux.dmx.remove_frontend(&card->demux.dmx, &card->fe_hw);
  670. err_dmxdev_release:
  671. dvb_dmxdev_release(&card->dmxdev);
  672. err_dmx_release:
  673. dvb_dmx_release(&card->demux);
  674. err_unregister_adaptor:
  675. dvb_unregister_adapter(&card->dvb_adapter);
  676. return result;
  677. }
  678. static int dvb_bt8xx_probe(struct bttv_sub_device *sub)
  679. {
  680. struct dvb_bt8xx_card *card;
  681. struct pci_dev* bttv_pci_dev;
  682. int ret;
  683. if (!(card = kzalloc(sizeof(struct dvb_bt8xx_card), GFP_KERNEL)))
  684. return -ENOMEM;
  685. mutex_init(&card->lock);
  686. card->bttv_nr = sub->core->nr;
  687. strlcpy(card->card_name, sub->core->v4l2_dev.name, sizeof(card->card_name));
  688. card->i2c_adapter = &sub->core->i2c_adap;
  689. switch(sub->core->type) {
  690. case BTTV_BOARD_PINNACLESAT:
  691. card->gpio_mode = 0x0400c060;
  692. /* should be: BT878_A_GAIN=0,BT878_A_PWRDN,BT878_DA_DPM,BT878_DA_SBR,
  693. BT878_DA_IOM=1,BT878_DA_APP to enable serial highspeed mode. */
  694. card->op_sync_orin = BT878_RISC_SYNC_MASK;
  695. card->irq_err_ignore = BT878_AFBUS | BT878_AFDSR;
  696. break;
  697. case BTTV_BOARD_DVICO_DVBT_LITE:
  698. card->gpio_mode = 0x0400C060;
  699. card->op_sync_orin = BT878_RISC_SYNC_MASK;
  700. card->irq_err_ignore = BT878_AFBUS | BT878_AFDSR;
  701. /* 26, 15, 14, 6, 5
  702. * A_PWRDN DA_DPM DA_SBR DA_IOM_DA
  703. * DA_APP(parallel) */
  704. break;
  705. case BTTV_BOARD_DVICO_FUSIONHDTV_5_LITE:
  706. card->gpio_mode = 0x0400c060;
  707. card->op_sync_orin = BT878_RISC_SYNC_MASK;
  708. card->irq_err_ignore = BT878_AFBUS | BT878_AFDSR;
  709. break;
  710. case BTTV_BOARD_NEBULA_DIGITV:
  711. case BTTV_BOARD_AVDVBT_761:
  712. card->gpio_mode = (1 << 26) | (1 << 14) | (1 << 5);
  713. card->op_sync_orin = BT878_RISC_SYNC_MASK;
  714. card->irq_err_ignore = BT878_AFBUS | BT878_AFDSR;
  715. /* A_PWRDN DA_SBR DA_APP (high speed serial) */
  716. break;
  717. case BTTV_BOARD_AVDVBT_771: //case 0x07711461:
  718. card->gpio_mode = 0x0400402B;
  719. card->op_sync_orin = BT878_RISC_SYNC_MASK;
  720. card->irq_err_ignore = BT878_AFBUS | BT878_AFDSR;
  721. /* A_PWRDN DA_SBR DA_APP[0] PKTP=10 RISC_ENABLE FIFO_ENABLE*/
  722. break;
  723. case BTTV_BOARD_TWINHAN_DST:
  724. card->gpio_mode = 0x2204f2c;
  725. card->op_sync_orin = BT878_RISC_SYNC_MASK;
  726. card->irq_err_ignore = BT878_APABORT | BT878_ARIPERR |
  727. BT878_APPERR | BT878_AFBUS;
  728. /* 25,21,14,11,10,9,8,3,2 then
  729. * 0x33 = 5,4,1,0
  730. * A_SEL=SML, DA_MLB, DA_SBR,
  731. * DA_SDR=f, fifo trigger = 32 DWORDS
  732. * IOM = 0 == audio A/D
  733. * DPM = 0 == digital audio mode
  734. * == async data parallel port
  735. * then 0x33 (13 is set by start_capture)
  736. * DA_APP = async data parallel port,
  737. * ACAP_EN = 1,
  738. * RISC+FIFO ENABLE */
  739. break;
  740. case BTTV_BOARD_PC_HDTV:
  741. card->gpio_mode = 0x0100EC7B;
  742. card->op_sync_orin = BT878_RISC_SYNC_MASK;
  743. card->irq_err_ignore = BT878_AFBUS | BT878_AFDSR;
  744. break;
  745. default:
  746. pr_err("Unknown bttv card type: %d\n", sub->core->type);
  747. kfree(card);
  748. return -ENODEV;
  749. }
  750. dprintk("dvb_bt8xx: identified card%d as %s\n", card->bttv_nr, card->card_name);
  751. if (!(bttv_pci_dev = bttv_get_pcidev(card->bttv_nr))) {
  752. pr_err("no pci device for card %d\n", card->bttv_nr);
  753. kfree(card);
  754. return -ENODEV;
  755. }
  756. if (!(card->bt = dvb_bt8xx_878_match(card->bttv_nr, bttv_pci_dev))) {
  757. pr_err("unable to determine DMA core of card %d,\n", card->bttv_nr);
  758. pr_err("if you have the ALSA bt87x audio driver installed, try removing it.\n");
  759. kfree(card);
  760. return -ENODEV;
  761. }
  762. mutex_init(&card->bt->gpio_lock);
  763. card->bt->bttv_nr = sub->core->nr;
  764. if ( (ret = dvb_bt8xx_load_card(card, sub->core->type)) ) {
  765. kfree(card);
  766. return ret;
  767. }
  768. dev_set_drvdata(&sub->dev, card);
  769. return 0;
  770. }
  771. static void dvb_bt8xx_remove(struct bttv_sub_device *sub)
  772. {
  773. struct dvb_bt8xx_card *card = dev_get_drvdata(&sub->dev);
  774. dprintk("dvb_bt8xx: unloading card%d\n", card->bttv_nr);
  775. bt878_stop(card->bt);
  776. tasklet_kill(&card->bt->tasklet);
  777. dvb_net_release(&card->dvbnet);
  778. card->demux.dmx.remove_frontend(&card->demux.dmx, &card->fe_mem);
  779. card->demux.dmx.remove_frontend(&card->demux.dmx, &card->fe_hw);
  780. dvb_dmxdev_release(&card->dmxdev);
  781. dvb_dmx_release(&card->demux);
  782. if (card->fe) {
  783. dvb_unregister_frontend(card->fe);
  784. dvb_frontend_detach(card->fe);
  785. }
  786. dvb_unregister_adapter(&card->dvb_adapter);
  787. kfree(card);
  788. }
  789. static struct bttv_sub_driver driver = {
  790. .drv = {
  791. .name = "dvb-bt8xx",
  792. },
  793. .probe = dvb_bt8xx_probe,
  794. .remove = dvb_bt8xx_remove,
  795. /* FIXME:
  796. * .shutdown = dvb_bt8xx_shutdown,
  797. * .suspend = dvb_bt8xx_suspend,
  798. * .resume = dvb_bt8xx_resume,
  799. */
  800. };
  801. static int __init dvb_bt8xx_init(void)
  802. {
  803. return bttv_sub_register(&driver, "dvb");
  804. }
  805. static void __exit dvb_bt8xx_exit(void)
  806. {
  807. bttv_sub_unregister(&driver);
  808. }
  809. module_init(dvb_bt8xx_init);
  810. module_exit(dvb_bt8xx_exit);
  811. MODULE_DESCRIPTION("Bt8xx based DVB adapter driver");
  812. MODULE_AUTHOR("Florian Schirmer <jolt@tuxbox.org>");
  813. MODULE_LICENSE("GPL");