x86/amd-iommu: Add per IOMMU reference counting
[linux/fpc-iii.git] / sound / atmel / ac97c.c
blob0c0f8771656ac7d3e2725d81d4d918ad4e47a987
1 /*
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.
9 */
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>
22 #include <linux/io.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>
34 #include "ac97c.h"
36 enum {
37 DMA_TX_READY = 0,
38 DMA_RX_READY,
39 DMA_TX_CHAN_PRESENT,
40 DMA_RX_CHAN_PRESENT,
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;
51 struct atmel_ac97c {
52 struct clk *pclk;
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;
59 struct snd_pcm *pcm;
60 struct snd_ac97 *ac97;
61 struct snd_ac97_bus *ac97_bus;
63 u64 cur_format;
64 unsigned int cur_rate;
65 unsigned long flags;
66 /* Serialize access to opened variable */
67 spinlock_t lock;
68 void __iomem *regs;
69 int irq;
70 int opened;
71 int reset_pin;
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");
109 return -EINVAL;
112 if (direction == DMA_TO_DEVICE)
113 chan = chip->dma.tx_chan;
114 else
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);
122 if (IS_ERR(cdesc)) {
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);
130 } else {
131 cdesc->period_callback = atmel_ac97c_dma_capture_period_done;
132 set_bit(DMA_RX_READY, &chip->flags);
135 cdesc->period_callback_param = chip;
137 return 0;
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),
151 .rate_min = 4000,
152 .rate_max = 48000,
153 .channels_min = 1,
154 .channels_max = 2,
155 .buffer_bytes_max = 2 * 2 * 64 * 2048,
156 .period_bytes_min = 4096,
157 .period_bytes_max = 4096,
158 .periods_min = 6,
159 .periods_max = 64,
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);
168 chip->opened++;
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;
178 return 0;
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);
187 chip->opened++;
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;
197 return 0;
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);
205 chip->opened--;
206 if (!chip->opened) {
207 chip->cur_rate = 0;
208 chip->cur_format = 0;
210 mutex_unlock(&opened_mutex);
212 chip->playback_substream = NULL;
214 return 0;
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);
222 chip->opened--;
223 if (!chip->opened) {
224 chip->cur_rate = 0;
225 chip->cur_format = 0;
227 mutex_unlock(&opened_mutex);
229 chip->capture_substream = NULL;
231 return 0;
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);
238 int retval;
240 retval = snd_pcm_lib_malloc_pages(substream,
241 params_buffer_bytes(hw_params));
242 if (retval < 0)
243 return retval;
244 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
245 if (retval == 1)
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);
255 return retval;
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);
262 int retval;
264 retval = snd_pcm_lib_malloc_pages(substream,
265 params_buffer_bytes(hw_params));
266 if (retval < 0)
267 return retval;
268 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
269 if (retval == 1)
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);
279 return retval;
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);
303 int retval;
305 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
307 /* assign channels to AC97C channel A */
308 switch (runtime->channels) {
309 case 1:
310 word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
311 break;
312 case 2:
313 word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
314 | AC97C_CH_ASSIGN(PCM_RIGHT, A);
315 break;
316 default:
317 /* TODO: support more than two channels */
318 return -EINVAL;
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;
328 break;
329 case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
330 word &= ~(AC97C_CMR_CEM_LITTLE);
331 break;
332 default:
333 word = ac97c_readl(chip, OCA);
334 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
335 ac97c_writel(chip, OCA, word);
336 return -EINVAL;
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);
354 } else {
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,
361 runtime->rate);
362 if (retval)
363 dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
364 runtime->rate);
366 if (!test_bit(DMA_TX_READY, &chip->flags))
367 retval = atmel_ac97c_prepare_dma(chip, substream,
368 DMA_TO_DEVICE);
370 return retval;
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);
378 int retval;
380 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
382 /* assign channels to AC97C channel A */
383 switch (runtime->channels) {
384 case 1:
385 word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
386 break;
387 case 2:
388 word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
389 | AC97C_CH_ASSIGN(PCM_RIGHT, A);
390 break;
391 default:
392 /* TODO: support more than two channels */
393 return -EINVAL;
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;
403 break;
404 case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
405 word &= ~(AC97C_CMR_CEM_LITTLE);
406 break;
407 default:
408 word = ac97c_readl(chip, ICA);
409 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
410 ac97c_writel(chip, ICA, word);
411 return -EINVAL;
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);
429 } else {
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,
436 runtime->rate);
437 if (retval)
438 dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
439 runtime->rate);
441 if (!test_bit(DMA_RX_READY, &chip->flags))
442 retval = atmel_ac97c_prepare_dma(chip, substream,
443 DMA_FROM_DEVICE);
445 return retval;
448 static int
449 atmel_ac97c_playback_trigger(struct snd_pcm_substream *substream, int cmd)
451 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
452 unsigned long camr;
453 int retval = 0;
455 camr = ac97c_readl(chip, CAMR);
457 switch (cmd) {
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);
462 if (retval)
463 goto out;
464 camr |= AC97C_CMR_CENA;
465 break;
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;
472 break;
473 default:
474 retval = -EINVAL;
475 goto out;
478 ac97c_writel(chip, CAMR, camr);
479 out:
480 return retval;
483 static int
484 atmel_ac97c_capture_trigger(struct snd_pcm_substream *substream, int cmd)
486 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
487 unsigned long camr;
488 int retval = 0;
490 camr = ac97c_readl(chip, CAMR);
492 switch (cmd) {
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);
497 if (retval)
498 goto out;
499 camr |= AC97C_CMR_CENA;
500 break;
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;
507 break;
508 default:
509 retval = -EINVAL;
510 break;
513 ac97c_writel(chip, CAMR, camr);
514 out:
515 return retval;
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;
524 unsigned long bytes;
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;
532 return frames;
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;
541 unsigned long bytes;
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;
549 return frames;
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);
608 return retval;
611 static int __devinit atmel_ac97c_pcm_new(struct atmel_ac97c *chip)
613 struct snd_pcm *pcm;
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);
622 if (retval)
623 return retval;
625 if (capture)
626 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
627 &atmel_ac97_capture_ops);
628 if (playback)
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);
635 if (retval)
636 return retval;
638 pcm->private_data = chip;
639 pcm->info_flags = 0;
640 strcpy(pcm->name, chip->card->shortname);
641 chip->pcm = pcm;
643 return 0;
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,
655 unsigned short val)
657 struct atmel_ac97c *chip = get_chip(ac97);
658 unsigned long word;
659 int timeout = 40;
661 word = (reg & 0x7f) << 16 | val;
663 do {
664 if (ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) {
665 ac97c_writel(chip, COTHR, word);
666 return;
668 udelay(1);
669 } while (--timeout);
671 dev_dbg(&chip->pdev->dev, "codec write timeout\n");
674 static unsigned short atmel_ac97c_read(struct snd_ac97 *ac97,
675 unsigned short reg)
677 struct atmel_ac97c *chip = get_chip(ac97);
678 unsigned long word;
679 int timeout = 40;
680 int write = 10;
682 word = (0x80 | (reg & 0x7f)) << 16;
684 if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0)
685 ac97c_readl(chip, CORHR);
687 retry_write:
688 timeout = 40;
690 do {
691 if ((ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) != 0) {
692 ac97c_writel(chip, COTHR, word);
693 goto read_reg;
695 udelay(10);
696 } while (--timeout);
698 if (!--write)
699 goto timed_out;
700 goto retry_write;
702 read_reg:
703 do {
704 if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0) {
705 unsigned short val = ac97c_readl(chip, CORHR);
706 return val;
708 udelay(10);
709 } while (--timeout);
711 if (!--write)
712 goto timed_out;
713 goto retry_write;
715 timed_out:
716 dev_dbg(&chip->pdev->dev, "codec read timeout\n");
717 return 0xffff;
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) {
725 chan->private = dws;
726 return true;
727 } else
728 return false;
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. */
741 udelay(2);
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;
752 struct clk *pclk;
753 static struct snd_ac97_bus_ops ops = {
754 .write = atmel_ac97c_write,
755 .read = atmel_ac97c_read,
757 int retval;
758 int irq;
760 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
761 if (!regs) {
762 dev_dbg(&pdev->dev, "no memory resource\n");
763 return -ENXIO;
766 pdata = pdev->dev.platform_data;
767 if (!pdata) {
768 dev_dbg(&pdev->dev, "no platform data\n");
769 return -ENXIO;
772 irq = platform_get_irq(pdev, 0);
773 if (irq < 0) {
774 dev_dbg(&pdev->dev, "could not get irq\n");
775 return -ENXIO;
778 pclk = clk_get(&pdev->dev, "pclk");
779 if (IS_ERR(pclk)) {
780 dev_dbg(&pdev->dev, "no peripheral clock\n");
781 return PTR_ERR(pclk);
783 clk_enable(pclk);
785 retval = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
786 THIS_MODULE, sizeof(struct atmel_ac97c), &card);
787 if (retval) {
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);
795 if (retval) {
796 dev_dbg(&pdev->dev, "unable to request irq %d\n", irq);
797 goto err_request_irq;
799 chip->irq = 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");
807 chip->card = card;
808 chip->pclk = pclk;
809 chip->pdev = pdev;
810 chip->regs = ioremap(regs->start, regs->end - regs->start + 1);
812 if (!chip->regs) {
813 dev_dbg(&pdev->dev, "could not remap register memory\n");
814 goto err_ioremap;
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;
821 } else {
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);
836 if (retval) {
837 dev_dbg(&pdev->dev, "could not register on ac97 bus\n");
838 goto err_ac97_bus;
841 retval = atmel_ac97c_mixer_new(chip);
842 if (retval) {
843 dev_dbg(&pdev->dev, "could not register ac97 mixer\n");
844 goto err_ac97_bus;
847 if (pdata->rx_dws.dma_dev) {
848 struct dw_dma_slave *dws = &pdata->rx_dws;
849 dma_cap_mask_t mask;
851 dws->rx_reg = regs->start + AC97C_CARHR + 2;
853 dma_cap_zero(mask);
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 dev_name(&chip->dma.rx_chan->dev->device));
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;
865 dma_cap_mask_t mask;
867 dws->tx_reg = regs->start + AC97C_CATHR + 2;
869 dma_cap_zero(mask);
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 dev_name(&chip->dma.tx_chan->dev->device));
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");
882 retval = -ENODEV;
883 goto err_dma;
886 retval = atmel_ac97c_pcm_new(chip);
887 if (retval) {
888 dev_dbg(&pdev->dev, "could not register ac97 pcm device\n");
889 goto err_dma;
892 retval = snd_card_register(card);
893 if (retval) {
894 dev_dbg(&pdev->dev, "could not register sound card\n");
895 goto err_dma;
898 platform_set_drvdata(pdev, card);
900 dev_info(&pdev->dev, "Atmel AC97 controller at 0x%p\n",
901 chip->regs);
903 return 0;
905 err_dma:
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;
914 err_ac97_bus:
915 snd_card_set_dev(card, NULL);
917 if (gpio_is_valid(chip->reset_pin))
918 gpio_free(chip->reset_pin);
920 iounmap(chip->regs);
921 err_ioremap:
922 free_irq(irq, chip);
923 err_request_irq:
924 snd_card_free(card);
925 err_snd_card_new:
926 clk_disable(pclk);
927 clk_put(pclk);
928 return retval;
931 #ifdef CONFIG_PM
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);
943 return 0;
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);
957 return 0;
959 #else
960 #define atmel_ac97c_suspend NULL
961 #define atmel_ac97c_resume NULL
962 #endif
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);
977 clk_put(chip->pclk);
978 iounmap(chip->regs);
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);
991 snd_card_free(card);
993 platform_set_drvdata(pdev, NULL);
995 return 0;
998 static struct platform_driver atmel_ac97c_driver = {
999 .remove = __devexit_p(atmel_ac97c_remove),
1000 .driver = {
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,
1010 atmel_ac97c_probe);
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>");