cx25821-audio-upstream.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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-audio-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/delay.h>
  34. #include <linux/slab.h>
  35. #include <linux/uaccess.h>
  36. MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
  37. MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
  38. MODULE_LICENSE("GPL");
  39. static int _intr_msk = FLD_AUD_SRC_RISCI1 | FLD_AUD_SRC_OF |
  40. FLD_AUD_SRC_SYNC | FLD_AUD_SRC_OPC_ERR;
  41. static int cx25821_sram_channel_setup_upstream_audio(struct cx25821_dev *dev,
  42. const struct sram_channel *ch,
  43. unsigned int bpl, u32 risc)
  44. {
  45. unsigned int i, lines;
  46. u32 cdt;
  47. if (ch->cmds_start == 0) {
  48. cx_write(ch->ptr1_reg, 0);
  49. cx_write(ch->ptr2_reg, 0);
  50. cx_write(ch->cnt2_reg, 0);
  51. cx_write(ch->cnt1_reg, 0);
  52. return 0;
  53. }
  54. bpl = (bpl + 7) & ~7; /* alignment */
  55. cdt = ch->cdt;
  56. lines = ch->fifo_size / bpl;
  57. if (lines > 3)
  58. lines = 3;
  59. BUG_ON(lines < 2);
  60. /* write CDT */
  61. for (i = 0; i < lines; i++) {
  62. cx_write(cdt + 16 * i, ch->fifo_start + bpl * i);
  63. cx_write(cdt + 16 * i + 4, 0);
  64. cx_write(cdt + 16 * i + 8, 0);
  65. cx_write(cdt + 16 * i + 12, 0);
  66. }
  67. /* write CMDS */
  68. cx_write(ch->cmds_start + 0, risc);
  69. cx_write(ch->cmds_start + 4, 0);
  70. cx_write(ch->cmds_start + 8, cdt);
  71. cx_write(ch->cmds_start + 12, AUDIO_CDT_SIZE_QW);
  72. cx_write(ch->cmds_start + 16, ch->ctrl_start);
  73. /* IQ size */
  74. cx_write(ch->cmds_start + 20, AUDIO_IQ_SIZE_DW);
  75. for (i = 24; i < 80; i += 4)
  76. cx_write(ch->cmds_start + i, 0);
  77. /* fill registers */
  78. cx_write(ch->ptr1_reg, ch->fifo_start);
  79. cx_write(ch->ptr2_reg, cdt);
  80. cx_write(ch->cnt2_reg, AUDIO_CDT_SIZE_QW);
  81. cx_write(ch->cnt1_reg, AUDIO_CLUSTER_SIZE_QW - 1);
  82. return 0;
  83. }
  84. static __le32 *cx25821_risc_field_upstream_audio(struct cx25821_dev *dev,
  85. __le32 *rp,
  86. dma_addr_t databuf_phys_addr,
  87. unsigned int bpl,
  88. int fifo_enable)
  89. {
  90. unsigned int line;
  91. const struct sram_channel *sram_ch =
  92. dev->channels[dev->_audio_upstream_channel].sram_channels;
  93. int offset = 0;
  94. /* scan lines */
  95. for (line = 0; line < LINES_PER_AUDIO_BUFFER; line++) {
  96. *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
  97. *(rp++) = cpu_to_le32(databuf_phys_addr + offset);
  98. *(rp++) = cpu_to_le32(0); /* bits 63-32 */
  99. /* Check if we need to enable the FIFO
  100. * after the first 3 lines.
  101. * For the upstream audio channel,
  102. * the risc engine will enable the FIFO */
  103. if (fifo_enable && line == 2) {
  104. *(rp++) = RISC_WRITECR;
  105. *(rp++) = sram_ch->dma_ctl;
  106. *(rp++) = sram_ch->fld_aud_fifo_en;
  107. *(rp++) = 0x00000020;
  108. }
  109. offset += AUDIO_LINE_SIZE;
  110. }
  111. return rp;
  112. }
  113. static int cx25821_risc_buffer_upstream_audio(struct cx25821_dev *dev,
  114. struct pci_dev *pci,
  115. unsigned int bpl, unsigned int lines)
  116. {
  117. __le32 *rp;
  118. int fifo_enable = 0;
  119. int frame = 0, i = 0;
  120. int frame_size = AUDIO_DATA_BUF_SZ;
  121. int databuf_offset = 0;
  122. int risc_flag = RISC_CNT_INC;
  123. dma_addr_t risc_phys_jump_addr;
  124. /* Virtual address of Risc buffer program */
  125. rp = dev->_risc_virt_addr;
  126. /* sync instruction */
  127. *(rp++) = cpu_to_le32(RISC_RESYNC | AUDIO_SYNC_LINE);
  128. for (frame = 0; frame < NUM_AUDIO_FRAMES; frame++) {
  129. databuf_offset = frame_size * frame;
  130. if (frame == 0) {
  131. fifo_enable = 1;
  132. risc_flag = RISC_CNT_RESET;
  133. } else {
  134. fifo_enable = 0;
  135. risc_flag = RISC_CNT_INC;
  136. }
  137. /* Calculate physical jump address */
  138. if ((frame + 1) == NUM_AUDIO_FRAMES) {
  139. risc_phys_jump_addr =
  140. dev->_risc_phys_start_addr +
  141. RISC_SYNC_INSTRUCTION_SIZE;
  142. } else {
  143. risc_phys_jump_addr =
  144. dev->_risc_phys_start_addr +
  145. RISC_SYNC_INSTRUCTION_SIZE +
  146. AUDIO_RISC_DMA_BUF_SIZE * (frame + 1);
  147. }
  148. rp = cx25821_risc_field_upstream_audio(dev, rp,
  149. dev->_audiodata_buf_phys_addr + databuf_offset,
  150. bpl, fifo_enable);
  151. if (USE_RISC_NOOP_AUDIO) {
  152. for (i = 0; i < NUM_NO_OPS; i++)
  153. *(rp++) = cpu_to_le32(RISC_NOOP);
  154. }
  155. /* Loop to (Nth)FrameRISC or to Start of Risc program &
  156. * generate IRQ */
  157. *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | risc_flag);
  158. *(rp++) = cpu_to_le32(risc_phys_jump_addr);
  159. *(rp++) = cpu_to_le32(0);
  160. /* Recalculate virtual address based on frame index */
  161. rp = dev->_risc_virt_addr + RISC_SYNC_INSTRUCTION_SIZE / 4 +
  162. (AUDIO_RISC_DMA_BUF_SIZE * (frame + 1) / 4);
  163. }
  164. return 0;
  165. }
  166. static void cx25821_free_memory_audio(struct cx25821_dev *dev)
  167. {
  168. if (dev->_risc_virt_addr) {
  169. pci_free_consistent(dev->pci, dev->_audiorisc_size,
  170. dev->_risc_virt_addr, dev->_risc_phys_addr);
  171. dev->_risc_virt_addr = NULL;
  172. }
  173. if (dev->_audiodata_buf_virt_addr) {
  174. pci_free_consistent(dev->pci, dev->_audiodata_buf_size,
  175. dev->_audiodata_buf_virt_addr,
  176. dev->_audiodata_buf_phys_addr);
  177. dev->_audiodata_buf_virt_addr = NULL;
  178. }
  179. }
  180. void cx25821_stop_upstream_audio(struct cx25821_dev *dev)
  181. {
  182. const struct sram_channel *sram_ch =
  183. dev->channels[AUDIO_UPSTREAM_SRAM_CHANNEL_B].sram_channels;
  184. u32 tmp = 0;
  185. if (!dev->_audio_is_running) {
  186. printk(KERN_DEBUG
  187. pr_fmt("No audio file is currently running so return!\n"));
  188. return;
  189. }
  190. /* Disable RISC interrupts */
  191. cx_write(sram_ch->int_msk, 0);
  192. /* Turn OFF risc and fifo enable in AUD_DMA_CNTRL */
  193. tmp = cx_read(sram_ch->dma_ctl);
  194. cx_write(sram_ch->dma_ctl,
  195. tmp & ~(sram_ch->fld_aud_fifo_en | sram_ch->fld_aud_risc_en));
  196. /* Clear data buffer memory */
  197. if (dev->_audiodata_buf_virt_addr)
  198. memset(dev->_audiodata_buf_virt_addr, 0,
  199. dev->_audiodata_buf_size);
  200. dev->_audio_is_running = 0;
  201. dev->_is_first_audio_frame = 0;
  202. dev->_audioframe_count = 0;
  203. dev->_audiofile_status = END_OF_FILE;
  204. flush_work(&dev->_audio_work_entry);
  205. kfree(dev->_audiofilename);
  206. }
  207. void cx25821_free_mem_upstream_audio(struct cx25821_dev *dev)
  208. {
  209. if (dev->_audio_is_running)
  210. cx25821_stop_upstream_audio(dev);
  211. cx25821_free_memory_audio(dev);
  212. }
  213. static int cx25821_get_audio_data(struct cx25821_dev *dev,
  214. const struct sram_channel *sram_ch)
  215. {
  216. struct file *file;
  217. int frame_index_temp = dev->_audioframe_index;
  218. int i = 0;
  219. int frame_size = AUDIO_DATA_BUF_SZ;
  220. int frame_offset = frame_size * frame_index_temp;
  221. char mybuf[AUDIO_LINE_SIZE];
  222. loff_t file_offset = dev->_audioframe_count * frame_size;
  223. char *p = NULL;
  224. if (dev->_audiofile_status == END_OF_FILE)
  225. return 0;
  226. file = filp_open(dev->_audiofilename, O_RDONLY | O_LARGEFILE, 0);
  227. if (IS_ERR(file)) {
  228. pr_err("%s(): ERROR opening file(%s) with errno = %ld!\n",
  229. __func__, dev->_audiofilename, -PTR_ERR(file));
  230. return PTR_ERR(file);
  231. }
  232. if (dev->_audiodata_buf_virt_addr)
  233. p = (char *)dev->_audiodata_buf_virt_addr + frame_offset;
  234. for (i = 0; i < dev->_audio_lines_count; i++) {
  235. int n = kernel_read(file, file_offset, mybuf, AUDIO_LINE_SIZE);
  236. if (n < AUDIO_LINE_SIZE) {
  237. pr_info("Done: exit %s() since no more bytes to read from Audio file\n",
  238. __func__);
  239. dev->_audiofile_status = END_OF_FILE;
  240. fput(file);
  241. return 0;
  242. }
  243. dev->_audiofile_status = IN_PROGRESS;
  244. if (p) {
  245. memcpy(p, mybuf, n);
  246. p += n;
  247. }
  248. file_offset += n;
  249. }
  250. dev->_audioframe_count++;
  251. fput(file);
  252. return 0;
  253. }
  254. static void cx25821_audioups_handler(struct work_struct *work)
  255. {
  256. struct cx25821_dev *dev = container_of(work, struct cx25821_dev,
  257. _audio_work_entry);
  258. if (!dev) {
  259. pr_err("ERROR %s(): since container_of(work_struct) FAILED!\n",
  260. __func__);
  261. return;
  262. }
  263. cx25821_get_audio_data(dev, dev->channels[dev->_audio_upstream_channel].
  264. sram_channels);
  265. }
  266. static int cx25821_openfile_audio(struct cx25821_dev *dev,
  267. const struct sram_channel *sram_ch)
  268. {
  269. char *p = (void *)dev->_audiodata_buf_virt_addr;
  270. struct file *file;
  271. loff_t offset;
  272. int i, j;
  273. file = filp_open(dev->_audiofilename, O_RDONLY | O_LARGEFILE, 0);
  274. if (IS_ERR(file)) {
  275. pr_err("%s(): ERROR opening file(%s) with errno = %ld!\n",
  276. __func__, dev->_audiofilename, PTR_ERR(file));
  277. return PTR_ERR(file);
  278. }
  279. for (j = 0, offset = 0; j < NUM_AUDIO_FRAMES; j++) {
  280. for (i = 0; i < dev->_audio_lines_count; i++) {
  281. char buf[AUDIO_LINE_SIZE];
  282. int n = kernel_read(file, offset, buf,
  283. AUDIO_LINE_SIZE);
  284. if (n < AUDIO_LINE_SIZE) {
  285. pr_info("Done: exit %s() since no more bytes to read from Audio file\n",
  286. __func__);
  287. dev->_audiofile_status = END_OF_FILE;
  288. fput(file);
  289. return 0;
  290. }
  291. if (p)
  292. memcpy(p + offset, buf, n);
  293. offset += n;
  294. }
  295. dev->_audioframe_count++;
  296. }
  297. dev->_audiofile_status = IN_PROGRESS;
  298. fput(file);
  299. return 0;
  300. }
  301. static int cx25821_audio_upstream_buffer_prepare(struct cx25821_dev *dev,
  302. const struct sram_channel *sram_ch,
  303. int bpl)
  304. {
  305. int ret = 0;
  306. dma_addr_t dma_addr;
  307. dma_addr_t data_dma_addr;
  308. cx25821_free_memory_audio(dev);
  309. dev->_risc_virt_addr = pci_alloc_consistent(dev->pci,
  310. dev->audio_upstream_riscbuf_size, &dma_addr);
  311. dev->_risc_virt_start_addr = dev->_risc_virt_addr;
  312. dev->_risc_phys_start_addr = dma_addr;
  313. dev->_risc_phys_addr = dma_addr;
  314. dev->_audiorisc_size = dev->audio_upstream_riscbuf_size;
  315. if (!dev->_risc_virt_addr) {
  316. printk(KERN_DEBUG
  317. pr_fmt("ERROR: pci_alloc_consistent() FAILED to allocate memory for RISC program! Returning\n"));
  318. return -ENOMEM;
  319. }
  320. /* Clear out memory at address */
  321. memset(dev->_risc_virt_addr, 0, dev->_audiorisc_size);
  322. /* For Audio Data buffer allocation */
  323. dev->_audiodata_buf_virt_addr = pci_alloc_consistent(dev->pci,
  324. dev->audio_upstream_databuf_size, &data_dma_addr);
  325. dev->_audiodata_buf_phys_addr = data_dma_addr;
  326. dev->_audiodata_buf_size = dev->audio_upstream_databuf_size;
  327. if (!dev->_audiodata_buf_virt_addr) {
  328. printk(KERN_DEBUG
  329. pr_fmt("ERROR: pci_alloc_consistent() FAILED to allocate memory for data buffer! Returning\n"));
  330. return -ENOMEM;
  331. }
  332. /* Clear out memory at address */
  333. memset(dev->_audiodata_buf_virt_addr, 0, dev->_audiodata_buf_size);
  334. ret = cx25821_openfile_audio(dev, sram_ch);
  335. if (ret < 0)
  336. return ret;
  337. /* Creating RISC programs */
  338. ret = cx25821_risc_buffer_upstream_audio(dev, dev->pci, bpl,
  339. dev->_audio_lines_count);
  340. if (ret < 0) {
  341. printk(KERN_DEBUG
  342. pr_fmt("ERROR creating audio upstream RISC programs!\n"));
  343. goto error;
  344. }
  345. return 0;
  346. error:
  347. return ret;
  348. }
  349. static int cx25821_audio_upstream_irq(struct cx25821_dev *dev, int chan_num,
  350. u32 status)
  351. {
  352. int i = 0;
  353. u32 int_msk_tmp;
  354. const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
  355. dma_addr_t risc_phys_jump_addr;
  356. __le32 *rp;
  357. if (status & FLD_AUD_SRC_RISCI1) {
  358. /* Get interrupt_index of the program that interrupted */
  359. u32 prog_cnt = cx_read(channel->gpcnt);
  360. /* Since we've identified our IRQ, clear our bits from the
  361. * interrupt mask and interrupt status registers */
  362. cx_write(channel->int_msk, 0);
  363. cx_write(channel->int_stat, cx_read(channel->int_stat));
  364. spin_lock(&dev->slock);
  365. while (prog_cnt != dev->_last_index_irq) {
  366. /* Update _last_index_irq */
  367. if (dev->_last_index_irq < (NUMBER_OF_PROGRAMS - 1))
  368. dev->_last_index_irq++;
  369. else
  370. dev->_last_index_irq = 0;
  371. dev->_audioframe_index = dev->_last_index_irq;
  372. schedule_work(&dev->_audio_work_entry);
  373. }
  374. if (dev->_is_first_audio_frame) {
  375. dev->_is_first_audio_frame = 0;
  376. if (dev->_risc_virt_start_addr != NULL) {
  377. risc_phys_jump_addr =
  378. dev->_risc_phys_start_addr +
  379. RISC_SYNC_INSTRUCTION_SIZE +
  380. AUDIO_RISC_DMA_BUF_SIZE;
  381. rp = cx25821_risc_field_upstream_audio(dev,
  382. dev->_risc_virt_start_addr + 1,
  383. dev->_audiodata_buf_phys_addr,
  384. AUDIO_LINE_SIZE, FIFO_DISABLE);
  385. if (USE_RISC_NOOP_AUDIO) {
  386. for (i = 0; i < NUM_NO_OPS; i++) {
  387. *(rp++) =
  388. cpu_to_le32(RISC_NOOP);
  389. }
  390. }
  391. /* Jump to 2nd Audio Frame */
  392. *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 |
  393. RISC_CNT_RESET);
  394. *(rp++) = cpu_to_le32(risc_phys_jump_addr);
  395. *(rp++) = cpu_to_le32(0);
  396. }
  397. }
  398. spin_unlock(&dev->slock);
  399. } else {
  400. if (status & FLD_AUD_SRC_OF)
  401. pr_warn("%s(): Audio Received Overflow Error Interrupt!\n",
  402. __func__);
  403. if (status & FLD_AUD_SRC_SYNC)
  404. pr_warn("%s(): Audio Received Sync Error Interrupt!\n",
  405. __func__);
  406. if (status & FLD_AUD_SRC_OPC_ERR)
  407. pr_warn("%s(): Audio Received OpCode Error Interrupt!\n",
  408. __func__);
  409. /* Read and write back the interrupt status register to clear
  410. * our bits */
  411. cx_write(channel->int_stat, cx_read(channel->int_stat));
  412. }
  413. if (dev->_audiofile_status == END_OF_FILE) {
  414. pr_warn("EOF Channel Audio Framecount = %d\n",
  415. dev->_audioframe_count);
  416. return -1;
  417. }
  418. /* ElSE, set the interrupt mask register, re-enable irq. */
  419. int_msk_tmp = cx_read(channel->int_msk);
  420. cx_write(channel->int_msk, int_msk_tmp |= _intr_msk);
  421. return 0;
  422. }
  423. static irqreturn_t cx25821_upstream_irq_audio(int irq, void *dev_id)
  424. {
  425. struct cx25821_dev *dev = dev_id;
  426. u32 audio_status;
  427. int handled = 0;
  428. const struct sram_channel *sram_ch;
  429. if (!dev)
  430. return -1;
  431. sram_ch = dev->channels[dev->_audio_upstream_channel].sram_channels;
  432. audio_status = cx_read(sram_ch->int_stat);
  433. /* Only deal with our interrupt */
  434. if (audio_status) {
  435. handled = cx25821_audio_upstream_irq(dev,
  436. dev->_audio_upstream_channel, audio_status);
  437. }
  438. if (handled < 0)
  439. cx25821_stop_upstream_audio(dev);
  440. else
  441. handled += handled;
  442. return IRQ_RETVAL(handled);
  443. }
  444. static void cx25821_wait_fifo_enable(struct cx25821_dev *dev,
  445. const struct sram_channel *sram_ch)
  446. {
  447. int count = 0;
  448. u32 tmp;
  449. do {
  450. /* Wait 10 microsecond before checking to see if the FIFO is
  451. * turned ON. */
  452. udelay(10);
  453. tmp = cx_read(sram_ch->dma_ctl);
  454. /* 10 millisecond timeout */
  455. if (count++ > 1000) {
  456. pr_err("ERROR: %s() fifo is NOT turned on. Timeout!\n",
  457. __func__);
  458. return;
  459. }
  460. } while (!(tmp & sram_ch->fld_aud_fifo_en));
  461. }
  462. static int cx25821_start_audio_dma_upstream(struct cx25821_dev *dev,
  463. const struct sram_channel *sram_ch)
  464. {
  465. u32 tmp = 0;
  466. int err = 0;
  467. /* Set the physical start address of the RISC program in the initial
  468. * program counter(IPC) member of the CMDS. */
  469. cx_write(sram_ch->cmds_start + 0, dev->_risc_phys_addr);
  470. /* Risc IPC High 64 bits 63-32 */
  471. cx_write(sram_ch->cmds_start + 4, 0);
  472. /* reset counter */
  473. cx_write(sram_ch->gpcnt_ctl, 3);
  474. /* Set the line length (It looks like we do not need to set the
  475. * line length) */
  476. cx_write(sram_ch->aud_length, AUDIO_LINE_SIZE & FLD_AUD_DST_LN_LNGTH);
  477. /* Set the input mode to 16-bit */
  478. tmp = cx_read(sram_ch->aud_cfg);
  479. tmp |= FLD_AUD_SRC_ENABLE | FLD_AUD_DST_PK_MODE | FLD_AUD_CLK_ENABLE |
  480. FLD_AUD_MASTER_MODE | FLD_AUD_CLK_SELECT_PLL_D |
  481. FLD_AUD_SONY_MODE;
  482. cx_write(sram_ch->aud_cfg, tmp);
  483. /* Read and write back the interrupt status register to clear it */
  484. tmp = cx_read(sram_ch->int_stat);
  485. cx_write(sram_ch->int_stat, tmp);
  486. /* Clear our bits from the interrupt status register. */
  487. cx_write(sram_ch->int_stat, _intr_msk);
  488. /* Set the interrupt mask register, enable irq. */
  489. cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << sram_ch->irq_bit));
  490. tmp = cx_read(sram_ch->int_msk);
  491. cx_write(sram_ch->int_msk, tmp |= _intr_msk);
  492. err = request_irq(dev->pci->irq, cx25821_upstream_irq_audio,
  493. IRQF_SHARED, dev->name, dev);
  494. if (err < 0) {
  495. pr_err("%s: can't get upstream IRQ %d\n", dev->name,
  496. dev->pci->irq);
  497. goto fail_irq;
  498. }
  499. /* Start the DMA engine */
  500. tmp = cx_read(sram_ch->dma_ctl);
  501. cx_set(sram_ch->dma_ctl, tmp | sram_ch->fld_aud_risc_en);
  502. dev->_audio_is_running = 1;
  503. dev->_is_first_audio_frame = 1;
  504. /* The fifo_en bit turns on by the first Risc program */
  505. cx25821_wait_fifo_enable(dev, sram_ch);
  506. return 0;
  507. fail_irq:
  508. cx25821_dev_unregister(dev);
  509. return err;
  510. }
  511. int cx25821_audio_upstream_init(struct cx25821_dev *dev, int channel_select)
  512. {
  513. const struct sram_channel *sram_ch;
  514. int err = 0;
  515. if (dev->_audio_is_running) {
  516. pr_warn("Audio Channel is still running so return!\n");
  517. return 0;
  518. }
  519. dev->_audio_upstream_channel = channel_select;
  520. sram_ch = dev->channels[channel_select].sram_channels;
  521. /* Work queue */
  522. INIT_WORK(&dev->_audio_work_entry, cx25821_audioups_handler);
  523. dev->_last_index_irq = 0;
  524. dev->_audio_is_running = 0;
  525. dev->_audioframe_count = 0;
  526. dev->_audiofile_status = RESET_STATUS;
  527. dev->_audio_lines_count = LINES_PER_AUDIO_BUFFER;
  528. _line_size = AUDIO_LINE_SIZE;
  529. if ((dev->input_audiofilename) &&
  530. (strcmp(dev->input_audiofilename, "") != 0))
  531. dev->_audiofilename = kstrdup(dev->input_audiofilename,
  532. GFP_KERNEL);
  533. else
  534. dev->_audiofilename = kstrdup(_defaultAudioName,
  535. GFP_KERNEL);
  536. if (!dev->_audiofilename) {
  537. err = -ENOMEM;
  538. goto error;
  539. }
  540. cx25821_sram_channel_setup_upstream_audio(dev, sram_ch,
  541. _line_size, 0);
  542. dev->audio_upstream_riscbuf_size =
  543. AUDIO_RISC_DMA_BUF_SIZE * NUM_AUDIO_PROGS +
  544. RISC_SYNC_INSTRUCTION_SIZE;
  545. dev->audio_upstream_databuf_size = AUDIO_DATA_BUF_SZ * NUM_AUDIO_PROGS;
  546. /* Allocating buffers and prepare RISC program */
  547. err = cx25821_audio_upstream_buffer_prepare(dev, sram_ch,
  548. _line_size);
  549. if (err < 0) {
  550. pr_err("%s: Failed to set up Audio upstream buffers!\n",
  551. dev->name);
  552. goto error;
  553. }
  554. /* Start RISC engine */
  555. cx25821_start_audio_dma_upstream(dev, sram_ch);
  556. return 0;
  557. error:
  558. cx25821_dev_unregister(dev);
  559. return err;
  560. }