Commit | Line | Data |
---|---|---|
1a9fc855 MCC |
1 | /* |
2 | * Driver for the Conexant CX25821 PCIe bridge | |
3 | * | |
4 | * Copyright (C) 2009 Conexant Systems Inc. | |
5 | * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com> | |
6 | * Based on SAA713x ALSA driver and CX88 driver | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation, version 2 | |
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 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | */ | |
22 | ||
02b20b0b MCC |
23 | #include <linux/module.h> |
24 | #include <linux/init.h> | |
25 | #include <linux/device.h> | |
26 | #include <linux/interrupt.h> | |
27 | #include <linux/vmalloc.h> | |
28 | #include <linux/dma-mapping.h> | |
29 | #include <linux/pci.h> | |
5a0e3ad6 | 30 | #include <linux/slab.h> |
02b20b0b MCC |
31 | |
32 | #include <asm/delay.h> | |
33 | #include <sound/core.h> | |
34 | #include <sound/pcm.h> | |
35 | #include <sound/pcm_params.h> | |
36 | #include <sound/control.h> | |
37 | #include <sound/initval.h> | |
38 | #include <sound/tlv.h> | |
39 | ||
02b20b0b MCC |
40 | #include "cx25821.h" |
41 | #include "cx25821-reg.h" | |
42 | ||
43 | #define AUDIO_SRAM_CHANNEL SRAM_CH08 | |
44 | ||
45 | #define dprintk(level,fmt, arg...) if (debug >= level) \ | |
46 | printk(KERN_INFO "%s/1: " fmt, chip->dev->name , ## arg) | |
47 | ||
48 | #define dprintk_core(level,fmt, arg...) if (debug >= level) \ | |
49 | printk(KERN_DEBUG "%s/1: " fmt, chip->dev->name , ## arg) | |
50 | ||
02b20b0b MCC |
51 | /**************************************************************************** |
52 | Data type declarations - Can be moded to a header file later | |
53 | ****************************************************************************/ | |
54 | ||
02b20b0b MCC |
55 | static struct snd_card *snd_cx25821_cards[SNDRV_CARDS]; |
56 | static int devno; | |
57 | ||
58 | struct cx25821_audio_dev { | |
1a9fc855 MCC |
59 | struct cx25821_dev *dev; |
60 | struct cx25821_dmaqueue q; | |
02b20b0b MCC |
61 | |
62 | /* pci i/o */ | |
1a9fc855 | 63 | struct pci_dev *pci; |
02b20b0b MCC |
64 | |
65 | /* audio controls */ | |
1a9fc855 | 66 | int irq; |
02b20b0b | 67 | |
1a9fc855 | 68 | struct snd_card *card; |
02b20b0b MCC |
69 | |
70 | unsigned long iobase; | |
1a9fc855 MCC |
71 | spinlock_t reg_lock; |
72 | atomic_t count; | |
02b20b0b | 73 | |
1a9fc855 MCC |
74 | unsigned int dma_size; |
75 | unsigned int period_size; | |
76 | unsigned int num_periods; | |
02b20b0b | 77 | |
1a9fc855 | 78 | struct videobuf_dmabuf *dma_risc; |
02b20b0b | 79 | |
1a9fc855 | 80 | struct cx25821_buffer *buf; |
02b20b0b | 81 | |
1a9fc855 | 82 | struct snd_pcm_substream *substream; |
02b20b0b MCC |
83 | }; |
84 | typedef struct cx25821_audio_dev snd_cx25821_card_t; | |
85 | ||
86 | ||
02b20b0b MCC |
87 | /**************************************************************************** |
88 | Module global static vars | |
89 | ****************************************************************************/ | |
90 | ||
91 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ | |
92 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ | |
53e712d0 | 93 | static int enable[SNDRV_CARDS] = { 1,[1 ... (SNDRV_CARDS - 1)] = 1 }; |
02b20b0b MCC |
94 | |
95 | module_param_array(enable, bool, NULL, 0444); | |
96 | MODULE_PARM_DESC(enable, "Enable cx25821 soundcard. default enabled."); | |
97 | ||
98 | module_param_array(index, int, NULL, 0444); | |
99 | MODULE_PARM_DESC(index, "Index value for cx25821 capture interface(s)."); | |
100 | ||
02b20b0b MCC |
101 | /**************************************************************************** |
102 | Module macros | |
103 | ****************************************************************************/ | |
104 | ||
105 | MODULE_DESCRIPTION("ALSA driver module for cx25821 based capture cards"); | |
106 | MODULE_AUTHOR("Hiep Huynh"); | |
107 | MODULE_LICENSE("GPL"); | |
1a9fc855 | 108 | MODULE_SUPPORTED_DEVICE("{{Conexant,25821}"); //"{{Conexant,23881}," |
02b20b0b MCC |
109 | |
110 | static unsigned int debug; | |
1a9fc855 MCC |
111 | module_param(debug, int, 0644); |
112 | MODULE_PARM_DESC(debug, "enable debug messages"); | |
02b20b0b MCC |
113 | |
114 | /**************************************************************************** | |
115 | Module specific funtions | |
116 | ****************************************************************************/ | |
117 | /* Constants taken from cx88-reg.h */ | |
118 | #define AUD_INT_DN_RISCI1 (1 << 0) | |
119 | #define AUD_INT_UP_RISCI1 (1 << 1) | |
120 | #define AUD_INT_RDS_DN_RISCI1 (1 << 2) | |
1a9fc855 | 121 | #define AUD_INT_DN_RISCI2 (1 << 4) /* yes, 3 is skipped */ |
02b20b0b MCC |
122 | #define AUD_INT_UP_RISCI2 (1 << 5) |
123 | #define AUD_INT_RDS_DN_RISCI2 (1 << 6) | |
124 | #define AUD_INT_DN_SYNC (1 << 12) | |
125 | #define AUD_INT_UP_SYNC (1 << 13) | |
126 | #define AUD_INT_RDS_DN_SYNC (1 << 14) | |
127 | #define AUD_INT_OPC_ERR (1 << 16) | |
128 | #define AUD_INT_BER_IRQ (1 << 20) | |
129 | #define AUD_INT_MCHG_IRQ (1 << 21) | |
130 | #define GP_COUNT_CONTROL_RESET 0x3 | |
131 | ||
132 | #define PCI_MSK_AUD_EXT (1 << 4) | |
133 | #define PCI_MSK_AUD_INT (1 << 3) | |
134 | /* | |
135 | * BOARD Specific: Sets audio DMA | |
136 | */ | |
137 | ||
1a9fc855 | 138 | static int _cx25821_start_audio_dma(snd_cx25821_card_t * chip) |
02b20b0b | 139 | { |
1a9fc855 MCC |
140 | struct cx25821_buffer *buf = chip->buf; |
141 | struct cx25821_dev *dev = chip->dev; | |
142 | struct sram_channel *audio_ch = | |
143 | &cx25821_sram_channels[AUDIO_SRAM_CHANNEL]; | |
02b20b0b MCC |
144 | u32 tmp = 0; |
145 | ||
146 | // enable output on the GPIO 0 for the MCLK ADC (Audio) | |
1a9fc855 | 147 | cx25821_set_gpiopin_direction(chip->dev, 0, 0); |
02b20b0b MCC |
148 | |
149 | /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */ | |
1a9fc855 MCC |
150 | cx_clear(AUD_INT_DMA_CTL, |
151 | FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN); | |
02b20b0b MCC |
152 | |
153 | /* setup fifo + format - out channel */ | |
1a9fc855 MCC |
154 | cx25821_sram_channel_setup_audio(chip->dev, audio_ch, buf->bpl, |
155 | buf->risc.dma); | |
02b20b0b MCC |
156 | |
157 | /* sets bpl size */ | |
158 | cx_write(AUD_A_LNGTH, buf->bpl); | |
159 | ||
160 | /* reset counter */ | |
1a9fc855 | 161 | cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET); //GP_COUNT_CONTROL_RESET = 0x3 |
02b20b0b MCC |
162 | atomic_set(&chip->count, 0); |
163 | ||
1a9fc855 MCC |
164 | //Set the input mode to 16-bit |
165 | tmp = cx_read(AUD_A_CFG); | |
166 | cx_write(AUD_A_CFG, | |
167 | tmp | FLD_AUD_DST_PK_MODE | FLD_AUD_DST_ENABLE | | |
168 | FLD_AUD_CLK_ENABLE); | |
02b20b0b MCC |
169 | |
170 | //printk(KERN_INFO "DEBUG: Start audio DMA, %d B/line, cmds_start(0x%x)= %d lines/FIFO, %d periods, %d " | |
1a9fc855 MCC |
171 | // "byte buffer\n", buf->bpl, audio_ch->cmds_start, cx_read(audio_ch->cmds_start + 12)>>1, |
172 | // chip->num_periods, buf->bpl * chip->num_periods); | |
02b20b0b MCC |
173 | |
174 | /* Enables corresponding bits at AUD_INT_STAT */ | |
1a9fc855 MCC |
175 | cx_write(AUD_A_INT_MSK, |
176 | FLD_AUD_DST_RISCI1 | FLD_AUD_DST_OF | FLD_AUD_DST_SYNC | | |
177 | FLD_AUD_DST_OPC_ERR); | |
02b20b0b MCC |
178 | |
179 | /* Clean any pending interrupt bits already set */ | |
180 | cx_write(AUD_A_INT_STAT, ~0); | |
181 | ||
182 | /* enable audio irqs */ | |
183 | cx_set(PCI_INT_MSK, chip->dev->pci_irqmask | PCI_MSK_AUD_INT); | |
184 | ||
1a9fc855 MCC |
185 | // Turn on audio downstream fifo and risc enable 0x101 |
186 | tmp = cx_read(AUD_INT_DMA_CTL); | |
187 | cx_set(AUD_INT_DMA_CTL, | |
188 | tmp | (FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN)); | |
02b20b0b MCC |
189 | |
190 | mdelay(100); | |
191 | return 0; | |
192 | } | |
193 | ||
194 | /* | |
195 | * BOARD Specific: Resets audio DMA | |
196 | */ | |
1a9fc855 | 197 | static int _cx25821_stop_audio_dma(snd_cx25821_card_t * chip) |
02b20b0b MCC |
198 | { |
199 | struct cx25821_dev *dev = chip->dev; | |
200 | ||
201 | /* stop dma */ | |
1a9fc855 MCC |
202 | cx_clear(AUD_INT_DMA_CTL, |
203 | FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN); | |
02b20b0b MCC |
204 | |
205 | /* disable irqs */ | |
206 | cx_clear(PCI_INT_MSK, PCI_MSK_AUD_INT); | |
1a9fc855 MCC |
207 | cx_clear(AUD_A_INT_MSK, |
208 | AUD_INT_OPC_ERR | AUD_INT_DN_SYNC | AUD_INT_DN_RISCI2 | | |
209 | AUD_INT_DN_RISCI1); | |
02b20b0b MCC |
210 | |
211 | return 0; | |
212 | } | |
213 | ||
214 | #define MAX_IRQ_LOOP 50 | |
215 | ||
216 | /* | |
217 | * BOARD Specific: IRQ dma bits | |
218 | */ | |
219 | static char *cx25821_aud_irqs[32] = { | |
1a9fc855 MCC |
220 | "dn_risci1", "up_risci1", "rds_dn_risc1", /* 0-2 */ |
221 | NULL, /* reserved */ | |
222 | "dn_risci2", "up_risci2", "rds_dn_risc2", /* 4-6 */ | |
223 | NULL, /* reserved */ | |
224 | "dnf_of", "upf_uf", "rds_dnf_uf", /* 8-10 */ | |
225 | NULL, /* reserved */ | |
226 | "dn_sync", "up_sync", "rds_dn_sync", /* 12-14 */ | |
227 | NULL, /* reserved */ | |
228 | "opc_err", "par_err", "rip_err", /* 16-18 */ | |
229 | "pci_abort", "ber_irq", "mchg_irq" /* 19-21 */ | |
02b20b0b MCC |
230 | }; |
231 | ||
232 | /* | |
233 | * BOARD Specific: Threats IRQ audio specific calls | |
234 | */ | |
1a9fc855 | 235 | static void cx25821_aud_irq(snd_cx25821_card_t * chip, u32 status, u32 mask) |
02b20b0b MCC |
236 | { |
237 | struct cx25821_dev *dev = chip->dev; | |
238 | ||
1a9fc855 | 239 | if (0 == (status & mask)) { |
02b20b0b MCC |
240 | return; |
241 | } | |
242 | ||
243 | cx_write(AUD_A_INT_STAT, status); | |
1a9fc855 | 244 | if (debug > 1 || (status & mask & ~0xff)) |
02b20b0b | 245 | cx25821_print_irqbits(dev->name, "irq aud", |
1a9fc855 MCC |
246 | cx25821_aud_irqs, |
247 | ARRAY_SIZE(cx25821_aud_irqs), status, | |
248 | mask); | |
02b20b0b MCC |
249 | |
250 | /* risc op code error */ | |
251 | if (status & AUD_INT_OPC_ERR) { | |
1a9fc855 MCC |
252 | printk(KERN_WARNING "WARNING %s/1: Audio risc op code error\n", |
253 | dev->name); | |
254 | ||
255 | cx_clear(AUD_INT_DMA_CTL, | |
256 | FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN); | |
257 | cx25821_sram_channel_dump_audio(dev, | |
258 | &cx25821_sram_channels | |
259 | [AUDIO_SRAM_CHANNEL]); | |
02b20b0b MCC |
260 | } |
261 | if (status & AUD_INT_DN_SYNC) { | |
1a9fc855 MCC |
262 | printk(KERN_WARNING "WARNING %s: Downstream sync error!\n", |
263 | dev->name); | |
02b20b0b MCC |
264 | cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET); |
265 | return; | |
266 | } | |
267 | ||
02b20b0b MCC |
268 | /* risc1 downstream */ |
269 | if (status & AUD_INT_DN_RISCI1) { | |
270 | atomic_set(&chip->count, cx_read(AUD_A_GPCNT)); | |
271 | snd_pcm_period_elapsed(chip->substream); | |
272 | } | |
273 | } | |
274 | ||
02b20b0b MCC |
275 | /* |
276 | * BOARD Specific: Handles IRQ calls | |
277 | */ | |
278 | static irqreturn_t cx25821_irq(int irq, void *dev_id) | |
279 | { | |
280 | snd_cx25821_card_t *chip = dev_id; | |
281 | struct cx25821_dev *dev = chip->dev; | |
282 | u32 status, pci_status; | |
283 | u32 audint_status, audint_mask; | |
284 | int loop, handled = 0; | |
285 | int audint_count = 0; | |
286 | ||
02b20b0b | 287 | audint_status = cx_read(AUD_A_INT_STAT); |
1a9fc855 | 288 | audint_mask = cx_read(AUD_A_INT_MSK); |
02b20b0b MCC |
289 | audint_count = cx_read(AUD_A_GPCNT); |
290 | status = cx_read(PCI_INT_STAT); | |
291 | ||
1a9fc855 | 292 | for (loop = 0; loop < 1; loop++) { |
02b20b0b | 293 | status = cx_read(PCI_INT_STAT); |
1a9fc855 | 294 | if (0 == status) { |
02b20b0b MCC |
295 | status = cx_read(PCI_INT_STAT); |
296 | audint_status = cx_read(AUD_A_INT_STAT); | |
297 | audint_mask = cx_read(AUD_A_INT_MSK); | |
298 | ||
1a9fc855 | 299 | if (status) { |
02b20b0b MCC |
300 | handled = 1; |
301 | cx_write(PCI_INT_STAT, status); | |
302 | ||
1a9fc855 MCC |
303 | cx25821_aud_irq(chip, audint_status, |
304 | audint_mask); | |
02b20b0b | 305 | break; |
1a9fc855 | 306 | } else |
02b20b0b MCC |
307 | goto out; |
308 | } | |
309 | ||
310 | handled = 1; | |
311 | cx_write(PCI_INT_STAT, status); | |
312 | ||
313 | cx25821_aud_irq(chip, audint_status, audint_mask); | |
314 | } | |
315 | ||
316 | pci_status = cx_read(PCI_INT_STAT); | |
317 | ||
318 | if (handled) | |
319 | cx_write(PCI_INT_STAT, pci_status); | |
320 | ||
1a9fc855 | 321 | out: |
02b20b0b MCC |
322 | return IRQ_RETVAL(handled); |
323 | } | |
324 | ||
1a9fc855 | 325 | static int dsp_buffer_free(snd_cx25821_card_t * chip) |
02b20b0b MCC |
326 | { |
327 | BUG_ON(!chip->dma_size); | |
328 | ||
1a9fc855 | 329 | dprintk(2, "Freeing buffer\n"); |
02b20b0b MCC |
330 | videobuf_sg_dma_unmap(&chip->pci->dev, chip->dma_risc); |
331 | videobuf_dma_free(chip->dma_risc); | |
1a9fc855 | 332 | btcx_riscmem_free(chip->pci, &chip->buf->risc); |
02b20b0b MCC |
333 | kfree(chip->buf); |
334 | ||
335 | chip->dma_risc = NULL; | |
336 | chip->dma_size = 0; | |
337 | ||
338 | return 0; | |
339 | } | |
340 | ||
341 | /**************************************************************************** | |
342 | ALSA PCM Interface | |
343 | ****************************************************************************/ | |
344 | ||
345 | /* | |
346 | * Digital hardware definition | |
347 | */ | |
348 | #define DEFAULT_FIFO_SIZE 384 | |
349 | static struct snd_pcm_hardware snd_cx25821_digital_hw = { | |
350 | .info = SNDRV_PCM_INFO_MMAP | | |
1a9fc855 MCC |
351 | SNDRV_PCM_INFO_INTERLEAVED | |
352 | SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID, | |
02b20b0b MCC |
353 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
354 | ||
1a9fc855 MCC |
355 | .rates = SNDRV_PCM_RATE_48000, |
356 | .rate_min = 48000, | |
357 | .rate_max = 48000, | |
02b20b0b MCC |
358 | .channels_min = 2, |
359 | .channels_max = 2, | |
360 | /* Analog audio output will be full of clicks and pops if there | |
361 | are not exactly four lines in the SRAM FIFO buffer. */ | |
1a9fc855 MCC |
362 | .period_bytes_min = DEFAULT_FIFO_SIZE / 3, |
363 | .period_bytes_max = DEFAULT_FIFO_SIZE / 3, | |
02b20b0b MCC |
364 | .periods_min = 1, |
365 | .periods_max = AUDIO_LINE_SIZE, | |
1a9fc855 | 366 | .buffer_bytes_max = (AUDIO_LINE_SIZE * AUDIO_LINE_SIZE), //128*128 = 16384 = 1024 * 16 |
02b20b0b MCC |
367 | }; |
368 | ||
02b20b0b MCC |
369 | /* |
370 | * audio pcm capture open callback | |
371 | */ | |
372 | static int snd_cx25821_pcm_open(struct snd_pcm_substream *substream) | |
373 | { | |
374 | snd_cx25821_card_t *chip = snd_pcm_substream_chip(substream); | |
375 | struct snd_pcm_runtime *runtime = substream->runtime; | |
376 | int err; | |
377 | unsigned int bpl = 0; | |
378 | ||
379 | if (!chip) { | |
380 | printk(KERN_ERR "DEBUG: cx25821 can't find device struct." | |
1a9fc855 | 381 | " Can't proceed with open\n"); |
02b20b0b MCC |
382 | return -ENODEV; |
383 | } | |
384 | ||
1a9fc855 MCC |
385 | err = |
386 | snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS); | |
02b20b0b MCC |
387 | if (err < 0) |
388 | goto _error; | |
389 | ||
390 | chip->substream = substream; | |
391 | ||
392 | runtime->hw = snd_cx25821_digital_hw; | |
393 | ||
1a9fc855 MCC |
394 | if (cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size != |
395 | DEFAULT_FIFO_SIZE) { | |
396 | bpl = cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size / 3; //since there are 3 audio Clusters | |
397 | bpl &= ~7; /* must be multiple of 8 */ | |
02b20b0b | 398 | |
1a9fc855 | 399 | if (bpl > AUDIO_LINE_SIZE) { |
02b20b0b MCC |
400 | bpl = AUDIO_LINE_SIZE; |
401 | } | |
402 | runtime->hw.period_bytes_min = bpl; | |
403 | runtime->hw.period_bytes_max = bpl; | |
404 | } | |
405 | ||
406 | return 0; | |
1a9fc855 MCC |
407 | _error: |
408 | dprintk(1, "Error opening PCM!\n"); | |
02b20b0b MCC |
409 | return err; |
410 | } | |
411 | ||
412 | /* | |
413 | * audio close callback | |
414 | */ | |
415 | static int snd_cx25821_close(struct snd_pcm_substream *substream) | |
416 | { | |
417 | return 0; | |
418 | } | |
419 | ||
420 | /* | |
421 | * hw_params callback | |
422 | */ | |
1a9fc855 MCC |
423 | static int snd_cx25821_hw_params(struct snd_pcm_substream *substream, |
424 | struct snd_pcm_hw_params *hw_params) | |
02b20b0b MCC |
425 | { |
426 | snd_cx25821_card_t *chip = snd_pcm_substream_chip(substream); | |
427 | struct videobuf_dmabuf *dma; | |
428 | ||
429 | struct cx25821_buffer *buf; | |
430 | int ret; | |
431 | ||
432 | if (substream->runtime->dma_area) { | |
433 | dsp_buffer_free(chip); | |
434 | substream->runtime->dma_area = NULL; | |
435 | } | |
436 | ||
02b20b0b MCC |
437 | chip->period_size = params_period_bytes(hw_params); |
438 | chip->num_periods = params_periods(hw_params); | |
439 | chip->dma_size = chip->period_size * params_periods(hw_params); | |
440 | ||
441 | BUG_ON(!chip->dma_size); | |
1a9fc855 | 442 | BUG_ON(chip->num_periods & (chip->num_periods - 1)); |
02b20b0b MCC |
443 | |
444 | buf = videobuf_sg_alloc(sizeof(*buf)); | |
445 | if (NULL == buf) | |
446 | return -ENOMEM; | |
447 | ||
1a9fc855 | 448 | if (chip->period_size > AUDIO_LINE_SIZE) { |
02b20b0b MCC |
449 | chip->period_size = AUDIO_LINE_SIZE; |
450 | } | |
451 | ||
02b20b0b | 452 | buf->vb.memory = V4L2_MEMORY_MMAP; |
1a9fc855 MCC |
453 | buf->vb.field = V4L2_FIELD_NONE; |
454 | buf->vb.width = chip->period_size; | |
455 | buf->bpl = chip->period_size; | |
02b20b0b | 456 | buf->vb.height = chip->num_periods; |
1a9fc855 | 457 | buf->vb.size = chip->dma_size; |
02b20b0b MCC |
458 | |
459 | dma = videobuf_to_dma(&buf->vb); | |
460 | videobuf_dma_init(dma); | |
461 | ||
462 | ret = videobuf_dma_init_kernel(dma, PCI_DMA_FROMDEVICE, | |
1a9fc855 MCC |
463 | (PAGE_ALIGN(buf->vb.size) >> |
464 | PAGE_SHIFT)); | |
02b20b0b MCC |
465 | if (ret < 0) |
466 | goto error; | |
467 | ||
468 | ret = videobuf_sg_dma_map(&chip->pci->dev, dma); | |
469 | if (ret < 0) | |
470 | goto error; | |
471 | ||
1a9fc855 MCC |
472 | ret = |
473 | cx25821_risc_databuffer_audio(chip->pci, &buf->risc, dma->sglist, | |
474 | buf->vb.width, buf->vb.height, 1); | |
475 | if (ret < 0) { | |
476 | printk(KERN_INFO | |
477 | "DEBUG: ERROR after cx25821_risc_databuffer_audio() \n"); | |
02b20b0b MCC |
478 | goto error; |
479 | } | |
480 | ||
02b20b0b | 481 | /* Loop back to start of program */ |
1a9fc855 | 482 | buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC); |
02b20b0b | 483 | buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma); |
1a9fc855 | 484 | buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */ |
02b20b0b MCC |
485 | |
486 | buf->vb.state = VIDEOBUF_PREPARED; | |
487 | ||
488 | chip->buf = buf; | |
489 | chip->dma_risc = dma; | |
490 | ||
491 | substream->runtime->dma_area = chip->dma_risc->vmalloc; | |
492 | substream->runtime->dma_bytes = chip->dma_size; | |
493 | substream->runtime->dma_addr = 0; | |
494 | ||
495 | return 0; | |
496 | ||
1a9fc855 | 497 | error: |
02b20b0b MCC |
498 | kfree(buf); |
499 | return ret; | |
500 | } | |
501 | ||
502 | /* | |
503 | * hw free callback | |
504 | */ | |
1a9fc855 | 505 | static int snd_cx25821_hw_free(struct snd_pcm_substream *substream) |
02b20b0b MCC |
506 | { |
507 | snd_cx25821_card_t *chip = snd_pcm_substream_chip(substream); | |
508 | ||
509 | if (substream->runtime->dma_area) { | |
510 | dsp_buffer_free(chip); | |
511 | substream->runtime->dma_area = NULL; | |
512 | } | |
513 | ||
514 | return 0; | |
515 | } | |
516 | ||
517 | /* | |
518 | * prepare callback | |
519 | */ | |
520 | static int snd_cx25821_prepare(struct snd_pcm_substream *substream) | |
521 | { | |
522 | return 0; | |
523 | } | |
524 | ||
525 | /* | |
526 | * trigger callback | |
527 | */ | |
1a9fc855 MCC |
528 | static int snd_cx25821_card_trigger(struct snd_pcm_substream *substream, |
529 | int cmd) | |
02b20b0b MCC |
530 | { |
531 | snd_cx25821_card_t *chip = snd_pcm_substream_chip(substream); | |
532 | int err = 0; | |
533 | ||
534 | /* Local interrupts are already disabled by ALSA */ | |
535 | spin_lock(&chip->reg_lock); | |
536 | ||
1a9fc855 MCC |
537 | switch (cmd) { |
538 | case SNDRV_PCM_TRIGGER_START: | |
539 | err = _cx25821_start_audio_dma(chip); | |
540 | break; | |
541 | case SNDRV_PCM_TRIGGER_STOP: | |
542 | err = _cx25821_stop_audio_dma(chip); | |
543 | break; | |
544 | default: | |
545 | err = -EINVAL; | |
546 | break; | |
02b20b0b MCC |
547 | } |
548 | ||
549 | spin_unlock(&chip->reg_lock); | |
550 | ||
551 | return err; | |
552 | } | |
553 | ||
554 | /* | |
555 | * pointer callback | |
556 | */ | |
1a9fc855 MCC |
557 | static snd_pcm_uframes_t snd_cx25821_pointer(struct snd_pcm_substream |
558 | *substream) | |
02b20b0b MCC |
559 | { |
560 | snd_cx25821_card_t *chip = snd_pcm_substream_chip(substream); | |
561 | struct snd_pcm_runtime *runtime = substream->runtime; | |
562 | u16 count; | |
563 | ||
564 | count = atomic_read(&chip->count); | |
565 | ||
1a9fc855 | 566 | return runtime->period_size * (count & (runtime->periods - 1)); |
02b20b0b MCC |
567 | } |
568 | ||
569 | /* | |
570 | * page callback (needed for mmap) | |
571 | */ | |
572 | static struct page *snd_cx25821_page(struct snd_pcm_substream *substream, | |
1a9fc855 | 573 | unsigned long offset) |
02b20b0b MCC |
574 | { |
575 | void *pageptr = substream->runtime->dma_area + offset; | |
576 | ||
577 | return vmalloc_to_page(pageptr); | |
578 | } | |
579 | ||
580 | /* | |
581 | * operators | |
582 | */ | |
583 | static struct snd_pcm_ops snd_cx25821_pcm_ops = { | |
584 | .open = snd_cx25821_pcm_open, | |
585 | .close = snd_cx25821_close, | |
586 | .ioctl = snd_pcm_lib_ioctl, | |
587 | .hw_params = snd_cx25821_hw_params, | |
588 | .hw_free = snd_cx25821_hw_free, | |
589 | .prepare = snd_cx25821_prepare, | |
590 | .trigger = snd_cx25821_card_trigger, | |
591 | .pointer = snd_cx25821_pointer, | |
592 | .page = snd_cx25821_page, | |
593 | }; | |
594 | ||
02b20b0b MCC |
595 | /* |
596 | * ALSA create a PCM device: Called when initializing the board. Sets up the name and hooks up | |
597 | * the callbacks | |
598 | */ | |
1a9fc855 | 599 | static int snd_cx25821_pcm(snd_cx25821_card_t * chip, int device, char *name) |
02b20b0b MCC |
600 | { |
601 | struct snd_pcm *pcm; | |
602 | int err; | |
603 | ||
604 | err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm); | |
1a9fc855 MCC |
605 | if (err < 0) { |
606 | printk(KERN_INFO "ERROR: FAILED snd_pcm_new() in %s\n", | |
607 | __func__); | |
02b20b0b MCC |
608 | return err; |
609 | } | |
610 | pcm->private_data = chip; | |
611 | pcm->info_flags = 0; | |
612 | strcpy(pcm->name, name); | |
613 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx25821_pcm_ops); | |
614 | ||
615 | return 0; | |
616 | } | |
617 | ||
02b20b0b MCC |
618 | /**************************************************************************** |
619 | Basic Flow for Sound Devices | |
620 | ****************************************************************************/ | |
621 | ||
622 | /* | |
623 | * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio | |
624 | * Only boards with eeprom and byte 1 at eeprom=1 have it | |
625 | */ | |
626 | ||
627 | static struct pci_device_id cx25821_audio_pci_tbl[] __devinitdata = { | |
1a9fc855 MCC |
628 | {0x14f1, 0x0920, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, |
629 | {0,} | |
02b20b0b | 630 | }; |
1a9fc855 | 631 | |
02b20b0b MCC |
632 | MODULE_DEVICE_TABLE(pci, cx25821_audio_pci_tbl); |
633 | ||
1a9fc855 MCC |
634 | /* |
635 | * Not used in the function snd_cx25821_dev_free so removing | |
636 | * from the file. | |
637 | */ | |
02b20b0b MCC |
638 | /* |
639 | static int snd_cx25821_free(snd_cx25821_card_t *chip) | |
640 | { | |
641 | if (chip->irq >= 0) | |
642 | free_irq(chip->irq, chip); | |
643 | ||
644 | cx25821_dev_unregister(chip->dev); | |
645 | pci_disable_device(chip->pci); | |
646 | ||
647 | return 0; | |
648 | } | |
1a9fc855 | 649 | */ |
02b20b0b MCC |
650 | |
651 | /* | |
652 | * Component Destructor | |
653 | */ | |
1a9fc855 | 654 | static void snd_cx25821_dev_free(struct snd_card *card) |
02b20b0b MCC |
655 | { |
656 | snd_cx25821_card_t *chip = card->private_data; | |
657 | ||
658 | //snd_cx25821_free(chip); | |
659 | snd_card_free(chip->card); | |
660 | } | |
661 | ||
02b20b0b MCC |
662 | /* |
663 | * Alsa Constructor - Component probe | |
664 | */ | |
665 | static int cx25821_audio_initdev(struct cx25821_dev *dev) | |
666 | { | |
1a9fc855 MCC |
667 | struct snd_card *card; |
668 | snd_cx25821_card_t *chip; | |
669 | int err; | |
02b20b0b | 670 | |
1a9fc855 MCC |
671 | if (devno >= SNDRV_CARDS) { |
672 | printk(KERN_INFO "DEBUG ERROR: devno >= SNDRV_CARDS %s\n", | |
673 | __func__); | |
02b20b0b MCC |
674 | return (-ENODEV); |
675 | } | |
676 | ||
677 | if (!enable[devno]) { | |
678 | ++devno; | |
679 | printk(KERN_INFO "DEBUG ERROR: !enable[devno] %s\n", __func__); | |
680 | return (-ENOENT); | |
681 | } | |
682 | ||
53e712d0 MCC |
683 | err = snd_card_create(index[devno], id[devno], THIS_MODULE, |
684 | sizeof(snd_cx25821_card_t), &card); | |
685 | if (err < 0) { | |
1a9fc855 MCC |
686 | printk(KERN_INFO |
687 | "DEBUG ERROR: cannot create snd_card_new in %s\n", | |
688 | __func__); | |
53e712d0 | 689 | return err; |
02b20b0b MCC |
690 | } |
691 | ||
692 | strcpy(card->driver, "cx25821"); | |
693 | ||
694 | /* Card "creation" */ | |
695 | card->private_free = snd_cx25821_dev_free; | |
696 | chip = (snd_cx25821_card_t *) card->private_data; | |
697 | spin_lock_init(&chip->reg_lock); | |
698 | ||
699 | chip->dev = dev; | |
700 | chip->card = card; | |
701 | chip->pci = dev->pci; | |
702 | chip->iobase = pci_resource_start(dev->pci, 0); | |
703 | ||
02b20b0b MCC |
704 | chip->irq = dev->pci->irq; |
705 | ||
706 | err = request_irq(dev->pci->irq, cx25821_irq, | |
707 | IRQF_SHARED | IRQF_DISABLED, chip->dev->name, chip); | |
708 | ||
709 | if (err < 0) { | |
1a9fc855 MCC |
710 | printk(KERN_ERR "ERROR %s: can't get IRQ %d for ALSA\n", |
711 | chip->dev->name, dev->pci->irq); | |
02b20b0b MCC |
712 | goto error; |
713 | } | |
714 | ||
1a9fc855 MCC |
715 | if ((err = snd_cx25821_pcm(chip, 0, "cx25821 Digital")) < 0) { |
716 | printk(KERN_INFO | |
717 | "DEBUG ERROR: cannot create snd_cx25821_pcm %s\n", | |
718 | __func__); | |
02b20b0b MCC |
719 | goto error; |
720 | } | |
721 | ||
722 | snd_card_set_dev(card, &chip->pci->dev); | |
723 | ||
02b20b0b | 724 | strcpy(card->shortname, "cx25821"); |
1a9fc855 MCC |
725 | sprintf(card->longname, "%s at 0x%lx irq %d", chip->dev->name, |
726 | chip->iobase, chip->irq); | |
727 | strcpy(card->mixername, "CX25821"); | |
02b20b0b | 728 | |
1a9fc855 MCC |
729 | printk(KERN_INFO "%s/%i: ALSA support for cx25821 boards\n", |
730 | card->driver, devno); | |
02b20b0b MCC |
731 | |
732 | err = snd_card_register(card); | |
1a9fc855 MCC |
733 | if (err < 0) { |
734 | printk(KERN_INFO "DEBUG ERROR: cannot register sound card %s\n", | |
735 | __func__); | |
02b20b0b MCC |
736 | goto error; |
737 | } | |
738 | ||
739 | snd_cx25821_cards[devno] = card; | |
740 | ||
741 | devno++; | |
742 | return 0; | |
743 | ||
1a9fc855 | 744 | error: |
02b20b0b MCC |
745 | snd_card_free(card); |
746 | return err; | |
747 | } | |
748 | ||
02b20b0b MCC |
749 | /**************************************************************************** |
750 | LINUX MODULE INIT | |
751 | ****************************************************************************/ | |
752 | static void cx25821_audio_fini(void) | |
753 | { | |
754 | snd_card_free(snd_cx25821_cards[0]); | |
755 | } | |
756 | ||
757 | /* | |
758 | * Module initializer | |
759 | * | |
760 | * Loops through present saa7134 cards, and assigns an ALSA device | |
761 | * to each one | |
762 | * | |
763 | */ | |
764 | static int cx25821_alsa_init(void) | |
765 | { | |
766 | struct cx25821_dev *dev = NULL; | |
767 | struct list_head *list; | |
768 | ||
1a9fc855 | 769 | list_for_each(list, &cx25821_devlist) { |
02b20b0b MCC |
770 | dev = list_entry(list, struct cx25821_dev, devlist); |
771 | cx25821_audio_initdev(dev); | |
772 | } | |
773 | ||
774 | if (dev == NULL) | |
1a9fc855 MCC |
775 | printk(KERN_INFO |
776 | "cx25821 ERROR ALSA: no cx25821 cards found\n"); | |
02b20b0b MCC |
777 | |
778 | return 0; | |
779 | ||
780 | } | |
781 | ||
782 | late_initcall(cx25821_alsa_init); | |
783 | module_exit(cx25821_audio_fini); | |
784 | ||
785 | /* ----------------------------------------------------------- */ | |
786 | /* | |
787 | * Local variables: | |
788 | * c-basic-offset: 8 | |
789 | * End: | |
790 | */ |