Linux Audio

Check our new training course

Linux BSP upgrade and security maintenance

Need help to get security updates for your Linux BSP?
Loading...
v4.6
  1/*
  2 *   Sound driver for Silicon Graphics O2 Workstations A/V board audio.
  3 *
  4 *   Copyright 2003 Vivien Chappelier <vivien.chappelier@linux-mips.org>
  5 *   Copyright 2008 Thomas Bogendoerfer <tsbogend@alpha.franken.de>
  6 *   Mxier part taken from mace_audio.c:
  7 *   Copyright 2007 Thorben Jändling <tj.trevelyan@gmail.com>
  8 *
  9 *   This program is free software; you can redistribute it and/or modify
 10 *   it under the terms of the GNU General Public License as published by
 11 *   the Free Software Foundation; either version 2 of the License, or
 12 *   (at your option) any later version.
 13 *
 14 *   This program is distributed in the hope that it will be useful,
 15 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17 *   GNU General Public License for more details.
 18 *
 19 *   You should have received a copy of the GNU General Public License
 20 *   along with this program; if not, write to the Free Software
 21 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 22 *
 23 */
 24
 25#include <linux/init.h>
 26#include <linux/delay.h>
 27#include <linux/spinlock.h>
 28#include <linux/interrupt.h>
 29#include <linux/dma-mapping.h>
 30#include <linux/platform_device.h>
 31#include <linux/io.h>
 32#include <linux/slab.h>
 33#include <linux/module.h>
 34
 35#include <asm/ip32/ip32_ints.h>
 36#include <asm/ip32/mace.h>
 37
 38#include <sound/core.h>
 39#include <sound/control.h>
 40#include <sound/pcm.h>
 41#define SNDRV_GET_ID
 42#include <sound/initval.h>
 43#include <sound/ad1843.h>
 44
 45
 46MODULE_AUTHOR("Vivien Chappelier <vivien.chappelier@linux-mips.org>");
 47MODULE_DESCRIPTION("SGI O2 Audio");
 48MODULE_LICENSE("GPL");
 49MODULE_SUPPORTED_DEVICE("{{Silicon Graphics, O2 Audio}}");
 50
 51static int index = SNDRV_DEFAULT_IDX1;  /* Index 0-MAX */
 52static char *id = SNDRV_DEFAULT_STR1;   /* ID for this card */
 53
 54module_param(index, int, 0444);
 55MODULE_PARM_DESC(index, "Index value for SGI O2 soundcard.");
 56module_param(id, charp, 0444);
 57MODULE_PARM_DESC(id, "ID string for SGI O2 soundcard.");
 58
 59
 60#define AUDIO_CONTROL_RESET              BIT(0) /* 1: reset audio interface */
 61#define AUDIO_CONTROL_CODEC_PRESENT      BIT(1) /* 1: codec detected */
 62
 63#define CODEC_CONTROL_WORD_SHIFT        0
 64#define CODEC_CONTROL_READ              BIT(16)
 65#define CODEC_CONTROL_ADDRESS_SHIFT     17
 66
 67#define CHANNEL_CONTROL_RESET           BIT(10) /* 1: reset channel */
 68#define CHANNEL_DMA_ENABLE              BIT(9)  /* 1: enable DMA transfer */
 69#define CHANNEL_INT_THRESHOLD_DISABLED  (0 << 5) /* interrupt disabled */
 70#define CHANNEL_INT_THRESHOLD_25        (1 << 5) /* int on buffer >25% full */
 71#define CHANNEL_INT_THRESHOLD_50        (2 << 5) /* int on buffer >50% full */
 72#define CHANNEL_INT_THRESHOLD_75        (3 << 5) /* int on buffer >75% full */
 73#define CHANNEL_INT_THRESHOLD_EMPTY     (4 << 5) /* int on buffer empty */
 74#define CHANNEL_INT_THRESHOLD_NOT_EMPTY (5 << 5) /* int on buffer !empty */
 75#define CHANNEL_INT_THRESHOLD_FULL      (6 << 5) /* int on buffer empty */
 76#define CHANNEL_INT_THRESHOLD_NOT_FULL  (7 << 5) /* int on buffer !empty */
 77
 78#define CHANNEL_RING_SHIFT              12
 79#define CHANNEL_RING_SIZE               (1 << CHANNEL_RING_SHIFT)
 80#define CHANNEL_RING_MASK               (CHANNEL_RING_SIZE - 1)
 81
 82#define CHANNEL_LEFT_SHIFT 40
 83#define CHANNEL_RIGHT_SHIFT 8
 84
 85struct snd_sgio2audio_chan {
 86	int idx;
 87	struct snd_pcm_substream *substream;
 88	int pos;
 89	snd_pcm_uframes_t size;
 90	spinlock_t lock;
 91};
 92
 93/* definition of the chip-specific record */
 94struct snd_sgio2audio {
 95	struct snd_card *card;
 96
 97	/* codec */
 98	struct snd_ad1843 ad1843;
 99	spinlock_t ad1843_lock;
100
101	/* channels */
102	struct snd_sgio2audio_chan channel[3];
103
104	/* resources */
105	void *ring_base;
106	dma_addr_t ring_base_dma;
107};
108
109/* AD1843 access */
110
111/*
112 * read_ad1843_reg returns the current contents of a 16 bit AD1843 register.
113 *
114 * Returns unsigned register value on success, -errno on failure.
115 */
116static int read_ad1843_reg(void *priv, int reg)
117{
118	struct snd_sgio2audio *chip = priv;
119	int val;
120	unsigned long flags;
121
122	spin_lock_irqsave(&chip->ad1843_lock, flags);
123
124	writeq((reg << CODEC_CONTROL_ADDRESS_SHIFT) |
125	       CODEC_CONTROL_READ, &mace->perif.audio.codec_control);
126	wmb();
127	val = readq(&mace->perif.audio.codec_control); /* flush bus */
128	udelay(200);
129
130	val = readq(&mace->perif.audio.codec_read);
131
132	spin_unlock_irqrestore(&chip->ad1843_lock, flags);
133	return val;
134}
135
136/*
137 * write_ad1843_reg writes the specified value to a 16 bit AD1843 register.
138 */
139static int write_ad1843_reg(void *priv, int reg, int word)
140{
141	struct snd_sgio2audio *chip = priv;
142	int val;
143	unsigned long flags;
144
145	spin_lock_irqsave(&chip->ad1843_lock, flags);
146
147	writeq((reg << CODEC_CONTROL_ADDRESS_SHIFT) |
148	       (word << CODEC_CONTROL_WORD_SHIFT),
149	       &mace->perif.audio.codec_control);
150	wmb();
151	val = readq(&mace->perif.audio.codec_control); /* flush bus */
152	udelay(200);
153
154	spin_unlock_irqrestore(&chip->ad1843_lock, flags);
155	return 0;
156}
157
158static int sgio2audio_gain_info(struct snd_kcontrol *kcontrol,
159			       struct snd_ctl_elem_info *uinfo)
160{
161	struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
162
163	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
164	uinfo->count = 2;
165	uinfo->value.integer.min = 0;
166	uinfo->value.integer.max = ad1843_get_gain_max(&chip->ad1843,
167					     (int)kcontrol->private_value);
168	return 0;
169}
170
171static int sgio2audio_gain_get(struct snd_kcontrol *kcontrol,
172			       struct snd_ctl_elem_value *ucontrol)
173{
174	struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
175	int vol;
176
177	vol = ad1843_get_gain(&chip->ad1843, (int)kcontrol->private_value);
178
179	ucontrol->value.integer.value[0] = (vol >> 8) & 0xFF;
180	ucontrol->value.integer.value[1] = vol & 0xFF;
181
182	return 0;
183}
184
185static int sgio2audio_gain_put(struct snd_kcontrol *kcontrol,
186			struct snd_ctl_elem_value *ucontrol)
187{
188	struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
189	int newvol, oldvol;
190
191	oldvol = ad1843_get_gain(&chip->ad1843, kcontrol->private_value);
192	newvol = (ucontrol->value.integer.value[0] << 8) |
193		ucontrol->value.integer.value[1];
194
195	newvol = ad1843_set_gain(&chip->ad1843, kcontrol->private_value,
196		newvol);
197
198	return newvol != oldvol;
199}
200
201static int sgio2audio_source_info(struct snd_kcontrol *kcontrol,
202			       struct snd_ctl_elem_info *uinfo)
203{
204	static const char * const texts[3] = {
205		"Cam Mic", "Mic", "Line"
206	};
207	return snd_ctl_enum_info(uinfo, 1, 3, texts);
 
 
 
 
 
 
 
208}
209
210static int sgio2audio_source_get(struct snd_kcontrol *kcontrol,
211			       struct snd_ctl_elem_value *ucontrol)
212{
213	struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
214
215	ucontrol->value.enumerated.item[0] = ad1843_get_recsrc(&chip->ad1843);
216	return 0;
217}
218
219static int sgio2audio_source_put(struct snd_kcontrol *kcontrol,
220			struct snd_ctl_elem_value *ucontrol)
221{
222	struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
223	int newsrc, oldsrc;
224
225	oldsrc = ad1843_get_recsrc(&chip->ad1843);
226	newsrc = ad1843_set_recsrc(&chip->ad1843,
227				   ucontrol->value.enumerated.item[0]);
228
229	return newsrc != oldsrc;
230}
231
232/* dac1/pcm0 mixer control */
233static struct snd_kcontrol_new sgio2audio_ctrl_pcm0 = {
234	.iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
235	.name           = "PCM Playback Volume",
236	.index          = 0,
237	.access         = SNDRV_CTL_ELEM_ACCESS_READWRITE,
238	.private_value  = AD1843_GAIN_PCM_0,
239	.info           = sgio2audio_gain_info,
240	.get            = sgio2audio_gain_get,
241	.put            = sgio2audio_gain_put,
242};
243
244/* dac2/pcm1 mixer control */
245static struct snd_kcontrol_new sgio2audio_ctrl_pcm1 = {
246	.iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
247	.name           = "PCM Playback Volume",
248	.index          = 1,
249	.access         = SNDRV_CTL_ELEM_ACCESS_READWRITE,
250	.private_value  = AD1843_GAIN_PCM_1,
251	.info           = sgio2audio_gain_info,
252	.get            = sgio2audio_gain_get,
253	.put            = sgio2audio_gain_put,
254};
255
256/* record level mixer control */
257static struct snd_kcontrol_new sgio2audio_ctrl_reclevel = {
258	.iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
259	.name           = "Capture Volume",
260	.access         = SNDRV_CTL_ELEM_ACCESS_READWRITE,
261	.private_value  = AD1843_GAIN_RECLEV,
262	.info           = sgio2audio_gain_info,
263	.get            = sgio2audio_gain_get,
264	.put            = sgio2audio_gain_put,
265};
266
267/* record level source control */
268static struct snd_kcontrol_new sgio2audio_ctrl_recsource = {
269	.iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
270	.name           = "Capture Source",
271	.access         = SNDRV_CTL_ELEM_ACCESS_READWRITE,
272	.info           = sgio2audio_source_info,
273	.get            = sgio2audio_source_get,
274	.put            = sgio2audio_source_put,
275};
276
277/* line mixer control */
278static struct snd_kcontrol_new sgio2audio_ctrl_line = {
279	.iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
280	.name           = "Line Playback Volume",
281	.index          = 0,
282	.access         = SNDRV_CTL_ELEM_ACCESS_READWRITE,
283	.private_value  = AD1843_GAIN_LINE,
284	.info           = sgio2audio_gain_info,
285	.get            = sgio2audio_gain_get,
286	.put            = sgio2audio_gain_put,
287};
288
289/* cd mixer control */
290static struct snd_kcontrol_new sgio2audio_ctrl_cd = {
291	.iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
292	.name           = "Line Playback Volume",
293	.index          = 1,
294	.access         = SNDRV_CTL_ELEM_ACCESS_READWRITE,
295	.private_value  = AD1843_GAIN_LINE_2,
296	.info           = sgio2audio_gain_info,
297	.get            = sgio2audio_gain_get,
298	.put            = sgio2audio_gain_put,
299};
300
301/* mic mixer control */
302static struct snd_kcontrol_new sgio2audio_ctrl_mic = {
303	.iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
304	.name           = "Mic Playback Volume",
305	.access         = SNDRV_CTL_ELEM_ACCESS_READWRITE,
306	.private_value  = AD1843_GAIN_MIC,
307	.info           = sgio2audio_gain_info,
308	.get            = sgio2audio_gain_get,
309	.put            = sgio2audio_gain_put,
310};
311
312
313static int snd_sgio2audio_new_mixer(struct snd_sgio2audio *chip)
314{
315	int err;
316
317	err = snd_ctl_add(chip->card,
318			  snd_ctl_new1(&sgio2audio_ctrl_pcm0, chip));
319	if (err < 0)
320		return err;
321
322	err = snd_ctl_add(chip->card,
323			  snd_ctl_new1(&sgio2audio_ctrl_pcm1, chip));
324	if (err < 0)
325		return err;
326
327	err = snd_ctl_add(chip->card,
328			  snd_ctl_new1(&sgio2audio_ctrl_reclevel, chip));
329	if (err < 0)
330		return err;
331
332	err = snd_ctl_add(chip->card,
333			  snd_ctl_new1(&sgio2audio_ctrl_recsource, chip));
334	if (err < 0)
335		return err;
336	err = snd_ctl_add(chip->card,
337			  snd_ctl_new1(&sgio2audio_ctrl_line, chip));
338	if (err < 0)
339		return err;
340
341	err = snd_ctl_add(chip->card,
342			  snd_ctl_new1(&sgio2audio_ctrl_cd, chip));
343	if (err < 0)
344		return err;
345
346	err = snd_ctl_add(chip->card,
347			  snd_ctl_new1(&sgio2audio_ctrl_mic, chip));
348	if (err < 0)
349		return err;
350
351	return 0;
352}
353
354/* low-level audio interface DMA */
355
356/* get data out of bounce buffer, count must be a multiple of 32 */
357/* returns 1 if a period has elapsed */
358static int snd_sgio2audio_dma_pull_frag(struct snd_sgio2audio *chip,
359					unsigned int ch, unsigned int count)
360{
361	int ret;
362	unsigned long src_base, src_pos, dst_mask;
363	unsigned char *dst_base;
364	int dst_pos;
365	u64 *src;
366	s16 *dst;
367	u64 x;
368	unsigned long flags;
369	struct snd_pcm_runtime *runtime = chip->channel[ch].substream->runtime;
370
371	spin_lock_irqsave(&chip->channel[ch].lock, flags);
372
373	src_base = (unsigned long) chip->ring_base | (ch << CHANNEL_RING_SHIFT);
374	src_pos = readq(&mace->perif.audio.chan[ch].read_ptr);
375	dst_base = runtime->dma_area;
376	dst_pos = chip->channel[ch].pos;
377	dst_mask = frames_to_bytes(runtime, runtime->buffer_size) - 1;
378
379	/* check if a period has elapsed */
380	chip->channel[ch].size += (count >> 3); /* in frames */
381	ret = chip->channel[ch].size >= runtime->period_size;
382	chip->channel[ch].size %= runtime->period_size;
383
384	while (count) {
385		src = (u64 *)(src_base + src_pos);
386		dst = (s16 *)(dst_base + dst_pos);
387
388		x = *src;
389		dst[0] = (x >> CHANNEL_LEFT_SHIFT) & 0xffff;
390		dst[1] = (x >> CHANNEL_RIGHT_SHIFT) & 0xffff;
391
392		src_pos = (src_pos + sizeof(u64)) & CHANNEL_RING_MASK;
393		dst_pos = (dst_pos + 2 * sizeof(s16)) & dst_mask;
394		count -= sizeof(u64);
395	}
396
397	writeq(src_pos, &mace->perif.audio.chan[ch].read_ptr); /* in bytes */
398	chip->channel[ch].pos = dst_pos;
399
400	spin_unlock_irqrestore(&chip->channel[ch].lock, flags);
401	return ret;
402}
403
404/* put some DMA data in bounce buffer, count must be a multiple of 32 */
405/* returns 1 if a period has elapsed */
406static int snd_sgio2audio_dma_push_frag(struct snd_sgio2audio *chip,
407					unsigned int ch, unsigned int count)
408{
409	int ret;
410	s64 l, r;
411	unsigned long dst_base, dst_pos, src_mask;
412	unsigned char *src_base;
413	int src_pos;
414	u64 *dst;
415	s16 *src;
416	unsigned long flags;
417	struct snd_pcm_runtime *runtime = chip->channel[ch].substream->runtime;
418
419	spin_lock_irqsave(&chip->channel[ch].lock, flags);
420
421	dst_base = (unsigned long)chip->ring_base | (ch << CHANNEL_RING_SHIFT);
422	dst_pos = readq(&mace->perif.audio.chan[ch].write_ptr);
423	src_base = runtime->dma_area;
424	src_pos = chip->channel[ch].pos;
425	src_mask = frames_to_bytes(runtime, runtime->buffer_size) - 1;
426
427	/* check if a period has elapsed */
428	chip->channel[ch].size += (count >> 3); /* in frames */
429	ret = chip->channel[ch].size >= runtime->period_size;
430	chip->channel[ch].size %= runtime->period_size;
431
432	while (count) {
433		src = (s16 *)(src_base + src_pos);
434		dst = (u64 *)(dst_base + dst_pos);
435
436		l = src[0]; /* sign extend */
437		r = src[1]; /* sign extend */
438
439		*dst = ((l & 0x00ffffff) << CHANNEL_LEFT_SHIFT) |
440			((r & 0x00ffffff) << CHANNEL_RIGHT_SHIFT);
441
442		dst_pos = (dst_pos + sizeof(u64)) & CHANNEL_RING_MASK;
443		src_pos = (src_pos + 2 * sizeof(s16)) & src_mask;
444		count -= sizeof(u64);
445	}
446
447	writeq(dst_pos, &mace->perif.audio.chan[ch].write_ptr); /* in bytes */
448	chip->channel[ch].pos = src_pos;
449
450	spin_unlock_irqrestore(&chip->channel[ch].lock, flags);
451	return ret;
452}
453
454static int snd_sgio2audio_dma_start(struct snd_pcm_substream *substream)
455{
456	struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
457	struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
458	int ch = chan->idx;
459
460	/* reset DMA channel */
461	writeq(CHANNEL_CONTROL_RESET, &mace->perif.audio.chan[ch].control);
462	udelay(10);
463	writeq(0, &mace->perif.audio.chan[ch].control);
464
465	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
466		/* push a full buffer */
467		snd_sgio2audio_dma_push_frag(chip, ch, CHANNEL_RING_SIZE - 32);
468	}
469	/* set DMA to wake on 50% empty and enable interrupt */
470	writeq(CHANNEL_DMA_ENABLE | CHANNEL_INT_THRESHOLD_50,
471	       &mace->perif.audio.chan[ch].control);
472	return 0;
473}
474
475static int snd_sgio2audio_dma_stop(struct snd_pcm_substream *substream)
476{
477	struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
478
479	writeq(0, &mace->perif.audio.chan[chan->idx].control);
480	return 0;
481}
482
483static irqreturn_t snd_sgio2audio_dma_in_isr(int irq, void *dev_id)
484{
485	struct snd_sgio2audio_chan *chan = dev_id;
486	struct snd_pcm_substream *substream;
487	struct snd_sgio2audio *chip;
488	int count, ch;
489
490	substream = chan->substream;
491	chip = snd_pcm_substream_chip(substream);
492	ch = chan->idx;
493
494	/* empty the ring */
495	count = CHANNEL_RING_SIZE -
496		readq(&mace->perif.audio.chan[ch].depth) - 32;
497	if (snd_sgio2audio_dma_pull_frag(chip, ch, count))
498		snd_pcm_period_elapsed(substream);
499
500	return IRQ_HANDLED;
501}
502
503static irqreturn_t snd_sgio2audio_dma_out_isr(int irq, void *dev_id)
504{
505	struct snd_sgio2audio_chan *chan = dev_id;
506	struct snd_pcm_substream *substream;
507	struct snd_sgio2audio *chip;
508	int count, ch;
509
510	substream = chan->substream;
511	chip = snd_pcm_substream_chip(substream);
512	ch = chan->idx;
513	/* fill the ring */
514	count = CHANNEL_RING_SIZE -
515		readq(&mace->perif.audio.chan[ch].depth) - 32;
516	if (snd_sgio2audio_dma_push_frag(chip, ch, count))
517		snd_pcm_period_elapsed(substream);
518
519	return IRQ_HANDLED;
520}
521
522static irqreturn_t snd_sgio2audio_error_isr(int irq, void *dev_id)
523{
524	struct snd_sgio2audio_chan *chan = dev_id;
525	struct snd_pcm_substream *substream;
526
527	substream = chan->substream;
528	snd_sgio2audio_dma_stop(substream);
529	snd_sgio2audio_dma_start(substream);
530	return IRQ_HANDLED;
531}
532
533/* PCM part */
534/* PCM hardware definition */
535static struct snd_pcm_hardware snd_sgio2audio_pcm_hw = {
536	.info = (SNDRV_PCM_INFO_MMAP |
537		 SNDRV_PCM_INFO_MMAP_VALID |
538		 SNDRV_PCM_INFO_INTERLEAVED |
539		 SNDRV_PCM_INFO_BLOCK_TRANSFER),
540	.formats =          SNDRV_PCM_FMTBIT_S16_BE,
541	.rates =            SNDRV_PCM_RATE_8000_48000,
542	.rate_min =         8000,
543	.rate_max =         48000,
544	.channels_min =     2,
545	.channels_max =     2,
546	.buffer_bytes_max = 65536,
547	.period_bytes_min = 32768,
548	.period_bytes_max = 65536,
549	.periods_min =      1,
550	.periods_max =      1024,
551};
552
553/* PCM playback open callback */
554static int snd_sgio2audio_playback1_open(struct snd_pcm_substream *substream)
555{
556	struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
557	struct snd_pcm_runtime *runtime = substream->runtime;
558
559	runtime->hw = snd_sgio2audio_pcm_hw;
560	runtime->private_data = &chip->channel[1];
561	return 0;
562}
563
564static int snd_sgio2audio_playback2_open(struct snd_pcm_substream *substream)
565{
566	struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
567	struct snd_pcm_runtime *runtime = substream->runtime;
568
569	runtime->hw = snd_sgio2audio_pcm_hw;
570	runtime->private_data = &chip->channel[2];
571	return 0;
572}
573
574/* PCM capture open callback */
575static int snd_sgio2audio_capture_open(struct snd_pcm_substream *substream)
576{
577	struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
578	struct snd_pcm_runtime *runtime = substream->runtime;
579
580	runtime->hw = snd_sgio2audio_pcm_hw;
581	runtime->private_data = &chip->channel[0];
582	return 0;
583}
584
585/* PCM close callback */
586static int snd_sgio2audio_pcm_close(struct snd_pcm_substream *substream)
587{
588	struct snd_pcm_runtime *runtime = substream->runtime;
589
590	runtime->private_data = NULL;
591	return 0;
592}
593
594
595/* hw_params callback */
596static int snd_sgio2audio_pcm_hw_params(struct snd_pcm_substream *substream,
597					struct snd_pcm_hw_params *hw_params)
598{
599	return snd_pcm_lib_alloc_vmalloc_buffer(substream,
600						params_buffer_bytes(hw_params));
601}
602
603/* hw_free callback */
604static int snd_sgio2audio_pcm_hw_free(struct snd_pcm_substream *substream)
605{
606	return snd_pcm_lib_free_vmalloc_buffer(substream);
607}
608
609/* prepare callback */
610static int snd_sgio2audio_pcm_prepare(struct snd_pcm_substream *substream)
611{
612	struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
613	struct snd_pcm_runtime *runtime = substream->runtime;
614	struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
615	int ch = chan->idx;
616	unsigned long flags;
617
618	spin_lock_irqsave(&chip->channel[ch].lock, flags);
619
620	/* Setup the pseudo-dma transfer pointers.  */
621	chip->channel[ch].pos = 0;
622	chip->channel[ch].size = 0;
623	chip->channel[ch].substream = substream;
624
625	/* set AD1843 format */
626	/* hardware format is always S16_LE */
627	switch (substream->stream) {
628	case SNDRV_PCM_STREAM_PLAYBACK:
629		ad1843_setup_dac(&chip->ad1843,
630				 ch - 1,
631				 runtime->rate,
632				 SNDRV_PCM_FORMAT_S16_LE,
633				 runtime->channels);
634		break;
635	case SNDRV_PCM_STREAM_CAPTURE:
636		ad1843_setup_adc(&chip->ad1843,
637				 runtime->rate,
638				 SNDRV_PCM_FORMAT_S16_LE,
639				 runtime->channels);
640		break;
641	}
642	spin_unlock_irqrestore(&chip->channel[ch].lock, flags);
643	return 0;
644}
645
646/* trigger callback */
647static int snd_sgio2audio_pcm_trigger(struct snd_pcm_substream *substream,
648				      int cmd)
649{
650	switch (cmd) {
651	case SNDRV_PCM_TRIGGER_START:
652		/* start the PCM engine */
653		snd_sgio2audio_dma_start(substream);
654		break;
655	case SNDRV_PCM_TRIGGER_STOP:
656		/* stop the PCM engine */
657		snd_sgio2audio_dma_stop(substream);
658		break;
659	default:
660		return -EINVAL;
661	}
662	return 0;
663}
664
665/* pointer callback */
666static snd_pcm_uframes_t
667snd_sgio2audio_pcm_pointer(struct snd_pcm_substream *substream)
668{
669	struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
670	struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
671
672	/* get the current hardware pointer */
673	return bytes_to_frames(substream->runtime,
674			       chip->channel[chan->idx].pos);
675}
676
677/* operators */
678static struct snd_pcm_ops snd_sgio2audio_playback1_ops = {
679	.open =        snd_sgio2audio_playback1_open,
680	.close =       snd_sgio2audio_pcm_close,
681	.ioctl =       snd_pcm_lib_ioctl,
682	.hw_params =   snd_sgio2audio_pcm_hw_params,
683	.hw_free =     snd_sgio2audio_pcm_hw_free,
684	.prepare =     snd_sgio2audio_pcm_prepare,
685	.trigger =     snd_sgio2audio_pcm_trigger,
686	.pointer =     snd_sgio2audio_pcm_pointer,
687	.page =        snd_pcm_lib_get_vmalloc_page,
688	.mmap =        snd_pcm_lib_mmap_vmalloc,
689};
690
691static struct snd_pcm_ops snd_sgio2audio_playback2_ops = {
692	.open =        snd_sgio2audio_playback2_open,
693	.close =       snd_sgio2audio_pcm_close,
694	.ioctl =       snd_pcm_lib_ioctl,
695	.hw_params =   snd_sgio2audio_pcm_hw_params,
696	.hw_free =     snd_sgio2audio_pcm_hw_free,
697	.prepare =     snd_sgio2audio_pcm_prepare,
698	.trigger =     snd_sgio2audio_pcm_trigger,
699	.pointer =     snd_sgio2audio_pcm_pointer,
700	.page =        snd_pcm_lib_get_vmalloc_page,
701	.mmap =        snd_pcm_lib_mmap_vmalloc,
702};
703
704static struct snd_pcm_ops snd_sgio2audio_capture_ops = {
705	.open =        snd_sgio2audio_capture_open,
706	.close =       snd_sgio2audio_pcm_close,
707	.ioctl =       snd_pcm_lib_ioctl,
708	.hw_params =   snd_sgio2audio_pcm_hw_params,
709	.hw_free =     snd_sgio2audio_pcm_hw_free,
710	.prepare =     snd_sgio2audio_pcm_prepare,
711	.trigger =     snd_sgio2audio_pcm_trigger,
712	.pointer =     snd_sgio2audio_pcm_pointer,
713	.page =        snd_pcm_lib_get_vmalloc_page,
714	.mmap =        snd_pcm_lib_mmap_vmalloc,
715};
716
717/*
718 *  definitions of capture are omitted here...
719 */
720
721/* create a pcm device */
722static int snd_sgio2audio_new_pcm(struct snd_sgio2audio *chip)
723{
724	struct snd_pcm *pcm;
725	int err;
726
727	/* create first pcm device with one outputs and one input */
728	err = snd_pcm_new(chip->card, "SGI O2 Audio", 0, 1, 1, &pcm);
729	if (err < 0)
730		return err;
731
732	pcm->private_data = chip;
733	strcpy(pcm->name, "SGI O2 DAC1");
734
735	/* set operators */
736	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
737			&snd_sgio2audio_playback1_ops);
738	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
739			&snd_sgio2audio_capture_ops);
740
741	/* create second  pcm device with one outputs and no input */
742	err = snd_pcm_new(chip->card, "SGI O2 Audio", 1, 1, 0, &pcm);
743	if (err < 0)
744		return err;
745
746	pcm->private_data = chip;
747	strcpy(pcm->name, "SGI O2 DAC2");
748
749	/* set operators */
750	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
751			&snd_sgio2audio_playback2_ops);
752
753	return 0;
754}
755
756static struct {
757	int idx;
758	int irq;
759	irqreturn_t (*isr)(int, void *);
760	const char *desc;
761} snd_sgio2_isr_table[] = {
762	{
763		.idx = 0,
764		.irq = MACEISA_AUDIO1_DMAT_IRQ,
765		.isr = snd_sgio2audio_dma_in_isr,
766		.desc = "Capture DMA Channel 0"
767	}, {
768		.idx = 0,
769		.irq = MACEISA_AUDIO1_OF_IRQ,
770		.isr = snd_sgio2audio_error_isr,
771		.desc = "Capture Overflow"
772	}, {
773		.idx = 1,
774		.irq = MACEISA_AUDIO2_DMAT_IRQ,
775		.isr = snd_sgio2audio_dma_out_isr,
776		.desc = "Playback DMA Channel 1"
777	}, {
778		.idx = 1,
779		.irq = MACEISA_AUDIO2_MERR_IRQ,
780		.isr = snd_sgio2audio_error_isr,
781		.desc = "Memory Error Channel 1"
782	}, {
783		.idx = 2,
784		.irq = MACEISA_AUDIO3_DMAT_IRQ,
785		.isr = snd_sgio2audio_dma_out_isr,
786		.desc = "Playback DMA Channel 2"
787	}, {
788		.idx = 2,
789		.irq = MACEISA_AUDIO3_MERR_IRQ,
790		.isr = snd_sgio2audio_error_isr,
791		.desc = "Memory Error Channel 2"
792	}
793};
794
795/* ALSA driver */
796
797static int snd_sgio2audio_free(struct snd_sgio2audio *chip)
798{
799	int i;
800
801	/* reset interface */
802	writeq(AUDIO_CONTROL_RESET, &mace->perif.audio.control);
803	udelay(1);
804	writeq(0, &mace->perif.audio.control);
805
806	/* release IRQ's */
807	for (i = 0; i < ARRAY_SIZE(snd_sgio2_isr_table); i++)
808		free_irq(snd_sgio2_isr_table[i].irq,
809			 &chip->channel[snd_sgio2_isr_table[i].idx]);
810
811	dma_free_coherent(NULL, MACEISA_RINGBUFFERS_SIZE,
812			  chip->ring_base, chip->ring_base_dma);
813
814	/* release card data */
815	kfree(chip);
816	return 0;
817}
818
819static int snd_sgio2audio_dev_free(struct snd_device *device)
820{
821	struct snd_sgio2audio *chip = device->device_data;
822
823	return snd_sgio2audio_free(chip);
824}
825
826static struct snd_device_ops ops = {
827	.dev_free = snd_sgio2audio_dev_free,
828};
829
830static int snd_sgio2audio_create(struct snd_card *card,
831				 struct snd_sgio2audio **rchip)
832{
833	struct snd_sgio2audio *chip;
834	int i, err;
835
836	*rchip = NULL;
837
838	/* check if a codec is attached to the interface */
839	/* (Audio or Audio/Video board present) */
840	if (!(readq(&mace->perif.audio.control) & AUDIO_CONTROL_CODEC_PRESENT))
841		return -ENOENT;
842
843	chip = kzalloc(sizeof(struct snd_sgio2audio), GFP_KERNEL);
844	if (chip == NULL)
845		return -ENOMEM;
846
847	chip->card = card;
848
849	chip->ring_base = dma_alloc_coherent(NULL, MACEISA_RINGBUFFERS_SIZE,
850					     &chip->ring_base_dma, GFP_USER);
851	if (chip->ring_base == NULL) {
852		printk(KERN_ERR
853		       "sgio2audio: could not allocate ring buffers\n");
854		kfree(chip);
855		return -ENOMEM;
856	}
857
858	spin_lock_init(&chip->ad1843_lock);
859
860	/* initialize channels */
861	for (i = 0; i < 3; i++) {
862		spin_lock_init(&chip->channel[i].lock);
863		chip->channel[i].idx = i;
864	}
865
866	/* allocate IRQs */
867	for (i = 0; i < ARRAY_SIZE(snd_sgio2_isr_table); i++) {
868		if (request_irq(snd_sgio2_isr_table[i].irq,
869				snd_sgio2_isr_table[i].isr,
870				0,
871				snd_sgio2_isr_table[i].desc,
872				&chip->channel[snd_sgio2_isr_table[i].idx])) {
873			snd_sgio2audio_free(chip);
874			printk(KERN_ERR "sgio2audio: cannot allocate irq %d\n",
875			       snd_sgio2_isr_table[i].irq);
876			return -EBUSY;
877		}
878	}
879
880	/* reset the interface */
881	writeq(AUDIO_CONTROL_RESET, &mace->perif.audio.control);
882	udelay(1);
883	writeq(0, &mace->perif.audio.control);
884	msleep_interruptible(1); /* give time to recover */
885
886	/* set ring base */
887	writeq(chip->ring_base_dma, &mace->perif.ctrl.ringbase);
888
889	/* attach the AD1843 codec */
890	chip->ad1843.read = read_ad1843_reg;
891	chip->ad1843.write = write_ad1843_reg;
892	chip->ad1843.chip = chip;
893
894	/* initialize the AD1843 codec */
895	err = ad1843_init(&chip->ad1843);
896	if (err < 0) {
897		snd_sgio2audio_free(chip);
898		return err;
899	}
900
901	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
902	if (err < 0) {
903		snd_sgio2audio_free(chip);
904		return err;
905	}
906	*rchip = chip;
907	return 0;
908}
909
910static int snd_sgio2audio_probe(struct platform_device *pdev)
911{
912	struct snd_card *card;
913	struct snd_sgio2audio *chip;
914	int err;
915
916	err = snd_card_new(&pdev->dev, index, id, THIS_MODULE, 0, &card);
917	if (err < 0)
918		return err;
919
920	err = snd_sgio2audio_create(card, &chip);
921	if (err < 0) {
922		snd_card_free(card);
923		return err;
924	}
 
925
926	err = snd_sgio2audio_new_pcm(chip);
927	if (err < 0) {
928		snd_card_free(card);
929		return err;
930	}
931	err = snd_sgio2audio_new_mixer(chip);
932	if (err < 0) {
933		snd_card_free(card);
934		return err;
935	}
936
937	strcpy(card->driver, "SGI O2 Audio");
938	strcpy(card->shortname, "SGI O2 Audio");
939	sprintf(card->longname, "%s irq %i-%i",
940		card->shortname,
941		MACEISA_AUDIO1_DMAT_IRQ,
942		MACEISA_AUDIO3_MERR_IRQ);
943
944	err = snd_card_register(card);
945	if (err < 0) {
946		snd_card_free(card);
947		return err;
948	}
949	platform_set_drvdata(pdev, card);
950	return 0;
951}
952
953static int snd_sgio2audio_remove(struct platform_device *pdev)
954{
955	struct snd_card *card = platform_get_drvdata(pdev);
956
957	snd_card_free(card);
 
958	return 0;
959}
960
961static struct platform_driver sgio2audio_driver = {
962	.probe	= snd_sgio2audio_probe,
963	.remove	= snd_sgio2audio_remove,
964	.driver = {
965		.name	= "sgio2audio",
 
966	}
967};
968
969module_platform_driver(sgio2audio_driver);
 
 
 
 
 
 
 
 
 
 
 
v3.1
  1/*
  2 *   Sound driver for Silicon Graphics O2 Workstations A/V board audio.
  3 *
  4 *   Copyright 2003 Vivien Chappelier <vivien.chappelier@linux-mips.org>
  5 *   Copyright 2008 Thomas Bogendoerfer <tsbogend@alpha.franken.de>
  6 *   Mxier part taken from mace_audio.c:
  7 *   Copyright 2007 Thorben Jändling <tj.trevelyan@gmail.com>
  8 *
  9 *   This program is free software; you can redistribute it and/or modify
 10 *   it under the terms of the GNU General Public License as published by
 11 *   the Free Software Foundation; either version 2 of the License, or
 12 *   (at your option) any later version.
 13 *
 14 *   This program is distributed in the hope that it will be useful,
 15 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17 *   GNU General Public License for more details.
 18 *
 19 *   You should have received a copy of the GNU General Public License
 20 *   along with this program; if not, write to the Free Software
 21 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 22 *
 23 */
 24
 25#include <linux/init.h>
 26#include <linux/delay.h>
 27#include <linux/spinlock.h>
 28#include <linux/interrupt.h>
 29#include <linux/dma-mapping.h>
 30#include <linux/platform_device.h>
 31#include <linux/io.h>
 32#include <linux/slab.h>
 
 33
 34#include <asm/ip32/ip32_ints.h>
 35#include <asm/ip32/mace.h>
 36
 37#include <sound/core.h>
 38#include <sound/control.h>
 39#include <sound/pcm.h>
 40#define SNDRV_GET_ID
 41#include <sound/initval.h>
 42#include <sound/ad1843.h>
 43
 44
 45MODULE_AUTHOR("Vivien Chappelier <vivien.chappelier@linux-mips.org>");
 46MODULE_DESCRIPTION("SGI O2 Audio");
 47MODULE_LICENSE("GPL");
 48MODULE_SUPPORTED_DEVICE("{{Silicon Graphics, O2 Audio}}");
 49
 50static int index = SNDRV_DEFAULT_IDX1;  /* Index 0-MAX */
 51static char *id = SNDRV_DEFAULT_STR1;   /* ID for this card */
 52
 53module_param(index, int, 0444);
 54MODULE_PARM_DESC(index, "Index value for SGI O2 soundcard.");
 55module_param(id, charp, 0444);
 56MODULE_PARM_DESC(id, "ID string for SGI O2 soundcard.");
 57
 58
 59#define AUDIO_CONTROL_RESET              BIT(0) /* 1: reset audio interface */
 60#define AUDIO_CONTROL_CODEC_PRESENT      BIT(1) /* 1: codec detected */
 61
 62#define CODEC_CONTROL_WORD_SHIFT        0
 63#define CODEC_CONTROL_READ              BIT(16)
 64#define CODEC_CONTROL_ADDRESS_SHIFT     17
 65
 66#define CHANNEL_CONTROL_RESET           BIT(10) /* 1: reset channel */
 67#define CHANNEL_DMA_ENABLE              BIT(9)  /* 1: enable DMA transfer */
 68#define CHANNEL_INT_THRESHOLD_DISABLED  (0 << 5) /* interrupt disabled */
 69#define CHANNEL_INT_THRESHOLD_25        (1 << 5) /* int on buffer >25% full */
 70#define CHANNEL_INT_THRESHOLD_50        (2 << 5) /* int on buffer >50% full */
 71#define CHANNEL_INT_THRESHOLD_75        (3 << 5) /* int on buffer >75% full */
 72#define CHANNEL_INT_THRESHOLD_EMPTY     (4 << 5) /* int on buffer empty */
 73#define CHANNEL_INT_THRESHOLD_NOT_EMPTY (5 << 5) /* int on buffer !empty */
 74#define CHANNEL_INT_THRESHOLD_FULL      (6 << 5) /* int on buffer empty */
 75#define CHANNEL_INT_THRESHOLD_NOT_FULL  (7 << 5) /* int on buffer !empty */
 76
 77#define CHANNEL_RING_SHIFT              12
 78#define CHANNEL_RING_SIZE               (1 << CHANNEL_RING_SHIFT)
 79#define CHANNEL_RING_MASK               (CHANNEL_RING_SIZE - 1)
 80
 81#define CHANNEL_LEFT_SHIFT 40
 82#define CHANNEL_RIGHT_SHIFT 8
 83
 84struct snd_sgio2audio_chan {
 85	int idx;
 86	struct snd_pcm_substream *substream;
 87	int pos;
 88	snd_pcm_uframes_t size;
 89	spinlock_t lock;
 90};
 91
 92/* definition of the chip-specific record */
 93struct snd_sgio2audio {
 94	struct snd_card *card;
 95
 96	/* codec */
 97	struct snd_ad1843 ad1843;
 98	spinlock_t ad1843_lock;
 99
100	/* channels */
101	struct snd_sgio2audio_chan channel[3];
102
103	/* resources */
104	void *ring_base;
105	dma_addr_t ring_base_dma;
106};
107
108/* AD1843 access */
109
110/*
111 * read_ad1843_reg returns the current contents of a 16 bit AD1843 register.
112 *
113 * Returns unsigned register value on success, -errno on failure.
114 */
115static int read_ad1843_reg(void *priv, int reg)
116{
117	struct snd_sgio2audio *chip = priv;
118	int val;
119	unsigned long flags;
120
121	spin_lock_irqsave(&chip->ad1843_lock, flags);
122
123	writeq((reg << CODEC_CONTROL_ADDRESS_SHIFT) |
124	       CODEC_CONTROL_READ, &mace->perif.audio.codec_control);
125	wmb();
126	val = readq(&mace->perif.audio.codec_control); /* flush bus */
127	udelay(200);
128
129	val = readq(&mace->perif.audio.codec_read);
130
131	spin_unlock_irqrestore(&chip->ad1843_lock, flags);
132	return val;
133}
134
135/*
136 * write_ad1843_reg writes the specified value to a 16 bit AD1843 register.
137 */
138static int write_ad1843_reg(void *priv, int reg, int word)
139{
140	struct snd_sgio2audio *chip = priv;
141	int val;
142	unsigned long flags;
143
144	spin_lock_irqsave(&chip->ad1843_lock, flags);
145
146	writeq((reg << CODEC_CONTROL_ADDRESS_SHIFT) |
147	       (word << CODEC_CONTROL_WORD_SHIFT),
148	       &mace->perif.audio.codec_control);
149	wmb();
150	val = readq(&mace->perif.audio.codec_control); /* flush bus */
151	udelay(200);
152
153	spin_unlock_irqrestore(&chip->ad1843_lock, flags);
154	return 0;
155}
156
157static int sgio2audio_gain_info(struct snd_kcontrol *kcontrol,
158			       struct snd_ctl_elem_info *uinfo)
159{
160	struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
161
162	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
163	uinfo->count = 2;
164	uinfo->value.integer.min = 0;
165	uinfo->value.integer.max = ad1843_get_gain_max(&chip->ad1843,
166					     (int)kcontrol->private_value);
167	return 0;
168}
169
170static int sgio2audio_gain_get(struct snd_kcontrol *kcontrol,
171			       struct snd_ctl_elem_value *ucontrol)
172{
173	struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
174	int vol;
175
176	vol = ad1843_get_gain(&chip->ad1843, (int)kcontrol->private_value);
177
178	ucontrol->value.integer.value[0] = (vol >> 8) & 0xFF;
179	ucontrol->value.integer.value[1] = vol & 0xFF;
180
181	return 0;
182}
183
184static int sgio2audio_gain_put(struct snd_kcontrol *kcontrol,
185			struct snd_ctl_elem_value *ucontrol)
186{
187	struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
188	int newvol, oldvol;
189
190	oldvol = ad1843_get_gain(&chip->ad1843, kcontrol->private_value);
191	newvol = (ucontrol->value.integer.value[0] << 8) |
192		ucontrol->value.integer.value[1];
193
194	newvol = ad1843_set_gain(&chip->ad1843, kcontrol->private_value,
195		newvol);
196
197	return newvol != oldvol;
198}
199
200static int sgio2audio_source_info(struct snd_kcontrol *kcontrol,
201			       struct snd_ctl_elem_info *uinfo)
202{
203	static const char *texts[3] = {
204		"Cam Mic", "Mic", "Line"
205	};
206	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
207	uinfo->count = 1;
208	uinfo->value.enumerated.items = 3;
209	if (uinfo->value.enumerated.item >= 3)
210		uinfo->value.enumerated.item = 1;
211	strcpy(uinfo->value.enumerated.name,
212	       texts[uinfo->value.enumerated.item]);
213	return 0;
214}
215
216static int sgio2audio_source_get(struct snd_kcontrol *kcontrol,
217			       struct snd_ctl_elem_value *ucontrol)
218{
219	struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
220
221	ucontrol->value.enumerated.item[0] = ad1843_get_recsrc(&chip->ad1843);
222	return 0;
223}
224
225static int sgio2audio_source_put(struct snd_kcontrol *kcontrol,
226			struct snd_ctl_elem_value *ucontrol)
227{
228	struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
229	int newsrc, oldsrc;
230
231	oldsrc = ad1843_get_recsrc(&chip->ad1843);
232	newsrc = ad1843_set_recsrc(&chip->ad1843,
233				   ucontrol->value.enumerated.item[0]);
234
235	return newsrc != oldsrc;
236}
237
238/* dac1/pcm0 mixer control */
239static struct snd_kcontrol_new sgio2audio_ctrl_pcm0 __devinitdata = {
240	.iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
241	.name           = "PCM Playback Volume",
242	.index          = 0,
243	.access         = SNDRV_CTL_ELEM_ACCESS_READWRITE,
244	.private_value  = AD1843_GAIN_PCM_0,
245	.info           = sgio2audio_gain_info,
246	.get            = sgio2audio_gain_get,
247	.put            = sgio2audio_gain_put,
248};
249
250/* dac2/pcm1 mixer control */
251static struct snd_kcontrol_new sgio2audio_ctrl_pcm1 __devinitdata = {
252	.iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
253	.name           = "PCM Playback Volume",
254	.index          = 1,
255	.access         = SNDRV_CTL_ELEM_ACCESS_READWRITE,
256	.private_value  = AD1843_GAIN_PCM_1,
257	.info           = sgio2audio_gain_info,
258	.get            = sgio2audio_gain_get,
259	.put            = sgio2audio_gain_put,
260};
261
262/* record level mixer control */
263static struct snd_kcontrol_new sgio2audio_ctrl_reclevel __devinitdata = {
264	.iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
265	.name           = "Capture Volume",
266	.access         = SNDRV_CTL_ELEM_ACCESS_READWRITE,
267	.private_value  = AD1843_GAIN_RECLEV,
268	.info           = sgio2audio_gain_info,
269	.get            = sgio2audio_gain_get,
270	.put            = sgio2audio_gain_put,
271};
272
273/* record level source control */
274static struct snd_kcontrol_new sgio2audio_ctrl_recsource __devinitdata = {
275	.iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
276	.name           = "Capture Source",
277	.access         = SNDRV_CTL_ELEM_ACCESS_READWRITE,
278	.info           = sgio2audio_source_info,
279	.get            = sgio2audio_source_get,
280	.put            = sgio2audio_source_put,
281};
282
283/* line mixer control */
284static struct snd_kcontrol_new sgio2audio_ctrl_line __devinitdata = {
285	.iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
286	.name           = "Line Playback Volume",
287	.index          = 0,
288	.access         = SNDRV_CTL_ELEM_ACCESS_READWRITE,
289	.private_value  = AD1843_GAIN_LINE,
290	.info           = sgio2audio_gain_info,
291	.get            = sgio2audio_gain_get,
292	.put            = sgio2audio_gain_put,
293};
294
295/* cd mixer control */
296static struct snd_kcontrol_new sgio2audio_ctrl_cd __devinitdata = {
297	.iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
298	.name           = "Line Playback Volume",
299	.index          = 1,
300	.access         = SNDRV_CTL_ELEM_ACCESS_READWRITE,
301	.private_value  = AD1843_GAIN_LINE_2,
302	.info           = sgio2audio_gain_info,
303	.get            = sgio2audio_gain_get,
304	.put            = sgio2audio_gain_put,
305};
306
307/* mic mixer control */
308static struct snd_kcontrol_new sgio2audio_ctrl_mic __devinitdata = {
309	.iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
310	.name           = "Mic Playback Volume",
311	.access         = SNDRV_CTL_ELEM_ACCESS_READWRITE,
312	.private_value  = AD1843_GAIN_MIC,
313	.info           = sgio2audio_gain_info,
314	.get            = sgio2audio_gain_get,
315	.put            = sgio2audio_gain_put,
316};
317
318
319static int __devinit snd_sgio2audio_new_mixer(struct snd_sgio2audio *chip)
320{
321	int err;
322
323	err = snd_ctl_add(chip->card,
324			  snd_ctl_new1(&sgio2audio_ctrl_pcm0, chip));
325	if (err < 0)
326		return err;
327
328	err = snd_ctl_add(chip->card,
329			  snd_ctl_new1(&sgio2audio_ctrl_pcm1, chip));
330	if (err < 0)
331		return err;
332
333	err = snd_ctl_add(chip->card,
334			  snd_ctl_new1(&sgio2audio_ctrl_reclevel, chip));
335	if (err < 0)
336		return err;
337
338	err = snd_ctl_add(chip->card,
339			  snd_ctl_new1(&sgio2audio_ctrl_recsource, chip));
340	if (err < 0)
341		return err;
342	err = snd_ctl_add(chip->card,
343			  snd_ctl_new1(&sgio2audio_ctrl_line, chip));
344	if (err < 0)
345		return err;
346
347	err = snd_ctl_add(chip->card,
348			  snd_ctl_new1(&sgio2audio_ctrl_cd, chip));
349	if (err < 0)
350		return err;
351
352	err = snd_ctl_add(chip->card,
353			  snd_ctl_new1(&sgio2audio_ctrl_mic, chip));
354	if (err < 0)
355		return err;
356
357	return 0;
358}
359
360/* low-level audio interface DMA */
361
362/* get data out of bounce buffer, count must be a multiple of 32 */
363/* returns 1 if a period has elapsed */
364static int snd_sgio2audio_dma_pull_frag(struct snd_sgio2audio *chip,
365					unsigned int ch, unsigned int count)
366{
367	int ret;
368	unsigned long src_base, src_pos, dst_mask;
369	unsigned char *dst_base;
370	int dst_pos;
371	u64 *src;
372	s16 *dst;
373	u64 x;
374	unsigned long flags;
375	struct snd_pcm_runtime *runtime = chip->channel[ch].substream->runtime;
376
377	spin_lock_irqsave(&chip->channel[ch].lock, flags);
378
379	src_base = (unsigned long) chip->ring_base | (ch << CHANNEL_RING_SHIFT);
380	src_pos = readq(&mace->perif.audio.chan[ch].read_ptr);
381	dst_base = runtime->dma_area;
382	dst_pos = chip->channel[ch].pos;
383	dst_mask = frames_to_bytes(runtime, runtime->buffer_size) - 1;
384
385	/* check if a period has elapsed */
386	chip->channel[ch].size += (count >> 3); /* in frames */
387	ret = chip->channel[ch].size >= runtime->period_size;
388	chip->channel[ch].size %= runtime->period_size;
389
390	while (count) {
391		src = (u64 *)(src_base + src_pos);
392		dst = (s16 *)(dst_base + dst_pos);
393
394		x = *src;
395		dst[0] = (x >> CHANNEL_LEFT_SHIFT) & 0xffff;
396		dst[1] = (x >> CHANNEL_RIGHT_SHIFT) & 0xffff;
397
398		src_pos = (src_pos + sizeof(u64)) & CHANNEL_RING_MASK;
399		dst_pos = (dst_pos + 2 * sizeof(s16)) & dst_mask;
400		count -= sizeof(u64);
401	}
402
403	writeq(src_pos, &mace->perif.audio.chan[ch].read_ptr); /* in bytes */
404	chip->channel[ch].pos = dst_pos;
405
406	spin_unlock_irqrestore(&chip->channel[ch].lock, flags);
407	return ret;
408}
409
410/* put some DMA data in bounce buffer, count must be a multiple of 32 */
411/* returns 1 if a period has elapsed */
412static int snd_sgio2audio_dma_push_frag(struct snd_sgio2audio *chip,
413					unsigned int ch, unsigned int count)
414{
415	int ret;
416	s64 l, r;
417	unsigned long dst_base, dst_pos, src_mask;
418	unsigned char *src_base;
419	int src_pos;
420	u64 *dst;
421	s16 *src;
422	unsigned long flags;
423	struct snd_pcm_runtime *runtime = chip->channel[ch].substream->runtime;
424
425	spin_lock_irqsave(&chip->channel[ch].lock, flags);
426
427	dst_base = (unsigned long)chip->ring_base | (ch << CHANNEL_RING_SHIFT);
428	dst_pos = readq(&mace->perif.audio.chan[ch].write_ptr);
429	src_base = runtime->dma_area;
430	src_pos = chip->channel[ch].pos;
431	src_mask = frames_to_bytes(runtime, runtime->buffer_size) - 1;
432
433	/* check if a period has elapsed */
434	chip->channel[ch].size += (count >> 3); /* in frames */
435	ret = chip->channel[ch].size >= runtime->period_size;
436	chip->channel[ch].size %= runtime->period_size;
437
438	while (count) {
439		src = (s16 *)(src_base + src_pos);
440		dst = (u64 *)(dst_base + dst_pos);
441
442		l = src[0]; /* sign extend */
443		r = src[1]; /* sign extend */
444
445		*dst = ((l & 0x00ffffff) << CHANNEL_LEFT_SHIFT) |
446			((r & 0x00ffffff) << CHANNEL_RIGHT_SHIFT);
447
448		dst_pos = (dst_pos + sizeof(u64)) & CHANNEL_RING_MASK;
449		src_pos = (src_pos + 2 * sizeof(s16)) & src_mask;
450		count -= sizeof(u64);
451	}
452
453	writeq(dst_pos, &mace->perif.audio.chan[ch].write_ptr); /* in bytes */
454	chip->channel[ch].pos = src_pos;
455
456	spin_unlock_irqrestore(&chip->channel[ch].lock, flags);
457	return ret;
458}
459
460static int snd_sgio2audio_dma_start(struct snd_pcm_substream *substream)
461{
462	struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
463	struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
464	int ch = chan->idx;
465
466	/* reset DMA channel */
467	writeq(CHANNEL_CONTROL_RESET, &mace->perif.audio.chan[ch].control);
468	udelay(10);
469	writeq(0, &mace->perif.audio.chan[ch].control);
470
471	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
472		/* push a full buffer */
473		snd_sgio2audio_dma_push_frag(chip, ch, CHANNEL_RING_SIZE - 32);
474	}
475	/* set DMA to wake on 50% empty and enable interrupt */
476	writeq(CHANNEL_DMA_ENABLE | CHANNEL_INT_THRESHOLD_50,
477	       &mace->perif.audio.chan[ch].control);
478	return 0;
479}
480
481static int snd_sgio2audio_dma_stop(struct snd_pcm_substream *substream)
482{
483	struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
484
485	writeq(0, &mace->perif.audio.chan[chan->idx].control);
486	return 0;
487}
488
489static irqreturn_t snd_sgio2audio_dma_in_isr(int irq, void *dev_id)
490{
491	struct snd_sgio2audio_chan *chan = dev_id;
492	struct snd_pcm_substream *substream;
493	struct snd_sgio2audio *chip;
494	int count, ch;
495
496	substream = chan->substream;
497	chip = snd_pcm_substream_chip(substream);
498	ch = chan->idx;
499
500	/* empty the ring */
501	count = CHANNEL_RING_SIZE -
502		readq(&mace->perif.audio.chan[ch].depth) - 32;
503	if (snd_sgio2audio_dma_pull_frag(chip, ch, count))
504		snd_pcm_period_elapsed(substream);
505
506	return IRQ_HANDLED;
507}
508
509static irqreturn_t snd_sgio2audio_dma_out_isr(int irq, void *dev_id)
510{
511	struct snd_sgio2audio_chan *chan = dev_id;
512	struct snd_pcm_substream *substream;
513	struct snd_sgio2audio *chip;
514	int count, ch;
515
516	substream = chan->substream;
517	chip = snd_pcm_substream_chip(substream);
518	ch = chan->idx;
519	/* fill the ring */
520	count = CHANNEL_RING_SIZE -
521		readq(&mace->perif.audio.chan[ch].depth) - 32;
522	if (snd_sgio2audio_dma_push_frag(chip, ch, count))
523		snd_pcm_period_elapsed(substream);
524
525	return IRQ_HANDLED;
526}
527
528static irqreturn_t snd_sgio2audio_error_isr(int irq, void *dev_id)
529{
530	struct snd_sgio2audio_chan *chan = dev_id;
531	struct snd_pcm_substream *substream;
532
533	substream = chan->substream;
534	snd_sgio2audio_dma_stop(substream);
535	snd_sgio2audio_dma_start(substream);
536	return IRQ_HANDLED;
537}
538
539/* PCM part */
540/* PCM hardware definition */
541static struct snd_pcm_hardware snd_sgio2audio_pcm_hw = {
542	.info = (SNDRV_PCM_INFO_MMAP |
543		 SNDRV_PCM_INFO_MMAP_VALID |
544		 SNDRV_PCM_INFO_INTERLEAVED |
545		 SNDRV_PCM_INFO_BLOCK_TRANSFER),
546	.formats =          SNDRV_PCM_FMTBIT_S16_BE,
547	.rates =            SNDRV_PCM_RATE_8000_48000,
548	.rate_min =         8000,
549	.rate_max =         48000,
550	.channels_min =     2,
551	.channels_max =     2,
552	.buffer_bytes_max = 65536,
553	.period_bytes_min = 32768,
554	.period_bytes_max = 65536,
555	.periods_min =      1,
556	.periods_max =      1024,
557};
558
559/* PCM playback open callback */
560static int snd_sgio2audio_playback1_open(struct snd_pcm_substream *substream)
561{
562	struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
563	struct snd_pcm_runtime *runtime = substream->runtime;
564
565	runtime->hw = snd_sgio2audio_pcm_hw;
566	runtime->private_data = &chip->channel[1];
567	return 0;
568}
569
570static int snd_sgio2audio_playback2_open(struct snd_pcm_substream *substream)
571{
572	struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
573	struct snd_pcm_runtime *runtime = substream->runtime;
574
575	runtime->hw = snd_sgio2audio_pcm_hw;
576	runtime->private_data = &chip->channel[2];
577	return 0;
578}
579
580/* PCM capture open callback */
581static int snd_sgio2audio_capture_open(struct snd_pcm_substream *substream)
582{
583	struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
584	struct snd_pcm_runtime *runtime = substream->runtime;
585
586	runtime->hw = snd_sgio2audio_pcm_hw;
587	runtime->private_data = &chip->channel[0];
588	return 0;
589}
590
591/* PCM close callback */
592static int snd_sgio2audio_pcm_close(struct snd_pcm_substream *substream)
593{
594	struct snd_pcm_runtime *runtime = substream->runtime;
595
596	runtime->private_data = NULL;
597	return 0;
598}
599
600
601/* hw_params callback */
602static int snd_sgio2audio_pcm_hw_params(struct snd_pcm_substream *substream,
603					struct snd_pcm_hw_params *hw_params)
604{
605	return snd_pcm_lib_alloc_vmalloc_buffer(substream,
606						params_buffer_bytes(hw_params));
607}
608
609/* hw_free callback */
610static int snd_sgio2audio_pcm_hw_free(struct snd_pcm_substream *substream)
611{
612	return snd_pcm_lib_free_vmalloc_buffer(substream);
613}
614
615/* prepare callback */
616static int snd_sgio2audio_pcm_prepare(struct snd_pcm_substream *substream)
617{
618	struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
619	struct snd_pcm_runtime *runtime = substream->runtime;
620	struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
621	int ch = chan->idx;
622	unsigned long flags;
623
624	spin_lock_irqsave(&chip->channel[ch].lock, flags);
625
626	/* Setup the pseudo-dma transfer pointers.  */
627	chip->channel[ch].pos = 0;
628	chip->channel[ch].size = 0;
629	chip->channel[ch].substream = substream;
630
631	/* set AD1843 format */
632	/* hardware format is always S16_LE */
633	switch (substream->stream) {
634	case SNDRV_PCM_STREAM_PLAYBACK:
635		ad1843_setup_dac(&chip->ad1843,
636				 ch - 1,
637				 runtime->rate,
638				 SNDRV_PCM_FORMAT_S16_LE,
639				 runtime->channels);
640		break;
641	case SNDRV_PCM_STREAM_CAPTURE:
642		ad1843_setup_adc(&chip->ad1843,
643				 runtime->rate,
644				 SNDRV_PCM_FORMAT_S16_LE,
645				 runtime->channels);
646		break;
647	}
648	spin_unlock_irqrestore(&chip->channel[ch].lock, flags);
649	return 0;
650}
651
652/* trigger callback */
653static int snd_sgio2audio_pcm_trigger(struct snd_pcm_substream *substream,
654				      int cmd)
655{
656	switch (cmd) {
657	case SNDRV_PCM_TRIGGER_START:
658		/* start the PCM engine */
659		snd_sgio2audio_dma_start(substream);
660		break;
661	case SNDRV_PCM_TRIGGER_STOP:
662		/* stop the PCM engine */
663		snd_sgio2audio_dma_stop(substream);
664		break;
665	default:
666		return -EINVAL;
667	}
668	return 0;
669}
670
671/* pointer callback */
672static snd_pcm_uframes_t
673snd_sgio2audio_pcm_pointer(struct snd_pcm_substream *substream)
674{
675	struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
676	struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
677
678	/* get the current hardware pointer */
679	return bytes_to_frames(substream->runtime,
680			       chip->channel[chan->idx].pos);
681}
682
683/* operators */
684static struct snd_pcm_ops snd_sgio2audio_playback1_ops = {
685	.open =        snd_sgio2audio_playback1_open,
686	.close =       snd_sgio2audio_pcm_close,
687	.ioctl =       snd_pcm_lib_ioctl,
688	.hw_params =   snd_sgio2audio_pcm_hw_params,
689	.hw_free =     snd_sgio2audio_pcm_hw_free,
690	.prepare =     snd_sgio2audio_pcm_prepare,
691	.trigger =     snd_sgio2audio_pcm_trigger,
692	.pointer =     snd_sgio2audio_pcm_pointer,
693	.page =        snd_pcm_lib_get_vmalloc_page,
694	.mmap =        snd_pcm_lib_mmap_vmalloc,
695};
696
697static struct snd_pcm_ops snd_sgio2audio_playback2_ops = {
698	.open =        snd_sgio2audio_playback2_open,
699	.close =       snd_sgio2audio_pcm_close,
700	.ioctl =       snd_pcm_lib_ioctl,
701	.hw_params =   snd_sgio2audio_pcm_hw_params,
702	.hw_free =     snd_sgio2audio_pcm_hw_free,
703	.prepare =     snd_sgio2audio_pcm_prepare,
704	.trigger =     snd_sgio2audio_pcm_trigger,
705	.pointer =     snd_sgio2audio_pcm_pointer,
706	.page =        snd_pcm_lib_get_vmalloc_page,
707	.mmap =        snd_pcm_lib_mmap_vmalloc,
708};
709
710static struct snd_pcm_ops snd_sgio2audio_capture_ops = {
711	.open =        snd_sgio2audio_capture_open,
712	.close =       snd_sgio2audio_pcm_close,
713	.ioctl =       snd_pcm_lib_ioctl,
714	.hw_params =   snd_sgio2audio_pcm_hw_params,
715	.hw_free =     snd_sgio2audio_pcm_hw_free,
716	.prepare =     snd_sgio2audio_pcm_prepare,
717	.trigger =     snd_sgio2audio_pcm_trigger,
718	.pointer =     snd_sgio2audio_pcm_pointer,
719	.page =        snd_pcm_lib_get_vmalloc_page,
720	.mmap =        snd_pcm_lib_mmap_vmalloc,
721};
722
723/*
724 *  definitions of capture are omitted here...
725 */
726
727/* create a pcm device */
728static int __devinit snd_sgio2audio_new_pcm(struct snd_sgio2audio *chip)
729{
730	struct snd_pcm *pcm;
731	int err;
732
733	/* create first pcm device with one outputs and one input */
734	err = snd_pcm_new(chip->card, "SGI O2 Audio", 0, 1, 1, &pcm);
735	if (err < 0)
736		return err;
737
738	pcm->private_data = chip;
739	strcpy(pcm->name, "SGI O2 DAC1");
740
741	/* set operators */
742	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
743			&snd_sgio2audio_playback1_ops);
744	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
745			&snd_sgio2audio_capture_ops);
746
747	/* create second  pcm device with one outputs and no input */
748	err = snd_pcm_new(chip->card, "SGI O2 Audio", 1, 1, 0, &pcm);
749	if (err < 0)
750		return err;
751
752	pcm->private_data = chip;
753	strcpy(pcm->name, "SGI O2 DAC2");
754
755	/* set operators */
756	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
757			&snd_sgio2audio_playback2_ops);
758
759	return 0;
760}
761
762static struct {
763	int idx;
764	int irq;
765	irqreturn_t (*isr)(int, void *);
766	const char *desc;
767} snd_sgio2_isr_table[] = {
768	{
769		.idx = 0,
770		.irq = MACEISA_AUDIO1_DMAT_IRQ,
771		.isr = snd_sgio2audio_dma_in_isr,
772		.desc = "Capture DMA Channel 0"
773	}, {
774		.idx = 0,
775		.irq = MACEISA_AUDIO1_OF_IRQ,
776		.isr = snd_sgio2audio_error_isr,
777		.desc = "Capture Overflow"
778	}, {
779		.idx = 1,
780		.irq = MACEISA_AUDIO2_DMAT_IRQ,
781		.isr = snd_sgio2audio_dma_out_isr,
782		.desc = "Playback DMA Channel 1"
783	}, {
784		.idx = 1,
785		.irq = MACEISA_AUDIO2_MERR_IRQ,
786		.isr = snd_sgio2audio_error_isr,
787		.desc = "Memory Error Channel 1"
788	}, {
789		.idx = 2,
790		.irq = MACEISA_AUDIO3_DMAT_IRQ,
791		.isr = snd_sgio2audio_dma_out_isr,
792		.desc = "Playback DMA Channel 2"
793	}, {
794		.idx = 2,
795		.irq = MACEISA_AUDIO3_MERR_IRQ,
796		.isr = snd_sgio2audio_error_isr,
797		.desc = "Memory Error Channel 2"
798	}
799};
800
801/* ALSA driver */
802
803static int snd_sgio2audio_free(struct snd_sgio2audio *chip)
804{
805	int i;
806
807	/* reset interface */
808	writeq(AUDIO_CONTROL_RESET, &mace->perif.audio.control);
809	udelay(1);
810	writeq(0, &mace->perif.audio.control);
811
812	/* release IRQ's */
813	for (i = 0; i < ARRAY_SIZE(snd_sgio2_isr_table); i++)
814		free_irq(snd_sgio2_isr_table[i].irq,
815			 &chip->channel[snd_sgio2_isr_table[i].idx]);
816
817	dma_free_coherent(NULL, MACEISA_RINGBUFFERS_SIZE,
818			  chip->ring_base, chip->ring_base_dma);
819
820	/* release card data */
821	kfree(chip);
822	return 0;
823}
824
825static int snd_sgio2audio_dev_free(struct snd_device *device)
826{
827	struct snd_sgio2audio *chip = device->device_data;
828
829	return snd_sgio2audio_free(chip);
830}
831
832static struct snd_device_ops ops = {
833	.dev_free = snd_sgio2audio_dev_free,
834};
835
836static int __devinit snd_sgio2audio_create(struct snd_card *card,
837					   struct snd_sgio2audio **rchip)
838{
839	struct snd_sgio2audio *chip;
840	int i, err;
841
842	*rchip = NULL;
843
844	/* check if a codec is attached to the interface */
845	/* (Audio or Audio/Video board present) */
846	if (!(readq(&mace->perif.audio.control) & AUDIO_CONTROL_CODEC_PRESENT))
847		return -ENOENT;
848
849	chip = kzalloc(sizeof(struct snd_sgio2audio), GFP_KERNEL);
850	if (chip == NULL)
851		return -ENOMEM;
852
853	chip->card = card;
854
855	chip->ring_base = dma_alloc_coherent(NULL, MACEISA_RINGBUFFERS_SIZE,
856					     &chip->ring_base_dma, GFP_USER);
857	if (chip->ring_base == NULL) {
858		printk(KERN_ERR
859		       "sgio2audio: could not allocate ring buffers\n");
860		kfree(chip);
861		return -ENOMEM;
862	}
863
864	spin_lock_init(&chip->ad1843_lock);
865
866	/* initialize channels */
867	for (i = 0; i < 3; i++) {
868		spin_lock_init(&chip->channel[i].lock);
869		chip->channel[i].idx = i;
870	}
871
872	/* allocate IRQs */
873	for (i = 0; i < ARRAY_SIZE(snd_sgio2_isr_table); i++) {
874		if (request_irq(snd_sgio2_isr_table[i].irq,
875				snd_sgio2_isr_table[i].isr,
876				0,
877				snd_sgio2_isr_table[i].desc,
878				&chip->channel[snd_sgio2_isr_table[i].idx])) {
879			snd_sgio2audio_free(chip);
880			printk(KERN_ERR "sgio2audio: cannot allocate irq %d\n",
881			       snd_sgio2_isr_table[i].irq);
882			return -EBUSY;
883		}
884	}
885
886	/* reset the interface */
887	writeq(AUDIO_CONTROL_RESET, &mace->perif.audio.control);
888	udelay(1);
889	writeq(0, &mace->perif.audio.control);
890	msleep_interruptible(1); /* give time to recover */
891
892	/* set ring base */
893	writeq(chip->ring_base_dma, &mace->perif.ctrl.ringbase);
894
895	/* attach the AD1843 codec */
896	chip->ad1843.read = read_ad1843_reg;
897	chip->ad1843.write = write_ad1843_reg;
898	chip->ad1843.chip = chip;
899
900	/* initialize the AD1843 codec */
901	err = ad1843_init(&chip->ad1843);
902	if (err < 0) {
903		snd_sgio2audio_free(chip);
904		return err;
905	}
906
907	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
908	if (err < 0) {
909		snd_sgio2audio_free(chip);
910		return err;
911	}
912	*rchip = chip;
913	return 0;
914}
915
916static int __devinit snd_sgio2audio_probe(struct platform_device *pdev)
917{
918	struct snd_card *card;
919	struct snd_sgio2audio *chip;
920	int err;
921
922	err = snd_card_create(index, id, THIS_MODULE, 0, &card);
923	if (err < 0)
924		return err;
925
926	err = snd_sgio2audio_create(card, &chip);
927	if (err < 0) {
928		snd_card_free(card);
929		return err;
930	}
931	snd_card_set_dev(card, &pdev->dev);
932
933	err = snd_sgio2audio_new_pcm(chip);
934	if (err < 0) {
935		snd_card_free(card);
936		return err;
937	}
938	err = snd_sgio2audio_new_mixer(chip);
939	if (err < 0) {
940		snd_card_free(card);
941		return err;
942	}
943
944	strcpy(card->driver, "SGI O2 Audio");
945	strcpy(card->shortname, "SGI O2 Audio");
946	sprintf(card->longname, "%s irq %i-%i",
947		card->shortname,
948		MACEISA_AUDIO1_DMAT_IRQ,
949		MACEISA_AUDIO3_MERR_IRQ);
950
951	err = snd_card_register(card);
952	if (err < 0) {
953		snd_card_free(card);
954		return err;
955	}
956	platform_set_drvdata(pdev, card);
957	return 0;
958}
959
960static int __devexit snd_sgio2audio_remove(struct platform_device *pdev)
961{
962	struct snd_card *card = platform_get_drvdata(pdev);
963
964	snd_card_free(card);
965	platform_set_drvdata(pdev, NULL);
966	return 0;
967}
968
969static struct platform_driver sgio2audio_driver = {
970	.probe	= snd_sgio2audio_probe,
971	.remove	= __devexit_p(snd_sgio2audio_remove),
972	.driver = {
973		.name	= "sgio2audio",
974		.owner	= THIS_MODULE,
975	}
976};
977
978static int __init alsa_card_sgio2audio_init(void)
979{
980	return platform_driver_register(&sgio2audio_driver);
981}
982
983static void __exit alsa_card_sgio2audio_exit(void)
984{
985	platform_driver_unregister(&sgio2audio_driver);
986}
987
988module_init(alsa_card_sgio2audio_init)
989module_exit(alsa_card_sgio2audio_exit)