1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Sound driver for Silicon Graphics O2 Workstations A/V board audio.
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>
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>
18 #include <linux/slab.h>
19 #include <linux/module.h>
21 #include <asm/ip32/ip32_ints.h>
22 #include <asm/ip32/mace.h>
24 #include <sound/core.h>
25 #include <sound/control.h>
26 #include <sound/pcm.h>
28 #include <sound/initval.h>
29 #include <sound/ad1843.h>
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}}");
37 static int index
= SNDRV_DEFAULT_IDX1
; /* Index 0-MAX */
38 static char *id
= SNDRV_DEFAULT_STR1
; /* ID for this card */
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.");
46 #define AUDIO_CONTROL_RESET BIT(0) /* 1: reset audio interface */
47 #define AUDIO_CONTROL_CODEC_PRESENT BIT(1) /* 1: codec detected */
49 #define CODEC_CONTROL_WORD_SHIFT 0
50 #define CODEC_CONTROL_READ BIT(16)
51 #define CODEC_CONTROL_ADDRESS_SHIFT 17
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 */
64 #define CHANNEL_RING_SHIFT 12
65 #define CHANNEL_RING_SIZE (1 << CHANNEL_RING_SHIFT)
66 #define CHANNEL_RING_MASK (CHANNEL_RING_SIZE - 1)
68 #define CHANNEL_LEFT_SHIFT 40
69 #define CHANNEL_RIGHT_SHIFT 8
71 struct snd_sgio2audio_chan
{
73 struct snd_pcm_substream
*substream
;
75 snd_pcm_uframes_t size
;
79 /* definition of the chip-specific record */
80 struct snd_sgio2audio
{
81 struct snd_card
*card
;
84 struct snd_ad1843 ad1843
;
85 spinlock_t ad1843_lock
;
88 struct snd_sgio2audio_chan channel
[3];
92 dma_addr_t ring_base_dma
;
98 * read_ad1843_reg returns the current contents of a 16 bit AD1843 register.
100 * Returns unsigned register value on success, -errno on failure.
102 static int read_ad1843_reg(void *priv
, int reg
)
104 struct snd_sgio2audio
*chip
= priv
;
108 spin_lock_irqsave(&chip
->ad1843_lock
, flags
);
110 writeq((reg
<< CODEC_CONTROL_ADDRESS_SHIFT
) |
111 CODEC_CONTROL_READ
, &mace
->perif
.audio
.codec_control
);
113 val
= readq(&mace
->perif
.audio
.codec_control
); /* flush bus */
116 val
= readq(&mace
->perif
.audio
.codec_read
);
118 spin_unlock_irqrestore(&chip
->ad1843_lock
, flags
);
123 * write_ad1843_reg writes the specified value to a 16 bit AD1843 register.
125 static int write_ad1843_reg(void *priv
, int reg
, int word
)
127 struct snd_sgio2audio
*chip
= priv
;
131 spin_lock_irqsave(&chip
->ad1843_lock
, flags
);
133 writeq((reg
<< CODEC_CONTROL_ADDRESS_SHIFT
) |
134 (word
<< CODEC_CONTROL_WORD_SHIFT
),
135 &mace
->perif
.audio
.codec_control
);
137 val
= readq(&mace
->perif
.audio
.codec_control
); /* flush bus */
140 spin_unlock_irqrestore(&chip
->ad1843_lock
, flags
);
144 static int sgio2audio_gain_info(struct snd_kcontrol
*kcontrol
,
145 struct snd_ctl_elem_info
*uinfo
)
147 struct snd_sgio2audio
*chip
= snd_kcontrol_chip(kcontrol
);
149 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
151 uinfo
->value
.integer
.min
= 0;
152 uinfo
->value
.integer
.max
= ad1843_get_gain_max(&chip
->ad1843
,
153 (int)kcontrol
->private_value
);
157 static int sgio2audio_gain_get(struct snd_kcontrol
*kcontrol
,
158 struct snd_ctl_elem_value
*ucontrol
)
160 struct snd_sgio2audio
*chip
= snd_kcontrol_chip(kcontrol
);
163 vol
= ad1843_get_gain(&chip
->ad1843
, (int)kcontrol
->private_value
);
165 ucontrol
->value
.integer
.value
[0] = (vol
>> 8) & 0xFF;
166 ucontrol
->value
.integer
.value
[1] = vol
& 0xFF;
171 static int sgio2audio_gain_put(struct snd_kcontrol
*kcontrol
,
172 struct snd_ctl_elem_value
*ucontrol
)
174 struct snd_sgio2audio
*chip
= snd_kcontrol_chip(kcontrol
);
177 oldvol
= ad1843_get_gain(&chip
->ad1843
, kcontrol
->private_value
);
178 newvol
= (ucontrol
->value
.integer
.value
[0] << 8) |
179 ucontrol
->value
.integer
.value
[1];
181 newvol
= ad1843_set_gain(&chip
->ad1843
, kcontrol
->private_value
,
184 return newvol
!= oldvol
;
187 static int sgio2audio_source_info(struct snd_kcontrol
*kcontrol
,
188 struct snd_ctl_elem_info
*uinfo
)
190 static const char * const texts
[3] = {
191 "Cam Mic", "Mic", "Line"
193 return snd_ctl_enum_info(uinfo
, 1, 3, texts
);
196 static int sgio2audio_source_get(struct snd_kcontrol
*kcontrol
,
197 struct snd_ctl_elem_value
*ucontrol
)
199 struct snd_sgio2audio
*chip
= snd_kcontrol_chip(kcontrol
);
201 ucontrol
->value
.enumerated
.item
[0] = ad1843_get_recsrc(&chip
->ad1843
);
205 static int sgio2audio_source_put(struct snd_kcontrol
*kcontrol
,
206 struct snd_ctl_elem_value
*ucontrol
)
208 struct snd_sgio2audio
*chip
= snd_kcontrol_chip(kcontrol
);
211 oldsrc
= ad1843_get_recsrc(&chip
->ad1843
);
212 newsrc
= ad1843_set_recsrc(&chip
->ad1843
,
213 ucontrol
->value
.enumerated
.item
[0]);
215 return newsrc
!= oldsrc
;
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",
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
,
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",
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
,
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
,
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
,
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",
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
,
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",
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
,
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
,
299 static int snd_sgio2audio_new_mixer(struct snd_sgio2audio
*chip
)
303 err
= snd_ctl_add(chip
->card
,
304 snd_ctl_new1(&sgio2audio_ctrl_pcm0
, chip
));
308 err
= snd_ctl_add(chip
->card
,
309 snd_ctl_new1(&sgio2audio_ctrl_pcm1
, chip
));
313 err
= snd_ctl_add(chip
->card
,
314 snd_ctl_new1(&sgio2audio_ctrl_reclevel
, chip
));
318 err
= snd_ctl_add(chip
->card
,
319 snd_ctl_new1(&sgio2audio_ctrl_recsource
, chip
));
322 err
= snd_ctl_add(chip
->card
,
323 snd_ctl_new1(&sgio2audio_ctrl_line
, chip
));
327 err
= snd_ctl_add(chip
->card
,
328 snd_ctl_new1(&sgio2audio_ctrl_cd
, chip
));
332 err
= snd_ctl_add(chip
->card
,
333 snd_ctl_new1(&sgio2audio_ctrl_mic
, chip
));
340 /* low-level audio interface DMA */
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
)
348 unsigned long src_base
, src_pos
, dst_mask
;
349 unsigned char *dst_base
;
355 struct snd_pcm_runtime
*runtime
= chip
->channel
[ch
].substream
->runtime
;
357 spin_lock_irqsave(&chip
->channel
[ch
].lock
, flags
);
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;
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
;
371 src
= (u64
*)(src_base
+ src_pos
);
372 dst
= (s16
*)(dst_base
+ dst_pos
);
375 dst
[0] = (x
>> CHANNEL_LEFT_SHIFT
) & 0xffff;
376 dst
[1] = (x
>> CHANNEL_RIGHT_SHIFT
) & 0xffff;
378 src_pos
= (src_pos
+ sizeof(u64
)) & CHANNEL_RING_MASK
;
379 dst_pos
= (dst_pos
+ 2 * sizeof(s16
)) & dst_mask
;
380 count
-= sizeof(u64
);
383 writeq(src_pos
, &mace
->perif
.audio
.chan
[ch
].read_ptr
); /* in bytes */
384 chip
->channel
[ch
].pos
= dst_pos
;
386 spin_unlock_irqrestore(&chip
->channel
[ch
].lock
, flags
);
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
)
397 unsigned long dst_base
, dst_pos
, src_mask
;
398 unsigned char *src_base
;
403 struct snd_pcm_runtime
*runtime
= chip
->channel
[ch
].substream
->runtime
;
405 spin_lock_irqsave(&chip
->channel
[ch
].lock
, flags
);
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;
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
;
419 src
= (s16
*)(src_base
+ src_pos
);
420 dst
= (u64
*)(dst_base
+ dst_pos
);
422 l
= src
[0]; /* sign extend */
423 r
= src
[1]; /* sign extend */
425 *dst
= ((l
& 0x00ffffff) << CHANNEL_LEFT_SHIFT
) |
426 ((r
& 0x00ffffff) << CHANNEL_RIGHT_SHIFT
);
428 dst_pos
= (dst_pos
+ sizeof(u64
)) & CHANNEL_RING_MASK
;
429 src_pos
= (src_pos
+ 2 * sizeof(s16
)) & src_mask
;
430 count
-= sizeof(u64
);
433 writeq(dst_pos
, &mace
->perif
.audio
.chan
[ch
].write_ptr
); /* in bytes */
434 chip
->channel
[ch
].pos
= src_pos
;
436 spin_unlock_irqrestore(&chip
->channel
[ch
].lock
, flags
);
440 static int snd_sgio2audio_dma_start(struct snd_pcm_substream
*substream
)
442 struct snd_sgio2audio
*chip
= snd_pcm_substream_chip(substream
);
443 struct snd_sgio2audio_chan
*chan
= substream
->runtime
->private_data
;
446 /* reset DMA channel */
447 writeq(CHANNEL_CONTROL_RESET
, &mace
->perif
.audio
.chan
[ch
].control
);
449 writeq(0, &mace
->perif
.audio
.chan
[ch
].control
);
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);
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
);
461 static int snd_sgio2audio_dma_stop(struct snd_pcm_substream
*substream
)
463 struct snd_sgio2audio_chan
*chan
= substream
->runtime
->private_data
;
465 writeq(0, &mace
->perif
.audio
.chan
[chan
->idx
].control
);
469 static irqreturn_t
snd_sgio2audio_dma_in_isr(int irq
, void *dev_id
)
471 struct snd_sgio2audio_chan
*chan
= dev_id
;
472 struct snd_pcm_substream
*substream
;
473 struct snd_sgio2audio
*chip
;
476 substream
= chan
->substream
;
477 chip
= snd_pcm_substream_chip(substream
);
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
);
489 static irqreturn_t
snd_sgio2audio_dma_out_isr(int irq
, void *dev_id
)
491 struct snd_sgio2audio_chan
*chan
= dev_id
;
492 struct snd_pcm_substream
*substream
;
493 struct snd_sgio2audio
*chip
;
496 substream
= chan
->substream
;
497 chip
= snd_pcm_substream_chip(substream
);
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
);
508 static irqreturn_t
snd_sgio2audio_error_isr(int irq
, void *dev_id
)
510 struct snd_sgio2audio_chan
*chan
= dev_id
;
511 struct snd_pcm_substream
*substream
;
513 substream
= chan
->substream
;
514 snd_sgio2audio_dma_stop(substream
);
515 snd_sgio2audio_dma_start(substream
);
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
,
532 .buffer_bytes_max
= 65536,
533 .period_bytes_min
= 32768,
534 .period_bytes_max
= 65536,
539 /* PCM playback open callback */
540 static int snd_sgio2audio_playback1_open(struct snd_pcm_substream
*substream
)
542 struct snd_sgio2audio
*chip
= snd_pcm_substream_chip(substream
);
543 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
545 runtime
->hw
= snd_sgio2audio_pcm_hw
;
546 runtime
->private_data
= &chip
->channel
[1];
550 static int snd_sgio2audio_playback2_open(struct snd_pcm_substream
*substream
)
552 struct snd_sgio2audio
*chip
= snd_pcm_substream_chip(substream
);
553 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
555 runtime
->hw
= snd_sgio2audio_pcm_hw
;
556 runtime
->private_data
= &chip
->channel
[2];
560 /* PCM capture open callback */
561 static int snd_sgio2audio_capture_open(struct snd_pcm_substream
*substream
)
563 struct snd_sgio2audio
*chip
= snd_pcm_substream_chip(substream
);
564 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
566 runtime
->hw
= snd_sgio2audio_pcm_hw
;
567 runtime
->private_data
= &chip
->channel
[0];
571 /* PCM close callback */
572 static int snd_sgio2audio_pcm_close(struct snd_pcm_substream
*substream
)
574 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
576 runtime
->private_data
= NULL
;
580 /* prepare callback */
581 static int snd_sgio2audio_pcm_prepare(struct snd_pcm_substream
*substream
)
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
;
589 spin_lock_irqsave(&chip
->channel
[ch
].lock
, flags
);
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
;
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
,
603 SNDRV_PCM_FORMAT_S16_LE
,
606 case SNDRV_PCM_STREAM_CAPTURE
:
607 ad1843_setup_adc(&chip
->ad1843
,
609 SNDRV_PCM_FORMAT_S16_LE
,
613 spin_unlock_irqrestore(&chip
->channel
[ch
].lock
, flags
);
617 /* trigger callback */
618 static int snd_sgio2audio_pcm_trigger(struct snd_pcm_substream
*substream
,
622 case SNDRV_PCM_TRIGGER_START
:
623 /* start the PCM engine */
624 snd_sgio2audio_dma_start(substream
);
626 case SNDRV_PCM_TRIGGER_STOP
:
627 /* stop the PCM engine */
628 snd_sgio2audio_dma_stop(substream
);
636 /* pointer callback */
637 static snd_pcm_uframes_t
638 snd_sgio2audio_pcm_pointer(struct snd_pcm_substream
*substream
)
640 struct snd_sgio2audio
*chip
= snd_pcm_substream_chip(substream
);
641 struct snd_sgio2audio_chan
*chan
= substream
->runtime
->private_data
;
643 /* get the current hardware pointer */
644 return bytes_to_frames(substream
->runtime
,
645 chip
->channel
[chan
->idx
].pos
);
649 static const struct snd_pcm_ops snd_sgio2audio_playback1_ops
= {
650 .open
= snd_sgio2audio_playback1_open
,
651 .close
= snd_sgio2audio_pcm_close
,
652 .prepare
= snd_sgio2audio_pcm_prepare
,
653 .trigger
= snd_sgio2audio_pcm_trigger
,
654 .pointer
= snd_sgio2audio_pcm_pointer
,
657 static const struct snd_pcm_ops snd_sgio2audio_playback2_ops
= {
658 .open
= snd_sgio2audio_playback2_open
,
659 .close
= snd_sgio2audio_pcm_close
,
660 .prepare
= snd_sgio2audio_pcm_prepare
,
661 .trigger
= snd_sgio2audio_pcm_trigger
,
662 .pointer
= snd_sgio2audio_pcm_pointer
,
665 static const struct snd_pcm_ops snd_sgio2audio_capture_ops
= {
666 .open
= snd_sgio2audio_capture_open
,
667 .close
= snd_sgio2audio_pcm_close
,
668 .prepare
= snd_sgio2audio_pcm_prepare
,
669 .trigger
= snd_sgio2audio_pcm_trigger
,
670 .pointer
= snd_sgio2audio_pcm_pointer
,
674 * definitions of capture are omitted here...
677 /* create a pcm device */
678 static int snd_sgio2audio_new_pcm(struct snd_sgio2audio
*chip
)
683 /* create first pcm device with one outputs and one input */
684 err
= snd_pcm_new(chip
->card
, "SGI O2 Audio", 0, 1, 1, &pcm
);
688 pcm
->private_data
= chip
;
689 strcpy(pcm
->name
, "SGI O2 DAC1");
692 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
693 &snd_sgio2audio_playback1_ops
);
694 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
,
695 &snd_sgio2audio_capture_ops
);
696 snd_pcm_set_managed_buffer_all(pcm
, SNDRV_DMA_TYPE_VMALLOC
, NULL
, 0, 0);
698 /* create second pcm device with one outputs and no input */
699 err
= snd_pcm_new(chip
->card
, "SGI O2 Audio", 1, 1, 0, &pcm
);
703 pcm
->private_data
= chip
;
704 strcpy(pcm
->name
, "SGI O2 DAC2");
707 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
708 &snd_sgio2audio_playback2_ops
);
709 snd_pcm_set_managed_buffer_all(pcm
, SNDRV_DMA_TYPE_VMALLOC
, NULL
, 0, 0);
717 irqreturn_t (*isr
)(int, void *);
719 } snd_sgio2_isr_table
[] = {
722 .irq
= MACEISA_AUDIO1_DMAT_IRQ
,
723 .isr
= snd_sgio2audio_dma_in_isr
,
724 .desc
= "Capture DMA Channel 0"
727 .irq
= MACEISA_AUDIO1_OF_IRQ
,
728 .isr
= snd_sgio2audio_error_isr
,
729 .desc
= "Capture Overflow"
732 .irq
= MACEISA_AUDIO2_DMAT_IRQ
,
733 .isr
= snd_sgio2audio_dma_out_isr
,
734 .desc
= "Playback DMA Channel 1"
737 .irq
= MACEISA_AUDIO2_MERR_IRQ
,
738 .isr
= snd_sgio2audio_error_isr
,
739 .desc
= "Memory Error Channel 1"
742 .irq
= MACEISA_AUDIO3_DMAT_IRQ
,
743 .isr
= snd_sgio2audio_dma_out_isr
,
744 .desc
= "Playback DMA Channel 2"
747 .irq
= MACEISA_AUDIO3_MERR_IRQ
,
748 .isr
= snd_sgio2audio_error_isr
,
749 .desc
= "Memory Error Channel 2"
755 static int snd_sgio2audio_free(struct snd_sgio2audio
*chip
)
759 /* reset interface */
760 writeq(AUDIO_CONTROL_RESET
, &mace
->perif
.audio
.control
);
762 writeq(0, &mace
->perif
.audio
.control
);
765 for (i
= 0; i
< ARRAY_SIZE(snd_sgio2_isr_table
); i
++)
766 free_irq(snd_sgio2_isr_table
[i
].irq
,
767 &chip
->channel
[snd_sgio2_isr_table
[i
].idx
]);
769 dma_free_coherent(chip
->card
->dev
, MACEISA_RINGBUFFERS_SIZE
,
770 chip
->ring_base
, chip
->ring_base_dma
);
772 /* release card data */
777 static int snd_sgio2audio_dev_free(struct snd_device
*device
)
779 struct snd_sgio2audio
*chip
= device
->device_data
;
781 return snd_sgio2audio_free(chip
);
784 static const struct snd_device_ops ops
= {
785 .dev_free
= snd_sgio2audio_dev_free
,
788 static int snd_sgio2audio_create(struct snd_card
*card
,
789 struct snd_sgio2audio
**rchip
)
791 struct snd_sgio2audio
*chip
;
796 /* check if a codec is attached to the interface */
797 /* (Audio or Audio/Video board present) */
798 if (!(readq(&mace
->perif
.audio
.control
) & AUDIO_CONTROL_CODEC_PRESENT
))
801 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
807 chip
->ring_base
= dma_alloc_coherent(card
->dev
,
808 MACEISA_RINGBUFFERS_SIZE
,
809 &chip
->ring_base_dma
, GFP_KERNEL
);
810 if (chip
->ring_base
== NULL
) {
812 "sgio2audio: could not allocate ring buffers\n");
817 spin_lock_init(&chip
->ad1843_lock
);
819 /* initialize channels */
820 for (i
= 0; i
< 3; i
++) {
821 spin_lock_init(&chip
->channel
[i
].lock
);
822 chip
->channel
[i
].idx
= i
;
826 for (i
= 0; i
< ARRAY_SIZE(snd_sgio2_isr_table
); i
++) {
827 if (request_irq(snd_sgio2_isr_table
[i
].irq
,
828 snd_sgio2_isr_table
[i
].isr
,
830 snd_sgio2_isr_table
[i
].desc
,
831 &chip
->channel
[snd_sgio2_isr_table
[i
].idx
])) {
832 snd_sgio2audio_free(chip
);
833 printk(KERN_ERR
"sgio2audio: cannot allocate irq %d\n",
834 snd_sgio2_isr_table
[i
].irq
);
839 /* reset the interface */
840 writeq(AUDIO_CONTROL_RESET
, &mace
->perif
.audio
.control
);
842 writeq(0, &mace
->perif
.audio
.control
);
843 msleep_interruptible(1); /* give time to recover */
846 writeq(chip
->ring_base_dma
, &mace
->perif
.ctrl
.ringbase
);
848 /* attach the AD1843 codec */
849 chip
->ad1843
.read
= read_ad1843_reg
;
850 chip
->ad1843
.write
= write_ad1843_reg
;
851 chip
->ad1843
.chip
= chip
;
853 /* initialize the AD1843 codec */
854 err
= ad1843_init(&chip
->ad1843
);
856 snd_sgio2audio_free(chip
);
860 err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, chip
, &ops
);
862 snd_sgio2audio_free(chip
);
869 static int snd_sgio2audio_probe(struct platform_device
*pdev
)
871 struct snd_card
*card
;
872 struct snd_sgio2audio
*chip
;
875 err
= snd_card_new(&pdev
->dev
, index
, id
, THIS_MODULE
, 0, &card
);
879 err
= snd_sgio2audio_create(card
, &chip
);
885 err
= snd_sgio2audio_new_pcm(chip
);
890 err
= snd_sgio2audio_new_mixer(chip
);
896 strcpy(card
->driver
, "SGI O2 Audio");
897 strcpy(card
->shortname
, "SGI O2 Audio");
898 sprintf(card
->longname
, "%s irq %i-%i",
900 MACEISA_AUDIO1_DMAT_IRQ
,
901 MACEISA_AUDIO3_MERR_IRQ
);
903 err
= snd_card_register(card
);
908 platform_set_drvdata(pdev
, card
);
912 static int snd_sgio2audio_remove(struct platform_device
*pdev
)
914 struct snd_card
*card
= platform_get_drvdata(pdev
);
920 static struct platform_driver sgio2audio_driver
= {
921 .probe
= snd_sgio2audio_probe
,
922 .remove
= snd_sgio2audio_remove
,
924 .name
= "sgio2audio",
928 module_platform_driver(sgio2audio_driver
);