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>
25 #include <sound/core.h>
26 #include <sound/initval.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/ac97_codec.h>
30 #include <sound/atmel-ac97c.h>
31 #include <sound/memalloc.h>
33 #include <linux/dw_dmac.h>
36 #include <mach/hardware.h>
37 #include <mach/gpio.h>
48 /* Serialize access to opened variable */
49 static DEFINE_MUTEX(opened_mutex
);
51 struct atmel_ac97c_dma
{
52 struct dma_chan
*rx_chan
;
53 struct dma_chan
*tx_chan
;
58 struct platform_device
*pdev
;
59 struct atmel_ac97c_dma dma
;
61 struct snd_pcm_substream
*playback_substream
;
62 struct snd_pcm_substream
*capture_substream
;
63 struct snd_card
*card
;
65 struct snd_ac97
*ac97
;
66 struct snd_ac97_bus
*ac97_bus
;
69 unsigned int cur_rate
;
71 int playback_period
, capture_period
;
72 /* Serialize access to opened variable */
80 #define get_chip(card) ((struct atmel_ac97c *)(card)->private_data)
82 #define ac97c_writel(chip, reg, val) \
83 __raw_writel((val), (chip)->regs + AC97C_##reg)
84 #define ac97c_readl(chip, reg) \
85 __raw_readl((chip)->regs + AC97C_##reg)
87 /* This function is called by the DMA driver. */
88 static void atmel_ac97c_dma_playback_period_done(void *arg
)
90 struct atmel_ac97c
*chip
= arg
;
91 snd_pcm_period_elapsed(chip
->playback_substream
);
94 static void atmel_ac97c_dma_capture_period_done(void *arg
)
96 struct atmel_ac97c
*chip
= arg
;
97 snd_pcm_period_elapsed(chip
->capture_substream
);
100 static int atmel_ac97c_prepare_dma(struct atmel_ac97c
*chip
,
101 struct snd_pcm_substream
*substream
,
102 enum dma_data_direction direction
)
104 struct dma_chan
*chan
;
105 struct dw_cyclic_desc
*cdesc
;
106 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
107 unsigned long buffer_len
, period_len
;
110 * We don't do DMA on "complex" transfers, i.e. with
111 * non-halfword-aligned buffers or lengths.
113 if (runtime
->dma_addr
& 1 || runtime
->buffer_size
& 1) {
114 dev_dbg(&chip
->pdev
->dev
, "too complex transfer\n");
118 if (direction
== DMA_TO_DEVICE
)
119 chan
= chip
->dma
.tx_chan
;
121 chan
= chip
->dma
.rx_chan
;
123 buffer_len
= frames_to_bytes(runtime
, runtime
->buffer_size
);
124 period_len
= frames_to_bytes(runtime
, runtime
->period_size
);
126 cdesc
= dw_dma_cyclic_prep(chan
, runtime
->dma_addr
, buffer_len
,
127 period_len
, direction
);
129 dev_dbg(&chip
->pdev
->dev
, "could not prepare cyclic DMA\n");
130 return PTR_ERR(cdesc
);
133 if (direction
== DMA_TO_DEVICE
) {
134 cdesc
->period_callback
= atmel_ac97c_dma_playback_period_done
;
135 set_bit(DMA_TX_READY
, &chip
->flags
);
137 cdesc
->period_callback
= atmel_ac97c_dma_capture_period_done
;
138 set_bit(DMA_RX_READY
, &chip
->flags
);
141 cdesc
->period_callback_param
= chip
;
146 static struct snd_pcm_hardware atmel_ac97c_hw
= {
147 .info
= (SNDRV_PCM_INFO_MMAP
148 | SNDRV_PCM_INFO_MMAP_VALID
149 | SNDRV_PCM_INFO_INTERLEAVED
150 | SNDRV_PCM_INFO_BLOCK_TRANSFER
151 | SNDRV_PCM_INFO_JOINT_DUPLEX
152 | SNDRV_PCM_INFO_RESUME
153 | SNDRV_PCM_INFO_PAUSE
),
154 .formats
= (SNDRV_PCM_FMTBIT_S16_BE
155 | SNDRV_PCM_FMTBIT_S16_LE
),
156 .rates
= (SNDRV_PCM_RATE_CONTINUOUS
),
161 .buffer_bytes_max
= 2 * 2 * 64 * 2048,
162 .period_bytes_min
= 4096,
163 .period_bytes_max
= 4096,
168 static int atmel_ac97c_playback_open(struct snd_pcm_substream
*substream
)
170 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
171 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
173 mutex_lock(&opened_mutex
);
175 runtime
->hw
= atmel_ac97c_hw
;
176 if (chip
->cur_rate
) {
177 runtime
->hw
.rate_min
= chip
->cur_rate
;
178 runtime
->hw
.rate_max
= chip
->cur_rate
;
180 if (chip
->cur_format
)
181 runtime
->hw
.formats
= (1ULL << chip
->cur_format
);
182 mutex_unlock(&opened_mutex
);
183 chip
->playback_substream
= substream
;
187 static int atmel_ac97c_capture_open(struct snd_pcm_substream
*substream
)
189 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
190 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
192 mutex_lock(&opened_mutex
);
194 runtime
->hw
= atmel_ac97c_hw
;
195 if (chip
->cur_rate
) {
196 runtime
->hw
.rate_min
= chip
->cur_rate
;
197 runtime
->hw
.rate_max
= chip
->cur_rate
;
199 if (chip
->cur_format
)
200 runtime
->hw
.formats
= (1ULL << chip
->cur_format
);
201 mutex_unlock(&opened_mutex
);
202 chip
->capture_substream
= substream
;
206 static int atmel_ac97c_playback_close(struct snd_pcm_substream
*substream
)
208 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
210 mutex_lock(&opened_mutex
);
214 chip
->cur_format
= 0;
216 mutex_unlock(&opened_mutex
);
218 chip
->playback_substream
= NULL
;
223 static int atmel_ac97c_capture_close(struct snd_pcm_substream
*substream
)
225 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
227 mutex_lock(&opened_mutex
);
231 chip
->cur_format
= 0;
233 mutex_unlock(&opened_mutex
);
235 chip
->capture_substream
= NULL
;
240 static int atmel_ac97c_playback_hw_params(struct snd_pcm_substream
*substream
,
241 struct snd_pcm_hw_params
*hw_params
)
243 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
246 retval
= snd_pcm_lib_malloc_pages(substream
,
247 params_buffer_bytes(hw_params
));
250 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
251 if (cpu_is_at32ap7000()) {
252 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
254 if (test_and_clear_bit(DMA_TX_READY
, &chip
->flags
))
255 dw_dma_cyclic_free(chip
->dma
.tx_chan
);
257 /* Set restrictions to params. */
258 mutex_lock(&opened_mutex
);
259 chip
->cur_rate
= params_rate(hw_params
);
260 chip
->cur_format
= params_format(hw_params
);
261 mutex_unlock(&opened_mutex
);
266 static int atmel_ac97c_capture_hw_params(struct snd_pcm_substream
*substream
,
267 struct snd_pcm_hw_params
*hw_params
)
269 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
272 retval
= snd_pcm_lib_malloc_pages(substream
,
273 params_buffer_bytes(hw_params
));
276 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
277 if (cpu_is_at32ap7000()) {
280 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
282 if (test_and_clear_bit(DMA_RX_READY
, &chip
->flags
))
283 dw_dma_cyclic_free(chip
->dma
.rx_chan
);
286 /* Set restrictions to params. */
287 mutex_lock(&opened_mutex
);
288 chip
->cur_rate
= params_rate(hw_params
);
289 chip
->cur_format
= params_format(hw_params
);
290 mutex_unlock(&opened_mutex
);
295 static int atmel_ac97c_playback_hw_free(struct snd_pcm_substream
*substream
)
297 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
298 if (cpu_is_at32ap7000()) {
299 if (test_and_clear_bit(DMA_TX_READY
, &chip
->flags
))
300 dw_dma_cyclic_free(chip
->dma
.tx_chan
);
302 return snd_pcm_lib_free_pages(substream
);
305 static int atmel_ac97c_capture_hw_free(struct snd_pcm_substream
*substream
)
307 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
308 if (cpu_is_at32ap7000()) {
309 if (test_and_clear_bit(DMA_RX_READY
, &chip
->flags
))
310 dw_dma_cyclic_free(chip
->dma
.rx_chan
);
312 return snd_pcm_lib_free_pages(substream
);
315 static int atmel_ac97c_playback_prepare(struct snd_pcm_substream
*substream
)
317 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
318 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
319 int block_size
= frames_to_bytes(runtime
, runtime
->period_size
);
320 unsigned long word
= ac97c_readl(chip
, OCA
);
323 chip
->playback_period
= 0;
324 word
&= ~(AC97C_CH_MASK(PCM_LEFT
) | AC97C_CH_MASK(PCM_RIGHT
));
326 /* assign channels to AC97C channel A */
327 switch (runtime
->channels
) {
329 word
|= AC97C_CH_ASSIGN(PCM_LEFT
, A
);
332 word
|= AC97C_CH_ASSIGN(PCM_LEFT
, A
)
333 | AC97C_CH_ASSIGN(PCM_RIGHT
, A
);
336 /* TODO: support more than two channels */
339 ac97c_writel(chip
, OCA
, word
);
341 /* configure sample format and size */
342 word
= ac97c_readl(chip
, CAMR
);
343 if (chip
->opened
<= 1)
344 word
= AC97C_CMR_DMAEN
| AC97C_CMR_SIZE_16
;
346 word
|= AC97C_CMR_DMAEN
| AC97C_CMR_SIZE_16
;
348 switch (runtime
->format
) {
349 case SNDRV_PCM_FORMAT_S16_LE
:
350 if (cpu_is_at32ap7000())
351 word
|= AC97C_CMR_CEM_LITTLE
;
353 case SNDRV_PCM_FORMAT_S16_BE
: /* fall through */
354 word
&= ~(AC97C_CMR_CEM_LITTLE
);
357 word
= ac97c_readl(chip
, OCA
);
358 word
&= ~(AC97C_CH_MASK(PCM_LEFT
) | AC97C_CH_MASK(PCM_RIGHT
));
359 ac97c_writel(chip
, OCA
, word
);
363 /* Enable underrun interrupt on channel A */
364 word
|= AC97C_CSR_UNRUN
;
366 ac97c_writel(chip
, CAMR
, word
);
368 /* Enable channel A event interrupt */
369 word
= ac97c_readl(chip
, IMR
);
370 word
|= AC97C_SR_CAEVT
;
371 ac97c_writel(chip
, IER
, word
);
373 /* set variable rate if needed */
374 if (runtime
->rate
!= 48000) {
375 word
= ac97c_readl(chip
, MR
);
376 word
|= AC97C_MR_VRA
;
377 ac97c_writel(chip
, MR
, word
);
379 word
= ac97c_readl(chip
, MR
);
380 word
&= ~(AC97C_MR_VRA
);
381 ac97c_writel(chip
, MR
, word
);
384 retval
= snd_ac97_set_rate(chip
->ac97
, AC97_PCM_FRONT_DAC_RATE
,
387 dev_dbg(&chip
->pdev
->dev
, "could not set rate %d Hz\n",
390 if (cpu_is_at32ap7000()) {
391 if (!test_bit(DMA_TX_READY
, &chip
->flags
))
392 retval
= atmel_ac97c_prepare_dma(chip
, substream
,
395 /* Initialize and start the PDC */
396 writel(runtime
->dma_addr
, chip
->regs
+ ATMEL_PDC_TPR
);
397 writel(block_size
/ 2, chip
->regs
+ ATMEL_PDC_TCR
);
398 writel(runtime
->dma_addr
+ block_size
,
399 chip
->regs
+ ATMEL_PDC_TNPR
);
400 writel(block_size
/ 2, chip
->regs
+ ATMEL_PDC_TNCR
);
406 static int atmel_ac97c_capture_prepare(struct snd_pcm_substream
*substream
)
408 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
409 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
410 int block_size
= frames_to_bytes(runtime
, runtime
->period_size
);
411 unsigned long word
= ac97c_readl(chip
, ICA
);
414 chip
->capture_period
= 0;
415 word
&= ~(AC97C_CH_MASK(PCM_LEFT
) | AC97C_CH_MASK(PCM_RIGHT
));
417 /* assign channels to AC97C channel A */
418 switch (runtime
->channels
) {
420 word
|= AC97C_CH_ASSIGN(PCM_LEFT
, A
);
423 word
|= AC97C_CH_ASSIGN(PCM_LEFT
, A
)
424 | AC97C_CH_ASSIGN(PCM_RIGHT
, A
);
427 /* TODO: support more than two channels */
430 ac97c_writel(chip
, ICA
, word
);
432 /* configure sample format and size */
433 word
= ac97c_readl(chip
, CAMR
);
434 if (chip
->opened
<= 1)
435 word
= AC97C_CMR_DMAEN
| AC97C_CMR_SIZE_16
;
437 word
|= AC97C_CMR_DMAEN
| AC97C_CMR_SIZE_16
;
439 switch (runtime
->format
) {
440 case SNDRV_PCM_FORMAT_S16_LE
:
441 if (cpu_is_at32ap7000())
442 word
|= AC97C_CMR_CEM_LITTLE
;
444 case SNDRV_PCM_FORMAT_S16_BE
: /* fall through */
445 word
&= ~(AC97C_CMR_CEM_LITTLE
);
448 word
= ac97c_readl(chip
, ICA
);
449 word
&= ~(AC97C_CH_MASK(PCM_LEFT
) | AC97C_CH_MASK(PCM_RIGHT
));
450 ac97c_writel(chip
, ICA
, word
);
454 /* Enable overrun interrupt on channel A */
455 word
|= AC97C_CSR_OVRUN
;
457 ac97c_writel(chip
, CAMR
, word
);
459 /* Enable channel A event interrupt */
460 word
= ac97c_readl(chip
, IMR
);
461 word
|= AC97C_SR_CAEVT
;
462 ac97c_writel(chip
, IER
, word
);
464 /* set variable rate if needed */
465 if (runtime
->rate
!= 48000) {
466 word
= ac97c_readl(chip
, MR
);
467 word
|= AC97C_MR_VRA
;
468 ac97c_writel(chip
, MR
, word
);
470 word
= ac97c_readl(chip
, MR
);
471 word
&= ~(AC97C_MR_VRA
);
472 ac97c_writel(chip
, MR
, word
);
475 retval
= snd_ac97_set_rate(chip
->ac97
, AC97_PCM_LR_ADC_RATE
,
478 dev_dbg(&chip
->pdev
->dev
, "could not set rate %d Hz\n",
481 if (cpu_is_at32ap7000()) {
482 if (!test_bit(DMA_RX_READY
, &chip
->flags
))
483 retval
= atmel_ac97c_prepare_dma(chip
, substream
,
486 /* Initialize and start the PDC */
487 writel(runtime
->dma_addr
, chip
->regs
+ ATMEL_PDC_RPR
);
488 writel(block_size
/ 2, chip
->regs
+ ATMEL_PDC_RCR
);
489 writel(runtime
->dma_addr
+ block_size
,
490 chip
->regs
+ ATMEL_PDC_RNPR
);
491 writel(block_size
/ 2, chip
->regs
+ ATMEL_PDC_RNCR
);
498 atmel_ac97c_playback_trigger(struct snd_pcm_substream
*substream
, int cmd
)
500 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
501 unsigned long camr
, ptcr
= 0;
504 camr
= ac97c_readl(chip
, CAMR
);
507 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
: /* fall through */
508 case SNDRV_PCM_TRIGGER_RESUME
: /* fall through */
509 case SNDRV_PCM_TRIGGER_START
:
510 if (cpu_is_at32ap7000()) {
511 retval
= dw_dma_cyclic_start(chip
->dma
.tx_chan
);
515 ptcr
= ATMEL_PDC_TXTEN
;
517 camr
|= AC97C_CMR_CENA
| AC97C_CSR_ENDTX
;
519 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
: /* fall through */
520 case SNDRV_PCM_TRIGGER_SUSPEND
: /* fall through */
521 case SNDRV_PCM_TRIGGER_STOP
:
522 if (cpu_is_at32ap7000())
523 dw_dma_cyclic_stop(chip
->dma
.tx_chan
);
525 ptcr
|= ATMEL_PDC_TXTDIS
;
526 if (chip
->opened
<= 1)
527 camr
&= ~AC97C_CMR_CENA
;
534 ac97c_writel(chip
, CAMR
, camr
);
535 if (!cpu_is_at32ap7000())
536 writel(ptcr
, chip
->regs
+ ATMEL_PDC_PTCR
);
542 atmel_ac97c_capture_trigger(struct snd_pcm_substream
*substream
, int cmd
)
544 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
545 unsigned long camr
, ptcr
= 0;
548 camr
= ac97c_readl(chip
, CAMR
);
549 ptcr
= readl(chip
->regs
+ ATMEL_PDC_PTSR
);
552 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
: /* fall through */
553 case SNDRV_PCM_TRIGGER_RESUME
: /* fall through */
554 case SNDRV_PCM_TRIGGER_START
:
555 if (cpu_is_at32ap7000()) {
556 retval
= dw_dma_cyclic_start(chip
->dma
.rx_chan
);
560 ptcr
= ATMEL_PDC_RXTEN
;
562 camr
|= AC97C_CMR_CENA
| AC97C_CSR_ENDRX
;
564 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
: /* fall through */
565 case SNDRV_PCM_TRIGGER_SUSPEND
: /* fall through */
566 case SNDRV_PCM_TRIGGER_STOP
:
567 if (cpu_is_at32ap7000())
568 dw_dma_cyclic_stop(chip
->dma
.rx_chan
);
570 ptcr
|= (ATMEL_PDC_RXTDIS
);
571 if (chip
->opened
<= 1)
572 camr
&= ~AC97C_CMR_CENA
;
579 ac97c_writel(chip
, CAMR
, camr
);
580 if (!cpu_is_at32ap7000())
581 writel(ptcr
, chip
->regs
+ ATMEL_PDC_PTCR
);
586 static snd_pcm_uframes_t
587 atmel_ac97c_playback_pointer(struct snd_pcm_substream
*substream
)
589 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
590 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
591 snd_pcm_uframes_t frames
;
594 if (cpu_is_at32ap7000())
595 bytes
= dw_dma_get_src_addr(chip
->dma
.tx_chan
);
597 bytes
= readl(chip
->regs
+ ATMEL_PDC_TPR
);
598 bytes
-= runtime
->dma_addr
;
600 frames
= bytes_to_frames(runtime
, bytes
);
601 if (frames
>= runtime
->buffer_size
)
602 frames
-= runtime
->buffer_size
;
606 static snd_pcm_uframes_t
607 atmel_ac97c_capture_pointer(struct snd_pcm_substream
*substream
)
609 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
610 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
611 snd_pcm_uframes_t frames
;
614 if (cpu_is_at32ap7000())
615 bytes
= dw_dma_get_dst_addr(chip
->dma
.rx_chan
);
617 bytes
= readl(chip
->regs
+ ATMEL_PDC_RPR
);
618 bytes
-= runtime
->dma_addr
;
620 frames
= bytes_to_frames(runtime
, bytes
);
621 if (frames
>= runtime
->buffer_size
)
622 frames
-= runtime
->buffer_size
;
626 static struct snd_pcm_ops atmel_ac97_playback_ops
= {
627 .open
= atmel_ac97c_playback_open
,
628 .close
= atmel_ac97c_playback_close
,
629 .ioctl
= snd_pcm_lib_ioctl
,
630 .hw_params
= atmel_ac97c_playback_hw_params
,
631 .hw_free
= atmel_ac97c_playback_hw_free
,
632 .prepare
= atmel_ac97c_playback_prepare
,
633 .trigger
= atmel_ac97c_playback_trigger
,
634 .pointer
= atmel_ac97c_playback_pointer
,
637 static struct snd_pcm_ops atmel_ac97_capture_ops
= {
638 .open
= atmel_ac97c_capture_open
,
639 .close
= atmel_ac97c_capture_close
,
640 .ioctl
= snd_pcm_lib_ioctl
,
641 .hw_params
= atmel_ac97c_capture_hw_params
,
642 .hw_free
= atmel_ac97c_capture_hw_free
,
643 .prepare
= atmel_ac97c_capture_prepare
,
644 .trigger
= atmel_ac97c_capture_trigger
,
645 .pointer
= atmel_ac97c_capture_pointer
,
648 static irqreturn_t
atmel_ac97c_interrupt(int irq
, void *dev
)
650 struct atmel_ac97c
*chip
= (struct atmel_ac97c
*)dev
;
651 irqreturn_t retval
= IRQ_NONE
;
652 u32 sr
= ac97c_readl(chip
, SR
);
653 u32 casr
= ac97c_readl(chip
, CASR
);
654 u32 cosr
= ac97c_readl(chip
, COSR
);
655 u32 camr
= ac97c_readl(chip
, CAMR
);
657 if (sr
& AC97C_SR_CAEVT
) {
658 struct snd_pcm_runtime
*runtime
;
659 int offset
, next_period
, block_size
;
660 dev_dbg(&chip
->pdev
->dev
, "channel A event%s%s%s%s%s%s\n",
661 casr
& AC97C_CSR_OVRUN
? " OVRUN" : "",
662 casr
& AC97C_CSR_RXRDY
? " RXRDY" : "",
663 casr
& AC97C_CSR_UNRUN
? " UNRUN" : "",
664 casr
& AC97C_CSR_TXEMPTY
? " TXEMPTY" : "",
665 casr
& AC97C_CSR_TXRDY
? " TXRDY" : "",
666 !casr
? " NONE" : "");
667 if (!cpu_is_at32ap7000()) {
668 if ((casr
& camr
) & AC97C_CSR_ENDTX
) {
669 runtime
= chip
->playback_substream
->runtime
;
670 block_size
= frames_to_bytes(runtime
,
671 runtime
->period_size
);
672 chip
->playback_period
++;
674 if (chip
->playback_period
== runtime
->periods
)
675 chip
->playback_period
= 0;
676 next_period
= chip
->playback_period
+ 1;
677 if (next_period
== runtime
->periods
)
680 offset
= block_size
* next_period
;
682 writel(runtime
->dma_addr
+ offset
,
683 chip
->regs
+ ATMEL_PDC_TNPR
);
684 writel(block_size
/ 2,
685 chip
->regs
+ ATMEL_PDC_TNCR
);
687 snd_pcm_period_elapsed(
688 chip
->playback_substream
);
690 if ((casr
& camr
) & AC97C_CSR_ENDRX
) {
691 runtime
= chip
->capture_substream
->runtime
;
692 block_size
= frames_to_bytes(runtime
,
693 runtime
->period_size
);
694 chip
->capture_period
++;
696 if (chip
->capture_period
== runtime
->periods
)
697 chip
->capture_period
= 0;
698 next_period
= chip
->capture_period
+ 1;
699 if (next_period
== runtime
->periods
)
702 offset
= block_size
* next_period
;
704 writel(runtime
->dma_addr
+ offset
,
705 chip
->regs
+ ATMEL_PDC_RNPR
);
706 writel(block_size
/ 2,
707 chip
->regs
+ ATMEL_PDC_RNCR
);
708 snd_pcm_period_elapsed(chip
->capture_substream
);
711 retval
= IRQ_HANDLED
;
714 if (sr
& AC97C_SR_COEVT
) {
715 dev_info(&chip
->pdev
->dev
, "codec channel event%s%s%s%s%s\n",
716 cosr
& AC97C_CSR_OVRUN
? " OVRUN" : "",
717 cosr
& AC97C_CSR_RXRDY
? " RXRDY" : "",
718 cosr
& AC97C_CSR_TXEMPTY
? " TXEMPTY" : "",
719 cosr
& AC97C_CSR_TXRDY
? " TXRDY" : "",
720 !cosr
? " NONE" : "");
721 retval
= IRQ_HANDLED
;
724 if (retval
== IRQ_NONE
) {
725 dev_err(&chip
->pdev
->dev
, "spurious interrupt sr 0x%08x "
726 "casr 0x%08x cosr 0x%08x\n", sr
, casr
, cosr
);
732 static struct ac97_pcm at91_ac97_pcm_defs
[] __devinitdata
= {
737 .slots
= ((1 << AC97_SLOT_PCM_LEFT
)
738 | (1 << AC97_SLOT_PCM_RIGHT
)),
746 .slots
= ((1 << AC97_SLOT_PCM_LEFT
)
747 | (1 << AC97_SLOT_PCM_RIGHT
)),
755 .slots
= (1<<AC97_SLOT_MIC
),
760 static int __devinit
atmel_ac97c_pcm_new(struct atmel_ac97c
*chip
)
763 struct snd_pcm_hardware hw
= atmel_ac97c_hw
;
764 int capture
, playback
, retval
, err
;
766 capture
= test_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
);
767 playback
= test_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
);
769 if (!cpu_is_at32ap7000()) {
770 err
= snd_ac97_pcm_assign(chip
->ac97_bus
,
771 ARRAY_SIZE(at91_ac97_pcm_defs
),
776 retval
= snd_pcm_new(chip
->card
, chip
->card
->shortname
,
777 chip
->pdev
->id
, playback
, capture
, &pcm
);
782 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
,
783 &atmel_ac97_capture_ops
);
785 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
786 &atmel_ac97_playback_ops
);
788 retval
= snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_DEV
,
789 &chip
->pdev
->dev
, hw
.periods_min
* hw
.period_bytes_min
,
790 hw
.buffer_bytes_max
);
794 pcm
->private_data
= chip
;
796 strcpy(pcm
->name
, chip
->card
->shortname
);
802 static int atmel_ac97c_mixer_new(struct atmel_ac97c
*chip
)
804 struct snd_ac97_template
template;
805 memset(&template, 0, sizeof(template));
806 template.private_data
= chip
;
807 return snd_ac97_mixer(chip
->ac97_bus
, &template, &chip
->ac97
);
810 static void atmel_ac97c_write(struct snd_ac97
*ac97
, unsigned short reg
,
813 struct atmel_ac97c
*chip
= get_chip(ac97
);
817 word
= (reg
& 0x7f) << 16 | val
;
820 if (ac97c_readl(chip
, COSR
) & AC97C_CSR_TXRDY
) {
821 ac97c_writel(chip
, COTHR
, word
);
827 dev_dbg(&chip
->pdev
->dev
, "codec write timeout\n");
830 static unsigned short atmel_ac97c_read(struct snd_ac97
*ac97
,
833 struct atmel_ac97c
*chip
= get_chip(ac97
);
838 word
= (0x80 | (reg
& 0x7f)) << 16;
840 if ((ac97c_readl(chip
, COSR
) & AC97C_CSR_RXRDY
) != 0)
841 ac97c_readl(chip
, CORHR
);
847 if ((ac97c_readl(chip
, COSR
) & AC97C_CSR_TXRDY
) != 0) {
848 ac97c_writel(chip
, COTHR
, word
);
860 if ((ac97c_readl(chip
, COSR
) & AC97C_CSR_RXRDY
) != 0) {
861 unsigned short val
= ac97c_readl(chip
, CORHR
);
872 dev_dbg(&chip
->pdev
->dev
, "codec read timeout\n");
876 static bool filter(struct dma_chan
*chan
, void *slave
)
878 struct dw_dma_slave
*dws
= slave
;
880 if (dws
->dma_dev
== chan
->device
->dev
) {
887 static void atmel_ac97c_reset(struct atmel_ac97c
*chip
)
889 ac97c_writel(chip
, MR
, 0);
890 ac97c_writel(chip
, MR
, AC97C_MR_ENA
);
891 ac97c_writel(chip
, CAMR
, 0);
892 ac97c_writel(chip
, COMR
, 0);
894 if (gpio_is_valid(chip
->reset_pin
)) {
895 gpio_set_value(chip
->reset_pin
, 0);
896 /* AC97 v2.2 specifications says minimum 1 us. */
898 gpio_set_value(chip
->reset_pin
, 1);
902 static int __devinit
atmel_ac97c_probe(struct platform_device
*pdev
)
904 struct snd_card
*card
;
905 struct atmel_ac97c
*chip
;
906 struct resource
*regs
;
907 struct ac97c_platform_data
*pdata
;
909 static struct snd_ac97_bus_ops ops
= {
910 .write
= atmel_ac97c_write
,
911 .read
= atmel_ac97c_read
,
916 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
918 dev_dbg(&pdev
->dev
, "no memory resource\n");
922 pdata
= pdev
->dev
.platform_data
;
924 dev_dbg(&pdev
->dev
, "no platform data\n");
928 irq
= platform_get_irq(pdev
, 0);
930 dev_dbg(&pdev
->dev
, "could not get irq\n");
934 if (cpu_is_at32ap7000()) {
935 pclk
= clk_get(&pdev
->dev
, "pclk");
937 pclk
= clk_get(&pdev
->dev
, "ac97_clk");
941 dev_dbg(&pdev
->dev
, "no peripheral clock\n");
942 return PTR_ERR(pclk
);
946 retval
= snd_card_create(SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
,
947 THIS_MODULE
, sizeof(struct atmel_ac97c
), &card
);
949 dev_dbg(&pdev
->dev
, "could not create sound card device\n");
950 goto err_snd_card_new
;
953 chip
= get_chip(card
);
955 retval
= request_irq(irq
, atmel_ac97c_interrupt
, 0, "AC97C", chip
);
957 dev_dbg(&pdev
->dev
, "unable to request irq %d\n", irq
);
958 goto err_request_irq
;
962 spin_lock_init(&chip
->lock
);
964 strcpy(card
->driver
, "Atmel AC97C");
965 strcpy(card
->shortname
, "Atmel AC97C");
966 sprintf(card
->longname
, "Atmel AC97 controller");
971 chip
->regs
= ioremap(regs
->start
, regs
->end
- regs
->start
+ 1);
974 dev_dbg(&pdev
->dev
, "could not remap register memory\n");
978 if (gpio_is_valid(pdata
->reset_pin
)) {
979 if (gpio_request(pdata
->reset_pin
, "reset_pin")) {
980 dev_dbg(&pdev
->dev
, "reset pin not available\n");
981 chip
->reset_pin
= -ENODEV
;
983 gpio_direction_output(pdata
->reset_pin
, 1);
984 chip
->reset_pin
= pdata
->reset_pin
;
988 snd_card_set_dev(card
, &pdev
->dev
);
990 atmel_ac97c_reset(chip
);
992 /* Enable overrun interrupt from codec channel */
993 ac97c_writel(chip
, COMR
, AC97C_CSR_OVRUN
);
994 ac97c_writel(chip
, IER
, ac97c_readl(chip
, IMR
) | AC97C_SR_COEVT
);
996 retval
= snd_ac97_bus(card
, 0, &ops
, chip
, &chip
->ac97_bus
);
998 dev_dbg(&pdev
->dev
, "could not register on ac97 bus\n");
1002 retval
= atmel_ac97c_mixer_new(chip
);
1004 dev_dbg(&pdev
->dev
, "could not register ac97 mixer\n");
1008 if (cpu_is_at32ap7000()) {
1009 if (pdata
->rx_dws
.dma_dev
) {
1010 struct dw_dma_slave
*dws
= &pdata
->rx_dws
;
1011 dma_cap_mask_t mask
;
1013 dws
->rx_reg
= regs
->start
+ AC97C_CARHR
+ 2;
1016 dma_cap_set(DMA_SLAVE
, mask
);
1018 chip
->dma
.rx_chan
= dma_request_channel(mask
, filter
,
1021 dev_info(&chip
->pdev
->dev
, "using %s for DMA RX\n",
1022 dev_name(&chip
->dma
.rx_chan
->dev
->device
));
1023 set_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
);
1026 if (pdata
->tx_dws
.dma_dev
) {
1027 struct dw_dma_slave
*dws
= &pdata
->tx_dws
;
1028 dma_cap_mask_t mask
;
1030 dws
->tx_reg
= regs
->start
+ AC97C_CATHR
+ 2;
1033 dma_cap_set(DMA_SLAVE
, mask
);
1035 chip
->dma
.tx_chan
= dma_request_channel(mask
, filter
,
1038 dev_info(&chip
->pdev
->dev
, "using %s for DMA TX\n",
1039 dev_name(&chip
->dma
.tx_chan
->dev
->device
));
1040 set_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
);
1043 if (!test_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
) &&
1044 !test_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
)) {
1045 dev_dbg(&pdev
->dev
, "DMA not available\n");
1050 /* Just pretend that we have DMA channel(for at91 i is actually
1052 set_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
);
1053 set_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
);
1056 retval
= atmel_ac97c_pcm_new(chip
);
1058 dev_dbg(&pdev
->dev
, "could not register ac97 pcm device\n");
1062 retval
= snd_card_register(card
);
1064 dev_dbg(&pdev
->dev
, "could not register sound card\n");
1068 platform_set_drvdata(pdev
, card
);
1070 dev_info(&pdev
->dev
, "Atmel AC97 controller at 0x%p, irq = %d\n",
1076 if (cpu_is_at32ap7000()) {
1077 if (test_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
))
1078 dma_release_channel(chip
->dma
.rx_chan
);
1079 if (test_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
))
1080 dma_release_channel(chip
->dma
.tx_chan
);
1081 clear_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
);
1082 clear_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
);
1083 chip
->dma
.rx_chan
= NULL
;
1084 chip
->dma
.tx_chan
= NULL
;
1087 snd_card_set_dev(card
, NULL
);
1089 if (gpio_is_valid(chip
->reset_pin
))
1090 gpio_free(chip
->reset_pin
);
1092 iounmap(chip
->regs
);
1094 free_irq(irq
, chip
);
1096 snd_card_free(card
);
1104 static int atmel_ac97c_suspend(struct platform_device
*pdev
, pm_message_t msg
)
1106 struct snd_card
*card
= platform_get_drvdata(pdev
);
1107 struct atmel_ac97c
*chip
= card
->private_data
;
1109 if (cpu_is_at32ap7000()) {
1110 if (test_bit(DMA_RX_READY
, &chip
->flags
))
1111 dw_dma_cyclic_stop(chip
->dma
.rx_chan
);
1112 if (test_bit(DMA_TX_READY
, &chip
->flags
))
1113 dw_dma_cyclic_stop(chip
->dma
.tx_chan
);
1115 clk_disable(chip
->pclk
);
1120 static int atmel_ac97c_resume(struct platform_device
*pdev
)
1122 struct snd_card
*card
= platform_get_drvdata(pdev
);
1123 struct atmel_ac97c
*chip
= card
->private_data
;
1125 clk_enable(chip
->pclk
);
1126 if (cpu_is_at32ap7000()) {
1127 if (test_bit(DMA_RX_READY
, &chip
->flags
))
1128 dw_dma_cyclic_start(chip
->dma
.rx_chan
);
1129 if (test_bit(DMA_TX_READY
, &chip
->flags
))
1130 dw_dma_cyclic_start(chip
->dma
.tx_chan
);
1135 #define atmel_ac97c_suspend NULL
1136 #define atmel_ac97c_resume NULL
1139 static int __devexit
atmel_ac97c_remove(struct platform_device
*pdev
)
1141 struct snd_card
*card
= platform_get_drvdata(pdev
);
1142 struct atmel_ac97c
*chip
= get_chip(card
);
1144 if (gpio_is_valid(chip
->reset_pin
))
1145 gpio_free(chip
->reset_pin
);
1147 ac97c_writel(chip
, CAMR
, 0);
1148 ac97c_writel(chip
, COMR
, 0);
1149 ac97c_writel(chip
, MR
, 0);
1151 clk_disable(chip
->pclk
);
1152 clk_put(chip
->pclk
);
1153 iounmap(chip
->regs
);
1154 free_irq(chip
->irq
, chip
);
1156 if (cpu_is_at32ap7000()) {
1157 if (test_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
))
1158 dma_release_channel(chip
->dma
.rx_chan
);
1159 if (test_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
))
1160 dma_release_channel(chip
->dma
.tx_chan
);
1161 clear_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
);
1162 clear_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
);
1163 chip
->dma
.rx_chan
= NULL
;
1164 chip
->dma
.tx_chan
= NULL
;
1167 snd_card_set_dev(card
, NULL
);
1168 snd_card_free(card
);
1170 platform_set_drvdata(pdev
, NULL
);
1175 static struct platform_driver atmel_ac97c_driver
= {
1176 .remove
= __devexit_p(atmel_ac97c_remove
),
1178 .name
= "atmel_ac97c",
1180 .suspend
= atmel_ac97c_suspend
,
1181 .resume
= atmel_ac97c_resume
,
1184 static int __init
atmel_ac97c_init(void)
1186 return platform_driver_probe(&atmel_ac97c_driver
,
1189 module_init(atmel_ac97c_init
);
1191 static void __exit
atmel_ac97c_exit(void)
1193 platform_driver_unregister(&atmel_ac97c_driver
);
1195 module_exit(atmel_ac97c_exit
);
1197 MODULE_LICENSE("GPL");
1198 MODULE_DESCRIPTION("Driver for Atmel AC97 controller");
1199 MODULE_AUTHOR("Hans-Christian Egtvedt <hans-christian.egtvedt@atmel.com>");