aboutsummaryrefslogblamecommitdiffstats
path: root/sound/parisc/harmony.c
blob: 8b3ea26469adcdcfd19ddb071a51a55e480781cc (plain) (tree)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854





















































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                               
                                  


                            
                                                                    

                                                                  
                                         



















                                                                    

                                            














































































































                                                              
/* Hewlett-Packard Harmony audio driver
 *
 *   This is a driver for the Harmony audio chipset found
 *   on the LASI ASIC of various early HP PA-RISC workstations.
 *
 *   Copyright (C) 2004, Kyle McMartin <kyle@{debian.org,parisc-linux.org}>
 *
 *     Based on the previous Harmony incarnations by,
 *       Copyright 2000 (c) Linuxcare Canada, Alex deVries
 *       Copyright 2000-2003 (c) Helge Deller
 *       Copyright 2001 (c) Matthieu Delahaye
 *       Copyright 2001 (c) Jean-Christophe Vaugeois
 *       Copyright 2003 (c) Laurent Canet
 *       Copyright 2004 (c) Stuart Brady
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License, version 2, as
 *   published by the Free Software Foundation.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Notes:
 *   - graveyard and silence buffers last for lifetime of
 *     the driver. playback and capture buffers are allocated
 *     per _open()/_close().
 * 
 * TODO:
 *
 */

#include <linux/init.h>
#include <linux/slab.h>
#include <linux/time.h>
#include <linux/wait.h>
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/spinlock.h>
#include <linux/dma-mapping.h>

#include <sound/driver.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/control.h>
#include <sound/rawmidi.h>
#include <sound/initval.h>
#include <sound/info.h>

#include <asm/io.h>
#include <asm/hardware.h>
#include <asm/parisc-device.h>

#include "harmony.h"

static struct parisc_device_id snd_harmony_devtable[] = {
	/* bushmaster / flounder */
	{ HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007A }, 
	/* 712 / 715 */
	{ HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007B }, 
	/* pace */
	{ HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007E }, 
	/* outfield / coral II */
	{ HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007F },
	{ 0, }
};

MODULE_DEVICE_TABLE(parisc, snd_harmony_devtable);

#define NAME "harmony"
#define PFX  NAME ": "

static unsigned int snd_harmony_rates[] = {
	5512, 6615, 8000, 9600,
	11025, 16000, 18900, 22050,
	27428, 32000, 33075, 37800,
	44100, 48000
};

static unsigned int rate_bits[14] = {
	HARMONY_SR_5KHZ, HARMONY_SR_6KHZ, HARMONY_SR_8KHZ,
	HARMONY_SR_9KHZ, HARMONY_SR_11KHZ, HARMONY_SR_16KHZ,
	HARMONY_SR_18KHZ, HARMONY_SR_22KHZ, HARMONY_SR_27KHZ,
	HARMONY_SR_32KHZ, HARMONY_SR_33KHZ, HARMONY_SR_37KHZ,
	HARMONY_SR_44KHZ, HARMONY_SR_48KHZ
};

static snd_pcm_hw_constraint_list_t hw_constraint_rates = {
	.count = ARRAY_SIZE(snd_harmony_rates),
	.list = snd_harmony_rates,
	.mask = 0,
};

inline unsigned long
harmony_read(harmony_t *h, unsigned r)
{
	return __raw_readl(h->iobase + r);
}

inline void
harmony_write(harmony_t *h, unsigned r, unsigned long v)
{
	__raw_writel(v, h->iobase + r);
}

static void
harmony_wait_for_control(harmony_t *h)
{
	while (harmony_read(h, HARMONY_CNTL) & HARMONY_CNTL_C) ;
}

inline void
harmony_reset(harmony_t *h)
{
	harmony_write(h, HARMONY_RESET, 1);
	mdelay(50);
	harmony_write(h, HARMONY_RESET, 0);
}

static void
harmony_disable_interrupts(harmony_t *h)
{
	u32 dstatus;
	harmony_wait_for_control(h);
	dstatus = harmony_read(h, HARMONY_DSTATUS);
	dstatus &= ~HARMONY_DSTATUS_IE;
	harmony_write(h, HARMONY_DSTATUS, dstatus);
}

static void
harmony_enable_interrupts(harmony_t *h)
{
	u32 dstatus;
	harmony_wait_for_control(h);
	dstatus = harmony_read(h, HARMONY_DSTATUS);
	dstatus |= HARMONY_DSTATUS_IE;
	harmony_write(h, HARMONY_DSTATUS, dstatus);
}

static void
harmony_mute(harmony_t *h)
{
	unsigned long flags;

	spin_lock_irqsave(&h->mixer_lock, flags);
	harmony_wait_for_control(h);
	harmony_write(h, HARMONY_GAINCTL, HARMONY_GAIN_SILENCE);
	spin_unlock_irqrestore(&h->mixer_lock, flags);
}

static void
harmony_unmute(harmony_t *h)
{
	unsigned long flags;

	spin_lock_irqsave(&h->mixer_lock, flags);
	harmony_wait_for_control(h);
	harmony_write(h, HARMONY_GAINCTL, h->st.gain);
	spin_unlock_irqrestore(&h->mixer_lock, flags);
}

static void
harmony_set_control(harmony_t *h)
{
	u32 ctrl;
	unsigned long flags;

	spin_lock_irqsave(&h->lock, flags);

	ctrl = (HARMONY_CNTL_C      |
		(h->st.format << 6) |
		(h->st.stereo << 5) |
		(h->st.rate));

	harmony_wait_for_control(h);
	harmony_write(h, HARMONY_CNTL, ctrl);

	spin_unlock_irqrestore(&h->lock, flags);
}

static irqreturn_t
snd_harmony_interrupt(int irq, void *dev, struct pt_regs *regs)
{
	u32 dstatus;
	harmony_t *h = dev;

	spin_lock(&h->lock);
	harmony_disable_interrupts(h);
	harmony_wait_for_control(h);
	dstatus = harmony_read(h, HARMONY_DSTATUS);
	spin_unlock(&h->lock);

	if (dstatus & HARMONY_DSTATUS_PN) {
		if (h->psubs) {
			spin_lock(&h->lock);
			h->pbuf.buf += h->pbuf.count; /* PAGE_SIZE */
			h->pbuf.buf %= h->pbuf.size; /* MAX_BUFS*PAGE_SIZE */

			harmony_write(h, HARMONY_PNXTADD, 
				      h->pbuf.addr + h->pbuf.buf);
			h->stats.play_intr++;
			spin_unlock(&h->lock);
                        snd_pcm_period_elapsed(h->psubs);
		} else {
			spin_lock(&h->lock);
			harmony_write(h, HARMONY_PNXTADD, h->sdma.addr);
			h->stats.silence_intr++;
			spin_unlock(&h->lock);
		}
	}

	if (dstatus & HARMONY_DSTATUS_RN) {
		if (h->csubs) {
			spin_lock(&h->lock);
			h->cbuf.buf += h->cbuf.count;
			h->cbuf.buf %= h->cbuf.size;

			harmony_write(h, HARMONY_RNXTADD,
				      h->cbuf.addr + h->cbuf.buf);
			h->stats.rec_intr++;
			spin_unlock(&h->lock);
                        snd_pcm_period_elapsed(h->csubs);
		} else {
			spin_lock(&h->lock);
			harmony_write(h, HARMONY_RNXTADD, h->gdma.addr);
			h->stats.graveyard_intr++;
			spin_unlock(&h->lock);
		}
	}

	spin_lock(&h->lock);
	harmony_enable_interrupts(h);
	spin_unlock(&h->lock);

	return IRQ_HANDLED;
}

static unsigned int 
snd_harmony_rate_bits(int rate)
{
	unsigned int i;
	
	for (i = 0; i < ARRAY_SIZE(snd_harmony_rates); i++)
		if (snd_harmony_rates[i] == rate)
			return rate_bits[i];

	return HARMONY_SR_44KHZ;
}

static snd_pcm_hardware_t snd_harmony_playback =
{
	.info =	(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | 
		 SNDRV_PCM_INFO_JOINT_DUPLEX | SNDRV_PCM_INFO_MMAP_VALID |
		 SNDRV_PCM_INFO_BLOCK_TRANSFER),
	.formats = (SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_MU_LAW |
		    SNDRV_PCM_FMTBIT_A_LAW),
	.rates = (SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000 |
		  SNDRV_PCM_RATE_KNOT),
	.rate_min = 5512,
	.rate_max = 48000,
	.channels_min =	1,
	.channels_max =	2,
	.buffer_bytes_max = MAX_BUF_SIZE,
	.period_bytes_min = BUF_SIZE,
	.period_bytes_max = BUF_SIZE,
	.periods_min = 1,
	.periods_max = MAX_BUFS,
	.fifo_size = 0,
};

static snd_pcm_hardware_t snd_harmony_capture =
{
        .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
                 SNDRV_PCM_INFO_JOINT_DUPLEX | SNDRV_PCM_INFO_MMAP_VALID |
                 SNDRV_PCM_INFO_BLOCK_TRANSFER),
        .formats = (SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_MU_LAW |
                    SNDRV_PCM_FMTBIT_A_LAW),
        .rates = (SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000 |
		  SNDRV_PCM_RATE_KNOT),
        .rate_min = 5512,
        .rate_max = 48000,
        .channels_min = 1,
        .channels_max = 2,
        .buffer_bytes_max = MAX_BUF_SIZE,
        .period_bytes_min = BUF_SIZE,
        .period_bytes_max = BUF_SIZE,
        .periods_min = 1,
        .periods_max = MAX_BUFS,
        .fifo_size = 0,
};

static int
snd_harmony_playback_trigger(snd_pcm_substream_t *ss, int cmd)
{
	harmony_t *h = snd_pcm_substream_chip(ss);
	unsigned long flags;

	if (h->st.capturing)
		return -EBUSY;

	spin_lock_irqsave(&h->lock, flags);
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
		h->st.playing = 1;
		harmony_write(h, HARMONY_PNXTADD, h->pbuf.addr);
		harmony_write(h, HARMONY_RNXTADD, h->gdma.addr);
		harmony_unmute(h);
		harmony_enable_interrupts(h);
		break;
	case SNDRV_PCM_TRIGGER_STOP:
		h->st.playing = 0;
		harmony_mute(h);
		harmony_disable_interrupts(h);
		break;
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
	case SNDRV_PCM_TRIGGER_SUSPEND:
	default:
		spin_unlock_irqrestore(&h->lock, flags);
		snd_BUG();
		return -EINVAL;
	}
	spin_unlock_irqrestore(&h->lock, flags);
	
	return 0;
}

static int
snd_harmony_capture_trigger(snd_pcm_substream_t *ss, int cmd)
{
        harmony_t *h = snd_pcm_substream_chip(ss);
	unsigned long flags;

	if (h->st.playing)
		return -EBUSY;

	spin_lock_irqsave(&h->lock, flags);
        switch (cmd) {
        case SNDRV_PCM_TRIGGER_START:
		h->st.capturing = 1;
                harmony_write(h, HARMONY_PNXTADD, h->sdma.addr);
                harmony_write(h, HARMONY_RNXTADD, h->cbuf.addr);
		harmony_unmute(h);
                harmony_enable_interrupts(h);
		break;
        case SNDRV_PCM_TRIGGER_STOP:
		h->st.capturing = 0;
                harmony_mute(h);
                harmony_disable_interrupts(h);
		break;
        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
        case SNDRV_PCM_TRIGGER_SUSPEND:
	default:
		spin_unlock_irqrestore(&h->lock, flags);
		snd_BUG();
                return -EINVAL;
        }
	spin_unlock_irqrestore(&h->lock, flags);
		
        return 0;
}

static int
snd_harmony_set_data_format(harmony_t *h, int fmt, int force)
{
	int o = h->st.format;
	int n;

	switch(fmt) {
	case SNDRV_PCM_FORMAT_S16_BE:
		n = HARMONY_DF_16BIT_LINEAR;
		break;
	case SNDRV_PCM_FORMAT_A_LAW:
		n = HARMONY_DF_8BIT_ALAW;
		break;
	case SNDRV_PCM_FORMAT_MU_LAW:
		n = HARMONY_DF_8BIT_ULAW;
		break;
	default:
		n = HARMONY_DF_16BIT_LINEAR;
		break;
	}

	if (force || o != n) {
		snd_pcm_format_set_silence(fmt, h->sdma.area, SILENCE_BUFSZ / 
					   (snd_pcm_format_physical_width(fmt)
					    / 8));
	}

	return n;
}

static int
snd_harmony_playback_prepare(snd_pcm_substream_t *ss)
{
	harmony_t *h = snd_pcm_substream_chip(ss);
	snd_pcm_runtime_t *rt = ss->runtime;
	
	if (h->st.capturing)
		return -EBUSY;
	
	h->pbuf.size = snd_pcm_lib_buffer_bytes(ss);
	h->pbuf.count = snd_pcm_lib_period_bytes(ss);
	h->pbuf.buf = 0;
	h->st.playing = 0;

	h->st.rate = snd_harmony_rate_bits(rt->rate);
	h->st.format = snd_harmony_set_data_format(h, rt->format, 0);
	
	if (rt->channels == 2)
		h->st.stereo = HARMONY_SS_STEREO;
	else
		h->st.stereo = HARMONY_SS_MONO;

	harmony_set_control(h);

	h->pbuf.addr = rt->dma_addr;

	return 0;
}

static int
snd_harmony_capture_prepare(snd_pcm_substream_t *ss)
{
        harmony_t *h = snd_pcm_substream_chip(ss);
        snd_pcm_runtime_t *rt = ss->runtime;

	if (h->st.playing)
		return -EBUSY;

        h->cbuf.size = snd_pcm_lib_buffer_bytes(ss);
        h->cbuf.count = snd_pcm_lib_period_bytes(ss);
        h->cbuf.buf = 0;
	h->st.capturing = 0;

        h->st.rate = snd_harmony_rate_bits(rt->rate);
        h->st.format = snd_harmony_set_data_format(h, rt->format, 0);

        if (rt->channels == 2)
                h->st.stereo = HARMONY_SS_STEREO;
        else
                h->st.stereo = HARMONY_SS_MONO;

        harmony_set_control(h);

        h->cbuf.addr = rt->dma_addr;

        return 0;
}

static snd_pcm_uframes_t 
snd_harmony_playback_pointer(snd_pcm_substream_t *ss)
{
	snd_pcm_runtime_t *rt = ss->runtime;
	harmony_t *h = snd_pcm_substream_chip(ss);
	unsigned long pcuradd;
	unsigned long played;

	if (!(h->st.playing) || (h->psubs == NULL)) 
		return 0;

	if ((h->pbuf.addr == 0) || (h->pbuf.size == 0))
		return 0;
	
	pcuradd = harmony_read(h, HARMONY_PCURADD);
	played = pcuradd - h->pbuf.addr;

#ifdef HARMONY_DEBUG
	printk(KERN_DEBUG PFX "playback_pointer is 0x%lx-0x%lx = %d bytes\n", 
	       pcuradd, h->pbuf.addr, played);	
#endif

	if (pcuradd > h->pbuf.addr + h->pbuf.size) {
		return 0;
	}

	return bytes_to_frames(rt, played);
}

static snd_pcm_uframes_t
snd_harmony_capture_pointer(snd_pcm_substream_t *ss)
{
        snd_pcm_runtime_t *rt = ss->runtime;
        harmony_t *h = snd_pcm_substream_chip(ss);
        unsigned long rcuradd;
        unsigned long caught;

        if (!(h->st.capturing) || (h->csubs == NULL))
                return 0;

        if ((h->cbuf.addr == 0) || (h->cbuf.size == 0))
                return 0;

        rcuradd = harmony_read(h, HARMONY_RCURADD);
        caught = rcuradd - h->cbuf.addr;

#ifdef HARMONY_DEBUG
        printk(KERN_DEBUG PFX "capture_pointer is 0x%lx-0x%lx = %d bytes\n",
               rcuradd, h->cbuf.addr, caught);
#endif

        if (rcuradd > h->cbuf.addr + h->cbuf.size) {
		return 0;
	}

        return bytes_to_frames(rt, caught);
}

static int 
snd_harmony_playback_open(snd_pcm_substream_t *ss)
{
	harmony_t *h = snd_pcm_substream_chip(ss);
	snd_pcm_runtime_t *rt = ss->runtime;
	int err;
	
	h->psubs = ss;
	rt->hw = snd_harmony_playback;
	snd_pcm_hw_constraint_list(rt, 0, SNDRV_PCM_HW_PARAM_RATE, 
				   &hw_constraint_rates);
	
	err = snd_pcm_hw_constraint_integer(rt, SNDRV_PCM_HW_PARAM_PERIODS);
	if (err < 0)
		return err;
	
	return 0;
}

static int
snd_harmony_capture_open(snd_pcm_substream_t *ss)
{
        harmony_t *h = snd_pcm_substream_chip(ss);
        snd_pcm_runtime_t *rt = ss->runtime;
        int err;

        h->csubs = ss;
        rt->hw = snd_harmony_capture;
        snd_pcm_hw_constraint_list(rt, 0, SNDRV_PCM_HW_PARAM_RATE,
                                   &hw_constraint_rates);

        err = snd_pcm_hw_constraint_integer(rt, SNDRV_PCM_HW_PARAM_PERIODS);
        if (err < 0)
                return err;

        return 0;
}

static int 
snd_harmony_playback_close(snd_pcm_substream_t *ss)
{
	harmony_t *h = snd_pcm_substream_chip(ss);
	h->psubs = NULL;
	return 0;
}

static int
snd_harmony_capture_close(snd_pcm_substream_t *ss)
{
        harmony_t *h = snd_pcm_substream_chip(ss);
        h->csubs = NULL;
        return 0;
}

static int 
snd_harmony_hw_params(snd_pcm_substream_t *ss,
		      snd_pcm_hw_params_t *hw)
{
	int err;
	harmony_t *h = snd_pcm_substream_chip(ss);
	
	err = snd_pcm_lib_malloc_pages(ss, params_buffer_bytes(hw));
	if (err > 0 && h->dma.type == SNDRV_DMA_TYPE_CONTINUOUS)
		ss->runtime->dma_addr = __pa(ss->runtime->dma_area);
	
	return err;
}

static int 
snd_harmony_hw_free(snd_pcm_substream_t *ss) 
{
	return snd_pcm_lib_free_pages(ss);
}

static snd_pcm_ops_t snd_harmony_playback_ops = {
	.open =	snd_harmony_playback_open,
	.close = snd_harmony_playback_close,
	.ioctl = snd_pcm_lib_ioctl,
	.hw_params = snd_harmony_hw_params,
	.hw_free = snd_harmony_hw_free,
	.prepare = snd_harmony_playback_prepare,
	.trigger = snd_harmony_playback_trigger,
 	.pointer = snd_harmony_playback_pointer,
};

static snd_pcm_ops_t snd_harmony_capture_ops = {
        .open = snd_harmony_capture_open,
        .close = snd_harmony_capture_close,
        .ioctl = snd_pcm_lib_ioctl,
        .hw_params = snd_harmony_hw_params,
        .hw_free = snd_harmony_hw_free,
        .prepare = snd_harmony_capture_prepare,
        .trigger = snd_harmony_capture_trigger,
        .pointer = snd_harmony_capture_pointer,
};

static int 
snd_harmony_pcm_init(harmony_t *h)
{
	snd_pcm_t *pcm;
	int err;

	harmony_disable_interrupts(h);
	
   	err = snd_pcm_new(h->card, "harmony", 0, 1, 1, &pcm);
	if (err < 0)
		return err;
	
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, 
			&snd_harmony_playback_ops);
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
			&snd_harmony_capture_ops);

	pcm->private_data = h;
	pcm->info_flags = 0;
	strcpy(pcm->name, "harmony");
	h->pcm = pcm;

	h->psubs = NULL;
	h->csubs = NULL;
	
	/* initialize graveyard buffer */
	h->dma.type = SNDRV_DMA_TYPE_DEV;
	h->dma.dev = &h->dev->dev;
	err = snd_dma_alloc_pages(h->dma.type,
				  h->dma.dev,
				  BUF_SIZE*GRAVEYARD_BUFS,
				  &h->gdma);
	if (err < 0) {
		printk(KERN_ERR PFX "cannot allocate graveyard buffer!\n");
		return err;
	}
	
	/* initialize silence buffers */
	err = snd_dma_alloc_pages(h->dma.type,
				  h->dma.dev,
				  BUF_SIZE*SILENCE_BUFS,
				  &h->sdma);
	if (err < 0) {
		printk(KERN_ERR PFX "cannot allocate silence buffer!\n");
		return err;
	}

	/* pre-allocate space for DMA */
	err = snd_pcm_lib_preallocate_pages_for_all(pcm, h->dma.type,
						    h->dma.dev,
						    MAX_BUF_SIZE, 
						    MAX_BUF_SIZE);
	if (err < 0) {
		printk(KERN_ERR PFX "buffer allocation error: %d\n", err);
		return err;
	}

	h->st.format = snd_harmony_set_data_format(h,
		SNDRV_PCM_FORMAT_S16_BE, 1);

	return 0;
}

static void 
snd_harmony_set_new_gain(harmony_t *h)
{
 	harmony_wait_for_control(h);
	harmony_write(h, HARMONY_GAINCTL, h->st.gain);
}

static int 
snd_harmony_mixercontrol_info(snd_kcontrol_t *kc, 
			      snd_ctl_elem_info_t *uinfo)
{
	int mask = (kc->private_value >> 16) & 0xff;
	int left_shift = (kc->private_value) & 0xff;
	int right_shift = (kc->private_value >> 8) & 0xff;
	
	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : 
		       SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = left_shift == right_shift ? 1 : 2;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = mask;

	return 0;
}

static int 
snd_harmony_volume_get(snd_kcontrol_t *kc, 
		       snd_ctl_elem_value_t *ucontrol)
{
	harmony_t *h = snd_kcontrol_chip(kc);
	int shift_left = (kc->private_value) & 0xff;
	int shift_right = (kc->private_value >> 8) & 0xff;
	int mask = (kc->private_value >> 16) & 0xff;
	int invert = (kc->private_value >> 24) & 0xff;
	int left, right;
	unsigned long flags;
	
	spin_lock_irqsave(&h->mixer_lock, flags);

	left = (h->st.gain >> shift_left) & mask;
	right = (h->st.gain >> shift_right) & mask;

	if (invert) {
		left = mask - left;
		right = mask - right;
	}
	ucontrol->value.integer.value[0] = left;
	ucontrol->value.integer.value[1] = right;

	spin_unlock_irqrestore(&h->mixer_lock, flags);

	return 0;
}  

static int 
snd_harmony_volume_put(snd_kcontrol_t *kc, 
		       snd_ctl_elem_value_t *ucontrol)
{
	harmony_t *h = snd_kcontrol_chip(kc);
	int shift_left = (kc->private_value) & 0xff;
	int shift_right = (kc->private_value >> 8) & 0xff;
	int mask = (kc->private_value >> 16) & 0xff;
	int invert = (kc->private_value >> 24) & 0xff;
	int left, right;
	int old_gain = h->st.gain;
	unsigned long flags;
	
	left = ucontrol->value.integer.value[0] & mask;
	right = ucontrol->value.integer.value[1] & mask;
	if (invert) {
		left = mask - left;
		right = mask - right;
	}
	
	spin_lock_irqsave(&h->mixer_lock, flags);

	h->st.gain &= ~( (mask << shift_right) | (mask << shift_left) );
 	h->st.gain |=  ( (left << shift_left) | (right << shift_right) );
	snd_harmony_set_new_gain(h);

	spin_unlock_irqrestore(&h->mixer_lock, flags);
	
	return (old_gain - h->st.gain);
}

#define HARMONY_CONTROLS (sizeof(snd_harmony_controls)/ \
                          sizeof(snd_kcontrol_new_t))

#define HARMONY_VOLUME(xname, left_shift, right_shift, mask, invert) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,                \
  .info = snd_harmony_mixercontrol_info,                             \
  .get = snd_harmony_volume_get, .put = snd_harmony_volume_put,      \
  .private_value = ((left_shift) | ((right_shift) << 8) |            \
                   ((mask) << 16) | ((invert) << 24)) }

static snd_kcontrol_new_t snd_harmony_controls[] = {
	HARMONY_VOLUME("Playback Volume", HARMONY_GAIN_LO_SHIFT, 
		       HARMONY_GAIN_RO_SHIFT, HARMONY_GAIN_OUT, 1),
	HARMONY_VOLUME("Capture Volume", HARMONY_GAIN_LI_SHIFT,
		       HARMONY_GAIN_RI_SHIFT, HARMONY_GAIN_IN, 0),
};

static void __init 
snd_harmony_mixer_reset(harmony_t *h)
{
	harmony_mute(h);
	harmony_reset(h);
	h->st.gain = HARMONY_GAIN_DEFAULT;
	harmony_unmute(h);
}

static int __init 
snd_harmony_mixer_init(harmony_t *h)
{
	snd_card_t *card = h->card;
	int idx, err;

	snd_assert(h != NULL, return -EINVAL);
	strcpy(card->mixername, "Harmony Gain control interface");

	for (idx = 0; idx < HARMONY_CONTROLS; idx++) {
		err = snd_ctl_add(card, 
				  snd_ctl_new1(&snd_harmony_controls[idx], h));
		if (err < 0)
			return err;
	}
	
	snd_harmony_mixer_reset(h);

	return 0;
}

static int
snd_harmony_free(harmony_t *h)
{
        if (h->gdma.addr)
                snd_dma_free_pages(&h->gdma);
        if (h->sdma.addr)
                snd_dma_free_pages(&h->sdma);

	if (h->irq >= 0)
		free_irq(h->irq, h);

	if (h->iobase)
		iounmap(h->iobase);

	parisc_set_drvdata(h->dev, NULL);

	kfree(h);
	return 0;
}

static int
snd_harmony_dev_free(snd_device_t *dev)
{
	harmony_t *h = dev->device_data;
	return snd_harmony_free(h);
}

static int __devinit
snd_harmony_create(snd_card_t *card, 
		   struct parisc_device *padev, 
		   harmony_t **rchip)
{
	int err;
	harmony_t *h;
	static snd_device_ops_t ops = {
		.dev_free = snd_harmony_dev_free,
	};

	*rchip = NULL;

	h = kmalloc(sizeof(*h), GFP_KERNEL);
	if (h == NULL)
		return -ENOMEM;

	memset(&h->st, 0, sizeof(h->st));
	memset(&h->stats, 0, sizeof(h->stats));
	memset(&h->pbuf, 0, sizeof(h->pbuf));
	memset(&h->cbuf, 0, sizeof(h->cbuf));

	h->hpa = padev->hpa.start;
	h->card = card;
	h->dev = padev;
	h->irq = padev->irq;
	h->iobase = ioremap_nocache(padev->hpa.start, HARMONY_SIZE);
	if (h->iobase == NULL) {
		printk(KERN_ERR PFX "unable to remap hpa 0x%lx\n",
		       padev->hpa.start);
		err = -EBUSY;
		goto free_and_ret;
	}
		
	err = request_irq(h->irq, snd_harmony_interrupt, 0,
			  "harmony", h);
	if (err) {
		printk(KERN_ERR PFX "could not obtain interrupt %d",
		       h->irq);
		goto free_and_ret;
	}

	spin_lock_init(&h->mixer_lock);
	spin_lock_init(&h->lock);

        if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
                                  h, &ops)) < 0) {
                goto free_and_ret;
        }

	snd_card_set_dev(card, &padev->dev);

	*rchip = h;

	return 0;

free_and_ret:
	snd_harmony_free(h);
	return err;
}

static int __devinit
snd_harmony_probe(struct parisc_device *padev)
{
	int err;
	static int dev;
	snd_card_t *card;
	harmony_t *h;
	static int index = SNDRV_DEFAULT_IDX1;
	static char *id = SNDRV_DEFAULT_STR1;

	h = parisc_get_drvdata(padev);
	if (h != NULL) {
		return -ENODEV;
	}

	card = snd_card_new(index, id, THIS_MODULE, 0);
	if (card == NULL)
		return -ENOMEM;

	err = snd_harmony_create(card, padev, &h);
	if (err < 0) {
		goto free_and_ret;
	}

	err = snd_harmony_pcm_init(h);
	if (err < 0) {
		goto free_and_ret;
	}

	err = snd_harmony_mixer_init(h);
	if (err < 0) {
		goto free_and_ret;
	}

	strcpy(card->driver, "harmony");
	strcpy(card->shortname, "Harmony");
	sprintf(card->longname, "%s at 0x%lx, irq %i",
		card->shortname, h->hpa, h->irq);

	err = snd_card_register(card);
	if (err < 0) {
		goto free_and_ret;
	}

	dev++;
	parisc_set_drvdata(padev, h);

	return 0;

free_and_ret:
	snd_card_free(card);
	return err;
}

static int __devexit
snd_harmony_remove(struct parisc_device *padev)
{
	harmony_t *h = parisc_get_drvdata(padev);
	snd_card_free(h->card);
	return 0;
}

static struct parisc_driver snd_harmony_driver = {
	.name = "harmony",
	.id_table = snd_harmony_devtable,
	.probe = snd_harmony_probe,
	.remove = snd_harmony_remove,
};

static int __init 
alsa_harmony_init(void)
{
	int err;

	err = register_parisc_driver(&snd_harmony_driver);
	if (err < 0) {
		printk(KERN_ERR PFX "device not found\n");
		return err;
	}

	return 0;
}

static void __exit
alsa_harmony_fini(void)
{
	int err;

	err = unregister_parisc_driver(&snd_harmony_driver);
	if (err < 0) {
		printk(KERN_ERR PFX "failed to unregister\n");
	}
	
	return;
}

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Kyle McMartin <kyle@parisc-linux.org>");
MODULE_DESCRIPTION("Harmony sound driver");

module_init(alsa_harmony_init);
module_exit(alsa_harmony_fini);