1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/spinlock.h>
28#include <linux/gfp.h>
29#include <linux/vmalloc.h>
30#include <linux/interrupt.h>
31#include <linux/dma-mapping.h>
32#include <linux/platform_device.h>
33#include <linux/io.h>
34
35#include <asm/ip32/ip32_ints.h>
36#include <asm/ip32/mace.h>
37
38#include <sound/core.h>
39#include <sound/control.h>
40#include <sound/pcm.h>
41#define SNDRV_GET_ID
42#include <sound/initval.h>
43#include <sound/ad1843.h>
44
45
46MODULE_AUTHOR("Vivien Chappelier <vivien.chappelier@linux-mips.org>");
47MODULE_DESCRIPTION("SGI O2 Audio");
48MODULE_LICENSE("GPL");
49MODULE_SUPPORTED_DEVICE("{{Silicon Graphics, O2 Audio}}");
50
51static int index = SNDRV_DEFAULT_IDX1;
52static char *id = SNDRV_DEFAULT_STR1;
53
54module_param(index, int, 0444);
55MODULE_PARM_DESC(index, "Index value for SGI O2 soundcard.");
56module_param(id, charp, 0444);
57MODULE_PARM_DESC(id, "ID string for SGI O2 soundcard.");
58
59
60#define AUDIO_CONTROL_RESET BIT(0)
61#define AUDIO_CONTROL_CODEC_PRESENT BIT(1)
62
63#define CODEC_CONTROL_WORD_SHIFT 0
64#define CODEC_CONTROL_READ BIT(16)
65#define CODEC_CONTROL_ADDRESS_SHIFT 17
66
67#define CHANNEL_CONTROL_RESET BIT(10)
68#define CHANNEL_DMA_ENABLE BIT(9)
69#define CHANNEL_INT_THRESHOLD_DISABLED (0 << 5)
70#define CHANNEL_INT_THRESHOLD_25 (1 << 5)
71#define CHANNEL_INT_THRESHOLD_50 (2 << 5)
72#define CHANNEL_INT_THRESHOLD_75 (3 << 5)
73#define CHANNEL_INT_THRESHOLD_EMPTY (4 << 5)
74#define CHANNEL_INT_THRESHOLD_NOT_EMPTY (5 << 5)
75#define CHANNEL_INT_THRESHOLD_FULL (6 << 5)
76#define CHANNEL_INT_THRESHOLD_NOT_FULL (7 << 5)
77
78#define CHANNEL_RING_SHIFT 12
79#define CHANNEL_RING_SIZE (1 << CHANNEL_RING_SHIFT)
80#define CHANNEL_RING_MASK (CHANNEL_RING_SIZE - 1)
81
82#define CHANNEL_LEFT_SHIFT 40
83#define CHANNEL_RIGHT_SHIFT 8
84
85struct snd_sgio2audio_chan {
86 int idx;
87 struct snd_pcm_substream *substream;
88 int pos;
89 snd_pcm_uframes_t size;
90 spinlock_t lock;
91};
92
93
94struct snd_sgio2audio {
95 struct snd_card *card;
96
97
98 struct snd_ad1843 ad1843;
99 spinlock_t ad1843_lock;
100
101
102 struct snd_sgio2audio_chan channel[3];
103
104
105 void *ring_base;
106 dma_addr_t ring_base_dma;
107};
108
109
110
111
112
113
114
115
116static int read_ad1843_reg(void *priv, int reg)
117{
118 struct snd_sgio2audio *chip = priv;
119 int val;
120 unsigned long flags;
121
122 spin_lock_irqsave(&chip->ad1843_lock, flags);
123
124 writeq((reg << CODEC_CONTROL_ADDRESS_SHIFT) |
125 CODEC_CONTROL_READ, &mace->perif.audio.codec_control);
126 wmb();
127 val = readq(&mace->perif.audio.codec_control);
128 udelay(200);
129
130 val = readq(&mace->perif.audio.codec_read);
131
132 spin_unlock_irqrestore(&chip->ad1843_lock, flags);
133 return val;
134}
135
136
137
138
139static int write_ad1843_reg(void *priv, int reg, int word)
140{
141 struct snd_sgio2audio *chip = priv;
142 int val;
143 unsigned long flags;
144
145 spin_lock_irqsave(&chip->ad1843_lock, flags);
146
147 writeq((reg << CODEC_CONTROL_ADDRESS_SHIFT) |
148 (word << CODEC_CONTROL_WORD_SHIFT),
149 &mace->perif.audio.codec_control);
150 wmb();
151 val = readq(&mace->perif.audio.codec_control);
152 udelay(200);
153
154 spin_unlock_irqrestore(&chip->ad1843_lock, flags);
155 return 0;
156}
157
158static int sgio2audio_gain_info(struct snd_kcontrol *kcontrol,
159 struct snd_ctl_elem_info *uinfo)
160{
161 struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
162
163 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
164 uinfo->count = 2;
165 uinfo->value.integer.min = 0;
166 uinfo->value.integer.max = ad1843_get_gain_max(&chip->ad1843,
167 (int)kcontrol->private_value);
168 return 0;
169}
170
171static int sgio2audio_gain_get(struct snd_kcontrol *kcontrol,
172 struct snd_ctl_elem_value *ucontrol)
173{
174 struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
175 int vol;
176
177 vol = ad1843_get_gain(&chip->ad1843, (int)kcontrol->private_value);
178
179 ucontrol->value.integer.value[0] = (vol >> 8) & 0xFF;
180 ucontrol->value.integer.value[1] = vol & 0xFF;
181
182 return 0;
183}
184
185static int sgio2audio_gain_put(struct snd_kcontrol *kcontrol,
186 struct snd_ctl_elem_value *ucontrol)
187{
188 struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
189 int newvol, oldvol;
190
191 oldvol = ad1843_get_gain(&chip->ad1843, kcontrol->private_value);
192 newvol = (ucontrol->value.integer.value[0] << 8) |
193 ucontrol->value.integer.value[1];
194
195 newvol = ad1843_set_gain(&chip->ad1843, kcontrol->private_value,
196 newvol);
197
198 return newvol != oldvol;
199}
200
201static int sgio2audio_source_info(struct snd_kcontrol *kcontrol,
202 struct snd_ctl_elem_info *uinfo)
203{
204 static const char *texts[3] = {
205 "Cam Mic", "Mic", "Line"
206 };
207 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
208 uinfo->count = 1;
209 uinfo->value.enumerated.items = 3;
210 if (uinfo->value.enumerated.item >= 3)
211 uinfo->value.enumerated.item = 1;
212 strcpy(uinfo->value.enumerated.name,
213 texts[uinfo->value.enumerated.item]);
214 return 0;
215}
216
217static int sgio2audio_source_get(struct snd_kcontrol *kcontrol,
218 struct snd_ctl_elem_value *ucontrol)
219{
220 struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
221
222 ucontrol->value.enumerated.item[0] = ad1843_get_recsrc(&chip->ad1843);
223 return 0;
224}
225
226static int sgio2audio_source_put(struct snd_kcontrol *kcontrol,
227 struct snd_ctl_elem_value *ucontrol)
228{
229 struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
230 int newsrc, oldsrc;
231
232 oldsrc = ad1843_get_recsrc(&chip->ad1843);
233 newsrc = ad1843_set_recsrc(&chip->ad1843,
234 ucontrol->value.enumerated.item[0]);
235
236 return newsrc != oldsrc;
237}
238
239
240static struct snd_kcontrol_new sgio2audio_ctrl_pcm0 __devinitdata = {
241 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
242 .name = "PCM Playback Volume",
243 .index = 0,
244 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
245 .private_value = AD1843_GAIN_PCM_0,
246 .info = sgio2audio_gain_info,
247 .get = sgio2audio_gain_get,
248 .put = sgio2audio_gain_put,
249};
250
251
252static struct snd_kcontrol_new sgio2audio_ctrl_pcm1 __devinitdata = {
253 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
254 .name = "PCM Playback Volume",
255 .index = 1,
256 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
257 .private_value = AD1843_GAIN_PCM_1,
258 .info = sgio2audio_gain_info,
259 .get = sgio2audio_gain_get,
260 .put = sgio2audio_gain_put,
261};
262
263
264static struct snd_kcontrol_new sgio2audio_ctrl_reclevel __devinitdata = {
265 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
266 .name = "Capture Volume",
267 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
268 .private_value = AD1843_GAIN_RECLEV,
269 .info = sgio2audio_gain_info,
270 .get = sgio2audio_gain_get,
271 .put = sgio2audio_gain_put,
272};
273
274
275static struct snd_kcontrol_new sgio2audio_ctrl_recsource __devinitdata = {
276 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
277 .name = "Capture Source",
278 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
279 .info = sgio2audio_source_info,
280 .get = sgio2audio_source_get,
281 .put = sgio2audio_source_put,
282};
283
284
285static struct snd_kcontrol_new sgio2audio_ctrl_line __devinitdata = {
286 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
287 .name = "Line Playback Volume",
288 .index = 0,
289 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
290 .private_value = AD1843_GAIN_LINE,
291 .info = sgio2audio_gain_info,
292 .get = sgio2audio_gain_get,
293 .put = sgio2audio_gain_put,
294};
295
296
297static struct snd_kcontrol_new sgio2audio_ctrl_cd __devinitdata = {
298 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
299 .name = "Line Playback Volume",
300 .index = 1,
301 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
302 .private_value = AD1843_GAIN_LINE_2,
303 .info = sgio2audio_gain_info,
304 .get = sgio2audio_gain_get,
305 .put = sgio2audio_gain_put,
306};
307
308
309static struct snd_kcontrol_new sgio2audio_ctrl_mic __devinitdata = {
310 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
311 .name = "Mic Playback Volume",
312 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
313 .private_value = AD1843_GAIN_MIC,
314 .info = sgio2audio_gain_info,
315 .get = sgio2audio_gain_get,
316 .put = sgio2audio_gain_put,
317};
318
319
320static int __devinit snd_sgio2audio_new_mixer(struct snd_sgio2audio *chip)
321{
322 int err;
323
324 err = snd_ctl_add(chip->card,
325 snd_ctl_new1(&sgio2audio_ctrl_pcm0, chip));
326 if (err < 0)
327 return err;
328
329 err = snd_ctl_add(chip->card,
330 snd_ctl_new1(&sgio2audio_ctrl_pcm1, chip));
331 if (err < 0)
332 return err;
333
334 err = snd_ctl_add(chip->card,
335 snd_ctl_new1(&sgio2audio_ctrl_reclevel, chip));
336 if (err < 0)
337 return err;
338
339 err = snd_ctl_add(chip->card,
340 snd_ctl_new1(&sgio2audio_ctrl_recsource, chip));
341 if (err < 0)
342 return err;
343 err = snd_ctl_add(chip->card,
344 snd_ctl_new1(&sgio2audio_ctrl_line, chip));
345 if (err < 0)
346 return err;
347
348 err = snd_ctl_add(chip->card,
349 snd_ctl_new1(&sgio2audio_ctrl_cd, chip));
350 if (err < 0)
351 return err;
352
353 err = snd_ctl_add(chip->card,
354 snd_ctl_new1(&sgio2audio_ctrl_mic, chip));
355 if (err < 0)
356 return err;
357
358 return 0;
359}
360
361
362
363
364
365static int snd_sgio2audio_dma_pull_frag(struct snd_sgio2audio *chip,
366 unsigned int ch, unsigned int count)
367{
368 int ret;
369 unsigned long src_base, src_pos, dst_mask;
370 unsigned char *dst_base;
371 int dst_pos;
372 u64 *src;
373 s16 *dst;
374 u64 x;
375 unsigned long flags;
376 struct snd_pcm_runtime *runtime = chip->channel[ch].substream->runtime;
377
378 spin_lock_irqsave(&chip->channel[ch].lock, flags);
379
380 src_base = (unsigned long) chip->ring_base | (ch << CHANNEL_RING_SHIFT);
381 src_pos = readq(&mace->perif.audio.chan[ch].read_ptr);
382 dst_base = runtime->dma_area;
383 dst_pos = chip->channel[ch].pos;
384 dst_mask = frames_to_bytes(runtime, runtime->buffer_size) - 1;
385
386
387 chip->channel[ch].size += (count >> 3);
388 ret = chip->channel[ch].size >= runtime->period_size;
389 chip->channel[ch].size %= runtime->period_size;
390
391 while (count) {
392 src = (u64 *)(src_base + src_pos);
393 dst = (s16 *)(dst_base + dst_pos);
394
395 x = *src;
396 dst[0] = (x >> CHANNEL_LEFT_SHIFT) & 0xffff;
397 dst[1] = (x >> CHANNEL_RIGHT_SHIFT) & 0xffff;
398
399 src_pos = (src_pos + sizeof(u64)) & CHANNEL_RING_MASK;
400 dst_pos = (dst_pos + 2 * sizeof(s16)) & dst_mask;
401 count -= sizeof(u64);
402 }
403
404 writeq(src_pos, &mace->perif.audio.chan[ch].read_ptr);
405 chip->channel[ch].pos = dst_pos;
406
407 spin_unlock_irqrestore(&chip->channel[ch].lock, flags);
408 return ret;
409}
410
411
412
413static int snd_sgio2audio_dma_push_frag(struct snd_sgio2audio *chip,
414 unsigned int ch, unsigned int count)
415{
416 int ret;
417 s64 l, r;
418 unsigned long dst_base, dst_pos, src_mask;
419 unsigned char *src_base;
420 int src_pos;
421 u64 *dst;
422 s16 *src;
423 unsigned long flags;
424 struct snd_pcm_runtime *runtime = chip->channel[ch].substream->runtime;
425
426 spin_lock_irqsave(&chip->channel[ch].lock, flags);
427
428 dst_base = (unsigned long)chip->ring_base | (ch << CHANNEL_RING_SHIFT);
429 dst_pos = readq(&mace->perif.audio.chan[ch].write_ptr);
430 src_base = runtime->dma_area;
431 src_pos = chip->channel[ch].pos;
432 src_mask = frames_to_bytes(runtime, runtime->buffer_size) - 1;
433
434
435 chip->channel[ch].size += (count >> 3);
436 ret = chip->channel[ch].size >= runtime->period_size;
437 chip->channel[ch].size %= runtime->period_size;
438
439 while (count) {
440 src = (s16 *)(src_base + src_pos);
441 dst = (u64 *)(dst_base + dst_pos);
442
443 l = src[0];
444 r = src[1];
445
446 *dst = ((l & 0x00ffffff) << CHANNEL_LEFT_SHIFT) |
447 ((r & 0x00ffffff) << CHANNEL_RIGHT_SHIFT);
448
449 dst_pos = (dst_pos + sizeof(u64)) & CHANNEL_RING_MASK;
450 src_pos = (src_pos + 2 * sizeof(s16)) & src_mask;
451 count -= sizeof(u64);
452 }
453
454 writeq(dst_pos, &mace->perif.audio.chan[ch].write_ptr);
455 chip->channel[ch].pos = src_pos;
456
457 spin_unlock_irqrestore(&chip->channel[ch].lock, flags);
458 return ret;
459}
460
461static int snd_sgio2audio_dma_start(struct snd_pcm_substream *substream)
462{
463 struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
464 struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
465 int ch = chan->idx;
466
467
468 writeq(CHANNEL_CONTROL_RESET, &mace->perif.audio.chan[ch].control);
469 udelay(10);
470 writeq(0, &mace->perif.audio.chan[ch].control);
471
472 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
473
474 snd_sgio2audio_dma_push_frag(chip, ch, CHANNEL_RING_SIZE - 32);
475 }
476
477 writeq(CHANNEL_DMA_ENABLE | CHANNEL_INT_THRESHOLD_50,
478 &mace->perif.audio.chan[ch].control);
479 return 0;
480}
481
482static int snd_sgio2audio_dma_stop(struct snd_pcm_substream *substream)
483{
484 struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
485
486 writeq(0, &mace->perif.audio.chan[chan->idx].control);
487 return 0;
488}
489
490static irqreturn_t snd_sgio2audio_dma_in_isr(int irq, void *dev_id)
491{
492 struct snd_sgio2audio_chan *chan = dev_id;
493 struct snd_pcm_substream *substream;
494 struct snd_sgio2audio *chip;
495 int count, ch;
496
497 substream = chan->substream;
498 chip = snd_pcm_substream_chip(substream);
499 ch = chan->idx;
500
501
502 count = CHANNEL_RING_SIZE -
503 readq(&mace->perif.audio.chan[ch].depth) - 32;
504 if (snd_sgio2audio_dma_pull_frag(chip, ch, count))
505 snd_pcm_period_elapsed(substream);
506
507 return IRQ_HANDLED;
508}
509
510static irqreturn_t snd_sgio2audio_dma_out_isr(int irq, void *dev_id)
511{
512 struct snd_sgio2audio_chan *chan = dev_id;
513 struct snd_pcm_substream *substream;
514 struct snd_sgio2audio *chip;
515 int count, ch;
516
517 substream = chan->substream;
518 chip = snd_pcm_substream_chip(substream);
519 ch = chan->idx;
520
521 count = CHANNEL_RING_SIZE -
522 readq(&mace->perif.audio.chan[ch].depth) - 32;
523 if (snd_sgio2audio_dma_push_frag(chip, ch, count))
524 snd_pcm_period_elapsed(substream);
525
526 return IRQ_HANDLED;
527}
528
529static irqreturn_t snd_sgio2audio_error_isr(int irq, void *dev_id)
530{
531 struct snd_sgio2audio_chan *chan = dev_id;
532 struct snd_pcm_substream *substream;
533
534 substream = chan->substream;
535 snd_sgio2audio_dma_stop(substream);
536 snd_sgio2audio_dma_start(substream);
537 return IRQ_HANDLED;
538}
539
540
541
542static struct snd_pcm_hardware snd_sgio2audio_pcm_hw = {
543 .info = (SNDRV_PCM_INFO_MMAP |
544 SNDRV_PCM_INFO_MMAP_VALID |
545 SNDRV_PCM_INFO_INTERLEAVED |
546 SNDRV_PCM_INFO_BLOCK_TRANSFER),
547 .formats = SNDRV_PCM_FMTBIT_S16_BE,
548 .rates = SNDRV_PCM_RATE_8000_48000,
549 .rate_min = 8000,
550 .rate_max = 48000,
551 .channels_min = 2,
552 .channels_max = 2,
553 .buffer_bytes_max = 65536,
554 .period_bytes_min = 32768,
555 .period_bytes_max = 65536,
556 .periods_min = 1,
557 .periods_max = 1024,
558};
559
560
561static int snd_sgio2audio_playback1_open(struct snd_pcm_substream *substream)
562{
563 struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
564 struct snd_pcm_runtime *runtime = substream->runtime;
565
566 runtime->hw = snd_sgio2audio_pcm_hw;
567 runtime->private_data = &chip->channel[1];
568 return 0;
569}
570
571static int snd_sgio2audio_playback2_open(struct snd_pcm_substream *substream)
572{
573 struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
574 struct snd_pcm_runtime *runtime = substream->runtime;
575
576 runtime->hw = snd_sgio2audio_pcm_hw;
577 runtime->private_data = &chip->channel[2];
578 return 0;
579}
580
581
582static int snd_sgio2audio_capture_open(struct snd_pcm_substream *substream)
583{
584 struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
585 struct snd_pcm_runtime *runtime = substream->runtime;
586
587 runtime->hw = snd_sgio2audio_pcm_hw;
588 runtime->private_data = &chip->channel[0];
589 return 0;
590}
591
592
593static int snd_sgio2audio_pcm_close(struct snd_pcm_substream *substream)
594{
595 struct snd_pcm_runtime *runtime = substream->runtime;
596
597 runtime->private_data = NULL;
598 return 0;
599}
600
601
602
603static int snd_sgio2audio_pcm_hw_params(struct snd_pcm_substream *substream,
604 struct snd_pcm_hw_params *hw_params)
605{
606 struct snd_pcm_runtime *runtime = substream->runtime;
607 int size = params_buffer_bytes(hw_params);
608
609
610 if (runtime->dma_area)
611 vfree(runtime->dma_area);
612 runtime->dma_area = vmalloc(size);
613 if (runtime->dma_area == NULL)
614 return -ENOMEM;
615 runtime->dma_bytes = size;
616 return 0;
617}
618
619
620static int snd_sgio2audio_pcm_hw_free(struct snd_pcm_substream *substream)
621{
622 vfree(substream->runtime->dma_area);
623 substream->runtime->dma_area = NULL;
624 return 0;
625}
626
627
628static int snd_sgio2audio_pcm_prepare(struct snd_pcm_substream *substream)
629{
630 struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
631 struct snd_pcm_runtime *runtime = substream->runtime;
632 struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
633 int ch = chan->idx;
634 unsigned long flags;
635
636 spin_lock_irqsave(&chip->channel[ch].lock, flags);
637
638
639 chip->channel[ch].pos = 0;
640 chip->channel[ch].size = 0;
641 chip->channel[ch].substream = substream;
642
643
644
645 switch (substream->stream) {
646 case SNDRV_PCM_STREAM_PLAYBACK:
647 ad1843_setup_dac(&chip->ad1843,
648 ch - 1,
649 runtime->rate,
650 SNDRV_PCM_FORMAT_S16_LE,
651 runtime->channels);
652 break;
653 case SNDRV_PCM_STREAM_CAPTURE:
654 ad1843_setup_adc(&chip->ad1843,
655 runtime->rate,
656 SNDRV_PCM_FORMAT_S16_LE,
657 runtime->channels);
658 break;
659 }
660 spin_unlock_irqrestore(&chip->channel[ch].lock, flags);
661 return 0;
662}
663
664
665static int snd_sgio2audio_pcm_trigger(struct snd_pcm_substream *substream,
666 int cmd)
667{
668 switch (cmd) {
669 case SNDRV_PCM_TRIGGER_START:
670
671 snd_sgio2audio_dma_start(substream);
672 break;
673 case SNDRV_PCM_TRIGGER_STOP:
674
675 snd_sgio2audio_dma_stop(substream);
676 break;
677 default:
678 return -EINVAL;
679 }
680 return 0;
681}
682
683
684static snd_pcm_uframes_t
685snd_sgio2audio_pcm_pointer(struct snd_pcm_substream *substream)
686{
687 struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
688 struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
689
690
691 return bytes_to_frames(substream->runtime,
692 chip->channel[chan->idx].pos);
693}
694
695
696static struct page *snd_sgio2audio_page(struct snd_pcm_substream *substream,
697 unsigned long offset)
698{
699 return vmalloc_to_page(substream->runtime->dma_area + offset);
700}
701
702
703static struct snd_pcm_ops snd_sgio2audio_playback1_ops = {
704 .open = snd_sgio2audio_playback1_open,
705 .close = snd_sgio2audio_pcm_close,
706 .ioctl = snd_pcm_lib_ioctl,
707 .hw_params = snd_sgio2audio_pcm_hw_params,
708 .hw_free = snd_sgio2audio_pcm_hw_free,
709 .prepare = snd_sgio2audio_pcm_prepare,
710 .trigger = snd_sgio2audio_pcm_trigger,
711 .pointer = snd_sgio2audio_pcm_pointer,
712 .page = snd_sgio2audio_page,
713};
714
715static struct snd_pcm_ops snd_sgio2audio_playback2_ops = {
716 .open = snd_sgio2audio_playback2_open,
717 .close = snd_sgio2audio_pcm_close,
718 .ioctl = snd_pcm_lib_ioctl,
719 .hw_params = snd_sgio2audio_pcm_hw_params,
720 .hw_free = snd_sgio2audio_pcm_hw_free,
721 .prepare = snd_sgio2audio_pcm_prepare,
722 .trigger = snd_sgio2audio_pcm_trigger,
723 .pointer = snd_sgio2audio_pcm_pointer,
724 .page = snd_sgio2audio_page,
725};
726
727static struct snd_pcm_ops snd_sgio2audio_capture_ops = {
728 .open = snd_sgio2audio_capture_open,
729 .close = snd_sgio2audio_pcm_close,
730 .ioctl = snd_pcm_lib_ioctl,
731 .hw_params = snd_sgio2audio_pcm_hw_params,
732 .hw_free = snd_sgio2audio_pcm_hw_free,
733 .prepare = snd_sgio2audio_pcm_prepare,
734 .trigger = snd_sgio2audio_pcm_trigger,
735 .pointer = snd_sgio2audio_pcm_pointer,
736 .page = snd_sgio2audio_page,
737};
738
739
740
741
742
743
744static int __devinit snd_sgio2audio_new_pcm(struct snd_sgio2audio *chip)
745{
746 struct snd_pcm *pcm;
747 int err;
748
749
750 err = snd_pcm_new(chip->card, "SGI O2 Audio", 0, 1, 1, &pcm);
751 if (err < 0)
752 return err;
753
754 pcm->private_data = chip;
755 strcpy(pcm->name, "SGI O2 DAC1");
756
757
758 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
759 &snd_sgio2audio_playback1_ops);
760 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
761 &snd_sgio2audio_capture_ops);
762
763
764 err = snd_pcm_new(chip->card, "SGI O2 Audio", 1, 1, 0, &pcm);
765 if (err < 0)
766 return err;
767
768 pcm->private_data = chip;
769 strcpy(pcm->name, "SGI O2 DAC2");
770
771
772 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
773 &snd_sgio2audio_playback2_ops);
774
775 return 0;
776}
777
778static struct {
779 int idx;
780 int irq;
781 irqreturn_t (*isr)(int, void *);
782 const char *desc;
783} snd_sgio2_isr_table[] = {
784 {
785 .idx = 0,
786 .irq = MACEISA_AUDIO1_DMAT_IRQ,
787 .isr = snd_sgio2audio_dma_in_isr,
788 .desc = "Capture DMA Channel 0"
789 }, {
790 .idx = 0,
791 .irq = MACEISA_AUDIO1_OF_IRQ,
792 .isr = snd_sgio2audio_error_isr,
793 .desc = "Capture Overflow"
794 }, {
795 .idx = 1,
796 .irq = MACEISA_AUDIO2_DMAT_IRQ,
797 .isr = snd_sgio2audio_dma_out_isr,
798 .desc = "Playback DMA Channel 1"
799 }, {
800 .idx = 1,
801 .irq = MACEISA_AUDIO2_MERR_IRQ,
802 .isr = snd_sgio2audio_error_isr,
803 .desc = "Memory Error Channel 1"
804 }, {
805 .idx = 2,
806 .irq = MACEISA_AUDIO3_DMAT_IRQ,
807 .isr = snd_sgio2audio_dma_out_isr,
808 .desc = "Playback DMA Channel 2"
809 }, {
810 .idx = 2,
811 .irq = MACEISA_AUDIO3_MERR_IRQ,
812 .isr = snd_sgio2audio_error_isr,
813 .desc = "Memory Error Channel 2"
814 }
815};
816
817
818
819static int snd_sgio2audio_free(struct snd_sgio2audio *chip)
820{
821 int i;
822
823
824 writeq(AUDIO_CONTROL_RESET, &mace->perif.audio.control);
825 udelay(1);
826 writeq(0, &mace->perif.audio.control);
827
828
829 for (i = 0; i < ARRAY_SIZE(snd_sgio2_isr_table); i++)
830 free_irq(snd_sgio2_isr_table[i].irq,
831 &chip->channel[snd_sgio2_isr_table[i].idx]);
832
833 dma_free_coherent(NULL, MACEISA_RINGBUFFERS_SIZE,
834 chip->ring_base, chip->ring_base_dma);
835
836
837 kfree(chip);
838 return 0;
839}
840
841static int snd_sgio2audio_dev_free(struct snd_device *device)
842{
843 struct snd_sgio2audio *chip = device->device_data;
844
845 return snd_sgio2audio_free(chip);
846}
847
848static struct snd_device_ops ops = {
849 .dev_free = snd_sgio2audio_dev_free,
850};
851
852static int __devinit snd_sgio2audio_create(struct snd_card *card,
853 struct snd_sgio2audio **rchip)
854{
855 struct snd_sgio2audio *chip;
856 int i, err;
857
858 *rchip = NULL;
859
860
861
862 if (!(readq(&mace->perif.audio.control) & AUDIO_CONTROL_CODEC_PRESENT))
863 return -ENOENT;
864
865 chip = kzalloc(sizeof(struct snd_sgio2audio), GFP_KERNEL);
866 if (chip == NULL)
867 return -ENOMEM;
868
869 chip->card = card;
870
871 chip->ring_base = dma_alloc_coherent(NULL, MACEISA_RINGBUFFERS_SIZE,
872 &chip->ring_base_dma, GFP_USER);
873 if (chip->ring_base == NULL) {
874 printk(KERN_ERR
875 "sgio2audio: could not allocate ring buffers\n");
876 kfree(chip);
877 return -ENOMEM;
878 }
879
880 spin_lock_init(&chip->ad1843_lock);
881
882
883 for (i = 0; i < 3; i++) {
884 spin_lock_init(&chip->channel[i].lock);
885 chip->channel[i].idx = i;
886 }
887
888
889 for (i = 0; i < ARRAY_SIZE(snd_sgio2_isr_table); i++) {
890 if (request_irq(snd_sgio2_isr_table[i].irq,
891 snd_sgio2_isr_table[i].isr,
892 0,
893 snd_sgio2_isr_table[i].desc,
894 &chip->channel[snd_sgio2_isr_table[i].idx])) {
895 snd_sgio2audio_free(chip);
896 printk(KERN_ERR "sgio2audio: cannot allocate irq %d\n",
897 snd_sgio2_isr_table[i].irq);
898 return -EBUSY;
899 }
900 }
901
902
903 writeq(AUDIO_CONTROL_RESET, &mace->perif.audio.control);
904 udelay(1);
905 writeq(0, &mace->perif.audio.control);
906 msleep_interruptible(1);
907
908
909 writeq(chip->ring_base_dma, &mace->perif.ctrl.ringbase);
910
911
912 chip->ad1843.read = read_ad1843_reg;
913 chip->ad1843.write = write_ad1843_reg;
914 chip->ad1843.chip = chip;
915
916
917 err = ad1843_init(&chip->ad1843);
918 if (err < 0) {
919 snd_sgio2audio_free(chip);
920 return err;
921 }
922
923 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
924 if (err < 0) {
925 snd_sgio2audio_free(chip);
926 return err;
927 }
928 *rchip = chip;
929 return 0;
930}
931
932static int __devinit snd_sgio2audio_probe(struct platform_device *pdev)
933{
934 struct snd_card *card;
935 struct snd_sgio2audio *chip;
936 int err;
937
938 err = snd_card_create(index, id, THIS_MODULE, 0, &card);
939 if (err < 0)
940 return err;
941
942 err = snd_sgio2audio_create(card, &chip);
943 if (err < 0) {
944 snd_card_free(card);
945 return err;
946 }
947 snd_card_set_dev(card, &pdev->dev);
948
949 err = snd_sgio2audio_new_pcm(chip);
950 if (err < 0) {
951 snd_card_free(card);
952 return err;
953 }
954 err = snd_sgio2audio_new_mixer(chip);
955 if (err < 0) {
956 snd_card_free(card);
957 return err;
958 }
959
960 strcpy(card->driver, "SGI O2 Audio");
961 strcpy(card->shortname, "SGI O2 Audio");
962 sprintf(card->longname, "%s irq %i-%i",
963 card->shortname,
964 MACEISA_AUDIO1_DMAT_IRQ,
965 MACEISA_AUDIO3_MERR_IRQ);
966
967 err = snd_card_register(card);
968 if (err < 0) {
969 snd_card_free(card);
970 return err;
971 }
972 platform_set_drvdata(pdev, card);
973 return 0;
974}
975
976static int __devexit snd_sgio2audio_remove(struct platform_device *pdev)
977{
978 struct snd_card *card = platform_get_drvdata(pdev);
979
980 snd_card_free(card);
981 platform_set_drvdata(pdev, NULL);
982 return 0;
983}
984
985static struct platform_driver sgio2audio_driver = {
986 .probe = snd_sgio2audio_probe,
987 .remove = __devexit_p(snd_sgio2audio_remove),
988 .driver = {
989 .name = "sgio2audio",
990 .owner = THIS_MODULE,
991 }
992};
993
994static int __init alsa_card_sgio2audio_init(void)
995{
996 return platform_driver_register(&sgio2audio_driver);
997}
998
999static void __exit alsa_card_sgio2audio_exit(void)
1000{
1001 platform_driver_unregister(&sgio2audio_driver);
1002}
1003
1004module_init(alsa_card_sgio2audio_init)
1005module_exit(alsa_card_sgio2audio_exit)
1006