blob: 68dbc2dbc9825c5ee3f3de4202fd6e35631e4afc [file] [log] [blame]
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -03001/*
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
Joe Perches36d89f72010-11-07 17:48:21 -030023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030025#include "cx25821-video.h"
26#include "cx25821-audio-upstream.h"
27
28#include <linux/fs.h>
29#include <linux/errno.h>
30#include <linux/kernel.h>
31#include <linux/init.h>
32#include <linux/module.h>
33#include <linux/syscalls.h>
34#include <linux/file.h>
35#include <linux/fcntl.h>
36#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -030038#include <linux/uaccess.h>
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030039
40MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
41MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
42MODULE_LICENSE("GPL");
43
Linus Torvaldsf063a0c2010-10-28 12:13:00 -070044static int _intr_msk = FLD_AUD_SRC_RISCI1 | FLD_AUD_SRC_OF |
45 FLD_AUD_SRC_SYNC | FLD_AUD_SRC_OPC_ERR;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030046
Mauro Carvalho Chehabdafc4562012-10-27 12:42:59 -030047static int cx25821_sram_channel_setup_upstream_audio(struct cx25821_dev *dev,
Hans Verkuilbfef0d32013-04-13 06:28:54 -030048 const struct sram_channel *ch,
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030049 unsigned int bpl, u32 risc)
50{
51 unsigned int i, lines;
52 u32 cdt;
53
54 if (ch->cmds_start == 0) {
55 cx_write(ch->ptr1_reg, 0);
56 cx_write(ch->ptr2_reg, 0);
57 cx_write(ch->cnt2_reg, 0);
58 cx_write(ch->cnt1_reg, 0);
59 return 0;
60 }
61
62 bpl = (bpl + 7) & ~7; /* alignment */
63 cdt = ch->cdt;
64 lines = ch->fifo_size / bpl;
65
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -030066 if (lines > 3)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030067 lines = 3;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030068
69 BUG_ON(lines < 2);
70
71 /* write CDT */
72 for (i = 0; i < lines; i++) {
73 cx_write(cdt + 16 * i, ch->fifo_start + bpl * i);
74 cx_write(cdt + 16 * i + 4, 0);
75 cx_write(cdt + 16 * i + 8, 0);
76 cx_write(cdt + 16 * i + 12, 0);
77 }
78
79 /* write CMDS */
80 cx_write(ch->cmds_start + 0, risc);
81
82 cx_write(ch->cmds_start + 4, 0);
83 cx_write(ch->cmds_start + 8, cdt);
84 cx_write(ch->cmds_start + 12, AUDIO_CDT_SIZE_QW);
85 cx_write(ch->cmds_start + 16, ch->ctrl_start);
86
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -030087 /* IQ size */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030088 cx_write(ch->cmds_start + 20, AUDIO_IQ_SIZE_DW);
89
90 for (i = 24; i < 80; i += 4)
91 cx_write(ch->cmds_start + i, 0);
92
93 /* fill registers */
94 cx_write(ch->ptr1_reg, ch->fifo_start);
95 cx_write(ch->ptr2_reg, cdt);
96 cx_write(ch->cnt2_reg, AUDIO_CDT_SIZE_QW);
97 cx_write(ch->cnt1_reg, AUDIO_CLUSTER_SIZE_QW - 1);
98
99 return 0;
100}
101
102static __le32 *cx25821_risc_field_upstream_audio(struct cx25821_dev *dev,
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300103 __le32 *rp,
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300104 dma_addr_t databuf_phys_addr,
105 unsigned int bpl,
106 int fifo_enable)
107{
108 unsigned int line;
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300109 const struct sram_channel *sram_ch =
Leonid V. Fedorenchik81f4c8d2011-10-22 01:43:25 -0300110 dev->channels[dev->_audio_upstream_channel].sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300111 int offset = 0;
112
113 /* scan lines */
114 for (line = 0; line < LINES_PER_AUDIO_BUFFER; line++) {
115 *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
116 *(rp++) = cpu_to_le32(databuf_phys_addr + offset);
117 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
118
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300119 /* Check if we need to enable the FIFO
120 * after the first 3 lines.
121 * For the upstream audio channel,
122 * the risc engine will enable the FIFO */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300123 if (fifo_enable && line == 2) {
124 *(rp++) = RISC_WRITECR;
125 *(rp++) = sram_ch->dma_ctl;
126 *(rp++) = sram_ch->fld_aud_fifo_en;
127 *(rp++) = 0x00000020;
128 }
129
130 offset += AUDIO_LINE_SIZE;
131 }
132
133 return rp;
134}
135
Mauro Carvalho Chehabdafc4562012-10-27 12:42:59 -0300136static int cx25821_risc_buffer_upstream_audio(struct cx25821_dev *dev,
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300137 struct pci_dev *pci,
138 unsigned int bpl, unsigned int lines)
139{
140 __le32 *rp;
141 int fifo_enable = 0;
142 int frame = 0, i = 0;
143 int frame_size = AUDIO_DATA_BUF_SZ;
144 int databuf_offset = 0;
145 int risc_flag = RISC_CNT_INC;
146 dma_addr_t risc_phys_jump_addr;
147
148 /* Virtual address of Risc buffer program */
149 rp = dev->_risc_virt_addr;
150
151 /* sync instruction */
152 *(rp++) = cpu_to_le32(RISC_RESYNC | AUDIO_SYNC_LINE);
153
154 for (frame = 0; frame < NUM_AUDIO_FRAMES; frame++) {
155 databuf_offset = frame_size * frame;
156
157 if (frame == 0) {
158 fifo_enable = 1;
159 risc_flag = RISC_CNT_RESET;
160 } else {
161 fifo_enable = 0;
162 risc_flag = RISC_CNT_INC;
163 }
164
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300165 /* Calculate physical jump address */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300166 if ((frame + 1) == NUM_AUDIO_FRAMES) {
167 risc_phys_jump_addr =
168 dev->_risc_phys_start_addr +
169 RISC_SYNC_INSTRUCTION_SIZE;
170 } else {
171 risc_phys_jump_addr =
172 dev->_risc_phys_start_addr +
173 RISC_SYNC_INSTRUCTION_SIZE +
174 AUDIO_RISC_DMA_BUF_SIZE * (frame + 1);
175 }
176
177 rp = cx25821_risc_field_upstream_audio(dev, rp,
Leonid V. Fedorenchik72a52fc2011-10-22 01:43:27 -0300178 dev->_audiodata_buf_phys_addr + databuf_offset,
179 bpl, fifo_enable);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300180
181 if (USE_RISC_NOOP_AUDIO) {
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300182 for (i = 0; i < NUM_NO_OPS; i++)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300183 *(rp++) = cpu_to_le32(RISC_NOOP);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300184 }
185
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300186 /* Loop to (Nth)FrameRISC or to Start of Risc program &
187 * generate IRQ */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300188 *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | risc_flag);
189 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
190 *(rp++) = cpu_to_le32(0);
191
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300192 /* Recalculate virtual address based on frame index */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300193 rp = dev->_risc_virt_addr + RISC_SYNC_INSTRUCTION_SIZE / 4 +
Leonid V. Fedorenchik81f4c8d2011-10-22 01:43:25 -0300194 (AUDIO_RISC_DMA_BUF_SIZE * (frame + 1) / 4);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300195 }
196
197 return 0;
198}
199
Mauro Carvalho Chehabdafc4562012-10-27 12:42:59 -0300200static void cx25821_free_memory_audio(struct cx25821_dev *dev)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300201{
202 if (dev->_risc_virt_addr) {
203 pci_free_consistent(dev->pci, dev->_audiorisc_size,
204 dev->_risc_virt_addr, dev->_risc_phys_addr);
205 dev->_risc_virt_addr = NULL;
206 }
207
208 if (dev->_audiodata_buf_virt_addr) {
209 pci_free_consistent(dev->pci, dev->_audiodata_buf_size,
210 dev->_audiodata_buf_virt_addr,
211 dev->_audiodata_buf_phys_addr);
212 dev->_audiodata_buf_virt_addr = NULL;
213 }
214}
215
216void cx25821_stop_upstream_audio(struct cx25821_dev *dev)
217{
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300218 const struct sram_channel *sram_ch =
Leonid V. Fedorenchik81f4c8d2011-10-22 01:43:25 -0300219 dev->channels[AUDIO_UPSTREAM_SRAM_CHANNEL_B].sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300220 u32 tmp = 0;
221
222 if (!dev->_audio_is_running) {
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300223 printk(KERN_DEBUG
Joe Perches36d89f72010-11-07 17:48:21 -0300224 pr_fmt("No audio file is currently running so return!\n"));
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300225 return;
226 }
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300227 /* Disable RISC interrupts */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300228 cx_write(sram_ch->int_msk, 0);
229
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300230 /* Turn OFF risc and fifo enable in AUD_DMA_CNTRL */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300231 tmp = cx_read(sram_ch->dma_ctl);
232 cx_write(sram_ch->dma_ctl,
233 tmp & ~(sram_ch->fld_aud_fifo_en | sram_ch->fld_aud_risc_en));
234
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300235 /* Clear data buffer memory */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300236 if (dev->_audiodata_buf_virt_addr)
237 memset(dev->_audiodata_buf_virt_addr, 0,
238 dev->_audiodata_buf_size);
239
240 dev->_audio_is_running = 0;
241 dev->_is_first_audio_frame = 0;
242 dev->_audioframe_count = 0;
243 dev->_audiofile_status = END_OF_FILE;
244
Ilia Mirkinb0091782011-03-13 00:28:58 -0500245 kfree(dev->_irq_audio_queues);
246 dev->_irq_audio_queues = NULL;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300247
Ilia Mirkinb0091782011-03-13 00:28:58 -0500248 kfree(dev->_audiofilename);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300249}
250
251void cx25821_free_mem_upstream_audio(struct cx25821_dev *dev)
252{
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300253 if (dev->_audio_is_running)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300254 cx25821_stop_upstream_audio(dev);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300255
256 cx25821_free_memory_audio(dev);
257}
258
Mauro Carvalho Chehabdafc4562012-10-27 12:42:59 -0300259static int cx25821_get_audio_data(struct cx25821_dev *dev,
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300260 const struct sram_channel *sram_ch)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300261{
Al Viro2ce8fce2013-04-13 16:35:35 -0400262 struct file *file;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300263 int frame_index_temp = dev->_audioframe_index;
264 int i = 0;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300265 int frame_size = AUDIO_DATA_BUF_SZ;
266 int frame_offset = frame_size * frame_index_temp;
Al Viro2ce8fce2013-04-13 16:35:35 -0400267 char mybuf[AUDIO_LINE_SIZE];
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300268 loff_t file_offset = dev->_audioframe_count * frame_size;
Al Viro2ce8fce2013-04-13 16:35:35 -0400269 char *p = NULL;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300270
271 if (dev->_audiofile_status == END_OF_FILE)
272 return 0;
273
Al Viro2ce8fce2013-04-13 16:35:35 -0400274 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);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300279 }
280
Al Viro2ce8fce2013-04-13 16:35:35 -0400281 if (dev->_audiodata_buf_virt_addr)
282 p = (char *)dev->_audiodata_buf_virt_addr + frame_offset;
283
284 for (i = 0; i < dev->_audio_lines_count; i++) {
285 int n = kernel_read(file, file_offset, mybuf, AUDIO_LINE_SIZE);
286 if (n < AUDIO_LINE_SIZE) {
287 pr_info("Done: exit %s() since no more bytes to read from Audio file\n",
288 __func__);
289 dev->_audiofile_status = END_OF_FILE;
290 fput(file);
291 return 0;
292 }
293 dev->_audiofile_status = IN_PROGRESS;
294 if (p) {
295 memcpy(p, mybuf, n);
296 p += n;
297 }
298 file_offset += n;
299 }
300 dev->_audioframe_count++;
301 fput(file);
302
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300303 return 0;
304}
305
306static void cx25821_audioups_handler(struct work_struct *work)
307{
Leonid V. Fedorenchik72a52fc2011-10-22 01:43:27 -0300308 struct cx25821_dev *dev = container_of(work, struct cx25821_dev,
309 _audio_work_entry);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300310
311 if (!dev) {
Joe Perches36d89f72010-11-07 17:48:21 -0300312 pr_err("ERROR %s(): since container_of(work_struct) FAILED!\n",
Leonid V. Fedorenchik81f4c8d2011-10-22 01:43:25 -0300313 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300314 return;
315 }
316
Leonid V. Fedorenchik1531e882011-09-02 11:55:38 +0800317 cx25821_get_audio_data(dev, dev->channels[dev->_audio_upstream_channel].
318 sram_channels);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300319}
320
Mauro Carvalho Chehabdafc4562012-10-27 12:42:59 -0300321static int cx25821_openfile_audio(struct cx25821_dev *dev,
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300322 const struct sram_channel *sram_ch)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300323{
Al Viro3af07612013-04-13 16:48:24 -0400324 char *p = (void *)dev->_audiodata_buf_virt_addr;
325 struct file *file;
326 loff_t offset;
327 int i, j;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300328
Al Viro3af07612013-04-13 16:48:24 -0400329 file = filp_open(dev->_audiofilename, O_RDONLY | O_LARGEFILE, 0);
330 if (IS_ERR(file)) {
331 pr_err("%s(): ERROR opening file(%s) with errno = %ld!\n",
332 __func__, dev->_audiofilename, PTR_ERR(file));
333 return PTR_ERR(file);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300334 }
335
Al Viro3af07612013-04-13 16:48:24 -0400336 for (j = 0, offset = 0; j < NUM_AUDIO_FRAMES; j++) {
337 for (i = 0; i < dev->_audio_lines_count; i++) {
338 char buf[AUDIO_LINE_SIZE];
339 int n = kernel_read(file, offset, buf,
340 AUDIO_LINE_SIZE);
341
342 if (n < AUDIO_LINE_SIZE) {
343 pr_info("Done: exit %s() since no more bytes to read from Audio file\n",
344 __func__);
345 dev->_audiofile_status = END_OF_FILE;
346 fput(file);
347 return 0;
348 }
349
350 if (p)
351 memcpy(p + offset, buf, n);
352
353 offset += n;
354 }
355 dev->_audioframe_count++;
356 }
357 dev->_audiofile_status = IN_PROGRESS;
358 fput(file);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300359 return 0;
360}
361
362static int cx25821_audio_upstream_buffer_prepare(struct cx25821_dev *dev,
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300363 const struct sram_channel *sram_ch,
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300364 int bpl)
365{
366 int ret = 0;
367 dma_addr_t dma_addr;
368 dma_addr_t data_dma_addr;
369
370 cx25821_free_memory_audio(dev);
371
Leonid V. Fedorenchik72a52fc2011-10-22 01:43:27 -0300372 dev->_risc_virt_addr = pci_alloc_consistent(dev->pci,
373 dev->audio_upstream_riscbuf_size, &dma_addr);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300374 dev->_risc_virt_start_addr = dev->_risc_virt_addr;
375 dev->_risc_phys_start_addr = dma_addr;
376 dev->_risc_phys_addr = dma_addr;
377 dev->_audiorisc_size = dev->audio_upstream_riscbuf_size;
378
379 if (!dev->_risc_virt_addr) {
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300380 printk(KERN_DEBUG
Leonid V. Fedorenchik81f4c8d2011-10-22 01:43:25 -0300381 pr_fmt("ERROR: pci_alloc_consistent() FAILED to allocate memory for RISC program! Returning\n"));
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300382 return -ENOMEM;
383 }
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300384 /* Clear out memory at address */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300385 memset(dev->_risc_virt_addr, 0, dev->_audiorisc_size);
386
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300387 /* For Audio Data buffer allocation */
Leonid V. Fedorenchik72a52fc2011-10-22 01:43:27 -0300388 dev->_audiodata_buf_virt_addr = pci_alloc_consistent(dev->pci,
389 dev->audio_upstream_databuf_size, &data_dma_addr);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300390 dev->_audiodata_buf_phys_addr = data_dma_addr;
391 dev->_audiodata_buf_size = dev->audio_upstream_databuf_size;
392
393 if (!dev->_audiodata_buf_virt_addr) {
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300394 printk(KERN_DEBUG
Leonid V. Fedorenchik81f4c8d2011-10-22 01:43:25 -0300395 pr_fmt("ERROR: pci_alloc_consistent() FAILED to allocate memory for data buffer! Returning\n"));
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300396 return -ENOMEM;
397 }
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300398 /* Clear out memory at address */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300399 memset(dev->_audiodata_buf_virt_addr, 0, dev->_audiodata_buf_size);
400
401 ret = cx25821_openfile_audio(dev, sram_ch);
402 if (ret < 0)
403 return ret;
404
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300405 /* Creating RISC programs */
Leonid V. Fedorenchik72a52fc2011-10-22 01:43:27 -0300406 ret = cx25821_risc_buffer_upstream_audio(dev, dev->pci, bpl,
407 dev->_audio_lines_count);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300408 if (ret < 0) {
409 printk(KERN_DEBUG
Leonid V. Fedorenchik81f4c8d2011-10-22 01:43:25 -0300410 pr_fmt("ERROR creating audio upstream RISC programs!\n"));
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300411 goto error;
412 }
413
414 return 0;
415
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300416error:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300417 return ret;
418}
419
Mauro Carvalho Chehabdafc4562012-10-27 12:42:59 -0300420static int cx25821_audio_upstream_irq(struct cx25821_dev *dev, int chan_num,
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300421 u32 status)
422{
423 int i = 0;
424 u32 int_msk_tmp;
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300425 const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300426 dma_addr_t risc_phys_jump_addr;
427 __le32 *rp;
428
429 if (status & FLD_AUD_SRC_RISCI1) {
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300430 /* Get interrupt_index of the program that interrupted */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300431 u32 prog_cnt = cx_read(channel->gpcnt);
432
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300433 /* Since we've identified our IRQ, clear our bits from the
434 * interrupt mask and interrupt status registers */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300435 cx_write(channel->int_msk, 0);
436 cx_write(channel->int_stat, cx_read(channel->int_stat));
437
438 spin_lock(&dev->slock);
439
440 while (prog_cnt != dev->_last_index_irq) {
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300441 /* Update _last_index_irq */
442 if (dev->_last_index_irq < (NUMBER_OF_PROGRAMS - 1))
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300443 dev->_last_index_irq++;
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300444 else
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300445 dev->_last_index_irq = 0;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300446
447 dev->_audioframe_index = dev->_last_index_irq;
448
449 queue_work(dev->_irq_audio_queues,
450 &dev->_audio_work_entry);
451 }
452
453 if (dev->_is_first_audio_frame) {
454 dev->_is_first_audio_frame = 0;
455
456 if (dev->_risc_virt_start_addr != NULL) {
457 risc_phys_jump_addr =
Leonid V. Fedorenchik81f4c8d2011-10-22 01:43:25 -0300458 dev->_risc_phys_start_addr +
459 RISC_SYNC_INSTRUCTION_SIZE +
460 AUDIO_RISC_DMA_BUF_SIZE;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300461
462 rp = cx25821_risc_field_upstream_audio(dev,
Leonid V. Fedorenchik54082f12011-09-02 11:55:39 +0800463 dev->_risc_virt_start_addr + 1,
464 dev->_audiodata_buf_phys_addr,
465 AUDIO_LINE_SIZE, FIFO_DISABLE);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300466
467 if (USE_RISC_NOOP_AUDIO) {
468 for (i = 0; i < NUM_NO_OPS; i++) {
469 *(rp++) =
470 cpu_to_le32(RISC_NOOP);
471 }
472 }
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300473 /* Jump to 2nd Audio Frame */
Leonid V. Fedorenchik1531e882011-09-02 11:55:38 +0800474 *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 |
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300475 RISC_CNT_RESET);
476 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
477 *(rp++) = cpu_to_le32(0);
478 }
479 }
480
481 spin_unlock(&dev->slock);
482 } else {
483 if (status & FLD_AUD_SRC_OF)
Joe Perches36d89f72010-11-07 17:48:21 -0300484 pr_warn("%s(): Audio Received Overflow Error Interrupt!\n",
485 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300486
487 if (status & FLD_AUD_SRC_SYNC)
Joe Perches36d89f72010-11-07 17:48:21 -0300488 pr_warn("%s(): Audio Received Sync Error Interrupt!\n",
489 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300490
491 if (status & FLD_AUD_SRC_OPC_ERR)
Joe Perches36d89f72010-11-07 17:48:21 -0300492 pr_warn("%s(): Audio Received OpCode Error Interrupt!\n",
493 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300494
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300495 /* Read and write back the interrupt status register to clear
496 * our bits */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300497 cx_write(channel->int_stat, cx_read(channel->int_stat));
498 }
499
500 if (dev->_audiofile_status == END_OF_FILE) {
Joe Perches36d89f72010-11-07 17:48:21 -0300501 pr_warn("EOF Channel Audio Framecount = %d\n",
502 dev->_audioframe_count);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300503 return -1;
504 }
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300505 /* ElSE, set the interrupt mask register, re-enable irq. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300506 int_msk_tmp = cx_read(channel->int_msk);
507 cx_write(channel->int_msk, int_msk_tmp |= _intr_msk);
508
509 return 0;
510}
511
512static irqreturn_t cx25821_upstream_irq_audio(int irq, void *dev_id)
513{
514 struct cx25821_dev *dev = dev_id;
Hans Verkuil30fdf032012-04-20 06:26:19 -0300515 u32 audio_status;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300516 int handled = 0;
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300517 const struct sram_channel *sram_ch;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300518
519 if (!dev)
520 return -1;
521
Leonid V. Fedorenchik6100c572011-09-02 11:55:30 +0800522 sram_ch = dev->channels[dev->_audio_upstream_channel].sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300523
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300524 audio_status = cx_read(sram_ch->int_stat);
525
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300526 /* Only deal with our interrupt */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300527 if (audio_status) {
Leonid V. Fedorenchik1531e882011-09-02 11:55:38 +0800528 handled = cx25821_audio_upstream_irq(dev,
529 dev->_audio_upstream_channel, audio_status);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300530 }
531
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300532 if (handled < 0)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300533 cx25821_stop_upstream_audio(dev);
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300534 else
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300535 handled += handled;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300536
537 return IRQ_RETVAL(handled);
538}
539
540static void cx25821_wait_fifo_enable(struct cx25821_dev *dev,
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300541 const struct sram_channel *sram_ch)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300542{
543 int count = 0;
544 u32 tmp;
545
546 do {
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300547 /* Wait 10 microsecond before checking to see if the FIFO is
548 * turned ON. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300549 udelay(10);
550
551 tmp = cx_read(sram_ch->dma_ctl);
552
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300553 /* 10 millisecond timeout */
554 if (count++ > 1000) {
Joe Perches36d89f72010-11-07 17:48:21 -0300555 pr_err("ERROR: %s() fifo is NOT turned on. Timeout!\n",
Leonid V. Fedorenchik81f4c8d2011-10-22 01:43:25 -0300556 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300557 return;
558 }
559
560 } while (!(tmp & sram_ch->fld_aud_fifo_en));
561
562}
563
Mauro Carvalho Chehabdafc4562012-10-27 12:42:59 -0300564static int cx25821_start_audio_dma_upstream(struct cx25821_dev *dev,
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300565 const struct sram_channel *sram_ch)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300566{
567 u32 tmp = 0;
568 int err = 0;
569
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300570 /* Set the physical start address of the RISC program in the initial
571 * program counter(IPC) member of the CMDS. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300572 cx_write(sram_ch->cmds_start + 0, dev->_risc_phys_addr);
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300573 /* Risc IPC High 64 bits 63-32 */
574 cx_write(sram_ch->cmds_start + 4, 0);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300575
576 /* reset counter */
577 cx_write(sram_ch->gpcnt_ctl, 3);
578
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300579 /* Set the line length (It looks like we do not need to set the
580 * line length) */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300581 cx_write(sram_ch->aud_length, AUDIO_LINE_SIZE & FLD_AUD_DST_LN_LNGTH);
582
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300583 /* Set the input mode to 16-bit */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300584 tmp = cx_read(sram_ch->aud_cfg);
Leonid V. Fedorenchik72a52fc2011-10-22 01:43:27 -0300585 tmp |= FLD_AUD_SRC_ENABLE | FLD_AUD_DST_PK_MODE | FLD_AUD_CLK_ENABLE |
586 FLD_AUD_MASTER_MODE | FLD_AUD_CLK_SELECT_PLL_D |
587 FLD_AUD_SONY_MODE;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300588 cx_write(sram_ch->aud_cfg, tmp);
589
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300590 /* Read and write back the interrupt status register to clear it */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300591 tmp = cx_read(sram_ch->int_stat);
592 cx_write(sram_ch->int_stat, tmp);
593
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300594 /* Clear our bits from the interrupt status register. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300595 cx_write(sram_ch->int_stat, _intr_msk);
596
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300597 /* Set the interrupt mask register, enable irq. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300598 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << sram_ch->irq_bit));
599 tmp = cx_read(sram_ch->int_msk);
600 cx_write(sram_ch->int_msk, tmp |= _intr_msk);
601
Leonid V. Fedorenchik72a52fc2011-10-22 01:43:27 -0300602 err = request_irq(dev->pci->irq, cx25821_upstream_irq_audio,
Yong Zhang18e93512011-09-07 16:10:22 +0800603 IRQF_SHARED, dev->name, dev);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300604 if (err < 0) {
Leonid V. Fedorenchik72a52fc2011-10-22 01:43:27 -0300605 pr_err("%s: can't get upstream IRQ %d\n", dev->name,
606 dev->pci->irq);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300607 goto fail_irq;
608 }
609
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300610 /* Start the DMA engine */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300611 tmp = cx_read(sram_ch->dma_ctl);
612 cx_set(sram_ch->dma_ctl, tmp | sram_ch->fld_aud_risc_en);
613
614 dev->_audio_is_running = 1;
615 dev->_is_first_audio_frame = 1;
616
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300617 /* The fifo_en bit turns on by the first Risc program */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300618 cx25821_wait_fifo_enable(dev, sram_ch);
619
620 return 0;
621
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300622fail_irq:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300623 cx25821_dev_unregister(dev);
624 return err;
625}
626
627int cx25821_audio_upstream_init(struct cx25821_dev *dev, int channel_select)
628{
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300629 const struct sram_channel *sram_ch;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300630 int err = 0;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300631
632 if (dev->_audio_is_running) {
Joe Perches36d89f72010-11-07 17:48:21 -0300633 pr_warn("Audio Channel is still running so return!\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300634 return 0;
635 }
636
Leonid V. Fedorenchik6100c572011-09-02 11:55:30 +0800637 dev->_audio_upstream_channel = channel_select;
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300638 sram_ch = dev->channels[channel_select].sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300639
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300640 /* Work queue */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300641 INIT_WORK(&dev->_audio_work_entry, cx25821_audioups_handler);
642 dev->_irq_audio_queues =
643 create_singlethread_workqueue("cx25821_audioworkqueue");
644
645 if (!dev->_irq_audio_queues) {
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300646 printk(KERN_DEBUG
Leonid V. Fedorenchik81f4c8d2011-10-22 01:43:25 -0300647 pr_fmt("ERROR: create_singlethread_workqueue() for Audio FAILED!\n"));
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300648 return -ENOMEM;
649 }
650
651 dev->_last_index_irq = 0;
652 dev->_audio_is_running = 0;
653 dev->_audioframe_count = 0;
654 dev->_audiofile_status = RESET_STATUS;
655 dev->_audio_lines_count = LINES_PER_AUDIO_BUFFER;
656 _line_size = AUDIO_LINE_SIZE;
657
Peter Senna Tschudincab3e1f2012-09-17 04:04:58 -0300658 if ((dev->input_audiofilename) &&
659 (strcmp(dev->input_audiofilename, "") != 0))
Peter Senna Tschudin6c17c242012-09-17 04:04:57 -0300660 dev->_audiofilename = kstrdup(dev->input_audiofilename,
661 GFP_KERNEL);
Peter Senna Tschudincab3e1f2012-09-17 04:04:58 -0300662 else
Peter Senna Tschudin6c17c242012-09-17 04:04:57 -0300663 dev->_audiofilename = kstrdup(_defaultAudioName,
664 GFP_KERNEL);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300665
Peter Senna Tschudincab3e1f2012-09-17 04:04:58 -0300666 if (!dev->_audiofilename) {
667 err = -ENOMEM;
668 goto error;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300669 }
670
Peter Senna Tschudinc81d4972012-09-17 04:04:56 -0300671 cx25821_sram_channel_setup_upstream_audio(dev, sram_ch,
672 _line_size, 0);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300673
674 dev->audio_upstream_riscbuf_size =
Leonid V. Fedorenchik81f4c8d2011-10-22 01:43:25 -0300675 AUDIO_RISC_DMA_BUF_SIZE * NUM_AUDIO_PROGS +
676 RISC_SYNC_INSTRUCTION_SIZE;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300677 dev->audio_upstream_databuf_size = AUDIO_DATA_BUF_SZ * NUM_AUDIO_PROGS;
678
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300679 /* Allocating buffers and prepare RISC program */
Peter Senna Tschudinc81d4972012-09-17 04:04:56 -0300680 err = cx25821_audio_upstream_buffer_prepare(dev, sram_ch,
Leonid V. Fedorenchik1531e882011-09-02 11:55:38 +0800681 _line_size);
Peter Senna Tschudinc81d4972012-09-17 04:04:56 -0300682 if (err < 0) {
Joe Perches36d89f72010-11-07 17:48:21 -0300683 pr_err("%s: Failed to set up Audio upstream buffers!\n",
Leonid V. Fedorenchik81f4c8d2011-10-22 01:43:25 -0300684 dev->name);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300685 goto error;
686 }
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300687 /* Start RISC engine */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300688 cx25821_start_audio_dma_upstream(dev, sram_ch);
689
690 return 0;
691
Mauro Carvalho Chehabb5f11cc2010-05-18 00:51:02 -0300692error:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300693 cx25821_dev_unregister(dev);
694
695 return err;
696}