cx25821-audio-upstream.c 18 KB

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