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 | 31 | |
8889a8a5 | 32 | #include <linux/delay.h> |
02b20b0b MCC |
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 | ||
8889a8a5 | 45 | #define dprintk(level, fmt, arg...) if (debug >= level) \ |
02b20b0b MCC |
46 | printk(KERN_INFO "%s/1: " fmt, chip->dev->name , ## arg) |
47 | ||
8889a8a5 | 48 | #define dprintk_core(level, fmt, arg...) if (debug >= level) \ |
02b20b0b MCC |
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 | 83 | }; |
02b20b0b MCC |
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 */ | |
8889a8a5 | 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"); | |
8889a8a5 | 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 | ||
8889a8a5 | 137 | static int _cx25821_start_audio_dma(struct cx25821_audio_dev *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 | ||
8889a8a5 | 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 */ | |
8889a8a5 OP |
160 | /* GP_COUNT_CONTROL_RESET = 0x3 */ |
161 | cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET); | |
02b20b0b MCC |
162 | atomic_set(&chip->count, 0); |
163 | ||
8889a8a5 | 164 | /* Set the input mode to 16-bit */ |
1a9fc855 MCC |
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 | 169 | |
8889a8a5 OP |
170 | /* printk(KERN_INFO "DEBUG: Start audio DMA, %d B/line," |
171 | "cmds_start(0x%x)= %d lines/FIFO, %d periods, " | |
172 | "%d byte buffer\n", buf->bpl, | |
173 | audio_ch->cmds_start, | |
174 | cx_read(audio_ch->cmds_start + 12)>>1, | |
175 | chip->num_periods, buf->bpl *chip->num_periods); | |
176 | */ | |
02b20b0b MCC |
177 | |
178 | /* Enables corresponding bits at AUD_INT_STAT */ | |
1a9fc855 MCC |
179 | cx_write(AUD_A_INT_MSK, |
180 | FLD_AUD_DST_RISCI1 | FLD_AUD_DST_OF | FLD_AUD_DST_SYNC | | |
181 | FLD_AUD_DST_OPC_ERR); | |
02b20b0b MCC |
182 | |
183 | /* Clean any pending interrupt bits already set */ | |
184 | cx_write(AUD_A_INT_STAT, ~0); | |
185 | ||
186 | /* enable audio irqs */ | |
187 | cx_set(PCI_INT_MSK, chip->dev->pci_irqmask | PCI_MSK_AUD_INT); | |
188 | ||
8889a8a5 | 189 | /* Turn on audio downstream fifo and risc enable 0x101 */ |
1a9fc855 MCC |
190 | tmp = cx_read(AUD_INT_DMA_CTL); |
191 | cx_set(AUD_INT_DMA_CTL, | |
192 | tmp | (FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN)); | |
02b20b0b MCC |
193 | |
194 | mdelay(100); | |
195 | return 0; | |
196 | } | |
197 | ||
198 | /* | |
199 | * BOARD Specific: Resets audio DMA | |
200 | */ | |
8889a8a5 | 201 | static int _cx25821_stop_audio_dma(struct cx25821_audio_dev *chip) |
02b20b0b MCC |
202 | { |
203 | struct cx25821_dev *dev = chip->dev; | |
204 | ||
205 | /* stop dma */ | |
1a9fc855 MCC |
206 | cx_clear(AUD_INT_DMA_CTL, |
207 | FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN); | |
02b20b0b MCC |
208 | |
209 | /* disable irqs */ | |
210 | cx_clear(PCI_INT_MSK, PCI_MSK_AUD_INT); | |
1a9fc855 MCC |
211 | cx_clear(AUD_A_INT_MSK, |
212 | AUD_INT_OPC_ERR | AUD_INT_DN_SYNC | AUD_INT_DN_RISCI2 | | |
213 | AUD_INT_DN_RISCI1); | |
02b20b0b MCC |
214 | |
215 | return 0; | |
216 | } | |
217 | ||
218 | #define MAX_IRQ_LOOP 50 | |
219 | ||
220 | /* | |
221 | * BOARD Specific: IRQ dma bits | |
222 | */ | |
223 | static char *cx25821_aud_irqs[32] = { | |
1a9fc855 MCC |
224 | "dn_risci1", "up_risci1", "rds_dn_risc1", /* 0-2 */ |
225 | NULL, /* reserved */ | |
226 | "dn_risci2", "up_risci2", "rds_dn_risc2", /* 4-6 */ | |
227 | NULL, /* reserved */ | |
228 | "dnf_of", "upf_uf", "rds_dnf_uf", /* 8-10 */ | |
229 | NULL, /* reserved */ | |
230 | "dn_sync", "up_sync", "rds_dn_sync", /* 12-14 */ | |
231 | NULL, /* reserved */ | |
232 | "opc_err", "par_err", "rip_err", /* 16-18 */ | |
233 | "pci_abort", "ber_irq", "mchg_irq" /* 19-21 */ | |
02b20b0b MCC |
234 | }; |
235 | ||
236 | /* | |
237 | * BOARD Specific: Threats IRQ audio specific calls | |
238 | */ | |
8889a8a5 OP |
239 | static void cx25821_aud_irq(struct cx25821_audio_dev *chip, u32 status, |
240 | u32 mask) | |
02b20b0b MCC |
241 | { |
242 | struct cx25821_dev *dev = chip->dev; | |
243 | ||
8889a8a5 | 244 | if (0 == (status & mask)) |
02b20b0b | 245 | return; |
02b20b0b MCC |
246 | |
247 | cx_write(AUD_A_INT_STAT, status); | |
1a9fc855 | 248 | if (debug > 1 || (status & mask & ~0xff)) |
02b20b0b | 249 | cx25821_print_irqbits(dev->name, "irq aud", |
1a9fc855 MCC |
250 | cx25821_aud_irqs, |
251 | ARRAY_SIZE(cx25821_aud_irqs), status, | |
252 | mask); | |
02b20b0b MCC |
253 | |
254 | /* risc op code error */ | |
255 | if (status & AUD_INT_OPC_ERR) { | |
1a9fc855 MCC |
256 | printk(KERN_WARNING "WARNING %s/1: Audio risc op code error\n", |
257 | dev->name); | |
258 | ||
259 | cx_clear(AUD_INT_DMA_CTL, | |
260 | FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN); | |
261 | cx25821_sram_channel_dump_audio(dev, | |
262 | &cx25821_sram_channels | |
263 | [AUDIO_SRAM_CHANNEL]); | |
02b20b0b MCC |
264 | } |
265 | if (status & AUD_INT_DN_SYNC) { | |
1a9fc855 MCC |
266 | printk(KERN_WARNING "WARNING %s: Downstream sync error!\n", |
267 | dev->name); | |
02b20b0b MCC |
268 | cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET); |
269 | return; | |
270 | } | |
271 | ||
02b20b0b MCC |
272 | /* risc1 downstream */ |
273 | if (status & AUD_INT_DN_RISCI1) { | |
274 | atomic_set(&chip->count, cx_read(AUD_A_GPCNT)); | |
275 | snd_pcm_period_elapsed(chip->substream); | |
276 | } | |
277 | } | |
278 | ||
02b20b0b MCC |
279 | /* |
280 | * BOARD Specific: Handles IRQ calls | |
281 | */ | |
282 | static irqreturn_t cx25821_irq(int irq, void *dev_id) | |
283 | { | |
8889a8a5 | 284 | struct cx25821_audio_dev *chip = dev_id; |
02b20b0b MCC |
285 | struct cx25821_dev *dev = chip->dev; |
286 | u32 status, pci_status; | |
287 | u32 audint_status, audint_mask; | |
288 | int loop, handled = 0; | |
289 | int audint_count = 0; | |
290 | ||
02b20b0b | 291 | audint_status = cx_read(AUD_A_INT_STAT); |
1a9fc855 | 292 | audint_mask = cx_read(AUD_A_INT_MSK); |
02b20b0b MCC |
293 | audint_count = cx_read(AUD_A_GPCNT); |
294 | status = cx_read(PCI_INT_STAT); | |
295 | ||
1a9fc855 | 296 | for (loop = 0; loop < 1; loop++) { |
02b20b0b | 297 | status = cx_read(PCI_INT_STAT); |
1a9fc855 | 298 | if (0 == status) { |
02b20b0b MCC |
299 | status = cx_read(PCI_INT_STAT); |
300 | audint_status = cx_read(AUD_A_INT_STAT); | |
301 | audint_mask = cx_read(AUD_A_INT_MSK); | |
302 | ||
1a9fc855 | 303 | if (status) { |
02b20b0b MCC |
304 | handled = 1; |
305 | cx_write(PCI_INT_STAT, status); | |
306 | ||
1a9fc855 MCC |
307 | cx25821_aud_irq(chip, audint_status, |
308 | audint_mask); | |
02b20b0b | 309 | break; |
1a9fc855 | 310 | } else |
02b20b0b MCC |
311 | goto out; |
312 | } | |
313 | ||
314 | handled = 1; | |
315 | cx_write(PCI_INT_STAT, status); | |
316 | ||
317 | cx25821_aud_irq(chip, audint_status, audint_mask); | |
318 | } | |
319 | ||
320 | pci_status = cx_read(PCI_INT_STAT); | |
321 | ||
322 | if (handled) | |
323 | cx_write(PCI_INT_STAT, pci_status); | |
324 | ||
8889a8a5 | 325 | out: |
02b20b0b MCC |
326 | return IRQ_RETVAL(handled); |
327 | } | |
328 | ||
8889a8a5 | 329 | static int dsp_buffer_free(struct cx25821_audio_dev *chip) |
02b20b0b MCC |
330 | { |
331 | BUG_ON(!chip->dma_size); | |
332 | ||
1a9fc855 | 333 | dprintk(2, "Freeing buffer\n"); |
02b20b0b MCC |
334 | videobuf_sg_dma_unmap(&chip->pci->dev, chip->dma_risc); |
335 | videobuf_dma_free(chip->dma_risc); | |
1a9fc855 | 336 | btcx_riscmem_free(chip->pci, &chip->buf->risc); |
02b20b0b MCC |
337 | kfree(chip->buf); |
338 | ||
339 | chip->dma_risc = NULL; | |
340 | chip->dma_size = 0; | |
341 | ||
342 | return 0; | |
343 | } | |
344 | ||
345 | /**************************************************************************** | |
346 | ALSA PCM Interface | |
347 | ****************************************************************************/ | |
348 | ||
349 | /* | |
350 | * Digital hardware definition | |
351 | */ | |
352 | #define DEFAULT_FIFO_SIZE 384 | |
353 | static struct snd_pcm_hardware snd_cx25821_digital_hw = { | |
354 | .info = SNDRV_PCM_INFO_MMAP | | |
1a9fc855 MCC |
355 | SNDRV_PCM_INFO_INTERLEAVED | |
356 | SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID, | |
02b20b0b MCC |
357 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
358 | ||
1a9fc855 MCC |
359 | .rates = SNDRV_PCM_RATE_48000, |
360 | .rate_min = 48000, | |
361 | .rate_max = 48000, | |
02b20b0b MCC |
362 | .channels_min = 2, |
363 | .channels_max = 2, | |
364 | /* Analog audio output will be full of clicks and pops if there | |
365 | are not exactly four lines in the SRAM FIFO buffer. */ | |
1a9fc855 MCC |
366 | .period_bytes_min = DEFAULT_FIFO_SIZE / 3, |
367 | .period_bytes_max = DEFAULT_FIFO_SIZE / 3, | |
02b20b0b MCC |
368 | .periods_min = 1, |
369 | .periods_max = AUDIO_LINE_SIZE, | |
8889a8a5 OP |
370 | /* 128 * 128 = 16384 = 1024 * 16 */ |
371 | .buffer_bytes_max = (AUDIO_LINE_SIZE * AUDIO_LINE_SIZE), | |
02b20b0b MCC |
372 | }; |
373 | ||
02b20b0b MCC |
374 | /* |
375 | * audio pcm capture open callback | |
376 | */ | |
377 | static int snd_cx25821_pcm_open(struct snd_pcm_substream *substream) | |
378 | { | |
8889a8a5 | 379 | struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream); |
02b20b0b MCC |
380 | struct snd_pcm_runtime *runtime = substream->runtime; |
381 | int err; | |
382 | unsigned int bpl = 0; | |
383 | ||
384 | if (!chip) { | |
385 | printk(KERN_ERR "DEBUG: cx25821 can't find device struct." | |
1a9fc855 | 386 | " Can't proceed with open\n"); |
02b20b0b MCC |
387 | return -ENODEV; |
388 | } | |
389 | ||
1a9fc855 MCC |
390 | err = |
391 | snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS); | |
02b20b0b MCC |
392 | if (err < 0) |
393 | goto _error; | |
394 | ||
395 | chip->substream = substream; | |
396 | ||
397 | runtime->hw = snd_cx25821_digital_hw; | |
398 | ||
1a9fc855 MCC |
399 | if (cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size != |
400 | DEFAULT_FIFO_SIZE) { | |
8889a8a5 OP |
401 | /* since there are 3 audio Clusters */ |
402 | bpl = cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size / 3; | |
1a9fc855 | 403 | bpl &= ~7; /* must be multiple of 8 */ |
02b20b0b | 404 | |
8889a8a5 | 405 | if (bpl > AUDIO_LINE_SIZE) |
02b20b0b | 406 | bpl = AUDIO_LINE_SIZE; |
8889a8a5 | 407 | |
02b20b0b MCC |
408 | runtime->hw.period_bytes_min = bpl; |
409 | runtime->hw.period_bytes_max = bpl; | |
410 | } | |
411 | ||
412 | return 0; | |
8889a8a5 | 413 | _error: |
1a9fc855 | 414 | dprintk(1, "Error opening PCM!\n"); |
02b20b0b MCC |
415 | return err; |
416 | } | |
417 | ||
418 | /* | |
419 | * audio close callback | |
420 | */ | |
421 | static int snd_cx25821_close(struct snd_pcm_substream *substream) | |
422 | { | |
423 | return 0; | |
424 | } | |
425 | ||
426 | /* | |
427 | * hw_params callback | |
428 | */ | |
1a9fc855 MCC |
429 | static int snd_cx25821_hw_params(struct snd_pcm_substream *substream, |
430 | struct snd_pcm_hw_params *hw_params) | |
02b20b0b | 431 | { |
8889a8a5 | 432 | struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream); |
02b20b0b MCC |
433 | struct videobuf_dmabuf *dma; |
434 | ||
435 | struct cx25821_buffer *buf; | |
436 | int ret; | |
437 | ||
438 | if (substream->runtime->dma_area) { | |
439 | dsp_buffer_free(chip); | |
440 | substream->runtime->dma_area = NULL; | |
441 | } | |
442 | ||
02b20b0b MCC |
443 | chip->period_size = params_period_bytes(hw_params); |
444 | chip->num_periods = params_periods(hw_params); | |
445 | chip->dma_size = chip->period_size * params_periods(hw_params); | |
446 | ||
447 | BUG_ON(!chip->dma_size); | |
1a9fc855 | 448 | BUG_ON(chip->num_periods & (chip->num_periods - 1)); |
02b20b0b MCC |
449 | |
450 | buf = videobuf_sg_alloc(sizeof(*buf)); | |
451 | if (NULL == buf) | |
452 | return -ENOMEM; | |
453 | ||
8889a8a5 | 454 | if (chip->period_size > AUDIO_LINE_SIZE) |
02b20b0b | 455 | chip->period_size = AUDIO_LINE_SIZE; |
02b20b0b | 456 | |
02b20b0b | 457 | buf->vb.memory = V4L2_MEMORY_MMAP; |
1a9fc855 MCC |
458 | buf->vb.field = V4L2_FIELD_NONE; |
459 | buf->vb.width = chip->period_size; | |
460 | buf->bpl = chip->period_size; | |
02b20b0b | 461 | buf->vb.height = chip->num_periods; |
1a9fc855 | 462 | buf->vb.size = chip->dma_size; |
02b20b0b MCC |
463 | |
464 | dma = videobuf_to_dma(&buf->vb); | |
465 | videobuf_dma_init(dma); | |
466 | ||
467 | ret = videobuf_dma_init_kernel(dma, PCI_DMA_FROMDEVICE, | |
1a9fc855 MCC |
468 | (PAGE_ALIGN(buf->vb.size) >> |
469 | PAGE_SHIFT)); | |
02b20b0b MCC |
470 | if (ret < 0) |
471 | goto error; | |
472 | ||
473 | ret = videobuf_sg_dma_map(&chip->pci->dev, dma); | |
474 | if (ret < 0) | |
475 | goto error; | |
476 | ||
1a9fc855 MCC |
477 | ret = |
478 | cx25821_risc_databuffer_audio(chip->pci, &buf->risc, dma->sglist, | |
479 | buf->vb.width, buf->vb.height, 1); | |
480 | if (ret < 0) { | |
481 | printk(KERN_INFO | |
8889a8a5 | 482 | "DEBUG: ERROR after cx25821_risc_databuffer_audio()\n"); |
02b20b0b MCC |
483 | goto error; |
484 | } | |
485 | ||
02b20b0b | 486 | /* Loop back to start of program */ |
1a9fc855 | 487 | buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC); |
02b20b0b | 488 | buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma); |
1a9fc855 | 489 | buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */ |
02b20b0b MCC |
490 | |
491 | buf->vb.state = VIDEOBUF_PREPARED; | |
492 | ||
493 | chip->buf = buf; | |
494 | chip->dma_risc = dma; | |
495 | ||
496 | substream->runtime->dma_area = chip->dma_risc->vmalloc; | |
497 | substream->runtime->dma_bytes = chip->dma_size; | |
498 | substream->runtime->dma_addr = 0; | |
499 | ||
500 | return 0; | |
501 | ||
8889a8a5 | 502 | error: |
02b20b0b MCC |
503 | kfree(buf); |
504 | return ret; | |
505 | } | |
506 | ||
507 | /* | |
508 | * hw free callback | |
509 | */ | |
1a9fc855 | 510 | static int snd_cx25821_hw_free(struct snd_pcm_substream *substream) |
02b20b0b | 511 | { |
8889a8a5 | 512 | struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream); |
02b20b0b MCC |
513 | |
514 | if (substream->runtime->dma_area) { | |
515 | dsp_buffer_free(chip); | |
516 | substream->runtime->dma_area = NULL; | |
517 | } | |
518 | ||
519 | return 0; | |
520 | } | |
521 | ||
522 | /* | |
523 | * prepare callback | |
524 | */ | |
525 | static int snd_cx25821_prepare(struct snd_pcm_substream *substream) | |
526 | { | |
527 | return 0; | |
528 | } | |
529 | ||
530 | /* | |
531 | * trigger callback | |
532 | */ | |
1a9fc855 MCC |
533 | static int snd_cx25821_card_trigger(struct snd_pcm_substream *substream, |
534 | int cmd) | |
02b20b0b | 535 | { |
8889a8a5 | 536 | struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream); |
02b20b0b MCC |
537 | int err = 0; |
538 | ||
539 | /* Local interrupts are already disabled by ALSA */ | |
540 | spin_lock(&chip->reg_lock); | |
541 | ||
1a9fc855 MCC |
542 | switch (cmd) { |
543 | case SNDRV_PCM_TRIGGER_START: | |
544 | err = _cx25821_start_audio_dma(chip); | |
545 | break; | |
546 | case SNDRV_PCM_TRIGGER_STOP: | |
547 | err = _cx25821_stop_audio_dma(chip); | |
548 | break; | |
549 | default: | |
550 | err = -EINVAL; | |
551 | break; | |
02b20b0b MCC |
552 | } |
553 | ||
554 | spin_unlock(&chip->reg_lock); | |
555 | ||
556 | return err; | |
557 | } | |
558 | ||
559 | /* | |
560 | * pointer callback | |
561 | */ | |
1a9fc855 MCC |
562 | static snd_pcm_uframes_t snd_cx25821_pointer(struct snd_pcm_substream |
563 | *substream) | |
02b20b0b | 564 | { |
8889a8a5 | 565 | struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream); |
02b20b0b MCC |
566 | struct snd_pcm_runtime *runtime = substream->runtime; |
567 | u16 count; | |
568 | ||
569 | count = atomic_read(&chip->count); | |
570 | ||
1a9fc855 | 571 | return runtime->period_size * (count & (runtime->periods - 1)); |
02b20b0b MCC |
572 | } |
573 | ||
574 | /* | |
575 | * page callback (needed for mmap) | |
576 | */ | |
577 | static struct page *snd_cx25821_page(struct snd_pcm_substream *substream, | |
1a9fc855 | 578 | unsigned long offset) |
02b20b0b MCC |
579 | { |
580 | void *pageptr = substream->runtime->dma_area + offset; | |
581 | ||
582 | return vmalloc_to_page(pageptr); | |
583 | } | |
584 | ||
585 | /* | |
586 | * operators | |
587 | */ | |
588 | static struct snd_pcm_ops snd_cx25821_pcm_ops = { | |
589 | .open = snd_cx25821_pcm_open, | |
590 | .close = snd_cx25821_close, | |
591 | .ioctl = snd_pcm_lib_ioctl, | |
592 | .hw_params = snd_cx25821_hw_params, | |
593 | .hw_free = snd_cx25821_hw_free, | |
594 | .prepare = snd_cx25821_prepare, | |
595 | .trigger = snd_cx25821_card_trigger, | |
596 | .pointer = snd_cx25821_pointer, | |
597 | .page = snd_cx25821_page, | |
598 | }; | |
599 | ||
02b20b0b | 600 | /* |
8889a8a5 OP |
601 | * ALSA create a PCM device: Called when initializing the board. |
602 | * Sets up the name and hooks up the callbacks | |
02b20b0b | 603 | */ |
8889a8a5 OP |
604 | static int snd_cx25821_pcm(struct cx25821_audio_dev *chip, int device, |
605 | char *name) | |
02b20b0b MCC |
606 | { |
607 | struct snd_pcm *pcm; | |
608 | int err; | |
609 | ||
610 | err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm); | |
1a9fc855 MCC |
611 | if (err < 0) { |
612 | printk(KERN_INFO "ERROR: FAILED snd_pcm_new() in %s\n", | |
613 | __func__); | |
02b20b0b MCC |
614 | return err; |
615 | } | |
616 | pcm->private_data = chip; | |
617 | pcm->info_flags = 0; | |
618 | strcpy(pcm->name, name); | |
619 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx25821_pcm_ops); | |
620 | ||
621 | return 0; | |
622 | } | |
623 | ||
02b20b0b MCC |
624 | /**************************************************************************** |
625 | Basic Flow for Sound Devices | |
626 | ****************************************************************************/ | |
627 | ||
628 | /* | |
629 | * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio | |
630 | * Only boards with eeprom and byte 1 at eeprom=1 have it | |
631 | */ | |
632 | ||
633 | static struct pci_device_id cx25821_audio_pci_tbl[] __devinitdata = { | |
1a9fc855 MCC |
634 | {0x14f1, 0x0920, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, |
635 | {0,} | |
02b20b0b | 636 | }; |
1a9fc855 | 637 | |
02b20b0b MCC |
638 | MODULE_DEVICE_TABLE(pci, cx25821_audio_pci_tbl); |
639 | ||
1a9fc855 MCC |
640 | /* |
641 | * Not used in the function snd_cx25821_dev_free so removing | |
642 | * from the file. | |
643 | */ | |
02b20b0b | 644 | /* |
8889a8a5 | 645 | static int snd_cx25821_free(struct cx25821_audio_dev *chip) |
02b20b0b MCC |
646 | { |
647 | if (chip->irq >= 0) | |
648 | free_irq(chip->irq, chip); | |
649 | ||
650 | cx25821_dev_unregister(chip->dev); | |
651 | pci_disable_device(chip->pci); | |
652 | ||
653 | return 0; | |
654 | } | |
1a9fc855 | 655 | */ |
02b20b0b MCC |
656 | |
657 | /* | |
658 | * Component Destructor | |
659 | */ | |
1a9fc855 | 660 | static void snd_cx25821_dev_free(struct snd_card *card) |
02b20b0b | 661 | { |
8889a8a5 | 662 | struct cx25821_audio_dev *chip = card->private_data; |
02b20b0b | 663 | |
8889a8a5 | 664 | /* snd_cx25821_free(chip); */ |
02b20b0b MCC |
665 | snd_card_free(chip->card); |
666 | } | |
667 | ||
02b20b0b MCC |
668 | /* |
669 | * Alsa Constructor - Component probe | |
670 | */ | |
671 | static int cx25821_audio_initdev(struct cx25821_dev *dev) | |
672 | { | |
1a9fc855 | 673 | struct snd_card *card; |
8889a8a5 | 674 | struct cx25821_audio_dev *chip; |
1a9fc855 | 675 | int err; |
02b20b0b | 676 | |
1a9fc855 MCC |
677 | if (devno >= SNDRV_CARDS) { |
678 | printk(KERN_INFO "DEBUG ERROR: devno >= SNDRV_CARDS %s\n", | |
679 | __func__); | |
8889a8a5 | 680 | return -ENODEV; |
02b20b0b MCC |
681 | } |
682 | ||
683 | if (!enable[devno]) { | |
684 | ++devno; | |
685 | printk(KERN_INFO "DEBUG ERROR: !enable[devno] %s\n", __func__); | |
8889a8a5 | 686 | return -ENOENT; |
02b20b0b MCC |
687 | } |
688 | ||
53e712d0 | 689 | err = snd_card_create(index[devno], id[devno], THIS_MODULE, |
8889a8a5 | 690 | sizeof(struct cx25821_audio_dev), &card); |
53e712d0 | 691 | if (err < 0) { |
1a9fc855 MCC |
692 | printk(KERN_INFO |
693 | "DEBUG ERROR: cannot create snd_card_new in %s\n", | |
694 | __func__); | |
53e712d0 | 695 | return err; |
02b20b0b MCC |
696 | } |
697 | ||
698 | strcpy(card->driver, "cx25821"); | |
699 | ||
700 | /* Card "creation" */ | |
701 | card->private_free = snd_cx25821_dev_free; | |
8889a8a5 | 702 | chip = (struct cx25821_audio_dev *) card->private_data; |
02b20b0b MCC |
703 | spin_lock_init(&chip->reg_lock); |
704 | ||
705 | chip->dev = dev; | |
706 | chip->card = card; | |
707 | chip->pci = dev->pci; | |
708 | chip->iobase = pci_resource_start(dev->pci, 0); | |
709 | ||
02b20b0b MCC |
710 | chip->irq = dev->pci->irq; |
711 | ||
712 | err = request_irq(dev->pci->irq, cx25821_irq, | |
713 | IRQF_SHARED | IRQF_DISABLED, chip->dev->name, chip); | |
714 | ||
715 | if (err < 0) { | |
1a9fc855 MCC |
716 | printk(KERN_ERR "ERROR %s: can't get IRQ %d for ALSA\n", |
717 | chip->dev->name, dev->pci->irq); | |
02b20b0b MCC |
718 | goto error; |
719 | } | |
720 | ||
8889a8a5 | 721 | if ((err == snd_cx25821_pcm(chip, 0, "cx25821 Digital")) < 0) { |
1a9fc855 MCC |
722 | printk(KERN_INFO |
723 | "DEBUG ERROR: cannot create snd_cx25821_pcm %s\n", | |
724 | __func__); | |
02b20b0b MCC |
725 | goto error; |
726 | } | |
727 | ||
728 | snd_card_set_dev(card, &chip->pci->dev); | |
729 | ||
02b20b0b | 730 | strcpy(card->shortname, "cx25821"); |
1a9fc855 MCC |
731 | sprintf(card->longname, "%s at 0x%lx irq %d", chip->dev->name, |
732 | chip->iobase, chip->irq); | |
733 | strcpy(card->mixername, "CX25821"); | |
02b20b0b | 734 | |
1a9fc855 MCC |
735 | printk(KERN_INFO "%s/%i: ALSA support for cx25821 boards\n", |
736 | card->driver, devno); | |
02b20b0b MCC |
737 | |
738 | err = snd_card_register(card); | |
1a9fc855 MCC |
739 | if (err < 0) { |
740 | printk(KERN_INFO "DEBUG ERROR: cannot register sound card %s\n", | |
741 | __func__); | |
02b20b0b MCC |
742 | goto error; |
743 | } | |
744 | ||
745 | snd_cx25821_cards[devno] = card; | |
746 | ||
747 | devno++; | |
748 | return 0; | |
749 | ||
8889a8a5 | 750 | error: |
02b20b0b MCC |
751 | snd_card_free(card); |
752 | return err; | |
753 | } | |
754 | ||
02b20b0b MCC |
755 | /**************************************************************************** |
756 | LINUX MODULE INIT | |
757 | ****************************************************************************/ | |
758 | static void cx25821_audio_fini(void) | |
759 | { | |
760 | snd_card_free(snd_cx25821_cards[0]); | |
761 | } | |
762 | ||
763 | /* | |
764 | * Module initializer | |
765 | * | |
766 | * Loops through present saa7134 cards, and assigns an ALSA device | |
767 | * to each one | |
768 | * | |
769 | */ | |
770 | static int cx25821_alsa_init(void) | |
771 | { | |
772 | struct cx25821_dev *dev = NULL; | |
773 | struct list_head *list; | |
774 | ||
1a9fc855 | 775 | list_for_each(list, &cx25821_devlist) { |
02b20b0b MCC |
776 | dev = list_entry(list, struct cx25821_dev, devlist); |
777 | cx25821_audio_initdev(dev); | |
778 | } | |
779 | ||
780 | if (dev == NULL) | |
1a9fc855 MCC |
781 | printk(KERN_INFO |
782 | "cx25821 ERROR ALSA: no cx25821 cards found\n"); | |
02b20b0b MCC |
783 | |
784 | return 0; | |
785 | ||
786 | } | |
787 | ||
788 | late_initcall(cx25821_alsa_init); | |
789 | module_exit(cx25821_audio_fini); | |
790 | ||
791 | /* ----------------------------------------------------------- */ | |
792 | /* | |
793 | * Local variables: | |
794 | * c-basic-offset: 8 | |
795 | * End: | |
796 | */ |