2 * Driver for Atmel AC97C
4 * Copyright (C) 2005-2009 Atmel Corporation
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/bitmap.h>
13 #include <linux/device.h>
14 #include <linux/dmaengine.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/atmel_pdc.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/mutex.h>
22 #include <linux/gpio.h>
23 #include <linux/types.h>
26 #include <sound/core.h>
27 #include <sound/initval.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/ac97_codec.h>
31 #include <sound/atmel-ac97c.h>
32 #include <sound/memalloc.h>
34 #include <linux/dw_dmac.h>
37 #include <mach/gpio.h>
39 #ifdef CONFIG_ARCH_AT91
40 #include <mach/hardware.h>
52 /* Serialize access to opened variable */
53 static DEFINE_MUTEX(opened_mutex);
55 struct atmel_ac97c_dma {
56 struct dma_chan *rx_chan;
57 struct dma_chan *tx_chan;
62 struct platform_device *pdev;
63 struct atmel_ac97c_dma dma;
65 struct snd_pcm_substream *playback_substream;
66 struct snd_pcm_substream *capture_substream;
67 struct snd_card *card;
69 struct snd_ac97 *ac97;
70 struct snd_ac97_bus *ac97_bus;
73 unsigned int cur_rate;
75 int playback_period, capture_period;
76 /* Serialize access to opened variable */
84 #define get_chip(card) ((struct atmel_ac97c *)(card)->private_data)
86 #define ac97c_writel(chip, reg, val) \
87 __raw_writel((val), (chip)->regs + AC97C_##reg)
88 #define ac97c_readl(chip, reg) \
89 __raw_readl((chip)->regs + AC97C_##reg)
91 /* This function is called by the DMA driver. */
92 static void atmel_ac97c_dma_playback_period_done(void *arg)
94 struct atmel_ac97c *chip = arg;
95 snd_pcm_period_elapsed(chip->playback_substream);
98 static void atmel_ac97c_dma_capture_period_done(void *arg)
100 struct atmel_ac97c *chip = arg;
101 snd_pcm_period_elapsed(chip->capture_substream);
104 static int atmel_ac97c_prepare_dma(struct atmel_ac97c *chip,
105 struct snd_pcm_substream *substream,
106 enum dma_transfer_direction direction)
108 struct dma_chan *chan;
109 struct dw_cyclic_desc *cdesc;
110 struct snd_pcm_runtime *runtime = substream->runtime;
111 unsigned long buffer_len, period_len;
114 * We don't do DMA on "complex" transfers, i.e. with
115 * non-halfword-aligned buffers or lengths.
117 if (runtime->dma_addr & 1 || runtime->buffer_size & 1) {
118 dev_dbg(&chip->pdev->dev, "too complex transfer\n");
122 if (direction == DMA_MEM_TO_DEV)
123 chan = chip->dma.tx_chan;
125 chan = chip->dma.rx_chan;
127 buffer_len = frames_to_bytes(runtime, runtime->buffer_size);
128 period_len = frames_to_bytes(runtime, runtime->period_size);
130 cdesc = dw_dma_cyclic_prep(chan, runtime->dma_addr, buffer_len,
131 period_len, direction);
133 dev_dbg(&chip->pdev->dev, "could not prepare cyclic DMA\n");
134 return PTR_ERR(cdesc);
137 if (direction == DMA_MEM_TO_DEV) {
138 cdesc->period_callback = atmel_ac97c_dma_playback_period_done;
139 set_bit(DMA_TX_READY, &chip->flags);
141 cdesc->period_callback = atmel_ac97c_dma_capture_period_done;
142 set_bit(DMA_RX_READY, &chip->flags);
145 cdesc->period_callback_param = chip;
150 static struct snd_pcm_hardware atmel_ac97c_hw = {
151 .info = (SNDRV_PCM_INFO_MMAP
152 | SNDRV_PCM_INFO_MMAP_VALID
153 | SNDRV_PCM_INFO_INTERLEAVED
154 | SNDRV_PCM_INFO_BLOCK_TRANSFER
155 | SNDRV_PCM_INFO_JOINT_DUPLEX
156 | SNDRV_PCM_INFO_RESUME
157 | SNDRV_PCM_INFO_PAUSE),
158 .formats = (SNDRV_PCM_FMTBIT_S16_BE
159 | SNDRV_PCM_FMTBIT_S16_LE),
160 .rates = (SNDRV_PCM_RATE_CONTINUOUS),
165 .buffer_bytes_max = 2 * 2 * 64 * 2048,
166 .period_bytes_min = 4096,
167 .period_bytes_max = 4096,
172 static int atmel_ac97c_playback_open(struct snd_pcm_substream *substream)
174 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
175 struct snd_pcm_runtime *runtime = substream->runtime;
177 mutex_lock(&opened_mutex);
179 runtime->hw = atmel_ac97c_hw;
180 if (chip->cur_rate) {
181 runtime->hw.rate_min = chip->cur_rate;
182 runtime->hw.rate_max = chip->cur_rate;
184 if (chip->cur_format)
185 runtime->hw.formats = (1ULL << chip->cur_format);
186 mutex_unlock(&opened_mutex);
187 chip->playback_substream = substream;
191 static int atmel_ac97c_capture_open(struct snd_pcm_substream *substream)
193 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
194 struct snd_pcm_runtime *runtime = substream->runtime;
196 mutex_lock(&opened_mutex);
198 runtime->hw = atmel_ac97c_hw;
199 if (chip->cur_rate) {
200 runtime->hw.rate_min = chip->cur_rate;
201 runtime->hw.rate_max = chip->cur_rate;
203 if (chip->cur_format)
204 runtime->hw.formats = (1ULL << chip->cur_format);
205 mutex_unlock(&opened_mutex);
206 chip->capture_substream = substream;
210 static int atmel_ac97c_playback_close(struct snd_pcm_substream *substream)
212 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
214 mutex_lock(&opened_mutex);
218 chip->cur_format = 0;
220 mutex_unlock(&opened_mutex);
222 chip->playback_substream = NULL;
227 static int atmel_ac97c_capture_close(struct snd_pcm_substream *substream)
229 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
231 mutex_lock(&opened_mutex);
235 chip->cur_format = 0;
237 mutex_unlock(&opened_mutex);
239 chip->capture_substream = NULL;
244 static int atmel_ac97c_playback_hw_params(struct snd_pcm_substream *substream,
245 struct snd_pcm_hw_params *hw_params)
247 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
250 retval = snd_pcm_lib_malloc_pages(substream,
251 params_buffer_bytes(hw_params));
254 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
255 if (cpu_is_at32ap7000()) {
256 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
258 if (test_and_clear_bit(DMA_TX_READY, &chip->flags))
259 dw_dma_cyclic_free(chip->dma.tx_chan);
261 /* Set restrictions to params. */
262 mutex_lock(&opened_mutex);
263 chip->cur_rate = params_rate(hw_params);
264 chip->cur_format = params_format(hw_params);
265 mutex_unlock(&opened_mutex);
270 static int atmel_ac97c_capture_hw_params(struct snd_pcm_substream *substream,
271 struct snd_pcm_hw_params *hw_params)
273 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
276 retval = snd_pcm_lib_malloc_pages(substream,
277 params_buffer_bytes(hw_params));
280 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
281 if (cpu_is_at32ap7000()) {
284 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
286 if (test_and_clear_bit(DMA_RX_READY, &chip->flags))
287 dw_dma_cyclic_free(chip->dma.rx_chan);
290 /* Set restrictions to params. */
291 mutex_lock(&opened_mutex);
292 chip->cur_rate = params_rate(hw_params);
293 chip->cur_format = params_format(hw_params);
294 mutex_unlock(&opened_mutex);
299 static int atmel_ac97c_playback_hw_free(struct snd_pcm_substream *substream)
301 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
302 if (cpu_is_at32ap7000()) {
303 if (test_and_clear_bit(DMA_TX_READY, &chip->flags))
304 dw_dma_cyclic_free(chip->dma.tx_chan);
306 return snd_pcm_lib_free_pages(substream);
309 static int atmel_ac97c_capture_hw_free(struct snd_pcm_substream *substream)
311 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
312 if (cpu_is_at32ap7000()) {
313 if (test_and_clear_bit(DMA_RX_READY, &chip->flags))
314 dw_dma_cyclic_free(chip->dma.rx_chan);
316 return snd_pcm_lib_free_pages(substream);
319 static int atmel_ac97c_playback_prepare(struct snd_pcm_substream *substream)
321 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
322 struct snd_pcm_runtime *runtime = substream->runtime;
323 int block_size = frames_to_bytes(runtime, runtime->period_size);
324 unsigned long word = ac97c_readl(chip, OCA);
327 chip->playback_period = 0;
328 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
330 /* assign channels to AC97C channel A */
331 switch (runtime->channels) {
333 word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
336 word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
337 | AC97C_CH_ASSIGN(PCM_RIGHT, A);
340 /* TODO: support more than two channels */
343 ac97c_writel(chip, OCA, word);
345 /* configure sample format and size */
346 word = ac97c_readl(chip, CAMR);
347 if (chip->opened <= 1)
348 word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
350 word |= AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
352 switch (runtime->format) {
353 case SNDRV_PCM_FORMAT_S16_LE:
354 if (cpu_is_at32ap7000())
355 word |= AC97C_CMR_CEM_LITTLE;
357 case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
358 word &= ~(AC97C_CMR_CEM_LITTLE);
361 word = ac97c_readl(chip, OCA);
362 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
363 ac97c_writel(chip, OCA, word);
367 /* Enable underrun interrupt on channel A */
368 word |= AC97C_CSR_UNRUN;
370 ac97c_writel(chip, CAMR, word);
372 /* Enable channel A event interrupt */
373 word = ac97c_readl(chip, IMR);
374 word |= AC97C_SR_CAEVT;
375 ac97c_writel(chip, IER, word);
377 /* set variable rate if needed */
378 if (runtime->rate != 48000) {
379 word = ac97c_readl(chip, MR);
380 word |= AC97C_MR_VRA;
381 ac97c_writel(chip, MR, word);
383 word = ac97c_readl(chip, MR);
384 word &= ~(AC97C_MR_VRA);
385 ac97c_writel(chip, MR, word);
388 retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
391 dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
394 if (cpu_is_at32ap7000()) {
395 if (!test_bit(DMA_TX_READY, &chip->flags))
396 retval = atmel_ac97c_prepare_dma(chip, substream,
399 /* Initialize and start the PDC */
400 writel(runtime->dma_addr, chip->regs + ATMEL_PDC_TPR);
401 writel(block_size / 2, chip->regs + ATMEL_PDC_TCR);
402 writel(runtime->dma_addr + block_size,
403 chip->regs + ATMEL_PDC_TNPR);
404 writel(block_size / 2, chip->regs + ATMEL_PDC_TNCR);
410 static int atmel_ac97c_capture_prepare(struct snd_pcm_substream *substream)
412 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
413 struct snd_pcm_runtime *runtime = substream->runtime;
414 int block_size = frames_to_bytes(runtime, runtime->period_size);
415 unsigned long word = ac97c_readl(chip, ICA);
418 chip->capture_period = 0;
419 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
421 /* assign channels to AC97C channel A */
422 switch (runtime->channels) {
424 word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
427 word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
428 | AC97C_CH_ASSIGN(PCM_RIGHT, A);
431 /* TODO: support more than two channels */
434 ac97c_writel(chip, ICA, word);
436 /* configure sample format and size */
437 word = ac97c_readl(chip, CAMR);
438 if (chip->opened <= 1)
439 word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
441 word |= AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
443 switch (runtime->format) {
444 case SNDRV_PCM_FORMAT_S16_LE:
445 if (cpu_is_at32ap7000())
446 word |= AC97C_CMR_CEM_LITTLE;
448 case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
449 word &= ~(AC97C_CMR_CEM_LITTLE);
452 word = ac97c_readl(chip, ICA);
453 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
454 ac97c_writel(chip, ICA, word);
458 /* Enable overrun interrupt on channel A */
459 word |= AC97C_CSR_OVRUN;
461 ac97c_writel(chip, CAMR, word);
463 /* Enable channel A event interrupt */
464 word = ac97c_readl(chip, IMR);
465 word |= AC97C_SR_CAEVT;
466 ac97c_writel(chip, IER, word);
468 /* set variable rate if needed */
469 if (runtime->rate != 48000) {
470 word = ac97c_readl(chip, MR);
471 word |= AC97C_MR_VRA;
472 ac97c_writel(chip, MR, word);
474 word = ac97c_readl(chip, MR);
475 word &= ~(AC97C_MR_VRA);
476 ac97c_writel(chip, MR, word);
479 retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE,
482 dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
485 if (cpu_is_at32ap7000()) {
486 if (!test_bit(DMA_RX_READY, &chip->flags))
487 retval = atmel_ac97c_prepare_dma(chip, substream,
490 /* Initialize and start the PDC */
491 writel(runtime->dma_addr, chip->regs + ATMEL_PDC_RPR);
492 writel(block_size / 2, chip->regs + ATMEL_PDC_RCR);
493 writel(runtime->dma_addr + block_size,
494 chip->regs + ATMEL_PDC_RNPR);
495 writel(block_size / 2, chip->regs + ATMEL_PDC_RNCR);
502 atmel_ac97c_playback_trigger(struct snd_pcm_substream *substream, int cmd)
504 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
505 unsigned long camr, ptcr = 0;
508 camr = ac97c_readl(chip, CAMR);
511 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
512 case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
513 case SNDRV_PCM_TRIGGER_START:
514 if (cpu_is_at32ap7000()) {
515 retval = dw_dma_cyclic_start(chip->dma.tx_chan);
519 ptcr = ATMEL_PDC_TXTEN;
521 camr |= AC97C_CMR_CENA | AC97C_CSR_ENDTX;
523 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
524 case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
525 case SNDRV_PCM_TRIGGER_STOP:
526 if (cpu_is_at32ap7000())
527 dw_dma_cyclic_stop(chip->dma.tx_chan);
529 ptcr |= ATMEL_PDC_TXTDIS;
530 if (chip->opened <= 1)
531 camr &= ~AC97C_CMR_CENA;
538 ac97c_writel(chip, CAMR, camr);
539 if (!cpu_is_at32ap7000())
540 writel(ptcr, chip->regs + ATMEL_PDC_PTCR);
546 atmel_ac97c_capture_trigger(struct snd_pcm_substream *substream, int cmd)
548 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
549 unsigned long camr, ptcr = 0;
552 camr = ac97c_readl(chip, CAMR);
553 ptcr = readl(chip->regs + ATMEL_PDC_PTSR);
556 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
557 case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
558 case SNDRV_PCM_TRIGGER_START:
559 if (cpu_is_at32ap7000()) {
560 retval = dw_dma_cyclic_start(chip->dma.rx_chan);
564 ptcr = ATMEL_PDC_RXTEN;
566 camr |= AC97C_CMR_CENA | AC97C_CSR_ENDRX;
568 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
569 case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
570 case SNDRV_PCM_TRIGGER_STOP:
571 if (cpu_is_at32ap7000())
572 dw_dma_cyclic_stop(chip->dma.rx_chan);
574 ptcr |= (ATMEL_PDC_RXTDIS);
575 if (chip->opened <= 1)
576 camr &= ~AC97C_CMR_CENA;
583 ac97c_writel(chip, CAMR, camr);
584 if (!cpu_is_at32ap7000())
585 writel(ptcr, chip->regs + ATMEL_PDC_PTCR);
590 static snd_pcm_uframes_t
591 atmel_ac97c_playback_pointer(struct snd_pcm_substream *substream)
593 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
594 struct snd_pcm_runtime *runtime = substream->runtime;
595 snd_pcm_uframes_t frames;
598 if (cpu_is_at32ap7000())
599 bytes = dw_dma_get_src_addr(chip->dma.tx_chan);
601 bytes = readl(chip->regs + ATMEL_PDC_TPR);
602 bytes -= runtime->dma_addr;
604 frames = bytes_to_frames(runtime, bytes);
605 if (frames >= runtime->buffer_size)
606 frames -= runtime->buffer_size;
610 static snd_pcm_uframes_t
611 atmel_ac97c_capture_pointer(struct snd_pcm_substream *substream)
613 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
614 struct snd_pcm_runtime *runtime = substream->runtime;
615 snd_pcm_uframes_t frames;
618 if (cpu_is_at32ap7000())
619 bytes = dw_dma_get_dst_addr(chip->dma.rx_chan);
621 bytes = readl(chip->regs + ATMEL_PDC_RPR);
622 bytes -= runtime->dma_addr;
624 frames = bytes_to_frames(runtime, bytes);
625 if (frames >= runtime->buffer_size)
626 frames -= runtime->buffer_size;
630 static struct snd_pcm_ops atmel_ac97_playback_ops = {
631 .open = atmel_ac97c_playback_open,
632 .close = atmel_ac97c_playback_close,
633 .ioctl = snd_pcm_lib_ioctl,
634 .hw_params = atmel_ac97c_playback_hw_params,
635 .hw_free = atmel_ac97c_playback_hw_free,
636 .prepare = atmel_ac97c_playback_prepare,
637 .trigger = atmel_ac97c_playback_trigger,
638 .pointer = atmel_ac97c_playback_pointer,
641 static struct snd_pcm_ops atmel_ac97_capture_ops = {
642 .open = atmel_ac97c_capture_open,
643 .close = atmel_ac97c_capture_close,
644 .ioctl = snd_pcm_lib_ioctl,
645 .hw_params = atmel_ac97c_capture_hw_params,
646 .hw_free = atmel_ac97c_capture_hw_free,
647 .prepare = atmel_ac97c_capture_prepare,
648 .trigger = atmel_ac97c_capture_trigger,
649 .pointer = atmel_ac97c_capture_pointer,
652 static irqreturn_t atmel_ac97c_interrupt(int irq, void *dev)
654 struct atmel_ac97c *chip = (struct atmel_ac97c *)dev;
655 irqreturn_t retval = IRQ_NONE;
656 u32 sr = ac97c_readl(chip, SR);
657 u32 casr = ac97c_readl(chip, CASR);
658 u32 cosr = ac97c_readl(chip, COSR);
659 u32 camr = ac97c_readl(chip, CAMR);
661 if (sr & AC97C_SR_CAEVT) {
662 struct snd_pcm_runtime *runtime;
663 int offset, next_period, block_size;
664 dev_dbg(&chip->pdev->dev, "channel A event%s%s%s%s%s%s\n",
665 casr & AC97C_CSR_OVRUN ? " OVRUN" : "",
666 casr & AC97C_CSR_RXRDY ? " RXRDY" : "",
667 casr & AC97C_CSR_UNRUN ? " UNRUN" : "",
668 casr & AC97C_CSR_TXEMPTY ? " TXEMPTY" : "",
669 casr & AC97C_CSR_TXRDY ? " TXRDY" : "",
670 !casr ? " NONE" : "");
671 if (!cpu_is_at32ap7000()) {
672 if ((casr & camr) & AC97C_CSR_ENDTX) {
673 runtime = chip->playback_substream->runtime;
674 block_size = frames_to_bytes(runtime,
675 runtime->period_size);
676 chip->playback_period++;
678 if (chip->playback_period == runtime->periods)
679 chip->playback_period = 0;
680 next_period = chip->playback_period + 1;
681 if (next_period == runtime->periods)
684 offset = block_size * next_period;
686 writel(runtime->dma_addr + offset,
687 chip->regs + ATMEL_PDC_TNPR);
688 writel(block_size / 2,
689 chip->regs + ATMEL_PDC_TNCR);
691 snd_pcm_period_elapsed(
692 chip->playback_substream);
694 if ((casr & camr) & AC97C_CSR_ENDRX) {
695 runtime = chip->capture_substream->runtime;
696 block_size = frames_to_bytes(runtime,
697 runtime->period_size);
698 chip->capture_period++;
700 if (chip->capture_period == runtime->periods)
701 chip->capture_period = 0;
702 next_period = chip->capture_period + 1;
703 if (next_period == runtime->periods)
706 offset = block_size * next_period;
708 writel(runtime->dma_addr + offset,
709 chip->regs + ATMEL_PDC_RNPR);
710 writel(block_size / 2,
711 chip->regs + ATMEL_PDC_RNCR);
712 snd_pcm_period_elapsed(chip->capture_substream);
715 retval = IRQ_HANDLED;
718 if (sr & AC97C_SR_COEVT) {
719 dev_info(&chip->pdev->dev, "codec channel event%s%s%s%s%s\n",
720 cosr & AC97C_CSR_OVRUN ? " OVRUN" : "",
721 cosr & AC97C_CSR_RXRDY ? " RXRDY" : "",
722 cosr & AC97C_CSR_TXEMPTY ? " TXEMPTY" : "",
723 cosr & AC97C_CSR_TXRDY ? " TXRDY" : "",
724 !cosr ? " NONE" : "");
725 retval = IRQ_HANDLED;
728 if (retval == IRQ_NONE) {
729 dev_err(&chip->pdev->dev, "spurious interrupt sr 0x%08x "
730 "casr 0x%08x cosr 0x%08x\n", sr, casr, cosr);
736 static struct ac97_pcm at91_ac97_pcm_defs[] __devinitdata = {
741 .slots = ((1 << AC97_SLOT_PCM_LEFT)
742 | (1 << AC97_SLOT_PCM_RIGHT)),
750 .slots = ((1 << AC97_SLOT_PCM_LEFT)
751 | (1 << AC97_SLOT_PCM_RIGHT)),
759 .slots = (1<<AC97_SLOT_MIC),
764 static int __devinit atmel_ac97c_pcm_new(struct atmel_ac97c *chip)
767 struct snd_pcm_hardware hw = atmel_ac97c_hw;
768 int capture, playback, retval, err;
770 capture = test_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
771 playback = test_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
773 if (!cpu_is_at32ap7000()) {
774 err = snd_ac97_pcm_assign(chip->ac97_bus,
775 ARRAY_SIZE(at91_ac97_pcm_defs),
780 retval = snd_pcm_new(chip->card, chip->card->shortname,
781 chip->pdev->id, playback, capture, &pcm);
786 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
787 &atmel_ac97_capture_ops);
789 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
790 &atmel_ac97_playback_ops);
792 retval = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
793 &chip->pdev->dev, hw.periods_min * hw.period_bytes_min,
794 hw.buffer_bytes_max);
798 pcm->private_data = chip;
800 strcpy(pcm->name, chip->card->shortname);
806 static int atmel_ac97c_mixer_new(struct atmel_ac97c *chip)
808 struct snd_ac97_template template;
809 memset(&template, 0, sizeof(template));
810 template.private_data = chip;
811 return snd_ac97_mixer(chip->ac97_bus, &template, &chip->ac97);
814 static void atmel_ac97c_write(struct snd_ac97 *ac97, unsigned short reg,
817 struct atmel_ac97c *chip = get_chip(ac97);
821 word = (reg & 0x7f) << 16 | val;
824 if (ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) {
825 ac97c_writel(chip, COTHR, word);
831 dev_dbg(&chip->pdev->dev, "codec write timeout\n");
834 static unsigned short atmel_ac97c_read(struct snd_ac97 *ac97,
837 struct atmel_ac97c *chip = get_chip(ac97);
842 word = (0x80 | (reg & 0x7f)) << 16;
844 if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0)
845 ac97c_readl(chip, CORHR);
851 if ((ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) != 0) {
852 ac97c_writel(chip, COTHR, word);
864 if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0) {
865 unsigned short val = ac97c_readl(chip, CORHR);
876 dev_dbg(&chip->pdev->dev, "codec read timeout\n");
880 static bool filter(struct dma_chan *chan, void *slave)
882 struct dw_dma_slave *dws = slave;
884 if (dws->dma_dev == chan->device->dev) {
891 static void atmel_ac97c_reset(struct atmel_ac97c *chip)
893 ac97c_writel(chip, MR, 0);
894 ac97c_writel(chip, MR, AC97C_MR_ENA);
895 ac97c_writel(chip, CAMR, 0);
896 ac97c_writel(chip, COMR, 0);
898 if (gpio_is_valid(chip->reset_pin)) {
899 gpio_set_value(chip->reset_pin, 0);
900 /* AC97 v2.2 specifications says minimum 1 us. */
902 gpio_set_value(chip->reset_pin, 1);
904 ac97c_writel(chip, MR, AC97C_MR_WRST | AC97C_MR_ENA);
906 ac97c_writel(chip, MR, AC97C_MR_ENA);
910 static int __devinit atmel_ac97c_probe(struct platform_device *pdev)
912 struct snd_card *card;
913 struct atmel_ac97c *chip;
914 struct resource *regs;
915 struct ac97c_platform_data *pdata;
917 static struct snd_ac97_bus_ops ops = {
918 .write = atmel_ac97c_write,
919 .read = atmel_ac97c_read,
924 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
926 dev_dbg(&pdev->dev, "no memory resource\n");
930 pdata = pdev->dev.platform_data;
932 dev_dbg(&pdev->dev, "no platform data\n");
936 irq = platform_get_irq(pdev, 0);
938 dev_dbg(&pdev->dev, "could not get irq\n");
942 if (cpu_is_at32ap7000()) {
943 pclk = clk_get(&pdev->dev, "pclk");
945 pclk = clk_get(&pdev->dev, "ac97_clk");
949 dev_dbg(&pdev->dev, "no peripheral clock\n");
950 return PTR_ERR(pclk);
954 retval = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
955 THIS_MODULE, sizeof(struct atmel_ac97c), &card);
957 dev_dbg(&pdev->dev, "could not create sound card device\n");
958 goto err_snd_card_new;
961 chip = get_chip(card);
963 retval = request_irq(irq, atmel_ac97c_interrupt, 0, "AC97C", chip);
965 dev_dbg(&pdev->dev, "unable to request irq %d\n", irq);
966 goto err_request_irq;
970 spin_lock_init(&chip->lock);
972 strcpy(card->driver, "Atmel AC97C");
973 strcpy(card->shortname, "Atmel AC97C");
974 sprintf(card->longname, "Atmel AC97 controller");
979 chip->regs = ioremap(regs->start, resource_size(regs));
982 dev_dbg(&pdev->dev, "could not remap register memory\n");
986 if (gpio_is_valid(pdata->reset_pin)) {
987 if (gpio_request(pdata->reset_pin, "reset_pin")) {
988 dev_dbg(&pdev->dev, "reset pin not available\n");
989 chip->reset_pin = -ENODEV;
991 gpio_direction_output(pdata->reset_pin, 1);
992 chip->reset_pin = pdata->reset_pin;
995 chip->reset_pin = -EINVAL;
998 snd_card_set_dev(card, &pdev->dev);
1000 atmel_ac97c_reset(chip);
1002 /* Enable overrun interrupt from codec channel */
1003 ac97c_writel(chip, COMR, AC97C_CSR_OVRUN);
1004 ac97c_writel(chip, IER, ac97c_readl(chip, IMR) | AC97C_SR_COEVT);
1006 retval = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus);
1008 dev_dbg(&pdev->dev, "could not register on ac97 bus\n");
1012 retval = atmel_ac97c_mixer_new(chip);
1014 dev_dbg(&pdev->dev, "could not register ac97 mixer\n");
1018 if (cpu_is_at32ap7000()) {
1019 if (pdata->rx_dws.dma_dev) {
1020 dma_cap_mask_t mask;
1023 dma_cap_set(DMA_SLAVE, mask);
1025 chip->dma.rx_chan = dma_request_channel(mask, filter,
1027 if (chip->dma.rx_chan) {
1028 struct dma_slave_config dma_conf = {
1029 .src_addr = regs->start + AC97C_CARHR +
1032 DMA_SLAVE_BUSWIDTH_2_BYTES,
1035 .direction = DMA_DEV_TO_MEM,
1039 dmaengine_slave_config(chip->dma.rx_chan,
1043 dev_info(&chip->pdev->dev, "using %s for DMA RX\n",
1044 dev_name(&chip->dma.rx_chan->dev->device));
1045 set_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
1048 if (pdata->tx_dws.dma_dev) {
1049 dma_cap_mask_t mask;
1052 dma_cap_set(DMA_SLAVE, mask);
1054 chip->dma.tx_chan = dma_request_channel(mask, filter,
1056 if (chip->dma.tx_chan) {
1057 struct dma_slave_config dma_conf = {
1058 .dst_addr = regs->start + AC97C_CATHR +
1061 DMA_SLAVE_BUSWIDTH_2_BYTES,
1064 .direction = DMA_MEM_TO_DEV,
1068 dmaengine_slave_config(chip->dma.tx_chan,
1072 dev_info(&chip->pdev->dev, "using %s for DMA TX\n",
1073 dev_name(&chip->dma.tx_chan->dev->device));
1074 set_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
1077 if (!test_bit(DMA_RX_CHAN_PRESENT, &chip->flags) &&
1078 !test_bit(DMA_TX_CHAN_PRESENT, &chip->flags)) {
1079 dev_dbg(&pdev->dev, "DMA not available\n");
1084 /* Just pretend that we have DMA channel(for at91 i is actually
1086 set_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
1087 set_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
1090 retval = atmel_ac97c_pcm_new(chip);
1092 dev_dbg(&pdev->dev, "could not register ac97 pcm device\n");
1096 retval = snd_card_register(card);
1098 dev_dbg(&pdev->dev, "could not register sound card\n");
1102 platform_set_drvdata(pdev, card);
1104 dev_info(&pdev->dev, "Atmel AC97 controller at 0x%p, irq = %d\n",
1110 if (cpu_is_at32ap7000()) {
1111 if (test_bit(DMA_RX_CHAN_PRESENT, &chip->flags))
1112 dma_release_channel(chip->dma.rx_chan);
1113 if (test_bit(DMA_TX_CHAN_PRESENT, &chip->flags))
1114 dma_release_channel(chip->dma.tx_chan);
1115 clear_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
1116 clear_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
1117 chip->dma.rx_chan = NULL;
1118 chip->dma.tx_chan = NULL;
1121 snd_card_set_dev(card, NULL);
1123 if (gpio_is_valid(chip->reset_pin))
1124 gpio_free(chip->reset_pin);
1126 iounmap(chip->regs);
1128 free_irq(irq, chip);
1130 snd_card_free(card);
1138 static int atmel_ac97c_suspend(struct platform_device *pdev, pm_message_t msg)
1140 struct snd_card *card = platform_get_drvdata(pdev);
1141 struct atmel_ac97c *chip = card->private_data;
1143 if (cpu_is_at32ap7000()) {
1144 if (test_bit(DMA_RX_READY, &chip->flags))
1145 dw_dma_cyclic_stop(chip->dma.rx_chan);
1146 if (test_bit(DMA_TX_READY, &chip->flags))
1147 dw_dma_cyclic_stop(chip->dma.tx_chan);
1149 clk_disable(chip->pclk);
1154 static int atmel_ac97c_resume(struct platform_device *pdev)
1156 struct snd_card *card = platform_get_drvdata(pdev);
1157 struct atmel_ac97c *chip = card->private_data;
1159 clk_enable(chip->pclk);
1160 if (cpu_is_at32ap7000()) {
1161 if (test_bit(DMA_RX_READY, &chip->flags))
1162 dw_dma_cyclic_start(chip->dma.rx_chan);
1163 if (test_bit(DMA_TX_READY, &chip->flags))
1164 dw_dma_cyclic_start(chip->dma.tx_chan);
1169 #define atmel_ac97c_suspend NULL
1170 #define atmel_ac97c_resume NULL
1173 static int __devexit atmel_ac97c_remove(struct platform_device *pdev)
1175 struct snd_card *card = platform_get_drvdata(pdev);
1176 struct atmel_ac97c *chip = get_chip(card);
1178 if (gpio_is_valid(chip->reset_pin))
1179 gpio_free(chip->reset_pin);
1181 ac97c_writel(chip, CAMR, 0);
1182 ac97c_writel(chip, COMR, 0);
1183 ac97c_writel(chip, MR, 0);
1185 clk_disable(chip->pclk);
1186 clk_put(chip->pclk);
1187 iounmap(chip->regs);
1188 free_irq(chip->irq, chip);
1190 if (cpu_is_at32ap7000()) {
1191 if (test_bit(DMA_RX_CHAN_PRESENT, &chip->flags))
1192 dma_release_channel(chip->dma.rx_chan);
1193 if (test_bit(DMA_TX_CHAN_PRESENT, &chip->flags))
1194 dma_release_channel(chip->dma.tx_chan);
1195 clear_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
1196 clear_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
1197 chip->dma.rx_chan = NULL;
1198 chip->dma.tx_chan = NULL;
1201 snd_card_set_dev(card, NULL);
1202 snd_card_free(card);
1204 platform_set_drvdata(pdev, NULL);
1209 static struct platform_driver atmel_ac97c_driver = {
1210 .remove = __devexit_p(atmel_ac97c_remove),
1212 .name = "atmel_ac97c",
1214 .suspend = atmel_ac97c_suspend,
1215 .resume = atmel_ac97c_resume,
1218 static int __init atmel_ac97c_init(void)
1220 return platform_driver_probe(&atmel_ac97c_driver,
1223 module_init(atmel_ac97c_init);
1225 static void __exit atmel_ac97c_exit(void)
1227 platform_driver_unregister(&atmel_ac97c_driver);
1229 module_exit(atmel_ac97c_exit);
1231 MODULE_LICENSE("GPL");
1232 MODULE_DESCRIPTION("Driver for Atmel AC97 controller");
1233 MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");