cx18-dvb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * cx18 functions for DVB support
  3. *
  4. * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
  5. * Copyright (C) 2008 Andy Walls <awalls@radix.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. *
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include "cx18-version.h"
  23. #include "cx18-dvb.h"
  24. #include "cx18-io.h"
  25. #include "cx18-queue.h"
  26. #include "cx18-streams.h"
  27. #include "cx18-cards.h"
  28. #include "cx18-gpio.h"
  29. #include "s5h1409.h"
  30. #include "mxl5005s.h"
  31. #include "zl10353.h"
  32. #include <linux/firmware.h>
  33. #include "mt352.h"
  34. #include "mt352_priv.h"
  35. #include "tuner-xc2028.h"
  36. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  37. #define CX18_REG_DMUX_NUM_PORT_0_CONTROL 0xd5a000
  38. #define CX18_CLOCK_ENABLE2 0xc71024
  39. #define CX18_DMUX_CLK_MASK 0x0080
  40. /*
  41. * CX18_CARD_HVR_1600_ESMT
  42. * CX18_CARD_HVR_1600_SAMSUNG
  43. */
  44. static struct mxl5005s_config hauppauge_hvr1600_tuner = {
  45. .i2c_address = 0xC6 >> 1,
  46. .if_freq = IF_FREQ_5380000HZ,
  47. .xtal_freq = CRYSTAL_FREQ_16000000HZ,
  48. .agc_mode = MXL_SINGLE_AGC,
  49. .tracking_filter = MXL_TF_C_H,
  50. .rssi_enable = MXL_RSSI_ENABLE,
  51. .cap_select = MXL_CAP_SEL_ENABLE,
  52. .div_out = MXL_DIV_OUT_4,
  53. .clock_out = MXL_CLOCK_OUT_DISABLE,
  54. .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
  55. .top = MXL5005S_TOP_25P2,
  56. .mod_mode = MXL_DIGITAL_MODE,
  57. .if_mode = MXL_ZERO_IF,
  58. .qam_gain = 0x02,
  59. .AgcMasterByte = 0x00,
  60. };
  61. static struct s5h1409_config hauppauge_hvr1600_config = {
  62. .demod_address = 0x32 >> 1,
  63. .output_mode = S5H1409_SERIAL_OUTPUT,
  64. .gpio = S5H1409_GPIO_ON,
  65. .qam_if = 44000,
  66. .inversion = S5H1409_INVERSION_OFF,
  67. .status_mode = S5H1409_DEMODLOCKING,
  68. .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
  69. .hvr1600_opt = S5H1409_HVR1600_OPTIMIZE
  70. };
  71. /*
  72. * CX18_CARD_LEADTEK_DVR3100H
  73. */
  74. /* Information/confirmation of proper config values provided by Terry Wu */
  75. static struct zl10353_config leadtek_dvr3100h_demod = {
  76. .demod_address = 0x1e >> 1, /* Datasheet suggested straps */
  77. .if2 = 45600, /* 4.560 MHz IF from the XC3028 */
  78. .parallel_ts = 1, /* Not a serial TS */
  79. .no_tuner = 1, /* XC3028 is not behind the gate */
  80. .disable_i2c_gate_ctrl = 1, /* Disable the I2C gate */
  81. };
  82. /*
  83. * CX18_CARD_YUAN_MPC718
  84. */
  85. /*
  86. * Due to
  87. *
  88. * 1. an absence of information on how to prgram the MT352
  89. * 2. the Linux mt352 module pushing MT352 initialzation off onto us here
  90. *
  91. * We have to use an init sequence that *you* must extract from the Windows
  92. * driver (yuanrap.sys) and which we load as a firmware.
  93. *
  94. * If someone can provide me with a Zarlink MT352 (Intel CE6352?) Design Manual
  95. * with chip programming details, then I can remove this annoyance.
  96. */
  97. static int yuan_mpc718_mt352_reqfw(struct cx18_stream *stream,
  98. const struct firmware **fw)
  99. {
  100. struct cx18 *cx = stream->cx;
  101. const char *fn = "dvb-cx18-mpc718-mt352.fw";
  102. int ret;
  103. ret = request_firmware(fw, fn, &cx->pci_dev->dev);
  104. if (ret)
  105. CX18_ERR("Unable to open firmware file %s\n", fn);
  106. else {
  107. size_t sz = (*fw)->size;
  108. if (sz < 2 || sz > 64 || (sz % 2) != 0) {
  109. CX18_ERR("Firmware %s has a bad size: %lu bytes\n",
  110. fn, (unsigned long) sz);
  111. ret = -EILSEQ;
  112. release_firmware(*fw);
  113. *fw = NULL;
  114. }
  115. }
  116. if (ret) {
  117. CX18_ERR("The MPC718 board variant with the MT352 DVB-T"
  118. "demodualtor will not work without it\n");
  119. CX18_ERR("Run 'linux/Documentation/dvb/get_dvb_firmware "
  120. "mpc718' if you need the firmware\n");
  121. }
  122. return ret;
  123. }
  124. static int yuan_mpc718_mt352_init(struct dvb_frontend *fe)
  125. {
  126. struct cx18_dvb *dvb = container_of(fe->dvb,
  127. struct cx18_dvb, dvb_adapter);
  128. struct cx18_stream *stream = container_of(dvb, struct cx18_stream, dvb);
  129. const struct firmware *fw = NULL;
  130. int ret;
  131. int i;
  132. u8 buf[3];
  133. ret = yuan_mpc718_mt352_reqfw(stream, &fw);
  134. if (ret)
  135. return ret;
  136. /* Loop through all the register-value pairs in the firmware file */
  137. for (i = 0; i < fw->size; i += 2) {
  138. buf[0] = fw->data[i];
  139. /* Intercept a few registers we want to set ourselves */
  140. switch (buf[0]) {
  141. case TRL_NOMINAL_RATE_0:
  142. /* Set our custom OFDM bandwidth in the case below */
  143. break;
  144. case TRL_NOMINAL_RATE_1:
  145. /* 6 MHz: 64/7 * 6/8 / 20.48 * 2^16 = 0x55b6.db6 */
  146. /* 7 MHz: 64/7 * 7/8 / 20.48 * 2^16 = 0x6400 */
  147. /* 8 MHz: 64/7 * 8/8 / 20.48 * 2^16 = 0x7249.249 */
  148. buf[1] = 0x72;
  149. buf[2] = 0x49;
  150. mt352_write(fe, buf, 3);
  151. break;
  152. case INPUT_FREQ_0:
  153. /* Set our custom IF in the case below */
  154. break;
  155. case INPUT_FREQ_1:
  156. /* 4.56 MHz IF: (20.48 - 4.56)/20.48 * 2^14 = 0x31c0 */
  157. buf[1] = 0x31;
  158. buf[2] = 0xc0;
  159. mt352_write(fe, buf, 3);
  160. break;
  161. default:
  162. /* Pass through the register-value pair from the fw */
  163. buf[1] = fw->data[i+1];
  164. mt352_write(fe, buf, 2);
  165. break;
  166. }
  167. }
  168. buf[0] = (u8) TUNER_GO;
  169. buf[1] = 0x01; /* Go */
  170. mt352_write(fe, buf, 2);
  171. release_firmware(fw);
  172. return 0;
  173. }
  174. static struct mt352_config yuan_mpc718_mt352_demod = {
  175. .demod_address = 0x1e >> 1,
  176. .adc_clock = 20480, /* 20.480 MHz */
  177. .if2 = 4560, /* 4.560 MHz */
  178. .no_tuner = 1, /* XC3028 is not behind the gate */
  179. .demod_init = yuan_mpc718_mt352_init,
  180. };
  181. static struct zl10353_config yuan_mpc718_zl10353_demod = {
  182. .demod_address = 0x1e >> 1, /* Datasheet suggested straps */
  183. .if2 = 45600, /* 4.560 MHz IF from the XC3028 */
  184. .parallel_ts = 1, /* Not a serial TS */
  185. .no_tuner = 1, /* XC3028 is not behind the gate */
  186. .disable_i2c_gate_ctrl = 1, /* Disable the I2C gate */
  187. };
  188. static int dvb_register(struct cx18_stream *stream);
  189. /* Kernel DVB framework calls this when the feed needs to start.
  190. * The CX18 framework should enable the transport DMA handling
  191. * and queue processing.
  192. */
  193. static int cx18_dvb_start_feed(struct dvb_demux_feed *feed)
  194. {
  195. struct dvb_demux *demux = feed->demux;
  196. struct cx18_stream *stream = (struct cx18_stream *) demux->priv;
  197. struct cx18 *cx = stream->cx;
  198. int ret;
  199. u32 v;
  200. CX18_DEBUG_INFO("Start feed: pid = 0x%x index = %d\n",
  201. feed->pid, feed->index);
  202. mutex_lock(&cx->serialize_lock);
  203. ret = cx18_init_on_first_open(cx);
  204. mutex_unlock(&cx->serialize_lock);
  205. if (ret) {
  206. CX18_ERR("Failed to initialize firmware starting DVB feed\n");
  207. return ret;
  208. }
  209. ret = -EINVAL;
  210. switch (cx->card->type) {
  211. case CX18_CARD_HVR_1600_ESMT:
  212. case CX18_CARD_HVR_1600_SAMSUNG:
  213. v = cx18_read_reg(cx, CX18_REG_DMUX_NUM_PORT_0_CONTROL);
  214. v |= 0x00400000; /* Serial Mode */
  215. v |= 0x00002000; /* Data Length - Byte */
  216. v |= 0x00010000; /* Error - Polarity */
  217. v |= 0x00020000; /* Error - Passthru */
  218. v |= 0x000c0000; /* Error - Ignore */
  219. cx18_write_reg(cx, v, CX18_REG_DMUX_NUM_PORT_0_CONTROL);
  220. break;
  221. case CX18_CARD_LEADTEK_DVR3100H:
  222. case CX18_CARD_YUAN_MPC718:
  223. default:
  224. /* Assumption - Parallel transport - Signalling
  225. * undefined or default.
  226. */
  227. break;
  228. }
  229. if (!demux->dmx.frontend)
  230. return -EINVAL;
  231. if (!stream)
  232. return -EINVAL;
  233. mutex_lock(&stream->dvb.feedlock);
  234. if (stream->dvb.feeding++ == 0) {
  235. CX18_DEBUG_INFO("Starting Transport DMA\n");
  236. set_bit(CX18_F_S_STREAMING, &stream->s_flags);
  237. ret = cx18_start_v4l2_encode_stream(stream);
  238. if (ret < 0) {
  239. CX18_DEBUG_INFO("Failed to start Transport DMA\n");
  240. stream->dvb.feeding--;
  241. if (stream->dvb.feeding == 0)
  242. clear_bit(CX18_F_S_STREAMING, &stream->s_flags);
  243. }
  244. } else
  245. ret = 0;
  246. mutex_unlock(&stream->dvb.feedlock);
  247. return ret;
  248. }
  249. /* Kernel DVB framework calls this when the feed needs to stop. */
  250. static int cx18_dvb_stop_feed(struct dvb_demux_feed *feed)
  251. {
  252. struct dvb_demux *demux = feed->demux;
  253. struct cx18_stream *stream = (struct cx18_stream *)demux->priv;
  254. struct cx18 *cx = stream->cx;
  255. int ret = -EINVAL;
  256. CX18_DEBUG_INFO("Stop feed: pid = 0x%x index = %d\n",
  257. feed->pid, feed->index);
  258. if (stream) {
  259. mutex_lock(&stream->dvb.feedlock);
  260. if (--stream->dvb.feeding == 0) {
  261. CX18_DEBUG_INFO("Stopping Transport DMA\n");
  262. ret = cx18_stop_v4l2_encode_stream(stream, 0);
  263. } else
  264. ret = 0;
  265. mutex_unlock(&stream->dvb.feedlock);
  266. }
  267. return ret;
  268. }
  269. int cx18_dvb_register(struct cx18_stream *stream)
  270. {
  271. struct cx18 *cx = stream->cx;
  272. struct cx18_dvb *dvb = &stream->dvb;
  273. struct dvb_adapter *dvb_adapter;
  274. struct dvb_demux *dvbdemux;
  275. struct dmx_demux *dmx;
  276. int ret;
  277. if (!dvb)
  278. return -EINVAL;
  279. ret = dvb_register_adapter(&dvb->dvb_adapter,
  280. CX18_DRIVER_NAME,
  281. THIS_MODULE, &cx->pci_dev->dev, adapter_nr);
  282. if (ret < 0)
  283. goto err_out;
  284. dvb_adapter = &dvb->dvb_adapter;
  285. dvbdemux = &dvb->demux;
  286. dvbdemux->priv = (void *)stream;
  287. dvbdemux->filternum = 256;
  288. dvbdemux->feednum = 256;
  289. dvbdemux->start_feed = cx18_dvb_start_feed;
  290. dvbdemux->stop_feed = cx18_dvb_stop_feed;
  291. dvbdemux->dmx.capabilities = (DMX_TS_FILTERING |
  292. DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING);
  293. ret = dvb_dmx_init(dvbdemux);
  294. if (ret < 0)
  295. goto err_dvb_unregister_adapter;
  296. dmx = &dvbdemux->dmx;
  297. dvb->hw_frontend.source = DMX_FRONTEND_0;
  298. dvb->mem_frontend.source = DMX_MEMORY_FE;
  299. dvb->dmxdev.filternum = 256;
  300. dvb->dmxdev.demux = dmx;
  301. ret = dvb_dmxdev_init(&dvb->dmxdev, dvb_adapter);
  302. if (ret < 0)
  303. goto err_dvb_dmx_release;
  304. ret = dmx->add_frontend(dmx, &dvb->hw_frontend);
  305. if (ret < 0)
  306. goto err_dvb_dmxdev_release;
  307. ret = dmx->add_frontend(dmx, &dvb->mem_frontend);
  308. if (ret < 0)
  309. goto err_remove_hw_frontend;
  310. ret = dmx->connect_frontend(dmx, &dvb->hw_frontend);
  311. if (ret < 0)
  312. goto err_remove_mem_frontend;
  313. ret = dvb_register(stream);
  314. if (ret < 0)
  315. goto err_disconnect_frontend;
  316. dvb_net_init(dvb_adapter, &dvb->dvbnet, dmx);
  317. CX18_INFO("DVB Frontend registered\n");
  318. CX18_INFO("Registered DVB adapter%d for %s (%d x %d.%02d kB)\n",
  319. stream->dvb.dvb_adapter.num, stream->name,
  320. stream->buffers, stream->buf_size/1024,
  321. (stream->buf_size * 100 / 1024) % 100);
  322. mutex_init(&dvb->feedlock);
  323. dvb->enabled = 1;
  324. return ret;
  325. err_disconnect_frontend:
  326. dmx->disconnect_frontend(dmx);
  327. err_remove_mem_frontend:
  328. dmx->remove_frontend(dmx, &dvb->mem_frontend);
  329. err_remove_hw_frontend:
  330. dmx->remove_frontend(dmx, &dvb->hw_frontend);
  331. err_dvb_dmxdev_release:
  332. dvb_dmxdev_release(&dvb->dmxdev);
  333. err_dvb_dmx_release:
  334. dvb_dmx_release(dvbdemux);
  335. err_dvb_unregister_adapter:
  336. dvb_unregister_adapter(dvb_adapter);
  337. err_out:
  338. return ret;
  339. }
  340. void cx18_dvb_unregister(struct cx18_stream *stream)
  341. {
  342. struct cx18 *cx = stream->cx;
  343. struct cx18_dvb *dvb = &stream->dvb;
  344. struct dvb_adapter *dvb_adapter;
  345. struct dvb_demux *dvbdemux;
  346. struct dmx_demux *dmx;
  347. CX18_INFO("unregister DVB\n");
  348. dvb_adapter = &dvb->dvb_adapter;
  349. dvbdemux = &dvb->demux;
  350. dmx = &dvbdemux->dmx;
  351. dmx->close(dmx);
  352. dvb_net_release(&dvb->dvbnet);
  353. dmx->remove_frontend(dmx, &dvb->mem_frontend);
  354. dmx->remove_frontend(dmx, &dvb->hw_frontend);
  355. dvb_dmxdev_release(&dvb->dmxdev);
  356. dvb_dmx_release(dvbdemux);
  357. dvb_unregister_frontend(dvb->fe);
  358. dvb_frontend_detach(dvb->fe);
  359. dvb_unregister_adapter(dvb_adapter);
  360. }
  361. /* All the DVB attach calls go here, this function get's modified
  362. * for each new card. cx18_dvb_start_feed() will also need changes.
  363. */
  364. static int dvb_register(struct cx18_stream *stream)
  365. {
  366. struct cx18_dvb *dvb = &stream->dvb;
  367. struct cx18 *cx = stream->cx;
  368. int ret = 0;
  369. switch (cx->card->type) {
  370. case CX18_CARD_HVR_1600_ESMT:
  371. case CX18_CARD_HVR_1600_SAMSUNG:
  372. dvb->fe = dvb_attach(s5h1409_attach,
  373. &hauppauge_hvr1600_config,
  374. &cx->i2c_adap[0]);
  375. if (dvb->fe != NULL) {
  376. dvb_attach(mxl5005s_attach, dvb->fe,
  377. &cx->i2c_adap[0],
  378. &hauppauge_hvr1600_tuner);
  379. ret = 0;
  380. }
  381. break;
  382. case CX18_CARD_LEADTEK_DVR3100H:
  383. dvb->fe = dvb_attach(zl10353_attach,
  384. &leadtek_dvr3100h_demod,
  385. &cx->i2c_adap[1]);
  386. if (dvb->fe != NULL) {
  387. struct dvb_frontend *fe;
  388. struct xc2028_config cfg = {
  389. .i2c_adap = &cx->i2c_adap[1],
  390. .i2c_addr = 0xc2 >> 1,
  391. .ctrl = NULL,
  392. };
  393. static struct xc2028_ctrl ctrl = {
  394. .fname = XC2028_DEFAULT_FIRMWARE,
  395. .max_len = 64,
  396. .demod = XC3028_FE_ZARLINK456,
  397. .type = XC2028_AUTO,
  398. };
  399. fe = dvb_attach(xc2028_attach, dvb->fe, &cfg);
  400. if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
  401. fe->ops.tuner_ops.set_config(fe, &ctrl);
  402. }
  403. break;
  404. case CX18_CARD_YUAN_MPC718:
  405. /*
  406. * TODO
  407. * Apparently, these cards also could instead have a
  408. * DiBcom demod supported by one of the db7000 drivers
  409. */
  410. dvb->fe = dvb_attach(mt352_attach,
  411. &yuan_mpc718_mt352_demod,
  412. &cx->i2c_adap[1]);
  413. if (dvb->fe == NULL)
  414. dvb->fe = dvb_attach(zl10353_attach,
  415. &yuan_mpc718_zl10353_demod,
  416. &cx->i2c_adap[1]);
  417. if (dvb->fe != NULL) {
  418. struct dvb_frontend *fe;
  419. struct xc2028_config cfg = {
  420. .i2c_adap = &cx->i2c_adap[1],
  421. .i2c_addr = 0xc2 >> 1,
  422. .ctrl = NULL,
  423. };
  424. static struct xc2028_ctrl ctrl = {
  425. .fname = XC2028_DEFAULT_FIRMWARE,
  426. .max_len = 64,
  427. .demod = XC3028_FE_ZARLINK456,
  428. .type = XC2028_AUTO,
  429. };
  430. fe = dvb_attach(xc2028_attach, dvb->fe, &cfg);
  431. if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
  432. fe->ops.tuner_ops.set_config(fe, &ctrl);
  433. }
  434. break;
  435. default:
  436. /* No Digital Tv Support */
  437. break;
  438. }
  439. if (dvb->fe == NULL) {
  440. CX18_ERR("frontend initialization failed\n");
  441. return -1;
  442. }
  443. dvb->fe->callback = cx18_reset_tuner_gpio;
  444. ret = dvb_register_frontend(&dvb->dvb_adapter, dvb->fe);
  445. if (ret < 0) {
  446. if (dvb->fe->ops.release)
  447. dvb->fe->ops.release(dvb->fe);
  448. return ret;
  449. }
  450. /*
  451. * The firmware seems to enable the TS DMUX clock
  452. * under various circumstances. However, since we know we
  453. * might use it, let's just turn it on ourselves here.
  454. */
  455. cx18_write_reg_expect(cx,
  456. (CX18_DMUX_CLK_MASK << 16) | CX18_DMUX_CLK_MASK,
  457. CX18_CLOCK_ENABLE2,
  458. CX18_DMUX_CLK_MASK,
  459. (CX18_DMUX_CLK_MASK << 16) | CX18_DMUX_CLK_MASK);
  460. return ret;
  461. }