xref: /openbmc/linux/sound/mips/sgio2audio.c (revision 3e30a927)
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 /* prepare callback */
581 static int snd_sgio2audio_pcm_prepare(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 	struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
586 	int ch = chan->idx;
587 	unsigned long flags;
588 
589 	spin_lock_irqsave(&chip->channel[ch].lock, flags);
590 
591 	/* Setup the pseudo-dma transfer pointers.  */
592 	chip->channel[ch].pos = 0;
593 	chip->channel[ch].size = 0;
594 	chip->channel[ch].substream = substream;
595 
596 	/* set AD1843 format */
597 	/* hardware format is always S16_LE */
598 	switch (substream->stream) {
599 	case SNDRV_PCM_STREAM_PLAYBACK:
600 		ad1843_setup_dac(&chip->ad1843,
601 				 ch - 1,
602 				 runtime->rate,
603 				 SNDRV_PCM_FORMAT_S16_LE,
604 				 runtime->channels);
605 		break;
606 	case SNDRV_PCM_STREAM_CAPTURE:
607 		ad1843_setup_adc(&chip->ad1843,
608 				 runtime->rate,
609 				 SNDRV_PCM_FORMAT_S16_LE,
610 				 runtime->channels);
611 		break;
612 	}
613 	spin_unlock_irqrestore(&chip->channel[ch].lock, flags);
614 	return 0;
615 }
616 
617 /* trigger callback */
618 static int snd_sgio2audio_pcm_trigger(struct snd_pcm_substream *substream,
619 				      int cmd)
620 {
621 	switch (cmd) {
622 	case SNDRV_PCM_TRIGGER_START:
623 		/* start the PCM engine */
624 		snd_sgio2audio_dma_start(substream);
625 		break;
626 	case SNDRV_PCM_TRIGGER_STOP:
627 		/* stop the PCM engine */
628 		snd_sgio2audio_dma_stop(substream);
629 		break;
630 	default:
631 		return -EINVAL;
632 	}
633 	return 0;
634 }
635 
636 /* pointer callback */
637 static snd_pcm_uframes_t
638 snd_sgio2audio_pcm_pointer(struct snd_pcm_substream *substream)
639 {
640 	struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
641 	struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
642 
643 	/* get the current hardware pointer */
644 	return bytes_to_frames(substream->runtime,
645 			       chip->channel[chan->idx].pos);
646 }
647 
648 /* operators */
649 static const struct snd_pcm_ops snd_sgio2audio_playback1_ops = {
650 	.open =        snd_sgio2audio_playback1_open,
651 	.close =       snd_sgio2audio_pcm_close,
652 	.hw_params =   snd_sgio2audio_pcm_hw_params,
653 	.hw_free =     snd_sgio2audio_pcm_hw_free,
654 	.prepare =     snd_sgio2audio_pcm_prepare,
655 	.trigger =     snd_sgio2audio_pcm_trigger,
656 	.pointer =     snd_sgio2audio_pcm_pointer,
657 };
658 
659 static const struct snd_pcm_ops snd_sgio2audio_playback2_ops = {
660 	.open =        snd_sgio2audio_playback2_open,
661 	.close =       snd_sgio2audio_pcm_close,
662 	.hw_params =   snd_sgio2audio_pcm_hw_params,
663 	.hw_free =     snd_sgio2audio_pcm_hw_free,
664 	.prepare =     snd_sgio2audio_pcm_prepare,
665 	.trigger =     snd_sgio2audio_pcm_trigger,
666 	.pointer =     snd_sgio2audio_pcm_pointer,
667 };
668 
669 static const struct snd_pcm_ops snd_sgio2audio_capture_ops = {
670 	.open =        snd_sgio2audio_capture_open,
671 	.close =       snd_sgio2audio_pcm_close,
672 	.hw_params =   snd_sgio2audio_pcm_hw_params,
673 	.hw_free =     snd_sgio2audio_pcm_hw_free,
674 	.prepare =     snd_sgio2audio_pcm_prepare,
675 	.trigger =     snd_sgio2audio_pcm_trigger,
676 	.pointer =     snd_sgio2audio_pcm_pointer,
677 };
678 
679 /*
680  *  definitions of capture are omitted here...
681  */
682 
683 /* create a pcm device */
684 static int snd_sgio2audio_new_pcm(struct snd_sgio2audio *chip)
685 {
686 	struct snd_pcm *pcm;
687 	int err;
688 
689 	/* create first pcm device with one outputs and one input */
690 	err = snd_pcm_new(chip->card, "SGI O2 Audio", 0, 1, 1, &pcm);
691 	if (err < 0)
692 		return err;
693 
694 	pcm->private_data = chip;
695 	strcpy(pcm->name, "SGI O2 DAC1");
696 
697 	/* set operators */
698 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
699 			&snd_sgio2audio_playback1_ops);
700 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
701 			&snd_sgio2audio_capture_ops);
702 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
703 
704 	/* create second  pcm device with one outputs and no input */
705 	err = snd_pcm_new(chip->card, "SGI O2 Audio", 1, 1, 0, &pcm);
706 	if (err < 0)
707 		return err;
708 
709 	pcm->private_data = chip;
710 	strcpy(pcm->name, "SGI O2 DAC2");
711 
712 	/* set operators */
713 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
714 			&snd_sgio2audio_playback2_ops);
715 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
716 
717 	return 0;
718 }
719 
720 static struct {
721 	int idx;
722 	int irq;
723 	irqreturn_t (*isr)(int, void *);
724 	const char *desc;
725 } snd_sgio2_isr_table[] = {
726 	{
727 		.idx = 0,
728 		.irq = MACEISA_AUDIO1_DMAT_IRQ,
729 		.isr = snd_sgio2audio_dma_in_isr,
730 		.desc = "Capture DMA Channel 0"
731 	}, {
732 		.idx = 0,
733 		.irq = MACEISA_AUDIO1_OF_IRQ,
734 		.isr = snd_sgio2audio_error_isr,
735 		.desc = "Capture Overflow"
736 	}, {
737 		.idx = 1,
738 		.irq = MACEISA_AUDIO2_DMAT_IRQ,
739 		.isr = snd_sgio2audio_dma_out_isr,
740 		.desc = "Playback DMA Channel 1"
741 	}, {
742 		.idx = 1,
743 		.irq = MACEISA_AUDIO2_MERR_IRQ,
744 		.isr = snd_sgio2audio_error_isr,
745 		.desc = "Memory Error Channel 1"
746 	}, {
747 		.idx = 2,
748 		.irq = MACEISA_AUDIO3_DMAT_IRQ,
749 		.isr = snd_sgio2audio_dma_out_isr,
750 		.desc = "Playback DMA Channel 2"
751 	}, {
752 		.idx = 2,
753 		.irq = MACEISA_AUDIO3_MERR_IRQ,
754 		.isr = snd_sgio2audio_error_isr,
755 		.desc = "Memory Error Channel 2"
756 	}
757 };
758 
759 /* ALSA driver */
760 
761 static int snd_sgio2audio_free(struct snd_sgio2audio *chip)
762 {
763 	int i;
764 
765 	/* reset interface */
766 	writeq(AUDIO_CONTROL_RESET, &mace->perif.audio.control);
767 	udelay(1);
768 	writeq(0, &mace->perif.audio.control);
769 
770 	/* release IRQ's */
771 	for (i = 0; i < ARRAY_SIZE(snd_sgio2_isr_table); i++)
772 		free_irq(snd_sgio2_isr_table[i].irq,
773 			 &chip->channel[snd_sgio2_isr_table[i].idx]);
774 
775 	dma_free_coherent(chip->card->dev, MACEISA_RINGBUFFERS_SIZE,
776 			  chip->ring_base, chip->ring_base_dma);
777 
778 	/* release card data */
779 	kfree(chip);
780 	return 0;
781 }
782 
783 static int snd_sgio2audio_dev_free(struct snd_device *device)
784 {
785 	struct snd_sgio2audio *chip = device->device_data;
786 
787 	return snd_sgio2audio_free(chip);
788 }
789 
790 static const struct snd_device_ops ops = {
791 	.dev_free = snd_sgio2audio_dev_free,
792 };
793 
794 static int snd_sgio2audio_create(struct snd_card *card,
795 				 struct snd_sgio2audio **rchip)
796 {
797 	struct snd_sgio2audio *chip;
798 	int i, err;
799 
800 	*rchip = NULL;
801 
802 	/* check if a codec is attached to the interface */
803 	/* (Audio or Audio/Video board present) */
804 	if (!(readq(&mace->perif.audio.control) & AUDIO_CONTROL_CODEC_PRESENT))
805 		return -ENOENT;
806 
807 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
808 	if (chip == NULL)
809 		return -ENOMEM;
810 
811 	chip->card = card;
812 
813 	chip->ring_base = dma_alloc_coherent(card->dev,
814 					     MACEISA_RINGBUFFERS_SIZE,
815 					     &chip->ring_base_dma, GFP_KERNEL);
816 	if (chip->ring_base == NULL) {
817 		printk(KERN_ERR
818 		       "sgio2audio: could not allocate ring buffers\n");
819 		kfree(chip);
820 		return -ENOMEM;
821 	}
822 
823 	spin_lock_init(&chip->ad1843_lock);
824 
825 	/* initialize channels */
826 	for (i = 0; i < 3; i++) {
827 		spin_lock_init(&chip->channel[i].lock);
828 		chip->channel[i].idx = i;
829 	}
830 
831 	/* allocate IRQs */
832 	for (i = 0; i < ARRAY_SIZE(snd_sgio2_isr_table); i++) {
833 		if (request_irq(snd_sgio2_isr_table[i].irq,
834 				snd_sgio2_isr_table[i].isr,
835 				0,
836 				snd_sgio2_isr_table[i].desc,
837 				&chip->channel[snd_sgio2_isr_table[i].idx])) {
838 			snd_sgio2audio_free(chip);
839 			printk(KERN_ERR "sgio2audio: cannot allocate irq %d\n",
840 			       snd_sgio2_isr_table[i].irq);
841 			return -EBUSY;
842 		}
843 	}
844 
845 	/* reset the interface */
846 	writeq(AUDIO_CONTROL_RESET, &mace->perif.audio.control);
847 	udelay(1);
848 	writeq(0, &mace->perif.audio.control);
849 	msleep_interruptible(1); /* give time to recover */
850 
851 	/* set ring base */
852 	writeq(chip->ring_base_dma, &mace->perif.ctrl.ringbase);
853 
854 	/* attach the AD1843 codec */
855 	chip->ad1843.read = read_ad1843_reg;
856 	chip->ad1843.write = write_ad1843_reg;
857 	chip->ad1843.chip = chip;
858 
859 	/* initialize the AD1843 codec */
860 	err = ad1843_init(&chip->ad1843);
861 	if (err < 0) {
862 		snd_sgio2audio_free(chip);
863 		return err;
864 	}
865 
866 	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
867 	if (err < 0) {
868 		snd_sgio2audio_free(chip);
869 		return err;
870 	}
871 	*rchip = chip;
872 	return 0;
873 }
874 
875 static int snd_sgio2audio_probe(struct platform_device *pdev)
876 {
877 	struct snd_card *card;
878 	struct snd_sgio2audio *chip;
879 	int err;
880 
881 	err = snd_card_new(&pdev->dev, index, id, THIS_MODULE, 0, &card);
882 	if (err < 0)
883 		return err;
884 
885 	err = snd_sgio2audio_create(card, &chip);
886 	if (err < 0) {
887 		snd_card_free(card);
888 		return err;
889 	}
890 
891 	err = snd_sgio2audio_new_pcm(chip);
892 	if (err < 0) {
893 		snd_card_free(card);
894 		return err;
895 	}
896 	err = snd_sgio2audio_new_mixer(chip);
897 	if (err < 0) {
898 		snd_card_free(card);
899 		return err;
900 	}
901 
902 	strcpy(card->driver, "SGI O2 Audio");
903 	strcpy(card->shortname, "SGI O2 Audio");
904 	sprintf(card->longname, "%s irq %i-%i",
905 		card->shortname,
906 		MACEISA_AUDIO1_DMAT_IRQ,
907 		MACEISA_AUDIO3_MERR_IRQ);
908 
909 	err = snd_card_register(card);
910 	if (err < 0) {
911 		snd_card_free(card);
912 		return err;
913 	}
914 	platform_set_drvdata(pdev, card);
915 	return 0;
916 }
917 
918 static int snd_sgio2audio_remove(struct platform_device *pdev)
919 {
920 	struct snd_card *card = platform_get_drvdata(pdev);
921 
922 	snd_card_free(card);
923 	return 0;
924 }
925 
926 static struct platform_driver sgio2audio_driver = {
927 	.probe	= snd_sgio2audio_probe,
928 	.remove	= snd_sgio2audio_remove,
929 	.driver = {
930 		.name	= "sgio2audio",
931 	}
932 };
933 
934 module_platform_driver(sgio2audio_driver);
935