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>
38 #ifdef CONFIG_ARCH_AT91
39 #include <mach/hardware.h>
51 /* Serialize access to opened variable */
52 static DEFINE_MUTEX(opened_mutex
);
54 struct atmel_ac97c_dma
{
55 struct dma_chan
*rx_chan
;
56 struct dma_chan
*tx_chan
;
61 struct platform_device
*pdev
;
62 struct atmel_ac97c_dma dma
;
64 struct snd_pcm_substream
*playback_substream
;
65 struct snd_pcm_substream
*capture_substream
;
66 struct snd_card
*card
;
68 struct snd_ac97
*ac97
;
69 struct snd_ac97_bus
*ac97_bus
;
72 unsigned int cur_rate
;
74 int playback_period
, capture_period
;
75 /* Serialize access to opened variable */
83 #define get_chip(card) ((struct atmel_ac97c *)(card)->private_data)
85 #define ac97c_writel(chip, reg, val) \
86 __raw_writel((val), (chip)->regs + AC97C_##reg)
87 #define ac97c_readl(chip, reg) \
88 __raw_readl((chip)->regs + AC97C_##reg)
90 /* This function is called by the DMA driver. */
91 static void atmel_ac97c_dma_playback_period_done(void *arg
)
93 struct atmel_ac97c
*chip
= arg
;
94 snd_pcm_period_elapsed(chip
->playback_substream
);
97 static void atmel_ac97c_dma_capture_period_done(void *arg
)
99 struct atmel_ac97c
*chip
= arg
;
100 snd_pcm_period_elapsed(chip
->capture_substream
);
103 static int atmel_ac97c_prepare_dma(struct atmel_ac97c
*chip
,
104 struct snd_pcm_substream
*substream
,
105 enum dma_transfer_direction direction
)
107 struct dma_chan
*chan
;
108 struct dw_cyclic_desc
*cdesc
;
109 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
110 unsigned long buffer_len
, period_len
;
113 * We don't do DMA on "complex" transfers, i.e. with
114 * non-halfword-aligned buffers or lengths.
116 if (runtime
->dma_addr
& 1 || runtime
->buffer_size
& 1) {
117 dev_dbg(&chip
->pdev
->dev
, "too complex transfer\n");
121 if (direction
== DMA_MEM_TO_DEV
)
122 chan
= chip
->dma
.tx_chan
;
124 chan
= chip
->dma
.rx_chan
;
126 buffer_len
= frames_to_bytes(runtime
, runtime
->buffer_size
);
127 period_len
= frames_to_bytes(runtime
, runtime
->period_size
);
129 cdesc
= dw_dma_cyclic_prep(chan
, runtime
->dma_addr
, buffer_len
,
130 period_len
, direction
);
132 dev_dbg(&chip
->pdev
->dev
, "could not prepare cyclic DMA\n");
133 return PTR_ERR(cdesc
);
136 if (direction
== DMA_MEM_TO_DEV
) {
137 cdesc
->period_callback
= atmel_ac97c_dma_playback_period_done
;
138 set_bit(DMA_TX_READY
, &chip
->flags
);
140 cdesc
->period_callback
= atmel_ac97c_dma_capture_period_done
;
141 set_bit(DMA_RX_READY
, &chip
->flags
);
144 cdesc
->period_callback_param
= chip
;
149 static struct snd_pcm_hardware atmel_ac97c_hw
= {
150 .info
= (SNDRV_PCM_INFO_MMAP
151 | SNDRV_PCM_INFO_MMAP_VALID
152 | SNDRV_PCM_INFO_INTERLEAVED
153 | SNDRV_PCM_INFO_BLOCK_TRANSFER
154 | SNDRV_PCM_INFO_JOINT_DUPLEX
155 | SNDRV_PCM_INFO_RESUME
156 | SNDRV_PCM_INFO_PAUSE
),
157 .formats
= (SNDRV_PCM_FMTBIT_S16_BE
158 | SNDRV_PCM_FMTBIT_S16_LE
),
159 .rates
= (SNDRV_PCM_RATE_CONTINUOUS
),
164 .buffer_bytes_max
= 2 * 2 * 64 * 2048,
165 .period_bytes_min
= 4096,
166 .period_bytes_max
= 4096,
171 static int atmel_ac97c_playback_open(struct snd_pcm_substream
*substream
)
173 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
174 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
176 mutex_lock(&opened_mutex
);
178 runtime
->hw
= atmel_ac97c_hw
;
179 if (chip
->cur_rate
) {
180 runtime
->hw
.rate_min
= chip
->cur_rate
;
181 runtime
->hw
.rate_max
= chip
->cur_rate
;
183 if (chip
->cur_format
)
184 runtime
->hw
.formats
= pcm_format_to_bits(chip
->cur_format
);
185 mutex_unlock(&opened_mutex
);
186 chip
->playback_substream
= substream
;
190 static int atmel_ac97c_capture_open(struct snd_pcm_substream
*substream
)
192 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
193 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
195 mutex_lock(&opened_mutex
);
197 runtime
->hw
= atmel_ac97c_hw
;
198 if (chip
->cur_rate
) {
199 runtime
->hw
.rate_min
= chip
->cur_rate
;
200 runtime
->hw
.rate_max
= chip
->cur_rate
;
202 if (chip
->cur_format
)
203 runtime
->hw
.formats
= pcm_format_to_bits(chip
->cur_format
);
204 mutex_unlock(&opened_mutex
);
205 chip
->capture_substream
= substream
;
209 static int atmel_ac97c_playback_close(struct snd_pcm_substream
*substream
)
211 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
213 mutex_lock(&opened_mutex
);
217 chip
->cur_format
= 0;
219 mutex_unlock(&opened_mutex
);
221 chip
->playback_substream
= NULL
;
226 static int atmel_ac97c_capture_close(struct snd_pcm_substream
*substream
)
228 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
230 mutex_lock(&opened_mutex
);
234 chip
->cur_format
= 0;
236 mutex_unlock(&opened_mutex
);
238 chip
->capture_substream
= NULL
;
243 static int atmel_ac97c_playback_hw_params(struct snd_pcm_substream
*substream
,
244 struct snd_pcm_hw_params
*hw_params
)
246 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
249 retval
= snd_pcm_lib_malloc_pages(substream
,
250 params_buffer_bytes(hw_params
));
253 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
254 if (cpu_is_at32ap7000()) {
255 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
257 if (test_and_clear_bit(DMA_TX_READY
, &chip
->flags
))
258 dw_dma_cyclic_free(chip
->dma
.tx_chan
);
260 /* Set restrictions to params. */
261 mutex_lock(&opened_mutex
);
262 chip
->cur_rate
= params_rate(hw_params
);
263 chip
->cur_format
= params_format(hw_params
);
264 mutex_unlock(&opened_mutex
);
269 static int atmel_ac97c_capture_hw_params(struct snd_pcm_substream
*substream
,
270 struct snd_pcm_hw_params
*hw_params
)
272 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
275 retval
= snd_pcm_lib_malloc_pages(substream
,
276 params_buffer_bytes(hw_params
));
279 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
280 if (cpu_is_at32ap7000() && retval
== 1)
281 if (test_and_clear_bit(DMA_RX_READY
, &chip
->flags
))
282 dw_dma_cyclic_free(chip
->dma
.rx_chan
);
284 /* Set restrictions to params. */
285 mutex_lock(&opened_mutex
);
286 chip
->cur_rate
= params_rate(hw_params
);
287 chip
->cur_format
= params_format(hw_params
);
288 mutex_unlock(&opened_mutex
);
293 static int atmel_ac97c_playback_hw_free(struct snd_pcm_substream
*substream
)
295 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
296 if (cpu_is_at32ap7000()) {
297 if (test_and_clear_bit(DMA_TX_READY
, &chip
->flags
))
298 dw_dma_cyclic_free(chip
->dma
.tx_chan
);
300 return snd_pcm_lib_free_pages(substream
);
303 static int atmel_ac97c_capture_hw_free(struct snd_pcm_substream
*substream
)
305 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
306 if (cpu_is_at32ap7000()) {
307 if (test_and_clear_bit(DMA_RX_READY
, &chip
->flags
))
308 dw_dma_cyclic_free(chip
->dma
.rx_chan
);
310 return snd_pcm_lib_free_pages(substream
);
313 static int atmel_ac97c_playback_prepare(struct snd_pcm_substream
*substream
)
315 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
316 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
317 int block_size
= frames_to_bytes(runtime
, runtime
->period_size
);
318 unsigned long word
= ac97c_readl(chip
, OCA
);
321 chip
->playback_period
= 0;
322 word
&= ~(AC97C_CH_MASK(PCM_LEFT
) | AC97C_CH_MASK(PCM_RIGHT
));
324 /* assign channels to AC97C channel A */
325 switch (runtime
->channels
) {
327 word
|= AC97C_CH_ASSIGN(PCM_LEFT
, A
);
330 word
|= AC97C_CH_ASSIGN(PCM_LEFT
, A
)
331 | AC97C_CH_ASSIGN(PCM_RIGHT
, A
);
334 /* TODO: support more than two channels */
337 ac97c_writel(chip
, OCA
, word
);
339 /* configure sample format and size */
340 word
= ac97c_readl(chip
, CAMR
);
341 if (chip
->opened
<= 1)
342 word
= AC97C_CMR_DMAEN
| AC97C_CMR_SIZE_16
;
344 word
|= AC97C_CMR_DMAEN
| AC97C_CMR_SIZE_16
;
346 switch (runtime
->format
) {
347 case SNDRV_PCM_FORMAT_S16_LE
:
348 if (cpu_is_at32ap7000())
349 word
|= AC97C_CMR_CEM_LITTLE
;
351 case SNDRV_PCM_FORMAT_S16_BE
: /* fall through */
352 word
&= ~(AC97C_CMR_CEM_LITTLE
);
355 word
= ac97c_readl(chip
, OCA
);
356 word
&= ~(AC97C_CH_MASK(PCM_LEFT
) | AC97C_CH_MASK(PCM_RIGHT
));
357 ac97c_writel(chip
, OCA
, word
);
361 /* Enable underrun interrupt on channel A */
362 word
|= AC97C_CSR_UNRUN
;
364 ac97c_writel(chip
, CAMR
, word
);
366 /* Enable channel A event interrupt */
367 word
= ac97c_readl(chip
, IMR
);
368 word
|= AC97C_SR_CAEVT
;
369 ac97c_writel(chip
, IER
, word
);
371 /* set variable rate if needed */
372 if (runtime
->rate
!= 48000) {
373 word
= ac97c_readl(chip
, MR
);
374 word
|= AC97C_MR_VRA
;
375 ac97c_writel(chip
, MR
, word
);
377 word
= ac97c_readl(chip
, MR
);
378 word
&= ~(AC97C_MR_VRA
);
379 ac97c_writel(chip
, MR
, word
);
382 retval
= snd_ac97_set_rate(chip
->ac97
, AC97_PCM_FRONT_DAC_RATE
,
385 dev_dbg(&chip
->pdev
->dev
, "could not set rate %d Hz\n",
388 if (cpu_is_at32ap7000()) {
389 if (!test_bit(DMA_TX_READY
, &chip
->flags
))
390 retval
= atmel_ac97c_prepare_dma(chip
, substream
,
393 /* Initialize and start the PDC */
394 writel(runtime
->dma_addr
, chip
->regs
+ ATMEL_PDC_TPR
);
395 writel(block_size
/ 2, chip
->regs
+ ATMEL_PDC_TCR
);
396 writel(runtime
->dma_addr
+ block_size
,
397 chip
->regs
+ ATMEL_PDC_TNPR
);
398 writel(block_size
/ 2, chip
->regs
+ ATMEL_PDC_TNCR
);
404 static int atmel_ac97c_capture_prepare(struct snd_pcm_substream
*substream
)
406 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
407 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
408 int block_size
= frames_to_bytes(runtime
, runtime
->period_size
);
409 unsigned long word
= ac97c_readl(chip
, ICA
);
412 chip
->capture_period
= 0;
413 word
&= ~(AC97C_CH_MASK(PCM_LEFT
) | AC97C_CH_MASK(PCM_RIGHT
));
415 /* assign channels to AC97C channel A */
416 switch (runtime
->channels
) {
418 word
|= AC97C_CH_ASSIGN(PCM_LEFT
, A
);
421 word
|= AC97C_CH_ASSIGN(PCM_LEFT
, A
)
422 | AC97C_CH_ASSIGN(PCM_RIGHT
, A
);
425 /* TODO: support more than two channels */
428 ac97c_writel(chip
, ICA
, word
);
430 /* configure sample format and size */
431 word
= ac97c_readl(chip
, CAMR
);
432 if (chip
->opened
<= 1)
433 word
= AC97C_CMR_DMAEN
| AC97C_CMR_SIZE_16
;
435 word
|= AC97C_CMR_DMAEN
| AC97C_CMR_SIZE_16
;
437 switch (runtime
->format
) {
438 case SNDRV_PCM_FORMAT_S16_LE
:
439 if (cpu_is_at32ap7000())
440 word
|= AC97C_CMR_CEM_LITTLE
;
442 case SNDRV_PCM_FORMAT_S16_BE
: /* fall through */
443 word
&= ~(AC97C_CMR_CEM_LITTLE
);
446 word
= ac97c_readl(chip
, ICA
);
447 word
&= ~(AC97C_CH_MASK(PCM_LEFT
) | AC97C_CH_MASK(PCM_RIGHT
));
448 ac97c_writel(chip
, ICA
, word
);
452 /* Enable overrun interrupt on channel A */
453 word
|= AC97C_CSR_OVRUN
;
455 ac97c_writel(chip
, CAMR
, word
);
457 /* Enable channel A event interrupt */
458 word
= ac97c_readl(chip
, IMR
);
459 word
|= AC97C_SR_CAEVT
;
460 ac97c_writel(chip
, IER
, word
);
462 /* set variable rate if needed */
463 if (runtime
->rate
!= 48000) {
464 word
= ac97c_readl(chip
, MR
);
465 word
|= AC97C_MR_VRA
;
466 ac97c_writel(chip
, MR
, word
);
468 word
= ac97c_readl(chip
, MR
);
469 word
&= ~(AC97C_MR_VRA
);
470 ac97c_writel(chip
, MR
, word
);
473 retval
= snd_ac97_set_rate(chip
->ac97
, AC97_PCM_LR_ADC_RATE
,
476 dev_dbg(&chip
->pdev
->dev
, "could not set rate %d Hz\n",
479 if (cpu_is_at32ap7000()) {
480 if (!test_bit(DMA_RX_READY
, &chip
->flags
))
481 retval
= atmel_ac97c_prepare_dma(chip
, substream
,
484 /* Initialize and start the PDC */
485 writel(runtime
->dma_addr
, chip
->regs
+ ATMEL_PDC_RPR
);
486 writel(block_size
/ 2, chip
->regs
+ ATMEL_PDC_RCR
);
487 writel(runtime
->dma_addr
+ block_size
,
488 chip
->regs
+ ATMEL_PDC_RNPR
);
489 writel(block_size
/ 2, chip
->regs
+ ATMEL_PDC_RNCR
);
496 atmel_ac97c_playback_trigger(struct snd_pcm_substream
*substream
, int cmd
)
498 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
499 unsigned long camr
, ptcr
= 0;
502 camr
= ac97c_readl(chip
, CAMR
);
505 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
: /* fall through */
506 case SNDRV_PCM_TRIGGER_RESUME
: /* fall through */
507 case SNDRV_PCM_TRIGGER_START
:
508 if (cpu_is_at32ap7000()) {
509 retval
= dw_dma_cyclic_start(chip
->dma
.tx_chan
);
513 ptcr
= ATMEL_PDC_TXTEN
;
515 camr
|= AC97C_CMR_CENA
| AC97C_CSR_ENDTX
;
517 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
: /* fall through */
518 case SNDRV_PCM_TRIGGER_SUSPEND
: /* fall through */
519 case SNDRV_PCM_TRIGGER_STOP
:
520 if (cpu_is_at32ap7000())
521 dw_dma_cyclic_stop(chip
->dma
.tx_chan
);
523 ptcr
|= ATMEL_PDC_TXTDIS
;
524 if (chip
->opened
<= 1)
525 camr
&= ~AC97C_CMR_CENA
;
532 ac97c_writel(chip
, CAMR
, camr
);
533 if (!cpu_is_at32ap7000())
534 writel(ptcr
, chip
->regs
+ ATMEL_PDC_PTCR
);
540 atmel_ac97c_capture_trigger(struct snd_pcm_substream
*substream
, int cmd
)
542 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
543 unsigned long camr
, ptcr
= 0;
546 camr
= ac97c_readl(chip
, CAMR
);
547 ptcr
= readl(chip
->regs
+ ATMEL_PDC_PTSR
);
550 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
: /* fall through */
551 case SNDRV_PCM_TRIGGER_RESUME
: /* fall through */
552 case SNDRV_PCM_TRIGGER_START
:
553 if (cpu_is_at32ap7000()) {
554 retval
= dw_dma_cyclic_start(chip
->dma
.rx_chan
);
558 ptcr
= ATMEL_PDC_RXTEN
;
560 camr
|= AC97C_CMR_CENA
| AC97C_CSR_ENDRX
;
562 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
: /* fall through */
563 case SNDRV_PCM_TRIGGER_SUSPEND
: /* fall through */
564 case SNDRV_PCM_TRIGGER_STOP
:
565 if (cpu_is_at32ap7000())
566 dw_dma_cyclic_stop(chip
->dma
.rx_chan
);
568 ptcr
|= (ATMEL_PDC_RXTDIS
);
569 if (chip
->opened
<= 1)
570 camr
&= ~AC97C_CMR_CENA
;
577 ac97c_writel(chip
, CAMR
, camr
);
578 if (!cpu_is_at32ap7000())
579 writel(ptcr
, chip
->regs
+ ATMEL_PDC_PTCR
);
584 static snd_pcm_uframes_t
585 atmel_ac97c_playback_pointer(struct snd_pcm_substream
*substream
)
587 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
588 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
589 snd_pcm_uframes_t frames
;
592 if (cpu_is_at32ap7000())
593 bytes
= dw_dma_get_src_addr(chip
->dma
.tx_chan
);
595 bytes
= readl(chip
->regs
+ ATMEL_PDC_TPR
);
596 bytes
-= runtime
->dma_addr
;
598 frames
= bytes_to_frames(runtime
, bytes
);
599 if (frames
>= runtime
->buffer_size
)
600 frames
-= runtime
->buffer_size
;
604 static snd_pcm_uframes_t
605 atmel_ac97c_capture_pointer(struct snd_pcm_substream
*substream
)
607 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
608 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
609 snd_pcm_uframes_t frames
;
612 if (cpu_is_at32ap7000())
613 bytes
= dw_dma_get_dst_addr(chip
->dma
.rx_chan
);
615 bytes
= readl(chip
->regs
+ ATMEL_PDC_RPR
);
616 bytes
-= runtime
->dma_addr
;
618 frames
= bytes_to_frames(runtime
, bytes
);
619 if (frames
>= runtime
->buffer_size
)
620 frames
-= runtime
->buffer_size
;
624 static struct snd_pcm_ops atmel_ac97_playback_ops
= {
625 .open
= atmel_ac97c_playback_open
,
626 .close
= atmel_ac97c_playback_close
,
627 .ioctl
= snd_pcm_lib_ioctl
,
628 .hw_params
= atmel_ac97c_playback_hw_params
,
629 .hw_free
= atmel_ac97c_playback_hw_free
,
630 .prepare
= atmel_ac97c_playback_prepare
,
631 .trigger
= atmel_ac97c_playback_trigger
,
632 .pointer
= atmel_ac97c_playback_pointer
,
635 static struct snd_pcm_ops atmel_ac97_capture_ops
= {
636 .open
= atmel_ac97c_capture_open
,
637 .close
= atmel_ac97c_capture_close
,
638 .ioctl
= snd_pcm_lib_ioctl
,
639 .hw_params
= atmel_ac97c_capture_hw_params
,
640 .hw_free
= atmel_ac97c_capture_hw_free
,
641 .prepare
= atmel_ac97c_capture_prepare
,
642 .trigger
= atmel_ac97c_capture_trigger
,
643 .pointer
= atmel_ac97c_capture_pointer
,
646 static irqreturn_t
atmel_ac97c_interrupt(int irq
, void *dev
)
648 struct atmel_ac97c
*chip
= (struct atmel_ac97c
*)dev
;
649 irqreturn_t retval
= IRQ_NONE
;
650 u32 sr
= ac97c_readl(chip
, SR
);
651 u32 casr
= ac97c_readl(chip
, CASR
);
652 u32 cosr
= ac97c_readl(chip
, COSR
);
653 u32 camr
= ac97c_readl(chip
, CAMR
);
655 if (sr
& AC97C_SR_CAEVT
) {
656 struct snd_pcm_runtime
*runtime
;
657 int offset
, next_period
, block_size
;
658 dev_dbg(&chip
->pdev
->dev
, "channel A event%s%s%s%s%s%s\n",
659 casr
& AC97C_CSR_OVRUN
? " OVRUN" : "",
660 casr
& AC97C_CSR_RXRDY
? " RXRDY" : "",
661 casr
& AC97C_CSR_UNRUN
? " UNRUN" : "",
662 casr
& AC97C_CSR_TXEMPTY
? " TXEMPTY" : "",
663 casr
& AC97C_CSR_TXRDY
? " TXRDY" : "",
664 !casr
? " NONE" : "");
665 if (!cpu_is_at32ap7000()) {
666 if ((casr
& camr
) & AC97C_CSR_ENDTX
) {
667 runtime
= chip
->playback_substream
->runtime
;
668 block_size
= frames_to_bytes(runtime
,
669 runtime
->period_size
);
670 chip
->playback_period
++;
672 if (chip
->playback_period
== runtime
->periods
)
673 chip
->playback_period
= 0;
674 next_period
= chip
->playback_period
+ 1;
675 if (next_period
== runtime
->periods
)
678 offset
= block_size
* next_period
;
680 writel(runtime
->dma_addr
+ offset
,
681 chip
->regs
+ ATMEL_PDC_TNPR
);
682 writel(block_size
/ 2,
683 chip
->regs
+ ATMEL_PDC_TNCR
);
685 snd_pcm_period_elapsed(
686 chip
->playback_substream
);
688 if ((casr
& camr
) & AC97C_CSR_ENDRX
) {
689 runtime
= chip
->capture_substream
->runtime
;
690 block_size
= frames_to_bytes(runtime
,
691 runtime
->period_size
);
692 chip
->capture_period
++;
694 if (chip
->capture_period
== runtime
->periods
)
695 chip
->capture_period
= 0;
696 next_period
= chip
->capture_period
+ 1;
697 if (next_period
== runtime
->periods
)
700 offset
= block_size
* next_period
;
702 writel(runtime
->dma_addr
+ offset
,
703 chip
->regs
+ ATMEL_PDC_RNPR
);
704 writel(block_size
/ 2,
705 chip
->regs
+ ATMEL_PDC_RNCR
);
706 snd_pcm_period_elapsed(chip
->capture_substream
);
709 retval
= IRQ_HANDLED
;
712 if (sr
& AC97C_SR_COEVT
) {
713 dev_info(&chip
->pdev
->dev
, "codec channel event%s%s%s%s%s\n",
714 cosr
& AC97C_CSR_OVRUN
? " OVRUN" : "",
715 cosr
& AC97C_CSR_RXRDY
? " RXRDY" : "",
716 cosr
& AC97C_CSR_TXEMPTY
? " TXEMPTY" : "",
717 cosr
& AC97C_CSR_TXRDY
? " TXRDY" : "",
718 !cosr
? " NONE" : "");
719 retval
= IRQ_HANDLED
;
722 if (retval
== IRQ_NONE
) {
723 dev_err(&chip
->pdev
->dev
, "spurious interrupt sr 0x%08x "
724 "casr 0x%08x cosr 0x%08x\n", sr
, casr
, cosr
);
730 static struct ac97_pcm at91_ac97_pcm_defs
[] = {
735 .slots
= ((1 << AC97_SLOT_PCM_LEFT
)
736 | (1 << AC97_SLOT_PCM_RIGHT
)),
744 .slots
= ((1 << AC97_SLOT_PCM_LEFT
)
745 | (1 << AC97_SLOT_PCM_RIGHT
)),
753 .slots
= (1<<AC97_SLOT_MIC
),
758 static int atmel_ac97c_pcm_new(struct atmel_ac97c
*chip
)
761 struct snd_pcm_hardware hw
= atmel_ac97c_hw
;
762 int capture
, playback
, retval
, err
;
764 capture
= test_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
);
765 playback
= test_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
);
767 if (!cpu_is_at32ap7000()) {
768 err
= snd_ac97_pcm_assign(chip
->ac97_bus
,
769 ARRAY_SIZE(at91_ac97_pcm_defs
),
774 retval
= snd_pcm_new(chip
->card
, chip
->card
->shortname
,
775 chip
->pdev
->id
, playback
, capture
, &pcm
);
780 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
,
781 &atmel_ac97_capture_ops
);
783 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
784 &atmel_ac97_playback_ops
);
786 retval
= snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_DEV
,
787 &chip
->pdev
->dev
, hw
.periods_min
* hw
.period_bytes_min
,
788 hw
.buffer_bytes_max
);
792 pcm
->private_data
= chip
;
794 strcpy(pcm
->name
, chip
->card
->shortname
);
800 static int atmel_ac97c_mixer_new(struct atmel_ac97c
*chip
)
802 struct snd_ac97_template
template;
803 memset(&template, 0, sizeof(template));
804 template.private_data
= chip
;
805 return snd_ac97_mixer(chip
->ac97_bus
, &template, &chip
->ac97
);
808 static void atmel_ac97c_write(struct snd_ac97
*ac97
, unsigned short reg
,
811 struct atmel_ac97c
*chip
= get_chip(ac97
);
815 word
= (reg
& 0x7f) << 16 | val
;
818 if (ac97c_readl(chip
, COSR
) & AC97C_CSR_TXRDY
) {
819 ac97c_writel(chip
, COTHR
, word
);
825 dev_dbg(&chip
->pdev
->dev
, "codec write timeout\n");
828 static unsigned short atmel_ac97c_read(struct snd_ac97
*ac97
,
831 struct atmel_ac97c
*chip
= get_chip(ac97
);
836 word
= (0x80 | (reg
& 0x7f)) << 16;
838 if ((ac97c_readl(chip
, COSR
) & AC97C_CSR_RXRDY
) != 0)
839 ac97c_readl(chip
, CORHR
);
845 if ((ac97c_readl(chip
, COSR
) & AC97C_CSR_TXRDY
) != 0) {
846 ac97c_writel(chip
, COTHR
, word
);
858 if ((ac97c_readl(chip
, COSR
) & AC97C_CSR_RXRDY
) != 0) {
859 unsigned short val
= ac97c_readl(chip
, CORHR
);
870 dev_dbg(&chip
->pdev
->dev
, "codec read timeout\n");
874 static bool filter(struct dma_chan
*chan
, void *slave
)
876 struct dw_dma_slave
*dws
= slave
;
878 if (dws
->dma_dev
== chan
->device
->dev
) {
885 static void atmel_ac97c_reset(struct atmel_ac97c
*chip
)
887 ac97c_writel(chip
, MR
, 0);
888 ac97c_writel(chip
, MR
, AC97C_MR_ENA
);
889 ac97c_writel(chip
, CAMR
, 0);
890 ac97c_writel(chip
, COMR
, 0);
892 if (gpio_is_valid(chip
->reset_pin
)) {
893 gpio_set_value(chip
->reset_pin
, 0);
894 /* AC97 v2.2 specifications says minimum 1 us. */
896 gpio_set_value(chip
->reset_pin
, 1);
898 ac97c_writel(chip
, MR
, AC97C_MR_WRST
| AC97C_MR_ENA
);
900 ac97c_writel(chip
, MR
, AC97C_MR_ENA
);
904 static int atmel_ac97c_probe(struct platform_device
*pdev
)
906 struct snd_card
*card
;
907 struct atmel_ac97c
*chip
;
908 struct resource
*regs
;
909 struct ac97c_platform_data
*pdata
;
911 static struct snd_ac97_bus_ops ops
= {
912 .write
= atmel_ac97c_write
,
913 .read
= atmel_ac97c_read
,
918 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
920 dev_dbg(&pdev
->dev
, "no memory resource\n");
924 pdata
= pdev
->dev
.platform_data
;
926 dev_dbg(&pdev
->dev
, "no platform data\n");
930 irq
= platform_get_irq(pdev
, 0);
932 dev_dbg(&pdev
->dev
, "could not get irq\n");
936 if (cpu_is_at32ap7000()) {
937 pclk
= clk_get(&pdev
->dev
, "pclk");
939 pclk
= clk_get(&pdev
->dev
, "ac97_clk");
943 dev_dbg(&pdev
->dev
, "no peripheral clock\n");
944 return PTR_ERR(pclk
);
948 retval
= snd_card_create(SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
,
949 THIS_MODULE
, sizeof(struct atmel_ac97c
), &card
);
951 dev_dbg(&pdev
->dev
, "could not create sound card device\n");
952 goto err_snd_card_new
;
955 chip
= get_chip(card
);
957 retval
= request_irq(irq
, atmel_ac97c_interrupt
, 0, "AC97C", chip
);
959 dev_dbg(&pdev
->dev
, "unable to request irq %d\n", irq
);
960 goto err_request_irq
;
964 spin_lock_init(&chip
->lock
);
966 strcpy(card
->driver
, "Atmel AC97C");
967 strcpy(card
->shortname
, "Atmel AC97C");
968 sprintf(card
->longname
, "Atmel AC97 controller");
973 chip
->regs
= ioremap(regs
->start
, resource_size(regs
));
976 dev_dbg(&pdev
->dev
, "could not remap register memory\n");
981 if (gpio_is_valid(pdata
->reset_pin
)) {
982 if (gpio_request(pdata
->reset_pin
, "reset_pin")) {
983 dev_dbg(&pdev
->dev
, "reset pin not available\n");
984 chip
->reset_pin
= -ENODEV
;
986 gpio_direction_output(pdata
->reset_pin
, 1);
987 chip
->reset_pin
= pdata
->reset_pin
;
990 chip
->reset_pin
= -EINVAL
;
993 snd_card_set_dev(card
, &pdev
->dev
);
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 snd_card_set_dev(card
, NULL
);
1118 if (gpio_is_valid(chip
->reset_pin
))
1119 gpio_free(chip
->reset_pin
);
1121 iounmap(chip
->regs
);
1123 free_irq(irq
, chip
);
1125 snd_card_free(card
);
1132 #ifdef CONFIG_PM_SLEEP
1133 static int atmel_ac97c_suspend(struct device
*pdev
)
1135 struct snd_card
*card
= dev_get_drvdata(pdev
);
1136 struct atmel_ac97c
*chip
= card
->private_data
;
1138 if (cpu_is_at32ap7000()) {
1139 if (test_bit(DMA_RX_READY
, &chip
->flags
))
1140 dw_dma_cyclic_stop(chip
->dma
.rx_chan
);
1141 if (test_bit(DMA_TX_READY
, &chip
->flags
))
1142 dw_dma_cyclic_stop(chip
->dma
.tx_chan
);
1144 clk_disable(chip
->pclk
);
1149 static int atmel_ac97c_resume(struct device
*pdev
)
1151 struct snd_card
*card
= dev_get_drvdata(pdev
);
1152 struct atmel_ac97c
*chip
= card
->private_data
;
1154 clk_enable(chip
->pclk
);
1155 if (cpu_is_at32ap7000()) {
1156 if (test_bit(DMA_RX_READY
, &chip
->flags
))
1157 dw_dma_cyclic_start(chip
->dma
.rx_chan
);
1158 if (test_bit(DMA_TX_READY
, &chip
->flags
))
1159 dw_dma_cyclic_start(chip
->dma
.tx_chan
);
1164 static SIMPLE_DEV_PM_OPS(atmel_ac97c_pm
, atmel_ac97c_suspend
, atmel_ac97c_resume
);
1165 #define ATMEL_AC97C_PM_OPS &atmel_ac97c_pm
1167 #define ATMEL_AC97C_PM_OPS NULL
1170 static int atmel_ac97c_remove(struct platform_device
*pdev
)
1172 struct snd_card
*card
= platform_get_drvdata(pdev
);
1173 struct atmel_ac97c
*chip
= get_chip(card
);
1175 if (gpio_is_valid(chip
->reset_pin
))
1176 gpio_free(chip
->reset_pin
);
1178 ac97c_writel(chip
, CAMR
, 0);
1179 ac97c_writel(chip
, COMR
, 0);
1180 ac97c_writel(chip
, MR
, 0);
1182 clk_disable(chip
->pclk
);
1183 clk_put(chip
->pclk
);
1184 iounmap(chip
->regs
);
1185 free_irq(chip
->irq
, chip
);
1187 if (cpu_is_at32ap7000()) {
1188 if (test_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
))
1189 dma_release_channel(chip
->dma
.rx_chan
);
1190 if (test_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
))
1191 dma_release_channel(chip
->dma
.tx_chan
);
1192 clear_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
);
1193 clear_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
);
1194 chip
->dma
.rx_chan
= NULL
;
1195 chip
->dma
.tx_chan
= NULL
;
1198 snd_card_set_dev(card
, NULL
);
1199 snd_card_free(card
);
1204 static struct platform_driver atmel_ac97c_driver
= {
1205 .remove
= atmel_ac97c_remove
,
1207 .name
= "atmel_ac97c",
1208 .owner
= THIS_MODULE
,
1209 .pm
= ATMEL_AC97C_PM_OPS
,
1213 static int __init
atmel_ac97c_init(void)
1215 return platform_driver_probe(&atmel_ac97c_driver
,
1218 module_init(atmel_ac97c_init
);
1220 static void __exit
atmel_ac97c_exit(void)
1222 platform_driver_unregister(&atmel_ac97c_driver
);
1224 module_exit(atmel_ac97c_exit
);
1226 MODULE_LICENSE("GPL");
1227 MODULE_DESCRIPTION("Driver for Atmel AC97 controller");
1228 MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");