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 <linux/of_gpio.h>
27 #include <linux/of_device.h>
29 #include <sound/core.h>
30 #include <sound/initval.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/ac97_codec.h>
34 #include <sound/atmel-ac97c.h>
35 #include <sound/memalloc.h>
37 #include <linux/platform_data/dma-dw.h>
38 #include <linux/dma/dw.h>
43 #define cpu_is_at32ap7000() 0
55 /* Serialize access to opened variable */
56 static DEFINE_MUTEX(opened_mutex);
58 struct atmel_ac97c_dma {
59 struct dma_chan *rx_chan;
60 struct dma_chan *tx_chan;
65 struct platform_device *pdev;
66 struct atmel_ac97c_dma dma;
68 struct snd_pcm_substream *playback_substream;
69 struct snd_pcm_substream *capture_substream;
70 struct snd_card *card;
72 struct snd_ac97 *ac97;
73 struct snd_ac97_bus *ac97_bus;
76 unsigned int cur_rate;
78 int playback_period, capture_period;
79 /* Serialize access to opened variable */
87 #define get_chip(card) ((struct atmel_ac97c *)(card)->private_data)
89 #define ac97c_writel(chip, reg, val) \
90 __raw_writel((val), (chip)->regs + AC97C_##reg)
91 #define ac97c_readl(chip, reg) \
92 __raw_readl((chip)->regs + AC97C_##reg)
94 /* This function is called by the DMA driver. */
95 static void atmel_ac97c_dma_playback_period_done(void *arg)
97 struct atmel_ac97c *chip = arg;
98 snd_pcm_period_elapsed(chip->playback_substream);
101 static void atmel_ac97c_dma_capture_period_done(void *arg)
103 struct atmel_ac97c *chip = arg;
104 snd_pcm_period_elapsed(chip->capture_substream);
107 static int atmel_ac97c_prepare_dma(struct atmel_ac97c *chip,
108 struct snd_pcm_substream *substream,
109 enum dma_transfer_direction direction)
111 struct dma_chan *chan;
112 struct dw_cyclic_desc *cdesc;
113 struct snd_pcm_runtime *runtime = substream->runtime;
114 unsigned long buffer_len, period_len;
117 * We don't do DMA on "complex" transfers, i.e. with
118 * non-halfword-aligned buffers or lengths.
120 if (runtime->dma_addr & 1 || runtime->buffer_size & 1) {
121 dev_dbg(&chip->pdev->dev, "too complex transfer\n");
125 if (direction == DMA_MEM_TO_DEV)
126 chan = chip->dma.tx_chan;
128 chan = chip->dma.rx_chan;
130 buffer_len = frames_to_bytes(runtime, runtime->buffer_size);
131 period_len = frames_to_bytes(runtime, runtime->period_size);
133 cdesc = dw_dma_cyclic_prep(chan, runtime->dma_addr, buffer_len,
134 period_len, direction);
136 dev_dbg(&chip->pdev->dev, "could not prepare cyclic DMA\n");
137 return PTR_ERR(cdesc);
140 if (direction == DMA_MEM_TO_DEV) {
141 cdesc->period_callback = atmel_ac97c_dma_playback_period_done;
142 set_bit(DMA_TX_READY, &chip->flags);
144 cdesc->period_callback = atmel_ac97c_dma_capture_period_done;
145 set_bit(DMA_RX_READY, &chip->flags);
148 cdesc->period_callback_param = chip;
153 static struct snd_pcm_hardware atmel_ac97c_hw = {
154 .info = (SNDRV_PCM_INFO_MMAP
155 | SNDRV_PCM_INFO_MMAP_VALID
156 | SNDRV_PCM_INFO_INTERLEAVED
157 | SNDRV_PCM_INFO_BLOCK_TRANSFER
158 | SNDRV_PCM_INFO_JOINT_DUPLEX
159 | SNDRV_PCM_INFO_RESUME
160 | SNDRV_PCM_INFO_PAUSE),
161 .formats = (SNDRV_PCM_FMTBIT_S16_BE
162 | SNDRV_PCM_FMTBIT_S16_LE),
163 .rates = (SNDRV_PCM_RATE_CONTINUOUS),
168 .buffer_bytes_max = 2 * 2 * 64 * 2048,
169 .period_bytes_min = 4096,
170 .period_bytes_max = 4096,
175 static int atmel_ac97c_playback_open(struct snd_pcm_substream *substream)
177 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
178 struct snd_pcm_runtime *runtime = substream->runtime;
180 mutex_lock(&opened_mutex);
182 runtime->hw = atmel_ac97c_hw;
183 if (chip->cur_rate) {
184 runtime->hw.rate_min = chip->cur_rate;
185 runtime->hw.rate_max = chip->cur_rate;
187 if (chip->cur_format)
188 runtime->hw.formats = pcm_format_to_bits(chip->cur_format);
189 mutex_unlock(&opened_mutex);
190 chip->playback_substream = substream;
194 static int atmel_ac97c_capture_open(struct snd_pcm_substream *substream)
196 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
197 struct snd_pcm_runtime *runtime = substream->runtime;
199 mutex_lock(&opened_mutex);
201 runtime->hw = atmel_ac97c_hw;
202 if (chip->cur_rate) {
203 runtime->hw.rate_min = chip->cur_rate;
204 runtime->hw.rate_max = chip->cur_rate;
206 if (chip->cur_format)
207 runtime->hw.formats = pcm_format_to_bits(chip->cur_format);
208 mutex_unlock(&opened_mutex);
209 chip->capture_substream = substream;
213 static int atmel_ac97c_playback_close(struct snd_pcm_substream *substream)
215 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
217 mutex_lock(&opened_mutex);
221 chip->cur_format = 0;
223 mutex_unlock(&opened_mutex);
225 chip->playback_substream = NULL;
230 static int atmel_ac97c_capture_close(struct snd_pcm_substream *substream)
232 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
234 mutex_lock(&opened_mutex);
238 chip->cur_format = 0;
240 mutex_unlock(&opened_mutex);
242 chip->capture_substream = NULL;
247 static int atmel_ac97c_playback_hw_params(struct snd_pcm_substream *substream,
248 struct snd_pcm_hw_params *hw_params)
250 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
253 retval = snd_pcm_lib_malloc_pages(substream,
254 params_buffer_bytes(hw_params));
257 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
258 if (cpu_is_at32ap7000()) {
259 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
261 if (test_and_clear_bit(DMA_TX_READY, &chip->flags))
262 dw_dma_cyclic_free(chip->dma.tx_chan);
264 /* Set restrictions to params. */
265 mutex_lock(&opened_mutex);
266 chip->cur_rate = params_rate(hw_params);
267 chip->cur_format = params_format(hw_params);
268 mutex_unlock(&opened_mutex);
273 static int atmel_ac97c_capture_hw_params(struct snd_pcm_substream *substream,
274 struct snd_pcm_hw_params *hw_params)
276 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
279 retval = snd_pcm_lib_malloc_pages(substream,
280 params_buffer_bytes(hw_params));
283 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
284 if (cpu_is_at32ap7000() && retval == 1)
285 if (test_and_clear_bit(DMA_RX_READY, &chip->flags))
286 dw_dma_cyclic_free(chip->dma.rx_chan);
288 /* Set restrictions to params. */
289 mutex_lock(&opened_mutex);
290 chip->cur_rate = params_rate(hw_params);
291 chip->cur_format = params_format(hw_params);
292 mutex_unlock(&opened_mutex);
297 static int atmel_ac97c_playback_hw_free(struct snd_pcm_substream *substream)
299 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
300 if (cpu_is_at32ap7000()) {
301 if (test_and_clear_bit(DMA_TX_READY, &chip->flags))
302 dw_dma_cyclic_free(chip->dma.tx_chan);
304 return snd_pcm_lib_free_pages(substream);
307 static int atmel_ac97c_capture_hw_free(struct snd_pcm_substream *substream)
309 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
310 if (cpu_is_at32ap7000()) {
311 if (test_and_clear_bit(DMA_RX_READY, &chip->flags))
312 dw_dma_cyclic_free(chip->dma.rx_chan);
314 return snd_pcm_lib_free_pages(substream);
317 static int atmel_ac97c_playback_prepare(struct snd_pcm_substream *substream)
319 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
320 struct snd_pcm_runtime *runtime = substream->runtime;
321 int block_size = frames_to_bytes(runtime, runtime->period_size);
322 unsigned long word = ac97c_readl(chip, OCA);
325 chip->playback_period = 0;
326 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
328 /* assign channels to AC97C channel A */
329 switch (runtime->channels) {
331 word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
334 word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
335 | AC97C_CH_ASSIGN(PCM_RIGHT, A);
338 /* TODO: support more than two channels */
341 ac97c_writel(chip, OCA, word);
343 /* configure sample format and size */
344 word = ac97c_readl(chip, CAMR);
345 if (chip->opened <= 1)
346 word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
348 word |= AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
350 switch (runtime->format) {
351 case SNDRV_PCM_FORMAT_S16_LE:
352 if (cpu_is_at32ap7000())
353 word |= AC97C_CMR_CEM_LITTLE;
355 case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
356 word &= ~(AC97C_CMR_CEM_LITTLE);
359 word = ac97c_readl(chip, OCA);
360 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
361 ac97c_writel(chip, OCA, word);
365 /* Enable underrun interrupt on channel A */
366 word |= AC97C_CSR_UNRUN;
368 ac97c_writel(chip, CAMR, word);
370 /* Enable channel A event interrupt */
371 word = ac97c_readl(chip, IMR);
372 word |= AC97C_SR_CAEVT;
373 ac97c_writel(chip, IER, word);
375 /* set variable rate if needed */
376 if (runtime->rate != 48000) {
377 word = ac97c_readl(chip, MR);
378 word |= AC97C_MR_VRA;
379 ac97c_writel(chip, MR, word);
381 word = ac97c_readl(chip, MR);
382 word &= ~(AC97C_MR_VRA);
383 ac97c_writel(chip, MR, word);
386 retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
389 dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
392 if (cpu_is_at32ap7000()) {
393 if (!test_bit(DMA_TX_READY, &chip->flags))
394 retval = atmel_ac97c_prepare_dma(chip, substream,
397 /* Initialize and start the PDC */
398 writel(runtime->dma_addr, chip->regs + ATMEL_PDC_TPR);
399 writel(block_size / 2, chip->regs + ATMEL_PDC_TCR);
400 writel(runtime->dma_addr + block_size,
401 chip->regs + ATMEL_PDC_TNPR);
402 writel(block_size / 2, chip->regs + ATMEL_PDC_TNCR);
408 static int atmel_ac97c_capture_prepare(struct snd_pcm_substream *substream)
410 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
411 struct snd_pcm_runtime *runtime = substream->runtime;
412 int block_size = frames_to_bytes(runtime, runtime->period_size);
413 unsigned long word = ac97c_readl(chip, ICA);
416 chip->capture_period = 0;
417 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
419 /* assign channels to AC97C channel A */
420 switch (runtime->channels) {
422 word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
425 word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
426 | AC97C_CH_ASSIGN(PCM_RIGHT, A);
429 /* TODO: support more than two channels */
432 ac97c_writel(chip, ICA, word);
434 /* configure sample format and size */
435 word = ac97c_readl(chip, CAMR);
436 if (chip->opened <= 1)
437 word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
439 word |= AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
441 switch (runtime->format) {
442 case SNDRV_PCM_FORMAT_S16_LE:
443 if (cpu_is_at32ap7000())
444 word |= AC97C_CMR_CEM_LITTLE;
446 case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
447 word &= ~(AC97C_CMR_CEM_LITTLE);
450 word = ac97c_readl(chip, ICA);
451 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
452 ac97c_writel(chip, ICA, word);
456 /* Enable overrun interrupt on channel A */
457 word |= AC97C_CSR_OVRUN;
459 ac97c_writel(chip, CAMR, word);
461 /* Enable channel A event interrupt */
462 word = ac97c_readl(chip, IMR);
463 word |= AC97C_SR_CAEVT;
464 ac97c_writel(chip, IER, word);
466 /* set variable rate if needed */
467 if (runtime->rate != 48000) {
468 word = ac97c_readl(chip, MR);
469 word |= AC97C_MR_VRA;
470 ac97c_writel(chip, MR, word);
472 word = ac97c_readl(chip, MR);
473 word &= ~(AC97C_MR_VRA);
474 ac97c_writel(chip, MR, word);
477 retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE,
480 dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
483 if (cpu_is_at32ap7000()) {
484 if (!test_bit(DMA_RX_READY, &chip->flags))
485 retval = atmel_ac97c_prepare_dma(chip, substream,
488 /* Initialize and start the PDC */
489 writel(runtime->dma_addr, chip->regs + ATMEL_PDC_RPR);
490 writel(block_size / 2, chip->regs + ATMEL_PDC_RCR);
491 writel(runtime->dma_addr + block_size,
492 chip->regs + ATMEL_PDC_RNPR);
493 writel(block_size / 2, chip->regs + ATMEL_PDC_RNCR);
500 atmel_ac97c_playback_trigger(struct snd_pcm_substream *substream, int cmd)
502 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
503 unsigned long camr, ptcr = 0;
506 camr = ac97c_readl(chip, CAMR);
509 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
510 case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
511 case SNDRV_PCM_TRIGGER_START:
512 if (cpu_is_at32ap7000()) {
513 retval = dw_dma_cyclic_start(chip->dma.tx_chan);
517 ptcr = ATMEL_PDC_TXTEN;
519 camr |= AC97C_CMR_CENA | AC97C_CSR_ENDTX;
521 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
522 case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
523 case SNDRV_PCM_TRIGGER_STOP:
524 if (cpu_is_at32ap7000())
525 dw_dma_cyclic_stop(chip->dma.tx_chan);
527 ptcr |= ATMEL_PDC_TXTDIS;
528 if (chip->opened <= 1)
529 camr &= ~AC97C_CMR_CENA;
536 ac97c_writel(chip, CAMR, camr);
537 if (!cpu_is_at32ap7000())
538 writel(ptcr, chip->regs + ATMEL_PDC_PTCR);
544 atmel_ac97c_capture_trigger(struct snd_pcm_substream *substream, int cmd)
546 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
547 unsigned long camr, ptcr = 0;
550 camr = ac97c_readl(chip, CAMR);
551 ptcr = readl(chip->regs + ATMEL_PDC_PTSR);
554 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
555 case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
556 case SNDRV_PCM_TRIGGER_START:
557 if (cpu_is_at32ap7000()) {
558 retval = dw_dma_cyclic_start(chip->dma.rx_chan);
562 ptcr = ATMEL_PDC_RXTEN;
564 camr |= AC97C_CMR_CENA | AC97C_CSR_ENDRX;
566 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
567 case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
568 case SNDRV_PCM_TRIGGER_STOP:
569 if (cpu_is_at32ap7000())
570 dw_dma_cyclic_stop(chip->dma.rx_chan);
572 ptcr |= (ATMEL_PDC_RXTDIS);
573 if (chip->opened <= 1)
574 camr &= ~AC97C_CMR_CENA;
581 ac97c_writel(chip, CAMR, camr);
582 if (!cpu_is_at32ap7000())
583 writel(ptcr, chip->regs + ATMEL_PDC_PTCR);
588 static snd_pcm_uframes_t
589 atmel_ac97c_playback_pointer(struct snd_pcm_substream *substream)
591 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
592 struct snd_pcm_runtime *runtime = substream->runtime;
593 snd_pcm_uframes_t frames;
596 if (cpu_is_at32ap7000())
597 bytes = dw_dma_get_src_addr(chip->dma.tx_chan);
599 bytes = readl(chip->regs + ATMEL_PDC_TPR);
600 bytes -= runtime->dma_addr;
602 frames = bytes_to_frames(runtime, bytes);
603 if (frames >= runtime->buffer_size)
604 frames -= runtime->buffer_size;
608 static snd_pcm_uframes_t
609 atmel_ac97c_capture_pointer(struct snd_pcm_substream *substream)
611 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
612 struct snd_pcm_runtime *runtime = substream->runtime;
613 snd_pcm_uframes_t frames;
616 if (cpu_is_at32ap7000())
617 bytes = dw_dma_get_dst_addr(chip->dma.rx_chan);
619 bytes = readl(chip->regs + ATMEL_PDC_RPR);
620 bytes -= runtime->dma_addr;
622 frames = bytes_to_frames(runtime, bytes);
623 if (frames >= runtime->buffer_size)
624 frames -= runtime->buffer_size;
628 static struct snd_pcm_ops atmel_ac97_playback_ops = {
629 .open = atmel_ac97c_playback_open,
630 .close = atmel_ac97c_playback_close,
631 .ioctl = snd_pcm_lib_ioctl,
632 .hw_params = atmel_ac97c_playback_hw_params,
633 .hw_free = atmel_ac97c_playback_hw_free,
634 .prepare = atmel_ac97c_playback_prepare,
635 .trigger = atmel_ac97c_playback_trigger,
636 .pointer = atmel_ac97c_playback_pointer,
639 static struct snd_pcm_ops atmel_ac97_capture_ops = {
640 .open = atmel_ac97c_capture_open,
641 .close = atmel_ac97c_capture_close,
642 .ioctl = snd_pcm_lib_ioctl,
643 .hw_params = atmel_ac97c_capture_hw_params,
644 .hw_free = atmel_ac97c_capture_hw_free,
645 .prepare = atmel_ac97c_capture_prepare,
646 .trigger = atmel_ac97c_capture_trigger,
647 .pointer = atmel_ac97c_capture_pointer,
650 static irqreturn_t atmel_ac97c_interrupt(int irq, void *dev)
652 struct atmel_ac97c *chip = (struct atmel_ac97c *)dev;
653 irqreturn_t retval = IRQ_NONE;
654 u32 sr = ac97c_readl(chip, SR);
655 u32 casr = ac97c_readl(chip, CASR);
656 u32 cosr = ac97c_readl(chip, COSR);
657 u32 camr = ac97c_readl(chip, CAMR);
659 if (sr & AC97C_SR_CAEVT) {
660 struct snd_pcm_runtime *runtime;
661 int offset, next_period, block_size;
662 dev_dbg(&chip->pdev->dev, "channel A event%s%s%s%s%s%s\n",
663 casr & AC97C_CSR_OVRUN ? " OVRUN" : "",
664 casr & AC97C_CSR_RXRDY ? " RXRDY" : "",
665 casr & AC97C_CSR_UNRUN ? " UNRUN" : "",
666 casr & AC97C_CSR_TXEMPTY ? " TXEMPTY" : "",
667 casr & AC97C_CSR_TXRDY ? " TXRDY" : "",
668 !casr ? " NONE" : "");
669 if (!cpu_is_at32ap7000()) {
670 if ((casr & camr) & AC97C_CSR_ENDTX) {
671 runtime = chip->playback_substream->runtime;
672 block_size = frames_to_bytes(runtime,
673 runtime->period_size);
674 chip->playback_period++;
676 if (chip->playback_period == runtime->periods)
677 chip->playback_period = 0;
678 next_period = chip->playback_period + 1;
679 if (next_period == runtime->periods)
682 offset = block_size * next_period;
684 writel(runtime->dma_addr + offset,
685 chip->regs + ATMEL_PDC_TNPR);
686 writel(block_size / 2,
687 chip->regs + ATMEL_PDC_TNCR);
689 snd_pcm_period_elapsed(
690 chip->playback_substream);
692 if ((casr & camr) & AC97C_CSR_ENDRX) {
693 runtime = chip->capture_substream->runtime;
694 block_size = frames_to_bytes(runtime,
695 runtime->period_size);
696 chip->capture_period++;
698 if (chip->capture_period == runtime->periods)
699 chip->capture_period = 0;
700 next_period = chip->capture_period + 1;
701 if (next_period == runtime->periods)
704 offset = block_size * next_period;
706 writel(runtime->dma_addr + offset,
707 chip->regs + ATMEL_PDC_RNPR);
708 writel(block_size / 2,
709 chip->regs + ATMEL_PDC_RNCR);
710 snd_pcm_period_elapsed(chip->capture_substream);
713 retval = IRQ_HANDLED;
716 if (sr & AC97C_SR_COEVT) {
717 dev_info(&chip->pdev->dev, "codec channel event%s%s%s%s%s\n",
718 cosr & AC97C_CSR_OVRUN ? " OVRUN" : "",
719 cosr & AC97C_CSR_RXRDY ? " RXRDY" : "",
720 cosr & AC97C_CSR_TXEMPTY ? " TXEMPTY" : "",
721 cosr & AC97C_CSR_TXRDY ? " TXRDY" : "",
722 !cosr ? " NONE" : "");
723 retval = IRQ_HANDLED;
726 if (retval == IRQ_NONE) {
727 dev_err(&chip->pdev->dev, "spurious interrupt sr 0x%08x "
728 "casr 0x%08x cosr 0x%08x\n", sr, casr, cosr);
734 static struct ac97_pcm at91_ac97_pcm_defs[] = {
739 .slots = ((1 << AC97_SLOT_PCM_LEFT)
740 | (1 << AC97_SLOT_PCM_RIGHT)),
748 .slots = ((1 << AC97_SLOT_PCM_LEFT)
749 | (1 << AC97_SLOT_PCM_RIGHT)),
757 .slots = (1<<AC97_SLOT_MIC),
762 static int atmel_ac97c_pcm_new(struct atmel_ac97c *chip)
765 struct snd_pcm_hardware hw = atmel_ac97c_hw;
766 int capture, playback, retval, err;
768 capture = test_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
769 playback = test_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
771 if (!cpu_is_at32ap7000()) {
772 err = snd_ac97_pcm_assign(chip->ac97_bus,
773 ARRAY_SIZE(at91_ac97_pcm_defs),
778 retval = snd_pcm_new(chip->card, chip->card->shortname,
779 0, playback, capture, &pcm);
784 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
785 &atmel_ac97_capture_ops);
787 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
788 &atmel_ac97_playback_ops);
790 retval = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
791 &chip->pdev->dev, hw.periods_min * hw.period_bytes_min,
792 hw.buffer_bytes_max);
796 pcm->private_data = chip;
798 strcpy(pcm->name, chip->card->shortname);
804 static int atmel_ac97c_mixer_new(struct atmel_ac97c *chip)
806 struct snd_ac97_template template;
807 memset(&template, 0, sizeof(template));
808 template.private_data = chip;
809 return snd_ac97_mixer(chip->ac97_bus, &template, &chip->ac97);
812 static void atmel_ac97c_write(struct snd_ac97 *ac97, unsigned short reg,
815 struct atmel_ac97c *chip = get_chip(ac97);
819 word = (reg & 0x7f) << 16 | val;
822 if (ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) {
823 ac97c_writel(chip, COTHR, word);
829 dev_dbg(&chip->pdev->dev, "codec write timeout\n");
832 static unsigned short atmel_ac97c_read(struct snd_ac97 *ac97,
835 struct atmel_ac97c *chip = get_chip(ac97);
840 word = (0x80 | (reg & 0x7f)) << 16;
842 if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0)
843 ac97c_readl(chip, CORHR);
849 if ((ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) != 0) {
850 ac97c_writel(chip, COTHR, word);
862 if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0) {
863 unsigned short val = ac97c_readl(chip, CORHR);
874 dev_dbg(&chip->pdev->dev, "codec read timeout\n");
878 static bool filter(struct dma_chan *chan, void *slave)
880 struct dw_dma_slave *dws = slave;
882 if (dws->dma_dev == chan->device->dev) {
889 static void atmel_ac97c_reset(struct atmel_ac97c *chip)
891 ac97c_writel(chip, MR, 0);
892 ac97c_writel(chip, MR, AC97C_MR_ENA);
893 ac97c_writel(chip, CAMR, 0);
894 ac97c_writel(chip, COMR, 0);
896 if (gpio_is_valid(chip->reset_pin)) {
897 gpio_set_value(chip->reset_pin, 0);
898 /* AC97 v2.2 specifications says minimum 1 us. */
900 gpio_set_value(chip->reset_pin, 1);
902 ac97c_writel(chip, MR, AC97C_MR_WRST | AC97C_MR_ENA);
904 ac97c_writel(chip, MR, AC97C_MR_ENA);
909 static const struct of_device_id atmel_ac97c_dt_ids[] = {
910 { .compatible = "atmel,at91sam9263-ac97c", },
913 MODULE_DEVICE_TABLE(of, atmel_ac97c_dt_ids);
915 static struct ac97c_platform_data *atmel_ac97c_probe_dt(struct device *dev)
917 struct ac97c_platform_data *pdata;
918 struct device_node *node = dev->of_node;
921 dev_err(dev, "Device does not have associated DT data\n");
922 return ERR_PTR(-EINVAL);
925 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
927 return ERR_PTR(-ENOMEM);
929 pdata->reset_pin = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
934 static struct ac97c_platform_data *atmel_ac97c_probe_dt(struct device *dev)
936 dev_err(dev, "no platform data defined\n");
937 return ERR_PTR(-ENXIO);
941 static int atmel_ac97c_probe(struct platform_device *pdev)
943 struct snd_card *card;
944 struct atmel_ac97c *chip;
945 struct resource *regs;
946 struct ac97c_platform_data *pdata;
948 static struct snd_ac97_bus_ops ops = {
949 .write = atmel_ac97c_write,
950 .read = atmel_ac97c_read,
955 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
957 dev_dbg(&pdev->dev, "no memory resource\n");
961 pdata = dev_get_platdata(&pdev->dev);
963 pdata = atmel_ac97c_probe_dt(&pdev->dev);
965 return PTR_ERR(pdata);
968 irq = platform_get_irq(pdev, 0);
970 dev_dbg(&pdev->dev, "could not get irq\n");
974 if (cpu_is_at32ap7000()) {
975 pclk = clk_get(&pdev->dev, "pclk");
977 pclk = clk_get(&pdev->dev, "ac97_clk");
981 dev_dbg(&pdev->dev, "no peripheral clock\n");
982 return PTR_ERR(pclk);
984 clk_prepare_enable(pclk);
986 retval = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1,
987 SNDRV_DEFAULT_STR1, THIS_MODULE,
988 sizeof(struct atmel_ac97c), &card);
990 dev_dbg(&pdev->dev, "could not create sound card device\n");
991 goto err_snd_card_new;
994 chip = get_chip(card);
996 retval = request_irq(irq, atmel_ac97c_interrupt, 0, "AC97C", chip);
998 dev_dbg(&pdev->dev, "unable to request irq %d\n", irq);
999 goto err_request_irq;
1003 spin_lock_init(&chip->lock);
1005 strcpy(card->driver, "Atmel AC97C");
1006 strcpy(card->shortname, "Atmel AC97C");
1007 sprintf(card->longname, "Atmel AC97 controller");
1012 chip->regs = ioremap(regs->start, resource_size(regs));
1015 dev_dbg(&pdev->dev, "could not remap register memory\n");
1020 if (gpio_is_valid(pdata->reset_pin)) {
1021 if (gpio_request(pdata->reset_pin, "reset_pin")) {
1022 dev_dbg(&pdev->dev, "reset pin not available\n");
1023 chip->reset_pin = -ENODEV;
1025 gpio_direction_output(pdata->reset_pin, 1);
1026 chip->reset_pin = pdata->reset_pin;
1029 chip->reset_pin = -EINVAL;
1032 atmel_ac97c_reset(chip);
1034 /* Enable overrun interrupt from codec channel */
1035 ac97c_writel(chip, COMR, AC97C_CSR_OVRUN);
1036 ac97c_writel(chip, IER, ac97c_readl(chip, IMR) | AC97C_SR_COEVT);
1038 retval = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus);
1040 dev_dbg(&pdev->dev, "could not register on ac97 bus\n");
1044 retval = atmel_ac97c_mixer_new(chip);
1046 dev_dbg(&pdev->dev, "could not register ac97 mixer\n");
1050 if (cpu_is_at32ap7000()) {
1051 if (pdata->rx_dws.dma_dev) {
1052 dma_cap_mask_t mask;
1055 dma_cap_set(DMA_SLAVE, mask);
1057 chip->dma.rx_chan = dma_request_channel(mask, filter,
1059 if (chip->dma.rx_chan) {
1060 struct dma_slave_config dma_conf = {
1061 .src_addr = regs->start + AC97C_CARHR +
1064 DMA_SLAVE_BUSWIDTH_2_BYTES,
1067 .direction = DMA_DEV_TO_MEM,
1071 dmaengine_slave_config(chip->dma.rx_chan,
1075 dev_info(&chip->pdev->dev, "using %s for DMA RX\n",
1076 dev_name(&chip->dma.rx_chan->dev->device));
1077 set_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
1080 if (pdata->tx_dws.dma_dev) {
1081 dma_cap_mask_t mask;
1084 dma_cap_set(DMA_SLAVE, mask);
1086 chip->dma.tx_chan = dma_request_channel(mask, filter,
1088 if (chip->dma.tx_chan) {
1089 struct dma_slave_config dma_conf = {
1090 .dst_addr = regs->start + AC97C_CATHR +
1093 DMA_SLAVE_BUSWIDTH_2_BYTES,
1096 .direction = DMA_MEM_TO_DEV,
1100 dmaengine_slave_config(chip->dma.tx_chan,
1104 dev_info(&chip->pdev->dev, "using %s for DMA TX\n",
1105 dev_name(&chip->dma.tx_chan->dev->device));
1106 set_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
1109 if (!test_bit(DMA_RX_CHAN_PRESENT, &chip->flags) &&
1110 !test_bit(DMA_TX_CHAN_PRESENT, &chip->flags)) {
1111 dev_dbg(&pdev->dev, "DMA not available\n");
1116 /* Just pretend that we have DMA channel(for at91 i is actually
1118 set_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
1119 set_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
1122 retval = atmel_ac97c_pcm_new(chip);
1124 dev_dbg(&pdev->dev, "could not register ac97 pcm device\n");
1128 retval = snd_card_register(card);
1130 dev_dbg(&pdev->dev, "could not register sound card\n");
1134 platform_set_drvdata(pdev, card);
1136 dev_info(&pdev->dev, "Atmel AC97 controller at 0x%p, irq = %d\n",
1142 if (cpu_is_at32ap7000()) {
1143 if (test_bit(DMA_RX_CHAN_PRESENT, &chip->flags))
1144 dma_release_channel(chip->dma.rx_chan);
1145 if (test_bit(DMA_TX_CHAN_PRESENT, &chip->flags))
1146 dma_release_channel(chip->dma.tx_chan);
1147 clear_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
1148 clear_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
1149 chip->dma.rx_chan = NULL;
1150 chip->dma.tx_chan = NULL;
1153 if (gpio_is_valid(chip->reset_pin))
1154 gpio_free(chip->reset_pin);
1156 iounmap(chip->regs);
1158 free_irq(irq, chip);
1160 snd_card_free(card);
1162 clk_disable_unprepare(pclk);
1167 #ifdef CONFIG_PM_SLEEP
1168 static int atmel_ac97c_suspend(struct device *pdev)
1170 struct snd_card *card = dev_get_drvdata(pdev);
1171 struct atmel_ac97c *chip = card->private_data;
1173 if (cpu_is_at32ap7000()) {
1174 if (test_bit(DMA_RX_READY, &chip->flags))
1175 dw_dma_cyclic_stop(chip->dma.rx_chan);
1176 if (test_bit(DMA_TX_READY, &chip->flags))
1177 dw_dma_cyclic_stop(chip->dma.tx_chan);
1179 clk_disable_unprepare(chip->pclk);
1184 static int atmel_ac97c_resume(struct device *pdev)
1186 struct snd_card *card = dev_get_drvdata(pdev);
1187 struct atmel_ac97c *chip = card->private_data;
1189 clk_prepare_enable(chip->pclk);
1190 if (cpu_is_at32ap7000()) {
1191 if (test_bit(DMA_RX_READY, &chip->flags))
1192 dw_dma_cyclic_start(chip->dma.rx_chan);
1193 if (test_bit(DMA_TX_READY, &chip->flags))
1194 dw_dma_cyclic_start(chip->dma.tx_chan);
1199 static SIMPLE_DEV_PM_OPS(atmel_ac97c_pm, atmel_ac97c_suspend, atmel_ac97c_resume);
1200 #define ATMEL_AC97C_PM_OPS &atmel_ac97c_pm
1202 #define ATMEL_AC97C_PM_OPS NULL
1205 static int atmel_ac97c_remove(struct platform_device *pdev)
1207 struct snd_card *card = platform_get_drvdata(pdev);
1208 struct atmel_ac97c *chip = get_chip(card);
1210 if (gpio_is_valid(chip->reset_pin))
1211 gpio_free(chip->reset_pin);
1213 ac97c_writel(chip, CAMR, 0);
1214 ac97c_writel(chip, COMR, 0);
1215 ac97c_writel(chip, MR, 0);
1217 clk_disable_unprepare(chip->pclk);
1218 clk_put(chip->pclk);
1219 iounmap(chip->regs);
1220 free_irq(chip->irq, chip);
1222 if (cpu_is_at32ap7000()) {
1223 if (test_bit(DMA_RX_CHAN_PRESENT, &chip->flags))
1224 dma_release_channel(chip->dma.rx_chan);
1225 if (test_bit(DMA_TX_CHAN_PRESENT, &chip->flags))
1226 dma_release_channel(chip->dma.tx_chan);
1227 clear_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
1228 clear_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
1229 chip->dma.rx_chan = NULL;
1230 chip->dma.tx_chan = NULL;
1233 snd_card_free(card);
1238 static struct platform_driver atmel_ac97c_driver = {
1239 .probe = atmel_ac97c_probe,
1240 .remove = atmel_ac97c_remove,
1242 .name = "atmel_ac97c",
1243 .pm = ATMEL_AC97C_PM_OPS,
1244 .of_match_table = of_match_ptr(atmel_ac97c_dt_ids),
1247 module_platform_driver(atmel_ac97c_driver);
1249 MODULE_LICENSE("GPL");
1250 MODULE_DESCRIPTION("Driver for Atmel AC97 controller");
1251 MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");