cx25821-audio-upstream.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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. kfree(dev->_irq_audio_queues);
  205. dev->_irq_audio_queues = NULL;
  206. kfree(dev->_audiofilename);
  207. }
  208. void cx25821_free_mem_upstream_audio(struct cx25821_dev *dev)
  209. {
  210. if (dev->_audio_is_running)
  211. cx25821_stop_upstream_audio(dev);
  212. cx25821_free_memory_audio(dev);
  213. }
  214. static int cx25821_get_audio_data(struct cx25821_dev *dev,
  215. const struct sram_channel *sram_ch)
  216. {
  217. struct file *file;
  218. int frame_index_temp = dev->_audioframe_index;
  219. int i = 0;
  220. int frame_size = AUDIO_DATA_BUF_SZ;
  221. int frame_offset = frame_size * frame_index_temp;
  222. char mybuf[AUDIO_LINE_SIZE];
  223. loff_t file_offset = dev->_audioframe_count * frame_size;
  224. char *p = NULL;
  225. if (dev->_audiofile_status == END_OF_FILE)
  226. return 0;
  227. file = filp_open(dev->_audiofilename, O_RDONLY | O_LARGEFILE, 0);
  228. if (IS_ERR(file)) {
  229. pr_err("%s(): ERROR opening file(%s) with errno = %ld!\n",
  230. __func__, dev->_audiofilename, -PTR_ERR(file));
  231. return PTR_ERR(file);
  232. }
  233. if (dev->_audiodata_buf_virt_addr)
  234. p = (char *)dev->_audiodata_buf_virt_addr + frame_offset;
  235. for (i = 0; i < dev->_audio_lines_count; i++) {
  236. int n = kernel_read(file, file_offset, mybuf, AUDIO_LINE_SIZE);
  237. if (n < AUDIO_LINE_SIZE) {
  238. pr_info("Done: exit %s() since no more bytes to read from Audio file\n",
  239. __func__);
  240. dev->_audiofile_status = END_OF_FILE;
  241. fput(file);
  242. return 0;
  243. }
  244. dev->_audiofile_status = IN_PROGRESS;
  245. if (p) {
  246. memcpy(p, mybuf, n);
  247. p += n;
  248. }
  249. file_offset += n;
  250. }
  251. dev->_audioframe_count++;
  252. fput(file);
  253. return 0;
  254. }
  255. static void cx25821_audioups_handler(struct work_struct *work)
  256. {
  257. struct cx25821_dev *dev = container_of(work, struct cx25821_dev,
  258. _audio_work_entry);
  259. if (!dev) {
  260. pr_err("ERROR %s(): since container_of(work_struct) FAILED!\n",
  261. __func__);
  262. return;
  263. }
  264. cx25821_get_audio_data(dev, dev->channels[dev->_audio_upstream_channel].
  265. sram_channels);
  266. }
  267. static int cx25821_openfile_audio(struct cx25821_dev *dev,
  268. const struct sram_channel *sram_ch)
  269. {
  270. char *p = (void *)dev->_audiodata_buf_virt_addr;
  271. struct file *file;
  272. loff_t offset;
  273. int i, j;
  274. file = filp_open(dev->_audiofilename, O_RDONLY | O_LARGEFILE, 0);
  275. if (IS_ERR(file)) {
  276. pr_err("%s(): ERROR opening file(%s) with errno = %ld!\n",
  277. __func__, dev->_audiofilename, PTR_ERR(file));
  278. return PTR_ERR(file);
  279. }
  280. for (j = 0, offset = 0; j < NUM_AUDIO_FRAMES; j++) {
  281. for (i = 0; i < dev->_audio_lines_count; i++) {
  282. char buf[AUDIO_LINE_SIZE];
  283. int n = kernel_read(file, offset, buf,
  284. AUDIO_LINE_SIZE);
  285. if (n < AUDIO_LINE_SIZE) {
  286. pr_info("Done: exit %s() since no more bytes to read from Audio file\n",
  287. __func__);
  288. dev->_audiofile_status = END_OF_FILE;
  289. fput(file);
  290. return 0;
  291. }
  292. if (p)
  293. memcpy(p + offset, buf, n);
  294. offset += n;
  295. }
  296. dev->_audioframe_count++;
  297. }
  298. dev->_audiofile_status = IN_PROGRESS;
  299. fput(file);
  300. return 0;
  301. }
  302. static int cx25821_audio_upstream_buffer_prepare(struct cx25821_dev *dev,
  303. const struct sram_channel *sram_ch,
  304. int bpl)
  305. {
  306. int ret = 0;
  307. dma_addr_t dma_addr;
  308. dma_addr_t data_dma_addr;
  309. cx25821_free_memory_audio(dev);
  310. dev->_risc_virt_addr = pci_alloc_consistent(dev->pci,
  311. dev->audio_upstream_riscbuf_size, &dma_addr);
  312. dev->_risc_virt_start_addr = dev->_risc_virt_addr;
  313. dev->_risc_phys_start_addr = dma_addr;
  314. dev->_risc_phys_addr = dma_addr;
  315. dev->_audiorisc_size = dev->audio_upstream_riscbuf_size;
  316. if (!dev->_risc_virt_addr) {
  317. printk(KERN_DEBUG
  318. pr_fmt("ERROR: pci_alloc_consistent() FAILED to allocate memory for RISC program! Returning\n"));
  319. return -ENOMEM;
  320. }
  321. /* Clear out memory at address */
  322. memset(dev->_risc_virt_addr, 0, dev->_audiorisc_size);
  323. /* For Audio Data buffer allocation */
  324. dev->_audiodata_buf_virt_addr = pci_alloc_consistent(dev->pci,
  325. dev->audio_upstream_databuf_size, &data_dma_addr);
  326. dev->_audiodata_buf_phys_addr = data_dma_addr;
  327. dev->_audiodata_buf_size = dev->audio_upstream_databuf_size;
  328. if (!dev->_audiodata_buf_virt_addr) {
  329. printk(KERN_DEBUG
  330. pr_fmt("ERROR: pci_alloc_consistent() FAILED to allocate memory for data buffer! Returning\n"));
  331. return -ENOMEM;
  332. }
  333. /* Clear out memory at address */
  334. memset(dev->_audiodata_buf_virt_addr, 0, dev->_audiodata_buf_size);
  335. ret = cx25821_openfile_audio(dev, sram_ch);
  336. if (ret < 0)
  337. return ret;
  338. /* Creating RISC programs */
  339. ret = cx25821_risc_buffer_upstream_audio(dev, dev->pci, bpl,
  340. dev->_audio_lines_count);
  341. if (ret < 0) {
  342. printk(KERN_DEBUG
  343. pr_fmt("ERROR creating audio upstream RISC programs!\n"));
  344. goto error;
  345. }
  346. return 0;
  347. error:
  348. return ret;
  349. }
  350. static int cx25821_audio_upstream_irq(struct cx25821_dev *dev, int chan_num,
  351. u32 status)
  352. {
  353. int i = 0;
  354. u32 int_msk_tmp;
  355. const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
  356. dma_addr_t risc_phys_jump_addr;
  357. __le32 *rp;
  358. if (status & FLD_AUD_SRC_RISCI1) {
  359. /* Get interrupt_index of the program that interrupted */
  360. u32 prog_cnt = cx_read(channel->gpcnt);
  361. /* Since we've identified our IRQ, clear our bits from the
  362. * interrupt mask and interrupt status registers */
  363. cx_write(channel->int_msk, 0);
  364. cx_write(channel->int_stat, cx_read(channel->int_stat));
  365. spin_lock(&dev->slock);
  366. while (prog_cnt != dev->_last_index_irq) {
  367. /* Update _last_index_irq */
  368. if (dev->_last_index_irq < (NUMBER_OF_PROGRAMS - 1))
  369. dev->_last_index_irq++;
  370. else
  371. dev->_last_index_irq = 0;
  372. dev->_audioframe_index = dev->_last_index_irq;
  373. queue_work(dev->_irq_audio_queues,
  374. &dev->_audio_work_entry);
  375. }
  376. if (dev->_is_first_audio_frame) {
  377. dev->_is_first_audio_frame = 0;
  378. if (dev->_risc_virt_start_addr != NULL) {
  379. risc_phys_jump_addr =
  380. dev->_risc_phys_start_addr +
  381. RISC_SYNC_INSTRUCTION_SIZE +
  382. AUDIO_RISC_DMA_BUF_SIZE;
  383. rp = cx25821_risc_field_upstream_audio(dev,
  384. dev->_risc_virt_start_addr + 1,
  385. dev->_audiodata_buf_phys_addr,
  386. AUDIO_LINE_SIZE, FIFO_DISABLE);
  387. if (USE_RISC_NOOP_AUDIO) {
  388. for (i = 0; i < NUM_NO_OPS; i++) {
  389. *(rp++) =
  390. cpu_to_le32(RISC_NOOP);
  391. }
  392. }
  393. /* Jump to 2nd Audio Frame */
  394. *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 |
  395. RISC_CNT_RESET);
  396. *(rp++) = cpu_to_le32(risc_phys_jump_addr);
  397. *(rp++) = cpu_to_le32(0);
  398. }
  399. }
  400. spin_unlock(&dev->slock);
  401. } else {
  402. if (status & FLD_AUD_SRC_OF)
  403. pr_warn("%s(): Audio Received Overflow Error Interrupt!\n",
  404. __func__);
  405. if (status & FLD_AUD_SRC_SYNC)
  406. pr_warn("%s(): Audio Received Sync Error Interrupt!\n",
  407. __func__);
  408. if (status & FLD_AUD_SRC_OPC_ERR)
  409. pr_warn("%s(): Audio Received OpCode Error Interrupt!\n",
  410. __func__);
  411. /* Read and write back the interrupt status register to clear
  412. * our bits */
  413. cx_write(channel->int_stat, cx_read(channel->int_stat));
  414. }
  415. if (dev->_audiofile_status == END_OF_FILE) {
  416. pr_warn("EOF Channel Audio Framecount = %d\n",
  417. dev->_audioframe_count);
  418. return -1;
  419. }
  420. /* ElSE, set the interrupt mask register, re-enable irq. */
  421. int_msk_tmp = cx_read(channel->int_msk);
  422. cx_write(channel->int_msk, int_msk_tmp |= _intr_msk);
  423. return 0;
  424. }
  425. static irqreturn_t cx25821_upstream_irq_audio(int irq, void *dev_id)
  426. {
  427. struct cx25821_dev *dev = dev_id;
  428. u32 audio_status;
  429. int handled = 0;
  430. const struct sram_channel *sram_ch;
  431. if (!dev)
  432. return -1;
  433. sram_ch = dev->channels[dev->_audio_upstream_channel].sram_channels;
  434. audio_status = cx_read(sram_ch->int_stat);
  435. /* Only deal with our interrupt */
  436. if (audio_status) {
  437. handled = cx25821_audio_upstream_irq(dev,
  438. dev->_audio_upstream_channel, audio_status);
  439. }
  440. if (handled < 0)
  441. cx25821_stop_upstream_audio(dev);
  442. else
  443. handled += handled;
  444. return IRQ_RETVAL(handled);
  445. }
  446. static void cx25821_wait_fifo_enable(struct cx25821_dev *dev,
  447. const struct sram_channel *sram_ch)
  448. {
  449. int count = 0;
  450. u32 tmp;
  451. do {
  452. /* Wait 10 microsecond before checking to see if the FIFO is
  453. * turned ON. */
  454. udelay(10);
  455. tmp = cx_read(sram_ch->dma_ctl);
  456. /* 10 millisecond timeout */
  457. if (count++ > 1000) {
  458. pr_err("ERROR: %s() fifo is NOT turned on. Timeout!\n",
  459. __func__);
  460. return;
  461. }
  462. } while (!(tmp & sram_ch->fld_aud_fifo_en));
  463. }
  464. static int cx25821_start_audio_dma_upstream(struct cx25821_dev *dev,
  465. const struct sram_channel *sram_ch)
  466. {
  467. u32 tmp = 0;
  468. int err = 0;
  469. /* Set the physical start address of the RISC program in the initial
  470. * program counter(IPC) member of the CMDS. */
  471. cx_write(sram_ch->cmds_start + 0, dev->_risc_phys_addr);
  472. /* Risc IPC High 64 bits 63-32 */
  473. cx_write(sram_ch->cmds_start + 4, 0);
  474. /* reset counter */
  475. cx_write(sram_ch->gpcnt_ctl, 3);
  476. /* Set the line length (It looks like we do not need to set the
  477. * line length) */
  478. cx_write(sram_ch->aud_length, AUDIO_LINE_SIZE & FLD_AUD_DST_LN_LNGTH);
  479. /* Set the input mode to 16-bit */
  480. tmp = cx_read(sram_ch->aud_cfg);
  481. tmp |= FLD_AUD_SRC_ENABLE | FLD_AUD_DST_PK_MODE | FLD_AUD_CLK_ENABLE |
  482. FLD_AUD_MASTER_MODE | FLD_AUD_CLK_SELECT_PLL_D |
  483. FLD_AUD_SONY_MODE;
  484. cx_write(sram_ch->aud_cfg, tmp);
  485. /* Read and write back the interrupt status register to clear it */
  486. tmp = cx_read(sram_ch->int_stat);
  487. cx_write(sram_ch->int_stat, tmp);
  488. /* Clear our bits from the interrupt status register. */
  489. cx_write(sram_ch->int_stat, _intr_msk);
  490. /* Set the interrupt mask register, enable irq. */
  491. cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << sram_ch->irq_bit));
  492. tmp = cx_read(sram_ch->int_msk);
  493. cx_write(sram_ch->int_msk, tmp |= _intr_msk);
  494. err = request_irq(dev->pci->irq, cx25821_upstream_irq_audio,
  495. IRQF_SHARED, dev->name, dev);
  496. if (err < 0) {
  497. pr_err("%s: can't get upstream IRQ %d\n", dev->name,
  498. dev->pci->irq);
  499. goto fail_irq;
  500. }
  501. /* Start the DMA engine */
  502. tmp = cx_read(sram_ch->dma_ctl);
  503. cx_set(sram_ch->dma_ctl, tmp | sram_ch->fld_aud_risc_en);
  504. dev->_audio_is_running = 1;
  505. dev->_is_first_audio_frame = 1;
  506. /* The fifo_en bit turns on by the first Risc program */
  507. cx25821_wait_fifo_enable(dev, sram_ch);
  508. return 0;
  509. fail_irq:
  510. cx25821_dev_unregister(dev);
  511. return err;
  512. }
  513. int cx25821_audio_upstream_init(struct cx25821_dev *dev, int channel_select)
  514. {
  515. const struct sram_channel *sram_ch;
  516. int err = 0;
  517. if (dev->_audio_is_running) {
  518. pr_warn("Audio Channel is still running so return!\n");
  519. return 0;
  520. }
  521. dev->_audio_upstream_channel = channel_select;
  522. sram_ch = dev->channels[channel_select].sram_channels;
  523. /* Work queue */
  524. INIT_WORK(&dev->_audio_work_entry, cx25821_audioups_handler);
  525. dev->_irq_audio_queues =
  526. create_singlethread_workqueue("cx25821_audioworkqueue");
  527. if (!dev->_irq_audio_queues) {
  528. printk(KERN_DEBUG
  529. pr_fmt("ERROR: create_singlethread_workqueue() for Audio FAILED!\n"));
  530. return -ENOMEM;
  531. }
  532. dev->_last_index_irq = 0;
  533. dev->_audio_is_running = 0;
  534. dev->_audioframe_count = 0;
  535. dev->_audiofile_status = RESET_STATUS;
  536. dev->_audio_lines_count = LINES_PER_AUDIO_BUFFER;
  537. _line_size = AUDIO_LINE_SIZE;
  538. if ((dev->input_audiofilename) &&
  539. (strcmp(dev->input_audiofilename, "") != 0))
  540. dev->_audiofilename = kstrdup(dev->input_audiofilename,
  541. GFP_KERNEL);
  542. else
  543. dev->_audiofilename = kstrdup(_defaultAudioName,
  544. GFP_KERNEL);
  545. if (!dev->_audiofilename) {
  546. err = -ENOMEM;
  547. goto error;
  548. }
  549. cx25821_sram_channel_setup_upstream_audio(dev, sram_ch,
  550. _line_size, 0);
  551. dev->audio_upstream_riscbuf_size =
  552. AUDIO_RISC_DMA_BUF_SIZE * NUM_AUDIO_PROGS +
  553. RISC_SYNC_INSTRUCTION_SIZE;
  554. dev->audio_upstream_databuf_size = AUDIO_DATA_BUF_SZ * NUM_AUDIO_PROGS;
  555. /* Allocating buffers and prepare RISC program */
  556. err = cx25821_audio_upstream_buffer_prepare(dev, sram_ch,
  557. _line_size);
  558. if (err < 0) {
  559. pr_err("%s: Failed to set up Audio upstream buffers!\n",
  560. dev->name);
  561. goto error;
  562. }
  563. /* Start RISC engine */
  564. cx25821_start_audio_dma_upstream(dev, sram_ch);
  565. return 0;
  566. error:
  567. cx25821_dev_unregister(dev);
  568. return err;
  569. }