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