2 * Sound driver for Silicon Graphics O2 Workstations A/V board audio.
4 * Copyright 2003 Vivien Chappelier <vivien.chappelier@linux-mips.org>
5 * Copyright 2008 Thomas Bogendoerfer <tsbogend@alpha.franken.de>
6 * Mxier part taken from mace_audio.c:
7 * Copyright 2007 Thorben Jändling <tj.trevelyan@gmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/spinlock.h>
28 #include <linux/interrupt.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/platform_device.h>
32 #include <linux/slab.h>
33 #include <linux/module.h>
35 #include <asm/ip32/ip32_ints.h>
36 #include <asm/ip32/mace.h>
38 #include <sound/core.h>
39 #include <sound/control.h>
40 #include <sound/pcm.h>
42 #include <sound/initval.h>
43 #include <sound/ad1843.h>
46 MODULE_AUTHOR("Vivien Chappelier <vivien.chappelier@linux-mips.org>");
47 MODULE_DESCRIPTION("SGI O2 Audio");
48 MODULE_LICENSE("GPL");
49 MODULE_SUPPORTED_DEVICE("{{Silicon Graphics, O2 Audio}}");
51 static int index
= SNDRV_DEFAULT_IDX1
; /* Index 0-MAX */
52 static char *id
= SNDRV_DEFAULT_STR1
; /* ID for this card */
54 module_param(index
, int, 0444);
55 MODULE_PARM_DESC(index
, "Index value for SGI O2 soundcard.");
56 module_param(id
, charp
, 0444);
57 MODULE_PARM_DESC(id
, "ID string for SGI O2 soundcard.");
60 #define AUDIO_CONTROL_RESET BIT(0) /* 1: reset audio interface */
61 #define AUDIO_CONTROL_CODEC_PRESENT BIT(1) /* 1: codec detected */
63 #define CODEC_CONTROL_WORD_SHIFT 0
64 #define CODEC_CONTROL_READ BIT(16)
65 #define CODEC_CONTROL_ADDRESS_SHIFT 17
67 #define CHANNEL_CONTROL_RESET BIT(10) /* 1: reset channel */
68 #define CHANNEL_DMA_ENABLE BIT(9) /* 1: enable DMA transfer */
69 #define CHANNEL_INT_THRESHOLD_DISABLED (0 << 5) /* interrupt disabled */
70 #define CHANNEL_INT_THRESHOLD_25 (1 << 5) /* int on buffer >25% full */
71 #define CHANNEL_INT_THRESHOLD_50 (2 << 5) /* int on buffer >50% full */
72 #define CHANNEL_INT_THRESHOLD_75 (3 << 5) /* int on buffer >75% full */
73 #define CHANNEL_INT_THRESHOLD_EMPTY (4 << 5) /* int on buffer empty */
74 #define CHANNEL_INT_THRESHOLD_NOT_EMPTY (5 << 5) /* int on buffer !empty */
75 #define CHANNEL_INT_THRESHOLD_FULL (6 << 5) /* int on buffer empty */
76 #define CHANNEL_INT_THRESHOLD_NOT_FULL (7 << 5) /* int on buffer !empty */
78 #define CHANNEL_RING_SHIFT 12
79 #define CHANNEL_RING_SIZE (1 << CHANNEL_RING_SHIFT)
80 #define CHANNEL_RING_MASK (CHANNEL_RING_SIZE - 1)
82 #define CHANNEL_LEFT_SHIFT 40
83 #define CHANNEL_RIGHT_SHIFT 8
85 struct snd_sgio2audio_chan
{
87 struct snd_pcm_substream
*substream
;
89 snd_pcm_uframes_t size
;
93 /* definition of the chip-specific record */
94 struct snd_sgio2audio
{
95 struct snd_card
*card
;
98 struct snd_ad1843 ad1843
;
99 spinlock_t ad1843_lock
;
102 struct snd_sgio2audio_chan channel
[3];
106 dma_addr_t ring_base_dma
;
112 * read_ad1843_reg returns the current contents of a 16 bit AD1843 register.
114 * Returns unsigned register value on success, -errno on failure.
116 static int read_ad1843_reg(void *priv
, int reg
)
118 struct snd_sgio2audio
*chip
= priv
;
122 spin_lock_irqsave(&chip
->ad1843_lock
, flags
);
124 writeq((reg
<< CODEC_CONTROL_ADDRESS_SHIFT
) |
125 CODEC_CONTROL_READ
, &mace
->perif
.audio
.codec_control
);
127 val
= readq(&mace
->perif
.audio
.codec_control
); /* flush bus */
130 val
= readq(&mace
->perif
.audio
.codec_read
);
132 spin_unlock_irqrestore(&chip
->ad1843_lock
, flags
);
137 * write_ad1843_reg writes the specified value to a 16 bit AD1843 register.
139 static int write_ad1843_reg(void *priv
, int reg
, int word
)
141 struct snd_sgio2audio
*chip
= priv
;
145 spin_lock_irqsave(&chip
->ad1843_lock
, flags
);
147 writeq((reg
<< CODEC_CONTROL_ADDRESS_SHIFT
) |
148 (word
<< CODEC_CONTROL_WORD_SHIFT
),
149 &mace
->perif
.audio
.codec_control
);
151 val
= readq(&mace
->perif
.audio
.codec_control
); /* flush bus */
154 spin_unlock_irqrestore(&chip
->ad1843_lock
, flags
);
158 static int sgio2audio_gain_info(struct snd_kcontrol
*kcontrol
,
159 struct snd_ctl_elem_info
*uinfo
)
161 struct snd_sgio2audio
*chip
= snd_kcontrol_chip(kcontrol
);
163 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
165 uinfo
->value
.integer
.min
= 0;
166 uinfo
->value
.integer
.max
= ad1843_get_gain_max(&chip
->ad1843
,
167 (int)kcontrol
->private_value
);
171 static int sgio2audio_gain_get(struct snd_kcontrol
*kcontrol
,
172 struct snd_ctl_elem_value
*ucontrol
)
174 struct snd_sgio2audio
*chip
= snd_kcontrol_chip(kcontrol
);
177 vol
= ad1843_get_gain(&chip
->ad1843
, (int)kcontrol
->private_value
);
179 ucontrol
->value
.integer
.value
[0] = (vol
>> 8) & 0xFF;
180 ucontrol
->value
.integer
.value
[1] = vol
& 0xFF;
185 static int sgio2audio_gain_put(struct snd_kcontrol
*kcontrol
,
186 struct snd_ctl_elem_value
*ucontrol
)
188 struct snd_sgio2audio
*chip
= snd_kcontrol_chip(kcontrol
);
191 oldvol
= ad1843_get_gain(&chip
->ad1843
, kcontrol
->private_value
);
192 newvol
= (ucontrol
->value
.integer
.value
[0] << 8) |
193 ucontrol
->value
.integer
.value
[1];
195 newvol
= ad1843_set_gain(&chip
->ad1843
, kcontrol
->private_value
,
198 return newvol
!= oldvol
;
201 static int sgio2audio_source_info(struct snd_kcontrol
*kcontrol
,
202 struct snd_ctl_elem_info
*uinfo
)
204 static const char * const texts
[3] = {
205 "Cam Mic", "Mic", "Line"
207 return snd_ctl_enum_info(uinfo
, 1, 3, texts
);
210 static int sgio2audio_source_get(struct snd_kcontrol
*kcontrol
,
211 struct snd_ctl_elem_value
*ucontrol
)
213 struct snd_sgio2audio
*chip
= snd_kcontrol_chip(kcontrol
);
215 ucontrol
->value
.enumerated
.item
[0] = ad1843_get_recsrc(&chip
->ad1843
);
219 static int sgio2audio_source_put(struct snd_kcontrol
*kcontrol
,
220 struct snd_ctl_elem_value
*ucontrol
)
222 struct snd_sgio2audio
*chip
= snd_kcontrol_chip(kcontrol
);
225 oldsrc
= ad1843_get_recsrc(&chip
->ad1843
);
226 newsrc
= ad1843_set_recsrc(&chip
->ad1843
,
227 ucontrol
->value
.enumerated
.item
[0]);
229 return newsrc
!= oldsrc
;
232 /* dac1/pcm0 mixer control */
233 static const struct snd_kcontrol_new sgio2audio_ctrl_pcm0
= {
234 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
235 .name
= "PCM Playback Volume",
237 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
238 .private_value
= AD1843_GAIN_PCM_0
,
239 .info
= sgio2audio_gain_info
,
240 .get
= sgio2audio_gain_get
,
241 .put
= sgio2audio_gain_put
,
244 /* dac2/pcm1 mixer control */
245 static const struct snd_kcontrol_new sgio2audio_ctrl_pcm1
= {
246 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
247 .name
= "PCM Playback Volume",
249 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
250 .private_value
= AD1843_GAIN_PCM_1
,
251 .info
= sgio2audio_gain_info
,
252 .get
= sgio2audio_gain_get
,
253 .put
= sgio2audio_gain_put
,
256 /* record level mixer control */
257 static const struct snd_kcontrol_new sgio2audio_ctrl_reclevel
= {
258 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
259 .name
= "Capture Volume",
260 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
261 .private_value
= AD1843_GAIN_RECLEV
,
262 .info
= sgio2audio_gain_info
,
263 .get
= sgio2audio_gain_get
,
264 .put
= sgio2audio_gain_put
,
267 /* record level source control */
268 static const struct snd_kcontrol_new sgio2audio_ctrl_recsource
= {
269 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
270 .name
= "Capture Source",
271 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
272 .info
= sgio2audio_source_info
,
273 .get
= sgio2audio_source_get
,
274 .put
= sgio2audio_source_put
,
277 /* line mixer control */
278 static const struct snd_kcontrol_new sgio2audio_ctrl_line
= {
279 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
280 .name
= "Line Playback Volume",
282 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
283 .private_value
= AD1843_GAIN_LINE
,
284 .info
= sgio2audio_gain_info
,
285 .get
= sgio2audio_gain_get
,
286 .put
= sgio2audio_gain_put
,
289 /* cd mixer control */
290 static const struct snd_kcontrol_new sgio2audio_ctrl_cd
= {
291 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
292 .name
= "Line Playback Volume",
294 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
295 .private_value
= AD1843_GAIN_LINE_2
,
296 .info
= sgio2audio_gain_info
,
297 .get
= sgio2audio_gain_get
,
298 .put
= sgio2audio_gain_put
,
301 /* mic mixer control */
302 static const struct snd_kcontrol_new sgio2audio_ctrl_mic
= {
303 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
304 .name
= "Mic Playback Volume",
305 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
306 .private_value
= AD1843_GAIN_MIC
,
307 .info
= sgio2audio_gain_info
,
308 .get
= sgio2audio_gain_get
,
309 .put
= sgio2audio_gain_put
,
313 static int snd_sgio2audio_new_mixer(struct snd_sgio2audio
*chip
)
317 err
= snd_ctl_add(chip
->card
,
318 snd_ctl_new1(&sgio2audio_ctrl_pcm0
, chip
));
322 err
= snd_ctl_add(chip
->card
,
323 snd_ctl_new1(&sgio2audio_ctrl_pcm1
, chip
));
327 err
= snd_ctl_add(chip
->card
,
328 snd_ctl_new1(&sgio2audio_ctrl_reclevel
, chip
));
332 err
= snd_ctl_add(chip
->card
,
333 snd_ctl_new1(&sgio2audio_ctrl_recsource
, chip
));
336 err
= snd_ctl_add(chip
->card
,
337 snd_ctl_new1(&sgio2audio_ctrl_line
, chip
));
341 err
= snd_ctl_add(chip
->card
,
342 snd_ctl_new1(&sgio2audio_ctrl_cd
, chip
));
346 err
= snd_ctl_add(chip
->card
,
347 snd_ctl_new1(&sgio2audio_ctrl_mic
, chip
));
354 /* low-level audio interface DMA */
356 /* get data out of bounce buffer, count must be a multiple of 32 */
357 /* returns 1 if a period has elapsed */
358 static int snd_sgio2audio_dma_pull_frag(struct snd_sgio2audio
*chip
,
359 unsigned int ch
, unsigned int count
)
362 unsigned long src_base
, src_pos
, dst_mask
;
363 unsigned char *dst_base
;
369 struct snd_pcm_runtime
*runtime
= chip
->channel
[ch
].substream
->runtime
;
371 spin_lock_irqsave(&chip
->channel
[ch
].lock
, flags
);
373 src_base
= (unsigned long) chip
->ring_base
| (ch
<< CHANNEL_RING_SHIFT
);
374 src_pos
= readq(&mace
->perif
.audio
.chan
[ch
].read_ptr
);
375 dst_base
= runtime
->dma_area
;
376 dst_pos
= chip
->channel
[ch
].pos
;
377 dst_mask
= frames_to_bytes(runtime
, runtime
->buffer_size
) - 1;
379 /* check if a period has elapsed */
380 chip
->channel
[ch
].size
+= (count
>> 3); /* in frames */
381 ret
= chip
->channel
[ch
].size
>= runtime
->period_size
;
382 chip
->channel
[ch
].size
%= runtime
->period_size
;
385 src
= (u64
*)(src_base
+ src_pos
);
386 dst
= (s16
*)(dst_base
+ dst_pos
);
389 dst
[0] = (x
>> CHANNEL_LEFT_SHIFT
) & 0xffff;
390 dst
[1] = (x
>> CHANNEL_RIGHT_SHIFT
) & 0xffff;
392 src_pos
= (src_pos
+ sizeof(u64
)) & CHANNEL_RING_MASK
;
393 dst_pos
= (dst_pos
+ 2 * sizeof(s16
)) & dst_mask
;
394 count
-= sizeof(u64
);
397 writeq(src_pos
, &mace
->perif
.audio
.chan
[ch
].read_ptr
); /* in bytes */
398 chip
->channel
[ch
].pos
= dst_pos
;
400 spin_unlock_irqrestore(&chip
->channel
[ch
].lock
, flags
);
404 /* put some DMA data in bounce buffer, count must be a multiple of 32 */
405 /* returns 1 if a period has elapsed */
406 static int snd_sgio2audio_dma_push_frag(struct snd_sgio2audio
*chip
,
407 unsigned int ch
, unsigned int count
)
411 unsigned long dst_base
, dst_pos
, src_mask
;
412 unsigned char *src_base
;
417 struct snd_pcm_runtime
*runtime
= chip
->channel
[ch
].substream
->runtime
;
419 spin_lock_irqsave(&chip
->channel
[ch
].lock
, flags
);
421 dst_base
= (unsigned long)chip
->ring_base
| (ch
<< CHANNEL_RING_SHIFT
);
422 dst_pos
= readq(&mace
->perif
.audio
.chan
[ch
].write_ptr
);
423 src_base
= runtime
->dma_area
;
424 src_pos
= chip
->channel
[ch
].pos
;
425 src_mask
= frames_to_bytes(runtime
, runtime
->buffer_size
) - 1;
427 /* check if a period has elapsed */
428 chip
->channel
[ch
].size
+= (count
>> 3); /* in frames */
429 ret
= chip
->channel
[ch
].size
>= runtime
->period_size
;
430 chip
->channel
[ch
].size
%= runtime
->period_size
;
433 src
= (s16
*)(src_base
+ src_pos
);
434 dst
= (u64
*)(dst_base
+ dst_pos
);
436 l
= src
[0]; /* sign extend */
437 r
= src
[1]; /* sign extend */
439 *dst
= ((l
& 0x00ffffff) << CHANNEL_LEFT_SHIFT
) |
440 ((r
& 0x00ffffff) << CHANNEL_RIGHT_SHIFT
);
442 dst_pos
= (dst_pos
+ sizeof(u64
)) & CHANNEL_RING_MASK
;
443 src_pos
= (src_pos
+ 2 * sizeof(s16
)) & src_mask
;
444 count
-= sizeof(u64
);
447 writeq(dst_pos
, &mace
->perif
.audio
.chan
[ch
].write_ptr
); /* in bytes */
448 chip
->channel
[ch
].pos
= src_pos
;
450 spin_unlock_irqrestore(&chip
->channel
[ch
].lock
, flags
);
454 static int snd_sgio2audio_dma_start(struct snd_pcm_substream
*substream
)
456 struct snd_sgio2audio
*chip
= snd_pcm_substream_chip(substream
);
457 struct snd_sgio2audio_chan
*chan
= substream
->runtime
->private_data
;
460 /* reset DMA channel */
461 writeq(CHANNEL_CONTROL_RESET
, &mace
->perif
.audio
.chan
[ch
].control
);
463 writeq(0, &mace
->perif
.audio
.chan
[ch
].control
);
465 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
466 /* push a full buffer */
467 snd_sgio2audio_dma_push_frag(chip
, ch
, CHANNEL_RING_SIZE
- 32);
469 /* set DMA to wake on 50% empty and enable interrupt */
470 writeq(CHANNEL_DMA_ENABLE
| CHANNEL_INT_THRESHOLD_50
,
471 &mace
->perif
.audio
.chan
[ch
].control
);
475 static int snd_sgio2audio_dma_stop(struct snd_pcm_substream
*substream
)
477 struct snd_sgio2audio_chan
*chan
= substream
->runtime
->private_data
;
479 writeq(0, &mace
->perif
.audio
.chan
[chan
->idx
].control
);
483 static irqreturn_t
snd_sgio2audio_dma_in_isr(int irq
, void *dev_id
)
485 struct snd_sgio2audio_chan
*chan
= dev_id
;
486 struct snd_pcm_substream
*substream
;
487 struct snd_sgio2audio
*chip
;
490 substream
= chan
->substream
;
491 chip
= snd_pcm_substream_chip(substream
);
495 count
= CHANNEL_RING_SIZE
-
496 readq(&mace
->perif
.audio
.chan
[ch
].depth
) - 32;
497 if (snd_sgio2audio_dma_pull_frag(chip
, ch
, count
))
498 snd_pcm_period_elapsed(substream
);
503 static irqreturn_t
snd_sgio2audio_dma_out_isr(int irq
, void *dev_id
)
505 struct snd_sgio2audio_chan
*chan
= dev_id
;
506 struct snd_pcm_substream
*substream
;
507 struct snd_sgio2audio
*chip
;
510 substream
= chan
->substream
;
511 chip
= snd_pcm_substream_chip(substream
);
514 count
= CHANNEL_RING_SIZE
-
515 readq(&mace
->perif
.audio
.chan
[ch
].depth
) - 32;
516 if (snd_sgio2audio_dma_push_frag(chip
, ch
, count
))
517 snd_pcm_period_elapsed(substream
);
522 static irqreturn_t
snd_sgio2audio_error_isr(int irq
, void *dev_id
)
524 struct snd_sgio2audio_chan
*chan
= dev_id
;
525 struct snd_pcm_substream
*substream
;
527 substream
= chan
->substream
;
528 snd_sgio2audio_dma_stop(substream
);
529 snd_sgio2audio_dma_start(substream
);
534 /* PCM hardware definition */
535 static const struct snd_pcm_hardware snd_sgio2audio_pcm_hw
= {
536 .info
= (SNDRV_PCM_INFO_MMAP
|
537 SNDRV_PCM_INFO_MMAP_VALID
|
538 SNDRV_PCM_INFO_INTERLEAVED
|
539 SNDRV_PCM_INFO_BLOCK_TRANSFER
),
540 .formats
= SNDRV_PCM_FMTBIT_S16_BE
,
541 .rates
= SNDRV_PCM_RATE_8000_48000
,
546 .buffer_bytes_max
= 65536,
547 .period_bytes_min
= 32768,
548 .period_bytes_max
= 65536,
553 /* PCM playback open callback */
554 static int snd_sgio2audio_playback1_open(struct snd_pcm_substream
*substream
)
556 struct snd_sgio2audio
*chip
= snd_pcm_substream_chip(substream
);
557 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
559 runtime
->hw
= snd_sgio2audio_pcm_hw
;
560 runtime
->private_data
= &chip
->channel
[1];
564 static int snd_sgio2audio_playback2_open(struct snd_pcm_substream
*substream
)
566 struct snd_sgio2audio
*chip
= snd_pcm_substream_chip(substream
);
567 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
569 runtime
->hw
= snd_sgio2audio_pcm_hw
;
570 runtime
->private_data
= &chip
->channel
[2];
574 /* PCM capture open callback */
575 static int snd_sgio2audio_capture_open(struct snd_pcm_substream
*substream
)
577 struct snd_sgio2audio
*chip
= snd_pcm_substream_chip(substream
);
578 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
580 runtime
->hw
= snd_sgio2audio_pcm_hw
;
581 runtime
->private_data
= &chip
->channel
[0];
585 /* PCM close callback */
586 static int snd_sgio2audio_pcm_close(struct snd_pcm_substream
*substream
)
588 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
590 runtime
->private_data
= NULL
;
595 /* hw_params callback */
596 static int snd_sgio2audio_pcm_hw_params(struct snd_pcm_substream
*substream
,
597 struct snd_pcm_hw_params
*hw_params
)
599 return snd_pcm_lib_alloc_vmalloc_buffer(substream
,
600 params_buffer_bytes(hw_params
));
603 /* hw_free callback */
604 static int snd_sgio2audio_pcm_hw_free(struct snd_pcm_substream
*substream
)
606 return snd_pcm_lib_free_vmalloc_buffer(substream
);
609 /* prepare callback */
610 static int snd_sgio2audio_pcm_prepare(struct snd_pcm_substream
*substream
)
612 struct snd_sgio2audio
*chip
= snd_pcm_substream_chip(substream
);
613 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
614 struct snd_sgio2audio_chan
*chan
= substream
->runtime
->private_data
;
618 spin_lock_irqsave(&chip
->channel
[ch
].lock
, flags
);
620 /* Setup the pseudo-dma transfer pointers. */
621 chip
->channel
[ch
].pos
= 0;
622 chip
->channel
[ch
].size
= 0;
623 chip
->channel
[ch
].substream
= substream
;
625 /* set AD1843 format */
626 /* hardware format is always S16_LE */
627 switch (substream
->stream
) {
628 case SNDRV_PCM_STREAM_PLAYBACK
:
629 ad1843_setup_dac(&chip
->ad1843
,
632 SNDRV_PCM_FORMAT_S16_LE
,
635 case SNDRV_PCM_STREAM_CAPTURE
:
636 ad1843_setup_adc(&chip
->ad1843
,
638 SNDRV_PCM_FORMAT_S16_LE
,
642 spin_unlock_irqrestore(&chip
->channel
[ch
].lock
, flags
);
646 /* trigger callback */
647 static int snd_sgio2audio_pcm_trigger(struct snd_pcm_substream
*substream
,
651 case SNDRV_PCM_TRIGGER_START
:
652 /* start the PCM engine */
653 snd_sgio2audio_dma_start(substream
);
655 case SNDRV_PCM_TRIGGER_STOP
:
656 /* stop the PCM engine */
657 snd_sgio2audio_dma_stop(substream
);
665 /* pointer callback */
666 static snd_pcm_uframes_t
667 snd_sgio2audio_pcm_pointer(struct snd_pcm_substream
*substream
)
669 struct snd_sgio2audio
*chip
= snd_pcm_substream_chip(substream
);
670 struct snd_sgio2audio_chan
*chan
= substream
->runtime
->private_data
;
672 /* get the current hardware pointer */
673 return bytes_to_frames(substream
->runtime
,
674 chip
->channel
[chan
->idx
].pos
);
678 static const struct snd_pcm_ops snd_sgio2audio_playback1_ops
= {
679 .open
= snd_sgio2audio_playback1_open
,
680 .close
= snd_sgio2audio_pcm_close
,
681 .ioctl
= snd_pcm_lib_ioctl
,
682 .hw_params
= snd_sgio2audio_pcm_hw_params
,
683 .hw_free
= snd_sgio2audio_pcm_hw_free
,
684 .prepare
= snd_sgio2audio_pcm_prepare
,
685 .trigger
= snd_sgio2audio_pcm_trigger
,
686 .pointer
= snd_sgio2audio_pcm_pointer
,
687 .page
= snd_pcm_lib_get_vmalloc_page
,
690 static const struct snd_pcm_ops snd_sgio2audio_playback2_ops
= {
691 .open
= snd_sgio2audio_playback2_open
,
692 .close
= snd_sgio2audio_pcm_close
,
693 .ioctl
= snd_pcm_lib_ioctl
,
694 .hw_params
= snd_sgio2audio_pcm_hw_params
,
695 .hw_free
= snd_sgio2audio_pcm_hw_free
,
696 .prepare
= snd_sgio2audio_pcm_prepare
,
697 .trigger
= snd_sgio2audio_pcm_trigger
,
698 .pointer
= snd_sgio2audio_pcm_pointer
,
699 .page
= snd_pcm_lib_get_vmalloc_page
,
702 static const struct snd_pcm_ops snd_sgio2audio_capture_ops
= {
703 .open
= snd_sgio2audio_capture_open
,
704 .close
= snd_sgio2audio_pcm_close
,
705 .ioctl
= snd_pcm_lib_ioctl
,
706 .hw_params
= snd_sgio2audio_pcm_hw_params
,
707 .hw_free
= snd_sgio2audio_pcm_hw_free
,
708 .prepare
= snd_sgio2audio_pcm_prepare
,
709 .trigger
= snd_sgio2audio_pcm_trigger
,
710 .pointer
= snd_sgio2audio_pcm_pointer
,
711 .page
= snd_pcm_lib_get_vmalloc_page
,
715 * definitions of capture are omitted here...
718 /* create a pcm device */
719 static int snd_sgio2audio_new_pcm(struct snd_sgio2audio
*chip
)
724 /* create first pcm device with one outputs and one input */
725 err
= snd_pcm_new(chip
->card
, "SGI O2 Audio", 0, 1, 1, &pcm
);
729 pcm
->private_data
= chip
;
730 strcpy(pcm
->name
, "SGI O2 DAC1");
733 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
734 &snd_sgio2audio_playback1_ops
);
735 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
,
736 &snd_sgio2audio_capture_ops
);
738 /* create second pcm device with one outputs and no input */
739 err
= snd_pcm_new(chip
->card
, "SGI O2 Audio", 1, 1, 0, &pcm
);
743 pcm
->private_data
= chip
;
744 strcpy(pcm
->name
, "SGI O2 DAC2");
747 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
748 &snd_sgio2audio_playback2_ops
);
756 irqreturn_t (*isr
)(int, void *);
758 } snd_sgio2_isr_table
[] = {
761 .irq
= MACEISA_AUDIO1_DMAT_IRQ
,
762 .isr
= snd_sgio2audio_dma_in_isr
,
763 .desc
= "Capture DMA Channel 0"
766 .irq
= MACEISA_AUDIO1_OF_IRQ
,
767 .isr
= snd_sgio2audio_error_isr
,
768 .desc
= "Capture Overflow"
771 .irq
= MACEISA_AUDIO2_DMAT_IRQ
,
772 .isr
= snd_sgio2audio_dma_out_isr
,
773 .desc
= "Playback DMA Channel 1"
776 .irq
= MACEISA_AUDIO2_MERR_IRQ
,
777 .isr
= snd_sgio2audio_error_isr
,
778 .desc
= "Memory Error Channel 1"
781 .irq
= MACEISA_AUDIO3_DMAT_IRQ
,
782 .isr
= snd_sgio2audio_dma_out_isr
,
783 .desc
= "Playback DMA Channel 2"
786 .irq
= MACEISA_AUDIO3_MERR_IRQ
,
787 .isr
= snd_sgio2audio_error_isr
,
788 .desc
= "Memory Error Channel 2"
794 static int snd_sgio2audio_free(struct snd_sgio2audio
*chip
)
798 /* reset interface */
799 writeq(AUDIO_CONTROL_RESET
, &mace
->perif
.audio
.control
);
801 writeq(0, &mace
->perif
.audio
.control
);
804 for (i
= 0; i
< ARRAY_SIZE(snd_sgio2_isr_table
); i
++)
805 free_irq(snd_sgio2_isr_table
[i
].irq
,
806 &chip
->channel
[snd_sgio2_isr_table
[i
].idx
]);
808 dma_free_coherent(NULL
, MACEISA_RINGBUFFERS_SIZE
,
809 chip
->ring_base
, chip
->ring_base_dma
);
811 /* release card data */
816 static int snd_sgio2audio_dev_free(struct snd_device
*device
)
818 struct snd_sgio2audio
*chip
= device
->device_data
;
820 return snd_sgio2audio_free(chip
);
823 static struct snd_device_ops ops
= {
824 .dev_free
= snd_sgio2audio_dev_free
,
827 static int snd_sgio2audio_create(struct snd_card
*card
,
828 struct snd_sgio2audio
**rchip
)
830 struct snd_sgio2audio
*chip
;
835 /* check if a codec is attached to the interface */
836 /* (Audio or Audio/Video board present) */
837 if (!(readq(&mace
->perif
.audio
.control
) & AUDIO_CONTROL_CODEC_PRESENT
))
840 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
846 chip
->ring_base
= dma_alloc_coherent(NULL
, MACEISA_RINGBUFFERS_SIZE
,
847 &chip
->ring_base_dma
, GFP_USER
);
848 if (chip
->ring_base
== NULL
) {
850 "sgio2audio: could not allocate ring buffers\n");
855 spin_lock_init(&chip
->ad1843_lock
);
857 /* initialize channels */
858 for (i
= 0; i
< 3; i
++) {
859 spin_lock_init(&chip
->channel
[i
].lock
);
860 chip
->channel
[i
].idx
= i
;
864 for (i
= 0; i
< ARRAY_SIZE(snd_sgio2_isr_table
); i
++) {
865 if (request_irq(snd_sgio2_isr_table
[i
].irq
,
866 snd_sgio2_isr_table
[i
].isr
,
868 snd_sgio2_isr_table
[i
].desc
,
869 &chip
->channel
[snd_sgio2_isr_table
[i
].idx
])) {
870 snd_sgio2audio_free(chip
);
871 printk(KERN_ERR
"sgio2audio: cannot allocate irq %d\n",
872 snd_sgio2_isr_table
[i
].irq
);
877 /* reset the interface */
878 writeq(AUDIO_CONTROL_RESET
, &mace
->perif
.audio
.control
);
880 writeq(0, &mace
->perif
.audio
.control
);
881 msleep_interruptible(1); /* give time to recover */
884 writeq(chip
->ring_base_dma
, &mace
->perif
.ctrl
.ringbase
);
886 /* attach the AD1843 codec */
887 chip
->ad1843
.read
= read_ad1843_reg
;
888 chip
->ad1843
.write
= write_ad1843_reg
;
889 chip
->ad1843
.chip
= chip
;
891 /* initialize the AD1843 codec */
892 err
= ad1843_init(&chip
->ad1843
);
894 snd_sgio2audio_free(chip
);
898 err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, chip
, &ops
);
900 snd_sgio2audio_free(chip
);
907 static int snd_sgio2audio_probe(struct platform_device
*pdev
)
909 struct snd_card
*card
;
910 struct snd_sgio2audio
*chip
;
913 err
= snd_card_new(&pdev
->dev
, index
, id
, THIS_MODULE
, 0, &card
);
917 err
= snd_sgio2audio_create(card
, &chip
);
923 err
= snd_sgio2audio_new_pcm(chip
);
928 err
= snd_sgio2audio_new_mixer(chip
);
934 strcpy(card
->driver
, "SGI O2 Audio");
935 strcpy(card
->shortname
, "SGI O2 Audio");
936 sprintf(card
->longname
, "%s irq %i-%i",
938 MACEISA_AUDIO1_DMAT_IRQ
,
939 MACEISA_AUDIO3_MERR_IRQ
);
941 err
= snd_card_register(card
);
946 platform_set_drvdata(pdev
, card
);
950 static int snd_sgio2audio_remove(struct platform_device
*pdev
)
952 struct snd_card
*card
= platform_get_drvdata(pdev
);
958 static struct platform_driver sgio2audio_driver
= {
959 .probe
= snd_sgio2audio_probe
,
960 .remove
= snd_sgio2audio_remove
,
962 .name
= "sgio2audio",
966 module_platform_driver(sgio2audio_driver
);