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