cx25821-audio-upstream.c 20 KB

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