blob: a8f9a3b6e73d9d9475711966fc80b30e14a6e9a1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * BRIEF MODULE DESCRIPTION
3 * Driver for AMD Au1000 MIPS Processor, AC'97 Sound Port
4 *
5 * Copyright 2004 Cooper Street Innovations Inc.
6 * Author: Charles Eidsness <charles@cooper-street.com>
7 *
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.
12 *
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.
23 *
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.
27 *
28 * History:
29 *
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
34 *
35 */
36
37#include <linux/ioport.h>
38#include <linux/interrupt.h>
39#include <sound/driver.h>
40#include <linux/init.h>
41#include <linux/slab.h>
42#include <linux/version.h>
43#include <sound/core.h>
44#include <sound/initval.h>
45#include <sound/pcm.h>
46#include <sound/ac97_codec.h>
47#include <asm/mach-au1x00/au1000.h>
48#include <asm/mach-au1x00/au1000_dma.h>
49
50MODULE_AUTHOR("Charles Eidsness <charles@cooper-street.com>");
51MODULE_DESCRIPTION("Au1000 AC'97 ALSA Driver");
52MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -070053MODULE_SUPPORTED_DEVICE("{{AMD,Au1000 AC'97}}");
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define PLAYBACK 0
56#define CAPTURE 1
57#define AC97_SLOT_3 0x01
58#define AC97_SLOT_4 0x02
59#define AC97_SLOT_6 0x08
60#define AC97_CMD_IRQ 31
61#define READ 0
62#define WRITE 1
63#define READ_WAIT 2
64#define RW_DONE 3
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066typedef struct au1000_period au1000_period_t;
67struct au1000_period
68{
69 u32 start;
70 u32 relative_end; /*realtive to start of buffer*/
71 au1000_period_t * next;
72};
73
74/*Au1000 AC97 Port Control Reisters*/
75typedef struct au1000_ac97_reg au1000_ac97_reg_t;
76struct au1000_ac97_reg {
77 u32 volatile config;
78 u32 volatile status;
79 u32 volatile data;
80 u32 volatile cmd;
81 u32 volatile cntrl;
82};
83
84typedef struct audio_stream audio_stream_t;
85struct audio_stream {
86 snd_pcm_substream_t * substream;
87 int dma;
88 spinlock_t dma_lock;
89 au1000_period_t * buffer;
Takashi Iwai33ea25c2005-11-17 10:32:43 +010090 unsigned int period_size;
91 unsigned int periods;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092};
93
94typedef struct snd_card_au1000 {
95 snd_card_t *card;
96 au1000_ac97_reg_t volatile *ac97_ioport;
97
98 struct resource *ac97_res_port;
99 spinlock_t ac97_lock;
100 ac97_t *ac97;
101
102 snd_pcm_t *pcm;
103 audio_stream_t *stream[2]; /* playback & capture */
104} au1000_t;
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/*--------------------------- Local Functions --------------------------------*/
107static void
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100108au1000_set_ac97_xmit_slots(au1000_t *au1000, long xmit_slots)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 u32 volatile ac97_config;
111
112 spin_lock(&au1000->ac97_lock);
113 ac97_config = au1000->ac97_ioport->config;
114 ac97_config = ac97_config & ~AC97C_XMIT_SLOTS_MASK;
115 ac97_config |= (xmit_slots << AC97C_XMIT_SLOTS_BIT);
116 au1000->ac97_ioport->config = ac97_config;
117 spin_unlock(&au1000->ac97_lock);
118}
119
120static void
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100121au1000_set_ac97_recv_slots(au1000_t *au1000, long recv_slots)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 u32 volatile ac97_config;
124
125 spin_lock(&au1000->ac97_lock);
126 ac97_config = au1000->ac97_ioport->config;
127 ac97_config = ac97_config & ~AC97C_RECV_SLOTS_MASK;
128 ac97_config |= (recv_slots << AC97C_RECV_SLOTS_BIT);
129 au1000->ac97_ioport->config = ac97_config;
130 spin_unlock(&au1000->ac97_lock);
131}
132
133
134static void
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100135au1000_release_dma_link(audio_stream_t *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 au1000_period_t * pointer;
138 au1000_period_t * pointer_next;
139
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100140 stream->period_size = 0;
141 stream->periods = 0;
142 pointer = stream->buffer;
143 if (! pointer)
144 return;
145 do {
146 pointer_next = pointer->next;
147 kfree(pointer);
148 pointer = pointer_next;
149 } while (pointer != stream->buffer);
150 stream->buffer = NULL;
151}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100153static int
154au1000_setup_dma_link(audio_stream_t *stream, unsigned int period_bytes,
155 unsigned int periods)
156{
157 snd_pcm_substream_t *substream = stream->substream;
158 snd_pcm_runtime_t *runtime = substream->runtime;
159 unsigned long dma_start;
160 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100162 dma_start = virt_to_phys(runtime->dma_area);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100164 if (stream->period_size == period_bytes &&
165 stream->periods == periods)
166 return 0; /* not changed */
167
168 au1000_release_dma_link(stream);
169
170 stream->period_size = period_bytes;
171 stream->periods = periods;
172
173 stream->buffer = kmalloc(sizeof(au1000_period_t), GFP_KERNEL);
174 if (! stream->buffer)
175 return -ENOMEM;
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);
184 return -ENOMEM;
185 }
186 pointer = pointer->next;
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100189 pointer->next = stream->buffer;
190 return 0;
191}
192
193static void
194au1000_dma_stop(audio_stream_t *stream)
195{
196 snd_assert(stream->buffer, return);
197 disable_dma(stream->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200static void
201au1000_dma_start(audio_stream_t *stream)
202{
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100203 snd_assert(stream->buffer, return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100205 init_dma(stream->dma);
206 if (get_dma_active_buffer(stream->dma) == 0) {
207 clear_dma_done0(stream->dma);
208 set_dma_addr0(stream->dma, stream->buffer->start);
209 set_dma_count0(stream->dma, stream->period_size >> 1);
210 set_dma_addr1(stream->dma, stream->buffer->next->start);
211 set_dma_count1(stream->dma, stream->period_size >> 1);
212 } else {
213 clear_dma_done1(stream->dma);
214 set_dma_addr1(stream->dma, stream->buffer->start);
215 set_dma_count1(stream->dma, stream->period_size >> 1);
216 set_dma_addr0(stream->dma, stream->buffer->next->start);
217 set_dma_count0(stream->dma, stream->period_size >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100219 enable_dma_buffers(stream->dma);
220 start_dma(stream->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223static irqreturn_t
224au1000_dma_interrupt(int irq, void *dev_id, struct pt_regs *regs)
225{
226 audio_stream_t *stream = (audio_stream_t *) dev_id;
227 snd_pcm_substream_t *substream = stream->substream;
228
229 spin_lock(&stream->dma_lock);
230 switch (get_dma_buffer_done(stream->dma)) {
231 case DMA_D0:
232 stream->buffer = stream->buffer->next;
233 clear_dma_done0(stream->dma);
234 set_dma_addr0(stream->dma, stream->buffer->next->start);
235 set_dma_count0(stream->dma, stream->period_size >> 1);
236 enable_dma_buffer0(stream->dma);
237 break;
238 case DMA_D1:
239 stream->buffer = stream->buffer->next;
240 clear_dma_done1(stream->dma);
241 set_dma_addr1(stream->dma, stream->buffer->next->start);
242 set_dma_count1(stream->dma, stream->period_size >> 1);
243 enable_dma_buffer1(stream->dma);
244 break;
245 case (DMA_D0 | DMA_D1):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 printk(KERN_ERR "DMA %d missed interrupt.\n",stream->dma);
247 au1000_dma_stop(stream);
248 au1000_dma_start(stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 break;
250 case (~DMA_D0 & ~DMA_D1):
251 printk(KERN_ERR "DMA %d empty irq.\n",stream->dma);
252 }
253 spin_unlock(&stream->dma_lock);
254 snd_pcm_period_elapsed(substream);
255 return IRQ_HANDLED;
256}
257
258/*-------------------------- PCM Audio Streams -------------------------------*/
259
260static unsigned int rates[] = {8000, 11025, 16000, 22050};
261static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
262 .count = sizeof(rates) / sizeof(rates[0]),
263 .list = rates,
264 .mask = 0,
265};
266
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100267static snd_pcm_hardware_t snd_au1000_hw =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 .info = (SNDRV_PCM_INFO_INTERLEAVED | \
270 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
271 .formats = SNDRV_PCM_FMTBIT_S16_LE,
272 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |
273 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050),
274 .rate_min = 8000,
275 .rate_max = 22050,
276 .channels_min = 1,
277 .channels_max = 2,
278 .buffer_bytes_max = 128*1024,
279 .period_bytes_min = 32,
280 .period_bytes_max = 16*1024,
281 .periods_min = 8,
282 .periods_max = 255,
283 .fifo_size = 16,
284};
285
286static int
287snd_au1000_playback_open(snd_pcm_substream_t * substream)
288{
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100289 au1000_t *au1000 = substream->pcm->private_data;
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 au1000->stream[PLAYBACK]->substream = substream;
292 au1000->stream[PLAYBACK]->buffer = NULL;
293 substream->private_data = au1000->stream[PLAYBACK];
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100294 substream->runtime->hw = snd_au1000_hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return (snd_pcm_hw_constraint_list(substream->runtime, 0,
296 SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates) < 0);
297}
298
299static int
300snd_au1000_capture_open(snd_pcm_substream_t * substream)
301{
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100302 au1000_t *au1000 = substream->pcm->private_data;
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 au1000->stream[CAPTURE]->substream = substream;
305 au1000->stream[CAPTURE]->buffer = NULL;
306 substream->private_data = au1000->stream[CAPTURE];
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100307 substream->runtime->hw = snd_au1000_hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return (snd_pcm_hw_constraint_list(substream->runtime, 0,
309 SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates) < 0);
310
311}
312
313static int
314snd_au1000_playback_close(snd_pcm_substream_t * substream)
315{
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100316 au1000_t *au1000 = substream->pcm->private_data;
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 au1000->stream[PLAYBACK]->substream = NULL;
319 return 0;
320}
321
322static int
323snd_au1000_capture_close(snd_pcm_substream_t * substream)
324{
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100325 au1000_t *au1000 = substream->pcm->private_data;
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 au1000->stream[CAPTURE]->substream = NULL;
328 return 0;
329}
330
331static int
332snd_au1000_hw_params(snd_pcm_substream_t * substream,
333 snd_pcm_hw_params_t * hw_params)
334{
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100335 audio_stream_t *stream = substream->private_data;
336 int err;
337
338 err = snd_pcm_lib_malloc_pages(substream,
339 params_buffer_bytes(hw_params));
340 if (err < 0)
341 return err;
342 return au1000_setup_dma_link(stream,
343 params_period_bytes(hw_params),
344 params_periods(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347static int
348snd_au1000_hw_free(snd_pcm_substream_t * substream)
349{
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100350 audio_stream_t *stream = substream->private_data;
351 au1000_release_dma_link(stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return snd_pcm_lib_free_pages(substream);
353}
354
355static int
356snd_au1000_playback_prepare(snd_pcm_substream_t * substream)
357{
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100358 au1000_t *au1000 = substream->pcm->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 snd_pcm_runtime_t *runtime = substream->runtime;
360
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100361 if (runtime->channels == 1)
362 au1000_set_ac97_xmit_slots(au1000, AC97_SLOT_4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 else
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100364 au1000_set_ac97_xmit_slots(au1000, AC97_SLOT_3 | AC97_SLOT_4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 snd_ac97_set_rate(au1000->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
366 return 0;
367}
368
369static int
370snd_au1000_capture_prepare(snd_pcm_substream_t * substream)
371{
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100372 au1000_t *au1000 = substream->pcm->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 snd_pcm_runtime_t *runtime = substream->runtime;
374
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100375 if (runtime->channels == 1)
376 au1000_set_ac97_recv_slots(au1000, AC97_SLOT_4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 else
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100378 au1000_set_ac97_recv_slots(au1000, AC97_SLOT_3 | AC97_SLOT_4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 snd_ac97_set_rate(au1000->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
380 return 0;
381}
382
383static int
384snd_au1000_trigger(snd_pcm_substream_t * substream, int cmd)
385{
386 audio_stream_t *stream = substream->private_data;
387 int err = 0;
388
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100389 spin_lock(&stream->dma_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 switch (cmd) {
391 case SNDRV_PCM_TRIGGER_START:
392 au1000_dma_start(stream);
393 break;
394 case SNDRV_PCM_TRIGGER_STOP:
395 au1000_dma_stop(stream);
396 break;
397 default:
398 err = -EINVAL;
399 break;
400 }
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100401 spin_unlock(&stream->dma_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return err;
403}
404
405static snd_pcm_uframes_t
406snd_au1000_pointer(snd_pcm_substream_t * substream)
407{
408 audio_stream_t *stream = substream->private_data;
409 snd_pcm_runtime_t *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 long location;
411
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100412 spin_lock(&stream->dma_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 location = get_dma_residue(stream->dma);
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100414 spin_unlock(&stream->dma_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 location = stream->buffer->relative_end - location;
416 if (location == -1)
417 location = 0;
418 return bytes_to_frames(runtime,location);
419}
420
421static snd_pcm_ops_t snd_card_au1000_playback_ops = {
422 .open = snd_au1000_playback_open,
423 .close = snd_au1000_playback_close,
424 .ioctl = snd_pcm_lib_ioctl,
425 .hw_params = snd_au1000_hw_params,
426 .hw_free = snd_au1000_hw_free,
427 .prepare = snd_au1000_playback_prepare,
428 .trigger = snd_au1000_trigger,
429 .pointer = snd_au1000_pointer,
430};
431
432static snd_pcm_ops_t snd_card_au1000_capture_ops = {
433 .open = snd_au1000_capture_open,
434 .close = snd_au1000_capture_close,
435 .ioctl = snd_pcm_lib_ioctl,
436 .hw_params = snd_au1000_hw_params,
437 .hw_free = snd_au1000_hw_free,
438 .prepare = snd_au1000_capture_prepare,
439 .trigger = snd_au1000_trigger,
440 .pointer = snd_au1000_pointer,
441};
442
443static int __devinit
444snd_au1000_pcm_new(void)
445{
446 snd_pcm_t *pcm;
447 int err;
448 unsigned long flags;
449
450 if ((err = snd_pcm_new(au1000->card, "AU1000 AC97 PCM", 0, 1, 1, &pcm)) < 0)
451 return err;
452
453 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
454 snd_dma_continuous_data(GFP_KERNEL), 128*1024, 128*1024);
455
456 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
457 &snd_card_au1000_playback_ops);
458 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
459 &snd_card_au1000_capture_ops);
460
461 pcm->private_data = au1000;
462 pcm->info_flags = 0;
463 strcpy(pcm->name, "Au1000 AC97 PCM");
464
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100465 spin_lock_init(&au1000->stream[PLAYBACK]->dma_lock);
466 spin_lock_init(&au1000->stream[CAPTURE]->dma_lock);
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 flags = claim_dma_lock();
469 if ((au1000->stream[PLAYBACK]->dma = request_au1000_dma(DMA_ID_AC97C_TX,
470 "AC97 TX", au1000_dma_interrupt, SA_INTERRUPT,
471 au1000->stream[PLAYBACK])) < 0) {
472 release_dma_lock(flags);
473 return -EBUSY;
474 }
475 if ((au1000->stream[CAPTURE]->dma = request_au1000_dma(DMA_ID_AC97C_RX,
476 "AC97 RX", au1000_dma_interrupt, SA_INTERRUPT,
477 au1000->stream[CAPTURE])) < 0){
478 release_dma_lock(flags);
479 return -EBUSY;
480 }
481 /* enable DMA coherency in read/write DMA channels */
482 set_dma_mode(au1000->stream[PLAYBACK]->dma,
483 get_dma_mode(au1000->stream[PLAYBACK]->dma) & ~DMA_NC);
484 set_dma_mode(au1000->stream[CAPTURE]->dma,
485 get_dma_mode(au1000->stream[CAPTURE]->dma) & ~DMA_NC);
486 release_dma_lock(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 au1000->pcm = pcm;
488 return 0;
489}
490
491
492/*-------------------------- AC97 CODEC Control ------------------------------*/
493
494static unsigned short
495snd_au1000_ac97_read(ac97_t *ac97, unsigned short reg)
496{
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100497 au1000_t *au1000 = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 u32 volatile cmd;
499 u16 volatile data;
500 int i;
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100501
Konstantin Baydarov708f9972005-10-27 17:25:02 +0200502 spin_lock(&au1000->ac97_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503/* would rather use the interupt than this polling but it works and I can't
504get the interupt driven case to work efficiently */
505 for (i = 0; i < 0x5000; i++)
506 if (!(au1000->ac97_ioport->status & AC97C_CP))
507 break;
508 if (i == 0x5000)
509 printk(KERN_ERR "au1000 AC97: AC97 command read timeout\n");
510
511 cmd = (u32) reg & AC97C_INDEX_MASK;
512 cmd |= AC97C_READ;
513 au1000->ac97_ioport->cmd = cmd;
514
515 /* now wait for the data */
516 for (i = 0; i < 0x5000; i++)
517 if (!(au1000->ac97_ioport->status & AC97C_CP))
518 break;
519 if (i == 0x5000) {
520 printk(KERN_ERR "au1000 AC97: AC97 command read timeout\n");
521 return 0;
522 }
523
524 data = au1000->ac97_ioport->cmd & 0xffff;
Konstantin Baydarov708f9972005-10-27 17:25:02 +0200525 spin_unlock(&au1000->ac97_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 return data;
528
529}
530
531
532static void
533snd_au1000_ac97_write(ac97_t *ac97, unsigned short reg, unsigned short val)
534{
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100535 au1000_t *au1000 = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 u32 cmd;
537 int i;
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100538
Konstantin Baydarov708f9972005-10-27 17:25:02 +0200539 spin_lock(&au1000->ac97_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540/* would rather use the interupt than this polling but it works and I can't
541get the interupt driven case to work efficiently */
542 for (i = 0; i < 0x5000; i++)
543 if (!(au1000->ac97_ioport->status & AC97C_CP))
544 break;
545 if (i == 0x5000)
546 printk(KERN_ERR "au1000 AC97: AC97 command write timeout\n");
547
548 cmd = (u32) reg & AC97C_INDEX_MASK;
549 cmd &= ~AC97C_READ;
550 cmd |= ((u32) val << AC97C_WD_BIT);
551 au1000->ac97_ioport->cmd = cmd;
Konstantin Baydarov708f9972005-10-27 17:25:02 +0200552 spin_unlock(&au1000->ac97_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555static int __devinit
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100556snd_au1000_ac97_new(au1000_t *au1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 ac97_bus_t bus, *pbus;
560 ac97_t ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 if ((au1000->ac97_res_port = request_region(AC97C_CONFIG,
563 sizeof(au1000_ac97_reg_t), "Au1x00 AC97")) == NULL) {
564 snd_printk(KERN_ERR "ALSA AC97: can't grap AC97 port\n");
565 return -EBUSY;
566 }
567 au1000->ac97_ioport = (au1000_ac97_reg_t *) au1000->ac97_res_port->start;
568
569 spin_lock_init(&au1000->ac97_lock);
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* configure pins for AC'97
572 TODO: move to board_setup.c */
573 au_writel(au_readl(SYS_PINFUNC) & ~0x02, SYS_PINFUNC);
574
575 /* Initialise Au1000's AC'97 Control Block */
576 au1000->ac97_ioport->cntrl = AC97C_RS | AC97C_CE;
577 udelay(10);
578 au1000->ac97_ioport->cntrl = AC97C_CE;
579 udelay(10);
580
581 /* Initialise External CODEC -- cold reset */
582 au1000->ac97_ioport->config = AC97C_RESET;
583 udelay(10);
584 au1000->ac97_ioport->config = 0x0;
585 mdelay(5);
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* Initialise AC97 middle-layer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if ((err = snd_ac97_bus(au1000->card, 0, &ops, au1000, &pbus)) < 0)
589 return err;
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 memset(&ac97, 0, sizeof(ac97));
592 ac97.private_data = au1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if ((err = snd_ac97_mixer(pbus, &ac97, &au1000->ac97)) < 0)
594 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599/*------------------------------ Setup / Destroy ----------------------------*/
600
601void
602snd_au1000_free(snd_card_t *card)
603{
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100604 au1000_t *au1000 = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 if (au1000->ac97_res_port) {
607 /* put internal AC97 block into reset */
608 au1000->ac97_ioport->cntrl = AC97C_RS;
609 au1000->ac97_ioport = NULL;
Takashi Iwaib1d57762005-10-10 11:56:31 +0200610 release_and_free_resource(au1000->ac97_res_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
613 if (au1000->stream[PLAYBACK]->dma >= 0)
614 free_au1000_dma(au1000->stream[PLAYBACK]->dma);
615
616 if (au1000->stream[CAPTURE]->dma >= 0)
617 free_au1000_dma(au1000->stream[CAPTURE]->dma);
618
619 kfree(au1000->stream[PLAYBACK]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 kfree(au1000->stream[CAPTURE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100623
624static snd_card_t *au1000_card;
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626static int __init
627au1000_init(void)
628{
629 int err;
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100630 snd_card_t *card;
631 au1000_t *au1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100633 card = snd_card_new(-1, "AC97", THIS_MODULE, sizeof(au1000_t));
634 if (card == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return -ENOMEM;
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100636
637 card->private_free = snd_au1000_free;
638 au1000 = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /* so that snd_au1000_free will work as intended */
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100640 au1000->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 au1000->stream[PLAYBACK]->dma = -1;
642 au1000->stream[CAPTURE]->dma = -1;
643 au1000->ac97_res_port = NULL;
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100644 au1000->stream[PLAYBACK] = kmalloc(sizeof(audio_stream_t), GFP_KERNEL);
645 au1000->stream[CAPTURE] = kmalloc(sizeof(audio_stream_t), GFP_KERNEL);
646 if (au1000->stream[PLAYBACK] == NULL ||
647 au1000->stream[CAPTURE] == NULL) {
648 snd_card_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return -ENOMEM;
650 }
651
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100652 if ((err = snd_au1000_ac97_new(au1000)) < 0 ) {
653 snd_card_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return err;
655 }
656
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100657 if ((err = snd_au1000_pcm_new(au1000)) < 0) {
658 snd_card_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return err;
660 }
661
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100662 strcpy(card->driver, "Au1000-AC97");
663 strcpy(card->shortname, "AMD Au1000-AC97");
664 sprintf(card->longname, "AMD Au1000--AC97 ALSA Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100666 if ((err = snd_card_set_generic_dev(card)) < 0) {
667 snd_card_free(card);
Takashi Iwai16dab542005-09-05 17:17:58 +0200668 return err;
669 }
670
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100671 if ((err = snd_card_register(card)) < 0) {
672 snd_card_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return err;
674 }
675
676 printk( KERN_INFO "ALSA AC97: Driver Initialized\n" );
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100677 au1000_card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return 0;
679}
680
681static void __exit au1000_exit(void)
682{
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100683 snd_card_free(au1000_card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
686module_init(au1000_init);
687module_exit(au1000_exit);
688