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");
36 static int index
= SNDRV_DEFAULT_IDX1
; /* Index 0-MAX */
37 static char *id
= SNDRV_DEFAULT_STR1
; /* ID for this card */
39 module_param(index
, int, 0444);
40 MODULE_PARM_DESC(index
, "Index value for SGI O2 soundcard.");
41 module_param(id
, charp
, 0444);
42 MODULE_PARM_DESC(id
, "ID string for SGI O2 soundcard.");
45 #define AUDIO_CONTROL_RESET BIT(0) /* 1: reset audio interface */
46 #define AUDIO_CONTROL_CODEC_PRESENT BIT(1) /* 1: codec detected */
48 #define CODEC_CONTROL_WORD_SHIFT 0
49 #define CODEC_CONTROL_READ BIT(16)
50 #define CODEC_CONTROL_ADDRESS_SHIFT 17
52 #define CHANNEL_CONTROL_RESET BIT(10) /* 1: reset channel */
53 #define CHANNEL_DMA_ENABLE BIT(9) /* 1: enable DMA transfer */
54 #define CHANNEL_INT_THRESHOLD_DISABLED (0 << 5) /* interrupt disabled */
55 #define CHANNEL_INT_THRESHOLD_25 (1 << 5) /* int on buffer >25% full */
56 #define CHANNEL_INT_THRESHOLD_50 (2 << 5) /* int on buffer >50% full */
57 #define CHANNEL_INT_THRESHOLD_75 (3 << 5) /* int on buffer >75% full */
58 #define CHANNEL_INT_THRESHOLD_EMPTY (4 << 5) /* int on buffer empty */
59 #define CHANNEL_INT_THRESHOLD_NOT_EMPTY (5 << 5) /* int on buffer !empty */
60 #define CHANNEL_INT_THRESHOLD_FULL (6 << 5) /* int on buffer empty */
61 #define CHANNEL_INT_THRESHOLD_NOT_FULL (7 << 5) /* int on buffer !empty */
63 #define CHANNEL_RING_SHIFT 12
64 #define CHANNEL_RING_SIZE (1 << CHANNEL_RING_SHIFT)
65 #define CHANNEL_RING_MASK (CHANNEL_RING_SIZE - 1)
67 #define CHANNEL_LEFT_SHIFT 40
68 #define CHANNEL_RIGHT_SHIFT 8
70 struct snd_sgio2audio_chan
{
72 struct snd_pcm_substream
*substream
;
74 snd_pcm_uframes_t size
;
78 /* definition of the chip-specific record */
79 struct snd_sgio2audio
{
80 struct snd_card
*card
;
83 struct snd_ad1843 ad1843
;
84 spinlock_t ad1843_lock
;
87 struct snd_sgio2audio_chan channel
[3];
91 dma_addr_t ring_base_dma
;
97 * read_ad1843_reg returns the current contents of a 16 bit AD1843 register.
99 * Returns unsigned register value on success, -errno on failure.
101 static int read_ad1843_reg(void *priv
, int reg
)
103 struct snd_sgio2audio
*chip
= priv
;
107 spin_lock_irqsave(&chip
->ad1843_lock
, flags
);
109 writeq((reg
<< CODEC_CONTROL_ADDRESS_SHIFT
) |
110 CODEC_CONTROL_READ
, &mace
->perif
.audio
.codec_control
);
112 val
= readq(&mace
->perif
.audio
.codec_control
); /* flush bus */
115 val
= readq(&mace
->perif
.audio
.codec_read
);
117 spin_unlock_irqrestore(&chip
->ad1843_lock
, flags
);
122 * write_ad1843_reg writes the specified value to a 16 bit AD1843 register.
124 static int write_ad1843_reg(void *priv
, int reg
, int word
)
126 struct snd_sgio2audio
*chip
= priv
;
130 spin_lock_irqsave(&chip
->ad1843_lock
, flags
);
132 writeq((reg
<< CODEC_CONTROL_ADDRESS_SHIFT
) |
133 (word
<< CODEC_CONTROL_WORD_SHIFT
),
134 &mace
->perif
.audio
.codec_control
);
136 val
= readq(&mace
->perif
.audio
.codec_control
); /* flush bus */
139 spin_unlock_irqrestore(&chip
->ad1843_lock
, flags
);
143 static int sgio2audio_gain_info(struct snd_kcontrol
*kcontrol
,
144 struct snd_ctl_elem_info
*uinfo
)
146 struct snd_sgio2audio
*chip
= snd_kcontrol_chip(kcontrol
);
148 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
150 uinfo
->value
.integer
.min
= 0;
151 uinfo
->value
.integer
.max
= ad1843_get_gain_max(&chip
->ad1843
,
152 (int)kcontrol
->private_value
);
156 static int sgio2audio_gain_get(struct snd_kcontrol
*kcontrol
,
157 struct snd_ctl_elem_value
*ucontrol
)
159 struct snd_sgio2audio
*chip
= snd_kcontrol_chip(kcontrol
);
162 vol
= ad1843_get_gain(&chip
->ad1843
, (int)kcontrol
->private_value
);
164 ucontrol
->value
.integer
.value
[0] = (vol
>> 8) & 0xFF;
165 ucontrol
->value
.integer
.value
[1] = vol
& 0xFF;
170 static int sgio2audio_gain_put(struct snd_kcontrol
*kcontrol
,
171 struct snd_ctl_elem_value
*ucontrol
)
173 struct snd_sgio2audio
*chip
= snd_kcontrol_chip(kcontrol
);
176 oldvol
= ad1843_get_gain(&chip
->ad1843
, kcontrol
->private_value
);
177 newvol
= (ucontrol
->value
.integer
.value
[0] << 8) |
178 ucontrol
->value
.integer
.value
[1];
180 newvol
= ad1843_set_gain(&chip
->ad1843
, kcontrol
->private_value
,
183 return newvol
!= oldvol
;
186 static int sgio2audio_source_info(struct snd_kcontrol
*kcontrol
,
187 struct snd_ctl_elem_info
*uinfo
)
189 static const char * const texts
[3] = {
190 "Cam Mic", "Mic", "Line"
192 return snd_ctl_enum_info(uinfo
, 1, 3, texts
);
195 static int sgio2audio_source_get(struct snd_kcontrol
*kcontrol
,
196 struct snd_ctl_elem_value
*ucontrol
)
198 struct snd_sgio2audio
*chip
= snd_kcontrol_chip(kcontrol
);
200 ucontrol
->value
.enumerated
.item
[0] = ad1843_get_recsrc(&chip
->ad1843
);
204 static int sgio2audio_source_put(struct snd_kcontrol
*kcontrol
,
205 struct snd_ctl_elem_value
*ucontrol
)
207 struct snd_sgio2audio
*chip
= snd_kcontrol_chip(kcontrol
);
210 oldsrc
= ad1843_get_recsrc(&chip
->ad1843
);
211 newsrc
= ad1843_set_recsrc(&chip
->ad1843
,
212 ucontrol
->value
.enumerated
.item
[0]);
214 return newsrc
!= oldsrc
;
217 /* dac1/pcm0 mixer control */
218 static const struct snd_kcontrol_new sgio2audio_ctrl_pcm0
= {
219 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
220 .name
= "PCM Playback Volume",
222 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
223 .private_value
= AD1843_GAIN_PCM_0
,
224 .info
= sgio2audio_gain_info
,
225 .get
= sgio2audio_gain_get
,
226 .put
= sgio2audio_gain_put
,
229 /* dac2/pcm1 mixer control */
230 static const struct snd_kcontrol_new sgio2audio_ctrl_pcm1
= {
231 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
232 .name
= "PCM Playback Volume",
234 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
235 .private_value
= AD1843_GAIN_PCM_1
,
236 .info
= sgio2audio_gain_info
,
237 .get
= sgio2audio_gain_get
,
238 .put
= sgio2audio_gain_put
,
241 /* record level mixer control */
242 static const struct snd_kcontrol_new sgio2audio_ctrl_reclevel
= {
243 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
244 .name
= "Capture Volume",
245 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
246 .private_value
= AD1843_GAIN_RECLEV
,
247 .info
= sgio2audio_gain_info
,
248 .get
= sgio2audio_gain_get
,
249 .put
= sgio2audio_gain_put
,
252 /* record level source control */
253 static const struct snd_kcontrol_new sgio2audio_ctrl_recsource
= {
254 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
255 .name
= "Capture Source",
256 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
257 .info
= sgio2audio_source_info
,
258 .get
= sgio2audio_source_get
,
259 .put
= sgio2audio_source_put
,
262 /* line mixer control */
263 static const struct snd_kcontrol_new sgio2audio_ctrl_line
= {
264 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
265 .name
= "Line Playback Volume",
267 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
268 .private_value
= AD1843_GAIN_LINE
,
269 .info
= sgio2audio_gain_info
,
270 .get
= sgio2audio_gain_get
,
271 .put
= sgio2audio_gain_put
,
274 /* cd mixer control */
275 static const struct snd_kcontrol_new sgio2audio_ctrl_cd
= {
276 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
277 .name
= "Line Playback Volume",
279 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
280 .private_value
= AD1843_GAIN_LINE_2
,
281 .info
= sgio2audio_gain_info
,
282 .get
= sgio2audio_gain_get
,
283 .put
= sgio2audio_gain_put
,
286 /* mic mixer control */
287 static const struct snd_kcontrol_new sgio2audio_ctrl_mic
= {
288 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
289 .name
= "Mic Playback Volume",
290 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
291 .private_value
= AD1843_GAIN_MIC
,
292 .info
= sgio2audio_gain_info
,
293 .get
= sgio2audio_gain_get
,
294 .put
= sgio2audio_gain_put
,
298 static int snd_sgio2audio_new_mixer(struct snd_sgio2audio
*chip
)
302 err
= snd_ctl_add(chip
->card
,
303 snd_ctl_new1(&sgio2audio_ctrl_pcm0
, chip
));
307 err
= snd_ctl_add(chip
->card
,
308 snd_ctl_new1(&sgio2audio_ctrl_pcm1
, chip
));
312 err
= snd_ctl_add(chip
->card
,
313 snd_ctl_new1(&sgio2audio_ctrl_reclevel
, chip
));
317 err
= snd_ctl_add(chip
->card
,
318 snd_ctl_new1(&sgio2audio_ctrl_recsource
, chip
));
321 err
= snd_ctl_add(chip
->card
,
322 snd_ctl_new1(&sgio2audio_ctrl_line
, chip
));
326 err
= snd_ctl_add(chip
->card
,
327 snd_ctl_new1(&sgio2audio_ctrl_cd
, chip
));
331 err
= snd_ctl_add(chip
->card
,
332 snd_ctl_new1(&sgio2audio_ctrl_mic
, chip
));
339 /* low-level audio interface DMA */
341 /* get data out of bounce buffer, count must be a multiple of 32 */
342 /* returns 1 if a period has elapsed */
343 static int snd_sgio2audio_dma_pull_frag(struct snd_sgio2audio
*chip
,
344 unsigned int ch
, unsigned int count
)
347 unsigned long src_base
, src_pos
, dst_mask
;
348 unsigned char *dst_base
;
354 struct snd_pcm_runtime
*runtime
= chip
->channel
[ch
].substream
->runtime
;
356 spin_lock_irqsave(&chip
->channel
[ch
].lock
, flags
);
358 src_base
= (unsigned long) chip
->ring_base
| (ch
<< CHANNEL_RING_SHIFT
);
359 src_pos
= readq(&mace
->perif
.audio
.chan
[ch
].read_ptr
);
360 dst_base
= runtime
->dma_area
;
361 dst_pos
= chip
->channel
[ch
].pos
;
362 dst_mask
= frames_to_bytes(runtime
, runtime
->buffer_size
) - 1;
364 /* check if a period has elapsed */
365 chip
->channel
[ch
].size
+= (count
>> 3); /* in frames */
366 ret
= chip
->channel
[ch
].size
>= runtime
->period_size
;
367 chip
->channel
[ch
].size
%= runtime
->period_size
;
370 src
= (u64
*)(src_base
+ src_pos
);
371 dst
= (s16
*)(dst_base
+ dst_pos
);
374 dst
[0] = (x
>> CHANNEL_LEFT_SHIFT
) & 0xffff;
375 dst
[1] = (x
>> CHANNEL_RIGHT_SHIFT
) & 0xffff;
377 src_pos
= (src_pos
+ sizeof(u64
)) & CHANNEL_RING_MASK
;
378 dst_pos
= (dst_pos
+ 2 * sizeof(s16
)) & dst_mask
;
379 count
-= sizeof(u64
);
382 writeq(src_pos
, &mace
->perif
.audio
.chan
[ch
].read_ptr
); /* in bytes */
383 chip
->channel
[ch
].pos
= dst_pos
;
385 spin_unlock_irqrestore(&chip
->channel
[ch
].lock
, flags
);
389 /* put some DMA data in bounce buffer, count must be a multiple of 32 */
390 /* returns 1 if a period has elapsed */
391 static int snd_sgio2audio_dma_push_frag(struct snd_sgio2audio
*chip
,
392 unsigned int ch
, unsigned int count
)
396 unsigned long dst_base
, dst_pos
, src_mask
;
397 unsigned char *src_base
;
402 struct snd_pcm_runtime
*runtime
= chip
->channel
[ch
].substream
->runtime
;
404 spin_lock_irqsave(&chip
->channel
[ch
].lock
, flags
);
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;
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
;
418 src
= (s16
*)(src_base
+ src_pos
);
419 dst
= (u64
*)(dst_base
+ dst_pos
);
421 l
= src
[0]; /* sign extend */
422 r
= src
[1]; /* sign extend */
424 *dst
= ((l
& 0x00ffffff) << CHANNEL_LEFT_SHIFT
) |
425 ((r
& 0x00ffffff) << CHANNEL_RIGHT_SHIFT
);
427 dst_pos
= (dst_pos
+ sizeof(u64
)) & CHANNEL_RING_MASK
;
428 src_pos
= (src_pos
+ 2 * sizeof(s16
)) & src_mask
;
429 count
-= sizeof(u64
);
432 writeq(dst_pos
, &mace
->perif
.audio
.chan
[ch
].write_ptr
); /* in bytes */
433 chip
->channel
[ch
].pos
= src_pos
;
435 spin_unlock_irqrestore(&chip
->channel
[ch
].lock
, flags
);
439 static int snd_sgio2audio_dma_start(struct snd_pcm_substream
*substream
)
441 struct snd_sgio2audio
*chip
= snd_pcm_substream_chip(substream
);
442 struct snd_sgio2audio_chan
*chan
= substream
->runtime
->private_data
;
445 /* reset DMA channel */
446 writeq(CHANNEL_CONTROL_RESET
, &mace
->perif
.audio
.chan
[ch
].control
);
448 writeq(0, &mace
->perif
.audio
.chan
[ch
].control
);
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);
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
);
460 static int snd_sgio2audio_dma_stop(struct snd_pcm_substream
*substream
)
462 struct snd_sgio2audio_chan
*chan
= substream
->runtime
->private_data
;
464 writeq(0, &mace
->perif
.audio
.chan
[chan
->idx
].control
);
468 static irqreturn_t
snd_sgio2audio_dma_in_isr(int irq
, void *dev_id
)
470 struct snd_sgio2audio_chan
*chan
= dev_id
;
471 struct snd_pcm_substream
*substream
;
472 struct snd_sgio2audio
*chip
;
475 substream
= chan
->substream
;
476 chip
= snd_pcm_substream_chip(substream
);
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
);
488 static irqreturn_t
snd_sgio2audio_dma_out_isr(int irq
, void *dev_id
)
490 struct snd_sgio2audio_chan
*chan
= dev_id
;
491 struct snd_pcm_substream
*substream
;
492 struct snd_sgio2audio
*chip
;
495 substream
= chan
->substream
;
496 chip
= snd_pcm_substream_chip(substream
);
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
);
507 static irqreturn_t
snd_sgio2audio_error_isr(int irq
, void *dev_id
)
509 struct snd_sgio2audio_chan
*chan
= dev_id
;
510 struct snd_pcm_substream
*substream
;
512 substream
= chan
->substream
;
513 snd_sgio2audio_dma_stop(substream
);
514 snd_sgio2audio_dma_start(substream
);
519 /* PCM hardware definition */
520 static 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
,
531 .buffer_bytes_max
= 65536,
532 .period_bytes_min
= 32768,
533 .period_bytes_max
= 65536,
538 /* PCM playback open callback */
539 static int snd_sgio2audio_playback1_open(struct snd_pcm_substream
*substream
)
541 struct snd_sgio2audio
*chip
= snd_pcm_substream_chip(substream
);
542 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
544 runtime
->hw
= snd_sgio2audio_pcm_hw
;
545 runtime
->private_data
= &chip
->channel
[1];
549 static int snd_sgio2audio_playback2_open(struct snd_pcm_substream
*substream
)
551 struct snd_sgio2audio
*chip
= snd_pcm_substream_chip(substream
);
552 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
554 runtime
->hw
= snd_sgio2audio_pcm_hw
;
555 runtime
->private_data
= &chip
->channel
[2];
559 /* PCM capture open callback */
560 static int snd_sgio2audio_capture_open(struct snd_pcm_substream
*substream
)
562 struct snd_sgio2audio
*chip
= snd_pcm_substream_chip(substream
);
563 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
565 runtime
->hw
= snd_sgio2audio_pcm_hw
;
566 runtime
->private_data
= &chip
->channel
[0];
570 /* PCM close callback */
571 static int snd_sgio2audio_pcm_close(struct snd_pcm_substream
*substream
)
573 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
575 runtime
->private_data
= NULL
;
579 /* prepare callback */
580 static int snd_sgio2audio_pcm_prepare(struct snd_pcm_substream
*substream
)
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
;
588 spin_lock_irqsave(&chip
->channel
[ch
].lock
, flags
);
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
;
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
,
602 SNDRV_PCM_FORMAT_S16_LE
,
605 case SNDRV_PCM_STREAM_CAPTURE
:
606 ad1843_setup_adc(&chip
->ad1843
,
608 SNDRV_PCM_FORMAT_S16_LE
,
612 spin_unlock_irqrestore(&chip
->channel
[ch
].lock
, flags
);
616 /* trigger callback */
617 static int snd_sgio2audio_pcm_trigger(struct snd_pcm_substream
*substream
,
621 case SNDRV_PCM_TRIGGER_START
:
622 /* start the PCM engine */
623 snd_sgio2audio_dma_start(substream
);
625 case SNDRV_PCM_TRIGGER_STOP
:
626 /* stop the PCM engine */
627 snd_sgio2audio_dma_stop(substream
);
635 /* pointer callback */
636 static snd_pcm_uframes_t
637 snd_sgio2audio_pcm_pointer(struct snd_pcm_substream
*substream
)
639 struct snd_sgio2audio
*chip
= snd_pcm_substream_chip(substream
);
640 struct snd_sgio2audio_chan
*chan
= substream
->runtime
->private_data
;
642 /* get the current hardware pointer */
643 return bytes_to_frames(substream
->runtime
,
644 chip
->channel
[chan
->idx
].pos
);
648 static 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
,
656 static 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
,
664 static 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
,
673 * definitions of capture are omitted here...
676 /* create a pcm device */
677 static int snd_sgio2audio_new_pcm(struct snd_sgio2audio
*chip
)
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
);
687 pcm
->private_data
= chip
;
688 strcpy(pcm
->name
, "SGI O2 DAC1");
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);
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
);
702 pcm
->private_data
= chip
;
703 strcpy(pcm
->name
, "SGI O2 DAC2");
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);
716 irqreturn_t (*isr
)(int, void *);
718 } snd_sgio2_isr_table
[] = {
721 .irq
= MACEISA_AUDIO1_DMAT_IRQ
,
722 .isr
= snd_sgio2audio_dma_in_isr
,
723 .desc
= "Capture DMA Channel 0"
726 .irq
= MACEISA_AUDIO1_OF_IRQ
,
727 .isr
= snd_sgio2audio_error_isr
,
728 .desc
= "Capture Overflow"
731 .irq
= MACEISA_AUDIO2_DMAT_IRQ
,
732 .isr
= snd_sgio2audio_dma_out_isr
,
733 .desc
= "Playback DMA Channel 1"
736 .irq
= MACEISA_AUDIO2_MERR_IRQ
,
737 .isr
= snd_sgio2audio_error_isr
,
738 .desc
= "Memory Error Channel 1"
741 .irq
= MACEISA_AUDIO3_DMAT_IRQ
,
742 .isr
= snd_sgio2audio_dma_out_isr
,
743 .desc
= "Playback DMA Channel 2"
746 .irq
= MACEISA_AUDIO3_MERR_IRQ
,
747 .isr
= snd_sgio2audio_error_isr
,
748 .desc
= "Memory Error Channel 2"
754 static int snd_sgio2audio_free(struct snd_sgio2audio
*chip
)
758 /* reset interface */
759 writeq(AUDIO_CONTROL_RESET
, &mace
->perif
.audio
.control
);
761 writeq(0, &mace
->perif
.audio
.control
);
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
]);
768 dma_free_coherent(chip
->card
->dev
, MACEISA_RINGBUFFERS_SIZE
,
769 chip
->ring_base
, chip
->ring_base_dma
);
771 /* release card data */
776 static int snd_sgio2audio_dev_free(struct snd_device
*device
)
778 struct snd_sgio2audio
*chip
= device
->device_data
;
780 return snd_sgio2audio_free(chip
);
783 static const struct snd_device_ops ops
= {
784 .dev_free
= snd_sgio2audio_dev_free
,
787 static int snd_sgio2audio_create(struct snd_card
*card
,
788 struct snd_sgio2audio
**rchip
)
790 struct snd_sgio2audio
*chip
;
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
))
800 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
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
) {
811 "sgio2audio: could not allocate ring buffers\n");
816 spin_lock_init(&chip
->ad1843_lock
);
818 /* initialize channels */
819 for (i
= 0; i
< 3; i
++) {
820 spin_lock_init(&chip
->channel
[i
].lock
);
821 chip
->channel
[i
].idx
= i
;
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
,
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
);
838 /* reset the interface */
839 writeq(AUDIO_CONTROL_RESET
, &mace
->perif
.audio
.control
);
841 writeq(0, &mace
->perif
.audio
.control
);
842 msleep_interruptible(1); /* give time to recover */
845 writeq(chip
->ring_base_dma
, &mace
->perif
.ctrl
.ringbase
);
847 /* attach the AD1843 codec */
848 chip
->ad1843
.read
= read_ad1843_reg
;
849 chip
->ad1843
.write
= write_ad1843_reg
;
850 chip
->ad1843
.chip
= chip
;
852 /* initialize the AD1843 codec */
853 err
= ad1843_init(&chip
->ad1843
);
855 snd_sgio2audio_free(chip
);
859 err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, chip
, &ops
);
861 snd_sgio2audio_free(chip
);
868 static int snd_sgio2audio_probe(struct platform_device
*pdev
)
870 struct snd_card
*card
;
871 struct snd_sgio2audio
*chip
;
874 err
= snd_card_new(&pdev
->dev
, index
, id
, THIS_MODULE
, 0, &card
);
878 err
= snd_sgio2audio_create(card
, &chip
);
884 err
= snd_sgio2audio_new_pcm(chip
);
889 err
= snd_sgio2audio_new_mixer(chip
);
895 strcpy(card
->driver
, "SGI O2 Audio");
896 strcpy(card
->shortname
, "SGI O2 Audio");
897 sprintf(card
->longname
, "%s irq %i-%i",
899 MACEISA_AUDIO1_DMAT_IRQ
,
900 MACEISA_AUDIO3_MERR_IRQ
);
902 err
= snd_card_register(card
);
907 platform_set_drvdata(pdev
, card
);
911 static void snd_sgio2audio_remove(struct platform_device
*pdev
)
913 struct snd_card
*card
= platform_get_drvdata(pdev
);
918 static struct platform_driver sgio2audio_driver
= {
919 .probe
= snd_sgio2audio_probe
,
920 .remove
= snd_sgio2audio_remove
,
922 .name
= "sgio2audio",
926 module_platform_driver(sgio2audio_driver
);