1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/device.h>
27#include <linux/interrupt.h>
28#include <linux/vmalloc.h>
29#include <linux/dma-mapping.h>
30#include <linux/pci.h>
31
32#include <asm/delay.h>
33
34#include <sound/core.h>
35#include <sound/pcm.h>
36#include <sound/pcm_params.h>
37#include <sound/control.h>
38#include <sound/initval.h>
39
40#include <sound/tlv.h>
41
42
43#include "cx23885.h"
44#include "cx23885-reg.h"
45
46#define AUDIO_SRAM_CHANNEL SRAM_CH07
47
48#define dprintk(level, fmt, arg...) do { \
49 if (audio_debug + 1 > level) \
50 printk(KERN_INFO "%s: " fmt, chip->dev->name , ## arg); \
51} while(0)
52
53#define dprintk_core(level, fmt, arg...) if (audio_debug >= level) \
54 printk(KERN_DEBUG "%s: " fmt, chip->dev->name , ## arg)
55
56
57
58
59
60static unsigned int disable_analog_audio;
61module_param(disable_analog_audio, int, 0644);
62MODULE_PARM_DESC(disable_analog_audio, "disable analog audio ALSA driver");
63
64static unsigned int audio_debug;
65module_param(audio_debug, int, 0644);
66MODULE_PARM_DESC(audio_debug, "enable debug messages [analog audio]");
67
68
69
70
71
72
73#define AUD_INT_DN_RISCI1 (1 << 0)
74#define AUD_INT_UP_RISCI1 (1 << 1)
75#define AUD_INT_RDS_DN_RISCI1 (1 << 2)
76#define AUD_INT_DN_RISCI2 (1 << 4)
77#define AUD_INT_UP_RISCI2 (1 << 5)
78#define AUD_INT_RDS_DN_RISCI2 (1 << 6)
79#define AUD_INT_DN_SYNC (1 << 12)
80#define AUD_INT_UP_SYNC (1 << 13)
81#define AUD_INT_RDS_DN_SYNC (1 << 14)
82#define AUD_INT_OPC_ERR (1 << 16)
83#define AUD_INT_BER_IRQ (1 << 20)
84#define AUD_INT_MCHG_IRQ (1 << 21)
85#define GP_COUNT_CONTROL_RESET 0x3
86
87
88
89
90
91static int cx23885_start_audio_dma(struct cx23885_audio_dev *chip)
92{
93 struct cx23885_audio_buffer *buf = chip->buf;
94 struct cx23885_dev *dev = chip->dev;
95 struct sram_channel *audio_ch =
96 &dev->sram_channels[AUDIO_SRAM_CHANNEL];
97
98 dprintk(1, "%s()\n", __func__);
99
100
101 cx_clear(AUD_INT_DMA_CTL, 0x11);
102
103
104 cx23885_sram_channel_setup(chip->dev, audio_ch, buf->bpl,
105 buf->risc.dma);
106
107
108 cx_write(AUD_INT_A_LNGTH, buf->bpl);
109
110
111 cx_write(AUD_INT_A_MODE, 1);
112
113
114 cx_write(AUD_INT_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
115 atomic_set(&chip->count, 0);
116
117 dprintk(1, "Start audio DMA, %d B/line, %d lines/FIFO, %d periods, %d "
118 "byte buffer\n", buf->bpl, cx_read(audio_ch->cmds_start+12)>>1,
119 chip->num_periods, buf->bpl * chip->num_periods);
120
121
122 cx_write(AUDIO_INT_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
123 AUD_INT_DN_RISCI1);
124
125
126 cx_write(AUDIO_INT_INT_STAT, ~0);
127
128
129 cx_set(PCI_INT_MSK, chip->dev->pci_irqmask | PCI_MSK_AUD_INT);
130
131
132 cx_set(DEV_CNTRL2, (1<<5));
133 cx_set(AUD_INT_DMA_CTL, 0x11);
134
135 if (audio_debug)
136 cx23885_sram_channel_dump(chip->dev, audio_ch);
137
138 return 0;
139}
140
141
142
143
144static int cx23885_stop_audio_dma(struct cx23885_audio_dev *chip)
145{
146 struct cx23885_dev *dev = chip->dev;
147 dprintk(1, "Stopping audio DMA\n");
148
149
150 cx_clear(AUD_INT_DMA_CTL, 0x11);
151
152
153 cx_clear(PCI_INT_MSK, PCI_MSK_AUD_INT);
154 cx_clear(AUDIO_INT_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
155 AUD_INT_DN_RISCI1);
156
157 if (audio_debug)
158 cx23885_sram_channel_dump(chip->dev,
159 &dev->sram_channels[AUDIO_SRAM_CHANNEL]);
160
161 return 0;
162}
163
164
165
166
167int cx23885_audio_irq(struct cx23885_dev *dev, u32 status, u32 mask)
168{
169 struct cx23885_audio_dev *chip = dev->audio_dev;
170
171 if (0 == (status & mask))
172 return 0;
173
174 cx_write(AUDIO_INT_INT_STAT, status);
175
176
177 if (status & AUD_INT_OPC_ERR) {
178 printk(KERN_WARNING "%s/1: Audio risc op code error\n",
179 dev->name);
180 cx_clear(AUD_INT_DMA_CTL, 0x11);
181 cx23885_sram_channel_dump(dev,
182 &dev->sram_channels[AUDIO_SRAM_CHANNEL]);
183 }
184 if (status & AUD_INT_DN_SYNC) {
185 dprintk(1, "Downstream sync error\n");
186 cx_write(AUD_INT_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
187 return 1;
188 }
189
190 if (status & AUD_INT_DN_RISCI1) {
191 atomic_set(&chip->count, cx_read(AUD_INT_A_GPCNT));
192 snd_pcm_period_elapsed(chip->substream);
193 }
194
195
196 return 1;
197}
198
199static int dsp_buffer_free(struct cx23885_audio_dev *chip)
200{
201 BUG_ON(!chip->dma_size);
202
203 dprintk(2, "Freeing buffer\n");
204 videobuf_dma_unmap(&chip->pci->dev, chip->dma_risc);
205 videobuf_dma_free(chip->dma_risc);
206 btcx_riscmem_free(chip->pci, &chip->buf->risc);
207 kfree(chip->buf);
208
209 chip->dma_risc = NULL;
210 chip->dma_size = 0;
211
212 return 0;
213}
214
215
216
217
218
219
220
221
222#define DEFAULT_FIFO_SIZE 4096
223
224static struct snd_pcm_hardware snd_cx23885_digital_hw = {
225 .info = SNDRV_PCM_INFO_MMAP |
226 SNDRV_PCM_INFO_INTERLEAVED |
227 SNDRV_PCM_INFO_BLOCK_TRANSFER |
228 SNDRV_PCM_INFO_MMAP_VALID,
229 .formats = SNDRV_PCM_FMTBIT_S16_LE,
230
231 .rates = SNDRV_PCM_RATE_48000,
232 .rate_min = 48000,
233 .rate_max = 48000,
234 .channels_min = 2,
235 .channels_max = 2,
236
237
238 .period_bytes_min = DEFAULT_FIFO_SIZE/4,
239 .period_bytes_max = DEFAULT_FIFO_SIZE/4,
240 .periods_min = 1,
241 .periods_max = 1024,
242 .buffer_bytes_max = (1024*1024),
243};
244
245
246
247
248static int snd_cx23885_pcm_open(struct snd_pcm_substream *substream)
249{
250 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
251 struct snd_pcm_runtime *runtime = substream->runtime;
252 int err;
253
254 if (!chip) {
255 printk(KERN_ERR "BUG: cx23885 can't find device struct."
256 " Can't proceed with open\n");
257 return -ENODEV;
258 }
259
260 err = snd_pcm_hw_constraint_pow2(runtime, 0,
261 SNDRV_PCM_HW_PARAM_PERIODS);
262 if (err < 0)
263 goto _error;
264
265 chip->substream = substream;
266
267 runtime->hw = snd_cx23885_digital_hw;
268
269 if (chip->dev->sram_channels[AUDIO_SRAM_CHANNEL].fifo_size !=
270 DEFAULT_FIFO_SIZE) {
271 unsigned int bpl = chip->dev->
272 sram_channels[AUDIO_SRAM_CHANNEL].fifo_size / 4;
273 bpl &= ~7;
274 runtime->hw.period_bytes_min = bpl;
275 runtime->hw.period_bytes_max = bpl;
276 }
277
278 return 0;
279_error:
280 dprintk(1, "Error opening PCM!\n");
281 return err;
282}
283
284
285
286
287static int snd_cx23885_close(struct snd_pcm_substream *substream)
288{
289 return 0;
290}
291
292
293
294
295static int snd_cx23885_hw_params(struct snd_pcm_substream *substream,
296 struct snd_pcm_hw_params *hw_params)
297{
298 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
299 struct videobuf_dmabuf *dma;
300
301 struct cx23885_audio_buffer *buf;
302 int ret;
303
304 if (substream->runtime->dma_area) {
305 dsp_buffer_free(chip);
306 substream->runtime->dma_area = NULL;
307 }
308
309 chip->period_size = params_period_bytes(hw_params);
310 chip->num_periods = params_periods(hw_params);
311 chip->dma_size = chip->period_size * params_periods(hw_params);
312
313 BUG_ON(!chip->dma_size);
314 BUG_ON(chip->num_periods & (chip->num_periods-1));
315
316 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
317 if (NULL == buf)
318 return -ENOMEM;
319
320 buf->bpl = chip->period_size;
321
322 dma = &buf->dma;
323 videobuf_dma_init(dma);
324 ret = videobuf_dma_init_kernel(dma, PCI_DMA_FROMDEVICE,
325 (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
326 if (ret < 0)
327 goto error;
328
329 ret = videobuf_dma_map(&chip->pci->dev, dma);
330 if (ret < 0)
331 goto error;
332
333 ret = cx23885_risc_databuffer(chip->pci, &buf->risc, dma->sglist,
334 chip->period_size, chip->num_periods, 1);
335 if (ret < 0)
336 goto error;
337
338
339 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP|RISC_IRQ1|RISC_CNT_INC);
340 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
341 buf->risc.jmp[2] = cpu_to_le32(0);
342
343 chip->buf = buf;
344 chip->dma_risc = dma;
345
346 substream->runtime->dma_area = chip->dma_risc->vaddr;
347 substream->runtime->dma_bytes = chip->dma_size;
348 substream->runtime->dma_addr = 0;
349
350 return 0;
351
352error:
353 kfree(buf);
354 return ret;
355}
356
357
358
359
360static int snd_cx23885_hw_free(struct snd_pcm_substream *substream)
361{
362
363 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
364
365 if (substream->runtime->dma_area) {
366 dsp_buffer_free(chip);
367 substream->runtime->dma_area = NULL;
368 }
369
370 return 0;
371}
372
373
374
375
376static int snd_cx23885_prepare(struct snd_pcm_substream *substream)
377{
378 return 0;
379}
380
381
382
383
384static int snd_cx23885_card_trigger(struct snd_pcm_substream *substream,
385 int cmd)
386{
387 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
388 int err;
389
390
391 spin_lock(&chip->lock);
392
393 switch (cmd) {
394 case SNDRV_PCM_TRIGGER_START:
395 err = cx23885_start_audio_dma(chip);
396 break;
397 case SNDRV_PCM_TRIGGER_STOP:
398 err = cx23885_stop_audio_dma(chip);
399 break;
400 default:
401 err = -EINVAL;
402 break;
403 }
404
405 spin_unlock(&chip->lock);
406
407 return err;
408}
409
410
411
412
413static snd_pcm_uframes_t snd_cx23885_pointer(
414 struct snd_pcm_substream *substream)
415{
416 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
417 struct snd_pcm_runtime *runtime = substream->runtime;
418 u16 count;
419
420 count = atomic_read(&chip->count);
421
422 return runtime->period_size * (count & (runtime->periods-1));
423}
424
425
426
427
428static struct page *snd_cx23885_page(struct snd_pcm_substream *substream,
429 unsigned long offset)
430{
431 void *pageptr = substream->runtime->dma_area + offset;
432 return vmalloc_to_page(pageptr);
433}
434
435
436
437
438static struct snd_pcm_ops snd_cx23885_pcm_ops = {
439 .open = snd_cx23885_pcm_open,
440 .close = snd_cx23885_close,
441 .ioctl = snd_pcm_lib_ioctl,
442 .hw_params = snd_cx23885_hw_params,
443 .hw_free = snd_cx23885_hw_free,
444 .prepare = snd_cx23885_prepare,
445 .trigger = snd_cx23885_card_trigger,
446 .pointer = snd_cx23885_pointer,
447 .page = snd_cx23885_page,
448};
449
450
451
452
453static int snd_cx23885_pcm(struct cx23885_audio_dev *chip, int device,
454 char *name)
455{
456 int err;
457 struct snd_pcm *pcm;
458
459 err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
460 if (err < 0)
461 return err;
462 pcm->private_data = chip;
463 strcpy(pcm->name, name);
464 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx23885_pcm_ops);
465
466 return 0;
467}
468
469
470
471
472
473
474
475
476
477struct cx23885_audio_dev *cx23885_audio_register(struct cx23885_dev *dev)
478{
479 struct snd_card *card;
480 struct cx23885_audio_dev *chip;
481 int err;
482
483 if (disable_analog_audio)
484 return NULL;
485
486 if (dev->sram_channels[AUDIO_SRAM_CHANNEL].cmds_start == 0) {
487 printk(KERN_WARNING "%s(): Missing SRAM channel configuration "
488 "for analog TV Audio\n", __func__);
489 return NULL;
490 }
491
492 err = snd_card_new(&dev->pci->dev,
493 SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
494 THIS_MODULE, sizeof(struct cx23885_audio_dev), &card);
495 if (err < 0)
496 goto error;
497
498 chip = (struct cx23885_audio_dev *) card->private_data;
499 chip->dev = dev;
500 chip->pci = dev->pci;
501 chip->card = card;
502 spin_lock_init(&chip->lock);
503
504 err = snd_cx23885_pcm(chip, 0, "CX23885 Digital");
505 if (err < 0)
506 goto error;
507
508 strcpy(card->driver, "CX23885");
509 sprintf(card->shortname, "Conexant CX23885");
510 sprintf(card->longname, "%s at %s", card->shortname, dev->name);
511
512 err = snd_card_register(card);
513 if (err < 0)
514 goto error;
515
516 dprintk(0, "registered ALSA audio device\n");
517
518 return chip;
519
520error:
521 snd_card_free(card);
522 printk(KERN_ERR "%s(): Failed to register analog "
523 "audio adapter\n", __func__);
524
525 return NULL;
526}
527
528
529
530
531void cx23885_audio_unregister(struct cx23885_dev *dev)
532{
533 struct cx23885_audio_dev *chip = dev->audio_dev;
534
535 snd_card_free(chip->card);
536}
537