ALSA: snd-atmel-ac97c: remove dead break statements after return in switch case
[safe/jmp/linux-2.6] / sound / atmel / ac97c.c
1 /*
2  * Driver for the Atmel AC97C controller
3  *
4  * Copyright (C) 2005-2009 Atmel Corporation
5  *
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/dmaengine.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/mutex.h>
20 #include <linux/gpio.h>
21 #include <linux/io.h>
22
23 #include <sound/core.h>
24 #include <sound/initval.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/ac97_codec.h>
28 #include <sound/atmel-ac97c.h>
29 #include <sound/memalloc.h>
30
31 #include <linux/dw_dmac.h>
32
33 #include "ac97c.h"
34
35 enum {
36         DMA_TX_READY = 0,
37         DMA_RX_READY,
38         DMA_TX_CHAN_PRESENT,
39         DMA_RX_CHAN_PRESENT,
40 };
41
42 /* Serialize access to opened variable */
43 static DEFINE_MUTEX(opened_mutex);
44
45 struct atmel_ac97c_dma {
46         struct dma_chan                 *rx_chan;
47         struct dma_chan                 *tx_chan;
48 };
49
50 struct atmel_ac97c {
51         struct clk                      *pclk;
52         struct platform_device          *pdev;
53         struct atmel_ac97c_dma          dma;
54
55         struct snd_pcm_substream        *playback_substream;
56         struct snd_pcm_substream        *capture_substream;
57         struct snd_card                 *card;
58         struct snd_pcm                  *pcm;
59         struct snd_ac97                 *ac97;
60         struct snd_ac97_bus             *ac97_bus;
61
62         u64                             cur_format;
63         unsigned int                    cur_rate;
64         unsigned long                   flags;
65         /* Serialize access to opened variable */
66         spinlock_t                      lock;
67         void __iomem                    *regs;
68         int                             opened;
69         int                             reset_pin;
70 };
71
72 #define get_chip(card) ((struct atmel_ac97c *)(card)->private_data)
73
74 #define ac97c_writel(chip, reg, val)                    \
75         __raw_writel((val), (chip)->regs + AC97C_##reg)
76 #define ac97c_readl(chip, reg)                          \
77         __raw_readl((chip)->regs + AC97C_##reg)
78
79 /* This function is called by the DMA driver. */
80 static void atmel_ac97c_dma_playback_period_done(void *arg)
81 {
82         struct atmel_ac97c *chip = arg;
83         snd_pcm_period_elapsed(chip->playback_substream);
84 }
85
86 static void atmel_ac97c_dma_capture_period_done(void *arg)
87 {
88         struct atmel_ac97c *chip = arg;
89         snd_pcm_period_elapsed(chip->capture_substream);
90 }
91
92 static int atmel_ac97c_prepare_dma(struct atmel_ac97c *chip,
93                 struct snd_pcm_substream *substream,
94                 enum dma_data_direction direction)
95 {
96         struct dma_chan                 *chan;
97         struct dw_cyclic_desc           *cdesc;
98         struct snd_pcm_runtime          *runtime = substream->runtime;
99         unsigned long                   buffer_len, period_len;
100
101         /*
102          * We don't do DMA on "complex" transfers, i.e. with
103          * non-halfword-aligned buffers or lengths.
104          */
105         if (runtime->dma_addr & 1 || runtime->buffer_size & 1) {
106                 dev_dbg(&chip->pdev->dev, "too complex transfer\n");
107                 return -EINVAL;
108         }
109
110         if (direction == DMA_TO_DEVICE)
111                 chan = chip->dma.tx_chan;
112         else
113                 chan = chip->dma.rx_chan;
114
115         buffer_len = frames_to_bytes(runtime, runtime->buffer_size);
116         period_len = frames_to_bytes(runtime, runtime->period_size);
117
118         cdesc = dw_dma_cyclic_prep(chan, runtime->dma_addr, buffer_len,
119                         period_len, direction);
120         if (IS_ERR(cdesc)) {
121                 dev_dbg(&chip->pdev->dev, "could not prepare cyclic DMA\n");
122                 return PTR_ERR(cdesc);
123         }
124
125         if (direction == DMA_TO_DEVICE) {
126                 cdesc->period_callback = atmel_ac97c_dma_playback_period_done;
127                 set_bit(DMA_TX_READY, &chip->flags);
128         } else {
129                 cdesc->period_callback = atmel_ac97c_dma_capture_period_done;
130                 set_bit(DMA_RX_READY, &chip->flags);
131         }
132
133         cdesc->period_callback_param = chip;
134
135         return 0;
136 }
137
138 static struct snd_pcm_hardware atmel_ac97c_hw = {
139         .info                   = (SNDRV_PCM_INFO_MMAP
140                                   | SNDRV_PCM_INFO_MMAP_VALID
141                                   | SNDRV_PCM_INFO_INTERLEAVED
142                                   | SNDRV_PCM_INFO_BLOCK_TRANSFER
143                                   | SNDRV_PCM_INFO_JOINT_DUPLEX
144                                   | SNDRV_PCM_INFO_RESUME
145                                   | SNDRV_PCM_INFO_PAUSE),
146         .formats                = (SNDRV_PCM_FMTBIT_S16_BE
147                                   | SNDRV_PCM_FMTBIT_S16_LE),
148         .rates                  = (SNDRV_PCM_RATE_CONTINUOUS),
149         .rate_min               = 4000,
150         .rate_max               = 48000,
151         .channels_min           = 1,
152         .channels_max           = 2,
153         .buffer_bytes_max       = 64 * 4096,
154         .period_bytes_min       = 4096,
155         .period_bytes_max       = 4096,
156         .periods_min            = 4,
157         .periods_max            = 64,
158 };
159
160 static int atmel_ac97c_playback_open(struct snd_pcm_substream *substream)
161 {
162         struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
163         struct snd_pcm_runtime *runtime = substream->runtime;
164
165         mutex_lock(&opened_mutex);
166         chip->opened++;
167         runtime->hw = atmel_ac97c_hw;
168         if (chip->cur_rate) {
169                 runtime->hw.rate_min = chip->cur_rate;
170                 runtime->hw.rate_max = chip->cur_rate;
171         }
172         if (chip->cur_format)
173                 runtime->hw.formats = (1ULL << chip->cur_format);
174         mutex_unlock(&opened_mutex);
175         chip->playback_substream = substream;
176         return 0;
177 }
178
179 static int atmel_ac97c_capture_open(struct snd_pcm_substream *substream)
180 {
181         struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
182         struct snd_pcm_runtime *runtime = substream->runtime;
183
184         mutex_lock(&opened_mutex);
185         chip->opened++;
186         runtime->hw = atmel_ac97c_hw;
187         if (chip->cur_rate) {
188                 runtime->hw.rate_min = chip->cur_rate;
189                 runtime->hw.rate_max = chip->cur_rate;
190         }
191         if (chip->cur_format)
192                 runtime->hw.formats = (1ULL << chip->cur_format);
193         mutex_unlock(&opened_mutex);
194         chip->capture_substream = substream;
195         return 0;
196 }
197
198 static int atmel_ac97c_playback_close(struct snd_pcm_substream *substream)
199 {
200         struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
201
202         mutex_lock(&opened_mutex);
203         chip->opened--;
204         if (!chip->opened) {
205                 chip->cur_rate = 0;
206                 chip->cur_format = 0;
207         }
208         mutex_unlock(&opened_mutex);
209
210         chip->playback_substream = NULL;
211
212         return 0;
213 }
214
215 static int atmel_ac97c_capture_close(struct snd_pcm_substream *substream)
216 {
217         struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
218
219         mutex_lock(&opened_mutex);
220         chip->opened--;
221         if (!chip->opened) {
222                 chip->cur_rate = 0;
223                 chip->cur_format = 0;
224         }
225         mutex_unlock(&opened_mutex);
226
227         chip->capture_substream = NULL;
228
229         return 0;
230 }
231
232 static int atmel_ac97c_playback_hw_params(struct snd_pcm_substream *substream,
233                 struct snd_pcm_hw_params *hw_params)
234 {
235         struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
236         int retval;
237
238         retval = snd_pcm_lib_malloc_pages(substream,
239                                         params_buffer_bytes(hw_params));
240         if (retval < 0)
241                 return retval;
242         /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
243         if (retval == 1)
244                 if (test_and_clear_bit(DMA_TX_READY, &chip->flags))
245                         dw_dma_cyclic_free(chip->dma.tx_chan);
246
247         /* Set restrictions to params. */
248         mutex_lock(&opened_mutex);
249         chip->cur_rate = params_rate(hw_params);
250         chip->cur_format = params_format(hw_params);
251         mutex_unlock(&opened_mutex);
252
253         return retval;
254 }
255
256 static int atmel_ac97c_capture_hw_params(struct snd_pcm_substream *substream,
257                 struct snd_pcm_hw_params *hw_params)
258 {
259         struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
260         int retval;
261
262         retval = snd_pcm_lib_malloc_pages(substream,
263                                         params_buffer_bytes(hw_params));
264         if (retval < 0)
265                 return retval;
266         /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
267         if (retval == 1)
268                 if (test_and_clear_bit(DMA_RX_READY, &chip->flags))
269                         dw_dma_cyclic_free(chip->dma.rx_chan);
270
271         /* Set restrictions to params. */
272         mutex_lock(&opened_mutex);
273         chip->cur_rate = params_rate(hw_params);
274         chip->cur_format = params_format(hw_params);
275         mutex_unlock(&opened_mutex);
276
277         return retval;
278 }
279
280 static int atmel_ac97c_playback_hw_free(struct snd_pcm_substream *substream)
281 {
282         struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
283         if (test_and_clear_bit(DMA_TX_READY, &chip->flags))
284                 dw_dma_cyclic_free(chip->dma.tx_chan);
285         return snd_pcm_lib_free_pages(substream);
286 }
287
288 static int atmel_ac97c_capture_hw_free(struct snd_pcm_substream *substream)
289 {
290         struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
291         if (test_and_clear_bit(DMA_RX_READY, &chip->flags))
292                 dw_dma_cyclic_free(chip->dma.rx_chan);
293         return snd_pcm_lib_free_pages(substream);
294 }
295
296 static int atmel_ac97c_playback_prepare(struct snd_pcm_substream *substream)
297 {
298         struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
299         struct snd_pcm_runtime *runtime = substream->runtime;
300         unsigned long word = 0;
301         int retval;
302
303         /* assign channels to AC97C channel A */
304         switch (runtime->channels) {
305         case 1:
306                 word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
307                 break;
308         case 2:
309                 word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
310                         | AC97C_CH_ASSIGN(PCM_RIGHT, A);
311                 break;
312         default:
313                 /* TODO: support more than two channels */
314                 return -EINVAL;
315         }
316         ac97c_writel(chip, OCA, word);
317
318         /* configure sample format and size */
319         word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
320
321         switch (runtime->format) {
322         case SNDRV_PCM_FORMAT_S16_LE:
323                 word |= AC97C_CMR_CEM_LITTLE;
324                 break;
325         case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
326         default:
327                 word &= ~(AC97C_CMR_CEM_LITTLE);
328                 break;
329         }
330
331         ac97c_writel(chip, CAMR, word);
332
333         /* set variable rate if needed */
334         if (runtime->rate != 48000) {
335                 word = ac97c_readl(chip, MR);
336                 word |= AC97C_MR_VRA;
337                 ac97c_writel(chip, MR, word);
338         } else {
339                 word = ac97c_readl(chip, MR);
340                 word &= ~(AC97C_MR_VRA);
341                 ac97c_writel(chip, MR, word);
342         }
343
344         retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
345                         runtime->rate);
346         if (retval)
347                 dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
348                                 runtime->rate);
349
350         if (!test_bit(DMA_TX_READY, &chip->flags))
351                 retval = atmel_ac97c_prepare_dma(chip, substream,
352                                 DMA_TO_DEVICE);
353
354         return retval;
355 }
356
357 static int atmel_ac97c_capture_prepare(struct snd_pcm_substream *substream)
358 {
359         struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
360         struct snd_pcm_runtime *runtime = substream->runtime;
361         unsigned long word = 0;
362         int retval;
363
364         /* assign channels to AC97C channel A */
365         switch (runtime->channels) {
366         case 1:
367                 word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
368                 break;
369         case 2:
370                 word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
371                         | AC97C_CH_ASSIGN(PCM_RIGHT, A);
372                 break;
373         default:
374                 /* TODO: support more than two channels */
375                 return -EINVAL;
376         }
377         ac97c_writel(chip, ICA, word);
378
379         /* configure sample format and size */
380         word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
381
382         switch (runtime->format) {
383         case SNDRV_PCM_FORMAT_S16_LE:
384                 word |= AC97C_CMR_CEM_LITTLE;
385                 break;
386         case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
387         default:
388                 word &= ~(AC97C_CMR_CEM_LITTLE);
389                 break;
390         }
391
392         ac97c_writel(chip, CAMR, word);
393
394         /* set variable rate if needed */
395         if (runtime->rate != 48000) {
396                 word = ac97c_readl(chip, MR);
397                 word |= AC97C_MR_VRA;
398                 ac97c_writel(chip, MR, word);
399         } else {
400                 word = ac97c_readl(chip, MR);
401                 word &= ~(AC97C_MR_VRA);
402                 ac97c_writel(chip, MR, word);
403         }
404
405         retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE,
406                         runtime->rate);
407         if (retval)
408                 dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
409                                 runtime->rate);
410
411         if (!test_bit(DMA_RX_READY, &chip->flags))
412                 retval = atmel_ac97c_prepare_dma(chip, substream,
413                                 DMA_FROM_DEVICE);
414
415         return retval;
416 }
417
418 static int
419 atmel_ac97c_playback_trigger(struct snd_pcm_substream *substream, int cmd)
420 {
421         struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
422         unsigned long camr;
423         int retval = 0;
424
425         camr = ac97c_readl(chip, CAMR);
426
427         switch (cmd) {
428         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
429         case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
430         case SNDRV_PCM_TRIGGER_START:
431                 retval = dw_dma_cyclic_start(chip->dma.tx_chan);
432                 if (retval)
433                         goto out;
434                 camr |= AC97C_CMR_CENA;
435                 break;
436         case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
437         case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
438         case SNDRV_PCM_TRIGGER_STOP:
439                 dw_dma_cyclic_stop(chip->dma.tx_chan);
440                 if (chip->opened <= 1)
441                         camr &= ~AC97C_CMR_CENA;
442                 break;
443         default:
444                 retval = -EINVAL;
445                 goto out;
446         }
447
448         ac97c_writel(chip, CAMR, camr);
449 out:
450         return retval;
451 }
452
453 static int
454 atmel_ac97c_capture_trigger(struct snd_pcm_substream *substream, int cmd)
455 {
456         struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
457         unsigned long camr;
458         int retval = 0;
459
460         camr = ac97c_readl(chip, CAMR);
461
462         switch (cmd) {
463         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
464         case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
465         case SNDRV_PCM_TRIGGER_START:
466                 retval = dw_dma_cyclic_start(chip->dma.rx_chan);
467                 if (retval)
468                         goto out;
469                 camr |= AC97C_CMR_CENA;
470                 break;
471         case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
472         case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
473         case SNDRV_PCM_TRIGGER_STOP:
474                 dw_dma_cyclic_stop(chip->dma.rx_chan);
475                 if (chip->opened <= 1)
476                         camr &= ~AC97C_CMR_CENA;
477                 break;
478         default:
479                 retval = -EINVAL;
480                 break;
481         }
482
483         ac97c_writel(chip, CAMR, camr);
484 out:
485         return retval;
486 }
487
488 static snd_pcm_uframes_t
489 atmel_ac97c_playback_pointer(struct snd_pcm_substream *substream)
490 {
491         struct atmel_ac97c      *chip = snd_pcm_substream_chip(substream);
492         struct snd_pcm_runtime  *runtime = substream->runtime;
493         snd_pcm_uframes_t       frames;
494         unsigned long           bytes;
495
496         bytes = dw_dma_get_src_addr(chip->dma.tx_chan);
497         bytes -= runtime->dma_addr;
498
499         frames = bytes_to_frames(runtime, bytes);
500         if (frames >= runtime->buffer_size)
501                 frames -= runtime->buffer_size;
502         return frames;
503 }
504
505 static snd_pcm_uframes_t
506 atmel_ac97c_capture_pointer(struct snd_pcm_substream *substream)
507 {
508         struct atmel_ac97c      *chip = snd_pcm_substream_chip(substream);
509         struct snd_pcm_runtime  *runtime = substream->runtime;
510         snd_pcm_uframes_t       frames;
511         unsigned long           bytes;
512
513         bytes = dw_dma_get_dst_addr(chip->dma.rx_chan);
514         bytes -= runtime->dma_addr;
515
516         frames = bytes_to_frames(runtime, bytes);
517         if (frames >= runtime->buffer_size)
518                 frames -= runtime->buffer_size;
519         return frames;
520 }
521
522 static struct snd_pcm_ops atmel_ac97_playback_ops = {
523         .open           = atmel_ac97c_playback_open,
524         .close          = atmel_ac97c_playback_close,
525         .ioctl          = snd_pcm_lib_ioctl,
526         .hw_params      = atmel_ac97c_playback_hw_params,
527         .hw_free        = atmel_ac97c_playback_hw_free,
528         .prepare        = atmel_ac97c_playback_prepare,
529         .trigger        = atmel_ac97c_playback_trigger,
530         .pointer        = atmel_ac97c_playback_pointer,
531 };
532
533 static struct snd_pcm_ops atmel_ac97_capture_ops = {
534         .open           = atmel_ac97c_capture_open,
535         .close          = atmel_ac97c_capture_close,
536         .ioctl          = snd_pcm_lib_ioctl,
537         .hw_params      = atmel_ac97c_capture_hw_params,
538         .hw_free        = atmel_ac97c_capture_hw_free,
539         .prepare        = atmel_ac97c_capture_prepare,
540         .trigger        = atmel_ac97c_capture_trigger,
541         .pointer        = atmel_ac97c_capture_pointer,
542 };
543
544 static int __devinit atmel_ac97c_pcm_new(struct atmel_ac97c *chip)
545 {
546         struct snd_pcm          *pcm;
547         struct snd_pcm_hardware hw = atmel_ac97c_hw;
548         int                     capture, playback, retval;
549
550         capture = test_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
551         playback = test_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
552
553         retval = snd_pcm_new(chip->card, chip->card->shortname,
554                         chip->pdev->id, playback, capture, &pcm);
555         if (retval)
556                 return retval;
557
558         if (capture)
559                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
560                                 &atmel_ac97_capture_ops);
561         if (playback)
562                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
563                                 &atmel_ac97_playback_ops);
564
565         retval = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
566                         &chip->pdev->dev, hw.periods_min * hw.period_bytes_min,
567                         hw.buffer_bytes_max);
568         if (retval)
569                 return retval;
570
571         pcm->private_data = chip;
572         pcm->info_flags = 0;
573         strcpy(pcm->name, chip->card->shortname);
574         chip->pcm = pcm;
575
576         return 0;
577 }
578
579 static int atmel_ac97c_mixer_new(struct atmel_ac97c *chip)
580 {
581         struct snd_ac97_template template;
582         memset(&template, 0, sizeof(template));
583         template.private_data = chip;
584         return snd_ac97_mixer(chip->ac97_bus, &template, &chip->ac97);
585 }
586
587 static void atmel_ac97c_write(struct snd_ac97 *ac97, unsigned short reg,
588                 unsigned short val)
589 {
590         struct atmel_ac97c *chip = get_chip(ac97);
591         unsigned long word;
592         int timeout = 40;
593
594         word = (reg & 0x7f) << 16 | val;
595
596         do {
597                 if (ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) {
598                         ac97c_writel(chip, COTHR, word);
599                         return;
600                 }
601                 udelay(1);
602         } while (--timeout);
603
604         dev_dbg(&chip->pdev->dev, "codec write timeout\n");
605 }
606
607 static unsigned short atmel_ac97c_read(struct snd_ac97 *ac97,
608                 unsigned short reg)
609 {
610         struct atmel_ac97c *chip = get_chip(ac97);
611         unsigned long word;
612         int timeout = 40;
613         int write = 10;
614
615         word = (0x80 | (reg & 0x7f)) << 16;
616
617         if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0)
618                 ac97c_readl(chip, CORHR);
619
620 retry_write:
621         timeout = 40;
622
623         do {
624                 if ((ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) != 0) {
625                         ac97c_writel(chip, COTHR, word);
626                         goto read_reg;
627                 }
628                 udelay(10);
629         } while (--timeout);
630
631         if (!--write)
632                 goto timed_out;
633         goto retry_write;
634
635 read_reg:
636         do {
637                 if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0) {
638                         unsigned short val = ac97c_readl(chip, CORHR);
639                         return val;
640                 }
641                 udelay(10);
642         } while (--timeout);
643
644         if (!--write)
645                 goto timed_out;
646         goto retry_write;
647
648 timed_out:
649         dev_dbg(&chip->pdev->dev, "codec read timeout\n");
650         return 0xffff;
651 }
652
653 static bool filter(struct dma_chan *chan, void *slave)
654 {
655         struct dw_dma_slave *dws = slave;
656
657         if (dws->dma_dev == chan->device->dev) {
658                 chan->private = dws;
659                 return true;
660         } else
661                 return false;
662 }
663
664 static void atmel_ac97c_reset(struct atmel_ac97c *chip)
665 {
666         ac97c_writel(chip, MR, AC97C_MR_WRST);
667
668         if (gpio_is_valid(chip->reset_pin)) {
669                 gpio_set_value(chip->reset_pin, 0);
670                 /* AC97 v2.2 specifications says minimum 1 us. */
671                 udelay(10);
672                 gpio_set_value(chip->reset_pin, 1);
673         }
674
675         udelay(1);
676         ac97c_writel(chip, MR, AC97C_MR_ENA);
677 }
678
679 static int __devinit atmel_ac97c_probe(struct platform_device *pdev)
680 {
681         struct snd_card                 *card;
682         struct atmel_ac97c              *chip;
683         struct resource                 *regs;
684         struct ac97c_platform_data      *pdata;
685         struct clk                      *pclk;
686         static struct snd_ac97_bus_ops  ops = {
687                 .write  = atmel_ac97c_write,
688                 .read   = atmel_ac97c_read,
689         };
690         int                             retval;
691
692         regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
693         if (!regs) {
694                 dev_dbg(&pdev->dev, "no memory resource\n");
695                 return -ENXIO;
696         }
697
698         pdata = pdev->dev.platform_data;
699         if (!pdata) {
700                 dev_dbg(&pdev->dev, "no platform data\n");
701                 return -ENXIO;
702         }
703
704         pclk = clk_get(&pdev->dev, "pclk");
705         if (IS_ERR(pclk)) {
706                 dev_dbg(&pdev->dev, "no peripheral clock\n");
707                 return PTR_ERR(pclk);
708         }
709         clk_enable(pclk);
710
711         retval = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
712                         THIS_MODULE, sizeof(struct atmel_ac97c), &card);
713         if (retval) {
714                 dev_dbg(&pdev->dev, "could not create sound card device\n");
715                 goto err_snd_card_new;
716         }
717
718         chip = get_chip(card);
719
720         spin_lock_init(&chip->lock);
721
722         strcpy(card->driver, "Atmel AC97C");
723         strcpy(card->shortname, "Atmel AC97C");
724         sprintf(card->longname, "Atmel AC97 controller");
725
726         chip->card = card;
727         chip->pclk = pclk;
728         chip->pdev = pdev;
729         chip->regs = ioremap(regs->start, regs->end - regs->start + 1);
730
731         if (!chip->regs) {
732                 dev_dbg(&pdev->dev, "could not remap register memory\n");
733                 goto err_ioremap;
734         }
735
736         if (gpio_is_valid(pdata->reset_pin)) {
737                 if (gpio_request(pdata->reset_pin, "reset_pin")) {
738                         dev_dbg(&pdev->dev, "reset pin not available\n");
739                         chip->reset_pin = -ENODEV;
740                 } else {
741                         gpio_direction_output(pdata->reset_pin, 1);
742                         chip->reset_pin = pdata->reset_pin;
743                 }
744         }
745
746         snd_card_set_dev(card, &pdev->dev);
747
748         retval = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus);
749         if (retval) {
750                 dev_dbg(&pdev->dev, "could not register on ac97 bus\n");
751                 goto err_ac97_bus;
752         }
753
754         atmel_ac97c_reset(chip);
755
756         retval = atmel_ac97c_mixer_new(chip);
757         if (retval) {
758                 dev_dbg(&pdev->dev, "could not register ac97 mixer\n");
759                 goto err_ac97_bus;
760         }
761
762         if (pdata->rx_dws.dma_dev) {
763                 struct dw_dma_slave *dws = &pdata->rx_dws;
764                 dma_cap_mask_t mask;
765
766                 dws->rx_reg = regs->start + AC97C_CARHR + 2;
767
768                 dma_cap_zero(mask);
769                 dma_cap_set(DMA_SLAVE, mask);
770
771                 chip->dma.rx_chan = dma_request_channel(mask, filter, dws);
772
773                 dev_info(&chip->pdev->dev, "using %s for DMA RX\n",
774                                         chip->dma.rx_chan->dev->device.bus_id);
775                 set_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
776         }
777
778         if (pdata->tx_dws.dma_dev) {
779                 struct dw_dma_slave *dws = &pdata->tx_dws;
780                 dma_cap_mask_t mask;
781
782                 dws->tx_reg = regs->start + AC97C_CATHR + 2;
783
784                 dma_cap_zero(mask);
785                 dma_cap_set(DMA_SLAVE, mask);
786
787                 chip->dma.tx_chan = dma_request_channel(mask, filter, dws);
788
789                 dev_info(&chip->pdev->dev, "using %s for DMA TX\n",
790                                         chip->dma.tx_chan->dev->device.bus_id);
791                 set_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
792         }
793
794         if (!test_bit(DMA_RX_CHAN_PRESENT, &chip->flags) &&
795                         !test_bit(DMA_TX_CHAN_PRESENT, &chip->flags)) {
796                 dev_dbg(&pdev->dev, "DMA not available\n");
797                 retval = -ENODEV;
798                 goto err_dma;
799         }
800
801         retval = atmel_ac97c_pcm_new(chip);
802         if (retval) {
803                 dev_dbg(&pdev->dev, "could not register ac97 pcm device\n");
804                 goto err_dma;
805         }
806
807         retval = snd_card_register(card);
808         if (retval) {
809                 dev_dbg(&pdev->dev, "could not register sound card\n");
810                 goto err_ac97_bus;
811         }
812
813         platform_set_drvdata(pdev, card);
814
815         dev_info(&pdev->dev, "Atmel AC97 controller at 0x%p\n",
816                         chip->regs);
817
818         return 0;
819
820 err_dma:
821         if (test_bit(DMA_RX_CHAN_PRESENT, &chip->flags))
822                 dma_release_channel(chip->dma.rx_chan);
823         if (test_bit(DMA_TX_CHAN_PRESENT, &chip->flags))
824                 dma_release_channel(chip->dma.tx_chan);
825         clear_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
826         clear_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
827         chip->dma.rx_chan = NULL;
828         chip->dma.tx_chan = NULL;
829 err_ac97_bus:
830         snd_card_set_dev(card, NULL);
831
832         if (gpio_is_valid(chip->reset_pin))
833                 gpio_free(chip->reset_pin);
834
835         iounmap(chip->regs);
836 err_ioremap:
837         snd_card_free(card);
838 err_snd_card_new:
839         clk_disable(pclk);
840         clk_put(pclk);
841         return retval;
842 }
843
844 #ifdef CONFIG_PM
845 static int atmel_ac97c_suspend(struct platform_device *pdev, pm_message_t msg)
846 {
847         struct snd_card *card = platform_get_drvdata(pdev);
848         struct atmel_ac97c *chip = card->private_data;
849
850         if (test_bit(DMA_RX_READY, &chip->flags))
851                 dw_dma_cyclic_stop(chip->dma.rx_chan);
852         if (test_bit(DMA_TX_READY, &chip->flags))
853                 dw_dma_cyclic_stop(chip->dma.tx_chan);
854         clk_disable(chip->pclk);
855
856         return 0;
857 }
858
859 static int atmel_ac97c_resume(struct platform_device *pdev)
860 {
861         struct snd_card *card = platform_get_drvdata(pdev);
862         struct atmel_ac97c *chip = card->private_data;
863
864         clk_enable(chip->pclk);
865         if (test_bit(DMA_RX_READY, &chip->flags))
866                 dw_dma_cyclic_start(chip->dma.rx_chan);
867         if (test_bit(DMA_TX_READY, &chip->flags))
868                 dw_dma_cyclic_start(chip->dma.tx_chan);
869
870         return 0;
871 }
872 #else
873 #define atmel_ac97c_suspend NULL
874 #define atmel_ac97c_resume NULL
875 #endif
876
877 static int __devexit atmel_ac97c_remove(struct platform_device *pdev)
878 {
879         struct snd_card *card = platform_get_drvdata(pdev);
880         struct atmel_ac97c *chip = get_chip(card);
881
882         if (gpio_is_valid(chip->reset_pin))
883                 gpio_free(chip->reset_pin);
884
885         clk_disable(chip->pclk);
886         clk_put(chip->pclk);
887         iounmap(chip->regs);
888
889         if (test_bit(DMA_RX_CHAN_PRESENT, &chip->flags))
890                 dma_release_channel(chip->dma.rx_chan);
891         if (test_bit(DMA_TX_CHAN_PRESENT, &chip->flags))
892                 dma_release_channel(chip->dma.tx_chan);
893         clear_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
894         clear_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
895         chip->dma.rx_chan = NULL;
896         chip->dma.tx_chan = NULL;
897
898         snd_card_set_dev(card, NULL);
899         snd_card_free(card);
900
901         platform_set_drvdata(pdev, NULL);
902
903         return 0;
904 }
905
906 static struct platform_driver atmel_ac97c_driver = {
907         .remove         = __devexit_p(atmel_ac97c_remove),
908         .driver         = {
909                 .name   = "atmel_ac97c",
910         },
911         .suspend        = atmel_ac97c_suspend,
912         .resume         = atmel_ac97c_resume,
913 };
914
915 static int __init atmel_ac97c_init(void)
916 {
917         return platform_driver_probe(&atmel_ac97c_driver,
918                         atmel_ac97c_probe);
919 }
920 module_init(atmel_ac97c_init);
921
922 static void __exit atmel_ac97c_exit(void)
923 {
924         platform_driver_unregister(&atmel_ac97c_driver);
925 }
926 module_exit(atmel_ac97c_exit);
927
928 MODULE_LICENSE("GPL");
929 MODULE_DESCRIPTION("Driver for Atmel AC97 controller");
930 MODULE_AUTHOR("Hans-Christian Egtvedt <hans-christian.egtvedt@atmel.com>");