cx25821-video-upstream.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /*
  2. * Driver for the Conexant CX25821 PCIe bridge
  3. *
  4. * Copyright (C) 2009 Conexant Systems Inc.
  5. * Authors <hiep.huynh@conexant.com>, <shu.lin@conexant.com>
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include "cx25821-video.h"
  24. #include "cx25821-video-upstream.h"
  25. #include <linux/fs.h>
  26. #include <linux/errno.h>
  27. #include <linux/kernel.h>
  28. #include <linux/init.h>
  29. #include <linux/module.h>
  30. #include <linux/syscalls.h>
  31. #include <linux/file.h>
  32. #include <linux/fcntl.h>
  33. #include <linux/slab.h>
  34. #include <linux/uaccess.h>
  35. MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
  36. MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
  37. MODULE_LICENSE("GPL");
  38. static int _intr_msk = FLD_VID_SRC_RISC1 | FLD_VID_SRC_UF | FLD_VID_SRC_SYNC |
  39. FLD_VID_SRC_OPC_ERR;
  40. int cx25821_sram_channel_setup_upstream(struct cx25821_dev *dev,
  41. struct sram_channel *ch,
  42. unsigned int bpl, u32 risc)
  43. {
  44. unsigned int i, lines;
  45. u32 cdt;
  46. if (ch->cmds_start == 0) {
  47. cx_write(ch->ptr1_reg, 0);
  48. cx_write(ch->ptr2_reg, 0);
  49. cx_write(ch->cnt2_reg, 0);
  50. cx_write(ch->cnt1_reg, 0);
  51. return 0;
  52. }
  53. bpl = (bpl + 7) & ~7; /* alignment */
  54. cdt = ch->cdt;
  55. lines = ch->fifo_size / bpl;
  56. if (lines > 4)
  57. lines = 4;
  58. BUG_ON(lines < 2);
  59. /* write CDT */
  60. for (i = 0; i < lines; i++) {
  61. cx_write(cdt + 16 * i, ch->fifo_start + bpl * i);
  62. cx_write(cdt + 16 * i + 4, 0);
  63. cx_write(cdt + 16 * i + 8, 0);
  64. cx_write(cdt + 16 * i + 12, 0);
  65. }
  66. /* write CMDS */
  67. cx_write(ch->cmds_start + 0, risc);
  68. cx_write(ch->cmds_start + 4, 0);
  69. cx_write(ch->cmds_start + 8, cdt);
  70. cx_write(ch->cmds_start + 12, (lines * 16) >> 3);
  71. cx_write(ch->cmds_start + 16, ch->ctrl_start);
  72. cx_write(ch->cmds_start + 20, VID_IQ_SIZE_DW);
  73. for (i = 24; i < 80; i += 4)
  74. cx_write(ch->cmds_start + i, 0);
  75. /* fill registers */
  76. cx_write(ch->ptr1_reg, ch->fifo_start);
  77. cx_write(ch->ptr2_reg, cdt);
  78. cx_write(ch->cnt2_reg, (lines * 16) >> 3);
  79. cx_write(ch->cnt1_reg, (bpl >> 3) - 1);
  80. return 0;
  81. }
  82. static __le32 *cx25821_update_riscprogram(struct cx25821_dev *dev,
  83. __le32 *rp, unsigned int offset,
  84. unsigned int bpl, u32 sync_line,
  85. unsigned int lines, int fifo_enable,
  86. int field_type)
  87. {
  88. unsigned int line, i;
  89. int dist_betwn_starts = bpl * 2;
  90. *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
  91. if (USE_RISC_NOOP_VIDEO) {
  92. for (i = 0; i < NUM_NO_OPS; i++)
  93. *(rp++) = cpu_to_le32(RISC_NOOP);
  94. }
  95. /* scan lines */
  96. for (line = 0; line < lines; line++) {
  97. *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
  98. *(rp++) = cpu_to_le32(dev->_data_buf_phys_addr + offset);
  99. *(rp++) = cpu_to_le32(0); /* bits 63-32 */
  100. if ((lines <= NTSC_FIELD_HEIGHT)
  101. || (line < (NTSC_FIELD_HEIGHT - 1)) || !(dev->_isNTSC)) {
  102. offset += dist_betwn_starts;
  103. }
  104. }
  105. return rp;
  106. }
  107. static __le32 *cx25821_risc_field_upstream(struct cx25821_dev *dev, __le32 * rp,
  108. dma_addr_t databuf_phys_addr,
  109. unsigned int offset, u32 sync_line,
  110. unsigned int bpl, unsigned int lines,
  111. int fifo_enable, int field_type)
  112. {
  113. unsigned int line, i;
  114. struct sram_channel *sram_ch =
  115. dev->channels[dev->_channel_upstream_select].sram_channels;
  116. int dist_betwn_starts = bpl * 2;
  117. /* sync instruction */
  118. if (sync_line != NO_SYNC_LINE)
  119. *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
  120. if (USE_RISC_NOOP_VIDEO) {
  121. for (i = 0; i < NUM_NO_OPS; i++)
  122. *(rp++) = cpu_to_le32(RISC_NOOP);
  123. }
  124. /* scan lines */
  125. for (line = 0; line < lines; line++) {
  126. *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
  127. *(rp++) = cpu_to_le32(databuf_phys_addr + offset);
  128. *(rp++) = cpu_to_le32(0); /* bits 63-32 */
  129. if ((lines <= NTSC_FIELD_HEIGHT)
  130. || (line < (NTSC_FIELD_HEIGHT - 1)) || !(dev->_isNTSC))
  131. /* to skip the other field line */
  132. offset += dist_betwn_starts;
  133. /* check if we need to enable the FIFO after the first 4 lines
  134. * For the upstream video channel, the risc engine will enable
  135. * the FIFO. */
  136. if (fifo_enable && line == 3) {
  137. *(rp++) = RISC_WRITECR;
  138. *(rp++) = sram_ch->dma_ctl;
  139. *(rp++) = FLD_VID_FIFO_EN;
  140. *(rp++) = 0x00000001;
  141. }
  142. }
  143. return rp;
  144. }
  145. int cx25821_risc_buffer_upstream(struct cx25821_dev *dev,
  146. struct pci_dev *pci,
  147. unsigned int top_offset,
  148. unsigned int bpl, unsigned int lines)
  149. {
  150. __le32 *rp;
  151. int fifo_enable = 0;
  152. /* get line count for single field */
  153. int singlefield_lines = lines >> 1;
  154. int odd_num_lines = singlefield_lines;
  155. int frame = 0;
  156. int frame_size = 0;
  157. int databuf_offset = 0;
  158. int risc_program_size = 0;
  159. int risc_flag = RISC_CNT_RESET;
  160. unsigned int bottom_offset = bpl;
  161. dma_addr_t risc_phys_jump_addr;
  162. if (dev->_isNTSC) {
  163. odd_num_lines = singlefield_lines + 1;
  164. risc_program_size = FRAME1_VID_PROG_SIZE;
  165. frame_size = (bpl == Y411_LINE_SZ) ?
  166. FRAME_SIZE_NTSC_Y411 : FRAME_SIZE_NTSC_Y422;
  167. } else {
  168. risc_program_size = PAL_VID_PROG_SIZE;
  169. frame_size = (bpl == Y411_LINE_SZ) ?
  170. FRAME_SIZE_PAL_Y411 : FRAME_SIZE_PAL_Y422;
  171. }
  172. /* Virtual address of Risc buffer program */
  173. rp = dev->_dma_virt_addr;
  174. for (frame = 0; frame < NUM_FRAMES; frame++) {
  175. databuf_offset = frame_size * frame;
  176. if (UNSET != top_offset) {
  177. fifo_enable = (frame == 0) ? FIFO_ENABLE : FIFO_DISABLE;
  178. rp = cx25821_risc_field_upstream(dev, rp,
  179. dev->_data_buf_phys_addr +
  180. databuf_offset, top_offset, 0, bpl,
  181. odd_num_lines, fifo_enable, ODD_FIELD);
  182. }
  183. fifo_enable = FIFO_DISABLE;
  184. /* Even Field */
  185. rp = cx25821_risc_field_upstream(dev, rp,
  186. dev->_data_buf_phys_addr +
  187. databuf_offset, bottom_offset,
  188. 0x200, bpl, singlefield_lines,
  189. fifo_enable, EVEN_FIELD);
  190. if (frame == 0) {
  191. risc_flag = RISC_CNT_RESET;
  192. risc_phys_jump_addr = dev->_dma_phys_start_addr +
  193. risc_program_size;
  194. } else {
  195. risc_phys_jump_addr = dev->_dma_phys_start_addr;
  196. risc_flag = RISC_CNT_INC;
  197. }
  198. /* Loop to 2ndFrameRISC or to Start of Risc
  199. * program & generate IRQ
  200. */
  201. *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | risc_flag);
  202. *(rp++) = cpu_to_le32(risc_phys_jump_addr);
  203. *(rp++) = cpu_to_le32(0);
  204. }
  205. return 0;
  206. }
  207. void cx25821_stop_upstream_video_ch1(struct cx25821_dev *dev)
  208. {
  209. struct sram_channel *sram_ch =
  210. dev->channels[VID_UPSTREAM_SRAM_CHANNEL_I].sram_channels;
  211. u32 tmp = 0;
  212. if (!dev->_is_running) {
  213. pr_info("No video file is currently running so return!\n");
  214. return;
  215. }
  216. /* Disable RISC interrupts */
  217. tmp = cx_read(sram_ch->int_msk);
  218. cx_write(sram_ch->int_msk, tmp & ~_intr_msk);
  219. /* Turn OFF risc and fifo enable */
  220. tmp = cx_read(sram_ch->dma_ctl);
  221. cx_write(sram_ch->dma_ctl, tmp & ~(FLD_VID_FIFO_EN | FLD_VID_RISC_EN));
  222. /* Clear data buffer memory */
  223. if (dev->_data_buf_virt_addr)
  224. memset(dev->_data_buf_virt_addr, 0, dev->_data_buf_size);
  225. dev->_is_running = 0;
  226. dev->_is_first_frame = 0;
  227. dev->_frame_count = 0;
  228. dev->_file_status = END_OF_FILE;
  229. kfree(dev->_irq_queues);
  230. dev->_irq_queues = NULL;
  231. kfree(dev->_filename);
  232. tmp = cx_read(VID_CH_MODE_SEL);
  233. cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
  234. }
  235. void cx25821_free_mem_upstream_ch1(struct cx25821_dev *dev)
  236. {
  237. if (dev->_is_running)
  238. cx25821_stop_upstream_video_ch1(dev);
  239. if (dev->_dma_virt_addr) {
  240. pci_free_consistent(dev->pci, dev->_risc_size,
  241. dev->_dma_virt_addr, dev->_dma_phys_addr);
  242. dev->_dma_virt_addr = NULL;
  243. }
  244. if (dev->_data_buf_virt_addr) {
  245. pci_free_consistent(dev->pci, dev->_data_buf_size,
  246. dev->_data_buf_virt_addr,
  247. dev->_data_buf_phys_addr);
  248. dev->_data_buf_virt_addr = NULL;
  249. }
  250. }
  251. int cx25821_get_frame(struct cx25821_dev *dev, struct sram_channel *sram_ch)
  252. {
  253. struct file *myfile;
  254. int frame_index_temp = dev->_frame_index;
  255. int i = 0;
  256. int line_size = (dev->_pixel_format == PIXEL_FRMT_411) ?
  257. Y411_LINE_SZ : Y422_LINE_SZ;
  258. int frame_size = 0;
  259. int frame_offset = 0;
  260. ssize_t vfs_read_retval = 0;
  261. char mybuf[line_size];
  262. loff_t file_offset;
  263. loff_t pos;
  264. mm_segment_t old_fs;
  265. if (dev->_file_status == END_OF_FILE)
  266. return 0;
  267. if (dev->_isNTSC)
  268. frame_size = (line_size == Y411_LINE_SZ) ?
  269. FRAME_SIZE_NTSC_Y411 : FRAME_SIZE_NTSC_Y422;
  270. else
  271. frame_size = (line_size == Y411_LINE_SZ) ?
  272. FRAME_SIZE_PAL_Y411 : FRAME_SIZE_PAL_Y422;
  273. frame_offset = (frame_index_temp > 0) ? frame_size : 0;
  274. file_offset = dev->_frame_count * frame_size;
  275. myfile = filp_open(dev->_filename, O_RDONLY | O_LARGEFILE, 0);
  276. if (IS_ERR(myfile)) {
  277. const int open_errno = -PTR_ERR(myfile);
  278. pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
  279. __func__, dev->_filename, open_errno);
  280. return PTR_ERR(myfile);
  281. } else {
  282. if (!(myfile->f_op)) {
  283. pr_err("%s(): File has no file operations registered!\n",
  284. __func__);
  285. filp_close(myfile, NULL);
  286. return -EIO;
  287. }
  288. if (!myfile->f_op->read) {
  289. pr_err("%s(): File has no READ operations registered!\n",
  290. __func__);
  291. filp_close(myfile, NULL);
  292. return -EIO;
  293. }
  294. pos = myfile->f_pos;
  295. old_fs = get_fs();
  296. set_fs(KERNEL_DS);
  297. for (i = 0; i < dev->_lines_count; i++) {
  298. pos = file_offset;
  299. vfs_read_retval = vfs_read(myfile, mybuf, line_size,
  300. &pos);
  301. if (vfs_read_retval > 0 && vfs_read_retval == line_size
  302. && dev->_data_buf_virt_addr != NULL) {
  303. memcpy((void *)(dev->_data_buf_virt_addr +
  304. frame_offset / 4), mybuf,
  305. vfs_read_retval);
  306. }
  307. file_offset += vfs_read_retval;
  308. frame_offset += vfs_read_retval;
  309. if (vfs_read_retval < line_size) {
  310. pr_info("Done: exit %s() since no more bytes to read from Video file\n",
  311. __func__);
  312. break;
  313. }
  314. }
  315. if (i > 0)
  316. dev->_frame_count++;
  317. dev->_file_status = (vfs_read_retval == line_size) ?
  318. IN_PROGRESS : END_OF_FILE;
  319. set_fs(old_fs);
  320. filp_close(myfile, NULL);
  321. }
  322. return 0;
  323. }
  324. static void cx25821_vidups_handler(struct work_struct *work)
  325. {
  326. struct cx25821_dev *dev = container_of(work, struct cx25821_dev,
  327. _irq_work_entry);
  328. if (!dev) {
  329. pr_err("ERROR %s(): since container_of(work_struct) FAILED!\n",
  330. __func__);
  331. return;
  332. }
  333. cx25821_get_frame(dev, dev->channels[dev->_channel_upstream_select].
  334. sram_channels);
  335. }
  336. int cx25821_openfile(struct cx25821_dev *dev, struct sram_channel *sram_ch)
  337. {
  338. struct file *myfile;
  339. int i = 0, j = 0;
  340. int line_size = (dev->_pixel_format == PIXEL_FRMT_411) ?
  341. Y411_LINE_SZ : Y422_LINE_SZ;
  342. ssize_t vfs_read_retval = 0;
  343. char mybuf[line_size];
  344. loff_t pos;
  345. loff_t offset = (unsigned long)0;
  346. mm_segment_t old_fs;
  347. myfile = filp_open(dev->_filename, O_RDONLY | O_LARGEFILE, 0);
  348. if (IS_ERR(myfile)) {
  349. const int open_errno = -PTR_ERR(myfile);
  350. pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
  351. __func__, dev->_filename, open_errno);
  352. return PTR_ERR(myfile);
  353. } else {
  354. if (!(myfile->f_op)) {
  355. pr_err("%s(): File has no file operations registered!\n",
  356. __func__);
  357. filp_close(myfile, NULL);
  358. return -EIO;
  359. }
  360. if (!myfile->f_op->read) {
  361. pr_err("%s(): File has no READ operations registered! Returning\n",
  362. __func__);
  363. filp_close(myfile, NULL);
  364. return -EIO;
  365. }
  366. pos = myfile->f_pos;
  367. old_fs = get_fs();
  368. set_fs(KERNEL_DS);
  369. for (j = 0; j < NUM_FRAMES; j++) {
  370. for (i = 0; i < dev->_lines_count; i++) {
  371. pos = offset;
  372. vfs_read_retval = vfs_read(myfile, mybuf,
  373. line_size, &pos);
  374. if (vfs_read_retval > 0
  375. && vfs_read_retval == line_size
  376. && dev->_data_buf_virt_addr != NULL) {
  377. memcpy((void *)(dev->
  378. _data_buf_virt_addr +
  379. offset / 4), mybuf,
  380. vfs_read_retval);
  381. }
  382. offset += vfs_read_retval;
  383. if (vfs_read_retval < line_size) {
  384. pr_info("Done: exit %s() since no more bytes to read from Video file\n",
  385. __func__);
  386. break;
  387. }
  388. }
  389. if (i > 0)
  390. dev->_frame_count++;
  391. if (vfs_read_retval < line_size)
  392. break;
  393. }
  394. dev->_file_status = (vfs_read_retval == line_size) ?
  395. IN_PROGRESS : END_OF_FILE;
  396. set_fs(old_fs);
  397. myfile->f_pos = 0;
  398. filp_close(myfile, NULL);
  399. }
  400. return 0;
  401. }
  402. int cx25821_upstream_buffer_prepare(struct cx25821_dev *dev,
  403. struct sram_channel *sram_ch, int bpl)
  404. {
  405. int ret = 0;
  406. dma_addr_t dma_addr;
  407. dma_addr_t data_dma_addr;
  408. if (dev->_dma_virt_addr != NULL)
  409. pci_free_consistent(dev->pci, dev->upstream_riscbuf_size,
  410. dev->_dma_virt_addr, dev->_dma_phys_addr);
  411. dev->_dma_virt_addr = pci_alloc_consistent(dev->pci,
  412. dev->upstream_riscbuf_size, &dma_addr);
  413. dev->_dma_virt_start_addr = dev->_dma_virt_addr;
  414. dev->_dma_phys_start_addr = dma_addr;
  415. dev->_dma_phys_addr = dma_addr;
  416. dev->_risc_size = dev->upstream_riscbuf_size;
  417. if (!dev->_dma_virt_addr) {
  418. pr_err("FAILED to allocate memory for Risc buffer! Returning\n");
  419. return -ENOMEM;
  420. }
  421. /* Clear memory at address */
  422. memset(dev->_dma_virt_addr, 0, dev->_risc_size);
  423. if (dev->_data_buf_virt_addr != NULL)
  424. pci_free_consistent(dev->pci, dev->upstream_databuf_size,
  425. dev->_data_buf_virt_addr,
  426. dev->_data_buf_phys_addr);
  427. /* For Video Data buffer allocation */
  428. dev->_data_buf_virt_addr = pci_alloc_consistent(dev->pci,
  429. dev->upstream_databuf_size, &data_dma_addr);
  430. dev->_data_buf_phys_addr = data_dma_addr;
  431. dev->_data_buf_size = dev->upstream_databuf_size;
  432. if (!dev->_data_buf_virt_addr) {
  433. pr_err("FAILED to allocate memory for data buffer! Returning\n");
  434. return -ENOMEM;
  435. }
  436. /* Clear memory at address */
  437. memset(dev->_data_buf_virt_addr, 0, dev->_data_buf_size);
  438. ret = cx25821_openfile(dev, sram_ch);
  439. if (ret < 0)
  440. return ret;
  441. /* Create RISC programs */
  442. ret = cx25821_risc_buffer_upstream(dev, dev->pci, 0, bpl,
  443. dev->_lines_count);
  444. if (ret < 0) {
  445. pr_info("Failed creating Video Upstream Risc programs!\n");
  446. goto error;
  447. }
  448. return 0;
  449. error:
  450. return ret;
  451. }
  452. int cx25821_video_upstream_irq(struct cx25821_dev *dev, int chan_num,
  453. u32 status)
  454. {
  455. u32 int_msk_tmp;
  456. struct sram_channel *channel = dev->channels[chan_num].sram_channels;
  457. int singlefield_lines = NTSC_FIELD_HEIGHT;
  458. int line_size_in_bytes = Y422_LINE_SZ;
  459. int odd_risc_prog_size = 0;
  460. dma_addr_t risc_phys_jump_addr;
  461. __le32 *rp;
  462. if (status & FLD_VID_SRC_RISC1) {
  463. /* We should only process one program per call */
  464. u32 prog_cnt = cx_read(channel->gpcnt);
  465. /* Since we've identified our IRQ, clear our bits from the
  466. * interrupt mask and interrupt status registers */
  467. int_msk_tmp = cx_read(channel->int_msk);
  468. cx_write(channel->int_msk, int_msk_tmp & ~_intr_msk);
  469. cx_write(channel->int_stat, _intr_msk);
  470. spin_lock(&dev->slock);
  471. dev->_frame_index = prog_cnt;
  472. queue_work(dev->_irq_queues, &dev->_irq_work_entry);
  473. if (dev->_is_first_frame) {
  474. dev->_is_first_frame = 0;
  475. if (dev->_isNTSC) {
  476. singlefield_lines += 1;
  477. odd_risc_prog_size = ODD_FLD_NTSC_PROG_SIZE;
  478. } else {
  479. singlefield_lines = PAL_FIELD_HEIGHT;
  480. odd_risc_prog_size = ODD_FLD_PAL_PROG_SIZE;
  481. }
  482. if (dev->_dma_virt_start_addr != NULL) {
  483. line_size_in_bytes =
  484. (dev->_pixel_format ==
  485. PIXEL_FRMT_411) ? Y411_LINE_SZ :
  486. Y422_LINE_SZ;
  487. risc_phys_jump_addr =
  488. dev->_dma_phys_start_addr +
  489. odd_risc_prog_size;
  490. rp = cx25821_update_riscprogram(dev,
  491. dev->_dma_virt_start_addr, TOP_OFFSET,
  492. line_size_in_bytes, 0x0,
  493. singlefield_lines, FIFO_DISABLE,
  494. ODD_FIELD);
  495. /* Jump to Even Risc program of 1st Frame */
  496. *(rp++) = cpu_to_le32(RISC_JUMP);
  497. *(rp++) = cpu_to_le32(risc_phys_jump_addr);
  498. *(rp++) = cpu_to_le32(0);
  499. }
  500. }
  501. spin_unlock(&dev->slock);
  502. } else {
  503. if (status & FLD_VID_SRC_UF)
  504. pr_err("%s(): Video Received Underflow Error Interrupt!\n",
  505. __func__);
  506. if (status & FLD_VID_SRC_SYNC)
  507. pr_err("%s(): Video Received Sync Error Interrupt!\n",
  508. __func__);
  509. if (status & FLD_VID_SRC_OPC_ERR)
  510. pr_err("%s(): Video Received OpCode Error Interrupt!\n",
  511. __func__);
  512. }
  513. if (dev->_file_status == END_OF_FILE) {
  514. pr_err("EOF Channel 1 Framecount = %d\n", dev->_frame_count);
  515. return -1;
  516. }
  517. /* ElSE, set the interrupt mask register, re-enable irq. */
  518. int_msk_tmp = cx_read(channel->int_msk);
  519. cx_write(channel->int_msk, int_msk_tmp |= _intr_msk);
  520. return 0;
  521. }
  522. static irqreturn_t cx25821_upstream_irq(int irq, void *dev_id)
  523. {
  524. struct cx25821_dev *dev = dev_id;
  525. u32 msk_stat, vid_status;
  526. int handled = 0;
  527. int channel_num = 0;
  528. struct sram_channel *sram_ch;
  529. if (!dev)
  530. return -1;
  531. channel_num = VID_UPSTREAM_SRAM_CHANNEL_I;
  532. sram_ch = dev->channels[channel_num].sram_channels;
  533. msk_stat = cx_read(sram_ch->int_mstat);
  534. vid_status = cx_read(sram_ch->int_stat);
  535. /* Only deal with our interrupt */
  536. if (vid_status)
  537. handled = cx25821_video_upstream_irq(dev, channel_num,
  538. vid_status);
  539. if (handled < 0)
  540. cx25821_stop_upstream_video_ch1(dev);
  541. else
  542. handled += handled;
  543. return IRQ_RETVAL(handled);
  544. }
  545. void cx25821_set_pixelengine(struct cx25821_dev *dev, struct sram_channel *ch,
  546. int pix_format)
  547. {
  548. int width = WIDTH_D1;
  549. int height = dev->_lines_count;
  550. int num_lines, odd_num_lines;
  551. u32 value;
  552. int vip_mode = OUTPUT_FRMT_656;
  553. value = ((pix_format & 0x3) << 12) | (vip_mode & 0x7);
  554. value &= 0xFFFFFFEF;
  555. value |= dev->_isNTSC ? 0 : 0x10;
  556. cx_write(ch->vid_fmt_ctl, value);
  557. /* set number of active pixels in each line.
  558. * Default is 720 pixels in both NTSC and PAL format */
  559. cx_write(ch->vid_active_ctl1, width);
  560. num_lines = (height / 2) & 0x3FF;
  561. odd_num_lines = num_lines;
  562. if (dev->_isNTSC)
  563. odd_num_lines += 1;
  564. value = (num_lines << 16) | odd_num_lines;
  565. /* set number of active lines in field 0 (top) and field 1 (bottom) */
  566. cx_write(ch->vid_active_ctl2, value);
  567. cx_write(ch->vid_cdt_size, VID_CDT_SIZE >> 3);
  568. }
  569. int cx25821_start_video_dma_upstream(struct cx25821_dev *dev,
  570. struct sram_channel *sram_ch)
  571. {
  572. u32 tmp = 0;
  573. int err = 0;
  574. /* 656/VIP SRC Upstream Channel I & J and 7 - Host Bus Interface for
  575. * channel A-C
  576. */
  577. tmp = cx_read(VID_CH_MODE_SEL);
  578. cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);
  579. /* Set the physical start address of the RISC program in the initial
  580. * program counter(IPC) member of the cmds.
  581. */
  582. cx_write(sram_ch->cmds_start + 0, dev->_dma_phys_addr);
  583. /* Risc IPC High 64 bits 63-32 */
  584. cx_write(sram_ch->cmds_start + 4, 0);
  585. /* reset counter */
  586. cx_write(sram_ch->gpcnt_ctl, 3);
  587. /* Clear our bits from the interrupt status register. */
  588. cx_write(sram_ch->int_stat, _intr_msk);
  589. /* Set the interrupt mask register, enable irq. */
  590. cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << sram_ch->irq_bit));
  591. tmp = cx_read(sram_ch->int_msk);
  592. cx_write(sram_ch->int_msk, tmp |= _intr_msk);
  593. err = request_irq(dev->pci->irq, cx25821_upstream_irq,
  594. IRQF_SHARED, dev->name, dev);
  595. if (err < 0) {
  596. pr_err("%s: can't get upstream IRQ %d\n",
  597. dev->name, dev->pci->irq);
  598. goto fail_irq;
  599. }
  600. /* Start the DMA engine */
  601. tmp = cx_read(sram_ch->dma_ctl);
  602. cx_set(sram_ch->dma_ctl, tmp | FLD_VID_RISC_EN);
  603. dev->_is_running = 1;
  604. dev->_is_first_frame = 1;
  605. return 0;
  606. fail_irq:
  607. cx25821_dev_unregister(dev);
  608. return err;
  609. }
  610. int cx25821_vidupstream_init_ch1(struct cx25821_dev *dev, int channel_select,
  611. int pixel_format)
  612. {
  613. struct sram_channel *sram_ch;
  614. u32 tmp;
  615. int retval = 0;
  616. int err = 0;
  617. int data_frame_size = 0;
  618. int risc_buffer_size = 0;
  619. int str_length = 0;
  620. if (dev->_is_running) {
  621. pr_info("Video Channel is still running so return!\n");
  622. return 0;
  623. }
  624. dev->_channel_upstream_select = channel_select;
  625. sram_ch = dev->channels[channel_select].sram_channels;
  626. INIT_WORK(&dev->_irq_work_entry, cx25821_vidups_handler);
  627. dev->_irq_queues = create_singlethread_workqueue("cx25821_workqueue");
  628. if (!dev->_irq_queues) {
  629. pr_err("create_singlethread_workqueue() for Video FAILED!\n");
  630. return -ENOMEM;
  631. }
  632. /* 656/VIP SRC Upstream Channel I & J and 7 - Host Bus Interface for
  633. * channel A-C
  634. */
  635. tmp = cx_read(VID_CH_MODE_SEL);
  636. cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);
  637. dev->_is_running = 0;
  638. dev->_frame_count = 0;
  639. dev->_file_status = RESET_STATUS;
  640. dev->_lines_count = dev->_isNTSC ? 480 : 576;
  641. dev->_pixel_format = pixel_format;
  642. dev->_line_size = (dev->_pixel_format == PIXEL_FRMT_422) ?
  643. (WIDTH_D1 * 2) : (WIDTH_D1 * 3) / 2;
  644. data_frame_size = dev->_isNTSC ? NTSC_DATA_BUF_SZ : PAL_DATA_BUF_SZ;
  645. risc_buffer_size = dev->_isNTSC ?
  646. NTSC_RISC_BUF_SIZE : PAL_RISC_BUF_SIZE;
  647. if (dev->input_filename) {
  648. str_length = strlen(dev->input_filename);
  649. dev->_filename = kmemdup(dev->input_filename, str_length + 1,
  650. GFP_KERNEL);
  651. if (!dev->_filename)
  652. goto error;
  653. } else {
  654. str_length = strlen(dev->_defaultname);
  655. dev->_filename = kmemdup(dev->_defaultname, str_length + 1,
  656. GFP_KERNEL);
  657. if (!dev->_filename)
  658. goto error;
  659. }
  660. /* Default if filename is empty string */
  661. if (strcmp(dev->input_filename, "") == 0) {
  662. if (dev->_isNTSC) {
  663. dev->_filename =
  664. (dev->_pixel_format == PIXEL_FRMT_411) ?
  665. "/root/vid411.yuv" : "/root/vidtest.yuv";
  666. } else {
  667. dev->_filename =
  668. (dev->_pixel_format == PIXEL_FRMT_411) ?
  669. "/root/pal411.yuv" : "/root/pal422.yuv";
  670. }
  671. }
  672. dev->_is_running = 0;
  673. dev->_frame_count = 0;
  674. dev->_file_status = RESET_STATUS;
  675. dev->_lines_count = dev->_isNTSC ? 480 : 576;
  676. dev->_pixel_format = pixel_format;
  677. dev->_line_size = (dev->_pixel_format == PIXEL_FRMT_422) ?
  678. (WIDTH_D1 * 2) : (WIDTH_D1 * 3) / 2;
  679. retval = cx25821_sram_channel_setup_upstream(dev, sram_ch,
  680. dev->_line_size, 0);
  681. /* setup fifo + format */
  682. cx25821_set_pixelengine(dev, sram_ch, dev->_pixel_format);
  683. dev->upstream_riscbuf_size = risc_buffer_size * 2;
  684. dev->upstream_databuf_size = data_frame_size * 2;
  685. /* Allocating buffers and prepare RISC program */
  686. retval = cx25821_upstream_buffer_prepare(dev, sram_ch, dev->_line_size);
  687. if (retval < 0) {
  688. pr_err("%s: Failed to set up Video upstream buffers!\n",
  689. dev->name);
  690. goto error;
  691. }
  692. cx25821_start_video_dma_upstream(dev, sram_ch);
  693. return 0;
  694. error:
  695. cx25821_dev_unregister(dev);
  696. return err;
  697. }