144603Sdcs// SPDX-License-Identifier: GPL-2.0-or-later
244603Sdcs/*
344603Sdcs *   Sound driver for Silicon Graphics O2 Workstations A/V board audio.
444603Sdcs *
544603Sdcs *   Copyright 2003 Vivien Chappelier <vivien.chappelier@linux-mips.org>
644603Sdcs *   Copyright 2008 Thomas Bogendoerfer <tsbogend@alpha.franken.de>
744603Sdcs *   Mxier part taken from mace_audio.c:
844603Sdcs *   Copyright 2007 Thorben J��ndling <tj.trevelyan@gmail.com>
950477Speter */
1044603Sdcs
1144603Sdcs#include <linux/init.h>
1244603Sdcs#include <linux/delay.h>
1344603Sdcs#include <linux/spinlock.h>
1444603Sdcs#include <linux/interrupt.h>
1544603Sdcs#include <linux/dma-mapping.h>
1644603Sdcs#include <linux/platform_device.h>
1765785Sdcs#include <linux/io.h>
1865785Sdcs#include <linux/slab.h>
19135986Sru#include <linux/module.h>
2044603Sdcs
2197201Sgordon#include <asm/ip32/ip32_ints.h>
2297201Sgordon#include <asm/ip32/mace.h>
2397201Sgordon
2444603Sdcs#include <sound/core.h>
2544603Sdcs#include <sound/control.h>
2644603Sdcs#include <sound/pcm.h>
2744603Sdcs#define SNDRV_GET_ID
2844603Sdcs#include <sound/initval.h>
2944603Sdcs#include <sound/ad1843.h>
3044603Sdcs
3144603Sdcs
3247171SdcsMODULE_AUTHOR("Vivien Chappelier <vivien.chappelier@linux-mips.org>");
3347171SdcsMODULE_DESCRIPTION("SGI O2 Audio");
3444603SdcsMODULE_LICENSE("GPL");
3544603Sdcs
3665939Sdcsstatic int index = SNDRV_DEFAULT_IDX1;  /* Index 0-MAX */
3765939Sdcsstatic char *id = SNDRV_DEFAULT_STR1;   /* ID for this card */
38137344Skrion
3944603Sdcsmodule_param(index, int, 0444);
4044603SdcsMODULE_PARM_DESC(index, "Index value for SGI O2 soundcard.");
4144603Sdcsmodule_param(id, charp, 0444);
4244603SdcsMODULE_PARM_DESC(id, "ID string for SGI O2 soundcard.");
4344603Sdcs
4444603Sdcs
45146421Ssobomax#define AUDIO_CONTROL_RESET              BIT(0) /* 1: reset audio interface */
46146421Ssobomax#define AUDIO_CONTROL_CODEC_PRESENT      BIT(1) /* 1: codec detected */
47146421Ssobomax
48146421Ssobomax#define CODEC_CONTROL_WORD_SHIFT        0
49117090Sbrueffer#define CODEC_CONTROL_READ              BIT(16)
50149213Siedowse#define CODEC_CONTROL_ADDRESS_SHIFT     17
51148515Sbrian
5254247Sdcs#define CHANNEL_CONTROL_RESET           BIT(10) /* 1: reset channel */
53133217Sjmg#define CHANNEL_DMA_ENABLE              BIT(9)  /* 1: enable DMA transfer */
5452749Sdcs#define CHANNEL_INT_THRESHOLD_DISABLED  (0 << 5) /* interrupt disabled */
5554247Sdcs#define CHANNEL_INT_THRESHOLD_25        (1 << 5) /* int on buffer >25% full */
5654247Sdcs#define CHANNEL_INT_THRESHOLD_50        (2 << 5) /* int on buffer >50% full */
5744603Sdcs#define CHANNEL_INT_THRESHOLD_75        (3 << 5) /* int on buffer >75% full */
5844603Sdcs#define CHANNEL_INT_THRESHOLD_EMPTY     (4 << 5) /* int on buffer empty */
5944603Sdcs#define CHANNEL_INT_THRESHOLD_NOT_EMPTY (5 << 5) /* int on buffer !empty */
6044603Sdcs#define CHANNEL_INT_THRESHOLD_FULL      (6 << 5) /* int on buffer empty */
6144603Sdcs#define CHANNEL_INT_THRESHOLD_NOT_FULL  (7 << 5) /* int on buffer !empty */
6244603Sdcs
63148515Sbrian#define CHANNEL_RING_SHIFT              12
64148515Sbrian#define CHANNEL_RING_SIZE               (1 << CHANNEL_RING_SHIFT)
65148515Sbrian#define CHANNEL_RING_MASK               (CHANNEL_RING_SIZE - 1)
66148515Sbrian
67148515Sbrian#define CHANNEL_LEFT_SHIFT 40
68148515Sbrian#define CHANNEL_RIGHT_SHIFT 8
69148515Sbrian
70148515Sbrianstruct snd_sgio2audio_chan {
71148515Sbrian	int idx;
72148515Sbrian	struct snd_pcm_substream *substream;
73148515Sbrian	int pos;
74148515Sbrian	snd_pcm_uframes_t size;
75142009Sdes	spinlock_t lock;
7654265Smsmith};
7744603Sdcs
7844603Sdcs/* definition of the chip-specific record */
7944603Sdcsstruct snd_sgio2audio {
8044603Sdcs	struct snd_card *card;
8144603Sdcs
8244603Sdcs	/* codec */
83132853Sceri	struct snd_ad1843 ad1843;
8499332Smini	spinlock_t ad1843_lock;
8599332Smini
8699332Smini	/* channels */
8799332Smini	struct snd_sgio2audio_chan channel[3];
8899332Smini
8999332Smini	/* resources */
9099332Smini	void *ring_base;
9199332Smini	dma_addr_t ring_base_dma;
9299332Smini};
9399332Smini
9486902Srwatson/* AD1843 access */
95148817Skrion
9699332Smini/*
9799332Smini * read_ad1843_reg returns the current contents of a 16 bit AD1843 register.
98102862Sbrooks *
99106335Smini * Returns unsigned register value on success, -errno on failure.
10044603Sdcs */
10199332Sministatic int read_ad1843_reg(void *priv, int reg)
10299332Smini{
10344603Sdcs	struct snd_sgio2audio *chip = priv;
10495524Sdcs	int val;
10595524Sdcs	unsigned long flags;
106125091Sdes
10770706Sjhb	spin_lock_irqsave(&chip->ad1843_lock, flags);
10870706Sjhb
10970706Sjhb	writeq((reg << CODEC_CONTROL_ADDRESS_SHIFT) |
110111749Sharti	       CODEC_CONTROL_READ, &mace->perif.audio.codec_control);
111111749Sharti	wmb();
11244603Sdcs	val = readq(&mace->perif.audio.codec_control); /* flush bus */
11344603Sdcs	udelay(200);
11444603Sdcs
11544603Sdcs	val = readq(&mace->perif.audio.codec_read);
11644603Sdcs
11744603Sdcs	spin_unlock_irqrestore(&chip->ad1843_lock, flags);
11844603Sdcs	return val;
11944603Sdcs}
12044603Sdcs
12144603Sdcs/*
12277034Sru * write_ad1843_reg writes the specified value to a 16 bit AD1843 register.
123126455Sdes */
12477162Srustatic int write_ad1843_reg(void *priv, int reg, int word)
125135986Sru{
126135986Sru	struct snd_sgio2audio *chip = priv;
12747171Sdcs	int val;
12863962Ssheldonh	unsigned long flags;
12977034Sru
13044603Sdcs	spin_lock_irqsave(&chip->ad1843_lock, flags);
131135986Sru
13277034Sru	writeq((reg << CODEC_CONTROL_ADDRESS_SHIFT) |
13344603Sdcs	       (word << CODEC_CONTROL_WORD_SHIFT),
13444603Sdcs	       &mace->perif.audio.codec_control);
13544603Sdcs	wmb();
136135986Sru	val = readq(&mace->perif.audio.codec_control); /* flush bus */
137135986Sru	udelay(200);
138135986Sru
13944603Sdcs	spin_unlock_irqrestore(&chip->ad1843_lock, flags);
14044603Sdcs	return 0;
14144603Sdcs}
14244603Sdcs
14344603Sdcsstatic int sgio2audio_gain_info(struct snd_kcontrol *kcontrol,
14444603Sdcs			       struct snd_ctl_elem_info *uinfo)
14544603Sdcs{
14644603Sdcs	struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
14744603Sdcs
14844603Sdcs	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
14944603Sdcs	uinfo->count = 2;
15044603Sdcs	uinfo->value.integer.min = 0;
15144603Sdcs	uinfo->value.integer.max = ad1843_get_gain_max(&chip->ad1843,
15244603Sdcs					     (int)kcontrol->private_value);
15344603Sdcs	return 0;
15444603Sdcs}
15544603Sdcs
15644603Sdcsstatic int sgio2audio_gain_get(struct snd_kcontrol *kcontrol,
15744603Sdcs			       struct snd_ctl_elem_value *ucontrol)
15847173Sdcs{
15947173Sdcs	struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
16044603Sdcs	int vol;
16144603Sdcs
16244603Sdcs	vol = ad1843_get_gain(&chip->ad1843, (int)kcontrol->private_value);
16344603Sdcs
16444603Sdcs	ucontrol->value.integer.value[0] = (vol >> 8) & 0xFF;
16544603Sdcs	ucontrol->value.integer.value[1] = vol & 0xFF;
16644603Sdcs
167106330Smini	return 0;
168106335Smini}
169106335Smini
170106335Sministatic int sgio2audio_gain_put(struct snd_kcontrol *kcontrol,
17144603Sdcs			struct snd_ctl_elem_value *ucontrol)
17244603Sdcs{
173106335Smini	struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
174106335Smini	int newvol, oldvol;
17544603Sdcs
176106335Smini	oldvol = ad1843_get_gain(&chip->ad1843, kcontrol->private_value);
17744603Sdcs	newvol = (ucontrol->value.integer.value[0] << 8) |
178126263Smlaier		ucontrol->value.integer.value[1];
17944603Sdcs
18044603Sdcs	newvol = ad1843_set_gain(&chip->ad1843, kcontrol->private_value,
18144603Sdcs		newvol);
18247175Sdcs
18347175Sdcs	return newvol != oldvol;
18447175Sdcs}
18554020Sdcs
18655992Swpaulstatic int sgio2audio_source_info(struct snd_kcontrol *kcontrol,
187106330Smini			       struct snd_ctl_elem_info *uinfo)
188148817Skrion{
189148798Skrion	static const char * const texts[3] = {
190148817Skrion		"Cam Mic", "Mic", "Line"
191115054Smurray	};
192148817Skrion	return snd_ctl_enum_info(uinfo, 1, 3, texts);
193135986Sru}
194106335Smini
195106330Sministatic int sgio2audio_source_get(struct snd_kcontrol *kcontrol,
196148817Skrion			       struct snd_ctl_elem_value *ucontrol)
197148817Skrion{
19855017Swpaul	struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
199106330Smini
200106330Smini	ucontrol->value.enumerated.item[0] = ad1843_get_recsrc(&chip->ad1843);
201106330Smini	return 0;
202106330Smini}
203115054Smurray
204106330Sministatic int sgio2audio_source_put(struct snd_kcontrol *kcontrol,
205115054Smurray			struct snd_ctl_elem_value *ucontrol)
206106330Smini{
20754019Sdcs	struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
208106330Smini	int newsrc, oldsrc;
209148817Skrion
210115054Smurray	oldsrc = ad1843_get_recsrc(&chip->ad1843);
211106335Smini	newsrc = ad1843_set_recsrc(&chip->ad1843,
212106335Smini				   ucontrol->value.enumerated.item[0]);
213115054Smurray
214106335Smini	return newsrc != oldsrc;
215148817Skrion}
216106335Smini
217106335Smini/* dac1/pcm0 mixer control */
218106335Sministatic const struct snd_kcontrol_new sgio2audio_ctrl_pcm0 = {
219135986Sru	.iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
22055017Swpaul	.name           = "PCM Playback Volume",
221148817Skrion	.index          = 0,
222106335Smini	.access         = SNDRV_CTL_ELEM_ACCESS_READWRITE,
22354019Sdcs	.private_value  = AD1843_GAIN_PCM_0,
22454019Sdcs	.info           = sgio2audio_gain_info,
22554019Sdcs	.get            = sgio2audio_gain_get,
226106330Smini	.put            = sgio2audio_gain_put,
227106330Smini};
228106335Smini
22954019Sdcs/* dac2/pcm1 mixer control */
23054019Sdcsstatic const struct snd_kcontrol_new sgio2audio_ctrl_pcm1 = {
231106330Smini	.iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
232106335Smini	.name           = "PCM Playback Volume",
233135986Sru	.index          = 1,
234148817Skrion	.access         = SNDRV_CTL_ELEM_ACCESS_READWRITE,
23554019Sdcs	.private_value  = AD1843_GAIN_PCM_1,
236106330Smini	.info           = sgio2audio_gain_info,
23754019Sdcs	.get            = sgio2audio_gain_get,
23855992Swpaul	.put            = sgio2audio_gain_put,
23961757Smjacob};
240106330Smini
24154019Sdcs/* record level mixer control */
24247175Sdcsstatic const struct snd_kcontrol_new sgio2audio_ctrl_reclevel = {
24347175Sdcs	.iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
24474212Sjhb	.name           = "Capture Volume",
24574212Sjhb	.access         = SNDRV_CTL_ELEM_ACCESS_READWRITE,
24674212Sjhb	.private_value  = AD1843_GAIN_RECLEV,
24774212Sjhb	.info           = sgio2audio_gain_info,
24874212Sjhb	.get            = sgio2audio_gain_get,
24974212Sjhb	.put            = sgio2audio_gain_put,
25074212Sjhb};
25174212Sjhb
25274212Sjhb/* record level source control */
253135986Srustatic const struct snd_kcontrol_new sgio2audio_ctrl_recsource = {
254135986Sru	.iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
25574212Sjhb	.name           = "Capture Source",
256135986Sru	.access         = SNDRV_CTL_ELEM_ACCESS_READWRITE,
25774212Sjhb	.info           = sgio2audio_source_info,
258135986Sru	.get            = sgio2audio_source_get,
259135986Sru	.put            = sgio2audio_source_put,
26074212Sjhb};
261135986Sru
26274212Sjhb/* line mixer control */
263135986Srustatic const struct snd_kcontrol_new sgio2audio_ctrl_line = {
26474212Sjhb	.iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
265135986Sru	.name           = "Line Playback Volume",
26674212Sjhb	.index          = 0,
26774212Sjhb	.access         = SNDRV_CTL_ELEM_ACCESS_READWRITE,
268135986Sru	.private_value  = AD1843_GAIN_LINE,
26974212Sjhb	.info           = sgio2audio_gain_info,
27074212Sjhb	.get            = sgio2audio_gain_get,
27174212Sjhb	.put            = sgio2audio_gain_put,
27274212Sjhb};
27374212Sjhb
27474212Sjhb/* cd mixer control */
275135986Srustatic const struct snd_kcontrol_new sgio2audio_ctrl_cd = {
276135986Sru	.iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
27774212Sjhb	.name           = "Line Playback Volume",
27874212Sjhb	.index          = 1,
27974212Sjhb	.access         = SNDRV_CTL_ELEM_ACCESS_READWRITE,
280135986Sru	.private_value  = AD1843_GAIN_LINE_2,
28174212Sjhb	.info           = sgio2audio_gain_info,
28274212Sjhb	.get            = sgio2audio_gain_get,
28374212Sjhb	.put            = sgio2audio_gain_put,
28474212Sjhb};
28574212Sjhb
286132248Stanimura/* mic mixer control */
28774212Sjhbstatic const struct snd_kcontrol_new sgio2audio_ctrl_mic = {
288135986Sru	.iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
28974212Sjhb	.name           = "Mic Playback Volume",
290135986Sru	.access         = SNDRV_CTL_ELEM_ACCESS_READWRITE,
29174212Sjhb	.private_value  = AD1843_GAIN_MIC,
29274212Sjhb	.info           = sgio2audio_gain_info,
29374212Sjhb	.get            = sgio2audio_gain_get,
294135986Sru	.put            = sgio2audio_gain_put,
29574212Sjhb};
29674212Sjhb
29788253Sjim
29874212Sjhbstatic int snd_sgio2audio_new_mixer(struct snd_sgio2audio *chip)
29974212Sjhb{
30074212Sjhb	int err;
30174212Sjhb
302135986Sru	err = snd_ctl_add(chip->card,
303135986Sru			  snd_ctl_new1(&sgio2audio_ctrl_pcm0, chip));
30474212Sjhb	if (err < 0)
30574212Sjhb		return err;
30674212Sjhb
307102011Sorion	err = snd_ctl_add(chip->card,
30874212Sjhb			  snd_ctl_new1(&sgio2audio_ctrl_pcm1, chip));
309135986Sru	if (err < 0)
310107175Sdcs		return err;
31174212Sjhb
31274212Sjhb	err = snd_ctl_add(chip->card,
31353243Sn_hibma			  snd_ctl_new1(&sgio2audio_ctrl_reclevel, chip));
31453243Sn_hibma	if (err < 0)
31553243Sn_hibma		return err;
31653548Sn_hibma
31759895Sn_hibma	err = snd_ctl_add(chip->card,
31853548Sn_hibma			  snd_ctl_new1(&sgio2audio_ctrl_recsource, chip));
31991609Salfred	if (err < 0)
32053548Sn_hibma		return err;
32153548Sn_hibma	err = snd_ctl_add(chip->card,
32253548Sn_hibma			  snd_ctl_new1(&sgio2audio_ctrl_line, chip));
32353548Sn_hibma	if (err < 0)
32453548Sn_hibma		return err;
32567955Sn_hibma
32667562Sn_hibma	err = snd_ctl_add(chip->card,
32755162Swpaul			  snd_ctl_new1(&sgio2audio_ctrl_cd, chip));
328115054Smurray	if (err < 0)
32955944Swpaul		return err;
33055429Swpaul
331135986Sru	err = snd_ctl_add(chip->card,
33253243Sn_hibma			  snd_ctl_new1(&sgio2audio_ctrl_mic, chip));
33353243Sn_hibma	if (err < 0)
33444603Sdcs		return err;
33544603Sdcs
33644603Sdcs	return 0;
33747890Sroger}
33861757Smjacob
33974212Sjhb/* low-level audio interface DMA */
34074263Sjhb
34174263Sjhb/* get data out of bounce buffer, count must be a multiple of 32 */
34265689Smarkm/* returns 1 if a period has elapsed */
343128280Scpercivastatic int snd_sgio2audio_dma_pull_frag(struct snd_sgio2audio *chip,
34444603Sdcs					unsigned int ch, unsigned int count)
34544603Sdcs{
34686133Siwasaki	int ret;
34786133Siwasaki	unsigned long src_base, src_pos, dst_mask;
34886133Siwasaki	unsigned char *dst_base;
34986133Siwasaki	int dst_pos;
35086133Siwasaki	u64 *src;
35186133Siwasaki	s16 *dst;
35286133Siwasaki	u64 x;
353126453Sdes	unsigned long flags;
35486133Siwasaki	struct snd_pcm_runtime *runtime = chip->channel[ch].substream->runtime;
35586133Siwasaki
356101187Srwatson	spin_lock_irqsave(&chip->channel[ch].lock, flags);
357101187Srwatson
358101187Srwatson	src_base = (unsigned long) chip->ring_base | (ch << CHANNEL_RING_SHIFT);
359101187Srwatson	src_pos = readq(&mace->perif.audio.chan[ch].read_ptr);
360101187Srwatson	dst_base = runtime->dma_area;
361101187Srwatson	dst_pos = chip->channel[ch].pos;
362101187Srwatson	dst_mask = frames_to_bytes(runtime, runtime->buffer_size) - 1;
363101187Srwatson
364101187Srwatson	/* check if a period has elapsed */
365101187Srwatson	chip->channel[ch].size += (count >> 3); /* in frames */
366101187Srwatson	ret = chip->channel[ch].size >= runtime->period_size;
367101187Srwatson	chip->channel[ch].size %= runtime->period_size;
36844603Sdcs
36944603Sdcs	while (count) {
37044603Sdcs		src = (u64 *)(src_base + src_pos);
37144603Sdcs		dst = (s16 *)(dst_base + dst_pos);
37244603Sdcs
37344603Sdcs		x = *src;
37444603Sdcs		dst[0] = (x >> CHANNEL_LEFT_SHIFT) & 0xffff;
37544603Sdcs		dst[1] = (x >> CHANNEL_RIGHT_SHIFT) & 0xffff;
37644603Sdcs
37744603Sdcs		src_pos = (src_pos + sizeof(u64)) & CHANNEL_RING_MASK;
37844603Sdcs		dst_pos = (dst_pos + 2 * sizeof(s16)) & dst_mask;
379		count -= sizeof(u64);
380	}
381
382	writeq(src_pos, &mace->perif.audio.chan[ch].read_ptr); /* in bytes */
383	chip->channel[ch].pos = dst_pos;
384
385	spin_unlock_irqrestore(&chip->channel[ch].lock, flags);
386	return ret;
387}
388
389/* put some DMA data in bounce buffer, count must be a multiple of 32 */
390/* returns 1 if a period has elapsed */
391static int snd_sgio2audio_dma_push_frag(struct snd_sgio2audio *chip,
392					unsigned int ch, unsigned int count)
393{
394	int ret;
395	s64 l, r;
396	unsigned long dst_base, dst_pos, src_mask;
397	unsigned char *src_base;
398	int src_pos;
399	u64 *dst;
400	s16 *src;
401	unsigned long flags;
402	struct snd_pcm_runtime *runtime = chip->channel[ch].substream->runtime;
403
404	spin_lock_irqsave(&chip->channel[ch].lock, flags);
405
406	dst_base = (unsigned long)chip->ring_base | (ch << CHANNEL_RING_SHIFT);
407	dst_pos = readq(&mace->perif.audio.chan[ch].write_ptr);
408	src_base = runtime->dma_area;
409	src_pos = chip->channel[ch].pos;
410	src_mask = frames_to_bytes(runtime, runtime->buffer_size) - 1;
411
412	/* check if a period has elapsed */
413	chip->channel[ch].size += (count >> 3); /* in frames */
414	ret = chip->channel[ch].size >= runtime->period_size;
415	chip->channel[ch].size %= runtime->period_size;
416
417	while (count) {
418		src = (s16 *)(src_base + src_pos);
419		dst = (u64 *)(dst_base + dst_pos);
420
421		l = src[0]; /* sign extend */
422		r = src[1]; /* sign extend */
423
424		*dst = ((l & 0x00ffffff) << CHANNEL_LEFT_SHIFT) |
425			((r & 0x00ffffff) << CHANNEL_RIGHT_SHIFT);
426
427		dst_pos = (dst_pos + sizeof(u64)) & CHANNEL_RING_MASK;
428		src_pos = (src_pos + 2 * sizeof(s16)) & src_mask;
429		count -= sizeof(u64);
430	}
431
432	writeq(dst_pos, &mace->perif.audio.chan[ch].write_ptr); /* in bytes */
433	chip->channel[ch].pos = src_pos;
434
435	spin_unlock_irqrestore(&chip->channel[ch].lock, flags);
436	return ret;
437}
438
439static int snd_sgio2audio_dma_start(struct snd_pcm_substream *substream)
440{
441	struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
442	struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
443	int ch = chan->idx;
444
445	/* reset DMA channel */
446	writeq(CHANNEL_CONTROL_RESET, &mace->perif.audio.chan[ch].control);
447	udelay(10);
448	writeq(0, &mace->perif.audio.chan[ch].control);
449
450	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
451		/* push a full buffer */
452		snd_sgio2audio_dma_push_frag(chip, ch, CHANNEL_RING_SIZE - 32);
453	}
454	/* set DMA to wake on 50% empty and enable interrupt */
455	writeq(CHANNEL_DMA_ENABLE | CHANNEL_INT_THRESHOLD_50,
456	       &mace->perif.audio.chan[ch].control);
457	return 0;
458}
459
460static int snd_sgio2audio_dma_stop(struct snd_pcm_substream *substream)
461{
462	struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
463
464	writeq(0, &mace->perif.audio.chan[chan->idx].control);
465	return 0;
466}
467
468static irqreturn_t snd_sgio2audio_dma_in_isr(int irq, void *dev_id)
469{
470	struct snd_sgio2audio_chan *chan = dev_id;
471	struct snd_pcm_substream *substream;
472	struct snd_sgio2audio *chip;
473	int count, ch;
474
475	substream = chan->substream;
476	chip = snd_pcm_substream_chip(substream);
477	ch = chan->idx;
478
479	/* empty the ring */
480	count = CHANNEL_RING_SIZE -
481		readq(&mace->perif.audio.chan[ch].depth) - 32;
482	if (snd_sgio2audio_dma_pull_frag(chip, ch, count))
483		snd_pcm_period_elapsed(substream);
484
485	return IRQ_HANDLED;
486}
487
488static irqreturn_t snd_sgio2audio_dma_out_isr(int irq, void *dev_id)
489{
490	struct snd_sgio2audio_chan *chan = dev_id;
491	struct snd_pcm_substream *substream;
492	struct snd_sgio2audio *chip;
493	int count, ch;
494
495	substream = chan->substream;
496	chip = snd_pcm_substream_chip(substream);
497	ch = chan->idx;
498	/* fill the ring */
499	count = CHANNEL_RING_SIZE -
500		readq(&mace->perif.audio.chan[ch].depth) - 32;
501	if (snd_sgio2audio_dma_push_frag(chip, ch, count))
502		snd_pcm_period_elapsed(substream);
503
504	return IRQ_HANDLED;
505}
506
507static irqreturn_t snd_sgio2audio_error_isr(int irq, void *dev_id)
508{
509	struct snd_sgio2audio_chan *chan = dev_id;
510	struct snd_pcm_substream *substream;
511
512	substream = chan->substream;
513	snd_sgio2audio_dma_stop(substream);
514	snd_sgio2audio_dma_start(substream);
515	return IRQ_HANDLED;
516}
517
518/* PCM part */
519/* PCM hardware definition */
520static const struct snd_pcm_hardware snd_sgio2audio_pcm_hw = {
521	.info = (SNDRV_PCM_INFO_MMAP |
522		 SNDRV_PCM_INFO_MMAP_VALID |
523		 SNDRV_PCM_INFO_INTERLEAVED |
524		 SNDRV_PCM_INFO_BLOCK_TRANSFER),
525	.formats =          SNDRV_PCM_FMTBIT_S16_BE,
526	.rates =            SNDRV_PCM_RATE_8000_48000,
527	.rate_min =         8000,
528	.rate_max =         48000,
529	.channels_min =     2,
530	.channels_max =     2,
531	.buffer_bytes_max = 65536,
532	.period_bytes_min = 32768,
533	.period_bytes_max = 65536,
534	.periods_min =      1,
535	.periods_max =      1024,
536};
537
538/* PCM playback open callback */
539static int snd_sgio2audio_playback1_open(struct snd_pcm_substream *substream)
540{
541	struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
542	struct snd_pcm_runtime *runtime = substream->runtime;
543
544	runtime->hw = snd_sgio2audio_pcm_hw;
545	runtime->private_data = &chip->channel[1];
546	return 0;
547}
548
549static int snd_sgio2audio_playback2_open(struct snd_pcm_substream *substream)
550{
551	struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
552	struct snd_pcm_runtime *runtime = substream->runtime;
553
554	runtime->hw = snd_sgio2audio_pcm_hw;
555	runtime->private_data = &chip->channel[2];
556	return 0;
557}
558
559/* PCM capture open callback */
560static int snd_sgio2audio_capture_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[0];
567	return 0;
568}
569
570/* PCM close callback */
571static int snd_sgio2audio_pcm_close(struct snd_pcm_substream *substream)
572{
573	struct snd_pcm_runtime *runtime = substream->runtime;
574
575	runtime->private_data = NULL;
576	return 0;
577}
578
579/* prepare callback */
580static int snd_sgio2audio_pcm_prepare(struct snd_pcm_substream *substream)
581{
582	struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
583	struct snd_pcm_runtime *runtime = substream->runtime;
584	struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
585	int ch = chan->idx;
586	unsigned long flags;
587
588	spin_lock_irqsave(&chip->channel[ch].lock, flags);
589
590	/* Setup the pseudo-dma transfer pointers.  */
591	chip->channel[ch].pos = 0;
592	chip->channel[ch].size = 0;
593	chip->channel[ch].substream = substream;
594
595	/* set AD1843 format */
596	/* hardware format is always S16_LE */
597	switch (substream->stream) {
598	case SNDRV_PCM_STREAM_PLAYBACK:
599		ad1843_setup_dac(&chip->ad1843,
600				 ch - 1,
601				 runtime->rate,
602				 SNDRV_PCM_FORMAT_S16_LE,
603				 runtime->channels);
604		break;
605	case SNDRV_PCM_STREAM_CAPTURE:
606		ad1843_setup_adc(&chip->ad1843,
607				 runtime->rate,
608				 SNDRV_PCM_FORMAT_S16_LE,
609				 runtime->channels);
610		break;
611	}
612	spin_unlock_irqrestore(&chip->channel[ch].lock, flags);
613	return 0;
614}
615
616/* trigger callback */
617static int snd_sgio2audio_pcm_trigger(struct snd_pcm_substream *substream,
618				      int cmd)
619{
620	switch (cmd) {
621	case SNDRV_PCM_TRIGGER_START:
622		/* start the PCM engine */
623		snd_sgio2audio_dma_start(substream);
624		break;
625	case SNDRV_PCM_TRIGGER_STOP:
626		/* stop the PCM engine */
627		snd_sgio2audio_dma_stop(substream);
628		break;
629	default:
630		return -EINVAL;
631	}
632	return 0;
633}
634
635/* pointer callback */
636static snd_pcm_uframes_t
637snd_sgio2audio_pcm_pointer(struct snd_pcm_substream *substream)
638{
639	struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
640	struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
641
642	/* get the current hardware pointer */
643	return bytes_to_frames(substream->runtime,
644			       chip->channel[chan->idx].pos);
645}
646
647/* operators */
648static const struct snd_pcm_ops snd_sgio2audio_playback1_ops = {
649	.open =        snd_sgio2audio_playback1_open,
650	.close =       snd_sgio2audio_pcm_close,
651	.prepare =     snd_sgio2audio_pcm_prepare,
652	.trigger =     snd_sgio2audio_pcm_trigger,
653	.pointer =     snd_sgio2audio_pcm_pointer,
654};
655
656static const struct snd_pcm_ops snd_sgio2audio_playback2_ops = {
657	.open =        snd_sgio2audio_playback2_open,
658	.close =       snd_sgio2audio_pcm_close,
659	.prepare =     snd_sgio2audio_pcm_prepare,
660	.trigger =     snd_sgio2audio_pcm_trigger,
661	.pointer =     snd_sgio2audio_pcm_pointer,
662};
663
664static const struct snd_pcm_ops snd_sgio2audio_capture_ops = {
665	.open =        snd_sgio2audio_capture_open,
666	.close =       snd_sgio2audio_pcm_close,
667	.prepare =     snd_sgio2audio_pcm_prepare,
668	.trigger =     snd_sgio2audio_pcm_trigger,
669	.pointer =     snd_sgio2audio_pcm_pointer,
670};
671
672/*
673 *  definitions of capture are omitted here...
674 */
675
676/* create a pcm device */
677static int snd_sgio2audio_new_pcm(struct snd_sgio2audio *chip)
678{
679	struct snd_pcm *pcm;
680	int err;
681
682	/* create first pcm device with one outputs and one input */
683	err = snd_pcm_new(chip->card, "SGI O2 Audio", 0, 1, 1, &pcm);
684	if (err < 0)
685		return err;
686
687	pcm->private_data = chip;
688	strcpy(pcm->name, "SGI O2 DAC1");
689
690	/* set operators */
691	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
692			&snd_sgio2audio_playback1_ops);
693	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
694			&snd_sgio2audio_capture_ops);
695	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
696
697	/* create second  pcm device with one outputs and no input */
698	err = snd_pcm_new(chip->card, "SGI O2 Audio", 1, 1, 0, &pcm);
699	if (err < 0)
700		return err;
701
702	pcm->private_data = chip;
703	strcpy(pcm->name, "SGI O2 DAC2");
704
705	/* set operators */
706	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
707			&snd_sgio2audio_playback2_ops);
708	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
709
710	return 0;
711}
712
713static struct {
714	int idx;
715	int irq;
716	irqreturn_t (*isr)(int, void *);
717	const char *desc;
718} snd_sgio2_isr_table[] = {
719	{
720		.idx = 0,
721		.irq = MACEISA_AUDIO1_DMAT_IRQ,
722		.isr = snd_sgio2audio_dma_in_isr,
723		.desc = "Capture DMA Channel 0"
724	}, {
725		.idx = 0,
726		.irq = MACEISA_AUDIO1_OF_IRQ,
727		.isr = snd_sgio2audio_error_isr,
728		.desc = "Capture Overflow"
729	}, {
730		.idx = 1,
731		.irq = MACEISA_AUDIO2_DMAT_IRQ,
732		.isr = snd_sgio2audio_dma_out_isr,
733		.desc = "Playback DMA Channel 1"
734	}, {
735		.idx = 1,
736		.irq = MACEISA_AUDIO2_MERR_IRQ,
737		.isr = snd_sgio2audio_error_isr,
738		.desc = "Memory Error Channel 1"
739	}, {
740		.idx = 2,
741		.irq = MACEISA_AUDIO3_DMAT_IRQ,
742		.isr = snd_sgio2audio_dma_out_isr,
743		.desc = "Playback DMA Channel 2"
744	}, {
745		.idx = 2,
746		.irq = MACEISA_AUDIO3_MERR_IRQ,
747		.isr = snd_sgio2audio_error_isr,
748		.desc = "Memory Error Channel 2"
749	}
750};
751
752/* ALSA driver */
753
754static int snd_sgio2audio_free(struct snd_sgio2audio *chip)
755{
756	int i;
757
758	/* reset interface */
759	writeq(AUDIO_CONTROL_RESET, &mace->perif.audio.control);
760	udelay(1);
761	writeq(0, &mace->perif.audio.control);
762
763	/* release IRQ's */
764	for (i = 0; i < ARRAY_SIZE(snd_sgio2_isr_table); i++)
765		free_irq(snd_sgio2_isr_table[i].irq,
766			 &chip->channel[snd_sgio2_isr_table[i].idx]);
767
768	dma_free_coherent(chip->card->dev, MACEISA_RINGBUFFERS_SIZE,
769			  chip->ring_base, chip->ring_base_dma);
770
771	/* release card data */
772	kfree(chip);
773	return 0;
774}
775
776static int snd_sgio2audio_dev_free(struct snd_device *device)
777{
778	struct snd_sgio2audio *chip = device->device_data;
779
780	return snd_sgio2audio_free(chip);
781}
782
783static const struct snd_device_ops ops = {
784	.dev_free = snd_sgio2audio_dev_free,
785};
786
787static int snd_sgio2audio_create(struct snd_card *card,
788				 struct snd_sgio2audio **rchip)
789{
790	struct snd_sgio2audio *chip;
791	int i, err;
792
793	*rchip = NULL;
794
795	/* check if a codec is attached to the interface */
796	/* (Audio or Audio/Video board present) */
797	if (!(readq(&mace->perif.audio.control) & AUDIO_CONTROL_CODEC_PRESENT))
798		return -ENOENT;
799
800	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
801	if (chip == NULL)
802		return -ENOMEM;
803
804	chip->card = card;
805
806	chip->ring_base = dma_alloc_coherent(card->dev,
807					     MACEISA_RINGBUFFERS_SIZE,
808					     &chip->ring_base_dma, GFP_KERNEL);
809	if (chip->ring_base == NULL) {
810		printk(KERN_ERR
811		       "sgio2audio: could not allocate ring buffers\n");
812		kfree(chip);
813		return -ENOMEM;
814	}
815
816	spin_lock_init(&chip->ad1843_lock);
817
818	/* initialize channels */
819	for (i = 0; i < 3; i++) {
820		spin_lock_init(&chip->channel[i].lock);
821		chip->channel[i].idx = i;
822	}
823
824	/* allocate IRQs */
825	for (i = 0; i < ARRAY_SIZE(snd_sgio2_isr_table); i++) {
826		if (request_irq(snd_sgio2_isr_table[i].irq,
827				snd_sgio2_isr_table[i].isr,
828				0,
829				snd_sgio2_isr_table[i].desc,
830				&chip->channel[snd_sgio2_isr_table[i].idx])) {
831			snd_sgio2audio_free(chip);
832			printk(KERN_ERR "sgio2audio: cannot allocate irq %d\n",
833			       snd_sgio2_isr_table[i].irq);
834			return -EBUSY;
835		}
836	}
837
838	/* reset the interface */
839	writeq(AUDIO_CONTROL_RESET, &mace->perif.audio.control);
840	udelay(1);
841	writeq(0, &mace->perif.audio.control);
842	msleep_interruptible(1); /* give time to recover */
843
844	/* set ring base */
845	writeq(chip->ring_base_dma, &mace->perif.ctrl.ringbase);
846
847	/* attach the AD1843 codec */
848	chip->ad1843.read = read_ad1843_reg;
849	chip->ad1843.write = write_ad1843_reg;
850	chip->ad1843.chip = chip;
851
852	/* initialize the AD1843 codec */
853	err = ad1843_init(&chip->ad1843);
854	if (err < 0) {
855		snd_sgio2audio_free(chip);
856		return err;
857	}
858
859	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
860	if (err < 0) {
861		snd_sgio2audio_free(chip);
862		return err;
863	}
864	*rchip = chip;
865	return 0;
866}
867
868static int snd_sgio2audio_probe(struct platform_device *pdev)
869{
870	struct snd_card *card;
871	struct snd_sgio2audio *chip;
872	int err;
873
874	err = snd_card_new(&pdev->dev, index, id, THIS_MODULE, 0, &card);
875	if (err < 0)
876		return err;
877
878	err = snd_sgio2audio_create(card, &chip);
879	if (err < 0) {
880		snd_card_free(card);
881		return err;
882	}
883
884	err = snd_sgio2audio_new_pcm(chip);
885	if (err < 0) {
886		snd_card_free(card);
887		return err;
888	}
889	err = snd_sgio2audio_new_mixer(chip);
890	if (err < 0) {
891		snd_card_free(card);
892		return err;
893	}
894
895	strcpy(card->driver, "SGI O2 Audio");
896	strcpy(card->shortname, "SGI O2 Audio");
897	sprintf(card->longname, "%s irq %i-%i",
898		card->shortname,
899		MACEISA_AUDIO1_DMAT_IRQ,
900		MACEISA_AUDIO3_MERR_IRQ);
901
902	err = snd_card_register(card);
903	if (err < 0) {
904		snd_card_free(card);
905		return err;
906	}
907	platform_set_drvdata(pdev, card);
908	return 0;
909}
910
911static void snd_sgio2audio_remove(struct platform_device *pdev)
912{
913	struct snd_card *card = platform_get_drvdata(pdev);
914
915	snd_card_free(card);
916}
917
918static struct platform_driver sgio2audio_driver = {
919	.probe	= snd_sgio2audio_probe,
920	.remove_new = snd_sgio2audio_remove,
921	.driver = {
922		.name	= "sgio2audio",
923	}
924};
925
926module_platform_driver(sgio2audio_driver);
927