2 * BRIEF MODULE DESCRIPTION
3 * Driver for AMD Au1000 MIPS Processor, AC'97 Sound Port
5 * Copyright 2004 Cooper Street Innovations Inc.
6 * Author: Charles Eidsness <charles@cooper-street.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
14 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
16 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
19 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 675 Mass Ave, Cambridge, MA 02139, USA.
30 * 2004-09-09 Charles Eidsness -- Original verion -- based on
31 * sa11xx-uda1341.c ALSA driver and the
32 * au1000.c OSS driver.
33 * 2004-09-09 Matt Porter -- Added support for ALSA 1.0.6
37 #include <linux/ioport.h>
38 #include <linux/interrupt.h>
39 #include <linux/init.h>
40 #include <linux/platform_device.h>
41 #include <linux/slab.h>
42 #include <linux/module.h>
43 #include <sound/core.h>
44 #include <sound/initval.h>
45 #include <sound/pcm.h>
46 #include <sound/pcm_params.h>
47 #include <sound/ac97_codec.h>
48 #include <asm/mach-au1x00/au1000.h>
49 #include <asm/mach-au1x00/au1000_dma.h>
51 MODULE_AUTHOR("Charles Eidsness <charles@cooper-street.com>");
52 MODULE_DESCRIPTION("Au1000 AC'97 ALSA Driver");
53 MODULE_LICENSE("GPL");
54 MODULE_SUPPORTED_DEVICE("{{AMD,Au1000 AC'97}}");
58 #define AC97_SLOT_3 0x01
59 #define AC97_SLOT_4 0x02
60 #define AC97_SLOT_6 0x08
61 #define AC97_CMD_IRQ 31
70 u32 relative_end; /*realtive to start of buffer*/
71 struct au1000_period * next;
74 /*Au1000 AC97 Port Control Reisters*/
75 struct au1000_ac97_reg {
84 struct snd_pcm_substream *substream;
87 struct au1000_period * buffer;
88 unsigned int period_size;
93 struct snd_card *card;
94 struct au1000_ac97_reg volatile *ac97_ioport;
96 struct resource *ac97_res_port;
98 struct snd_ac97 *ac97;
101 struct audio_stream *stream[2]; /* playback & capture */
102 int dmaid[2]; /* tx(0)/rx(1) DMA ids */
105 /*--------------------------- Local Functions --------------------------------*/
107 au1000_set_ac97_xmit_slots(struct snd_au1000 *au1000, long xmit_slots)
109 u32 volatile ac97_config;
111 spin_lock(&au1000->ac97_lock);
112 ac97_config = au1000->ac97_ioport->config;
113 ac97_config = ac97_config & ~AC97C_XMIT_SLOTS_MASK;
114 ac97_config |= (xmit_slots << AC97C_XMIT_SLOTS_BIT);
115 au1000->ac97_ioport->config = ac97_config;
116 spin_unlock(&au1000->ac97_lock);
120 au1000_set_ac97_recv_slots(struct snd_au1000 *au1000, long recv_slots)
122 u32 volatile ac97_config;
124 spin_lock(&au1000->ac97_lock);
125 ac97_config = au1000->ac97_ioport->config;
126 ac97_config = ac97_config & ~AC97C_RECV_SLOTS_MASK;
127 ac97_config |= (recv_slots << AC97C_RECV_SLOTS_BIT);
128 au1000->ac97_ioport->config = ac97_config;
129 spin_unlock(&au1000->ac97_lock);
134 au1000_release_dma_link(struct audio_stream *stream)
136 struct au1000_period * pointer;
137 struct au1000_period * pointer_next;
139 stream->period_size = 0;
141 pointer = stream->buffer;
145 pointer_next = pointer->next;
147 pointer = pointer_next;
148 } while (pointer != stream->buffer);
149 stream->buffer = NULL;
153 au1000_setup_dma_link(struct audio_stream *stream, unsigned int period_bytes,
154 unsigned int periods)
156 struct snd_pcm_substream *substream = stream->substream;
157 struct snd_pcm_runtime *runtime = substream->runtime;
158 struct au1000_period *pointer;
159 unsigned long dma_start;
162 dma_start = virt_to_phys(runtime->dma_area);
164 if (stream->period_size == period_bytes &&
165 stream->periods == periods)
166 return 0; /* not changed */
168 au1000_release_dma_link(stream);
170 stream->period_size = period_bytes;
171 stream->periods = periods;
173 stream->buffer = kmalloc(sizeof(struct au1000_period), GFP_KERNEL);
174 if (! stream->buffer)
176 pointer = stream->buffer;
177 for (i = 0; i < periods; i++) {
178 pointer->start = (u32)(dma_start + (i * period_bytes));
179 pointer->relative_end = (u32) (((i+1) * period_bytes) - 0x1);
180 if (i < periods - 1) {
181 pointer->next = kmalloc(sizeof(struct au1000_period), GFP_KERNEL);
182 if (! pointer->next) {
183 au1000_release_dma_link(stream);
186 pointer = pointer->next;
189 pointer->next = stream->buffer;
194 au1000_dma_stop(struct audio_stream *stream)
196 if (snd_BUG_ON(!stream->buffer))
198 disable_dma(stream->dma);
202 au1000_dma_start(struct audio_stream *stream)
204 if (snd_BUG_ON(!stream->buffer))
207 init_dma(stream->dma);
208 if (get_dma_active_buffer(stream->dma) == 0) {
209 clear_dma_done0(stream->dma);
210 set_dma_addr0(stream->dma, stream->buffer->start);
211 set_dma_count0(stream->dma, stream->period_size >> 1);
212 set_dma_addr1(stream->dma, stream->buffer->next->start);
213 set_dma_count1(stream->dma, stream->period_size >> 1);
215 clear_dma_done1(stream->dma);
216 set_dma_addr1(stream->dma, stream->buffer->start);
217 set_dma_count1(stream->dma, stream->period_size >> 1);
218 set_dma_addr0(stream->dma, stream->buffer->next->start);
219 set_dma_count0(stream->dma, stream->period_size >> 1);
221 enable_dma_buffers(stream->dma);
222 start_dma(stream->dma);
226 au1000_dma_interrupt(int irq, void *dev_id)
228 struct audio_stream *stream = (struct audio_stream *) dev_id;
229 struct snd_pcm_substream *substream = stream->substream;
231 spin_lock(&stream->dma_lock);
232 switch (get_dma_buffer_done(stream->dma)) {
234 stream->buffer = stream->buffer->next;
235 clear_dma_done0(stream->dma);
236 set_dma_addr0(stream->dma, stream->buffer->next->start);
237 set_dma_count0(stream->dma, stream->period_size >> 1);
238 enable_dma_buffer0(stream->dma);
241 stream->buffer = stream->buffer->next;
242 clear_dma_done1(stream->dma);
243 set_dma_addr1(stream->dma, stream->buffer->next->start);
244 set_dma_count1(stream->dma, stream->period_size >> 1);
245 enable_dma_buffer1(stream->dma);
247 case (DMA_D0 | DMA_D1):
248 printk(KERN_ERR "DMA %d missed interrupt.\n",stream->dma);
249 au1000_dma_stop(stream);
250 au1000_dma_start(stream);
252 case (~DMA_D0 & ~DMA_D1):
253 printk(KERN_ERR "DMA %d empty irq.\n",stream->dma);
255 spin_unlock(&stream->dma_lock);
256 snd_pcm_period_elapsed(substream);
260 /*-------------------------- PCM Audio Streams -------------------------------*/
262 static unsigned int rates[] = {8000, 11025, 16000, 22050};
263 static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
264 .count = ARRAY_SIZE(rates),
269 static struct snd_pcm_hardware snd_au1000_hw =
271 .info = (SNDRV_PCM_INFO_INTERLEAVED | \
272 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
273 .formats = SNDRV_PCM_FMTBIT_S16_LE,
274 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |
275 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050),
280 .buffer_bytes_max = 128*1024,
281 .period_bytes_min = 32,
282 .period_bytes_max = 16*1024,
289 snd_au1000_playback_open(struct snd_pcm_substream *substream)
291 struct snd_au1000 *au1000 = substream->pcm->private_data;
293 au1000->stream[PLAYBACK]->substream = substream;
294 au1000->stream[PLAYBACK]->buffer = NULL;
295 substream->private_data = au1000->stream[PLAYBACK];
296 substream->runtime->hw = snd_au1000_hw;
297 return (snd_pcm_hw_constraint_list(substream->runtime, 0,
298 SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates) < 0);
302 snd_au1000_capture_open(struct snd_pcm_substream *substream)
304 struct snd_au1000 *au1000 = substream->pcm->private_data;
306 au1000->stream[CAPTURE]->substream = substream;
307 au1000->stream[CAPTURE]->buffer = NULL;
308 substream->private_data = au1000->stream[CAPTURE];
309 substream->runtime->hw = snd_au1000_hw;
310 return (snd_pcm_hw_constraint_list(substream->runtime, 0,
311 SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates) < 0);
315 snd_au1000_playback_close(struct snd_pcm_substream *substream)
317 struct snd_au1000 *au1000 = substream->pcm->private_data;
319 au1000->stream[PLAYBACK]->substream = NULL;
324 snd_au1000_capture_close(struct snd_pcm_substream *substream)
326 struct snd_au1000 *au1000 = substream->pcm->private_data;
328 au1000->stream[CAPTURE]->substream = NULL;
333 snd_au1000_hw_params(struct snd_pcm_substream *substream,
334 struct snd_pcm_hw_params *hw_params)
336 struct audio_stream *stream = substream->private_data;
339 err = snd_pcm_lib_malloc_pages(substream,
340 params_buffer_bytes(hw_params));
343 return au1000_setup_dma_link(stream,
344 params_period_bytes(hw_params),
345 params_periods(hw_params));
349 snd_au1000_hw_free(struct snd_pcm_substream *substream)
351 struct audio_stream *stream = substream->private_data;
352 au1000_release_dma_link(stream);
353 return snd_pcm_lib_free_pages(substream);
357 snd_au1000_playback_prepare(struct snd_pcm_substream *substream)
359 struct snd_au1000 *au1000 = substream->pcm->private_data;
360 struct snd_pcm_runtime *runtime = substream->runtime;
362 if (runtime->channels == 1)
363 au1000_set_ac97_xmit_slots(au1000, AC97_SLOT_4);
365 au1000_set_ac97_xmit_slots(au1000, AC97_SLOT_3 | AC97_SLOT_4);
366 snd_ac97_set_rate(au1000->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
371 snd_au1000_capture_prepare(struct snd_pcm_substream *substream)
373 struct snd_au1000 *au1000 = substream->pcm->private_data;
374 struct snd_pcm_runtime *runtime = substream->runtime;
376 if (runtime->channels == 1)
377 au1000_set_ac97_recv_slots(au1000, AC97_SLOT_4);
379 au1000_set_ac97_recv_slots(au1000, AC97_SLOT_3 | AC97_SLOT_4);
380 snd_ac97_set_rate(au1000->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
385 snd_au1000_trigger(struct snd_pcm_substream *substream, int cmd)
387 struct audio_stream *stream = substream->private_data;
390 spin_lock(&stream->dma_lock);
392 case SNDRV_PCM_TRIGGER_START:
393 au1000_dma_start(stream);
395 case SNDRV_PCM_TRIGGER_STOP:
396 au1000_dma_stop(stream);
402 spin_unlock(&stream->dma_lock);
406 static snd_pcm_uframes_t
407 snd_au1000_pointer(struct snd_pcm_substream *substream)
409 struct audio_stream *stream = substream->private_data;
410 struct snd_pcm_runtime *runtime = substream->runtime;
413 spin_lock(&stream->dma_lock);
414 location = get_dma_residue(stream->dma);
415 spin_unlock(&stream->dma_lock);
416 location = stream->buffer->relative_end - location;
419 return bytes_to_frames(runtime,location);
422 static struct snd_pcm_ops snd_card_au1000_playback_ops = {
423 .open = snd_au1000_playback_open,
424 .close = snd_au1000_playback_close,
425 .ioctl = snd_pcm_lib_ioctl,
426 .hw_params = snd_au1000_hw_params,
427 .hw_free = snd_au1000_hw_free,
428 .prepare = snd_au1000_playback_prepare,
429 .trigger = snd_au1000_trigger,
430 .pointer = snd_au1000_pointer,
433 static struct snd_pcm_ops snd_card_au1000_capture_ops = {
434 .open = snd_au1000_capture_open,
435 .close = snd_au1000_capture_close,
436 .ioctl = snd_pcm_lib_ioctl,
437 .hw_params = snd_au1000_hw_params,
438 .hw_free = snd_au1000_hw_free,
439 .prepare = snd_au1000_capture_prepare,
440 .trigger = snd_au1000_trigger,
441 .pointer = snd_au1000_pointer,
445 snd_au1000_pcm_new(struct snd_au1000 *au1000)
451 if ((err = snd_pcm_new(au1000->card, "AU1000 AC97 PCM", 0, 1, 1, &pcm)) < 0)
454 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
455 snd_dma_continuous_data(GFP_KERNEL), 128*1024, 128*1024);
457 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
458 &snd_card_au1000_playback_ops);
459 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
460 &snd_card_au1000_capture_ops);
462 pcm->private_data = au1000;
464 strcpy(pcm->name, "Au1000 AC97 PCM");
466 spin_lock_init(&au1000->stream[PLAYBACK]->dma_lock);
467 spin_lock_init(&au1000->stream[CAPTURE]->dma_lock);
469 flags = claim_dma_lock();
470 au1000->stream[PLAYBACK]->dma = request_au1000_dma(au1000->dmaid[0],
471 "AC97 TX", au1000_dma_interrupt, 0,
472 au1000->stream[PLAYBACK]);
473 if (au1000->stream[PLAYBACK]->dma < 0) {
474 release_dma_lock(flags);
477 au1000->stream[CAPTURE]->dma = request_au1000_dma(au1000->dmaid[1],
478 "AC97 RX", au1000_dma_interrupt, 0,
479 au1000->stream[CAPTURE]);
480 if (au1000->stream[CAPTURE]->dma < 0){
481 release_dma_lock(flags);
484 /* enable DMA coherency in read/write DMA channels */
485 set_dma_mode(au1000->stream[PLAYBACK]->dma,
486 get_dma_mode(au1000->stream[PLAYBACK]->dma) & ~DMA_NC);
487 set_dma_mode(au1000->stream[CAPTURE]->dma,
488 get_dma_mode(au1000->stream[CAPTURE]->dma) & ~DMA_NC);
489 release_dma_lock(flags);
495 /*-------------------------- AC97 CODEC Control ------------------------------*/
497 static unsigned short
498 snd_au1000_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
500 struct snd_au1000 *au1000 = ac97->private_data;
505 spin_lock(&au1000->ac97_lock);
506 /* would rather use the interrupt than this polling but it works and I can't
507 get the interrupt driven case to work efficiently */
508 for (i = 0; i < 0x5000; i++)
509 if (!(au1000->ac97_ioport->status & AC97C_CP))
512 printk(KERN_ERR "au1000 AC97: AC97 command read timeout\n");
514 cmd = (u32) reg & AC97C_INDEX_MASK;
516 au1000->ac97_ioport->cmd = cmd;
518 /* now wait for the data */
519 for (i = 0; i < 0x5000; i++)
520 if (!(au1000->ac97_ioport->status & AC97C_CP))
523 printk(KERN_ERR "au1000 AC97: AC97 command read timeout\n");
524 spin_unlock(&au1000->ac97_lock);
528 data = au1000->ac97_ioport->cmd & 0xffff;
529 spin_unlock(&au1000->ac97_lock);
537 snd_au1000_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short val)
539 struct snd_au1000 *au1000 = ac97->private_data;
543 spin_lock(&au1000->ac97_lock);
544 /* would rather use the interrupt than this polling but it works and I can't
545 get the interrupt driven case to work efficiently */
546 for (i = 0; i < 0x5000; i++)
547 if (!(au1000->ac97_ioport->status & AC97C_CP))
550 printk(KERN_ERR "au1000 AC97: AC97 command write timeout\n");
552 cmd = (u32) reg & AC97C_INDEX_MASK;
554 cmd |= ((u32) val << AC97C_WD_BIT);
555 au1000->ac97_ioport->cmd = cmd;
556 spin_unlock(&au1000->ac97_lock);
559 /*------------------------------ Setup / Destroy ----------------------------*/
561 static void snd_au1000_free(struct snd_card *card)
563 struct snd_au1000 *au1000 = card->private_data;
565 if (au1000->stream[PLAYBACK]) {
566 if (au1000->stream[PLAYBACK]->dma >= 0)
567 free_au1000_dma(au1000->stream[PLAYBACK]->dma);
568 kfree(au1000->stream[PLAYBACK]);
571 if (au1000->stream[CAPTURE]) {
572 if (au1000->stream[CAPTURE]->dma >= 0)
573 free_au1000_dma(au1000->stream[CAPTURE]->dma);
574 kfree(au1000->stream[CAPTURE]);
577 if (au1000->ac97_res_port) {
578 /* put internal AC97 block into reset */
579 if (au1000->ac97_ioport) {
580 au1000->ac97_ioport->cntrl = AC97C_RS;
581 iounmap(au1000->ac97_ioport);
582 au1000->ac97_ioport = NULL;
584 release_and_free_resource(au1000->ac97_res_port);
585 au1000->ac97_res_port = NULL;
589 static struct snd_ac97_bus_ops ops = {
590 .write = snd_au1000_ac97_write,
591 .read = snd_au1000_ac97_read,
594 static int au1000_ac97_probe(struct platform_device *pdev)
599 struct snd_card *card;
600 struct snd_au1000 *au1000;
601 struct snd_ac97_bus *pbus;
602 struct snd_ac97_template ac97;
604 err = snd_card_new(&pdev->dev, -1, "AC97", THIS_MODULE,
605 sizeof(struct snd_au1000), &card);
609 au1000 = card->private_data;
611 spin_lock_init(&au1000->ac97_lock);
613 /* from here on let ALSA call the special freeing function */
614 card->private_free = snd_au1000_free;
617 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
620 snd_printk(KERN_INFO "no TX DMA platform resource!\n");
623 au1000->dmaid[0] = r->start;
626 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
629 snd_printk(KERN_INFO "no RX DMA platform resource!\n");
632 au1000->dmaid[1] = r->start;
634 au1000->stream[PLAYBACK] = kmalloc(sizeof(struct audio_stream),
636 if (!au1000->stream[PLAYBACK])
638 au1000->stream[PLAYBACK]->dma = -1;
640 au1000->stream[CAPTURE] = kmalloc(sizeof(struct audio_stream),
642 if (!au1000->stream[CAPTURE])
644 au1000->stream[CAPTURE]->dma = -1;
646 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
651 au1000->ac97_res_port = request_mem_region(r->start, resource_size(r),
653 if (!au1000->ac97_res_port) {
654 snd_printk(KERN_ERR "ALSA AC97: can't grab AC97 port\n");
658 io = ioremap(r->start, resource_size(r));
662 au1000->ac97_ioport = (struct au1000_ac97_reg *)io;
664 /* configure pins for AC'97
665 TODO: move to board_setup.c */
666 au_writel(au_readl(SYS_PINFUNC) & ~0x02, SYS_PINFUNC);
668 /* Initialise Au1000's AC'97 Control Block */
669 au1000->ac97_ioport->cntrl = AC97C_RS | AC97C_CE;
671 au1000->ac97_ioport->cntrl = AC97C_CE;
674 /* Initialise External CODEC -- cold reset */
675 au1000->ac97_ioport->config = AC97C_RESET;
677 au1000->ac97_ioport->config = 0x0;
680 /* Initialise AC97 middle-layer */
681 err = snd_ac97_bus(au1000->card, 0, &ops, au1000, &pbus);
685 memset(&ac97, 0, sizeof(ac97));
686 ac97.private_data = au1000;
687 err = snd_ac97_mixer(pbus, &ac97, &au1000->ac97);
691 err = snd_au1000_pcm_new(au1000);
695 strcpy(card->driver, "Au1000-AC97");
696 strcpy(card->shortname, "AMD Au1000-AC97");
697 sprintf(card->longname, "AMD Au1000--AC97 ALSA Driver");
699 err = snd_card_register(card);
703 printk(KERN_INFO "ALSA AC97: Driver Initialized\n");
705 platform_set_drvdata(pdev, card);
714 static int au1000_ac97_remove(struct platform_device *pdev)
716 return snd_card_free(platform_get_drvdata(pdev));
719 struct platform_driver au1000_ac97c_driver = {
721 .name = "au1000-ac97c",
722 .owner = THIS_MODULE,
724 .probe = au1000_ac97_probe,
725 .remove = au1000_ac97_remove,
728 module_platform_driver(au1000_ac97c_driver);