cx25821-audio-upstream.c 19 KB

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