cx25821-video-upstream.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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 =
  166. (bpl ==
  167. Y411_LINE_SZ) ? FRAME_SIZE_NTSC_Y411 :
  168. FRAME_SIZE_NTSC_Y422;
  169. } else {
  170. risc_program_size = PAL_VID_PROG_SIZE;
  171. frame_size =
  172. (bpl ==
  173. Y411_LINE_SZ) ? FRAME_SIZE_PAL_Y411 : FRAME_SIZE_PAL_Y422;
  174. }
  175. /* Virtual address of Risc buffer program */
  176. rp = dev->_dma_virt_addr;
  177. for (frame = 0; frame < NUM_FRAMES; frame++) {
  178. databuf_offset = frame_size * frame;
  179. if (UNSET != top_offset) {
  180. fifo_enable = (frame == 0) ? FIFO_ENABLE : FIFO_DISABLE;
  181. rp = cx25821_risc_field_upstream(dev, rp,
  182. dev->
  183. _data_buf_phys_addr +
  184. databuf_offset,
  185. top_offset, 0, bpl,
  186. odd_num_lines,
  187. fifo_enable,
  188. ODD_FIELD);
  189. }
  190. fifo_enable = FIFO_DISABLE;
  191. /* Even Field */
  192. rp = cx25821_risc_field_upstream(dev, rp,
  193. dev->_data_buf_phys_addr +
  194. databuf_offset, bottom_offset,
  195. 0x200, bpl, singlefield_lines,
  196. fifo_enable, EVEN_FIELD);
  197. if (frame == 0) {
  198. risc_flag = RISC_CNT_RESET;
  199. risc_phys_jump_addr =
  200. dev->_dma_phys_start_addr + risc_program_size;
  201. } else {
  202. risc_phys_jump_addr = dev->_dma_phys_start_addr;
  203. risc_flag = RISC_CNT_INC;
  204. }
  205. /* Loop to 2ndFrameRISC or to Start of Risc
  206. * program & generate IRQ
  207. */
  208. *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | risc_flag);
  209. *(rp++) = cpu_to_le32(risc_phys_jump_addr);
  210. *(rp++) = cpu_to_le32(0);
  211. }
  212. return 0;
  213. }
  214. void cx25821_stop_upstream_video_ch1(struct cx25821_dev *dev)
  215. {
  216. struct sram_channel *sram_ch =
  217. dev->channels[VID_UPSTREAM_SRAM_CHANNEL_I].sram_channels;
  218. u32 tmp = 0;
  219. if (!dev->_is_running) {
  220. pr_info("No video file is currently running so return!\n");
  221. return;
  222. }
  223. /* Disable RISC interrupts */
  224. tmp = cx_read(sram_ch->int_msk);
  225. cx_write(sram_ch->int_msk, tmp & ~_intr_msk);
  226. /* Turn OFF risc and fifo enable */
  227. tmp = cx_read(sram_ch->dma_ctl);
  228. cx_write(sram_ch->dma_ctl, tmp & ~(FLD_VID_FIFO_EN | FLD_VID_RISC_EN));
  229. /* Clear data buffer memory */
  230. if (dev->_data_buf_virt_addr)
  231. memset(dev->_data_buf_virt_addr, 0, dev->_data_buf_size);
  232. dev->_is_running = 0;
  233. dev->_is_first_frame = 0;
  234. dev->_frame_count = 0;
  235. dev->_file_status = END_OF_FILE;
  236. kfree(dev->_irq_queues);
  237. dev->_irq_queues = NULL;
  238. kfree(dev->_filename);
  239. tmp = cx_read(VID_CH_MODE_SEL);
  240. cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
  241. }
  242. void cx25821_free_mem_upstream_ch1(struct cx25821_dev *dev)
  243. {
  244. if (dev->_is_running)
  245. cx25821_stop_upstream_video_ch1(dev);
  246. if (dev->_dma_virt_addr) {
  247. pci_free_consistent(dev->pci, dev->_risc_size,
  248. dev->_dma_virt_addr, dev->_dma_phys_addr);
  249. dev->_dma_virt_addr = NULL;
  250. }
  251. if (dev->_data_buf_virt_addr) {
  252. pci_free_consistent(dev->pci, dev->_data_buf_size,
  253. dev->_data_buf_virt_addr,
  254. dev->_data_buf_phys_addr);
  255. dev->_data_buf_virt_addr = NULL;
  256. }
  257. }
  258. int cx25821_get_frame(struct cx25821_dev *dev, struct sram_channel *sram_ch)
  259. {
  260. struct file *myfile;
  261. int frame_index_temp = dev->_frame_index;
  262. int i = 0;
  263. int line_size =
  264. (dev->_pixel_format ==
  265. PIXEL_FRMT_411) ? Y411_LINE_SZ : Y422_LINE_SZ;
  266. int frame_size = 0;
  267. int frame_offset = 0;
  268. ssize_t vfs_read_retval = 0;
  269. char mybuf[line_size];
  270. loff_t file_offset;
  271. loff_t pos;
  272. mm_segment_t old_fs;
  273. if (dev->_file_status == END_OF_FILE)
  274. return 0;
  275. if (dev->_isNTSC) {
  276. frame_size =
  277. (line_size ==
  278. Y411_LINE_SZ) ? FRAME_SIZE_NTSC_Y411 :
  279. FRAME_SIZE_NTSC_Y422;
  280. } else {
  281. frame_size =
  282. (line_size ==
  283. Y411_LINE_SZ) ? FRAME_SIZE_PAL_Y411 : FRAME_SIZE_PAL_Y422;
  284. }
  285. frame_offset = (frame_index_temp > 0) ? frame_size : 0;
  286. file_offset = dev->_frame_count * frame_size;
  287. myfile = filp_open(dev->_filename, O_RDONLY | O_LARGEFILE, 0);
  288. if (IS_ERR(myfile)) {
  289. const int open_errno = -PTR_ERR(myfile);
  290. pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
  291. __func__, dev->_filename, open_errno);
  292. return PTR_ERR(myfile);
  293. } else {
  294. if (!(myfile->f_op)) {
  295. pr_err("%s(): File has no file operations registered!\n",
  296. __func__);
  297. filp_close(myfile, NULL);
  298. return -EIO;
  299. }
  300. if (!myfile->f_op->read) {
  301. pr_err("%s(): File has no READ operations registered!\n",
  302. __func__);
  303. filp_close(myfile, NULL);
  304. return -EIO;
  305. }
  306. pos = myfile->f_pos;
  307. old_fs = get_fs();
  308. set_fs(KERNEL_DS);
  309. for (i = 0; i < dev->_lines_count; i++) {
  310. pos = file_offset;
  311. vfs_read_retval =
  312. vfs_read(myfile, mybuf, line_size, &pos);
  313. if (vfs_read_retval > 0 && vfs_read_retval == line_size
  314. && dev->_data_buf_virt_addr != NULL) {
  315. memcpy((void *)(dev->_data_buf_virt_addr +
  316. frame_offset / 4), mybuf,
  317. vfs_read_retval);
  318. }
  319. file_offset += vfs_read_retval;
  320. frame_offset += vfs_read_retval;
  321. if (vfs_read_retval < line_size) {
  322. pr_info("Done: exit %s() since no more bytes to read from Video file\n",
  323. __func__);
  324. break;
  325. }
  326. }
  327. if (i > 0)
  328. dev->_frame_count++;
  329. dev->_file_status =
  330. (vfs_read_retval == line_size) ? IN_PROGRESS : END_OF_FILE;
  331. set_fs(old_fs);
  332. filp_close(myfile, NULL);
  333. }
  334. return 0;
  335. }
  336. static void cx25821_vidups_handler(struct work_struct *work)
  337. {
  338. struct cx25821_dev *dev =
  339. container_of(work, struct cx25821_dev, _irq_work_entry);
  340. if (!dev) {
  341. pr_err("ERROR %s(): since container_of(work_struct) FAILED!\n",
  342. __func__);
  343. return;
  344. }
  345. cx25821_get_frame(dev,
  346. dev->channels[dev->_channel_upstream_select].
  347. sram_channels);
  348. }
  349. int cx25821_openfile(struct cx25821_dev *dev, struct sram_channel *sram_ch)
  350. {
  351. struct file *myfile;
  352. int i = 0, j = 0;
  353. int line_size =
  354. (dev->_pixel_format ==
  355. PIXEL_FRMT_411) ? Y411_LINE_SZ : Y422_LINE_SZ;
  356. ssize_t vfs_read_retval = 0;
  357. char mybuf[line_size];
  358. loff_t pos;
  359. loff_t offset = (unsigned long)0;
  360. mm_segment_t old_fs;
  361. myfile = filp_open(dev->_filename, O_RDONLY | O_LARGEFILE, 0);
  362. if (IS_ERR(myfile)) {
  363. const int open_errno = -PTR_ERR(myfile);
  364. pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
  365. __func__, dev->_filename, open_errno);
  366. return PTR_ERR(myfile);
  367. } else {
  368. if (!(myfile->f_op)) {
  369. pr_err("%s(): File has no file operations registered!\n",
  370. __func__);
  371. filp_close(myfile, NULL);
  372. return -EIO;
  373. }
  374. if (!myfile->f_op->read) {
  375. pr_err("%s(): File has no READ operations registered! Returning\n",
  376. __func__);
  377. filp_close(myfile, NULL);
  378. return -EIO;
  379. }
  380. pos = myfile->f_pos;
  381. old_fs = get_fs();
  382. set_fs(KERNEL_DS);
  383. for (j = 0; j < NUM_FRAMES; j++) {
  384. for (i = 0; i < dev->_lines_count; i++) {
  385. pos = offset;
  386. vfs_read_retval =
  387. vfs_read(myfile, mybuf, line_size, &pos);
  388. if (vfs_read_retval > 0
  389. && vfs_read_retval == line_size
  390. && dev->_data_buf_virt_addr != NULL) {
  391. memcpy((void *)(dev->
  392. _data_buf_virt_addr +
  393. offset / 4), mybuf,
  394. vfs_read_retval);
  395. }
  396. offset += vfs_read_retval;
  397. if (vfs_read_retval < line_size) {
  398. pr_info("Done: exit %s() since no more bytes to read from Video file\n",
  399. __func__);
  400. break;
  401. }
  402. }
  403. if (i > 0)
  404. dev->_frame_count++;
  405. if (vfs_read_retval < line_size)
  406. break;
  407. }
  408. dev->_file_status =
  409. (vfs_read_retval == line_size) ? IN_PROGRESS : END_OF_FILE;
  410. set_fs(old_fs);
  411. myfile->f_pos = 0;
  412. filp_close(myfile, NULL);
  413. }
  414. return 0;
  415. }
  416. int cx25821_upstream_buffer_prepare(struct cx25821_dev *dev,
  417. struct sram_channel *sram_ch, int bpl)
  418. {
  419. int ret = 0;
  420. dma_addr_t dma_addr;
  421. dma_addr_t data_dma_addr;
  422. if (dev->_dma_virt_addr != NULL) {
  423. pci_free_consistent(dev->pci, dev->upstream_riscbuf_size,
  424. dev->_dma_virt_addr, dev->_dma_phys_addr);
  425. }
  426. dev->_dma_virt_addr =
  427. pci_alloc_consistent(dev->pci, dev->upstream_riscbuf_size,
  428. &dma_addr);
  429. dev->_dma_virt_start_addr = dev->_dma_virt_addr;
  430. dev->_dma_phys_start_addr = dma_addr;
  431. dev->_dma_phys_addr = dma_addr;
  432. dev->_risc_size = dev->upstream_riscbuf_size;
  433. if (!dev->_dma_virt_addr) {
  434. pr_err("FAILED to allocate memory for Risc buffer! Returning\n");
  435. return -ENOMEM;
  436. }
  437. /* Clear memory at address */
  438. memset(dev->_dma_virt_addr, 0, dev->_risc_size);
  439. if (dev->_data_buf_virt_addr != NULL) {
  440. pci_free_consistent(dev->pci, dev->upstream_databuf_size,
  441. dev->_data_buf_virt_addr,
  442. dev->_data_buf_phys_addr);
  443. }
  444. /* For Video Data buffer allocation */
  445. dev->_data_buf_virt_addr =
  446. pci_alloc_consistent(dev->pci, dev->upstream_databuf_size,
  447. &data_dma_addr);
  448. dev->_data_buf_phys_addr = data_dma_addr;
  449. dev->_data_buf_size = dev->upstream_databuf_size;
  450. if (!dev->_data_buf_virt_addr) {
  451. pr_err("FAILED to allocate memory for data buffer! Returning\n");
  452. return -ENOMEM;
  453. }
  454. /* Clear memory at address */
  455. memset(dev->_data_buf_virt_addr, 0, dev->_data_buf_size);
  456. ret = cx25821_openfile(dev, sram_ch);
  457. if (ret < 0)
  458. return ret;
  459. /* Create RISC programs */
  460. ret =
  461. cx25821_risc_buffer_upstream(dev, dev->pci, 0, bpl,
  462. dev->_lines_count);
  463. if (ret < 0) {
  464. pr_info("Failed creating Video Upstream Risc programs!\n");
  465. goto error;
  466. }
  467. return 0;
  468. error:
  469. return ret;
  470. }
  471. int cx25821_video_upstream_irq(struct cx25821_dev *dev, int chan_num,
  472. u32 status)
  473. {
  474. u32 int_msk_tmp;
  475. struct sram_channel *channel = dev->channels[chan_num].sram_channels;
  476. int singlefield_lines = NTSC_FIELD_HEIGHT;
  477. int line_size_in_bytes = Y422_LINE_SZ;
  478. int odd_risc_prog_size = 0;
  479. dma_addr_t risc_phys_jump_addr;
  480. __le32 *rp;
  481. if (status & FLD_VID_SRC_RISC1) {
  482. /* We should only process one program per call */
  483. u32 prog_cnt = cx_read(channel->gpcnt);
  484. /* Since we've identified our IRQ, clear our bits from the
  485. * interrupt mask and interrupt status registers */
  486. int_msk_tmp = cx_read(channel->int_msk);
  487. cx_write(channel->int_msk, int_msk_tmp & ~_intr_msk);
  488. cx_write(channel->int_stat, _intr_msk);
  489. spin_lock(&dev->slock);
  490. dev->_frame_index = prog_cnt;
  491. queue_work(dev->_irq_queues, &dev->_irq_work_entry);
  492. if (dev->_is_first_frame) {
  493. dev->_is_first_frame = 0;
  494. if (dev->_isNTSC) {
  495. singlefield_lines += 1;
  496. odd_risc_prog_size = ODD_FLD_NTSC_PROG_SIZE;
  497. } else {
  498. singlefield_lines = PAL_FIELD_HEIGHT;
  499. odd_risc_prog_size = ODD_FLD_PAL_PROG_SIZE;
  500. }
  501. if (dev->_dma_virt_start_addr != NULL) {
  502. line_size_in_bytes =
  503. (dev->_pixel_format ==
  504. PIXEL_FRMT_411) ? Y411_LINE_SZ :
  505. Y422_LINE_SZ;
  506. risc_phys_jump_addr =
  507. dev->_dma_phys_start_addr +
  508. odd_risc_prog_size;
  509. rp = cx25821_update_riscprogram(dev,
  510. dev->_dma_virt_start_addr, TOP_OFFSET,
  511. line_size_in_bytes, 0x0,
  512. singlefield_lines, FIFO_DISABLE,
  513. ODD_FIELD);
  514. /* Jump to Even Risc program of 1st Frame */
  515. *(rp++) = cpu_to_le32(RISC_JUMP);
  516. *(rp++) = cpu_to_le32(risc_phys_jump_addr);
  517. *(rp++) = cpu_to_le32(0);
  518. }
  519. }
  520. spin_unlock(&dev->slock);
  521. } else {
  522. if (status & FLD_VID_SRC_UF)
  523. pr_err("%s(): Video Received Underflow Error Interrupt!\n",
  524. __func__);
  525. if (status & FLD_VID_SRC_SYNC)
  526. pr_err("%s(): Video Received Sync Error Interrupt!\n",
  527. __func__);
  528. if (status & FLD_VID_SRC_OPC_ERR)
  529. pr_err("%s(): Video Received OpCode Error Interrupt!\n",
  530. __func__);
  531. }
  532. if (dev->_file_status == END_OF_FILE) {
  533. pr_err("EOF Channel 1 Framecount = %d\n", dev->_frame_count);
  534. return -1;
  535. }
  536. /* ElSE, set the interrupt mask register, re-enable irq. */
  537. int_msk_tmp = cx_read(channel->int_msk);
  538. cx_write(channel->int_msk, int_msk_tmp |= _intr_msk);
  539. return 0;
  540. }
  541. static irqreturn_t cx25821_upstream_irq(int irq, void *dev_id)
  542. {
  543. struct cx25821_dev *dev = dev_id;
  544. u32 msk_stat, vid_status;
  545. int handled = 0;
  546. int channel_num = 0;
  547. struct sram_channel *sram_ch;
  548. if (!dev)
  549. return -1;
  550. channel_num = VID_UPSTREAM_SRAM_CHANNEL_I;
  551. sram_ch = dev->channels[channel_num].sram_channels;
  552. msk_stat = cx_read(sram_ch->int_mstat);
  553. vid_status = cx_read(sram_ch->int_stat);
  554. /* Only deal with our interrupt */
  555. if (vid_status) {
  556. handled =
  557. cx25821_video_upstream_irq(dev, channel_num, vid_status);
  558. }
  559. if (handled < 0)
  560. cx25821_stop_upstream_video_ch1(dev);
  561. else
  562. handled += handled;
  563. return IRQ_RETVAL(handled);
  564. }
  565. void cx25821_set_pixelengine(struct cx25821_dev *dev, struct sram_channel *ch,
  566. int pix_format)
  567. {
  568. int width = WIDTH_D1;
  569. int height = dev->_lines_count;
  570. int num_lines, odd_num_lines;
  571. u32 value;
  572. int vip_mode = OUTPUT_FRMT_656;
  573. value = ((pix_format & 0x3) << 12) | (vip_mode & 0x7);
  574. value &= 0xFFFFFFEF;
  575. value |= dev->_isNTSC ? 0 : 0x10;
  576. cx_write(ch->vid_fmt_ctl, value);
  577. /* set number of active pixels in each line.
  578. * Default is 720 pixels in both NTSC and PAL format */
  579. cx_write(ch->vid_active_ctl1, width);
  580. num_lines = (height / 2) & 0x3FF;
  581. odd_num_lines = num_lines;
  582. if (dev->_isNTSC)
  583. odd_num_lines += 1;
  584. value = (num_lines << 16) | odd_num_lines;
  585. /* set number of active lines in field 0 (top) and field 1 (bottom) */
  586. cx_write(ch->vid_active_ctl2, value);
  587. cx_write(ch->vid_cdt_size, VID_CDT_SIZE >> 3);
  588. }
  589. int cx25821_start_video_dma_upstream(struct cx25821_dev *dev,
  590. struct sram_channel *sram_ch)
  591. {
  592. u32 tmp = 0;
  593. int err = 0;
  594. /* 656/VIP SRC Upstream Channel I & J and 7 - Host Bus Interface for
  595. * channel A-C
  596. */
  597. tmp = cx_read(VID_CH_MODE_SEL);
  598. cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);
  599. /* Set the physical start address of the RISC program in the initial
  600. * program counter(IPC) member of the cmds.
  601. */
  602. cx_write(sram_ch->cmds_start + 0, dev->_dma_phys_addr);
  603. /* Risc IPC High 64 bits 63-32 */
  604. cx_write(sram_ch->cmds_start + 4, 0);
  605. /* reset counter */
  606. cx_write(sram_ch->gpcnt_ctl, 3);
  607. /* Clear our bits from the interrupt status register. */
  608. cx_write(sram_ch->int_stat, _intr_msk);
  609. /* Set the interrupt mask register, enable irq. */
  610. cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << sram_ch->irq_bit));
  611. tmp = cx_read(sram_ch->int_msk);
  612. cx_write(sram_ch->int_msk, tmp |= _intr_msk);
  613. err =
  614. request_irq(dev->pci->irq, cx25821_upstream_irq,
  615. IRQF_SHARED, dev->name, dev);
  616. if (err < 0) {
  617. pr_err("%s: can't get upstream IRQ %d\n",
  618. dev->name, dev->pci->irq);
  619. goto fail_irq;
  620. }
  621. /* Start the DMA engine */
  622. tmp = cx_read(sram_ch->dma_ctl);
  623. cx_set(sram_ch->dma_ctl, tmp | FLD_VID_RISC_EN);
  624. dev->_is_running = 1;
  625. dev->_is_first_frame = 1;
  626. return 0;
  627. fail_irq:
  628. cx25821_dev_unregister(dev);
  629. return err;
  630. }
  631. int cx25821_vidupstream_init_ch1(struct cx25821_dev *dev, int channel_select,
  632. int pixel_format)
  633. {
  634. struct sram_channel *sram_ch;
  635. u32 tmp;
  636. int retval = 0;
  637. int err = 0;
  638. int data_frame_size = 0;
  639. int risc_buffer_size = 0;
  640. int str_length = 0;
  641. if (dev->_is_running) {
  642. pr_info("Video Channel is still running so return!\n");
  643. return 0;
  644. }
  645. dev->_channel_upstream_select = channel_select;
  646. sram_ch = dev->channels[channel_select].sram_channels;
  647. INIT_WORK(&dev->_irq_work_entry, cx25821_vidups_handler);
  648. dev->_irq_queues = create_singlethread_workqueue("cx25821_workqueue");
  649. if (!dev->_irq_queues) {
  650. pr_err("create_singlethread_workqueue() for Video FAILED!\n");
  651. return -ENOMEM;
  652. }
  653. /* 656/VIP SRC Upstream Channel I & J and 7 - Host Bus Interface for
  654. * channel A-C
  655. */
  656. tmp = cx_read(VID_CH_MODE_SEL);
  657. cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);
  658. dev->_is_running = 0;
  659. dev->_frame_count = 0;
  660. dev->_file_status = RESET_STATUS;
  661. dev->_lines_count = dev->_isNTSC ? 480 : 576;
  662. dev->_pixel_format = pixel_format;
  663. dev->_line_size =
  664. (dev->_pixel_format ==
  665. PIXEL_FRMT_422) ? (WIDTH_D1 * 2) : (WIDTH_D1 * 3) / 2;
  666. data_frame_size = dev->_isNTSC ? NTSC_DATA_BUF_SZ : PAL_DATA_BUF_SZ;
  667. risc_buffer_size =
  668. dev->_isNTSC ? NTSC_RISC_BUF_SIZE : PAL_RISC_BUF_SIZE;
  669. if (dev->input_filename) {
  670. str_length = strlen(dev->input_filename);
  671. dev->_filename = kmalloc(str_length + 1, GFP_KERNEL);
  672. if (!dev->_filename)
  673. goto error;
  674. memcpy(dev->_filename, dev->input_filename, str_length + 1);
  675. } else {
  676. str_length = strlen(dev->_defaultname);
  677. dev->_filename = kmalloc(str_length + 1, GFP_KERNEL);
  678. if (!dev->_filename)
  679. goto error;
  680. memcpy(dev->_filename, dev->_defaultname, str_length + 1);
  681. }
  682. /* Default if filename is empty string */
  683. if (strcmp(dev->input_filename, "") == 0) {
  684. if (dev->_isNTSC) {
  685. dev->_filename =
  686. (dev->_pixel_format ==
  687. PIXEL_FRMT_411) ? "/root/vid411.yuv" :
  688. "/root/vidtest.yuv";
  689. } else {
  690. dev->_filename =
  691. (dev->_pixel_format ==
  692. PIXEL_FRMT_411) ? "/root/pal411.yuv" :
  693. "/root/pal422.yuv";
  694. }
  695. }
  696. dev->_is_running = 0;
  697. dev->_frame_count = 0;
  698. dev->_file_status = RESET_STATUS;
  699. dev->_lines_count = dev->_isNTSC ? 480 : 576;
  700. dev->_pixel_format = pixel_format;
  701. dev->_line_size =
  702. (dev->_pixel_format ==
  703. PIXEL_FRMT_422) ? (WIDTH_D1 * 2) : (WIDTH_D1 * 3) / 2;
  704. retval =
  705. cx25821_sram_channel_setup_upstream(dev, sram_ch, dev->_line_size,
  706. 0);
  707. /* setup fifo + format */
  708. cx25821_set_pixelengine(dev, sram_ch, dev->_pixel_format);
  709. dev->upstream_riscbuf_size = risc_buffer_size * 2;
  710. dev->upstream_databuf_size = data_frame_size * 2;
  711. /* Allocating buffers and prepare RISC program */
  712. retval = cx25821_upstream_buffer_prepare(dev, sram_ch, dev->_line_size);
  713. if (retval < 0) {
  714. pr_err("%s: Failed to set up Video upstream buffers!\n",
  715. dev->name);
  716. goto error;
  717. }
  718. cx25821_start_video_dma_upstream(dev, sram_ch);
  719. return 0;
  720. error:
  721. cx25821_dev_unregister(dev);
  722. return err;
  723. }