blob: fbcaa5434fd83d325dfe15ffb84451778b29bd6f [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/init.h>
Manuel Lauss7d132112009-03-29 11:47:06 +020040#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/slab.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040042#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <sound/core.h>
44#include <sound/initval.h>
45#include <sound/pcm.h>
Sergei Shtylylovd8327c72006-03-20 18:38:21 +010046#include <sound/pcm_params.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <sound/ac97_codec.h>
48#include <asm/mach-au1x00/au1000.h>
49#include <asm/mach-au1x00/au1000_dma.h>
50
51MODULE_AUTHOR("Charles Eidsness <charles@cooper-street.com>");
52MODULE_DESCRIPTION("Au1000 AC'97 ALSA Driver");
53MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -070054MODULE_SUPPORTED_DEVICE("{{AMD,Au1000 AC'97}}");
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define PLAYBACK 0
57#define CAPTURE 1
58#define AC97_SLOT_3 0x01
59#define AC97_SLOT_4 0x02
60#define AC97_SLOT_6 0x08
61#define AC97_CMD_IRQ 31
62#define READ 0
63#define WRITE 1
64#define READ_WAIT 2
65#define RW_DONE 3
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067struct au1000_period
68{
69 u32 start;
70 u32 relative_end; /*realtive to start of buffer*/
Takashi Iwaia0d6f882005-11-17 15:12:31 +010071 struct au1000_period * next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
74/*Au1000 AC97 Port Control Reisters*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070075struct au1000_ac97_reg {
76 u32 volatile config;
77 u32 volatile status;
78 u32 volatile data;
79 u32 volatile cmd;
80 u32 volatile cntrl;
81};
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083struct audio_stream {
Takashi Iwaia0d6f882005-11-17 15:12:31 +010084 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 int dma;
86 spinlock_t dma_lock;
Takashi Iwaia0d6f882005-11-17 15:12:31 +010087 struct au1000_period * buffer;
Takashi Iwai33ea25c2005-11-17 10:32:43 +010088 unsigned int period_size;
89 unsigned int periods;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090};
91
Takashi Iwaia0d6f882005-11-17 15:12:31 +010092struct snd_au1000 {
93 struct snd_card *card;
94 struct au1000_ac97_reg volatile *ac97_ioport;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 struct resource *ac97_res_port;
97 spinlock_t ac97_lock;
Takashi Iwaia0d6f882005-11-17 15:12:31 +010098 struct snd_ac97 *ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100100 struct snd_pcm *pcm;
101 struct audio_stream *stream[2]; /* playback & capture */
Manuel Lauss7d132112009-03-29 11:47:06 +0200102 int dmaid[2]; /* tx(0)/rx(1) DMA ids */
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100103};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*--------------------------- Local Functions --------------------------------*/
106static void
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100107au1000_set_ac97_xmit_slots(struct snd_au1000 *au1000, long xmit_slots)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 u32 volatile ac97_config;
110
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);
117}
118
119static void
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100120au1000_set_ac97_recv_slots(struct snd_au1000 *au1000, long recv_slots)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 u32 volatile ac97_config;
123
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);
130}
131
132
133static void
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100134au1000_release_dma_link(struct audio_stream *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100136 struct au1000_period * pointer;
137 struct au1000_period * pointer_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100139 stream->period_size = 0;
140 stream->periods = 0;
141 pointer = stream->buffer;
142 if (! pointer)
143 return;
144 do {
145 pointer_next = pointer->next;
146 kfree(pointer);
147 pointer = pointer_next;
148 } while (pointer != stream->buffer);
149 stream->buffer = NULL;
150}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100152static int
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100153au1000_setup_dma_link(struct audio_stream *stream, unsigned int period_bytes,
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100154 unsigned int periods)
155{
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100156 struct snd_pcm_substream *substream = stream->substream;
157 struct snd_pcm_runtime *runtime = substream->runtime;
Sergei Shtylylovd8327c72006-03-20 18:38:21 +0100158 struct au1000_period *pointer;
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100159 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
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100173 stream->buffer = kmalloc(sizeof(struct au1000_period), GFP_KERNEL);
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100174 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
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100194au1000_dma_stop(struct audio_stream *stream)
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100195{
Takashi Iwai5e246b82008-08-08 17:12:47 +0200196 if (snd_BUG_ON(!stream->buffer))
197 return;
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100198 disable_dma(stream->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201static void
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100202au1000_dma_start(struct audio_stream *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Takashi Iwai5e246b82008-08-08 17:12:47 +0200204 if (snd_BUG_ON(!stream->buffer))
205 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100207 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);
214 } else {
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100221 enable_dma_buffers(stream->dma);
222 start_dma(stream->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100226au1000_dma_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100228 struct audio_stream *stream = (struct audio_stream *) dev_id;
229 struct snd_pcm_substream *substream = stream->substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 spin_lock(&stream->dma_lock);
232 switch (get_dma_buffer_done(stream->dma)) {
233 case DMA_D0:
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);
239 break;
240 case DMA_D1:
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);
246 break;
247 case (DMA_D0 | DMA_D1):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 printk(KERN_ERR "DMA %d missed interrupt.\n",stream->dma);
249 au1000_dma_stop(stream);
250 au1000_dma_start(stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 break;
252 case (~DMA_D0 & ~DMA_D1):
253 printk(KERN_ERR "DMA %d empty irq.\n",stream->dma);
254 }
255 spin_unlock(&stream->dma_lock);
256 snd_pcm_period_elapsed(substream);
257 return IRQ_HANDLED;
258}
259
260/*-------------------------- PCM Audio Streams -------------------------------*/
261
262static unsigned int rates[] = {8000, 11025, 16000, 22050};
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100263static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
Tobias Klauser2ad34792006-09-29 02:00:20 -0700264 .count = ARRAY_SIZE(rates),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 .list = rates,
266 .mask = 0,
267};
268
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100269static struct snd_pcm_hardware snd_au1000_hw =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
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),
276 .rate_min = 8000,
277 .rate_max = 22050,
278 .channels_min = 1,
279 .channels_max = 2,
280 .buffer_bytes_max = 128*1024,
281 .period_bytes_min = 32,
282 .period_bytes_max = 16*1024,
283 .periods_min = 8,
284 .periods_max = 255,
285 .fifo_size = 16,
286};
287
288static int
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100289snd_au1000_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100291 struct snd_au1000 *au1000 = substream->pcm->private_data;
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 au1000->stream[PLAYBACK]->substream = substream;
294 au1000->stream[PLAYBACK]->buffer = NULL;
295 substream->private_data = au1000->stream[PLAYBACK];
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100296 substream->runtime->hw = snd_au1000_hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return (snd_pcm_hw_constraint_list(substream->runtime, 0,
298 SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates) < 0);
299}
300
301static int
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100302snd_au1000_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100304 struct snd_au1000 *au1000 = substream->pcm->private_data;
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 au1000->stream[CAPTURE]->substream = substream;
307 au1000->stream[CAPTURE]->buffer = NULL;
308 substream->private_data = au1000->stream[CAPTURE];
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100309 substream->runtime->hw = snd_au1000_hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return (snd_pcm_hw_constraint_list(substream->runtime, 0,
311 SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates) < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314static int
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100315snd_au1000_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100317 struct snd_au1000 *au1000 = substream->pcm->private_data;
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 au1000->stream[PLAYBACK]->substream = NULL;
320 return 0;
321}
322
323static int
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100324snd_au1000_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100326 struct snd_au1000 *au1000 = substream->pcm->private_data;
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 au1000->stream[CAPTURE]->substream = NULL;
329 return 0;
330}
331
332static int
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100333snd_au1000_hw_params(struct snd_pcm_substream *substream,
334 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100336 struct audio_stream *stream = substream->private_data;
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100337 int err;
338
339 err = snd_pcm_lib_malloc_pages(substream,
340 params_buffer_bytes(hw_params));
341 if (err < 0)
342 return err;
343 return au1000_setup_dma_link(stream,
344 params_period_bytes(hw_params),
345 params_periods(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
348static int
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100349snd_au1000_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100351 struct audio_stream *stream = substream->private_data;
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100352 au1000_release_dma_link(stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return snd_pcm_lib_free_pages(substream);
354}
355
356static int
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100357snd_au1000_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100359 struct snd_au1000 *au1000 = substream->pcm->private_data;
360 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100362 if (runtime->channels == 1)
363 au1000_set_ac97_xmit_slots(au1000, AC97_SLOT_4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 else
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100365 au1000_set_ac97_xmit_slots(au1000, AC97_SLOT_3 | AC97_SLOT_4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 snd_ac97_set_rate(au1000->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
367 return 0;
368}
369
370static int
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100371snd_au1000_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100373 struct snd_au1000 *au1000 = substream->pcm->private_data;
374 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100376 if (runtime->channels == 1)
377 au1000_set_ac97_recv_slots(au1000, AC97_SLOT_4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 else
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100379 au1000_set_ac97_recv_slots(au1000, AC97_SLOT_3 | AC97_SLOT_4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 snd_ac97_set_rate(au1000->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
381 return 0;
382}
383
384static int
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100385snd_au1000_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100387 struct audio_stream *stream = substream->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 int err = 0;
389
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100390 spin_lock(&stream->dma_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 switch (cmd) {
392 case SNDRV_PCM_TRIGGER_START:
393 au1000_dma_start(stream);
394 break;
395 case SNDRV_PCM_TRIGGER_STOP:
396 au1000_dma_stop(stream);
397 break;
398 default:
399 err = -EINVAL;
400 break;
401 }
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100402 spin_unlock(&stream->dma_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return err;
404}
405
406static snd_pcm_uframes_t
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100407snd_au1000_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100409 struct audio_stream *stream = substream->private_data;
410 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 long location;
412
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100413 spin_lock(&stream->dma_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 location = get_dma_residue(stream->dma);
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100415 spin_unlock(&stream->dma_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 location = stream->buffer->relative_end - location;
417 if (location == -1)
418 location = 0;
419 return bytes_to_frames(runtime,location);
420}
421
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100422static struct snd_pcm_ops snd_card_au1000_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 .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,
431};
432
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100433static struct snd_pcm_ops snd_card_au1000_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 .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,
442};
443
Bill Pembertone0f8cb52012-12-06 12:35:15 -0500444static int
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100445snd_au1000_pcm_new(struct snd_au1000 *au1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100447 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int err;
449 unsigned long flags;
450
451 if ((err = snd_pcm_new(au1000->card, "AU1000 AC97 PCM", 0, 1, 1, &pcm)) < 0)
452 return err;
453
454 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
455 snd_dma_continuous_data(GFP_KERNEL), 128*1024, 128*1024);
456
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);
461
462 pcm->private_data = au1000;
463 pcm->info_flags = 0;
464 strcpy(pcm->name, "Au1000 AC97 PCM");
465
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100466 spin_lock_init(&au1000->stream[PLAYBACK]->dma_lock);
467 spin_lock_init(&au1000->stream[CAPTURE]->dma_lock);
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 flags = claim_dma_lock();
Manuel Lauss7d132112009-03-29 11:47:06 +0200470 au1000->stream[PLAYBACK]->dma = request_au1000_dma(au1000->dmaid[0],
Yong Zhang88e24c32011-09-22 16:59:20 +0800471 "AC97 TX", au1000_dma_interrupt, 0,
Manuel Lauss7d132112009-03-29 11:47:06 +0200472 au1000->stream[PLAYBACK]);
473 if (au1000->stream[PLAYBACK]->dma < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 release_dma_lock(flags);
475 return -EBUSY;
476 }
Manuel Lauss7d132112009-03-29 11:47:06 +0200477 au1000->stream[CAPTURE]->dma = request_au1000_dma(au1000->dmaid[1],
Yong Zhang88e24c32011-09-22 16:59:20 +0800478 "AC97 RX", au1000_dma_interrupt, 0,
Manuel Lauss7d132112009-03-29 11:47:06 +0200479 au1000->stream[CAPTURE]);
480 if (au1000->stream[CAPTURE]->dma < 0){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 release_dma_lock(flags);
482 return -EBUSY;
483 }
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 au1000->pcm = pcm;
491 return 0;
492}
493
494
495/*-------------------------- AC97 CODEC Control ------------------------------*/
496
497static unsigned short
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100498snd_au1000_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100500 struct snd_au1000 *au1000 = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 u32 volatile cmd;
502 u16 volatile data;
503 int i;
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100504
Konstantin Baydarov708f9972005-10-27 17:25:02 +0200505 spin_lock(&au1000->ac97_lock);
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200506/* would rather use the interrupt than this polling but it works and I can't
507get the interrupt driven case to work efficiently */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 for (i = 0; i < 0x5000; i++)
509 if (!(au1000->ac97_ioport->status & AC97C_CP))
510 break;
511 if (i == 0x5000)
512 printk(KERN_ERR "au1000 AC97: AC97 command read timeout\n");
513
514 cmd = (u32) reg & AC97C_INDEX_MASK;
515 cmd |= AC97C_READ;
516 au1000->ac97_ioport->cmd = cmd;
517
518 /* now wait for the data */
519 for (i = 0; i < 0x5000; i++)
520 if (!(au1000->ac97_ioport->status & AC97C_CP))
521 break;
522 if (i == 0x5000) {
523 printk(KERN_ERR "au1000 AC97: AC97 command read timeout\n");
Julia Lawall1efddcc2010-05-26 17:59:27 +0200524 spin_unlock(&au1000->ac97_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return 0;
526 }
527
528 data = au1000->ac97_ioport->cmd & 0xffff;
Konstantin Baydarov708f9972005-10-27 17:25:02 +0200529 spin_unlock(&au1000->ac97_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 return data;
532
533}
534
535
536static void
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100537snd_au1000_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100539 struct snd_au1000 *au1000 = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 u32 cmd;
541 int i;
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100542
Konstantin Baydarov708f9972005-10-27 17:25:02 +0200543 spin_lock(&au1000->ac97_lock);
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200544/* would rather use the interrupt than this polling but it works and I can't
545get the interrupt driven case to work efficiently */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 for (i = 0; i < 0x5000; i++)
547 if (!(au1000->ac97_ioport->status & AC97C_CP))
548 break;
549 if (i == 0x5000)
550 printk(KERN_ERR "au1000 AC97: AC97 command write timeout\n");
551
552 cmd = (u32) reg & AC97C_INDEX_MASK;
553 cmd &= ~AC97C_READ;
554 cmd |= ((u32) val << AC97C_WD_BIT);
555 au1000->ac97_ioport->cmd = cmd;
Konstantin Baydarov708f9972005-10-27 17:25:02 +0200556 spin_unlock(&au1000->ac97_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Manuel Lauss7d132112009-03-29 11:47:06 +0200559/*------------------------------ Setup / Destroy ----------------------------*/
560
561static void snd_au1000_free(struct snd_card *card)
562{
563 struct snd_au1000 *au1000 = card->private_data;
564
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]);
569 }
570
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]);
575 }
576
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;
583 }
584 release_and_free_resource(au1000->ac97_res_port);
585 au1000->ac97_res_port = NULL;
586 }
587}
588
589static struct snd_ac97_bus_ops ops = {
590 .write = snd_au1000_ac97_write,
591 .read = snd_au1000_ac97_read,
592};
593
594static int au1000_ac97_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 int err;
Manuel Lauss7d132112009-03-29 11:47:06 +0200597 void __iomem *io;
598 struct resource *r;
599 struct snd_card *card;
600 struct snd_au1000 *au1000;
Takashi Iwaia0d6f882005-11-17 15:12:31 +0100601 struct snd_ac97_bus *pbus;
602 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Takashi Iwaibee1bb12014-01-29 14:34:47 +0100604 err = snd_card_new(&pdev->dev, -1, "AC97", THIS_MODULE,
605 sizeof(struct snd_au1000), &card);
Manuel Lauss7d132112009-03-29 11:47:06 +0200606 if (err < 0)
607 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Manuel Lauss7d132112009-03-29 11:47:06 +0200609 au1000 = card->private_data;
610 au1000->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 spin_lock_init(&au1000->ac97_lock);
612
Manuel Lauss7d132112009-03-29 11:47:06 +0200613 /* from here on let ALSA call the special freeing function */
614 card->private_free = snd_au1000_free;
615
616 /* TX DMA ID */
617 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
618 if (!r) {
619 err = -ENODEV;
620 snd_printk(KERN_INFO "no TX DMA platform resource!\n");
621 goto out;
622 }
623 au1000->dmaid[0] = r->start;
624
625 /* RX DMA ID */
626 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
627 if (!r) {
628 err = -ENODEV;
629 snd_printk(KERN_INFO "no RX DMA platform resource!\n");
630 goto out;
631 }
632 au1000->dmaid[1] = r->start;
633
634 au1000->stream[PLAYBACK] = kmalloc(sizeof(struct audio_stream),
635 GFP_KERNEL);
636 if (!au1000->stream[PLAYBACK])
637 goto out;
638 au1000->stream[PLAYBACK]->dma = -1;
639
640 au1000->stream[CAPTURE] = kmalloc(sizeof(struct audio_stream),
641 GFP_KERNEL);
642 if (!au1000->stream[CAPTURE])
643 goto out;
644 au1000->stream[CAPTURE]->dma = -1;
645
646 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
647 if (!r)
648 goto out;
649
650 err = -EBUSY;
Benoit Taine82285f22014-05-28 17:14:06 +0200651 au1000->ac97_res_port = request_mem_region(r->start, resource_size(r),
652 pdev->name);
Manuel Lauss7d132112009-03-29 11:47:06 +0200653 if (!au1000->ac97_res_port) {
654 snd_printk(KERN_ERR "ALSA AC97: can't grab AC97 port\n");
655 goto out;
656 }
657
Benoit Taine82285f22014-05-28 17:14:06 +0200658 io = ioremap(r->start, resource_size(r));
Manuel Lauss7d132112009-03-29 11:47:06 +0200659 if (!io)
660 goto out;
661
662 au1000->ac97_ioport = (struct au1000_ac97_reg *)io;
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /* configure pins for AC'97
665 TODO: move to board_setup.c */
666 au_writel(au_readl(SYS_PINFUNC) & ~0x02, SYS_PINFUNC);
667
668 /* Initialise Au1000's AC'97 Control Block */
669 au1000->ac97_ioport->cntrl = AC97C_RS | AC97C_CE;
670 udelay(10);
671 au1000->ac97_ioport->cntrl = AC97C_CE;
672 udelay(10);
673
674 /* Initialise External CODEC -- cold reset */
675 au1000->ac97_ioport->config = AC97C_RESET;
676 udelay(10);
677 au1000->ac97_ioport->config = 0x0;
678 mdelay(5);
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /* Initialise AC97 middle-layer */
Manuel Lauss7d132112009-03-29 11:47:06 +0200681 err = snd_ac97_bus(au1000->card, 0, &ops, au1000, &pbus);
682 if (err < 0)
683 goto out;
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 memset(&ac97, 0, sizeof(ac97));
686 ac97.private_data = au1000;
Manuel Lauss7d132112009-03-29 11:47:06 +0200687 err = snd_ac97_mixer(pbus, &ac97, &au1000->ac97);
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100688 if (err < 0)
Manuel Lauss7d132112009-03-29 11:47:06 +0200689 goto out;
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100690
Manuel Lauss7d132112009-03-29 11:47:06 +0200691 err = snd_au1000_pcm_new(au1000);
692 if (err < 0)
693 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Takashi Iwai33ea25c2005-11-17 10:32:43 +0100695 strcpy(card->driver, "Au1000-AC97");
696 strcpy(card->shortname, "AMD Au1000-AC97");
697 sprintf(card->longname, "AMD Au1000--AC97 ALSA Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Manuel Lauss7d132112009-03-29 11:47:06 +0200699 err = snd_card_register(card);
700 if (err < 0)
701 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Takashi Iwai2ebfb8e2009-02-05 16:11:58 +0100703 printk(KERN_INFO "ALSA AC97: Driver Initialized\n");
Manuel Lauss7d132112009-03-29 11:47:06 +0200704
705 platform_set_drvdata(pdev, card);
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return 0;
Manuel Lauss7d132112009-03-29 11:47:06 +0200708
709 out:
710 snd_card_free(card);
711 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
Manuel Lauss7d132112009-03-29 11:47:06 +0200714static int au1000_ac97_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Manuel Lauss7d132112009-03-29 11:47:06 +0200716 return snd_card_free(platform_get_drvdata(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
Manuel Lauss7d132112009-03-29 11:47:06 +0200719struct platform_driver au1000_ac97c_driver = {
720 .driver = {
721 .name = "au1000-ac97c",
722 .owner = THIS_MODULE,
723 },
724 .probe = au1000_ac97_probe,
725 .remove = au1000_ac97_remove,
726};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Christoph Jaeger34f980632014-04-09 09:42:32 +0200728module_platform_driver(au1000_ac97c_driver);