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 */
73 #define get_chip(card) ((struct atmel_ac97c *)(card)->private_data)
75 #define ac97c_writel(chip, reg, val) \
76 __raw_writel((val), (chip)->regs + AC97C_##reg)
77 #define ac97c_readl(chip, reg) \
78 __raw_readl((chip)->regs + AC97C_##reg)
80 /* This function is called by the DMA driver. */
81 static void atmel_ac97c_dma_playback_period_done(void *arg)
83 struct atmel_ac97c *chip = arg;
84 snd_pcm_period_elapsed(chip->playback_substream);
87 static void atmel_ac97c_dma_capture_period_done(void *arg)
89 struct atmel_ac97c *chip = arg;
90 snd_pcm_period_elapsed(chip->capture_substream);
93 static int atmel_ac97c_prepare_dma(struct atmel_ac97c *chip,
94 struct snd_pcm_substream *substream,
95 enum dma_data_direction direction)
97 struct dma_chan *chan;
98 struct dw_cyclic_desc *cdesc;
99 struct snd_pcm_runtime *runtime = substream->runtime;
100 unsigned long buffer_len, period_len;
103 * We don't do DMA on "complex" transfers, i.e. with
104 * non-halfword-aligned buffers or lengths.
106 if (runtime->dma_addr & 1 || runtime->buffer_size & 1) {
107 dev_dbg(&chip->pdev->dev, "too complex transfer\n");
111 if (direction == DMA_TO_DEVICE)
112 chan = chip->dma.tx_chan;
114 chan = chip->dma.rx_chan;
116 buffer_len = frames_to_bytes(runtime, runtime->buffer_size);
117 period_len = frames_to_bytes(runtime, runtime->period_size);
119 cdesc = dw_dma_cyclic_prep(chan, runtime->dma_addr, buffer_len,
120 period_len, direction);
122 dev_dbg(&chip->pdev->dev, "could not prepare cyclic DMA\n");
123 return PTR_ERR(cdesc);
126 if (direction == DMA_TO_DEVICE) {
127 cdesc->period_callback = atmel_ac97c_dma_playback_period_done;
128 set_bit(DMA_TX_READY, &chip->flags);
130 cdesc->period_callback = atmel_ac97c_dma_capture_period_done;
131 set_bit(DMA_RX_READY, &chip->flags);
134 cdesc->period_callback_param = chip;
139 static struct snd_pcm_hardware atmel_ac97c_hw = {
140 .info = (SNDRV_PCM_INFO_MMAP
141 | SNDRV_PCM_INFO_MMAP_VALID
142 | SNDRV_PCM_INFO_INTERLEAVED
143 | SNDRV_PCM_INFO_BLOCK_TRANSFER
144 | SNDRV_PCM_INFO_JOINT_DUPLEX
145 | SNDRV_PCM_INFO_RESUME
146 | SNDRV_PCM_INFO_PAUSE),
147 .formats = (SNDRV_PCM_FMTBIT_S16_BE
148 | SNDRV_PCM_FMTBIT_S16_LE),
149 .rates = (SNDRV_PCM_RATE_CONTINUOUS),
154 .buffer_bytes_max = 2 * 2 * 64 * 2048,
155 .period_bytes_min = 4096,
156 .period_bytes_max = 4096,
161 static int atmel_ac97c_playback_open(struct snd_pcm_substream *substream)
163 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
164 struct snd_pcm_runtime *runtime = substream->runtime;
166 mutex_lock(&opened_mutex);
168 runtime->hw = atmel_ac97c_hw;
169 if (chip->cur_rate) {
170 runtime->hw.rate_min = chip->cur_rate;
171 runtime->hw.rate_max = chip->cur_rate;
173 if (chip->cur_format)
174 runtime->hw.formats = (1ULL << chip->cur_format);
175 mutex_unlock(&opened_mutex);
176 chip->playback_substream = substream;
180 static int atmel_ac97c_capture_open(struct snd_pcm_substream *substream)
182 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
183 struct snd_pcm_runtime *runtime = substream->runtime;
185 mutex_lock(&opened_mutex);
187 runtime->hw = atmel_ac97c_hw;
188 if (chip->cur_rate) {
189 runtime->hw.rate_min = chip->cur_rate;
190 runtime->hw.rate_max = chip->cur_rate;
192 if (chip->cur_format)
193 runtime->hw.formats = (1ULL << chip->cur_format);
194 mutex_unlock(&opened_mutex);
195 chip->capture_substream = substream;
199 static int atmel_ac97c_playback_close(struct snd_pcm_substream *substream)
201 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
203 mutex_lock(&opened_mutex);
207 chip->cur_format = 0;
209 mutex_unlock(&opened_mutex);
211 chip->playback_substream = NULL;
216 static int atmel_ac97c_capture_close(struct snd_pcm_substream *substream)
218 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
220 mutex_lock(&opened_mutex);
224 chip->cur_format = 0;
226 mutex_unlock(&opened_mutex);
228 chip->capture_substream = NULL;
233 static int atmel_ac97c_playback_hw_params(struct snd_pcm_substream *substream,
234 struct snd_pcm_hw_params *hw_params)
236 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
239 retval = snd_pcm_lib_malloc_pages(substream,
240 params_buffer_bytes(hw_params));
243 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
245 if (test_and_clear_bit(DMA_TX_READY, &chip->flags))
246 dw_dma_cyclic_free(chip->dma.tx_chan);
248 /* Set restrictions to params. */
249 mutex_lock(&opened_mutex);
250 chip->cur_rate = params_rate(hw_params);
251 chip->cur_format = params_format(hw_params);
252 mutex_unlock(&opened_mutex);
257 static int atmel_ac97c_capture_hw_params(struct snd_pcm_substream *substream,
258 struct snd_pcm_hw_params *hw_params)
260 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
263 retval = snd_pcm_lib_malloc_pages(substream,
264 params_buffer_bytes(hw_params));
267 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
269 if (test_and_clear_bit(DMA_RX_READY, &chip->flags))
270 dw_dma_cyclic_free(chip->dma.rx_chan);
272 /* Set restrictions to params. */
273 mutex_lock(&opened_mutex);
274 chip->cur_rate = params_rate(hw_params);
275 chip->cur_format = params_format(hw_params);
276 mutex_unlock(&opened_mutex);
281 static int atmel_ac97c_playback_hw_free(struct snd_pcm_substream *substream)
283 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
284 if (test_and_clear_bit(DMA_TX_READY, &chip->flags))
285 dw_dma_cyclic_free(chip->dma.tx_chan);
286 return snd_pcm_lib_free_pages(substream);
289 static int atmel_ac97c_capture_hw_free(struct snd_pcm_substream *substream)
291 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
292 if (test_and_clear_bit(DMA_RX_READY, &chip->flags))
293 dw_dma_cyclic_free(chip->dma.rx_chan);
294 return snd_pcm_lib_free_pages(substream);
297 static int atmel_ac97c_playback_prepare(struct snd_pcm_substream *substream)
299 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
300 struct snd_pcm_runtime *runtime = substream->runtime;
301 unsigned long word = ac97c_readl(chip, OCA);
304 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
306 /* assign channels to AC97C channel A */
307 switch (runtime->channels) {
309 word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
312 word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
313 | AC97C_CH_ASSIGN(PCM_RIGHT, A);
316 /* TODO: support more than two channels */
319 ac97c_writel(chip, OCA, word);
321 /* configure sample format and size */
322 word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
324 switch (runtime->format) {
325 case SNDRV_PCM_FORMAT_S16_LE:
326 word |= AC97C_CMR_CEM_LITTLE;
328 case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
329 word &= ~(AC97C_CMR_CEM_LITTLE);
332 word = ac97c_readl(chip, OCA);
333 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
334 ac97c_writel(chip, OCA, word);
338 ac97c_writel(chip, CAMR, word);
340 /* set variable rate if needed */
341 if (runtime->rate != 48000) {
342 word = ac97c_readl(chip, MR);
343 word |= AC97C_MR_VRA;
344 ac97c_writel(chip, MR, word);
346 word = ac97c_readl(chip, MR);
347 word &= ~(AC97C_MR_VRA);
348 ac97c_writel(chip, MR, word);
351 retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
354 dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
357 if (!test_bit(DMA_TX_READY, &chip->flags))
358 retval = atmel_ac97c_prepare_dma(chip, substream,
364 static int atmel_ac97c_capture_prepare(struct snd_pcm_substream *substream)
366 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
367 struct snd_pcm_runtime *runtime = substream->runtime;
368 unsigned long word = ac97c_readl(chip, ICA);
371 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
373 /* assign channels to AC97C channel A */
374 switch (runtime->channels) {
376 word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
379 word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
380 | AC97C_CH_ASSIGN(PCM_RIGHT, A);
383 /* TODO: support more than two channels */
386 ac97c_writel(chip, ICA, word);
388 /* configure sample format and size */
389 word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
391 switch (runtime->format) {
392 case SNDRV_PCM_FORMAT_S16_LE:
393 word |= AC97C_CMR_CEM_LITTLE;
395 case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
396 word &= ~(AC97C_CMR_CEM_LITTLE);
399 word = ac97c_readl(chip, ICA);
400 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
401 ac97c_writel(chip, ICA, word);
405 ac97c_writel(chip, CAMR, word);
407 /* set variable rate if needed */
408 if (runtime->rate != 48000) {
409 word = ac97c_readl(chip, MR);
410 word |= AC97C_MR_VRA;
411 ac97c_writel(chip, MR, word);
413 word = ac97c_readl(chip, MR);
414 word &= ~(AC97C_MR_VRA);
415 ac97c_writel(chip, MR, word);
418 retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE,
421 dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
424 if (!test_bit(DMA_RX_READY, &chip->flags))
425 retval = atmel_ac97c_prepare_dma(chip, substream,
432 atmel_ac97c_playback_trigger(struct snd_pcm_substream *substream, int cmd)
434 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
438 camr = ac97c_readl(chip, CAMR);
441 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
442 case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
443 case SNDRV_PCM_TRIGGER_START:
444 retval = dw_dma_cyclic_start(chip->dma.tx_chan);
447 camr |= AC97C_CMR_CENA;
449 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
450 case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
451 case SNDRV_PCM_TRIGGER_STOP:
452 dw_dma_cyclic_stop(chip->dma.tx_chan);
453 if (chip->opened <= 1)
454 camr &= ~AC97C_CMR_CENA;
461 ac97c_writel(chip, CAMR, camr);
467 atmel_ac97c_capture_trigger(struct snd_pcm_substream *substream, int cmd)
469 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
473 camr = ac97c_readl(chip, CAMR);
476 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
477 case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
478 case SNDRV_PCM_TRIGGER_START:
479 retval = dw_dma_cyclic_start(chip->dma.rx_chan);
482 camr |= AC97C_CMR_CENA;
484 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
485 case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
486 case SNDRV_PCM_TRIGGER_STOP:
487 dw_dma_cyclic_stop(chip->dma.rx_chan);
488 if (chip->opened <= 1)
489 camr &= ~AC97C_CMR_CENA;
496 ac97c_writel(chip, CAMR, camr);
501 static snd_pcm_uframes_t
502 atmel_ac97c_playback_pointer(struct snd_pcm_substream *substream)
504 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
505 struct snd_pcm_runtime *runtime = substream->runtime;
506 snd_pcm_uframes_t frames;
509 bytes = dw_dma_get_src_addr(chip->dma.tx_chan);
510 bytes -= runtime->dma_addr;
512 frames = bytes_to_frames(runtime, bytes);
513 if (frames >= runtime->buffer_size)
514 frames -= runtime->buffer_size;
518 static snd_pcm_uframes_t
519 atmel_ac97c_capture_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_dst_addr(chip->dma.rx_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 struct snd_pcm_ops atmel_ac97_playback_ops = {
536 .open = atmel_ac97c_playback_open,
537 .close = atmel_ac97c_playback_close,
538 .ioctl = snd_pcm_lib_ioctl,
539 .hw_params = atmel_ac97c_playback_hw_params,
540 .hw_free = atmel_ac97c_playback_hw_free,
541 .prepare = atmel_ac97c_playback_prepare,
542 .trigger = atmel_ac97c_playback_trigger,
543 .pointer = atmel_ac97c_playback_pointer,
546 static struct snd_pcm_ops atmel_ac97_capture_ops = {
547 .open = atmel_ac97c_capture_open,
548 .close = atmel_ac97c_capture_close,
549 .ioctl = snd_pcm_lib_ioctl,
550 .hw_params = atmel_ac97c_capture_hw_params,
551 .hw_free = atmel_ac97c_capture_hw_free,
552 .prepare = atmel_ac97c_capture_prepare,
553 .trigger = atmel_ac97c_capture_trigger,
554 .pointer = atmel_ac97c_capture_pointer,
557 static int __devinit atmel_ac97c_pcm_new(struct atmel_ac97c *chip)
560 struct snd_pcm_hardware hw = atmel_ac97c_hw;
561 int capture, playback, retval;
563 capture = test_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
564 playback = test_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
566 retval = snd_pcm_new(chip->card, chip->card->shortname,
567 chip->pdev->id, playback, capture, &pcm);
572 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
573 &atmel_ac97_capture_ops);
575 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
576 &atmel_ac97_playback_ops);
578 retval = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
579 &chip->pdev->dev, hw.periods_min * hw.period_bytes_min,
580 hw.buffer_bytes_max);
584 pcm->private_data = chip;
586 strcpy(pcm->name, chip->card->shortname);
592 static int atmel_ac97c_mixer_new(struct atmel_ac97c *chip)
594 struct snd_ac97_template template;
595 memset(&template, 0, sizeof(template));
596 template.private_data = chip;
597 return snd_ac97_mixer(chip->ac97_bus, &template, &chip->ac97);
600 static void atmel_ac97c_write(struct snd_ac97 *ac97, unsigned short reg,
603 struct atmel_ac97c *chip = get_chip(ac97);
607 word = (reg & 0x7f) << 16 | val;
610 if (ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) {
611 ac97c_writel(chip, COTHR, word);
617 dev_dbg(&chip->pdev->dev, "codec write timeout\n");
620 static unsigned short atmel_ac97c_read(struct snd_ac97 *ac97,
623 struct atmel_ac97c *chip = get_chip(ac97);
628 word = (0x80 | (reg & 0x7f)) << 16;
630 if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0)
631 ac97c_readl(chip, CORHR);
637 if ((ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) != 0) {
638 ac97c_writel(chip, COTHR, word);
650 if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0) {
651 unsigned short val = ac97c_readl(chip, CORHR);
662 dev_dbg(&chip->pdev->dev, "codec read timeout\n");
666 static bool filter(struct dma_chan *chan, void *slave)
668 struct dw_dma_slave *dws = slave;
670 if (dws->dma_dev == chan->device->dev) {
677 static void atmel_ac97c_reset(struct atmel_ac97c *chip)
679 ac97c_writel(chip, MR, AC97C_MR_WRST);
681 if (gpio_is_valid(chip->reset_pin)) {
682 gpio_set_value(chip->reset_pin, 0);
683 /* AC97 v2.2 specifications says minimum 1 us. */
685 gpio_set_value(chip->reset_pin, 1);
689 ac97c_writel(chip, MR, AC97C_MR_ENA);
692 static int __devinit atmel_ac97c_probe(struct platform_device *pdev)
694 struct snd_card *card;
695 struct atmel_ac97c *chip;
696 struct resource *regs;
697 struct ac97c_platform_data *pdata;
699 static struct snd_ac97_bus_ops ops = {
700 .write = atmel_ac97c_write,
701 .read = atmel_ac97c_read,
705 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
707 dev_dbg(&pdev->dev, "no memory resource\n");
711 pdata = pdev->dev.platform_data;
713 dev_dbg(&pdev->dev, "no platform data\n");
717 pclk = clk_get(&pdev->dev, "pclk");
719 dev_dbg(&pdev->dev, "no peripheral clock\n");
720 return PTR_ERR(pclk);
724 retval = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
725 THIS_MODULE, sizeof(struct atmel_ac97c), &card);
727 dev_dbg(&pdev->dev, "could not create sound card device\n");
728 goto err_snd_card_new;
731 chip = get_chip(card);
733 spin_lock_init(&chip->lock);
735 strcpy(card->driver, "Atmel AC97C");
736 strcpy(card->shortname, "Atmel AC97C");
737 sprintf(card->longname, "Atmel AC97 controller");
742 chip->regs = ioremap(regs->start, regs->end - regs->start + 1);
745 dev_dbg(&pdev->dev, "could not remap register memory\n");
749 if (gpio_is_valid(pdata->reset_pin)) {
750 if (gpio_request(pdata->reset_pin, "reset_pin")) {
751 dev_dbg(&pdev->dev, "reset pin not available\n");
752 chip->reset_pin = -ENODEV;
754 gpio_direction_output(pdata->reset_pin, 1);
755 chip->reset_pin = pdata->reset_pin;
759 snd_card_set_dev(card, &pdev->dev);
761 retval = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus);
763 dev_dbg(&pdev->dev, "could not register on ac97 bus\n");
767 atmel_ac97c_reset(chip);
769 retval = atmel_ac97c_mixer_new(chip);
771 dev_dbg(&pdev->dev, "could not register ac97 mixer\n");
775 if (pdata->rx_dws.dma_dev) {
776 struct dw_dma_slave *dws = &pdata->rx_dws;
779 dws->rx_reg = regs->start + AC97C_CARHR + 2;
782 dma_cap_set(DMA_SLAVE, mask);
784 chip->dma.rx_chan = dma_request_channel(mask, filter, dws);
786 dev_info(&chip->pdev->dev, "using %s for DMA RX\n",
787 chip->dma.rx_chan->dev->device.bus_id);
788 set_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
791 if (pdata->tx_dws.dma_dev) {
792 struct dw_dma_slave *dws = &pdata->tx_dws;
795 dws->tx_reg = regs->start + AC97C_CATHR + 2;
798 dma_cap_set(DMA_SLAVE, mask);
800 chip->dma.tx_chan = dma_request_channel(mask, filter, dws);
802 dev_info(&chip->pdev->dev, "using %s for DMA TX\n",
803 chip->dma.tx_chan->dev->device.bus_id);
804 set_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
807 if (!test_bit(DMA_RX_CHAN_PRESENT, &chip->flags) &&
808 !test_bit(DMA_TX_CHAN_PRESENT, &chip->flags)) {
809 dev_dbg(&pdev->dev, "DMA not available\n");
814 retval = atmel_ac97c_pcm_new(chip);
816 dev_dbg(&pdev->dev, "could not register ac97 pcm device\n");
820 retval = snd_card_register(card);
822 dev_dbg(&pdev->dev, "could not register sound card\n");
826 platform_set_drvdata(pdev, card);
828 dev_info(&pdev->dev, "Atmel AC97 controller at 0x%p\n",
834 if (test_bit(DMA_RX_CHAN_PRESENT, &chip->flags))
835 dma_release_channel(chip->dma.rx_chan);
836 if (test_bit(DMA_TX_CHAN_PRESENT, &chip->flags))
837 dma_release_channel(chip->dma.tx_chan);
838 clear_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
839 clear_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
840 chip->dma.rx_chan = NULL;
841 chip->dma.tx_chan = NULL;
843 snd_card_set_dev(card, NULL);
845 if (gpio_is_valid(chip->reset_pin))
846 gpio_free(chip->reset_pin);
858 static int atmel_ac97c_suspend(struct platform_device *pdev, pm_message_t msg)
860 struct snd_card *card = platform_get_drvdata(pdev);
861 struct atmel_ac97c *chip = card->private_data;
863 if (test_bit(DMA_RX_READY, &chip->flags))
864 dw_dma_cyclic_stop(chip->dma.rx_chan);
865 if (test_bit(DMA_TX_READY, &chip->flags))
866 dw_dma_cyclic_stop(chip->dma.tx_chan);
867 clk_disable(chip->pclk);
872 static int atmel_ac97c_resume(struct platform_device *pdev)
874 struct snd_card *card = platform_get_drvdata(pdev);
875 struct atmel_ac97c *chip = card->private_data;
877 clk_enable(chip->pclk);
878 if (test_bit(DMA_RX_READY, &chip->flags))
879 dw_dma_cyclic_start(chip->dma.rx_chan);
880 if (test_bit(DMA_TX_READY, &chip->flags))
881 dw_dma_cyclic_start(chip->dma.tx_chan);
886 #define atmel_ac97c_suspend NULL
887 #define atmel_ac97c_resume NULL
890 static int __devexit atmel_ac97c_remove(struct platform_device *pdev)
892 struct snd_card *card = platform_get_drvdata(pdev);
893 struct atmel_ac97c *chip = get_chip(card);
895 if (gpio_is_valid(chip->reset_pin))
896 gpio_free(chip->reset_pin);
898 clk_disable(chip->pclk);
902 if (test_bit(DMA_RX_CHAN_PRESENT, &chip->flags))
903 dma_release_channel(chip->dma.rx_chan);
904 if (test_bit(DMA_TX_CHAN_PRESENT, &chip->flags))
905 dma_release_channel(chip->dma.tx_chan);
906 clear_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
907 clear_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
908 chip->dma.rx_chan = NULL;
909 chip->dma.tx_chan = NULL;
911 snd_card_set_dev(card, NULL);
914 platform_set_drvdata(pdev, NULL);
919 static struct platform_driver atmel_ac97c_driver = {
920 .remove = __devexit_p(atmel_ac97c_remove),
922 .name = "atmel_ac97c",
924 .suspend = atmel_ac97c_suspend,
925 .resume = atmel_ac97c_resume,
928 static int __init atmel_ac97c_init(void)
930 return platform_driver_probe(&atmel_ac97c_driver,
933 module_init(atmel_ac97c_init);
935 static void __exit atmel_ac97c_exit(void)
937 platform_driver_unregister(&atmel_ac97c_driver);
939 module_exit(atmel_ac97c_exit);
941 MODULE_LICENSE("GPL");
942 MODULE_DESCRIPTION("Driver for Atmel AC97 controller");
943 MODULE_AUTHOR("Hans-Christian Egtvedt <hans-christian.egtvedt@atmel.com>");