blob: 4e8f66d408120cbe6732483adcc3f75f72fc658c [file] [log] [blame]
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +01001/*
Hans-Christian Egtvedt128ed6a2009-04-02 08:21:12 +02002 * Driver for Atmel AC97C
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +01003 *
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>
Hans-Christian Egtvedt128ed6a2009-04-02 08:21:12 +020013#include <linux/device.h>
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +010014#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>
23
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>
31
32#include <linux/dw_dmac.h>
33
34#include "ac97c.h"
35
36enum {
37 DMA_TX_READY = 0,
38 DMA_RX_READY,
39 DMA_TX_CHAN_PRESENT,
40 DMA_RX_CHAN_PRESENT,
41};
42
43/* Serialize access to opened variable */
44static DEFINE_MUTEX(opened_mutex);
45
46struct atmel_ac97c_dma {
47 struct dma_chan *rx_chan;
48 struct dma_chan *tx_chan;
49};
50
51struct atmel_ac97c {
52 struct clk *pclk;
53 struct platform_device *pdev;
54 struct atmel_ac97c_dma dma;
55
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;
62
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 opened;
70 int reset_pin;
71};
72
73#define get_chip(card) ((struct atmel_ac97c *)(card)->private_data)
74
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)
79
80/* This function is called by the DMA driver. */
81static void atmel_ac97c_dma_playback_period_done(void *arg)
82{
83 struct atmel_ac97c *chip = arg;
84 snd_pcm_period_elapsed(chip->playback_substream);
85}
86
87static void atmel_ac97c_dma_capture_period_done(void *arg)
88{
89 struct atmel_ac97c *chip = arg;
90 snd_pcm_period_elapsed(chip->capture_substream);
91}
92
93static int atmel_ac97c_prepare_dma(struct atmel_ac97c *chip,
94 struct snd_pcm_substream *substream,
95 enum dma_data_direction direction)
96{
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;
101
102 /*
103 * We don't do DMA on "complex" transfers, i.e. with
104 * non-halfword-aligned buffers or lengths.
105 */
106 if (runtime->dma_addr & 1 || runtime->buffer_size & 1) {
107 dev_dbg(&chip->pdev->dev, "too complex transfer\n");
108 return -EINVAL;
109 }
110
111 if (direction == DMA_TO_DEVICE)
112 chan = chip->dma.tx_chan;
113 else
114 chan = chip->dma.rx_chan;
115
116 buffer_len = frames_to_bytes(runtime, runtime->buffer_size);
117 period_len = frames_to_bytes(runtime, runtime->period_size);
118
119 cdesc = dw_dma_cyclic_prep(chan, runtime->dma_addr, buffer_len,
120 period_len, direction);
121 if (IS_ERR(cdesc)) {
122 dev_dbg(&chip->pdev->dev, "could not prepare cyclic DMA\n");
123 return PTR_ERR(cdesc);
124 }
125
126 if (direction == DMA_TO_DEVICE) {
127 cdesc->period_callback = atmel_ac97c_dma_playback_period_done;
128 set_bit(DMA_TX_READY, &chip->flags);
129 } else {
130 cdesc->period_callback = atmel_ac97c_dma_capture_period_done;
131 set_bit(DMA_RX_READY, &chip->flags);
132 }
133
134 cdesc->period_callback_param = chip;
135
136 return 0;
137}
138
139static 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),
150 .rate_min = 4000,
151 .rate_max = 48000,
152 .channels_min = 1,
153 .channels_max = 2,
154 .buffer_bytes_max = 64 * 4096,
155 .period_bytes_min = 4096,
156 .period_bytes_max = 4096,
157 .periods_min = 4,
158 .periods_max = 64,
159};
160
161static int atmel_ac97c_playback_open(struct snd_pcm_substream *substream)
162{
163 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
164 struct snd_pcm_runtime *runtime = substream->runtime;
165
166 mutex_lock(&opened_mutex);
167 chip->opened++;
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;
172 }
173 if (chip->cur_format)
174 runtime->hw.formats = (1ULL << chip->cur_format);
175 mutex_unlock(&opened_mutex);
176 chip->playback_substream = substream;
177 return 0;
178}
179
180static int atmel_ac97c_capture_open(struct snd_pcm_substream *substream)
181{
182 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
183 struct snd_pcm_runtime *runtime = substream->runtime;
184
185 mutex_lock(&opened_mutex);
186 chip->opened++;
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;
191 }
192 if (chip->cur_format)
193 runtime->hw.formats = (1ULL << chip->cur_format);
194 mutex_unlock(&opened_mutex);
195 chip->capture_substream = substream;
196 return 0;
197}
198
199static int atmel_ac97c_playback_close(struct snd_pcm_substream *substream)
200{
201 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
202
203 mutex_lock(&opened_mutex);
204 chip->opened--;
205 if (!chip->opened) {
206 chip->cur_rate = 0;
207 chip->cur_format = 0;
208 }
209 mutex_unlock(&opened_mutex);
210
211 chip->playback_substream = NULL;
212
213 return 0;
214}
215
216static int atmel_ac97c_capture_close(struct snd_pcm_substream *substream)
217{
218 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
219
220 mutex_lock(&opened_mutex);
221 chip->opened--;
222 if (!chip->opened) {
223 chip->cur_rate = 0;
224 chip->cur_format = 0;
225 }
226 mutex_unlock(&opened_mutex);
227
228 chip->capture_substream = NULL;
229
230 return 0;
231}
232
233static int atmel_ac97c_playback_hw_params(struct snd_pcm_substream *substream,
234 struct snd_pcm_hw_params *hw_params)
235{
236 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
237 int retval;
238
239 retval = snd_pcm_lib_malloc_pages(substream,
240 params_buffer_bytes(hw_params));
241 if (retval < 0)
242 return retval;
243 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
244 if (retval == 1)
245 if (test_and_clear_bit(DMA_TX_READY, &chip->flags))
246 dw_dma_cyclic_free(chip->dma.tx_chan);
247
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);
253
254 return retval;
255}
256
257static int atmel_ac97c_capture_hw_params(struct snd_pcm_substream *substream,
258 struct snd_pcm_hw_params *hw_params)
259{
260 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
261 int retval;
262
263 retval = snd_pcm_lib_malloc_pages(substream,
264 params_buffer_bytes(hw_params));
265 if (retval < 0)
266 return retval;
267 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
268 if (retval == 1)
269 if (test_and_clear_bit(DMA_RX_READY, &chip->flags))
270 dw_dma_cyclic_free(chip->dma.rx_chan);
271
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);
277
278 return retval;
279}
280
281static int atmel_ac97c_playback_hw_free(struct snd_pcm_substream *substream)
282{
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);
287}
288
289static int atmel_ac97c_capture_hw_free(struct snd_pcm_substream *substream)
290{
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);
295}
296
297static int atmel_ac97c_playback_prepare(struct snd_pcm_substream *substream)
298{
299 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
300 struct snd_pcm_runtime *runtime = substream->runtime;
Hans-Christian Egtvedt128ed6a2009-04-02 08:21:12 +0200301 unsigned long word = ac97c_readl(chip, OCA);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100302 int retval;
303
Hans-Christian Egtvedt128ed6a2009-04-02 08:21:12 +0200304 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
305
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100306 /* assign channels to AC97C channel A */
307 switch (runtime->channels) {
308 case 1:
309 word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
310 break;
311 case 2:
312 word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
313 | AC97C_CH_ASSIGN(PCM_RIGHT, A);
314 break;
315 default:
316 /* TODO: support more than two channels */
317 return -EINVAL;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100318 }
319 ac97c_writel(chip, OCA, word);
320
321 /* configure sample format and size */
322 word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
323
324 switch (runtime->format) {
325 case SNDRV_PCM_FORMAT_S16_LE:
326 word |= AC97C_CMR_CEM_LITTLE;
327 break;
328 case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100329 word &= ~(AC97C_CMR_CEM_LITTLE);
330 break;
Hans-Christian Egtvedt128ed6a2009-04-02 08:21:12 +0200331 default:
332 word = ac97c_readl(chip, OCA);
333 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
334 ac97c_writel(chip, OCA, word);
335 return -EINVAL;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100336 }
337
338 ac97c_writel(chip, CAMR, word);
339
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);
345 } else {
346 word = ac97c_readl(chip, MR);
347 word &= ~(AC97C_MR_VRA);
348 ac97c_writel(chip, MR, word);
349 }
350
351 retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
352 runtime->rate);
353 if (retval)
354 dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
355 runtime->rate);
356
357 if (!test_bit(DMA_TX_READY, &chip->flags))
358 retval = atmel_ac97c_prepare_dma(chip, substream,
359 DMA_TO_DEVICE);
360
361 return retval;
362}
363
364static int atmel_ac97c_capture_prepare(struct snd_pcm_substream *substream)
365{
366 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
367 struct snd_pcm_runtime *runtime = substream->runtime;
Hans-Christian Egtvedt128ed6a2009-04-02 08:21:12 +0200368 unsigned long word = ac97c_readl(chip, ICA);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100369 int retval;
370
Hans-Christian Egtvedt128ed6a2009-04-02 08:21:12 +0200371 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
372
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100373 /* assign channels to AC97C channel A */
374 switch (runtime->channels) {
375 case 1:
376 word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
377 break;
378 case 2:
379 word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
380 | AC97C_CH_ASSIGN(PCM_RIGHT, A);
381 break;
382 default:
383 /* TODO: support more than two channels */
384 return -EINVAL;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100385 }
386 ac97c_writel(chip, ICA, word);
387
388 /* configure sample format and size */
389 word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
390
391 switch (runtime->format) {
392 case SNDRV_PCM_FORMAT_S16_LE:
393 word |= AC97C_CMR_CEM_LITTLE;
394 break;
395 case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100396 word &= ~(AC97C_CMR_CEM_LITTLE);
397 break;
Hans-Christian Egtvedt128ed6a2009-04-02 08:21:12 +0200398 default:
399 word = ac97c_readl(chip, ICA);
400 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
401 ac97c_writel(chip, ICA, word);
402 return -EINVAL;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100403 }
404
405 ac97c_writel(chip, CAMR, word);
406
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);
412 } else {
413 word = ac97c_readl(chip, MR);
414 word &= ~(AC97C_MR_VRA);
415 ac97c_writel(chip, MR, word);
416 }
417
418 retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE,
419 runtime->rate);
420 if (retval)
421 dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
422 runtime->rate);
423
424 if (!test_bit(DMA_RX_READY, &chip->flags))
425 retval = atmel_ac97c_prepare_dma(chip, substream,
426 DMA_FROM_DEVICE);
427
428 return retval;
429}
430
431static int
432atmel_ac97c_playback_trigger(struct snd_pcm_substream *substream, int cmd)
433{
434 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
435 unsigned long camr;
436 int retval = 0;
437
438 camr = ac97c_readl(chip, CAMR);
439
440 switch (cmd) {
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);
445 if (retval)
446 goto out;
447 camr |= AC97C_CMR_CENA;
448 break;
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;
455 break;
456 default:
457 retval = -EINVAL;
458 goto out;
459 }
460
461 ac97c_writel(chip, CAMR, camr);
462out:
463 return retval;
464}
465
466static int
467atmel_ac97c_capture_trigger(struct snd_pcm_substream *substream, int cmd)
468{
469 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
470 unsigned long camr;
471 int retval = 0;
472
473 camr = ac97c_readl(chip, CAMR);
474
475 switch (cmd) {
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);
480 if (retval)
481 goto out;
482 camr |= AC97C_CMR_CENA;
483 break;
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;
490 break;
491 default:
492 retval = -EINVAL;
493 break;
494 }
495
496 ac97c_writel(chip, CAMR, camr);
497out:
498 return retval;
499}
500
501static snd_pcm_uframes_t
502atmel_ac97c_playback_pointer(struct snd_pcm_substream *substream)
503{
504 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
505 struct snd_pcm_runtime *runtime = substream->runtime;
506 snd_pcm_uframes_t frames;
507 unsigned long bytes;
508
509 bytes = dw_dma_get_src_addr(chip->dma.tx_chan);
510 bytes -= runtime->dma_addr;
511
512 frames = bytes_to_frames(runtime, bytes);
513 if (frames >= runtime->buffer_size)
514 frames -= runtime->buffer_size;
515 return frames;
516}
517
518static snd_pcm_uframes_t
519atmel_ac97c_capture_pointer(struct snd_pcm_substream *substream)
520{
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;
525
526 bytes = dw_dma_get_dst_addr(chip->dma.rx_chan);
527 bytes -= runtime->dma_addr;
528
529 frames = bytes_to_frames(runtime, bytes);
530 if (frames >= runtime->buffer_size)
531 frames -= runtime->buffer_size;
532 return frames;
533}
534
535static 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,
544};
545
546static 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,
555};
556
557static int __devinit atmel_ac97c_pcm_new(struct atmel_ac97c *chip)
558{
559 struct snd_pcm *pcm;
560 struct snd_pcm_hardware hw = atmel_ac97c_hw;
561 int capture, playback, retval;
562
563 capture = test_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
564 playback = test_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
565
566 retval = snd_pcm_new(chip->card, chip->card->shortname,
567 chip->pdev->id, playback, capture, &pcm);
568 if (retval)
569 return retval;
570
571 if (capture)
572 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
573 &atmel_ac97_capture_ops);
574 if (playback)
575 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
576 &atmel_ac97_playback_ops);
577
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);
581 if (retval)
582 return retval;
583
584 pcm->private_data = chip;
585 pcm->info_flags = 0;
586 strcpy(pcm->name, chip->card->shortname);
587 chip->pcm = pcm;
588
589 return 0;
590}
591
592static int atmel_ac97c_mixer_new(struct atmel_ac97c *chip)
593{
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);
598}
599
600static void atmel_ac97c_write(struct snd_ac97 *ac97, unsigned short reg,
601 unsigned short val)
602{
603 struct atmel_ac97c *chip = get_chip(ac97);
604 unsigned long word;
605 int timeout = 40;
606
607 word = (reg & 0x7f) << 16 | val;
608
609 do {
610 if (ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) {
611 ac97c_writel(chip, COTHR, word);
612 return;
613 }
614 udelay(1);
615 } while (--timeout);
616
617 dev_dbg(&chip->pdev->dev, "codec write timeout\n");
618}
619
620static unsigned short atmel_ac97c_read(struct snd_ac97 *ac97,
621 unsigned short reg)
622{
623 struct atmel_ac97c *chip = get_chip(ac97);
624 unsigned long word;
625 int timeout = 40;
626 int write = 10;
627
628 word = (0x80 | (reg & 0x7f)) << 16;
629
630 if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0)
631 ac97c_readl(chip, CORHR);
632
633retry_write:
634 timeout = 40;
635
636 do {
637 if ((ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) != 0) {
638 ac97c_writel(chip, COTHR, word);
639 goto read_reg;
640 }
641 udelay(10);
642 } while (--timeout);
643
644 if (!--write)
645 goto timed_out;
646 goto retry_write;
647
648read_reg:
649 do {
650 if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0) {
651 unsigned short val = ac97c_readl(chip, CORHR);
652 return val;
653 }
654 udelay(10);
655 } while (--timeout);
656
657 if (!--write)
658 goto timed_out;
659 goto retry_write;
660
661timed_out:
662 dev_dbg(&chip->pdev->dev, "codec read timeout\n");
663 return 0xffff;
664}
665
666static bool filter(struct dma_chan *chan, void *slave)
667{
668 struct dw_dma_slave *dws = slave;
669
670 if (dws->dma_dev == chan->device->dev) {
671 chan->private = dws;
672 return true;
673 } else
674 return false;
675}
676
677static void atmel_ac97c_reset(struct atmel_ac97c *chip)
678{
679 ac97c_writel(chip, MR, AC97C_MR_WRST);
680
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. */
684 udelay(10);
685 gpio_set_value(chip->reset_pin, 1);
686 }
687
688 udelay(1);
689 ac97c_writel(chip, MR, AC97C_MR_ENA);
690}
691
692static int __devinit atmel_ac97c_probe(struct platform_device *pdev)
693{
694 struct snd_card *card;
695 struct atmel_ac97c *chip;
696 struct resource *regs;
697 struct ac97c_platform_data *pdata;
698 struct clk *pclk;
699 static struct snd_ac97_bus_ops ops = {
700 .write = atmel_ac97c_write,
701 .read = atmel_ac97c_read,
702 };
703 int retval;
704
705 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
706 if (!regs) {
707 dev_dbg(&pdev->dev, "no memory resource\n");
708 return -ENXIO;
709 }
710
711 pdata = pdev->dev.platform_data;
712 if (!pdata) {
713 dev_dbg(&pdev->dev, "no platform data\n");
714 return -ENXIO;
715 }
716
717 pclk = clk_get(&pdev->dev, "pclk");
718 if (IS_ERR(pclk)) {
719 dev_dbg(&pdev->dev, "no peripheral clock\n");
720 return PTR_ERR(pclk);
721 }
722 clk_enable(pclk);
723
724 retval = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
725 THIS_MODULE, sizeof(struct atmel_ac97c), &card);
726 if (retval) {
727 dev_dbg(&pdev->dev, "could not create sound card device\n");
728 goto err_snd_card_new;
729 }
730
731 chip = get_chip(card);
732
733 spin_lock_init(&chip->lock);
734
735 strcpy(card->driver, "Atmel AC97C");
736 strcpy(card->shortname, "Atmel AC97C");
737 sprintf(card->longname, "Atmel AC97 controller");
738
739 chip->card = card;
740 chip->pclk = pclk;
741 chip->pdev = pdev;
742 chip->regs = ioremap(regs->start, regs->end - regs->start + 1);
743
744 if (!chip->regs) {
745 dev_dbg(&pdev->dev, "could not remap register memory\n");
746 goto err_ioremap;
747 }
748
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;
753 } else {
754 gpio_direction_output(pdata->reset_pin, 1);
755 chip->reset_pin = pdata->reset_pin;
756 }
757 }
758
759 snd_card_set_dev(card, &pdev->dev);
760
761 retval = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus);
762 if (retval) {
763 dev_dbg(&pdev->dev, "could not register on ac97 bus\n");
764 goto err_ac97_bus;
765 }
766
767 atmel_ac97c_reset(chip);
768
769 retval = atmel_ac97c_mixer_new(chip);
770 if (retval) {
771 dev_dbg(&pdev->dev, "could not register ac97 mixer\n");
772 goto err_ac97_bus;
773 }
774
775 if (pdata->rx_dws.dma_dev) {
776 struct dw_dma_slave *dws = &pdata->rx_dws;
777 dma_cap_mask_t mask;
778
779 dws->rx_reg = regs->start + AC97C_CARHR + 2;
780
781 dma_cap_zero(mask);
782 dma_cap_set(DMA_SLAVE, mask);
783
784 chip->dma.rx_chan = dma_request_channel(mask, filter, dws);
785
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);
789 }
790
791 if (pdata->tx_dws.dma_dev) {
792 struct dw_dma_slave *dws = &pdata->tx_dws;
793 dma_cap_mask_t mask;
794
795 dws->tx_reg = regs->start + AC97C_CATHR + 2;
796
797 dma_cap_zero(mask);
798 dma_cap_set(DMA_SLAVE, mask);
799
800 chip->dma.tx_chan = dma_request_channel(mask, filter, dws);
801
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);
805 }
806
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");
810 retval = -ENODEV;
811 goto err_dma;
812 }
813
814 retval = atmel_ac97c_pcm_new(chip);
815 if (retval) {
816 dev_dbg(&pdev->dev, "could not register ac97 pcm device\n");
817 goto err_dma;
818 }
819
820 retval = snd_card_register(card);
821 if (retval) {
822 dev_dbg(&pdev->dev, "could not register sound card\n");
823 goto err_ac97_bus;
824 }
825
826 platform_set_drvdata(pdev, card);
827
828 dev_info(&pdev->dev, "Atmel AC97 controller at 0x%p\n",
829 chip->regs);
830
831 return 0;
832
833err_dma:
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;
842err_ac97_bus:
843 snd_card_set_dev(card, NULL);
844
845 if (gpio_is_valid(chip->reset_pin))
846 gpio_free(chip->reset_pin);
847
848 iounmap(chip->regs);
849err_ioremap:
850 snd_card_free(card);
851err_snd_card_new:
852 clk_disable(pclk);
853 clk_put(pclk);
854 return retval;
855}
856
857#ifdef CONFIG_PM
858static int atmel_ac97c_suspend(struct platform_device *pdev, pm_message_t msg)
859{
860 struct snd_card *card = platform_get_drvdata(pdev);
861 struct atmel_ac97c *chip = card->private_data;
862
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);
868
869 return 0;
870}
871
872static int atmel_ac97c_resume(struct platform_device *pdev)
873{
874 struct snd_card *card = platform_get_drvdata(pdev);
875 struct atmel_ac97c *chip = card->private_data;
876
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);
882
883 return 0;
884}
885#else
886#define atmel_ac97c_suspend NULL
887#define atmel_ac97c_resume NULL
888#endif
889
890static int __devexit atmel_ac97c_remove(struct platform_device *pdev)
891{
892 struct snd_card *card = platform_get_drvdata(pdev);
893 struct atmel_ac97c *chip = get_chip(card);
894
895 if (gpio_is_valid(chip->reset_pin))
896 gpio_free(chip->reset_pin);
897
898 clk_disable(chip->pclk);
899 clk_put(chip->pclk);
900 iounmap(chip->regs);
901
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;
910
911 snd_card_set_dev(card, NULL);
912 snd_card_free(card);
913
914 platform_set_drvdata(pdev, NULL);
915
916 return 0;
917}
918
919static struct platform_driver atmel_ac97c_driver = {
920 .remove = __devexit_p(atmel_ac97c_remove),
921 .driver = {
922 .name = "atmel_ac97c",
923 },
924 .suspend = atmel_ac97c_suspend,
925 .resume = atmel_ac97c_resume,
926};
927
928static int __init atmel_ac97c_init(void)
929{
930 return platform_driver_probe(&atmel_ac97c_driver,
931 atmel_ac97c_probe);
932}
933module_init(atmel_ac97c_init);
934
935static void __exit atmel_ac97c_exit(void)
936{
937 platform_driver_unregister(&atmel_ac97c_driver);
938}
939module_exit(atmel_ac97c_exit);
940
941MODULE_LICENSE("GPL");
942MODULE_DESCRIPTION("Driver for Atmel AC97 controller");
943MODULE_AUTHOR("Hans-Christian Egtvedt <hans-christian.egtvedt@atmel.com>");