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/platform_data/dma-dw.h>
35 #include <linux/dma/dw.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
= pcm_format_to_bits(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
= pcm_format_to_bits(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() && retval
== 1)
282 if (test_and_clear_bit(DMA_RX_READY
, &chip
->flags
))
283 dw_dma_cyclic_free(chip
->dma
.rx_chan
);
285 /* Set restrictions to params. */
286 mutex_lock(&opened_mutex
);
287 chip
->cur_rate
= params_rate(hw_params
);
288 chip
->cur_format
= params_format(hw_params
);
289 mutex_unlock(&opened_mutex
);
294 static int atmel_ac97c_playback_hw_free(struct snd_pcm_substream
*substream
)
296 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
297 if (cpu_is_at32ap7000()) {
298 if (test_and_clear_bit(DMA_TX_READY
, &chip
->flags
))
299 dw_dma_cyclic_free(chip
->dma
.tx_chan
);
301 return snd_pcm_lib_free_pages(substream
);
304 static int atmel_ac97c_capture_hw_free(struct snd_pcm_substream
*substream
)
306 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
307 if (cpu_is_at32ap7000()) {
308 if (test_and_clear_bit(DMA_RX_READY
, &chip
->flags
))
309 dw_dma_cyclic_free(chip
->dma
.rx_chan
);
311 return snd_pcm_lib_free_pages(substream
);
314 static int atmel_ac97c_playback_prepare(struct snd_pcm_substream
*substream
)
316 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
317 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
318 int block_size
= frames_to_bytes(runtime
, runtime
->period_size
);
319 unsigned long word
= ac97c_readl(chip
, OCA
);
322 chip
->playback_period
= 0;
323 word
&= ~(AC97C_CH_MASK(PCM_LEFT
) | AC97C_CH_MASK(PCM_RIGHT
));
325 /* assign channels to AC97C channel A */
326 switch (runtime
->channels
) {
328 word
|= AC97C_CH_ASSIGN(PCM_LEFT
, A
);
331 word
|= AC97C_CH_ASSIGN(PCM_LEFT
, A
)
332 | AC97C_CH_ASSIGN(PCM_RIGHT
, A
);
335 /* TODO: support more than two channels */
338 ac97c_writel(chip
, OCA
, word
);
340 /* configure sample format and size */
341 word
= ac97c_readl(chip
, CAMR
);
342 if (chip
->opened
<= 1)
343 word
= AC97C_CMR_DMAEN
| AC97C_CMR_SIZE_16
;
345 word
|= AC97C_CMR_DMAEN
| AC97C_CMR_SIZE_16
;
347 switch (runtime
->format
) {
348 case SNDRV_PCM_FORMAT_S16_LE
:
349 if (cpu_is_at32ap7000())
350 word
|= AC97C_CMR_CEM_LITTLE
;
352 case SNDRV_PCM_FORMAT_S16_BE
: /* fall through */
353 word
&= ~(AC97C_CMR_CEM_LITTLE
);
356 word
= ac97c_readl(chip
, OCA
);
357 word
&= ~(AC97C_CH_MASK(PCM_LEFT
) | AC97C_CH_MASK(PCM_RIGHT
));
358 ac97c_writel(chip
, OCA
, word
);
362 /* Enable underrun interrupt on channel A */
363 word
|= AC97C_CSR_UNRUN
;
365 ac97c_writel(chip
, CAMR
, word
);
367 /* Enable channel A event interrupt */
368 word
= ac97c_readl(chip
, IMR
);
369 word
|= AC97C_SR_CAEVT
;
370 ac97c_writel(chip
, IER
, word
);
372 /* set variable rate if needed */
373 if (runtime
->rate
!= 48000) {
374 word
= ac97c_readl(chip
, MR
);
375 word
|= AC97C_MR_VRA
;
376 ac97c_writel(chip
, MR
, word
);
378 word
= ac97c_readl(chip
, MR
);
379 word
&= ~(AC97C_MR_VRA
);
380 ac97c_writel(chip
, MR
, word
);
383 retval
= snd_ac97_set_rate(chip
->ac97
, AC97_PCM_FRONT_DAC_RATE
,
386 dev_dbg(&chip
->pdev
->dev
, "could not set rate %d Hz\n",
389 if (cpu_is_at32ap7000()) {
390 if (!test_bit(DMA_TX_READY
, &chip
->flags
))
391 retval
= atmel_ac97c_prepare_dma(chip
, substream
,
394 /* Initialize and start the PDC */
395 writel(runtime
->dma_addr
, chip
->regs
+ ATMEL_PDC_TPR
);
396 writel(block_size
/ 2, chip
->regs
+ ATMEL_PDC_TCR
);
397 writel(runtime
->dma_addr
+ block_size
,
398 chip
->regs
+ ATMEL_PDC_TNPR
);
399 writel(block_size
/ 2, chip
->regs
+ ATMEL_PDC_TNCR
);
405 static int atmel_ac97c_capture_prepare(struct snd_pcm_substream
*substream
)
407 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
408 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
409 int block_size
= frames_to_bytes(runtime
, runtime
->period_size
);
410 unsigned long word
= ac97c_readl(chip
, ICA
);
413 chip
->capture_period
= 0;
414 word
&= ~(AC97C_CH_MASK(PCM_LEFT
) | AC97C_CH_MASK(PCM_RIGHT
));
416 /* assign channels to AC97C channel A */
417 switch (runtime
->channels
) {
419 word
|= AC97C_CH_ASSIGN(PCM_LEFT
, A
);
422 word
|= AC97C_CH_ASSIGN(PCM_LEFT
, A
)
423 | AC97C_CH_ASSIGN(PCM_RIGHT
, A
);
426 /* TODO: support more than two channels */
429 ac97c_writel(chip
, ICA
, word
);
431 /* configure sample format and size */
432 word
= ac97c_readl(chip
, CAMR
);
433 if (chip
->opened
<= 1)
434 word
= AC97C_CMR_DMAEN
| AC97C_CMR_SIZE_16
;
436 word
|= AC97C_CMR_DMAEN
| AC97C_CMR_SIZE_16
;
438 switch (runtime
->format
) {
439 case SNDRV_PCM_FORMAT_S16_LE
:
440 if (cpu_is_at32ap7000())
441 word
|= AC97C_CMR_CEM_LITTLE
;
443 case SNDRV_PCM_FORMAT_S16_BE
: /* fall through */
444 word
&= ~(AC97C_CMR_CEM_LITTLE
);
447 word
= ac97c_readl(chip
, ICA
);
448 word
&= ~(AC97C_CH_MASK(PCM_LEFT
) | AC97C_CH_MASK(PCM_RIGHT
));
449 ac97c_writel(chip
, ICA
, word
);
453 /* Enable overrun interrupt on channel A */
454 word
|= AC97C_CSR_OVRUN
;
456 ac97c_writel(chip
, CAMR
, word
);
458 /* Enable channel A event interrupt */
459 word
= ac97c_readl(chip
, IMR
);
460 word
|= AC97C_SR_CAEVT
;
461 ac97c_writel(chip
, IER
, word
);
463 /* set variable rate if needed */
464 if (runtime
->rate
!= 48000) {
465 word
= ac97c_readl(chip
, MR
);
466 word
|= AC97C_MR_VRA
;
467 ac97c_writel(chip
, MR
, word
);
469 word
= ac97c_readl(chip
, MR
);
470 word
&= ~(AC97C_MR_VRA
);
471 ac97c_writel(chip
, MR
, word
);
474 retval
= snd_ac97_set_rate(chip
->ac97
, AC97_PCM_LR_ADC_RATE
,
477 dev_dbg(&chip
->pdev
->dev
, "could not set rate %d Hz\n",
480 if (cpu_is_at32ap7000()) {
481 if (!test_bit(DMA_RX_READY
, &chip
->flags
))
482 retval
= atmel_ac97c_prepare_dma(chip
, substream
,
485 /* Initialize and start the PDC */
486 writel(runtime
->dma_addr
, chip
->regs
+ ATMEL_PDC_RPR
);
487 writel(block_size
/ 2, chip
->regs
+ ATMEL_PDC_RCR
);
488 writel(runtime
->dma_addr
+ block_size
,
489 chip
->regs
+ ATMEL_PDC_RNPR
);
490 writel(block_size
/ 2, chip
->regs
+ ATMEL_PDC_RNCR
);
497 atmel_ac97c_playback_trigger(struct snd_pcm_substream
*substream
, int cmd
)
499 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
500 unsigned long camr
, ptcr
= 0;
503 camr
= ac97c_readl(chip
, CAMR
);
506 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
: /* fall through */
507 case SNDRV_PCM_TRIGGER_RESUME
: /* fall through */
508 case SNDRV_PCM_TRIGGER_START
:
509 if (cpu_is_at32ap7000()) {
510 retval
= dw_dma_cyclic_start(chip
->dma
.tx_chan
);
514 ptcr
= ATMEL_PDC_TXTEN
;
516 camr
|= AC97C_CMR_CENA
| AC97C_CSR_ENDTX
;
518 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
: /* fall through */
519 case SNDRV_PCM_TRIGGER_SUSPEND
: /* fall through */
520 case SNDRV_PCM_TRIGGER_STOP
:
521 if (cpu_is_at32ap7000())
522 dw_dma_cyclic_stop(chip
->dma
.tx_chan
);
524 ptcr
|= ATMEL_PDC_TXTDIS
;
525 if (chip
->opened
<= 1)
526 camr
&= ~AC97C_CMR_CENA
;
533 ac97c_writel(chip
, CAMR
, camr
);
534 if (!cpu_is_at32ap7000())
535 writel(ptcr
, chip
->regs
+ ATMEL_PDC_PTCR
);
541 atmel_ac97c_capture_trigger(struct snd_pcm_substream
*substream
, int cmd
)
543 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
544 unsigned long camr
, ptcr
= 0;
547 camr
= ac97c_readl(chip
, CAMR
);
548 ptcr
= readl(chip
->regs
+ ATMEL_PDC_PTSR
);
551 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
: /* fall through */
552 case SNDRV_PCM_TRIGGER_RESUME
: /* fall through */
553 case SNDRV_PCM_TRIGGER_START
:
554 if (cpu_is_at32ap7000()) {
555 retval
= dw_dma_cyclic_start(chip
->dma
.rx_chan
);
559 ptcr
= ATMEL_PDC_RXTEN
;
561 camr
|= AC97C_CMR_CENA
| AC97C_CSR_ENDRX
;
563 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
: /* fall through */
564 case SNDRV_PCM_TRIGGER_SUSPEND
: /* fall through */
565 case SNDRV_PCM_TRIGGER_STOP
:
566 if (cpu_is_at32ap7000())
567 dw_dma_cyclic_stop(chip
->dma
.rx_chan
);
569 ptcr
|= (ATMEL_PDC_RXTDIS
);
570 if (chip
->opened
<= 1)
571 camr
&= ~AC97C_CMR_CENA
;
578 ac97c_writel(chip
, CAMR
, camr
);
579 if (!cpu_is_at32ap7000())
580 writel(ptcr
, chip
->regs
+ ATMEL_PDC_PTCR
);
585 static snd_pcm_uframes_t
586 atmel_ac97c_playback_pointer(struct snd_pcm_substream
*substream
)
588 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
589 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
590 snd_pcm_uframes_t frames
;
593 if (cpu_is_at32ap7000())
594 bytes
= dw_dma_get_src_addr(chip
->dma
.tx_chan
);
596 bytes
= readl(chip
->regs
+ ATMEL_PDC_TPR
);
597 bytes
-= runtime
->dma_addr
;
599 frames
= bytes_to_frames(runtime
, bytes
);
600 if (frames
>= runtime
->buffer_size
)
601 frames
-= runtime
->buffer_size
;
605 static snd_pcm_uframes_t
606 atmel_ac97c_capture_pointer(struct snd_pcm_substream
*substream
)
608 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
609 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
610 snd_pcm_uframes_t frames
;
613 if (cpu_is_at32ap7000())
614 bytes
= dw_dma_get_dst_addr(chip
->dma
.rx_chan
);
616 bytes
= readl(chip
->regs
+ ATMEL_PDC_RPR
);
617 bytes
-= runtime
->dma_addr
;
619 frames
= bytes_to_frames(runtime
, bytes
);
620 if (frames
>= runtime
->buffer_size
)
621 frames
-= runtime
->buffer_size
;
625 static struct snd_pcm_ops atmel_ac97_playback_ops
= {
626 .open
= atmel_ac97c_playback_open
,
627 .close
= atmel_ac97c_playback_close
,
628 .ioctl
= snd_pcm_lib_ioctl
,
629 .hw_params
= atmel_ac97c_playback_hw_params
,
630 .hw_free
= atmel_ac97c_playback_hw_free
,
631 .prepare
= atmel_ac97c_playback_prepare
,
632 .trigger
= atmel_ac97c_playback_trigger
,
633 .pointer
= atmel_ac97c_playback_pointer
,
636 static struct snd_pcm_ops atmel_ac97_capture_ops
= {
637 .open
= atmel_ac97c_capture_open
,
638 .close
= atmel_ac97c_capture_close
,
639 .ioctl
= snd_pcm_lib_ioctl
,
640 .hw_params
= atmel_ac97c_capture_hw_params
,
641 .hw_free
= atmel_ac97c_capture_hw_free
,
642 .prepare
= atmel_ac97c_capture_prepare
,
643 .trigger
= atmel_ac97c_capture_trigger
,
644 .pointer
= atmel_ac97c_capture_pointer
,
647 static irqreturn_t
atmel_ac97c_interrupt(int irq
, void *dev
)
649 struct atmel_ac97c
*chip
= (struct atmel_ac97c
*)dev
;
650 irqreturn_t retval
= IRQ_NONE
;
651 u32 sr
= ac97c_readl(chip
, SR
);
652 u32 casr
= ac97c_readl(chip
, CASR
);
653 u32 cosr
= ac97c_readl(chip
, COSR
);
654 u32 camr
= ac97c_readl(chip
, CAMR
);
656 if (sr
& AC97C_SR_CAEVT
) {
657 struct snd_pcm_runtime
*runtime
;
658 int offset
, next_period
, block_size
;
659 dev_dbg(&chip
->pdev
->dev
, "channel A event%s%s%s%s%s%s\n",
660 casr
& AC97C_CSR_OVRUN
? " OVRUN" : "",
661 casr
& AC97C_CSR_RXRDY
? " RXRDY" : "",
662 casr
& AC97C_CSR_UNRUN
? " UNRUN" : "",
663 casr
& AC97C_CSR_TXEMPTY
? " TXEMPTY" : "",
664 casr
& AC97C_CSR_TXRDY
? " TXRDY" : "",
665 !casr
? " NONE" : "");
666 if (!cpu_is_at32ap7000()) {
667 if ((casr
& camr
) & AC97C_CSR_ENDTX
) {
668 runtime
= chip
->playback_substream
->runtime
;
669 block_size
= frames_to_bytes(runtime
,
670 runtime
->period_size
);
671 chip
->playback_period
++;
673 if (chip
->playback_period
== runtime
->periods
)
674 chip
->playback_period
= 0;
675 next_period
= chip
->playback_period
+ 1;
676 if (next_period
== runtime
->periods
)
679 offset
= block_size
* next_period
;
681 writel(runtime
->dma_addr
+ offset
,
682 chip
->regs
+ ATMEL_PDC_TNPR
);
683 writel(block_size
/ 2,
684 chip
->regs
+ ATMEL_PDC_TNCR
);
686 snd_pcm_period_elapsed(
687 chip
->playback_substream
);
689 if ((casr
& camr
) & AC97C_CSR_ENDRX
) {
690 runtime
= chip
->capture_substream
->runtime
;
691 block_size
= frames_to_bytes(runtime
,
692 runtime
->period_size
);
693 chip
->capture_period
++;
695 if (chip
->capture_period
== runtime
->periods
)
696 chip
->capture_period
= 0;
697 next_period
= chip
->capture_period
+ 1;
698 if (next_period
== runtime
->periods
)
701 offset
= block_size
* next_period
;
703 writel(runtime
->dma_addr
+ offset
,
704 chip
->regs
+ ATMEL_PDC_RNPR
);
705 writel(block_size
/ 2,
706 chip
->regs
+ ATMEL_PDC_RNCR
);
707 snd_pcm_period_elapsed(chip
->capture_substream
);
710 retval
= IRQ_HANDLED
;
713 if (sr
& AC97C_SR_COEVT
) {
714 dev_info(&chip
->pdev
->dev
, "codec channel event%s%s%s%s%s\n",
715 cosr
& AC97C_CSR_OVRUN
? " OVRUN" : "",
716 cosr
& AC97C_CSR_RXRDY
? " RXRDY" : "",
717 cosr
& AC97C_CSR_TXEMPTY
? " TXEMPTY" : "",
718 cosr
& AC97C_CSR_TXRDY
? " TXRDY" : "",
719 !cosr
? " NONE" : "");
720 retval
= IRQ_HANDLED
;
723 if (retval
== IRQ_NONE
) {
724 dev_err(&chip
->pdev
->dev
, "spurious interrupt sr 0x%08x "
725 "casr 0x%08x cosr 0x%08x\n", sr
, casr
, cosr
);
731 static struct ac97_pcm at91_ac97_pcm_defs
[] = {
736 .slots
= ((1 << AC97_SLOT_PCM_LEFT
)
737 | (1 << AC97_SLOT_PCM_RIGHT
)),
745 .slots
= ((1 << AC97_SLOT_PCM_LEFT
)
746 | (1 << AC97_SLOT_PCM_RIGHT
)),
754 .slots
= (1<<AC97_SLOT_MIC
),
759 static int atmel_ac97c_pcm_new(struct atmel_ac97c
*chip
)
762 struct snd_pcm_hardware hw
= atmel_ac97c_hw
;
763 int capture
, playback
, retval
, err
;
765 capture
= test_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
);
766 playback
= test_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
);
768 if (!cpu_is_at32ap7000()) {
769 err
= snd_ac97_pcm_assign(chip
->ac97_bus
,
770 ARRAY_SIZE(at91_ac97_pcm_defs
),
775 retval
= snd_pcm_new(chip
->card
, chip
->card
->shortname
,
776 0, playback
, capture
, &pcm
);
781 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
,
782 &atmel_ac97_capture_ops
);
784 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
785 &atmel_ac97_playback_ops
);
787 retval
= snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_DEV
,
788 &chip
->pdev
->dev
, hw
.periods_min
* hw
.period_bytes_min
,
789 hw
.buffer_bytes_max
);
793 pcm
->private_data
= chip
;
795 strcpy(pcm
->name
, chip
->card
->shortname
);
801 static int atmel_ac97c_mixer_new(struct atmel_ac97c
*chip
)
803 struct snd_ac97_template
template;
804 memset(&template, 0, sizeof(template));
805 template.private_data
= chip
;
806 return snd_ac97_mixer(chip
->ac97_bus
, &template, &chip
->ac97
);
809 static void atmel_ac97c_write(struct snd_ac97
*ac97
, unsigned short reg
,
812 struct atmel_ac97c
*chip
= get_chip(ac97
);
816 word
= (reg
& 0x7f) << 16 | val
;
819 if (ac97c_readl(chip
, COSR
) & AC97C_CSR_TXRDY
) {
820 ac97c_writel(chip
, COTHR
, word
);
826 dev_dbg(&chip
->pdev
->dev
, "codec write timeout\n");
829 static unsigned short atmel_ac97c_read(struct snd_ac97
*ac97
,
832 struct atmel_ac97c
*chip
= get_chip(ac97
);
837 word
= (0x80 | (reg
& 0x7f)) << 16;
839 if ((ac97c_readl(chip
, COSR
) & AC97C_CSR_RXRDY
) != 0)
840 ac97c_readl(chip
, CORHR
);
846 if ((ac97c_readl(chip
, COSR
) & AC97C_CSR_TXRDY
) != 0) {
847 ac97c_writel(chip
, COTHR
, word
);
859 if ((ac97c_readl(chip
, COSR
) & AC97C_CSR_RXRDY
) != 0) {
860 unsigned short val
= ac97c_readl(chip
, CORHR
);
871 dev_dbg(&chip
->pdev
->dev
, "codec read timeout\n");
875 static bool filter(struct dma_chan
*chan
, void *slave
)
877 struct dw_dma_slave
*dws
= slave
;
879 if (dws
->dma_dev
== chan
->device
->dev
) {
886 static void atmel_ac97c_reset(struct atmel_ac97c
*chip
)
888 ac97c_writel(chip
, MR
, 0);
889 ac97c_writel(chip
, MR
, AC97C_MR_ENA
);
890 ac97c_writel(chip
, CAMR
, 0);
891 ac97c_writel(chip
, COMR
, 0);
893 if (gpio_is_valid(chip
->reset_pin
)) {
894 gpio_set_value(chip
->reset_pin
, 0);
895 /* AC97 v2.2 specifications says minimum 1 us. */
897 gpio_set_value(chip
->reset_pin
, 1);
899 ac97c_writel(chip
, MR
, AC97C_MR_WRST
| AC97C_MR_ENA
);
901 ac97c_writel(chip
, MR
, AC97C_MR_ENA
);
905 static int atmel_ac97c_probe(struct platform_device
*pdev
)
907 struct snd_card
*card
;
908 struct atmel_ac97c
*chip
;
909 struct resource
*regs
;
910 struct ac97c_platform_data
*pdata
;
912 static struct snd_ac97_bus_ops ops
= {
913 .write
= atmel_ac97c_write
,
914 .read
= atmel_ac97c_read
,
919 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
921 dev_dbg(&pdev
->dev
, "no memory resource\n");
925 pdata
= pdev
->dev
.platform_data
;
927 dev_dbg(&pdev
->dev
, "no platform data\n");
931 irq
= platform_get_irq(pdev
, 0);
933 dev_dbg(&pdev
->dev
, "could not get irq\n");
937 if (cpu_is_at32ap7000()) {
938 pclk
= clk_get(&pdev
->dev
, "pclk");
940 pclk
= clk_get(&pdev
->dev
, "ac97_clk");
944 dev_dbg(&pdev
->dev
, "no peripheral clock\n");
945 return PTR_ERR(pclk
);
947 clk_prepare_enable(pclk
);
949 retval
= snd_card_new(&pdev
->dev
, SNDRV_DEFAULT_IDX1
,
950 SNDRV_DEFAULT_STR1
, THIS_MODULE
,
951 sizeof(struct atmel_ac97c
), &card
);
953 dev_dbg(&pdev
->dev
, "could not create sound card device\n");
954 goto err_snd_card_new
;
957 chip
= get_chip(card
);
959 retval
= request_irq(irq
, atmel_ac97c_interrupt
, 0, "AC97C", chip
);
961 dev_dbg(&pdev
->dev
, "unable to request irq %d\n", irq
);
962 goto err_request_irq
;
966 spin_lock_init(&chip
->lock
);
968 strcpy(card
->driver
, "Atmel AC97C");
969 strcpy(card
->shortname
, "Atmel AC97C");
970 sprintf(card
->longname
, "Atmel AC97 controller");
975 chip
->regs
= ioremap(regs
->start
, resource_size(regs
));
978 dev_dbg(&pdev
->dev
, "could not remap register memory\n");
983 if (gpio_is_valid(pdata
->reset_pin
)) {
984 if (gpio_request(pdata
->reset_pin
, "reset_pin")) {
985 dev_dbg(&pdev
->dev
, "reset pin not available\n");
986 chip
->reset_pin
= -ENODEV
;
988 gpio_direction_output(pdata
->reset_pin
, 1);
989 chip
->reset_pin
= pdata
->reset_pin
;
992 chip
->reset_pin
= -EINVAL
;
995 atmel_ac97c_reset(chip
);
997 /* Enable overrun interrupt from codec channel */
998 ac97c_writel(chip
, COMR
, AC97C_CSR_OVRUN
);
999 ac97c_writel(chip
, IER
, ac97c_readl(chip
, IMR
) | AC97C_SR_COEVT
);
1001 retval
= snd_ac97_bus(card
, 0, &ops
, chip
, &chip
->ac97_bus
);
1003 dev_dbg(&pdev
->dev
, "could not register on ac97 bus\n");
1007 retval
= atmel_ac97c_mixer_new(chip
);
1009 dev_dbg(&pdev
->dev
, "could not register ac97 mixer\n");
1013 if (cpu_is_at32ap7000()) {
1014 if (pdata
->rx_dws
.dma_dev
) {
1015 dma_cap_mask_t mask
;
1018 dma_cap_set(DMA_SLAVE
, mask
);
1020 chip
->dma
.rx_chan
= dma_request_channel(mask
, filter
,
1022 if (chip
->dma
.rx_chan
) {
1023 struct dma_slave_config dma_conf
= {
1024 .src_addr
= regs
->start
+ AC97C_CARHR
+
1027 DMA_SLAVE_BUSWIDTH_2_BYTES
,
1030 .direction
= DMA_DEV_TO_MEM
,
1034 dmaengine_slave_config(chip
->dma
.rx_chan
,
1038 dev_info(&chip
->pdev
->dev
, "using %s for DMA RX\n",
1039 dev_name(&chip
->dma
.rx_chan
->dev
->device
));
1040 set_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
);
1043 if (pdata
->tx_dws
.dma_dev
) {
1044 dma_cap_mask_t mask
;
1047 dma_cap_set(DMA_SLAVE
, mask
);
1049 chip
->dma
.tx_chan
= dma_request_channel(mask
, filter
,
1051 if (chip
->dma
.tx_chan
) {
1052 struct dma_slave_config dma_conf
= {
1053 .dst_addr
= regs
->start
+ AC97C_CATHR
+
1056 DMA_SLAVE_BUSWIDTH_2_BYTES
,
1059 .direction
= DMA_MEM_TO_DEV
,
1063 dmaengine_slave_config(chip
->dma
.tx_chan
,
1067 dev_info(&chip
->pdev
->dev
, "using %s for DMA TX\n",
1068 dev_name(&chip
->dma
.tx_chan
->dev
->device
));
1069 set_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
);
1072 if (!test_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
) &&
1073 !test_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
)) {
1074 dev_dbg(&pdev
->dev
, "DMA not available\n");
1079 /* Just pretend that we have DMA channel(for at91 i is actually
1081 set_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
);
1082 set_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
);
1085 retval
= atmel_ac97c_pcm_new(chip
);
1087 dev_dbg(&pdev
->dev
, "could not register ac97 pcm device\n");
1091 retval
= snd_card_register(card
);
1093 dev_dbg(&pdev
->dev
, "could not register sound card\n");
1097 platform_set_drvdata(pdev
, card
);
1099 dev_info(&pdev
->dev
, "Atmel AC97 controller at 0x%p, irq = %d\n",
1105 if (cpu_is_at32ap7000()) {
1106 if (test_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
))
1107 dma_release_channel(chip
->dma
.rx_chan
);
1108 if (test_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
))
1109 dma_release_channel(chip
->dma
.tx_chan
);
1110 clear_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
);
1111 clear_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
);
1112 chip
->dma
.rx_chan
= NULL
;
1113 chip
->dma
.tx_chan
= NULL
;
1116 if (gpio_is_valid(chip
->reset_pin
))
1117 gpio_free(chip
->reset_pin
);
1119 iounmap(chip
->regs
);
1121 free_irq(irq
, chip
);
1123 snd_card_free(card
);
1125 clk_disable_unprepare(pclk
);
1130 #ifdef CONFIG_PM_SLEEP
1131 static int atmel_ac97c_suspend(struct device
*pdev
)
1133 struct snd_card
*card
= dev_get_drvdata(pdev
);
1134 struct atmel_ac97c
*chip
= card
->private_data
;
1136 if (cpu_is_at32ap7000()) {
1137 if (test_bit(DMA_RX_READY
, &chip
->flags
))
1138 dw_dma_cyclic_stop(chip
->dma
.rx_chan
);
1139 if (test_bit(DMA_TX_READY
, &chip
->flags
))
1140 dw_dma_cyclic_stop(chip
->dma
.tx_chan
);
1142 clk_disable_unprepare(chip
->pclk
);
1147 static int atmel_ac97c_resume(struct device
*pdev
)
1149 struct snd_card
*card
= dev_get_drvdata(pdev
);
1150 struct atmel_ac97c
*chip
= card
->private_data
;
1152 clk_prepare_enable(chip
->pclk
);
1153 if (cpu_is_at32ap7000()) {
1154 if (test_bit(DMA_RX_READY
, &chip
->flags
))
1155 dw_dma_cyclic_start(chip
->dma
.rx_chan
);
1156 if (test_bit(DMA_TX_READY
, &chip
->flags
))
1157 dw_dma_cyclic_start(chip
->dma
.tx_chan
);
1162 static SIMPLE_DEV_PM_OPS(atmel_ac97c_pm
, atmel_ac97c_suspend
, atmel_ac97c_resume
);
1163 #define ATMEL_AC97C_PM_OPS &atmel_ac97c_pm
1165 #define ATMEL_AC97C_PM_OPS NULL
1168 static int atmel_ac97c_remove(struct platform_device
*pdev
)
1170 struct snd_card
*card
= platform_get_drvdata(pdev
);
1171 struct atmel_ac97c
*chip
= get_chip(card
);
1173 if (gpio_is_valid(chip
->reset_pin
))
1174 gpio_free(chip
->reset_pin
);
1176 ac97c_writel(chip
, CAMR
, 0);
1177 ac97c_writel(chip
, COMR
, 0);
1178 ac97c_writel(chip
, MR
, 0);
1180 clk_disable_unprepare(chip
->pclk
);
1181 clk_put(chip
->pclk
);
1182 iounmap(chip
->regs
);
1183 free_irq(chip
->irq
, chip
);
1185 if (cpu_is_at32ap7000()) {
1186 if (test_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
))
1187 dma_release_channel(chip
->dma
.rx_chan
);
1188 if (test_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
))
1189 dma_release_channel(chip
->dma
.tx_chan
);
1190 clear_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
);
1191 clear_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
);
1192 chip
->dma
.rx_chan
= NULL
;
1193 chip
->dma
.tx_chan
= NULL
;
1196 snd_card_free(card
);
1201 static struct platform_driver atmel_ac97c_driver
= {
1202 .probe
= atmel_ac97c_probe
,
1203 .remove
= atmel_ac97c_remove
,
1205 .name
= "atmel_ac97c",
1206 .pm
= ATMEL_AC97C_PM_OPS
,
1209 module_platform_driver(atmel_ac97c_driver
);
1211 MODULE_LICENSE("GPL");
1212 MODULE_DESCRIPTION("Driver for Atmel AC97 controller");
1213 MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");