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/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/mutex.h>
21 #include <linux/gpio.h>
24 #include <sound/core.h>
25 #include <sound/initval.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/ac97_codec.h>
29 #include <sound/atmel-ac97c.h>
30 #include <sound/memalloc.h>
32 #include <linux/dw_dmac.h>
43 /* Serialize access to opened variable */
44 static DEFINE_MUTEX(opened_mutex
);
46 struct atmel_ac97c_dma
{
47 struct dma_chan
*rx_chan
;
48 struct dma_chan
*tx_chan
;
53 struct platform_device
*pdev
;
54 struct atmel_ac97c_dma dma
;
56 struct snd_pcm_substream
*playback_substream
;
57 struct snd_pcm_substream
*capture_substream
;
58 struct snd_card
*card
;
60 struct snd_ac97
*ac97
;
61 struct snd_ac97_bus
*ac97_bus
;
64 unsigned int cur_rate
;
66 /* Serialize access to opened variable */
74 #define get_chip(card) ((struct atmel_ac97c *)(card)->private_data)
76 #define ac97c_writel(chip, reg, val) \
77 __raw_writel((val), (chip)->regs + AC97C_##reg)
78 #define ac97c_readl(chip, reg) \
79 __raw_readl((chip)->regs + AC97C_##reg)
81 /* This function is called by the DMA driver. */
82 static void atmel_ac97c_dma_playback_period_done(void *arg
)
84 struct atmel_ac97c
*chip
= arg
;
85 snd_pcm_period_elapsed(chip
->playback_substream
);
88 static void atmel_ac97c_dma_capture_period_done(void *arg
)
90 struct atmel_ac97c
*chip
= arg
;
91 snd_pcm_period_elapsed(chip
->capture_substream
);
94 static int atmel_ac97c_prepare_dma(struct atmel_ac97c
*chip
,
95 struct snd_pcm_substream
*substream
,
96 enum dma_data_direction direction
)
98 struct dma_chan
*chan
;
99 struct dw_cyclic_desc
*cdesc
;
100 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
101 unsigned long buffer_len
, period_len
;
104 * We don't do DMA on "complex" transfers, i.e. with
105 * non-halfword-aligned buffers or lengths.
107 if (runtime
->dma_addr
& 1 || runtime
->buffer_size
& 1) {
108 dev_dbg(&chip
->pdev
->dev
, "too complex transfer\n");
112 if (direction
== DMA_TO_DEVICE
)
113 chan
= chip
->dma
.tx_chan
;
115 chan
= chip
->dma
.rx_chan
;
117 buffer_len
= frames_to_bytes(runtime
, runtime
->buffer_size
);
118 period_len
= frames_to_bytes(runtime
, runtime
->period_size
);
120 cdesc
= dw_dma_cyclic_prep(chan
, runtime
->dma_addr
, buffer_len
,
121 period_len
, direction
);
123 dev_dbg(&chip
->pdev
->dev
, "could not prepare cyclic DMA\n");
124 return PTR_ERR(cdesc
);
127 if (direction
== DMA_TO_DEVICE
) {
128 cdesc
->period_callback
= atmel_ac97c_dma_playback_period_done
;
129 set_bit(DMA_TX_READY
, &chip
->flags
);
131 cdesc
->period_callback
= atmel_ac97c_dma_capture_period_done
;
132 set_bit(DMA_RX_READY
, &chip
->flags
);
135 cdesc
->period_callback_param
= chip
;
140 static struct snd_pcm_hardware atmel_ac97c_hw
= {
141 .info
= (SNDRV_PCM_INFO_MMAP
142 | SNDRV_PCM_INFO_MMAP_VALID
143 | SNDRV_PCM_INFO_INTERLEAVED
144 | SNDRV_PCM_INFO_BLOCK_TRANSFER
145 | SNDRV_PCM_INFO_JOINT_DUPLEX
146 | SNDRV_PCM_INFO_RESUME
147 | SNDRV_PCM_INFO_PAUSE
),
148 .formats
= (SNDRV_PCM_FMTBIT_S16_BE
149 | SNDRV_PCM_FMTBIT_S16_LE
),
150 .rates
= (SNDRV_PCM_RATE_CONTINUOUS
),
155 .buffer_bytes_max
= 2 * 2 * 64 * 2048,
156 .period_bytes_min
= 4096,
157 .period_bytes_max
= 4096,
162 static int atmel_ac97c_playback_open(struct snd_pcm_substream
*substream
)
164 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
165 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
167 mutex_lock(&opened_mutex
);
169 runtime
->hw
= atmel_ac97c_hw
;
170 if (chip
->cur_rate
) {
171 runtime
->hw
.rate_min
= chip
->cur_rate
;
172 runtime
->hw
.rate_max
= chip
->cur_rate
;
174 if (chip
->cur_format
)
175 runtime
->hw
.formats
= (1ULL << chip
->cur_format
);
176 mutex_unlock(&opened_mutex
);
177 chip
->playback_substream
= substream
;
181 static int atmel_ac97c_capture_open(struct snd_pcm_substream
*substream
)
183 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
184 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
186 mutex_lock(&opened_mutex
);
188 runtime
->hw
= atmel_ac97c_hw
;
189 if (chip
->cur_rate
) {
190 runtime
->hw
.rate_min
= chip
->cur_rate
;
191 runtime
->hw
.rate_max
= chip
->cur_rate
;
193 if (chip
->cur_format
)
194 runtime
->hw
.formats
= (1ULL << chip
->cur_format
);
195 mutex_unlock(&opened_mutex
);
196 chip
->capture_substream
= substream
;
200 static int atmel_ac97c_playback_close(struct snd_pcm_substream
*substream
)
202 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
204 mutex_lock(&opened_mutex
);
208 chip
->cur_format
= 0;
210 mutex_unlock(&opened_mutex
);
212 chip
->playback_substream
= NULL
;
217 static int atmel_ac97c_capture_close(struct snd_pcm_substream
*substream
)
219 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
221 mutex_lock(&opened_mutex
);
225 chip
->cur_format
= 0;
227 mutex_unlock(&opened_mutex
);
229 chip
->capture_substream
= NULL
;
234 static int atmel_ac97c_playback_hw_params(struct snd_pcm_substream
*substream
,
235 struct snd_pcm_hw_params
*hw_params
)
237 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
240 retval
= snd_pcm_lib_malloc_pages(substream
,
241 params_buffer_bytes(hw_params
));
244 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
246 if (test_and_clear_bit(DMA_TX_READY
, &chip
->flags
))
247 dw_dma_cyclic_free(chip
->dma
.tx_chan
);
249 /* Set restrictions to params. */
250 mutex_lock(&opened_mutex
);
251 chip
->cur_rate
= params_rate(hw_params
);
252 chip
->cur_format
= params_format(hw_params
);
253 mutex_unlock(&opened_mutex
);
258 static int atmel_ac97c_capture_hw_params(struct snd_pcm_substream
*substream
,
259 struct snd_pcm_hw_params
*hw_params
)
261 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
264 retval
= snd_pcm_lib_malloc_pages(substream
,
265 params_buffer_bytes(hw_params
));
268 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
270 if (test_and_clear_bit(DMA_RX_READY
, &chip
->flags
))
271 dw_dma_cyclic_free(chip
->dma
.rx_chan
);
273 /* Set restrictions to params. */
274 mutex_lock(&opened_mutex
);
275 chip
->cur_rate
= params_rate(hw_params
);
276 chip
->cur_format
= params_format(hw_params
);
277 mutex_unlock(&opened_mutex
);
282 static int atmel_ac97c_playback_hw_free(struct snd_pcm_substream
*substream
)
284 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
285 if (test_and_clear_bit(DMA_TX_READY
, &chip
->flags
))
286 dw_dma_cyclic_free(chip
->dma
.tx_chan
);
287 return snd_pcm_lib_free_pages(substream
);
290 static int atmel_ac97c_capture_hw_free(struct snd_pcm_substream
*substream
)
292 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
293 if (test_and_clear_bit(DMA_RX_READY
, &chip
->flags
))
294 dw_dma_cyclic_free(chip
->dma
.rx_chan
);
295 return snd_pcm_lib_free_pages(substream
);
298 static int atmel_ac97c_playback_prepare(struct snd_pcm_substream
*substream
)
300 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
301 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
302 unsigned long word
= ac97c_readl(chip
, OCA
);
305 word
&= ~(AC97C_CH_MASK(PCM_LEFT
) | AC97C_CH_MASK(PCM_RIGHT
));
307 /* assign channels to AC97C channel A */
308 switch (runtime
->channels
) {
310 word
|= AC97C_CH_ASSIGN(PCM_LEFT
, A
);
313 word
|= AC97C_CH_ASSIGN(PCM_LEFT
, A
)
314 | AC97C_CH_ASSIGN(PCM_RIGHT
, A
);
317 /* TODO: support more than two channels */
320 ac97c_writel(chip
, OCA
, word
);
322 /* configure sample format and size */
323 word
= AC97C_CMR_DMAEN
| AC97C_CMR_SIZE_16
;
325 switch (runtime
->format
) {
326 case SNDRV_PCM_FORMAT_S16_LE
:
327 word
|= AC97C_CMR_CEM_LITTLE
;
329 case SNDRV_PCM_FORMAT_S16_BE
: /* fall through */
330 word
&= ~(AC97C_CMR_CEM_LITTLE
);
333 word
= ac97c_readl(chip
, OCA
);
334 word
&= ~(AC97C_CH_MASK(PCM_LEFT
) | AC97C_CH_MASK(PCM_RIGHT
));
335 ac97c_writel(chip
, OCA
, word
);
339 /* Enable underrun interrupt on channel A */
340 word
|= AC97C_CSR_UNRUN
;
342 ac97c_writel(chip
, CAMR
, word
);
344 /* Enable channel A event interrupt */
345 word
= ac97c_readl(chip
, IMR
);
346 word
|= AC97C_SR_CAEVT
;
347 ac97c_writel(chip
, IER
, word
);
349 /* set variable rate if needed */
350 if (runtime
->rate
!= 48000) {
351 word
= ac97c_readl(chip
, MR
);
352 word
|= AC97C_MR_VRA
;
353 ac97c_writel(chip
, MR
, word
);
355 word
= ac97c_readl(chip
, MR
);
356 word
&= ~(AC97C_MR_VRA
);
357 ac97c_writel(chip
, MR
, word
);
360 retval
= snd_ac97_set_rate(chip
->ac97
, AC97_PCM_FRONT_DAC_RATE
,
363 dev_dbg(&chip
->pdev
->dev
, "could not set rate %d Hz\n",
366 if (!test_bit(DMA_TX_READY
, &chip
->flags
))
367 retval
= atmel_ac97c_prepare_dma(chip
, substream
,
373 static int atmel_ac97c_capture_prepare(struct snd_pcm_substream
*substream
)
375 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
376 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
377 unsigned long word
= ac97c_readl(chip
, ICA
);
380 word
&= ~(AC97C_CH_MASK(PCM_LEFT
) | AC97C_CH_MASK(PCM_RIGHT
));
382 /* assign channels to AC97C channel A */
383 switch (runtime
->channels
) {
385 word
|= AC97C_CH_ASSIGN(PCM_LEFT
, A
);
388 word
|= AC97C_CH_ASSIGN(PCM_LEFT
, A
)
389 | AC97C_CH_ASSIGN(PCM_RIGHT
, A
);
392 /* TODO: support more than two channels */
395 ac97c_writel(chip
, ICA
, word
);
397 /* configure sample format and size */
398 word
= AC97C_CMR_DMAEN
| AC97C_CMR_SIZE_16
;
400 switch (runtime
->format
) {
401 case SNDRV_PCM_FORMAT_S16_LE
:
402 word
|= AC97C_CMR_CEM_LITTLE
;
404 case SNDRV_PCM_FORMAT_S16_BE
: /* fall through */
405 word
&= ~(AC97C_CMR_CEM_LITTLE
);
408 word
= ac97c_readl(chip
, ICA
);
409 word
&= ~(AC97C_CH_MASK(PCM_LEFT
) | AC97C_CH_MASK(PCM_RIGHT
));
410 ac97c_writel(chip
, ICA
, word
);
414 /* Enable overrun interrupt on channel A */
415 word
|= AC97C_CSR_OVRUN
;
417 ac97c_writel(chip
, CAMR
, word
);
419 /* Enable channel A event interrupt */
420 word
= ac97c_readl(chip
, IMR
);
421 word
|= AC97C_SR_CAEVT
;
422 ac97c_writel(chip
, IER
, word
);
424 /* set variable rate if needed */
425 if (runtime
->rate
!= 48000) {
426 word
= ac97c_readl(chip
, MR
);
427 word
|= AC97C_MR_VRA
;
428 ac97c_writel(chip
, MR
, word
);
430 word
= ac97c_readl(chip
, MR
);
431 word
&= ~(AC97C_MR_VRA
);
432 ac97c_writel(chip
, MR
, word
);
435 retval
= snd_ac97_set_rate(chip
->ac97
, AC97_PCM_LR_ADC_RATE
,
438 dev_dbg(&chip
->pdev
->dev
, "could not set rate %d Hz\n",
441 if (!test_bit(DMA_RX_READY
, &chip
->flags
))
442 retval
= atmel_ac97c_prepare_dma(chip
, substream
,
449 atmel_ac97c_playback_trigger(struct snd_pcm_substream
*substream
, int cmd
)
451 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
455 camr
= ac97c_readl(chip
, CAMR
);
458 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
: /* fall through */
459 case SNDRV_PCM_TRIGGER_RESUME
: /* fall through */
460 case SNDRV_PCM_TRIGGER_START
:
461 retval
= dw_dma_cyclic_start(chip
->dma
.tx_chan
);
464 camr
|= AC97C_CMR_CENA
;
466 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
: /* fall through */
467 case SNDRV_PCM_TRIGGER_SUSPEND
: /* fall through */
468 case SNDRV_PCM_TRIGGER_STOP
:
469 dw_dma_cyclic_stop(chip
->dma
.tx_chan
);
470 if (chip
->opened
<= 1)
471 camr
&= ~AC97C_CMR_CENA
;
478 ac97c_writel(chip
, CAMR
, camr
);
484 atmel_ac97c_capture_trigger(struct snd_pcm_substream
*substream
, int cmd
)
486 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
490 camr
= ac97c_readl(chip
, CAMR
);
493 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
: /* fall through */
494 case SNDRV_PCM_TRIGGER_RESUME
: /* fall through */
495 case SNDRV_PCM_TRIGGER_START
:
496 retval
= dw_dma_cyclic_start(chip
->dma
.rx_chan
);
499 camr
|= AC97C_CMR_CENA
;
501 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
: /* fall through */
502 case SNDRV_PCM_TRIGGER_SUSPEND
: /* fall through */
503 case SNDRV_PCM_TRIGGER_STOP
:
504 dw_dma_cyclic_stop(chip
->dma
.rx_chan
);
505 if (chip
->opened
<= 1)
506 camr
&= ~AC97C_CMR_CENA
;
513 ac97c_writel(chip
, CAMR
, camr
);
518 static snd_pcm_uframes_t
519 atmel_ac97c_playback_pointer(struct snd_pcm_substream
*substream
)
521 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
522 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
523 snd_pcm_uframes_t frames
;
526 bytes
= dw_dma_get_src_addr(chip
->dma
.tx_chan
);
527 bytes
-= runtime
->dma_addr
;
529 frames
= bytes_to_frames(runtime
, bytes
);
530 if (frames
>= runtime
->buffer_size
)
531 frames
-= runtime
->buffer_size
;
535 static snd_pcm_uframes_t
536 atmel_ac97c_capture_pointer(struct snd_pcm_substream
*substream
)
538 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
539 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
540 snd_pcm_uframes_t frames
;
543 bytes
= dw_dma_get_dst_addr(chip
->dma
.rx_chan
);
544 bytes
-= runtime
->dma_addr
;
546 frames
= bytes_to_frames(runtime
, bytes
);
547 if (frames
>= runtime
->buffer_size
)
548 frames
-= runtime
->buffer_size
;
552 static struct snd_pcm_ops atmel_ac97_playback_ops
= {
553 .open
= atmel_ac97c_playback_open
,
554 .close
= atmel_ac97c_playback_close
,
555 .ioctl
= snd_pcm_lib_ioctl
,
556 .hw_params
= atmel_ac97c_playback_hw_params
,
557 .hw_free
= atmel_ac97c_playback_hw_free
,
558 .prepare
= atmel_ac97c_playback_prepare
,
559 .trigger
= atmel_ac97c_playback_trigger
,
560 .pointer
= atmel_ac97c_playback_pointer
,
563 static struct snd_pcm_ops atmel_ac97_capture_ops
= {
564 .open
= atmel_ac97c_capture_open
,
565 .close
= atmel_ac97c_capture_close
,
566 .ioctl
= snd_pcm_lib_ioctl
,
567 .hw_params
= atmel_ac97c_capture_hw_params
,
568 .hw_free
= atmel_ac97c_capture_hw_free
,
569 .prepare
= atmel_ac97c_capture_prepare
,
570 .trigger
= atmel_ac97c_capture_trigger
,
571 .pointer
= atmel_ac97c_capture_pointer
,
574 static irqreturn_t
atmel_ac97c_interrupt(int irq
, void *dev
)
576 struct atmel_ac97c
*chip
= (struct atmel_ac97c
*)dev
;
577 irqreturn_t retval
= IRQ_NONE
;
578 u32 sr
= ac97c_readl(chip
, SR
);
579 u32 casr
= ac97c_readl(chip
, CASR
);
580 u32 cosr
= ac97c_readl(chip
, COSR
);
582 if (sr
& AC97C_SR_CAEVT
) {
583 dev_info(&chip
->pdev
->dev
, "channel A event%s%s%s%s%s%s\n",
584 casr
& AC97C_CSR_OVRUN
? " OVRUN" : "",
585 casr
& AC97C_CSR_RXRDY
? " RXRDY" : "",
586 casr
& AC97C_CSR_UNRUN
? " UNRUN" : "",
587 casr
& AC97C_CSR_TXEMPTY
? " TXEMPTY" : "",
588 casr
& AC97C_CSR_TXRDY
? " TXRDY" : "",
589 !casr
? " NONE" : "");
590 retval
= IRQ_HANDLED
;
593 if (sr
& AC97C_SR_COEVT
) {
594 dev_info(&chip
->pdev
->dev
, "codec channel event%s%s%s%s%s\n",
595 cosr
& AC97C_CSR_OVRUN
? " OVRUN" : "",
596 cosr
& AC97C_CSR_RXRDY
? " RXRDY" : "",
597 cosr
& AC97C_CSR_TXEMPTY
? " TXEMPTY" : "",
598 cosr
& AC97C_CSR_TXRDY
? " TXRDY" : "",
599 !cosr
? " NONE" : "");
600 retval
= IRQ_HANDLED
;
603 if (retval
== IRQ_NONE
) {
604 dev_err(&chip
->pdev
->dev
, "spurious interrupt sr 0x%08x "
605 "casr 0x%08x cosr 0x%08x\n", sr
, casr
, cosr
);
611 static int __devinit
atmel_ac97c_pcm_new(struct atmel_ac97c
*chip
)
614 struct snd_pcm_hardware hw
= atmel_ac97c_hw
;
615 int capture
, playback
, retval
;
617 capture
= test_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
);
618 playback
= test_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
);
620 retval
= snd_pcm_new(chip
->card
, chip
->card
->shortname
,
621 chip
->pdev
->id
, playback
, capture
, &pcm
);
626 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
,
627 &atmel_ac97_capture_ops
);
629 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
630 &atmel_ac97_playback_ops
);
632 retval
= snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_DEV
,
633 &chip
->pdev
->dev
, hw
.periods_min
* hw
.period_bytes_min
,
634 hw
.buffer_bytes_max
);
638 pcm
->private_data
= chip
;
640 strcpy(pcm
->name
, chip
->card
->shortname
);
646 static int atmel_ac97c_mixer_new(struct atmel_ac97c
*chip
)
648 struct snd_ac97_template
template;
649 memset(&template, 0, sizeof(template));
650 template.private_data
= chip
;
651 return snd_ac97_mixer(chip
->ac97_bus
, &template, &chip
->ac97
);
654 static void atmel_ac97c_write(struct snd_ac97
*ac97
, unsigned short reg
,
657 struct atmel_ac97c
*chip
= get_chip(ac97
);
661 word
= (reg
& 0x7f) << 16 | val
;
664 if (ac97c_readl(chip
, COSR
) & AC97C_CSR_TXRDY
) {
665 ac97c_writel(chip
, COTHR
, word
);
671 dev_dbg(&chip
->pdev
->dev
, "codec write timeout\n");
674 static unsigned short atmel_ac97c_read(struct snd_ac97
*ac97
,
677 struct atmel_ac97c
*chip
= get_chip(ac97
);
682 word
= (0x80 | (reg
& 0x7f)) << 16;
684 if ((ac97c_readl(chip
, COSR
) & AC97C_CSR_RXRDY
) != 0)
685 ac97c_readl(chip
, CORHR
);
691 if ((ac97c_readl(chip
, COSR
) & AC97C_CSR_TXRDY
) != 0) {
692 ac97c_writel(chip
, COTHR
, word
);
704 if ((ac97c_readl(chip
, COSR
) & AC97C_CSR_RXRDY
) != 0) {
705 unsigned short val
= ac97c_readl(chip
, CORHR
);
716 dev_dbg(&chip
->pdev
->dev
, "codec read timeout\n");
720 static bool filter(struct dma_chan
*chan
, void *slave
)
722 struct dw_dma_slave
*dws
= slave
;
724 if (dws
->dma_dev
== chan
->device
->dev
) {
731 static void atmel_ac97c_reset(struct atmel_ac97c
*chip
)
733 ac97c_writel(chip
, MR
, 0);
734 ac97c_writel(chip
, MR
, AC97C_MR_ENA
);
735 ac97c_writel(chip
, CAMR
, 0);
736 ac97c_writel(chip
, COMR
, 0);
738 if (gpio_is_valid(chip
->reset_pin
)) {
739 gpio_set_value(chip
->reset_pin
, 0);
740 /* AC97 v2.2 specifications says minimum 1 us. */
742 gpio_set_value(chip
->reset_pin
, 1);
746 static int __devinit
atmel_ac97c_probe(struct platform_device
*pdev
)
748 struct snd_card
*card
;
749 struct atmel_ac97c
*chip
;
750 struct resource
*regs
;
751 struct ac97c_platform_data
*pdata
;
753 static struct snd_ac97_bus_ops ops
= {
754 .write
= atmel_ac97c_write
,
755 .read
= atmel_ac97c_read
,
760 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
762 dev_dbg(&pdev
->dev
, "no memory resource\n");
766 pdata
= pdev
->dev
.platform_data
;
768 dev_dbg(&pdev
->dev
, "no platform data\n");
772 irq
= platform_get_irq(pdev
, 0);
774 dev_dbg(&pdev
->dev
, "could not get irq\n");
778 pclk
= clk_get(&pdev
->dev
, "pclk");
780 dev_dbg(&pdev
->dev
, "no peripheral clock\n");
781 return PTR_ERR(pclk
);
785 retval
= snd_card_create(SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
,
786 THIS_MODULE
, sizeof(struct atmel_ac97c
), &card
);
788 dev_dbg(&pdev
->dev
, "could not create sound card device\n");
789 goto err_snd_card_new
;
792 chip
= get_chip(card
);
794 retval
= request_irq(irq
, atmel_ac97c_interrupt
, 0, "AC97C", chip
);
796 dev_dbg(&pdev
->dev
, "unable to request irq %d\n", irq
);
797 goto err_request_irq
;
801 spin_lock_init(&chip
->lock
);
803 strcpy(card
->driver
, "Atmel AC97C");
804 strcpy(card
->shortname
, "Atmel AC97C");
805 sprintf(card
->longname
, "Atmel AC97 controller");
810 chip
->regs
= ioremap(regs
->start
, regs
->end
- regs
->start
+ 1);
813 dev_dbg(&pdev
->dev
, "could not remap register memory\n");
817 if (gpio_is_valid(pdata
->reset_pin
)) {
818 if (gpio_request(pdata
->reset_pin
, "reset_pin")) {
819 dev_dbg(&pdev
->dev
, "reset pin not available\n");
820 chip
->reset_pin
= -ENODEV
;
822 gpio_direction_output(pdata
->reset_pin
, 1);
823 chip
->reset_pin
= pdata
->reset_pin
;
827 snd_card_set_dev(card
, &pdev
->dev
);
829 atmel_ac97c_reset(chip
);
831 /* Enable overrun interrupt from codec channel */
832 ac97c_writel(chip
, COMR
, AC97C_CSR_OVRUN
);
833 ac97c_writel(chip
, IER
, ac97c_readl(chip
, IMR
) | AC97C_SR_COEVT
);
835 retval
= snd_ac97_bus(card
, 0, &ops
, chip
, &chip
->ac97_bus
);
837 dev_dbg(&pdev
->dev
, "could not register on ac97 bus\n");
841 retval
= atmel_ac97c_mixer_new(chip
);
843 dev_dbg(&pdev
->dev
, "could not register ac97 mixer\n");
847 if (pdata
->rx_dws
.dma_dev
) {
848 struct dw_dma_slave
*dws
= &pdata
->rx_dws
;
851 dws
->rx_reg
= regs
->start
+ AC97C_CARHR
+ 2;
854 dma_cap_set(DMA_SLAVE
, mask
);
856 chip
->dma
.rx_chan
= dma_request_channel(mask
, filter
, dws
);
858 dev_info(&chip
->pdev
->dev
, "using %s for DMA RX\n",
859 chip
->dma
.rx_chan
->dev
->device
.bus_id
);
860 set_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
);
863 if (pdata
->tx_dws
.dma_dev
) {
864 struct dw_dma_slave
*dws
= &pdata
->tx_dws
;
867 dws
->tx_reg
= regs
->start
+ AC97C_CATHR
+ 2;
870 dma_cap_set(DMA_SLAVE
, mask
);
872 chip
->dma
.tx_chan
= dma_request_channel(mask
, filter
, dws
);
874 dev_info(&chip
->pdev
->dev
, "using %s for DMA TX\n",
875 chip
->dma
.tx_chan
->dev
->device
.bus_id
);
876 set_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
);
879 if (!test_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
) &&
880 !test_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
)) {
881 dev_dbg(&pdev
->dev
, "DMA not available\n");
886 retval
= atmel_ac97c_pcm_new(chip
);
888 dev_dbg(&pdev
->dev
, "could not register ac97 pcm device\n");
892 retval
= snd_card_register(card
);
894 dev_dbg(&pdev
->dev
, "could not register sound card\n");
898 platform_set_drvdata(pdev
, card
);
900 dev_info(&pdev
->dev
, "Atmel AC97 controller at 0x%p\n",
906 if (test_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
))
907 dma_release_channel(chip
->dma
.rx_chan
);
908 if (test_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
))
909 dma_release_channel(chip
->dma
.tx_chan
);
910 clear_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
);
911 clear_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
);
912 chip
->dma
.rx_chan
= NULL
;
913 chip
->dma
.tx_chan
= NULL
;
915 snd_card_set_dev(card
, NULL
);
917 if (gpio_is_valid(chip
->reset_pin
))
918 gpio_free(chip
->reset_pin
);
932 static int atmel_ac97c_suspend(struct platform_device
*pdev
, pm_message_t msg
)
934 struct snd_card
*card
= platform_get_drvdata(pdev
);
935 struct atmel_ac97c
*chip
= card
->private_data
;
937 if (test_bit(DMA_RX_READY
, &chip
->flags
))
938 dw_dma_cyclic_stop(chip
->dma
.rx_chan
);
939 if (test_bit(DMA_TX_READY
, &chip
->flags
))
940 dw_dma_cyclic_stop(chip
->dma
.tx_chan
);
941 clk_disable(chip
->pclk
);
946 static int atmel_ac97c_resume(struct platform_device
*pdev
)
948 struct snd_card
*card
= platform_get_drvdata(pdev
);
949 struct atmel_ac97c
*chip
= card
->private_data
;
951 clk_enable(chip
->pclk
);
952 if (test_bit(DMA_RX_READY
, &chip
->flags
))
953 dw_dma_cyclic_start(chip
->dma
.rx_chan
);
954 if (test_bit(DMA_TX_READY
, &chip
->flags
))
955 dw_dma_cyclic_start(chip
->dma
.tx_chan
);
960 #define atmel_ac97c_suspend NULL
961 #define atmel_ac97c_resume NULL
964 static int __devexit
atmel_ac97c_remove(struct platform_device
*pdev
)
966 struct snd_card
*card
= platform_get_drvdata(pdev
);
967 struct atmel_ac97c
*chip
= get_chip(card
);
969 if (gpio_is_valid(chip
->reset_pin
))
970 gpio_free(chip
->reset_pin
);
972 ac97c_writel(chip
, CAMR
, 0);
973 ac97c_writel(chip
, COMR
, 0);
974 ac97c_writel(chip
, MR
, 0);
976 clk_disable(chip
->pclk
);
979 free_irq(chip
->irq
, chip
);
981 if (test_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
))
982 dma_release_channel(chip
->dma
.rx_chan
);
983 if (test_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
))
984 dma_release_channel(chip
->dma
.tx_chan
);
985 clear_bit(DMA_RX_CHAN_PRESENT
, &chip
->flags
);
986 clear_bit(DMA_TX_CHAN_PRESENT
, &chip
->flags
);
987 chip
->dma
.rx_chan
= NULL
;
988 chip
->dma
.tx_chan
= NULL
;
990 snd_card_set_dev(card
, NULL
);
993 platform_set_drvdata(pdev
, NULL
);
998 static struct platform_driver atmel_ac97c_driver
= {
999 .remove
= __devexit_p(atmel_ac97c_remove
),
1001 .name
= "atmel_ac97c",
1003 .suspend
= atmel_ac97c_suspend
,
1004 .resume
= atmel_ac97c_resume
,
1007 static int __init
atmel_ac97c_init(void)
1009 return platform_driver_probe(&atmel_ac97c_driver
,
1012 module_init(atmel_ac97c_init
);
1014 static void __exit
atmel_ac97c_exit(void)
1016 platform_driver_unregister(&atmel_ac97c_driver
);
1018 module_exit(atmel_ac97c_exit
);
1020 MODULE_LICENSE("GPL");
1021 MODULE_DESCRIPTION("Driver for Atmel AC97 controller");
1022 MODULE_AUTHOR("Hans-Christian Egtvedt <hans-christian.egtvedt@atmel.com>");